Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,47 @@ static void hoistConditionalLoadsStores(
}
}

static bool hoistImplyingConditions(BranchInst *BI, IRBuilder<> &Builder,
const DataLayout &DL) {
if (!isa<ICmpInst>(BI->getCondition()))
return false;

ICmpInst *branchCond = cast<ICmpInst>(BI->getCondition());
BasicBlock *truePathBB = BI->getSuccessor(0);

for (auto &I : *truePathBB)
if (I.mayHaveSideEffects())
return false;

for (auto &I : *truePathBB) {
if (isa<ICmpInst>(I)) {
ICmpInst *impliedICmp = cast<ICmpInst>(&I);
if (impliedICmp->getPredicate() == branchCond->getPredicate() &&
impliedICmp->getOperand(0) == branchCond->getOperand(0) &&
impliedICmp->getOperand(1) == branchCond->getOperand(1)) {
// found the same condition, so we can skip processing this.
continue;
}

std::optional<bool> Imp = isImpliedCondition(impliedICmp, branchCond, DL);
if (Imp == true) {
Builder.SetInsertPoint(BI);
Value *newBranchCond = Builder.CreateICmp(impliedICmp->getPredicate(),
impliedICmp->getOperand(0),
impliedICmp->getOperand(1));

branchCond->replaceAllUsesWith(newBranchCond);
branchCond->eraseFromParent();
impliedICmp->replaceAllUsesWith(
ConstantInt::getTrue(truePathBB->getContext()));
return true;
}
}
}

return false;
}

static bool isSafeCheapLoadStore(const Instruction *I,
const TargetTransformInfo &TTI) {
// Not handle volatile or atomic.
Expand Down Expand Up @@ -8121,6 +8162,9 @@ bool SimplifyCFGOpt::simplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
if (simplifyBranchOnICmpChain(BI, Builder, DL))
return true;

if (hoistImplyingConditions(BI, Builder, DL))
return requestResimplify();

// If this basic block has dominating predecessor blocks and the dominating
// blocks' conditions imply BI's condition, we know the direction of BI.
std::optional<bool> Imp = isImpliedByDomCondition(BI->getCondition(), BI, DL);
Expand Down
Loading
Loading