@@ -1680,19 +1680,19 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
16801680 return isGEPFree (I);
16811681}
16821682
1683- // / Simplify \p Cmp if RHS is const and we can ValueTrack LHS,
1684- // This handles the case when the Cmp instruction is guarded a recursive call
1685- // that will cause the Cmp to fail/succeed for the next iteration .
1683+ // Simplify \p Cmp if RHS is const and we can ValueTrack LHS.
1684+ // This handles the case when the Cmp instruction is guarding a recursive call
1685+ // that will cause the Cmp to fail/succeed for the recursive call .
16861686bool CallAnalyzer::simplifyCmpInst (Function *F, CmpInst &Cmp) {
1687- // Bail out if the RHS is NOT const:
1688- if (!isa<Constant>(Cmp.getOperand (1 )))
1687+ // Bail out if LHS is not a function argument or RHS is NOT const:
1688+ if (!isa<Argument>(Cmp. getOperand ( 0 )) || !isa< Constant>(Cmp.getOperand (1 )))
16891689 return false ;
16901690 auto *CmpOp = Cmp.getOperand (0 );
16911691 // Iterate over the users of the function to check if it's a recursive
16921692 // function:
16931693 for (auto *U : F->users ()) {
16941694 CallInst *Call = dyn_cast<CallInst>(U);
1695- if (!Call || Call->getFunction () != F)
1695+ if (!Call || Call->getFunction () != F || Call-> getCalledFunction () != F )
16961696 continue ;
16971697 auto *CallBB = Call->getParent ();
16981698 auto *Predecessor = CallBB->getSinglePredecessor ();
@@ -1704,8 +1704,7 @@ bool CallAnalyzer::simplifyCmpInst(Function *F, CmpInst &Cmp) {
17041704 if (!Br || Br->isUnconditional ())
17051705 continue ;
17061706 // Check if the Br condition is the same Cmp instr we are investigating:
1707- auto *CmpInstr = dyn_cast<CmpInst>(Br->getCondition ());
1708- if (!CmpInstr || CmpInstr != &Cmp)
1707+ if (Br->getCondition () != &Cmp)
17091708 continue ;
17101709 // Check if there are any arg of the recursive callsite is affecting the cmp
17111710 // instr:
@@ -1715,7 +1714,7 @@ bool CallAnalyzer::simplifyCmpInst(Function *F, CmpInst &Cmp) {
17151714 ArgNum < F->arg_size () && ArgNum < Call->arg_size (); ArgNum++) {
17161715 FuncArg = F->getArg (ArgNum);
17171716 CallArg = Call->getArgOperand (ArgNum);
1718- if (( FuncArg == CmpOp) && ( CallArg != CmpOp) ) {
1717+ if (FuncArg == CmpOp && CallArg != CmpOp) {
17191718 ArgFound = true ;
17201719 break ;
17211720 }
@@ -1737,16 +1736,16 @@ bool CallAnalyzer::simplifyCmpInst(Function *F, CmpInst &Cmp) {
17371736 DT.recalculate (*F);
17381737 }
17391738 SQ.DT = &DT;
1740- Value *simplifiedInstruction = llvm::simplifyInstructionWithOperands (
1741- CmpInstr , {CallArg, Cmp.getOperand (1 )}, SQ);
1742- if (!simplifiedInstruction )
1739+ Value *SimplifiedInstruction = llvm::simplifyInstructionWithOperands (
1740+ cast<CmpInst>(&Cmp) , {CallArg, Cmp.getOperand (1 )}, SQ);
1741+ if (!SimplifiedInstruction )
17431742 continue ;
1744- if (auto *ConstVal = dyn_cast<llvm::ConstantInt>(simplifiedInstruction )) {
1745- bool isTrueSuccessor = CallBB == Br->getSuccessor (0 );
1743+ if (auto *ConstVal = dyn_cast<llvm::ConstantInt>(SimplifiedInstruction )) {
1744+ bool IsTrueSuccessor = CallBB == Br->getSuccessor (0 );
17461745 SimplifiedValues[&Cmp] = ConstVal;
17471746 if (ConstVal->isOne ())
1748- return !isTrueSuccessor ;
1749- return isTrueSuccessor ;
1747+ return !IsTrueSuccessor ;
1748+ return IsTrueSuccessor ;
17501749 }
17511750 }
17521751 return false ;
0 commit comments