diff options
author | Hongbin Zheng <etherzhhb@gmail.com> | 2017-09-27 03:11:46 +0000 |
---|---|---|
committer | Hongbin Zheng <etherzhhb@gmail.com> | 2017-09-27 03:11:46 +0000 |
commit | d1b7b2efba67fcedcc803b2bf7dda808b9a5c401 (patch) | |
tree | 97ce25fd46f47439ddec103a08336485ddfcc350 /llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | |
parent | 1d23cb4e7e8dbdff6fd5d8b365b14964254ca243 (diff) | |
download | bcm5719-llvm-d1b7b2efba67fcedcc803b2bf7dda808b9a5c401.tar.gz bcm5719-llvm-d1b7b2efba67fcedcc803b2bf7dda808b9a5c401.zip |
[SimplifyIndVar] Constant fold IV users
This patch tries to transform cases like:
for (unsigned i = 0; i < N; i += 2) {
bool c0 = (i & 0x1) == 0;
bool c1 = ((i + 1) & 0x1) == 1;
}
To
for (unsigned i = 0; i < N; i += 2) {
bool c0 = true;
bool c1 = true;
}
This commit also update test/Transforms/IndVarSimplify/replace-srem-by-urem.ll to prevent constant folding.
Differential Revision: https://reviews.llvm.org/D38272
llvm-svn: 314266
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyIndVar.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index 5228f067ae1..cef8fe1a614 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -35,6 +35,7 @@ using namespace llvm; STATISTIC(NumElimIdentity, "Number of IV identities eliminated"); STATISTIC(NumElimOperand, "Number of IV operands folded into a use"); +STATISTIC(NumFoldedUser, "Number of IV users folded into a constant"); STATISTIC(NumElimRem , "Number of IV remainder operations eliminated"); STATISTIC( NumSimplifiedSDiv, @@ -76,6 +77,7 @@ namespace { Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand); bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand); + bool foldConstantSCEV(Instruction *UseInst); bool eliminateOverflowIntrinsic(CallInst *CI); bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); @@ -534,6 +536,30 @@ bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst, return false; } +/// Replace the UseInst with a constant if possible +bool SimplifyIndvar::foldConstantSCEV(Instruction *I) { + if (!SE->isSCEVable(I->getType())) + return false; + + // Get the symbolic expression for this instruction. + const SCEV *S = SE->getSCEV(I); + + const Loop *L = LI->getLoopFor(I->getParent()); + S = SE->getSCEVAtScope(S, L); + + if (auto *C = dyn_cast<SCEVConstant>(S)) { + I->replaceAllUsesWith(C->getValue()); + DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I + << " with constant: " << *C << '\n'); + ++NumFoldedUser; + Changed = true; + DeadInsts.emplace_back(I); + return true; + } + + return false; +} + /// Eliminate any operation that SCEV can prove is an identity function. bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand) { @@ -741,6 +767,10 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { // Bypass back edges to avoid extra work. if (UseInst == CurrIV) continue; + // Try to replace UseInst with a constant before any other simplifications + if (foldConstantSCEV(UseInst)) + continue; + Instruction *IVOperand = UseOper.second; for (unsigned N = 0; IVOperand; ++N) { assert(N <= Simplified.size() && "runaway iteration"); |