Skip to content

Commit a2d68b4

Browse files
authored
[SelectOpt] Add handling for Select-like operations. (#77284)
Some operations behave like selects. For example `or(zext(c), y)` is the same as select(c, y|1, y)` and instcombine can canonicalize the select to the or form. These operations can still be worthwhile converting to branch as opposed to keeping as a select or or instruction. This patch attempts to add some basic handling for them, creating a SelectLike abstraction in the select optimization pass. The backend can opt into handling `or(zext(c),x)` as a select if it could be profitable, and the select optimization pass attempts to handle them in much the same way as a `select(c, x|1, x)`. The Or(x, 1) may need to be added as a new instruction, generated as the or is converted to branches. This helps fix a regression from selects being converted to or's recently.
1 parent d922c82 commit a2d68b4

File tree

7 files changed

+339
-128
lines changed

7 files changed

+339
-128
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,12 @@ class TargetTransformInfo {
934934
/// Should the Select Optimization pass be enabled and ran.
935935
bool enableSelectOptimize() const;
936936

937+
/// Should the Select Optimization pass treat the given instruction like a
938+
/// select, potentially converting it to a conditional branch. This can
939+
/// include select-like instructions like or(zext(c), x) that can be converted
940+
/// to selects.
941+
bool shouldTreatInstructionLikeSelect(const Instruction *I) const;
942+
937943
/// Enable matching of interleaved access groups.
938944
bool enableInterleavedAccessVectorization() const;
939945

@@ -1878,6 +1884,7 @@ class TargetTransformInfo::Concept {
18781884
virtual MemCmpExpansionOptions
18791885
enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const = 0;
18801886
virtual bool enableSelectOptimize() = 0;
1887+
virtual bool shouldTreatInstructionLikeSelect(const Instruction *I) = 0;
18811888
virtual bool enableInterleavedAccessVectorization() = 0;
18821889
virtual bool enableMaskedInterleavedAccessVectorization() = 0;
18831890
virtual bool isFPVectorizationPotentiallyUnsafe() = 0;
@@ -2415,6 +2422,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
24152422
bool enableSelectOptimize() override {
24162423
return Impl.enableSelectOptimize();
24172424
}
2425+
bool shouldTreatInstructionLikeSelect(const Instruction *I) override {
2426+
return Impl.shouldTreatInstructionLikeSelect(I);
2427+
}
24182428
bool enableInterleavedAccessVectorization() override {
24192429
return Impl.enableInterleavedAccessVectorization();
24202430
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,15 @@ class TargetTransformInfoImplBase {
378378

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

381+
bool shouldTreatInstructionLikeSelect(const Instruction *I) {
382+
// If the select is a logical-and/logical-or then it is better treated as a
383+
// and/or by the backend.
384+
using namespace llvm::PatternMatch;
385+
return isa<SelectInst>(I) &&
386+
!match(I, m_CombineOr(m_LogicalAnd(m_Value(), m_Value()),
387+
m_LogicalOr(m_Value(), m_Value())));
388+
}
389+
381390
bool enableInterleavedAccessVectorization() const { return false; }
382391

383392
bool enableMaskedInterleavedAccessVectorization() const { return false; }

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,11 @@ bool TargetTransformInfo::enableSelectOptimize() const {
604604
return TTIImpl->enableSelectOptimize();
605605
}
606606

607+
bool TargetTransformInfo::shouldTreatInstructionLikeSelect(
608+
const Instruction *I) const {
609+
return TTIImpl->shouldTreatInstructionLikeSelect(I);
610+
}
611+
607612
bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
608613
return TTIImpl->enableInterleavedAccessVectorization();
609614
}

0 commit comments

Comments
 (0)