@@ -1680,19 +1680,19 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
1680
1680
return isGEPFree (I);
1681
1681
}
1682
1682
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 .
1686
1686
bool 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 )))
1689
1689
return false ;
1690
1690
auto *CmpOp = Cmp.getOperand (0 );
1691
1691
// Iterate over the users of the function to check if it's a recursive
1692
1692
// function:
1693
1693
for (auto *U : F->users ()) {
1694
1694
CallInst *Call = dyn_cast<CallInst>(U);
1695
- if (!Call || Call->getFunction () != F)
1695
+ if (!Call || Call->getFunction () != F || Call-> getCalledFunction () != F )
1696
1696
continue ;
1697
1697
auto *CallBB = Call->getParent ();
1698
1698
auto *Predecessor = CallBB->getSinglePredecessor ();
@@ -1704,8 +1704,7 @@ bool CallAnalyzer::simplifyCmpInst(Function *F, CmpInst &Cmp) {
1704
1704
if (!Br || Br->isUnconditional ())
1705
1705
continue ;
1706
1706
// 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)
1709
1708
continue ;
1710
1709
// Check if there are any arg of the recursive callsite is affecting the cmp
1711
1710
// instr:
@@ -1715,7 +1714,7 @@ bool CallAnalyzer::simplifyCmpInst(Function *F, CmpInst &Cmp) {
1715
1714
ArgNum < F->arg_size () && ArgNum < Call->arg_size (); ArgNum++) {
1716
1715
FuncArg = F->getArg (ArgNum);
1717
1716
CallArg = Call->getArgOperand (ArgNum);
1718
- if (( FuncArg == CmpOp) && ( CallArg != CmpOp) ) {
1717
+ if (FuncArg == CmpOp && CallArg != CmpOp) {
1719
1718
ArgFound = true ;
1720
1719
break ;
1721
1720
}
@@ -1737,16 +1736,16 @@ bool CallAnalyzer::simplifyCmpInst(Function *F, CmpInst &Cmp) {
1737
1736
DT.recalculate (*F);
1738
1737
}
1739
1738
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 )
1743
1742
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 );
1746
1745
SimplifiedValues[&Cmp] = ConstVal;
1747
1746
if (ConstVal->isOne ())
1748
- return !isTrueSuccessor ;
1749
- return isTrueSuccessor ;
1747
+ return !IsTrueSuccessor ;
1748
+ return IsTrueSuccessor ;
1750
1749
}
1751
1750
}
1752
1751
return false ;
0 commit comments