@@ -263,8 +263,6 @@ class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
263263 // Cache the DataLayout since we use it a lot.
264264 const DataLayout &DL;
265265
266- DominatorTree DT;
267-
268266 // / The OptimizationRemarkEmitter available for this compilation.
269267 OptimizationRemarkEmitter *ORE;
270268
@@ -1688,66 +1686,51 @@ bool CallAnalyzer::simplifyCmpInstForRecCall(CmpInst &Cmp) {
16881686 if (!isa<Argument>(Cmp.getOperand (0 )) || !isa<Constant>(Cmp.getOperand (1 )))
16891687 return false ;
16901688 auto *CmpOp = Cmp.getOperand (0 );
1691- Function *F = Cmp. getFunction ();
1692- // Iterate over the users of the function to check if it's a recursive
1693- // function:
1694- for ( auto *U : F-> users ()) {
1695- CallInst *Call = dyn_cast<CallInst>(U );
1696- if (!Call || Call-> getFunction () != F || Call-> getCalledFunction () != F)
1697- continue ;
1698- auto *CallBB = Call-> getParent () ;
1699- auto *Predecessor = CallBB-> getSinglePredecessor ();
1700- // Only handle the case when the callsite has a single predecessor:
1701- if (!Predecessor )
1702- continue ;
1689+ // Make sure that the callsite is recursive:
1690+ if (CandidateCall. getCaller () != &F)
1691+ return false ;
1692+ // Only handle the case when the callsite has a single predecessor:
1693+ auto *CallBB = CandidateCall. getParent ( );
1694+ auto *Predecessor = CallBB-> getSinglePredecessor ();
1695+ if (!Predecessor)
1696+ return false ;
1697+ // Check if the callsite is guarded by the same Cmp instruction:
1698+ auto *Br = dyn_cast<BranchInst>(Predecessor-> getTerminator ());
1699+ if (!Br || Br-> isUnconditional () || Br-> getCondition () != &Cmp )
1700+ return false ;
17031701
1704- auto *Br = dyn_cast<BranchInst>(Predecessor->getTerminator ());
1705- if (!Br || Br->isUnconditional ())
1706- continue ;
1707- // Check if the Br condition is the same Cmp instr we are investigating:
1708- if (Br->getCondition () != &Cmp)
1709- continue ;
1710- // Check if there are any arg of the recursive callsite is affecting the cmp
1711- // instr:
1712- bool ArgFound = false ;
1713- Value *FuncArg = nullptr , *CallArg = nullptr ;
1714- for (unsigned ArgNum = 0 ;
1715- ArgNum < F->arg_size () && ArgNum < Call->arg_size (); ArgNum++) {
1716- FuncArg = F->getArg (ArgNum);
1717- CallArg = Call->getArgOperand (ArgNum);
1718- if (FuncArg == CmpOp && CallArg != CmpOp) {
1719- ArgFound = true ;
1720- break ;
1721- }
1722- }
1723- if (!ArgFound)
1724- continue ;
1725- // Now we have a recursive call that is guarded by a cmp instruction.
1726- // Check if this cmp can be simplified:
1727- SimplifyQuery SQ (DL, dyn_cast<Instruction>(CallArg));
1728- DomConditionCache DC;
1729- DC.registerBranch (Br);
1730- SQ.DC = &DC;
1731- if (DT.root_size () == 0 ) {
1732- // Dominator tree was never constructed for any function yet.
1733- DT.recalculate (*F);
1734- } else if (DT.getRoot ()->getParent () != F) {
1735- // Dominator tree was constructed for a different function, recalculate
1736- // it for the current function.
1737- DT.recalculate (*F);
1702+ // Check if there is any arg of the recursive callsite is affecting the cmp
1703+ // instr:
1704+ bool ArgFound = false ;
1705+ Value *FuncArg = nullptr , *CallArg = nullptr ;
1706+ for (unsigned ArgNum = 0 ;
1707+ ArgNum < F.arg_size () && ArgNum < CandidateCall.arg_size (); ArgNum++) {
1708+ FuncArg = F.getArg (ArgNum);
1709+ CallArg = CandidateCall.getArgOperand (ArgNum);
1710+ if (FuncArg == CmpOp && CallArg != CmpOp) {
1711+ ArgFound = true ;
1712+ break ;
17381713 }
1739- SQ.DT = &DT;
1740- Value *SimplifiedInstruction = llvm::simplifyInstructionWithOperands (
1741- cast<CmpInst>(&Cmp), {CallArg, Cmp.getOperand (1 )}, SQ);
1742- if (auto *ConstVal = dyn_cast_or_null<ConstantInt>(SimplifiedInstruction)) {
1743- bool IsTrueSuccessor = CallBB == Br->getSuccessor (0 );
1744- // Make sure that the BB of the recursive call is NOT the next successor
1745- // of the icmp. In other words, make sure that the recursion depth is 1.
1746- if ((ConstVal->isOne () && !IsTrueSuccessor) ||
1747- (ConstVal->isZero () && IsTrueSuccessor)) {
1748- SimplifiedValues[&Cmp] = ConstVal;
1749- return true ;
1750- }
1714+ }
1715+ if (!ArgFound)
1716+ return false ;
1717+
1718+ // Now we have a recursive call that is guarded by a cmp instruction.
1719+ // Check if this cmp can be simplified:
1720+ SimplifyQuery SQ (DL, dyn_cast<Instruction>(CallArg));
1721+ CondContext CC (&Cmp);
1722+ CC.Invert = (CallBB != Br->getSuccessor (0 ));
1723+ SQ.CC = &CC;
1724+ CC.AffectedValues .insert (FuncArg);
1725+ Value *SimplifiedInstruction = llvm::simplifyInstructionWithOperands (
1726+ cast<CmpInst>(&Cmp), {CallArg, Cmp.getOperand (1 )}, SQ);
1727+ if (auto *ConstVal = dyn_cast_or_null<ConstantInt>(SimplifiedInstruction)) {
1728+ // Make sure that the BB of the recursive call is NOT the true successor
1729+ // of the icmp. In other words, make sure that the recursion depth is 1.
1730+ if ((ConstVal->isOne () && CC.Invert ) ||
1731+ (ConstVal->isZero () && !CC.Invert )) {
1732+ SimplifiedValues[&Cmp] = ConstVal;
1733+ return true ;
17511734 }
17521735 }
17531736 return false ;
0 commit comments