Skip to content

Commit

Permalink
[SelectOpt] Add handling for not conditions.
Browse files Browse the repository at this point in the history
This patch attempts to help the SelectOpt pass detect select groups made up of
conditions and not(conditions). Usually these are canonicalized in instcombine
to remove the not and invert the true/false values, but this will not happen
for Loginal operations, which can be beneficial to convert if they are part of
a larger select group. The handling for not's are mostly handled in the
SelectLike, which can be marked as Inverted in order to reverse the TrueValue
and FalseValue.

This helps fix a regression in fortran minloc constructs, after #84628 helped
simplify a loop with branches into a loop with selects.
  • Loading branch information
davemgreen committed May 17, 2024
1 parent 4349ffb commit dc2e4cd
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 122 deletions.
76 changes: 60 additions & 16 deletions llvm/lib/CodeGen/SelectOptimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ class SelectOptimizeImpl {
class SelectLike {
SelectLike(Instruction *I) : I(I) {}

/// The select (/or) instruction.
Instruction *I;
/// Whether this select is inverted, "not(cond), FalseVal, TrueVal", as
/// opposed to the original condition.
bool Inverted = false;

public:
/// Match a select or select-like instruction, returning a SelectLike.
Expand All @@ -153,14 +157,22 @@ class SelectOptimizeImpl {
bool isValid() { return I; }
operator bool() { return isValid(); }

/// Invert the select by inverting the condition and switching the operands.
void setInverted() {
assert(!Inverted && "Trying to invert and inverted SelectLike");
assert(isa<Instruction>(getCondition()) &&
cast<Instruction>(getCondition())->getOpcode() ==
Instruction::Xor);
Inverted = true;
}
bool isInverted() const { return Inverted; }

Instruction *getI() { return I; }
const Instruction *getI() const { return I; }

Type *getType() const { return I->getType(); }

/// Return the condition for the SelectLike instruction. For example the
/// condition of a select or c in `or(zext(c), x)`
Value *getCondition() const {
Value *getNonInvertedCondition() const {
if (auto *Sel = dyn_cast<SelectInst>(I))
return Sel->getCondition();
// Or(zext) case
Expand All @@ -177,11 +189,22 @@ class SelectOptimizeImpl {
llvm_unreachable("Unhandled case in getCondition");
}

/// Return the condition for the SelectLike instruction. For example the
/// condition of a select or c in `or(zext(c), x)`
Value *getCondition() const {
Value *CC = getNonInvertedCondition();
if (Inverted)
return cast<Instruction>(CC)->getOperand(0);
return CC;
}

/// Return the true value for the SelectLike instruction. Note this may not
/// exist for all SelectLike instructions. For example, for `or(zext(c), x)`
/// the true value would be `or(x,1)`. As this value does not exist, nullptr
/// is returned.
Value *getTrueValue() const {
Value *getTrueValue(bool HonorInverts = true) const {
if (Inverted && HonorInverts)
return getFalseValue(false);
if (auto *Sel = dyn_cast<SelectInst>(I))
return Sel->getTrueValue();
// Or(zext) case - The true value is Or(X), so return nullptr as the value
Expand All @@ -195,7 +218,9 @@ class SelectOptimizeImpl {
/// Return the false value for the SelectLike instruction. For example the
/// getFalseValue of a select or `x` in `or(zext(c), x)` (which is
/// `select(c, x|1, x)`)
Value *getFalseValue() const {
Value *getFalseValue(bool HonorInverts = true) const {
if (Inverted && HonorInverts)
return getTrueValue(false);
if (auto *Sel = dyn_cast<SelectInst>(I))
return Sel->getFalseValue();
// Or(zext) case - return the operand which is not the zext.
Expand All @@ -216,8 +241,8 @@ class SelectOptimizeImpl {
/// InstCostMap. This may need to be generated for select-like instructions.
Scaled64 getTrueOpCost(DenseMap<const Instruction *, CostInfo> &InstCostMap,
const TargetTransformInfo *TTI) {
if (auto *Sel = dyn_cast<SelectInst>(I))
if (auto *I = dyn_cast<Instruction>(Sel->getTrueValue()))
if (isa<SelectInst>(I))
if (auto *I = dyn_cast<Instruction>(getTrueValue()))
return InstCostMap.contains(I) ? InstCostMap[I].NonPredCost
: Scaled64::getZero();

Expand All @@ -242,8 +267,8 @@ class SelectOptimizeImpl {
Scaled64
getFalseOpCost(DenseMap<const Instruction *, CostInfo> &InstCostMap,
const TargetTransformInfo *TTI) {
if (auto *Sel = dyn_cast<SelectInst>(I))
if (auto *I = dyn_cast<Instruction>(Sel->getFalseValue()))
if (isa<SelectInst>(I))
if (auto *I = dyn_cast<Instruction>(getFalseValue()))
return InstCostMap.contains(I) ? InstCostMap[I].NonPredCost
: Scaled64::getZero();

Expand Down Expand Up @@ -510,9 +535,10 @@ getTrueOrFalseValue(SelectOptimizeImpl::SelectLike SI, bool isTrue,
for (SelectInst *DefSI = dyn_cast<SelectInst>(SI.getI());
DefSI != nullptr && Selects.count(DefSI);
DefSI = dyn_cast<SelectInst>(V)) {
assert(DefSI->getCondition() == SI.getCondition() &&
"The condition of DefSI does not match with SI");
V = (isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue());
if (DefSI->getCondition() == SI.getCondition())
V = (isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue());
else // Handle inverted SI
V = (!isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue());
}

if (isa<BinaryOperator>(SI.getI())) {
Expand Down Expand Up @@ -634,16 +660,17 @@ void SelectOptimizeImpl::convertProfitableSIGroups(SelectGroups &ProfSIGroups) {

// Move any debug/pseudo instructions that were in-between the select
// group to the newly-created end block.
SmallVector<Instruction *, 2> DebugPseudoINS;
SmallVector<Instruction *, 2> SinkInstrs;
auto DIt = SI.getI()->getIterator();
while (&*DIt != LastSI.getI()) {
if (DIt->isDebugOrPseudoInst())
DebugPseudoINS.push_back(&*DIt);
SinkInstrs.push_back(&*DIt);
if (match(&*DIt, m_Not(m_Specific(SI.getCondition()))))
SinkInstrs.push_back(&*DIt);
DIt++;
}
for (auto *DI : DebugPseudoINS) {
for (auto *DI : SinkInstrs)
DI->moveBeforePreserving(&*EndBlock->getFirstInsertionPt());
}

// Duplicate implementation for DbgRecords, the non-instruction debug-info
// format. Helper lambda for moving DbgRecords to the end block.
Expand Down Expand Up @@ -765,6 +792,13 @@ void SelectOptimizeImpl::collectSelectGroups(BasicBlock &BB,
++BBIt;
continue;
}

// Skip not(select, if the not is part of the same select group
if (match(NI, m_Not(m_Specific(SI.getCondition())))) {
++BBIt;
continue;
}

// We only allow selects in the same group, not other select-like
// instructions.
if (!isa<SelectInst>(NI))
Expand All @@ -773,6 +807,10 @@ void SelectOptimizeImpl::collectSelectGroups(BasicBlock &BB,
SelectLike NSI = SelectLike::match(NI);
if (NSI && SI.getCondition() == NSI.getCondition()) {
SIGroup.push_back(NSI);
} else if (NSI && match(NSI.getCondition(),
m_Not(m_Specific(SI.getCondition())))) {
NSI.setInverted();
SIGroup.push_back(NSI);
} else
break;
++BBIt;
Expand All @@ -783,6 +821,12 @@ void SelectOptimizeImpl::collectSelectGroups(BasicBlock &BB,
if (!isSelectKindSupported(SI))
continue;

LLVM_DEBUG({
dbgs() << "New Select group with\n";
for (auto SI : SIGroup)
dbgs() << " " << *SI.getI() << "\n";
});

SIGroups.push_back(SIGroup);
}
}
Expand Down
Loading

0 comments on commit dc2e4cd

Please sign in to comment.