Skip to content

Commit eab7f12

Browse files
committed
Const correctness changes and AArch64 option
1 parent 56fe77d commit eab7f12

File tree

6 files changed

+32
-28
lines changed

6 files changed

+32
-28
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ class TargetTransformInfo {
938938
/// select, potentially converting it to a conditional branch. This can
939939
/// include select-like instructions like or(zext(c), x) that can be converted
940940
/// to selects.
941-
bool shouldTreatInstructionLikeSelect(Instruction *I) const;
941+
bool shouldTreatInstructionLikeSelect(const Instruction *I) const;
942942

943943
/// Enable matching of interleaved access groups.
944944
bool enableInterleavedAccessVectorization() const;
@@ -1884,7 +1884,7 @@ class TargetTransformInfo::Concept {
18841884
virtual MemCmpExpansionOptions
18851885
enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const = 0;
18861886
virtual bool enableSelectOptimize() = 0;
1887-
virtual bool shouldTreatInstructionLikeSelect(Instruction *I) = 0;
1887+
virtual bool shouldTreatInstructionLikeSelect(const Instruction *I) = 0;
18881888
virtual bool enableInterleavedAccessVectorization() = 0;
18891889
virtual bool enableMaskedInterleavedAccessVectorization() = 0;
18901890
virtual bool isFPVectorizationPotentiallyUnsafe() = 0;
@@ -2422,7 +2422,7 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
24222422
bool enableSelectOptimize() override {
24232423
return Impl.enableSelectOptimize();
24242424
}
2425-
bool shouldTreatInstructionLikeSelect(Instruction *I) override {
2425+
bool shouldTreatInstructionLikeSelect(const Instruction *I) override {
24262426
return Impl.shouldTreatInstructionLikeSelect(I);
24272427
}
24282428
bool enableInterleavedAccessVectorization() override {

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class TargetTransformInfoImplBase {
378378

379379
bool enableSelectOptimize() const { return true; }
380380

381-
bool shouldTreatInstructionLikeSelect(Instruction *I) {
381+
bool shouldTreatInstructionLikeSelect(const Instruction *I) {
382382
// If the select is a logical-and/logical-or then it is better treated as a
383383
// and/or by the backend.
384384
using namespace llvm::PatternMatch;

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ bool TargetTransformInfo::enableSelectOptimize() const {
605605
}
606606

607607
bool TargetTransformInfo::shouldTreatInstructionLikeSelect(
608-
Instruction *I) const {
608+
const Instruction *I) const {
609609
return TTIImpl->shouldTreatInstructionLikeSelect(I);
610610
}
611611

llvm/lib/CodeGen/SelectOptimize.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ class SelectOptimizeImpl {
318318
CostInfo *LoopCost);
319319

320320
// Returns a set of all the select instructions in the given select groups.
321-
SmallPtrSet<const Instruction *, 2> getSIset(const SelectGroups &SIGroups);
321+
SmallDenseMap<const Instruction *, SelectLike, 2>
322+
getSImap(const SelectGroups &SIGroups);
322323

323324
// Returns the latency cost of a given instruction.
324325
std::optional<uint64_t> computeInstCost(const Instruction *I);
@@ -1095,15 +1096,15 @@ bool SelectOptimizeImpl::computeLoopCosts(
10951096
DenseMap<const Instruction *, CostInfo> &InstCostMap, CostInfo *LoopCost) {
10961097
LLVM_DEBUG(dbgs() << "Calculating Latency / IPredCost / INonPredCost of loop "
10971098
<< L->getHeader()->getName() << "\n");
1098-
const auto &SIset = getSIset(SIGroups);
1099+
const auto &SImap = getSImap(SIGroups);
10991100
// Compute instruction and loop-critical-path costs across two iterations for
11001101
// both predicated and non-predicated version.
11011102
const unsigned Iterations = 2;
11021103
for (unsigned Iter = 0; Iter < Iterations; ++Iter) {
11031104
// Cost of the loop's critical path.
11041105
CostInfo &MaxCost = LoopCost[Iter];
11051106
for (BasicBlock *BB : L->getBlocks()) {
1106-
for (Instruction &I : *BB) {
1107+
for (const Instruction &I : *BB) {
11071108
if (I.isDebugOrPseudoInst())
11081109
continue;
11091110
// Compute the predicated and non-predicated cost of the instruction.
@@ -1140,10 +1141,8 @@ bool SelectOptimizeImpl::computeLoopCosts(
11401141
// BranchCost = PredictedPathCost + MispredictCost
11411142
// PredictedPathCost = TrueOpCost * TrueProb + FalseOpCost * FalseProb
11421143
// MispredictCost = max(MispredictPenalty, CondCost) * MispredictRate
1143-
if (SIset.contains(&I)) {
1144-
auto SI = SelectLike::match(&I);
1145-
assert(SI && "Expected to match an existing SelectLike");
1146-
1144+
if (SImap.contains(&I)) {
1145+
auto SI = SImap.at(&I);
11471146
Scaled64 TrueOpCost = SI.getTrueOpCost(InstCostMap, TTI);
11481147
Scaled64 FalseOpCost = SI.getFalseOpCost(InstCostMap, TTI);
11491148
Scaled64 PredictedPathCost =
@@ -1172,13 +1171,13 @@ bool SelectOptimizeImpl::computeLoopCosts(
11721171
return true;
11731172
}
11741173

1175-
SmallPtrSet<const Instruction *, 2>
1176-
SelectOptimizeImpl::getSIset(const SelectGroups &SIGroups) {
1177-
SmallPtrSet<const Instruction *, 2> SIset;
1174+
SmallDenseMap<const Instruction *, SelectOptimizeImpl::SelectLike, 2>
1175+
SelectOptimizeImpl::getSImap(const SelectGroups &SIGroups) {
1176+
SmallDenseMap<const Instruction *, SelectLike, 2> SImap;
11781177
for (const SelectGroup &ASI : SIGroups)
11791178
for (SelectLike SI : ASI)
1180-
SIset.insert(SI.getI());
1181-
return SIset;
1179+
SImap.try_emplace(SI.getI(), SI);
1180+
return SImap;
11821181
}
11831182

11841183
std::optional<uint64_t>

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ static cl::opt<unsigned> InlineCallPenaltyChangeSM(
5555
"inline-call-penalty-sm-change", cl::init(10), cl::Hidden,
5656
cl::desc("Penalty of inlining a call that requires a change to PSTATE.SM"));
5757

58+
static cl::opt<bool> EnableOrLikeSelectOpt("enable-aarch64-or-like-select",
59+
cl::init(true), cl::Hidden);
60+
5861
namespace {
5962
class TailFoldingOption {
6063
// These bitfields will only ever be set to something non-zero in operator=,
@@ -4047,3 +4050,15 @@ AArch64TTIImpl::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
40474050
return AM.Scale != 0 && AM.Scale != 1;
40484051
return -1;
40494052
}
4053+
4054+
bool AArch64TTIImpl::shouldTreatInstructionLikeSelect(const Instruction *I) {
4055+
// For the binary operators (e.g. or) we need to be more careful than
4056+
// selects, here we only transform them if they are already at a natural
4057+
// break point in the code - the end of a block with an unconditional
4058+
// terminator.
4059+
if (EnableOrLikeSelectOpt && I->getOpcode() == Instruction::Or &&
4060+
isa<BranchInst>(I->getNextNode()) &&
4061+
cast<BranchInst>(I->getNextNode())->isUnconditional())
4062+
return true;
4063+
return BaseT::shouldTreatInstructionLikeSelect(I);
4064+
}

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -412,17 +412,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
412412

413413
bool enableSelectOptimize() { return ST->enableSelectOptimize(); }
414414

415-
bool shouldTreatInstructionLikeSelect(Instruction *I) {
416-
// For the binary operators (e.g. or) we need to be more careful than
417-
// selects, here we only transform them if they are already at a natural
418-
// break point in the code - the end of a block with an unconditional
419-
// terminator.
420-
if (I->getOpcode() == Instruction::Or &&
421-
isa<BranchInst>(I->getNextNode()) &&
422-
cast<BranchInst>(I->getNextNode())->isUnconditional())
423-
return true;
424-
return BaseT::shouldTreatInstructionLikeSelect(I);
425-
}
415+
bool shouldTreatInstructionLikeSelect(const Instruction *I);
426416

427417
unsigned getStoreMinimumVF(unsigned VF, Type *ScalarMemTy,
428418
Type *ScalarValTy) const {

0 commit comments

Comments
 (0)