diff options
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyIndVar.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index 4e83d2f6e3c6..a28916bc9baf 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -93,6 +93,7 @@ namespace { void replaceRemWithNumeratorOrZero(BinaryOperator *Rem); void replaceSRemWithURem(BinaryOperator *Rem); bool eliminateSDiv(BinaryOperator *SDiv); + bool strengthenBinaryOp(BinaryOperator *BO, Instruction *IVOperand); bool strengthenOverflowingOperation(BinaryOperator *OBO, Instruction *IVOperand); bool strengthenRightShift(BinaryOperator *BO, Instruction *IVOperand); @@ -216,8 +217,10 @@ bool SimplifyIndvar::makeIVComparisonInvariant(ICmpInst *ICmp, // Do not generate something ridiculous. auto *PHTerm = Preheader->getTerminator(); - if (Rewriter.isHighCostExpansion({ InvariantLHS, InvariantRHS }, L, - 2 * SCEVCheapExpansionBudget, TTI, PHTerm)) + if (Rewriter.isHighCostExpansion({InvariantLHS, InvariantRHS}, L, + 2 * SCEVCheapExpansionBudget, TTI, PHTerm) || + !Rewriter.isSafeToExpandAt(InvariantLHS, PHTerm) || + !Rewriter.isSafeToExpandAt(InvariantRHS, PHTerm)) return false; auto *NewLHS = Rewriter.expandCodeFor(InvariantLHS, IVOperand->getType(), PHTerm); @@ -747,6 +750,13 @@ bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst, return true; } +bool SimplifyIndvar::strengthenBinaryOp(BinaryOperator *BO, + Instruction *IVOperand) { + return (isa<OverflowingBinaryOperator>(BO) && + strengthenOverflowingOperation(BO, IVOperand)) || + (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand)); +} + /// Annotate BO with nsw / nuw if it provably does not signed-overflow / /// unsigned-overflow. Returns true if anything changed, false otherwise. bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO, @@ -898,6 +908,14 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { if (replaceIVUserWithLoopInvariant(UseInst)) continue; + // Go further for the bitcast ''prtoint ptr to i64' + if (isa<PtrToIntInst>(UseInst)) + for (Use &U : UseInst->uses()) { + Instruction *User = cast<Instruction>(U.getUser()); + if (replaceIVUserWithLoopInvariant(User)) + break; // done replacing + } + Instruction *IVOperand = UseOper.second; for (unsigned N = 0; IVOperand; ++N) { assert(N <= Simplified.size() && "runaway iteration"); @@ -917,9 +935,7 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { } if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseInst)) { - if ((isa<OverflowingBinaryOperator>(BO) && - strengthenOverflowingOperation(BO, IVOperand)) || - (isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) { + if (strengthenBinaryOp(BO, IVOperand)) { // re-queue uses of the now modified binary operator and fall // through to the checks that remain. pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers); |