2001-12-03 17:28:42 +00:00
|
|
|
//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-03 17:28:42 +00:00
|
|
|
//
|
2004-04-02 20:24:31 +00:00
|
|
|
// This transformation analyzes and transforms the induction variables (and
|
|
|
|
// computations derived from them) into simpler forms suitable for subsequent
|
|
|
|
// analysis and transformation.
|
|
|
|
//
|
2006-08-18 09:01:07 +00:00
|
|
|
// This transformation makes the following changes to each loop with an
|
2004-04-02 20:24:31 +00:00
|
|
|
// identifiable induction variable:
|
|
|
|
// 1. All loops are transformed to have a SINGLE canonical induction variable
|
|
|
|
// which starts at zero and steps by one.
|
|
|
|
// 2. The canonical induction variable is guaranteed to be the first PHI node
|
|
|
|
// in the loop header block.
|
2009-06-14 22:38:41 +00:00
|
|
|
// 3. The canonical induction variable is guaranteed to be in a wide enough
|
|
|
|
// type so that IV expressions need not be (directly) zero-extended or
|
|
|
|
// sign-extended.
|
|
|
|
// 4. Any pointer arithmetic recurrences are raised to use array subscripts.
|
2004-04-02 20:24:31 +00:00
|
|
|
//
|
|
|
|
// If the trip count of a loop is computable, this pass also makes the following
|
|
|
|
// changes:
|
|
|
|
// 1. The exit condition for the loop is canonicalized to compare the
|
|
|
|
// induction value against the exit value. This turns loops like:
|
|
|
|
// 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
|
|
|
|
// 2. Any use outside of the loop of an expression derived from the indvar
|
|
|
|
// is changed to compute the derived value outside of the loop, eliminating
|
|
|
|
// the dependence on the exit value of the induction variable. If the only
|
|
|
|
// purpose of the loop is to compute the exit value of some derived
|
|
|
|
// expression, this transformation will make the loop dead.
|
|
|
|
//
|
|
|
|
// This transformation should be followed by strength reduction after all of the
|
2009-05-19 20:38:47 +00:00
|
|
|
// desired loop transformations have been performed.
|
2001-12-03 17:28:42 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
#define DEBUG_TYPE "indvars"
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-04-02 20:24:31 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
Change the canonical induction variable that we insert.
Instead of producing code like this:
Loop:
X = phi 0, X2
...
X2 = X + 1
if (X != N-1) goto Loop
We now generate code that looks like this:
Loop:
X = phi 0, X2
...
X2 = X + 1
if (X2 != N) goto Loop
This has two big advantages:
1. The trip count of the loop is now explicit in the code, allowing
the direct implementation of Loop::getTripCount()
2. This reduces register pressure in the loop, and allows X and X2 to be
put into the same register.
As a consequence of the second point, the code we generate for loops went
from:
.LBB2: # no_exit.1
...
mov %EDI, %ESI
inc %EDI
cmp %ESI, 2
mov %ESI, %EDI
jne .LBB2 # PC rel: no_exit.1
To:
.LBB2: # no_exit.1
...
inc %ESI
cmp %ESI, 3
jne .LBB2 # PC rel: no_exit.1
... which has two fewer moves, and uses one less register.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12961 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-15 15:21:43 +00:00
|
|
|
#include "llvm/Constants.h"
|
2003-12-22 05:02:01 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2010-03-15 22:23:03 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2009-07-03 00:17:18 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-04-02 20:24:31 +00:00
|
|
|
#include "llvm/Type.h"
|
2009-05-12 02:17:14 +00:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/IVUsers.h"
|
2005-07-30 00:12:19 +00:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2003-12-18 17:19:19 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2007-03-07 06:39:01 +00:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2002-02-12 22:39:50 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2009-08-23 04:37:46 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2007-01-07 01:14:12 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-23 04:37:46 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-18 17:19:19 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2009-05-12 02:17:14 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
For PR1064:
Implement the arbitrary bit-width integer feature. The feature allows
integers of any bitwidth (up to 64) to be defined instead of just 1, 8,
16, 32, and 64 bit integers.
This change does several things:
1. Introduces a new Derived Type, IntegerType, to represent the number of
bits in an integer. The Type classes SubclassData field is used to
store the number of bits. This allows 2^23 bits in an integer type.
2. Removes the five integer Type::TypeID values for the 1, 8, 16, 32 and
64-bit integers. These are replaced with just IntegerType which is not
a primitive any more.
3. Adjust the rest of LLVM to account for this change.
Note that while this incremental change lays the foundation for arbitrary
bit-width integers, LLVM has not yet been converted to actually deal with
them in any significant way. Most optimization passes, for example, will
still only deal with the byte-width integer types. Future increments
will rectify this situation.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33113 91177308-0d34-0410-b5e6-96231b3b80d8
2007-01-12 07:05:14 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-05-12 02:17:14 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2003-12-18 17:19:19 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
STATISTIC(NumRemoved , "Number of aux indvars removed");
|
|
|
|
STATISTIC(NumInserted, "Number of canonical indvars added");
|
|
|
|
STATISTIC(NumReplaced, "Number of exit values replaced");
|
|
|
|
STATISTIC(NumLFTR , "Number of loop exit tests replaced");
|
2003-12-22 03:58:44 +00:00
|
|
|
|
2006-12-19 21:40:18 +00:00
|
|
|
namespace {
|
2009-09-02 06:11:42 +00:00
|
|
|
class IndVarSimplify : public LoopPass {
|
2009-05-12 02:17:14 +00:00
|
|
|
IVUsers *IU;
|
2004-04-02 20:24:31 +00:00
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
2009-06-27 05:16:57 +00:00
|
|
|
DominatorTree *DT;
|
2003-12-23 07:47:09 +00:00
|
|
|
bool Changed;
|
2003-12-22 03:58:44 +00:00
|
|
|
public:
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2009-07-15 01:26:32 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
IndVarSimplify() : LoopPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2009-07-15 01:26:32 +00:00
|
|
|
virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
|
2009-02-17 20:49:49 +00:00
|
|
|
|
2009-07-15 01:26:32 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addRequired<LoopInfo>();
|
|
|
|
AU.addRequired<ScalarEvolution>();
|
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
|
|
|
AU.addRequiredID(LCSSAID);
|
|
|
|
AU.addRequired<IVUsers>();
|
|
|
|
AU.addPreserved<ScalarEvolution>();
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
|
|
|
AU.addPreservedID(LCSSAID);
|
|
|
|
AU.addPreserved<IVUsers>();
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
2003-12-22 03:58:44 +00:00
|
|
|
|
2004-04-02 20:24:31 +00:00
|
|
|
private:
|
2007-03-07 06:39:01 +00:00
|
|
|
|
2010-04-12 02:21:50 +00:00
|
|
|
void EliminateIVComparisons();
|
2010-04-13 01:46:36 +00:00
|
|
|
void EliminateIVRemainders();
|
2009-02-17 20:49:49 +00:00
|
|
|
void RewriteNonIntegerIVs(Loop *L);
|
|
|
|
|
2009-07-07 17:06:11 +00:00
|
|
|
ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount,
|
2009-02-17 15:57:39 +00:00
|
|
|
Value *IndVar,
|
2009-02-12 22:19:27 +00:00
|
|
|
BasicBlock *ExitingBlock,
|
|
|
|
BranchInst *BI,
|
2009-02-23 23:20:35 +00:00
|
|
|
SCEVExpander &Rewriter);
|
2010-02-22 04:11:59 +00:00
|
|
|
void RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter);
|
2004-04-02 20:24:31 +00:00
|
|
|
|
2010-02-22 04:11:59 +00:00
|
|
|
void RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter);
|
2008-09-09 21:41:07 +00:00
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
void SinkUnusedInvariants(Loop *L);
|
2009-05-12 02:17:14 +00:00
|
|
|
|
|
|
|
void HandleFloatingPointIV(Loop *L, PHINode *PH);
|
2003-12-22 03:58:44 +00:00
|
|
|
};
|
2002-09-10 05:24:05 +00:00
|
|
|
}
|
2001-12-04 04:32:29 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char IndVarSimplify::ID = 0;
|
|
|
|
static RegisterPass<IndVarSimplify>
|
|
|
|
X("indvars", "Canonicalize Induction Variables");
|
|
|
|
|
2008-10-22 23:32:42 +00:00
|
|
|
Pass *llvm::createIndVarSimplifyPass() {
|
2003-12-22 03:58:44 +00:00
|
|
|
return new IndVarSimplify();
|
2001-12-04 04:32:29 +00:00
|
|
|
}
|
|
|
|
|
2004-04-02 20:24:31 +00:00
|
|
|
/// LinearFunctionTestReplace - This method rewrites the exit condition of the
|
Change the canonical induction variable that we insert.
Instead of producing code like this:
Loop:
X = phi 0, X2
...
X2 = X + 1
if (X != N-1) goto Loop
We now generate code that looks like this:
Loop:
X = phi 0, X2
...
X2 = X + 1
if (X2 != N) goto Loop
This has two big advantages:
1. The trip count of the loop is now explicit in the code, allowing
the direct implementation of Loop::getTripCount()
2. This reduces register pressure in the loop, and allows X and X2 to be
put into the same register.
As a consequence of the second point, the code we generate for loops went
from:
.LBB2: # no_exit.1
...
mov %EDI, %ESI
inc %EDI
cmp %ESI, 2
mov %ESI, %EDI
jne .LBB2 # PC rel: no_exit.1
To:
.LBB2: # no_exit.1
...
inc %ESI
cmp %ESI, 3
jne .LBB2 # PC rel: no_exit.1
... which has two fewer moves, and uses one less register.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12961 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-15 15:21:43 +00:00
|
|
|
/// loop to be a canonical != comparison against the incremented loop induction
|
|
|
|
/// variable. This pass is able to rewrite the exit tests of any loop where the
|
|
|
|
/// SCEV analysis can determine a loop-invariant trip count of the loop, which
|
|
|
|
/// is actually a much broader range than just linear tests.
|
2009-05-12 02:17:14 +00:00
|
|
|
ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *BackedgeTakenCount,
|
2009-02-12 22:19:27 +00:00
|
|
|
Value *IndVar,
|
|
|
|
BasicBlock *ExitingBlock,
|
|
|
|
BranchInst *BI,
|
2009-02-23 23:20:35 +00:00
|
|
|
SCEVExpander &Rewriter) {
|
2010-04-12 21:13:43 +00:00
|
|
|
// Special case: If the backedge-taken count is a UDiv, it's very likely a
|
|
|
|
// UDiv that ScalarEvolution produced in order to compute a precise
|
|
|
|
// expression, rather than a UDiv from the user's code. If we can't find a
|
|
|
|
// UDiv in the code with some simple searching, assume the former and forego
|
|
|
|
// rewriting the loop.
|
|
|
|
if (isa<SCEVUDivExpr>(BackedgeTakenCount)) {
|
|
|
|
ICmpInst *OrigCond = dyn_cast<ICmpInst>(BI->getCondition());
|
|
|
|
if (!OrigCond) return 0;
|
|
|
|
const SCEV *R = SE->getSCEV(OrigCond->getOperand(1));
|
2010-05-03 22:09:21 +00:00
|
|
|
R = SE->getMinusSCEV(R, SE->getConstant(R->getType(), 1));
|
2010-04-12 21:13:43 +00:00
|
|
|
if (R != BackedgeTakenCount) {
|
|
|
|
const SCEV *L = SE->getSCEV(OrigCond->getOperand(0));
|
2010-05-03 22:09:21 +00:00
|
|
|
L = SE->getMinusSCEV(L, SE->getConstant(L->getType(), 1));
|
2010-04-12 21:13:43 +00:00
|
|
|
if (L != BackedgeTakenCount)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-15 20:26:22 +00:00
|
|
|
// If the exiting block is not the same as the backedge block, we must compare
|
|
|
|
// against the preincremented value, otherwise we prefer to compare against
|
|
|
|
// the post-incremented value.
|
2009-02-12 22:19:27 +00:00
|
|
|
Value *CmpIndVar;
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *RHS = BackedgeTakenCount;
|
2009-02-12 22:19:27 +00:00
|
|
|
if (ExitingBlock == L->getLoopLatch()) {
|
2009-02-24 18:55:53 +00:00
|
|
|
// Add one to the "backedge-taken" count to get the trip count.
|
|
|
|
// If this addition may overflow, we have to be more pessimistic and
|
|
|
|
// cast the induction variable before doing the add.
|
2010-05-03 22:09:21 +00:00
|
|
|
const SCEV *Zero = SE->getConstant(BackedgeTakenCount->getType(), 0);
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *N =
|
2009-02-24 18:55:53 +00:00
|
|
|
SE->getAddExpr(BackedgeTakenCount,
|
2010-05-03 22:09:21 +00:00
|
|
|
SE->getConstant(BackedgeTakenCount->getType(), 1));
|
2009-02-12 22:19:27 +00:00
|
|
|
if ((isa<SCEVConstant>(N) && !N->isZero()) ||
|
2010-04-11 19:27:13 +00:00
|
|
|
SE->isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
|
2009-02-12 22:19:27 +00:00
|
|
|
// No overflow. Cast the sum.
|
2009-02-24 18:55:53 +00:00
|
|
|
RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
|
2009-02-12 22:19:27 +00:00
|
|
|
} else {
|
|
|
|
// Potential overflow. Cast before doing the add.
|
2009-02-24 18:55:53 +00:00
|
|
|
RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
|
|
|
|
IndVar->getType());
|
|
|
|
RHS = SE->getAddExpr(RHS,
|
2010-05-03 22:09:21 +00:00
|
|
|
SE->getConstant(IndVar->getType(), 1));
|
2009-02-12 22:19:27 +00:00
|
|
|
}
|
|
|
|
|
2009-02-24 18:55:53 +00:00
|
|
|
// The BackedgeTaken expression contains the number of times that the
|
|
|
|
// backedge branches to the loop header. This is one less than the
|
|
|
|
// number of times the loop executes, so use the incremented indvar.
|
2009-02-12 22:19:27 +00:00
|
|
|
CmpIndVar = L->getCanonicalInductionVariableIncrement();
|
2004-04-15 20:26:22 +00:00
|
|
|
} else {
|
|
|
|
// We have to use the preincremented value...
|
2009-02-24 18:55:53 +00:00
|
|
|
RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
|
|
|
|
IndVar->getType());
|
2009-02-12 22:19:27 +00:00
|
|
|
CmpIndVar = IndVar;
|
2004-04-15 20:26:22 +00:00
|
|
|
}
|
Change the canonical induction variable that we insert.
Instead of producing code like this:
Loop:
X = phi 0, X2
...
X2 = X + 1
if (X != N-1) goto Loop
We now generate code that looks like this:
Loop:
X = phi 0, X2
...
X2 = X + 1
if (X2 != N) goto Loop
This has two big advantages:
1. The trip count of the loop is now explicit in the code, allowing
the direct implementation of Loop::getTripCount()
2. This reduces register pressure in the loop, and allows X and X2 to be
put into the same register.
As a consequence of the second point, the code we generate for loops went
from:
.LBB2: # no_exit.1
...
mov %EDI, %ESI
inc %EDI
cmp %ESI, 2
mov %ESI, %EDI
jne .LBB2 # PC rel: no_exit.1
To:
.LBB2: # no_exit.1
...
inc %ESI
cmp %ESI, 3
jne .LBB2 # PC rel: no_exit.1
... which has two fewer moves, and uses one less register.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12961 91177308-0d34-0410-b5e6-96231b3b80d8
2004-04-15 15:21:43 +00:00
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
// Expand the code for the iteration count.
|
2009-06-24 01:18:18 +00:00
|
|
|
assert(RHS->isLoopInvariant(L) &&
|
|
|
|
"Computed iteration count is not loop invariant!");
|
2009-06-26 22:53:46 +00:00
|
|
|
Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), BI);
|
2004-04-02 20:24:31 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Insert a new icmp_ne or icmp_eq instruction before the branch.
|
|
|
|
ICmpInst::Predicate Opcode;
|
2004-04-02 20:24:31 +00:00
|
|
|
if (L->contains(BI->getSuccessor(0)))
|
2006-12-23 06:05:41 +00:00
|
|
|
Opcode = ICmpInst::ICMP_NE;
|
2004-04-02 20:24:31 +00:00
|
|
|
else
|
2006-12-23 06:05:41 +00:00
|
|
|
Opcode = ICmpInst::ICMP_EQ;
|
2004-04-02 20:24:31 +00:00
|
|
|
|
2010-01-05 01:27:06 +00:00
|
|
|
DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n"
|
2009-08-23 04:37:46 +00:00
|
|
|
<< " LHS:" << *CmpIndVar << '\n'
|
|
|
|
<< " op:\t"
|
|
|
|
<< (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
|
|
|
|
<< " RHS:\t" << *RHS << "\n");
|
2009-02-12 22:19:27 +00:00
|
|
|
|
2009-07-09 23:48:35 +00:00
|
|
|
ICmpInst *Cond = new ICmpInst(BI, Opcode, CmpIndVar, ExitCnt, "exitcond");
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2010-02-22 02:07:36 +00:00
|
|
|
Value *OrigCond = BI->getCondition();
|
2009-05-24 19:11:38 +00:00
|
|
|
// It's tempting to use replaceAllUsesWith here to fully replace the old
|
|
|
|
// comparison, but that's not immediately safe, since users of the old
|
|
|
|
// comparison may not be dominated by the new comparison. Instead, just
|
|
|
|
// update the branch to use the new comparison; in the common case this
|
|
|
|
// will make old comparison dead.
|
|
|
|
BI->setCondition(Cond);
|
2009-05-12 02:17:14 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(OrigCond);
|
|
|
|
|
2004-04-02 20:24:31 +00:00
|
|
|
++NumLFTR;
|
|
|
|
Changed = true;
|
2009-05-12 02:17:14 +00:00
|
|
|
return Cond;
|
2004-04-02 20:24:31 +00:00
|
|
|
}
|
2003-12-22 03:58:44 +00:00
|
|
|
|
2004-04-02 20:24:31 +00:00
|
|
|
/// RewriteLoopExitValues - Check to see if this loop has a computable
|
|
|
|
/// loop-invariant execution count. If so, this means that we can compute the
|
|
|
|
/// final value of any expressions that are recurrent in the loop, and
|
|
|
|
/// substitute the exit values from the loop into any instructions outside of
|
|
|
|
/// the loop that use the final values of the current expressions.
|
2009-05-12 02:17:14 +00:00
|
|
|
///
|
|
|
|
/// This is mostly redundant with the regular IndVarSimplify activities that
|
|
|
|
/// happen later, except that it's more powerful in some cases, because it's
|
|
|
|
/// able to brute-force evaluate arbitrary instructions as long as they have
|
|
|
|
/// constant operands at the beginning of the loop.
|
2009-04-18 17:56:28 +00:00
|
|
|
void IndVarSimplify::RewriteLoopExitValues(Loop *L,
|
2009-06-26 22:53:46 +00:00
|
|
|
SCEVExpander &Rewriter) {
|
2009-05-12 02:17:14 +00:00
|
|
|
// Verify the input to the pass in already in LCSSA form.
|
2010-03-10 19:38:49 +00:00
|
|
|
assert(L->isLCSSAForm(*DT));
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2007-08-21 00:31:24 +00:00
|
|
|
SmallVector<BasicBlock*, 8> ExitBlocks;
|
2007-03-04 03:43:23 +00:00
|
|
|
L->getUniqueExitBlocks(ExitBlocks);
|
|
|
|
|
|
|
|
// Find all values that are computed inside the loop, but used outside of it.
|
|
|
|
// Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan
|
|
|
|
// the exit blocks of the loop to find them.
|
|
|
|
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
|
|
|
|
BasicBlock *ExitBB = ExitBlocks[i];
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2007-03-04 03:43:23 +00:00
|
|
|
// If there are no PHI nodes in this exit block, then no values defined
|
|
|
|
// inside the loop are used on this path, skip it.
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
|
|
|
|
if (!PN) continue;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2007-03-04 03:43:23 +00:00
|
|
|
unsigned NumPreds = PN->getNumIncomingValues();
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2007-03-04 03:43:23 +00:00
|
|
|
// Iterate over all of the PHI nodes.
|
|
|
|
BasicBlock::iterator BBI = ExitBB->begin();
|
|
|
|
while ((PN = dyn_cast<PHINode>(BBI++))) {
|
2009-05-24 19:36:09 +00:00
|
|
|
if (PN->use_empty())
|
|
|
|
continue; // dead use, don't replace it
|
2010-02-18 21:34:02 +00:00
|
|
|
|
|
|
|
// SCEV only supports integer expressions for now.
|
|
|
|
if (!PN->getType()->isIntegerTy() && !PN->getType()->isPointerTy())
|
|
|
|
continue;
|
|
|
|
|
2010-02-19 07:14:22 +00:00
|
|
|
// It's necessary to tell ScalarEvolution about this explicitly so that
|
|
|
|
// it can walk the def-use list and forget all SCEVs, as it may not be
|
|
|
|
// watching the PHI itself. Once the new exit value is in place, there
|
|
|
|
// may not be a def-use connection between the loop and every instruction
|
|
|
|
// which got a SCEVAddRecExpr for that loop.
|
|
|
|
SE->forgetValue(PN);
|
|
|
|
|
2007-03-04 03:43:23 +00:00
|
|
|
// Iterate over all of the values in all the PHI nodes.
|
|
|
|
for (unsigned i = 0; i != NumPreds; ++i) {
|
|
|
|
// If the value being merged in is not integer or is not defined
|
|
|
|
// in the loop, skip it.
|
|
|
|
Value *InVal = PN->getIncomingValue(i);
|
2010-02-18 21:34:02 +00:00
|
|
|
if (!isa<Instruction>(InVal))
|
2007-03-04 03:43:23 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this pred is for a subloop, not L itself, skip it.
|
2009-02-17 19:13:57 +00:00
|
|
|
if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
|
2007-03-04 03:43:23 +00:00
|
|
|
continue; // The Block is in a subloop, skip it.
|
|
|
|
|
|
|
|
// Check that InVal is defined in the loop.
|
|
|
|
Instruction *Inst = cast<Instruction>(InVal);
|
2009-12-18 01:24:09 +00:00
|
|
|
if (!L->contains(Inst))
|
2007-03-04 03:43:23 +00:00
|
|
|
continue;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2007-03-04 03:43:23 +00:00
|
|
|
// Okay, this instruction has a user outside of the current loop
|
|
|
|
// and varies predictably *inside* the loop. Evaluate the value it
|
|
|
|
// contains when the loop exits, if possible.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
|
2009-05-24 23:25:42 +00:00
|
|
|
if (!ExitValue->isLoopInvariant(L))
|
2007-03-04 03:43:23 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
++NumReplaced;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2010-01-05 01:27:06 +00:00
|
|
|
DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
|
2009-08-23 04:37:46 +00:00
|
|
|
<< " LoopVal = " << *Inst << "\n");
|
2007-03-04 01:00:28 +00:00
|
|
|
|
2007-03-04 03:43:23 +00:00
|
|
|
PN->setIncomingValue(i, ExitVal);
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// If this instruction is dead now, delete it.
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Inst);
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2009-07-14 01:09:02 +00:00
|
|
|
if (NumPreds == 1) {
|
|
|
|
// Completely replace a single-pred PHI. This is safe, because the
|
|
|
|
// NewVal won't be variant in the loop, so we don't need an LCSSA phi
|
|
|
|
// node anymore.
|
2007-03-04 03:43:23 +00:00
|
|
|
PN->replaceAllUsesWith(ExitVal);
|
2009-05-12 02:17:14 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(PN);
|
2007-03-03 22:48:48 +00:00
|
|
|
}
|
2005-06-15 21:29:31 +00:00
|
|
|
}
|
2009-07-14 01:09:02 +00:00
|
|
|
if (NumPreds != 1) {
|
2009-06-26 22:53:46 +00:00
|
|
|
// Clone the PHI and delete the original one. This lets IVUsers and
|
|
|
|
// any other maps purge the original user from their records.
|
2009-10-27 22:16:29 +00:00
|
|
|
PHINode *NewPN = cast<PHINode>(PN->clone());
|
2009-06-26 22:53:46 +00:00
|
|
|
NewPN->takeName(PN);
|
|
|
|
NewPN->insertBefore(PN);
|
|
|
|
PN->replaceAllUsesWith(NewPN);
|
|
|
|
PN->eraseFromParent();
|
|
|
|
}
|
2007-03-03 22:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-20 03:53:53 +00:00
|
|
|
|
|
|
|
// The insertion point instruction may have been deleted; clear it out
|
|
|
|
// so that the rewriter doesn't trip over it later.
|
|
|
|
Rewriter.clearInsertPoint();
|
2004-04-02 20:24:31 +00:00
|
|
|
}
|
2003-12-23 07:47:09 +00:00
|
|
|
|
2009-02-17 20:49:49 +00:00
|
|
|
void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
|
2009-04-16 03:18:22 +00:00
|
|
|
// First step. Check to see if there are any floating-point recurrences.
|
2004-04-02 20:24:31 +00:00
|
|
|
// If there are, change them into integer recurrences, permitting analysis by
|
|
|
|
// the SCEV routines.
|
2003-12-23 07:47:09 +00:00
|
|
|
//
|
2004-04-02 20:24:31 +00:00
|
|
|
BasicBlock *Header = L->getHeader();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
SmallVector<WeakVH, 8> PHIs;
|
|
|
|
for (BasicBlock::iterator I = Header->begin();
|
|
|
|
PHINode *PN = dyn_cast<PHINode>(I); ++I)
|
|
|
|
PHIs.push_back(PN);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
|
|
|
|
if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i]))
|
|
|
|
HandleFloatingPointIV(L, PN);
|
2003-12-23 07:47:09 +00:00
|
|
|
|
2009-04-16 03:18:22 +00:00
|
|
|
// If the loop previously had floating-point IV, ScalarEvolution
|
2009-02-17 20:49:49 +00:00
|
|
|
// may not have been able to compute a trip count. Now that we've done some
|
|
|
|
// re-writing, the trip count may be computable.
|
|
|
|
if (Changed)
|
2009-10-31 15:04:55 +00:00
|
|
|
SE->forgetLoop(L);
|
2009-04-15 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
2010-04-12 02:21:50 +00:00
|
|
|
void IndVarSimplify::EliminateIVComparisons() {
|
2010-04-12 07:29:15 +00:00
|
|
|
SmallVector<WeakVH, 16> DeadInsts;
|
|
|
|
|
2010-04-12 02:21:50 +00:00
|
|
|
// Look for ICmp users.
|
2010-04-12 07:29:15 +00:00
|
|
|
for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E; ++I) {
|
|
|
|
IVStrideUse &UI = *I;
|
2010-04-12 02:21:50 +00:00
|
|
|
ICmpInst *ICmp = dyn_cast<ICmpInst>(UI.getUser());
|
|
|
|
if (!ICmp) continue;
|
|
|
|
|
|
|
|
bool Swapped = UI.getOperandValToReplace() == ICmp->getOperand(1);
|
|
|
|
ICmpInst::Predicate Pred = ICmp->getPredicate();
|
|
|
|
if (Swapped) Pred = ICmpInst::getSwappedPredicate(Pred);
|
|
|
|
|
|
|
|
// Get the SCEVs for the ICmp operands.
|
|
|
|
const SCEV *S = IU->getReplacementExpr(UI);
|
|
|
|
const SCEV *X = SE->getSCEV(ICmp->getOperand(!Swapped));
|
|
|
|
|
|
|
|
// Simplify unnecessary loops away.
|
|
|
|
const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
|
|
|
|
S = SE->getSCEVAtScope(S, ICmpLoop);
|
|
|
|
X = SE->getSCEVAtScope(X, ICmpLoop);
|
|
|
|
|
|
|
|
// If the condition is always true or always false, replace it with
|
|
|
|
// a constant value.
|
|
|
|
if (SE->isKnownPredicate(Pred, S, X))
|
|
|
|
ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
|
|
|
|
else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X))
|
|
|
|
ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
|
2010-04-12 07:29:15 +00:00
|
|
|
DeadInsts.push_back(ICmp);
|
2010-04-12 02:21:50 +00:00
|
|
|
}
|
2010-04-12 07:29:15 +00:00
|
|
|
|
|
|
|
// Now that we're done iterating through lists, clean up any instructions
|
|
|
|
// which are now dead.
|
|
|
|
while (!DeadInsts.empty())
|
|
|
|
if (Instruction *Inst =
|
|
|
|
dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()))
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Inst);
|
2010-04-12 02:21:50 +00:00
|
|
|
}
|
|
|
|
|
2010-04-13 01:46:36 +00:00
|
|
|
void IndVarSimplify::EliminateIVRemainders() {
|
|
|
|
SmallVector<WeakVH, 16> DeadInsts;
|
|
|
|
|
|
|
|
// Look for SRem and URem users.
|
|
|
|
for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E; ++I) {
|
|
|
|
IVStrideUse &UI = *I;
|
|
|
|
BinaryOperator *Rem = dyn_cast<BinaryOperator>(UI.getUser());
|
|
|
|
if (!Rem) continue;
|
|
|
|
|
|
|
|
bool isSigned = Rem->getOpcode() == Instruction::SRem;
|
|
|
|
if (!isSigned && Rem->getOpcode() != Instruction::URem)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We're only interested in the case where we know something about
|
|
|
|
// the numerator.
|
|
|
|
if (UI.getOperandValToReplace() != Rem->getOperand(0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the SCEVs for the ICmp operands.
|
|
|
|
const SCEV *S = SE->getSCEV(Rem->getOperand(0));
|
|
|
|
const SCEV *X = SE->getSCEV(Rem->getOperand(1));
|
|
|
|
|
|
|
|
// Simplify unnecessary loops away.
|
|
|
|
const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
|
|
|
|
S = SE->getSCEVAtScope(S, ICmpLoop);
|
|
|
|
X = SE->getSCEVAtScope(X, ICmpLoop);
|
|
|
|
|
|
|
|
// i % n --> i if i is in [0,n).
|
|
|
|
if ((!isSigned || SE->isKnownNonNegative(S)) &&
|
|
|
|
SE->isKnownPredicate(isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
|
|
|
|
S, X))
|
|
|
|
Rem->replaceAllUsesWith(Rem->getOperand(0));
|
|
|
|
else {
|
|
|
|
// (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
|
|
|
|
const SCEV *LessOne =
|
2010-05-03 22:09:21 +00:00
|
|
|
SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1));
|
2010-04-13 01:46:36 +00:00
|
|
|
if ((!isSigned || SE->isKnownNonNegative(LessOne)) &&
|
|
|
|
SE->isKnownPredicate(isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
|
|
|
|
LessOne, X)) {
|
|
|
|
ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ,
|
|
|
|
Rem->getOperand(0), Rem->getOperand(1),
|
|
|
|
"tmp");
|
|
|
|
SelectInst *Sel =
|
|
|
|
SelectInst::Create(ICmp,
|
|
|
|
ConstantInt::get(Rem->getType(), 0),
|
|
|
|
Rem->getOperand(0), "tmp", Rem);
|
|
|
|
Rem->replaceAllUsesWith(Sel);
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inform IVUsers about the new users.
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Rem->getOperand(0)))
|
|
|
|
IU->AddUsersIfInteresting(I);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
|
|
|
|
DeadInsts.push_back(Rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we're done iterating through lists, clean up any instructions
|
|
|
|
// which are now dead.
|
|
|
|
while (!DeadInsts.empty())
|
|
|
|
if (Instruction *Inst =
|
|
|
|
dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()))
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Inst);
|
|
|
|
}
|
|
|
|
|
2009-02-12 22:19:27 +00:00
|
|
|
bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
|
2010-06-18 01:35:11 +00:00
|
|
|
// If LoopSimplify form is not available, stay out of trouble. Some notes:
|
|
|
|
// - LSR currently only supports LoopSimplify-form loops. Indvars'
|
|
|
|
// canonicalization can be a pessimization without LSR to "clean up"
|
|
|
|
// afterwards.
|
|
|
|
// - We depend on having a preheader; in particular,
|
|
|
|
// Loop::getCanonicalInductionVariable only supports loops with preheaders,
|
|
|
|
// and we're in trouble if we can't find the induction variable even when
|
|
|
|
// we've manually inserted one.
|
|
|
|
if (!L->isLoopSimplifyForm())
|
|
|
|
return false;
|
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
IU = &getAnalysis<IVUsers>();
|
2007-03-07 06:39:01 +00:00
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2009-06-27 05:16:57 +00:00
|
|
|
DT = &getAnalysis<DominatorTree>();
|
2007-03-07 06:39:01 +00:00
|
|
|
Changed = false;
|
2009-02-17 20:49:49 +00:00
|
|
|
|
2009-04-16 03:18:22 +00:00
|
|
|
// If there are any floating-point recurrences, attempt to
|
2009-02-17 20:49:49 +00:00
|
|
|
// transform them to use integer recurrences.
|
|
|
|
RewriteNonIntegerIVs(L);
|
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
|
2007-03-04 01:00:28 +00:00
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
// Create a rewriter object which we'll use to transform the code with.
|
|
|
|
SCEVExpander Rewriter(*SE);
|
|
|
|
|
2004-04-02 20:24:31 +00:00
|
|
|
// Check to see if this loop has a computable loop-invariant execution count.
|
|
|
|
// If so, this means that we can compute the final value of any expressions
|
|
|
|
// that are recurrent in the loop, and substitute the exit values from the
|
|
|
|
// loop into any instructions outside of the loop that use the final values of
|
|
|
|
// the current expressions.
|
2001-12-04 04:32:29 +00:00
|
|
|
//
|
2009-02-24 18:55:53 +00:00
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
|
2010-02-22 04:11:59 +00:00
|
|
|
RewriteLoopExitValues(L, Rewriter);
|
2004-04-02 20:24:31 +00:00
|
|
|
|
2010-04-12 07:56:56 +00:00
|
|
|
// Simplify ICmp IV users.
|
|
|
|
EliminateIVComparisons();
|
|
|
|
|
2010-04-13 01:46:36 +00:00
|
|
|
// Simplify SRem and URem IV users.
|
|
|
|
EliminateIVRemainders();
|
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Compute the type of the largest recurrence expression, and decide whether
|
|
|
|
// a canonical induction variable should be inserted.
|
2009-02-12 22:19:27 +00:00
|
|
|
const Type *LargestType = 0;
|
2009-05-12 02:17:14 +00:00
|
|
|
bool NeedCannIV = false;
|
2009-02-24 18:55:53 +00:00
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
|
|
|
|
LargestType = BackedgeTakenCount->getType();
|
2009-04-21 01:07:12 +00:00
|
|
|
LargestType = SE->getEffectiveSCEVType(LargestType);
|
2009-05-12 02:17:14 +00:00
|
|
|
// If we have a known trip count and a single exit block, we'll be
|
|
|
|
// rewriting the loop exit test condition below, which requires a
|
|
|
|
// canonical induction variable.
|
|
|
|
if (ExitingBlock)
|
|
|
|
NeedCannIV = true;
|
2004-04-17 18:08:33 +00:00
|
|
|
}
|
2010-02-12 10:34:29 +00:00
|
|
|
for (IVUsers::const_iterator I = IU->begin(), E = IU->end(); I != E; ++I) {
|
|
|
|
const Type *Ty =
|
|
|
|
SE->getEffectiveSCEVType(I->getOperandValToReplace()->getType());
|
2009-02-12 22:19:27 +00:00
|
|
|
if (!LargestType ||
|
2009-05-12 02:17:14 +00:00
|
|
|
SE->getTypeSizeInBits(Ty) >
|
2009-04-21 01:07:12 +00:00
|
|
|
SE->getTypeSizeInBits(LargestType))
|
2009-05-12 02:17:14 +00:00
|
|
|
LargestType = Ty;
|
2010-02-12 10:34:29 +00:00
|
|
|
NeedCannIV = true;
|
2003-12-22 09:53:29 +00:00
|
|
|
}
|
2001-12-04 04:32:29 +00:00
|
|
|
|
2010-02-10 16:03:48 +00:00
|
|
|
// Now that we know the largest of the induction variable expressions
|
2009-05-12 02:17:14 +00:00
|
|
|
// in this loop, insert a canonical induction variable of the largest size.
|
2009-02-12 22:19:27 +00:00
|
|
|
Value *IndVar = 0;
|
2009-05-12 02:17:14 +00:00
|
|
|
if (NeedCannIV) {
|
2010-02-25 06:57:05 +00:00
|
|
|
// Check to see if the loop already has any canonical-looking induction
|
|
|
|
// variables. If any are present and wider than the planned canonical
|
|
|
|
// induction variable, temporarily remove them, so that the Rewriter
|
|
|
|
// doesn't attempt to reuse them.
|
|
|
|
SmallVector<PHINode *, 2> OldCannIVs;
|
|
|
|
while (PHINode *OldCannIV = L->getCanonicalInductionVariable()) {
|
2009-06-13 16:25:49 +00:00
|
|
|
if (SE->getTypeSizeInBits(OldCannIV->getType()) >
|
|
|
|
SE->getTypeSizeInBits(LargestType))
|
|
|
|
OldCannIV->removeFromParent();
|
|
|
|
else
|
2010-02-25 06:57:05 +00:00
|
|
|
break;
|
|
|
|
OldCannIVs.push_back(OldCannIV);
|
2009-06-13 16:25:49 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType);
|
2009-06-13 16:25:49 +00:00
|
|
|
|
2009-02-12 22:19:27 +00:00
|
|
|
++NumInserted;
|
|
|
|
Changed = true;
|
2010-01-05 01:27:06 +00:00
|
|
|
DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n');
|
2009-06-13 16:25:49 +00:00
|
|
|
|
|
|
|
// Now that the official induction variable is established, reinsert
|
2010-02-25 06:57:05 +00:00
|
|
|
// any old canonical-looking variables after it so that the IR remains
|
|
|
|
// consistent. They will be deleted as part of the dead-PHI deletion at
|
2009-06-13 16:25:49 +00:00
|
|
|
// the end of the pass.
|
2010-02-25 06:57:05 +00:00
|
|
|
while (!OldCannIVs.empty()) {
|
|
|
|
PHINode *OldCannIV = OldCannIVs.pop_back_val();
|
|
|
|
OldCannIV->insertBefore(L->getHeader()->getFirstNonPHI());
|
|
|
|
}
|
2007-06-15 14:38:12 +00:00
|
|
|
}
|
2004-04-02 20:24:31 +00:00
|
|
|
|
2009-02-12 22:19:27 +00:00
|
|
|
// If we have a trip count expression, rewrite the loop's exit condition
|
|
|
|
// using it. We can currently only handle loops with a single exit.
|
2009-05-12 02:17:14 +00:00
|
|
|
ICmpInst *NewICmp = 0;
|
2010-02-25 06:57:05 +00:00
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
|
|
|
|
!BackedgeTakenCount->isZero() &&
|
|
|
|
ExitingBlock) {
|
2009-05-12 02:17:14 +00:00
|
|
|
assert(NeedCannIV &&
|
|
|
|
"LinearFunctionTestReplace requires a canonical induction variable");
|
2009-02-12 22:19:27 +00:00
|
|
|
// Can't rewrite non-branch yet.
|
2010-04-12 07:56:56 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
|
2009-05-12 02:17:14 +00:00
|
|
|
NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
|
|
|
|
ExitingBlock, BI, Rewriter);
|
|
|
|
}
|
2009-02-12 22:19:27 +00:00
|
|
|
|
2009-05-24 20:08:21 +00:00
|
|
|
// Rewrite IV-derived expressions. Clears the rewriter cache.
|
2010-02-22 04:11:59 +00:00
|
|
|
RewriteIVExpressions(L, Rewriter);
|
2004-04-22 14:59:40 +00:00
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
// The Rewriter may not be used from this point on.
|
2009-05-24 20:08:21 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Loop-invariant instructions in the preheader that aren't used in the
|
|
|
|
// loop may be sunk below the loop to reduce register pressure.
|
2009-06-26 22:53:46 +00:00
|
|
|
SinkUnusedInvariants(L);
|
2009-05-12 02:17:14 +00:00
|
|
|
|
|
|
|
// For completeness, inform IVUsers of the IV use in the newly-created
|
|
|
|
// loop exit test instruction.
|
|
|
|
if (NewICmp)
|
|
|
|
IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0)));
|
|
|
|
|
|
|
|
// Clean up dead instructions.
|
2010-01-05 16:31:45 +00:00
|
|
|
Changed |= DeleteDeadPHIs(L->getHeader());
|
2009-05-12 02:17:14 +00:00
|
|
|
// Check a post-condition.
|
2010-03-10 19:38:49 +00:00
|
|
|
assert(L->isLCSSAForm(*DT) && "Indvars did not leave the loop in lcssa form!");
|
2009-05-12 02:17:14 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-04-07 22:27:08 +00:00
|
|
|
// FIXME: It is an extremely bad idea to indvar substitute anything more
|
|
|
|
// complex than affine induction variables. Doing so will put expensive
|
|
|
|
// polynomial evaluations inside of the loop, and the str reduction pass
|
|
|
|
// currently can only reduce affine polynomials. For now just disable
|
|
|
|
// indvar subst on anything more complex than an affine addrec, unless
|
|
|
|
// it can be expanded to a trivial value.
|
|
|
|
static bool isSafe(const SCEV *S, const Loop *L) {
|
|
|
|
// Loop-invariant values are safe.
|
|
|
|
if (S->isLoopInvariant(L)) return true;
|
|
|
|
|
|
|
|
// Affine addrecs are safe. Non-affine are not, because LSR doesn't know how
|
|
|
|
// to transform them into efficient code.
|
|
|
|
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
|
|
|
|
return AR->isAffine();
|
|
|
|
|
|
|
|
// An add is safe it all its operands are safe.
|
|
|
|
if (const SCEVCommutativeExpr *Commutative = dyn_cast<SCEVCommutativeExpr>(S)) {
|
|
|
|
for (SCEVCommutativeExpr::op_iterator I = Commutative->op_begin(),
|
|
|
|
E = Commutative->op_end(); I != E; ++I)
|
|
|
|
if (!isSafe(*I, L)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A cast is safe if its operand is.
|
|
|
|
if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S))
|
|
|
|
return isSafe(C->getOperand(), L);
|
|
|
|
|
|
|
|
// A udiv is safe if its operands are.
|
|
|
|
if (const SCEVUDivExpr *UD = dyn_cast<SCEVUDivExpr>(S))
|
|
|
|
return isSafe(UD->getLHS(), L) &&
|
|
|
|
isSafe(UD->getRHS(), L);
|
|
|
|
|
|
|
|
// SCEVUnknown is always safe.
|
|
|
|
if (isa<SCEVUnknown>(S))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Nothing else is safe.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-22 04:11:59 +00:00
|
|
|
void IndVarSimplify::RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter) {
|
2009-05-12 02:17:14 +00:00
|
|
|
SmallVector<WeakVH, 16> DeadInsts;
|
|
|
|
|
|
|
|
// Rewrite all induction variable expressions in terms of the canonical
|
|
|
|
// induction variable.
|
|
|
|
//
|
|
|
|
// If there were induction variables of other sizes or offsets, manually
|
|
|
|
// add the offsets to the primary induction variable and cast, avoiding
|
|
|
|
// the need for the code evaluation methods to insert induction variables
|
|
|
|
// of different sizes.
|
2010-02-12 10:34:29 +00:00
|
|
|
for (IVUsers::iterator UI = IU->begin(), E = IU->end(); UI != E; ++UI) {
|
|
|
|
Value *Op = UI->getOperandValToReplace();
|
|
|
|
const Type *UseTy = Op->getType();
|
|
|
|
Instruction *User = UI->getUser();
|
|
|
|
|
|
|
|
// Compute the final addrec to expand into code.
|
|
|
|
const SCEV *AR = IU->getReplacementExpr(*UI);
|
|
|
|
|
|
|
|
// Evaluate the expression out of the loop, if possible.
|
|
|
|
if (!L->contains(UI->getUser())) {
|
|
|
|
const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop());
|
|
|
|
if (ExitVal->isLoopInvariant(L))
|
|
|
|
AR = ExitVal;
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
2010-02-12 10:34:29 +00:00
|
|
|
|
|
|
|
// FIXME: It is an extremely bad idea to indvar substitute anything more
|
|
|
|
// complex than affine induction variables. Doing so will put expensive
|
|
|
|
// polynomial evaluations inside of the loop, and the str reduction pass
|
|
|
|
// currently can only reduce affine polynomials. For now just disable
|
|
|
|
// indvar subst on anything more complex than an affine addrec, unless
|
|
|
|
// it can be expanded to a trivial value.
|
2010-04-07 22:27:08 +00:00
|
|
|
if (!isSafe(AR, L))
|
2010-02-12 10:34:29 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Determine the insertion point for this user. By default, insert
|
|
|
|
// immediately before the user. The SCEVExpander class will automatically
|
|
|
|
// hoist loop invariants out of the loop. For PHI nodes, there may be
|
|
|
|
// multiple uses, so compute the nearest common dominator for the
|
|
|
|
// incoming blocks.
|
|
|
|
Instruction *InsertPt = User;
|
|
|
|
if (PHINode *PHI = dyn_cast<PHINode>(InsertPt))
|
|
|
|
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
|
|
|
|
if (PHI->getIncomingValue(i) == Op) {
|
|
|
|
if (InsertPt == User)
|
|
|
|
InsertPt = PHI->getIncomingBlock(i)->getTerminator();
|
|
|
|
else
|
|
|
|
InsertPt =
|
|
|
|
DT->findNearestCommonDominator(InsertPt->getParent(),
|
|
|
|
PHI->getIncomingBlock(i))
|
|
|
|
->getTerminator();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now expand it into actual Instructions and patch it into place.
|
|
|
|
Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt);
|
|
|
|
|
2010-04-02 14:48:31 +00:00
|
|
|
// Inform ScalarEvolution that this value is changing. The change doesn't
|
|
|
|
// affect its value, but it does potentially affect which use lists the
|
|
|
|
// value will be on after the replacement, which affects ScalarEvolution's
|
|
|
|
// ability to walk use lists and drop dangling pointers when a value is
|
|
|
|
// deleted.
|
|
|
|
SE->forgetValue(User);
|
|
|
|
|
2010-02-12 10:34:29 +00:00
|
|
|
// Patch the new value into place.
|
|
|
|
if (Op->hasName())
|
|
|
|
NewVal->takeName(Op);
|
|
|
|
User->replaceUsesOfWith(Op, NewVal);
|
|
|
|
UI->setOperandValToReplace(NewVal);
|
|
|
|
DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n'
|
|
|
|
<< " into = " << *NewVal << "\n");
|
|
|
|
++NumRemoved;
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
// The old value may be dead now.
|
|
|
|
DeadInsts.push_back(Op);
|
2003-12-22 09:53:29 +00:00
|
|
|
}
|
2003-12-10 18:06:47 +00:00
|
|
|
|
2009-05-24 20:08:21 +00:00
|
|
|
// Clear the rewriter cache, because values that are in the rewriter's cache
|
|
|
|
// can be deleted in the loop below, causing the AssertingVH in the cache to
|
|
|
|
// trigger.
|
|
|
|
Rewriter.clear();
|
2009-05-12 02:17:14 +00:00
|
|
|
// Now that we're done iterating through lists, clean up any instructions
|
|
|
|
// which are now dead.
|
2010-01-21 02:09:26 +00:00
|
|
|
while (!DeadInsts.empty())
|
|
|
|
if (Instruction *Inst =
|
|
|
|
dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()))
|
2009-05-12 02:17:14 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If there's a single exit block, sink any loop-invariant values that
|
|
|
|
/// were defined in the preheader but not used inside the loop into the
|
|
|
|
/// exit block to reduce register pressure in the loop.
|
2009-06-26 22:53:46 +00:00
|
|
|
void IndVarSimplify::SinkUnusedInvariants(Loop *L) {
|
2009-05-12 02:17:14 +00:00
|
|
|
BasicBlock *ExitBlock = L->getExitBlock();
|
|
|
|
if (!ExitBlock) return;
|
|
|
|
|
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
2009-11-05 21:11:53 +00:00
|
|
|
if (!Preheader) return;
|
|
|
|
|
|
|
|
Instruction *InsertPt = ExitBlock->getFirstNonPHI();
|
2009-05-12 02:17:14 +00:00
|
|
|
BasicBlock::iterator I = Preheader->getTerminator();
|
|
|
|
while (I != Preheader->begin()) {
|
|
|
|
--I;
|
2009-06-26 22:53:46 +00:00
|
|
|
// New instructions were inserted at the end of the preheader.
|
|
|
|
if (isa<PHINode>(I))
|
2009-05-12 02:17:14 +00:00
|
|
|
break;
|
2010-03-23 21:15:59 +00:00
|
|
|
|
2009-07-15 22:48:29 +00:00
|
|
|
// Don't move instructions which might have side effects, since the side
|
2010-03-23 21:15:59 +00:00
|
|
|
// effects need to complete before instructions inside the loop. Also don't
|
|
|
|
// move instructions which might read memory, since the loop may modify
|
|
|
|
// memory. Note that it's okay if the instruction might have undefined
|
|
|
|
// behavior: LoopSimplify guarantees that the preheader dominates the exit
|
|
|
|
// block.
|
2009-07-15 22:48:29 +00:00
|
|
|
if (I->mayHaveSideEffects() || I->mayReadFromMemory())
|
2009-06-26 22:53:46 +00:00
|
|
|
continue;
|
2010-03-23 21:15:59 +00:00
|
|
|
|
2010-03-15 22:23:03 +00:00
|
|
|
// Skip debug info intrinsics.
|
|
|
|
if (isa<DbgInfoIntrinsic>(I))
|
|
|
|
continue;
|
2010-03-23 21:15:59 +00:00
|
|
|
|
2009-08-25 17:42:10 +00:00
|
|
|
// Don't sink static AllocaInsts out of the entry block, which would
|
|
|
|
// turn them into dynamic allocas!
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
|
|
|
|
if (AI->isStaticAlloca())
|
|
|
|
continue;
|
2010-03-23 21:15:59 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Determine if there is a use in or before the loop (direct or
|
|
|
|
// otherwise).
|
|
|
|
bool UsedInLoop = false;
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
|
|
|
|
UI != UE; ++UI) {
|
2010-07-09 15:40:10 +00:00
|
|
|
User *U = *UI;
|
|
|
|
BasicBlock *UseBB = cast<Instruction>(U)->getParent();
|
|
|
|
if (PHINode *P = dyn_cast<PHINode>(U)) {
|
2009-05-12 02:17:14 +00:00
|
|
|
unsigned i =
|
|
|
|
PHINode::getIncomingValueNumForOperand(UI.getOperandNo());
|
|
|
|
UseBB = P->getIncomingBlock(i);
|
|
|
|
}
|
|
|
|
if (UseBB == Preheader || L->contains(UseBB)) {
|
|
|
|
UsedInLoop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-03-23 21:15:59 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// If there is, the def must remain in the preheader.
|
|
|
|
if (UsedInLoop)
|
|
|
|
continue;
|
2010-03-23 21:15:59 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Otherwise, sink it to the exit block.
|
|
|
|
Instruction *ToMove = I;
|
|
|
|
bool Done = false;
|
2010-03-23 21:15:59 +00:00
|
|
|
|
|
|
|
if (I != Preheader->begin()) {
|
|
|
|
// Skip debug info intrinsics.
|
|
|
|
do {
|
|
|
|
--I;
|
|
|
|
} while (isa<DbgInfoIntrinsic>(I) && I != Preheader->begin());
|
|
|
|
|
|
|
|
if (isa<DbgInfoIntrinsic>(I) && I == Preheader->begin())
|
|
|
|
Done = true;
|
|
|
|
} else {
|
2009-05-12 02:17:14 +00:00
|
|
|
Done = true;
|
2010-03-23 21:15:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
ToMove->moveBefore(InsertPt);
|
2010-03-23 21:15:59 +00:00
|
|
|
if (Done) break;
|
2009-06-26 22:53:46 +00:00
|
|
|
InsertPt = ToMove;
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-03 06:41:49 +00:00
|
|
|
/// ConvertToSInt - Convert APF to an integer, if possible.
|
|
|
|
static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal) {
|
2008-11-17 23:27:13 +00:00
|
|
|
bool isExact = false;
|
2008-11-26 01:11:57 +00:00
|
|
|
if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
|
|
|
|
return false;
|
2010-04-03 06:41:49 +00:00
|
|
|
// See if we can convert this to an int64_t
|
|
|
|
uint64_t UIntVal;
|
|
|
|
if (APF.convertToInteger(&UIntVal, 64, true, APFloat::rmTowardZero,
|
|
|
|
&isExact) != APFloat::opOK || !isExact)
|
2008-11-17 23:27:13 +00:00
|
|
|
return false;
|
2010-04-03 06:41:49 +00:00
|
|
|
IntVal = UIntVal;
|
2008-11-17 23:27:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-11-03 18:32:19 +00:00
|
|
|
/// HandleFloatingPointIV - If the loop has floating induction variable
|
|
|
|
/// then insert corresponding integer induction variable if possible.
|
2008-11-17 21:32:02 +00:00
|
|
|
/// For example,
|
|
|
|
/// for(double i = 0; i < 10000; ++i)
|
|
|
|
/// bar(i)
|
|
|
|
/// is converted into
|
|
|
|
/// for(int i = 0; i < 10000; ++i)
|
|
|
|
/// bar((double)i);
|
|
|
|
///
|
2010-04-03 06:17:08 +00:00
|
|
|
void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) {
|
|
|
|
unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
|
2008-11-17 21:32:02 +00:00
|
|
|
unsigned BackEdge = IncomingEdge^1;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Check incoming value.
|
2010-04-03 06:05:10 +00:00
|
|
|
ConstantFP *InitValueVal =
|
2010-04-03 06:17:08 +00:00
|
|
|
dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge));
|
2010-04-03 07:18:48 +00:00
|
|
|
|
2010-04-03 06:41:49 +00:00
|
|
|
int64_t InitValue;
|
2010-04-03 07:18:48 +00:00
|
|
|
if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue))
|
2008-11-17 23:27:13 +00:00
|
|
|
return;
|
|
|
|
|
2010-04-03 06:17:08 +00:00
|
|
|
// Check IV increment. Reject this PN if increment operation is not
|
2008-11-17 23:27:13 +00:00
|
|
|
// an add or increment value can not be represented by an integer.
|
2009-02-17 19:13:57 +00:00
|
|
|
BinaryOperator *Incr =
|
2010-04-03 06:17:08 +00:00
|
|
|
dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge));
|
2010-04-03 05:54:59 +00:00
|
|
|
if (Incr == 0 || Incr->getOpcode() != Instruction::FAdd) return;
|
|
|
|
|
|
|
|
// If this is not an add of the PHI with a constantfp, or if the constant fp
|
|
|
|
// is not an integer, bail out.
|
2010-04-03 06:05:10 +00:00
|
|
|
ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1));
|
2010-04-03 07:18:48 +00:00
|
|
|
int64_t IncValue;
|
2010-04-03 06:17:08 +00:00
|
|
|
if (IncValueVal == 0 || Incr->getOperand(0) != PN ||
|
2010-04-03 07:18:48 +00:00
|
|
|
!ConvertToSInt(IncValueVal->getValueAPF(), IncValue))
|
2008-11-17 23:27:13 +00:00
|
|
|
return;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2010-04-03 06:17:08 +00:00
|
|
|
// Check Incr uses. One user is PN and the other user is an exit condition
|
2010-04-03 05:54:59 +00:00
|
|
|
// used by the conditional terminator.
|
2008-11-17 21:32:02 +00:00
|
|
|
Value::use_iterator IncrUse = Incr->use_begin();
|
|
|
|
Instruction *U1 = cast<Instruction>(IncrUse++);
|
|
|
|
if (IncrUse == Incr->use_end()) return;
|
|
|
|
Instruction *U2 = cast<Instruction>(IncrUse++);
|
|
|
|
if (IncrUse != Incr->use_end()) return;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2010-04-03 05:54:59 +00:00
|
|
|
// Find exit condition, which is an fcmp. If it doesn't exist, or if it isn't
|
|
|
|
// only used by a branch, we can't transform it.
|
2010-04-03 06:11:07 +00:00
|
|
|
FCmpInst *Compare = dyn_cast<FCmpInst>(U1);
|
|
|
|
if (!Compare)
|
|
|
|
Compare = dyn_cast<FCmpInst>(U2);
|
|
|
|
if (Compare == 0 || !Compare->hasOneUse() ||
|
|
|
|
!isa<BranchInst>(Compare->use_back()))
|
2010-04-03 05:54:59 +00:00
|
|
|
return;
|
2010-04-03 06:05:10 +00:00
|
|
|
|
2010-04-03 06:11:07 +00:00
|
|
|
BranchInst *TheBr = cast<BranchInst>(Compare->use_back());
|
2008-11-17 21:32:02 +00:00
|
|
|
|
2010-04-03 07:21:39 +00:00
|
|
|
// We need to verify that the branch actually controls the iteration count
|
|
|
|
// of the loop. If not, the new IV can overflow and no one will notice.
|
|
|
|
// The branch block must be in the loop and one of the successors must be out
|
|
|
|
// of the loop.
|
|
|
|
assert(TheBr->isConditional() && "Can't use fcmp if not conditional");
|
|
|
|
if (!L->contains(TheBr->getParent()) ||
|
|
|
|
(L->contains(TheBr->getSuccessor(0)) &&
|
|
|
|
L->contains(TheBr->getSuccessor(1))))
|
|
|
|
return;
|
2010-04-03 07:18:48 +00:00
|
|
|
|
|
|
|
|
2010-04-03 05:54:59 +00:00
|
|
|
// If it isn't a comparison with an integer-as-fp (the exit value), we can't
|
|
|
|
// transform it.
|
2010-04-03 06:11:07 +00:00
|
|
|
ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1));
|
2010-04-03 06:41:49 +00:00
|
|
|
int64_t ExitValue;
|
|
|
|
if (ExitValueVal == 0 ||
|
|
|
|
!ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue))
|
2008-11-17 21:32:02 +00:00
|
|
|
return;
|
2010-04-03 06:41:49 +00:00
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Find new predicate for integer comparison.
|
|
|
|
CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
|
2010-04-03 06:11:07 +00:00
|
|
|
switch (Compare->getPredicate()) {
|
2010-04-03 06:05:10 +00:00
|
|
|
default: return; // Unknown comparison.
|
2008-11-17 21:32:02 +00:00
|
|
|
case CmpInst::FCMP_OEQ:
|
2010-04-03 06:05:10 +00:00
|
|
|
case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break;
|
2010-04-03 07:18:48 +00:00
|
|
|
case CmpInst::FCMP_ONE:
|
|
|
|
case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break;
|
2008-11-17 21:32:02 +00:00
|
|
|
case CmpInst::FCMP_OGT:
|
2010-04-03 06:25:21 +00:00
|
|
|
case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break;
|
2008-11-17 21:32:02 +00:00
|
|
|
case CmpInst::FCMP_OGE:
|
2010-04-03 06:25:21 +00:00
|
|
|
case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break;
|
2008-11-17 21:32:02 +00:00
|
|
|
case CmpInst::FCMP_OLT:
|
2010-04-03 06:30:03 +00:00
|
|
|
case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break;
|
2008-11-17 21:32:02 +00:00
|
|
|
case CmpInst::FCMP_OLE:
|
2010-04-03 06:30:03 +00:00
|
|
|
case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break;
|
2008-11-03 18:32:19 +00:00
|
|
|
}
|
2010-04-03 07:18:48 +00:00
|
|
|
|
|
|
|
// We convert the floating point induction variable to a signed i32 value if
|
|
|
|
// we can. This is only safe if the comparison will not overflow in a way
|
|
|
|
// that won't be trapped by the integer equivalent operations. Check for this
|
|
|
|
// now.
|
|
|
|
// TODO: We could use i64 if it is native and the range requires it.
|
|
|
|
|
|
|
|
// The start/stride/exit values must all fit in signed i32.
|
|
|
|
if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If not actually striding (add x, 0.0), avoid touching the code.
|
|
|
|
if (IncValue == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Positive and negative strides have different safety conditions.
|
|
|
|
if (IncValue > 0) {
|
|
|
|
// If we have a positive stride, we require the init to be less than the
|
|
|
|
// exit value and an equality or less than comparison.
|
|
|
|
if (InitValue >= ExitValue ||
|
|
|
|
NewPred == CmpInst::ICMP_SGT || NewPred == CmpInst::ICMP_SGE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t Range = uint32_t(ExitValue-InitValue);
|
|
|
|
if (NewPred == CmpInst::ICMP_SLE) {
|
|
|
|
// Normalize SLE -> SLT, check for infinite loop.
|
|
|
|
if (++Range == 0) return; // Range overflows.
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Leftover = Range % uint32_t(IncValue);
|
|
|
|
|
|
|
|
// If this is an equality comparison, we require that the strided value
|
|
|
|
// exactly land on the exit value, otherwise the IV condition will wrap
|
|
|
|
// around and do things the fp IV wouldn't.
|
|
|
|
if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
|
|
|
|
Leftover != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the stride would wrap around the i32 before exiting, we can't
|
|
|
|
// transform the IV.
|
|
|
|
if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// If we have a negative stride, we require the init to be greater than the
|
|
|
|
// exit value and an equality or greater than comparison.
|
|
|
|
if (InitValue >= ExitValue ||
|
|
|
|
NewPred == CmpInst::ICMP_SLT || NewPred == CmpInst::ICMP_SLE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t Range = uint32_t(InitValue-ExitValue);
|
|
|
|
if (NewPred == CmpInst::ICMP_SGE) {
|
|
|
|
// Normalize SGE -> SGT, check for infinite loop.
|
|
|
|
if (++Range == 0) return; // Range overflows.
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Leftover = Range % uint32_t(-IncValue);
|
|
|
|
|
|
|
|
// If this is an equality comparison, we require that the strided value
|
|
|
|
// exactly land on the exit value, otherwise the IV condition will wrap
|
|
|
|
// around and do things the fp IV wouldn't.
|
|
|
|
if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
|
|
|
|
Leftover != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the stride would wrap around the i32 before exiting, we can't
|
|
|
|
// transform the IV.
|
|
|
|
if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext());
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2010-04-03 06:41:49 +00:00
|
|
|
// Insert new integer induction variable.
|
2010-04-03 06:17:08 +00:00
|
|
|
PHINode *NewPHI = PHINode::Create(Int32Ty, PN->getName()+".int", PN);
|
2010-04-03 06:05:10 +00:00
|
|
|
NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue),
|
2010-04-03 06:17:08 +00:00
|
|
|
PN->getIncomingBlock(IncomingEdge));
|
2008-11-17 21:32:02 +00:00
|
|
|
|
2010-04-03 06:05:10 +00:00
|
|
|
Value *NewAdd =
|
2010-04-03 07:18:48 +00:00
|
|
|
BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue),
|
2010-04-03 06:05:10 +00:00
|
|
|
Incr->getName()+".int", Incr);
|
2010-04-03 06:17:08 +00:00
|
|
|
NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge));
|
2008-11-17 21:32:02 +00:00
|
|
|
|
2010-04-03 06:11:07 +00:00
|
|
|
ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, NewAdd,
|
|
|
|
ConstantInt::get(Int32Ty, ExitValue),
|
|
|
|
Compare->getName());
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2010-04-03 06:17:08 +00:00
|
|
|
// In the following deletions, PN may become dead and may be deleted.
|
2009-05-12 02:17:14 +00:00
|
|
|
// Use a WeakVH to observe whether this happens.
|
2010-04-03 06:17:08 +00:00
|
|
|
WeakVH WeakPH = PN;
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2010-04-03 06:11:07 +00:00
|
|
|
// Delete the old floating point exit comparison. The branch starts using the
|
|
|
|
// new comparison.
|
|
|
|
NewCompare->takeName(Compare);
|
|
|
|
Compare->replaceAllUsesWith(NewCompare);
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Compare);
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2010-04-03 06:11:07 +00:00
|
|
|
// Delete the old floating point increment.
|
2009-07-30 23:03:37 +00:00
|
|
|
Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
|
2009-05-12 02:17:14 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Incr);
|
|
|
|
|
2010-04-03 06:16:22 +00:00
|
|
|
// If the FP induction variable still has uses, this is because something else
|
|
|
|
// in the loop uses its value. In order to canonicalize the induction
|
|
|
|
// variable, we chose to eliminate the IV and rewrite it in terms of an
|
|
|
|
// int->fp cast.
|
|
|
|
//
|
|
|
|
// We give preference to sitofp over uitofp because it is faster on most
|
|
|
|
// platforms.
|
|
|
|
if (WeakPH) {
|
2010-04-03 06:25:21 +00:00
|
|
|
Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
|
|
|
|
PN->getParent()->getFirstNonPHI());
|
|
|
|
PN->replaceAllUsesWith(Conv);
|
2010-04-03 06:17:08 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(PN);
|
2008-11-17 23:27:13 +00:00
|
|
|
}
|
2008-11-03 18:32:19 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Add a new IVUsers entry for the newly-created integer PHI.
|
|
|
|
IU->AddUsersIfInteresting(NewPHI);
|
|
|
|
}
|