Skip to content

Commit f76d584

Browse files
committed
Const correctness changes and AArch64 option
1 parent bd9787c commit f76d584

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);
@@ -1112,15 +1113,15 @@ bool SelectOptimizeImpl::computeLoopCosts(
11121113
DenseMap<const Instruction *, CostInfo> &InstCostMap, CostInfo *LoopCost) {
11131114
LLVM_DEBUG(dbgs() << "Calculating Latency / IPredCost / INonPredCost of loop "
11141115
<< L->getHeader()->getName() << "\n");
1115-
const auto &SIset = getSIset(SIGroups);
1116+
const auto &SImap = getSImap(SIGroups);
11161117
// Compute instruction and loop-critical-path costs across two iterations for
11171118
// both predicated and non-predicated version.
11181119
const unsigned Iterations = 2;
11191120
for (unsigned Iter = 0; Iter < Iterations; ++Iter) {
11201121
// Cost of the loop's critical path.
11211122
CostInfo &MaxCost = LoopCost[Iter];
11221123
for (BasicBlock *BB : L->getBlocks()) {
1123-
for (Instruction &I : *BB) {
1124+
for (const Instruction &I : *BB) {
11241125
if (I.isDebugOrPseudoInst())
11251126
continue;
11261127
// Compute the predicated and non-predicated cost of the instruction.
@@ -1157,10 +1158,8 @@ bool SelectOptimizeImpl::computeLoopCosts(
11571158
// BranchCost = PredictedPathCost + MispredictCost
11581159
// PredictedPathCost = TrueOpCost * TrueProb + FalseOpCost * FalseProb
11591160
// MispredictCost = max(MispredictPenalty, CondCost) * MispredictRate
1160-
if (SIset.contains(&I)) {
1161-
auto SI = SelectLike::match(&I);
1162-
assert(SI && "Expected to match an existing SelectLike");
1163-
1161+
if (SImap.contains(&I)) {
1162+
auto SI = SImap.at(&I);
11641163
Scaled64 TrueOpCost = SI.getTrueOpCost(InstCostMap, TTI);
11651164
Scaled64 FalseOpCost = SI.getFalseOpCost(InstCostMap, TTI);
11661165
Scaled64 PredictedPathCost =
@@ -1189,13 +1188,13 @@ bool SelectOptimizeImpl::computeLoopCosts(
11891188
return true;
11901189
}
11911190

1192-
SmallPtrSet<const Instruction *, 2>
1193-
SelectOptimizeImpl::getSIset(const SelectGroups &SIGroups) {
1194-
SmallPtrSet<const Instruction *, 2> SIset;
1191+
SmallDenseMap<const Instruction *, SelectOptimizeImpl::SelectLike, 2>
1192+
SelectOptimizeImpl::getSImap(const SelectGroups &SIGroups) {
1193+
SmallDenseMap<const Instruction *, SelectLike, 2> SImap;
11951194
for (const SelectGroup &ASI : SIGroups)
11961195
for (SelectLike SI : ASI)
1197-
SIset.insert(SI.getI());
1198-
return SIset;
1196+
SImap.try_emplace(SI.getI(), SI);
1197+
return SImap;
11991198
}
12001199

12011200
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=,
@@ -4048,3 +4051,15 @@ AArch64TTIImpl::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
40484051
return AM.Scale != 0 && AM.Scale != 1;
40494052
return -1;
40504053
}
4054+
4055+
bool AArch64TTIImpl::shouldTreatInstructionLikeSelect(const Instruction *I) {
4056+
// For the binary operators (e.g. or) we need to be more careful than
4057+
// selects, here we only transform them if they are already at a natural
4058+
// break point in the code - the end of a block with an unconditional
4059+
// terminator.
4060+
if (EnableOrLikeSelectOpt && I->getOpcode() == Instruction::Or &&
4061+
isa<BranchInst>(I->getNextNode()) &&
4062+
cast<BranchInst>(I->getNextNode())->isUnconditional())
4063+
return true;
4064+
return BaseT::shouldTreatInstructionLikeSelect(I);
4065+
}

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)