@@ -1334,27 +1334,22 @@ Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) {
13341334 // We already checked simple implication in InstSimplify, only handle complex
13351335 // cases here.
13361336 Value *X = Cmp.getOperand (0 ), *Y = Cmp.getOperand (1 );
1337- ICmpInst::Predicate DomPred;
13381337 const APInt *C;
13391338 if (!match (Y, m_APInt (C)))
13401339 return nullptr ;
13411340
13421341 CmpInst::Predicate Pred = Cmp.getPredicate ();
13431342 ConstantRange CR = ConstantRange::makeExactICmpRegion (Pred, *C);
13441343
1345- auto handleDomCond = [&](Value *DomCond, bool CondIsTrue) -> Instruction * {
1346- const APInt *DomC;
1347- if (!match (DomCond, m_ICmp (DomPred, m_Specific (X), m_APInt (DomC))))
1348- return nullptr ;
1344+ auto handleDomCond = [&](ICmpInst::Predicate DomPred,
1345+ const APInt *DomC) -> Instruction * {
13491346 // We have 2 compares of a variable with constants. Calculate the constant
13501347 // ranges of those compares to see if we can transform the 2nd compare:
13511348 // DomBB:
13521349 // DomCond = icmp DomPred X, DomC
13531350 // br DomCond, CmpBB, FalseBB
13541351 // CmpBB:
13551352 // Cmp = icmp Pred X, C
1356- if (!CondIsTrue)
1357- DomPred = CmpInst::getInversePredicate (DomPred);
13581353 ConstantRange DominatingCR =
13591354 ConstantRange::makeExactICmpRegion (DomPred, *DomC);
13601355 ConstantRange Intersection = DominatingCR.intersectWith (CR);
@@ -1388,15 +1383,21 @@ Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) {
13881383 };
13891384
13901385 for (BranchInst *BI : DC.conditionsFor (X)) {
1391- auto *Cond = BI->getCondition ();
1386+ ICmpInst::Predicate DomPred;
1387+ const APInt *DomC;
1388+ if (!match (BI->getCondition (),
1389+ m_ICmp (DomPred, m_Specific (X), m_APInt (DomC))))
1390+ continue ;
1391+
13921392 BasicBlockEdge Edge0 (BI->getParent (), BI->getSuccessor (0 ));
13931393 if (DT.dominates (Edge0, Cmp.getParent ())) {
1394- if (auto *V = handleDomCond (Cond, true ))
1394+ if (auto *V = handleDomCond (DomPred, DomC ))
13951395 return V;
13961396 } else {
13971397 BasicBlockEdge Edge1 (BI->getParent (), BI->getSuccessor (1 ));
13981398 if (DT.dominates (Edge1, Cmp.getParent ()))
1399- if (auto *V = handleDomCond (Cond, false ))
1399+ if (auto *V =
1400+ handleDomCond (CmpInst::getInversePredicate (DomPred), DomC))
14001401 return V;
14011402 }
14021403 }
0 commit comments