aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
diff options
context:
space:
mode:
authorAndreas Jonson <andjo403@hotmail.com>2025-08-17 09:53:40 +0200
committerGitHub <noreply@github.com>2025-08-17 09:53:40 +0200
commit5ae8a9b8cee3d4477fdec107a3ab29b633ec4f9f (patch)
tree1fb27097641c0ecc2787f40ed587643743f2db24 /llvm/lib/Transforms/Utils/SimplifyCFG.cpp
parente44784fb44bd00acc0ecd25537a359c3a1df8f17 (diff)
downloadllvm-5ae8a9b8cee3d4477fdec107a3ab29b633ec4f9f.zip
llvm-5ae8a9b8cee3d4477fdec107a3ab29b633ec4f9f.tar.gz
llvm-5ae8a9b8cee3d4477fdec107a3ab29b633ec4f9f.tar.bz2
[SimplifyCfg] Handle trunc nuw i1 condition in Equality comparison. (#153051)
proof: https://alive2.llvm.org/ce/z/WVt4-F
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyCFG.cpp30
1 files changed, 23 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1436e47..46d6c2a 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -810,11 +810,15 @@ Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
if (!SI->getParent()->hasNPredecessorsOrMore(128 / SI->getNumSuccessors()))
CV = SI->getCondition();
} else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
- if (BI->isConditional() && BI->getCondition()->hasOneUse())
+ if (BI->isConditional() && BI->getCondition()->hasOneUse()) {
if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
if (ICI->isEquality() && getConstantInt(ICI->getOperand(1), DL))
CV = ICI->getOperand(0);
+ } else if (auto *Trunc = dyn_cast<TruncInst>(BI->getCondition())) {
+ if (Trunc->hasNoUnsignedWrap())
+ CV = Trunc->getOperand(0);
}
+ }
// Unwrap any lossless ptrtoint cast.
if (CV) {
@@ -840,11 +844,20 @@ BasicBlock *SimplifyCFGOpt::getValueEqualityComparisonCases(
}
BranchInst *BI = cast<BranchInst>(TI);
- ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
- BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
- Cases.push_back(ValueEqualityComparisonCase(
- getConstantInt(ICI->getOperand(1), DL), Succ));
- return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
+ Value *Cond = BI->getCondition();
+ ICmpInst::Predicate Pred;
+ ConstantInt *C;
+ if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
+ Pred = ICI->getPredicate();
+ C = getConstantInt(ICI->getOperand(1), DL);
+ } else {
+ Pred = ICmpInst::ICMP_NE;
+ auto *Trunc = cast<TruncInst>(Cond);
+ C = ConstantInt::get(cast<IntegerType>(Trunc->getOperand(0)->getType()), 0);
+ }
+ BasicBlock *Succ = BI->getSuccessor(Pred == ICmpInst::ICMP_NE);
+ Cases.push_back(ValueEqualityComparisonCase(C, Succ));
+ return BI->getSuccessor(Pred == ICmpInst::ICMP_EQ);
}
/// Given a vector of bb/value pairs, remove any entries
@@ -1106,7 +1119,10 @@ static void getBranchWeights(Instruction *TI,
// default weight to be the first entry.
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
assert(Weights.size() == 2);
- ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
+ auto *ICI = dyn_cast<ICmpInst>(BI->getCondition());
+ if (!ICI)
+ return;
+
if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
std::swap(Weights.front(), Weights.back());
}