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"
|
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
|
|
|
|
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);
|
2009-06-26 22:53:46 +00:00
|
|
|
void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount,
|
|
|
|
SCEVExpander &Rewriter);
|
2004-04-02 20:24:31 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
void RewriteIVExpressions(Loop *L, const Type *LargestType,
|
2009-06-26 22:53:46 +00:00
|
|
|
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) {
|
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.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
|
|
|
|
const SCEV *N =
|
2009-02-24 18:55:53 +00:00
|
|
|
SE->getAddExpr(BackedgeTakenCount,
|
|
|
|
SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
|
2009-02-12 22:19:27 +00:00
|
|
|
if ((isa<SCEVConstant>(N) && !N->isZero()) ||
|
|
|
|
SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
|
|
|
|
// 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,
|
|
|
|
SE->getIntegerSCEV(1, IndVar->getType()));
|
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
|
|
|
|
2009-08-23 04:37:46 +00:00
|
|
|
DEBUG(errs() << "INDVARS: Rewriting loop exit condition to:\n"
|
|
|
|
<< " 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
|
|
|
|
|
|
|
Instruction *OrigCond = cast<Instruction>(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
|
|
|
const SCEV *BackedgeTakenCount,
|
|
|
|
SCEVExpander &Rewriter) {
|
2009-05-12 02:17:14 +00:00
|
|
|
// Verify the input to the pass in already in LCSSA form.
|
|
|
|
assert(L->isLCSSAForm());
|
|
|
|
|
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
|
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);
|
|
|
|
if (!isa<Instruction>(InVal) ||
|
|
|
|
// SCEV only supports integer expressions for now.
|
2009-04-16 03:18:22 +00:00
|
|
|
(!isa<IntegerType>(InVal->getType()) &&
|
|
|
|
!isa<PointerType>(InVal->getType())))
|
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);
|
|
|
|
if (!L->contains(Inst->getParent()))
|
|
|
|
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
|
|
|
|
2009-08-23 04:37:46 +00:00
|
|
|
DEBUG(errs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
|
|
|
|
<< " 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
|
|
|
}
|
|
|
|
}
|
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-02-24 18:55:53 +00:00
|
|
|
SE->forgetLoopBackedgeTakenCount(L);
|
2009-04-15 23:31:51 +00:00
|
|
|
}
|
|
|
|
|
2009-02-12 22:19:27 +00:00
|
|
|
bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
|
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))
|
2009-06-26 22:53:46 +00:00
|
|
|
RewriteLoopExitValues(L, BackedgeTakenCount, Rewriter);
|
2004-04-02 20:24:31 +00:00
|
|
|
|
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
|
|
|
}
|
2009-05-12 02:17:14 +00:00
|
|
|
for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *Stride = IU->StrideOrder[i];
|
2009-05-12 02:17:14 +00:00
|
|
|
const Type *Ty = SE->getEffectiveSCEVType(Stride->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;
|
|
|
|
|
2009-07-07 17:06:11 +00:00
|
|
|
std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI =
|
2009-05-12 02:17:14 +00:00
|
|
|
IU->IVUsesByStride.find(IU->StrideOrder[i]);
|
|
|
|
assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!");
|
|
|
|
|
|
|
|
if (!SI->second->Users.empty())
|
|
|
|
NeedCannIV = true;
|
2003-12-22 09:53:29 +00:00
|
|
|
}
|
2001-12-04 04:32:29 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Now that we know the largest of of the induction variable expressions
|
|
|
|
// 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) {
|
2009-06-13 16:25:49 +00:00
|
|
|
// Check to see if the loop already has a canonical-looking induction
|
|
|
|
// variable. If one is present and it's wider than the planned canonical
|
|
|
|
// induction variable, temporarily remove it, so that the Rewriter
|
|
|
|
// doesn't attempt to reuse it.
|
|
|
|
PHINode *OldCannIV = L->getCanonicalInductionVariable();
|
|
|
|
if (OldCannIV) {
|
|
|
|
if (SE->getTypeSizeInBits(OldCannIV->getType()) >
|
|
|
|
SE->getTypeSizeInBits(LargestType))
|
|
|
|
OldCannIV->removeFromParent();
|
|
|
|
else
|
|
|
|
OldCannIV = 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-08-23 04:37:46 +00:00
|
|
|
DEBUG(errs() << "INDVARS: New CanIV: " << *IndVar << '\n');
|
2009-06-13 16:25:49 +00:00
|
|
|
|
|
|
|
// Now that the official induction variable is established, reinsert
|
|
|
|
// the old canonical-looking variable after it so that the IR remains
|
|
|
|
// consistent. It will be deleted as part of the dead-PHI deletion at
|
|
|
|
// the end of the pass.
|
|
|
|
if (OldCannIV)
|
|
|
|
OldCannIV->insertAfter(cast<Instruction>(IndVar));
|
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;
|
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) {
|
|
|
|
assert(NeedCannIV &&
|
|
|
|
"LinearFunctionTestReplace requires a canonical induction variable");
|
2009-02-12 22:19:27 +00:00
|
|
|
// Can't rewrite non-branch yet.
|
2009-05-12 02:17:14 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
|
|
|
|
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.
|
2009-06-26 22:53:46 +00:00
|
|
|
RewriteIVExpressions(L, LargestType, 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.
|
|
|
|
DeleteDeadPHIs(L->getHeader());
|
|
|
|
// Check a post-condition.
|
|
|
|
assert(L->isLCSSAForm() && "Indvars did not leave the loop in lcssa form!");
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndVarSimplify::RewriteIVExpressions(Loop *L, const Type *LargestType,
|
2009-06-26 22:53:46 +00:00
|
|
|
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.
|
|
|
|
for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *Stride = IU->StrideOrder[i];
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2009-07-07 17:06:11 +00:00
|
|
|
std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI =
|
2009-05-12 02:17:14 +00:00
|
|
|
IU->IVUsesByStride.find(IU->StrideOrder[i]);
|
|
|
|
assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!");
|
|
|
|
ilist<IVStrideUse> &List = SI->second->Users;
|
|
|
|
for (ilist<IVStrideUse>::iterator UI = List.begin(),
|
|
|
|
E = List.end(); UI != E; ++UI) {
|
|
|
|
Value *Op = UI->getOperandValToReplace();
|
2009-06-13 16:25:49 +00:00
|
|
|
const Type *UseTy = Op->getType();
|
2009-05-12 02:17:14 +00:00
|
|
|
Instruction *User = UI->getUser();
|
|
|
|
|
|
|
|
// Compute the final addrec to expand into code.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *AR = IU->getReplacementExpr(*UI);
|
2009-05-12 02:17:14 +00:00
|
|
|
|
2009-06-24 01:18:18 +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.
|
|
|
|
if (!AR->isLoopInvariant(L) && !Stride->isLoopInvariant(L))
|
|
|
|
continue;
|
|
|
|
|
2009-06-27 05:16:57 +00:00
|
|
|
// 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.
|
2009-06-26 22:53:46 +00:00
|
|
|
Instruction *InsertPt = User;
|
|
|
|
if (PHINode *PHI = dyn_cast<PHINode>(InsertPt))
|
2009-06-27 05:16:57 +00:00
|
|
|
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
|
2009-06-26 22:53:46 +00:00
|
|
|
if (PHI->getIncomingValue(i) == Op) {
|
2009-06-27 05:16:57 +00:00
|
|
|
if (InsertPt == User)
|
|
|
|
InsertPt = PHI->getIncomingBlock(i)->getTerminator();
|
|
|
|
else
|
|
|
|
InsertPt =
|
|
|
|
DT->findNearestCommonDominator(InsertPt->getParent(),
|
|
|
|
PHI->getIncomingBlock(i))
|
|
|
|
->getTerminator();
|
2009-06-26 22:53:46 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 01:18:18 +00:00
|
|
|
// Now expand it into actual Instructions and patch it into place.
|
|
|
|
Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt);
|
2009-02-12 22:19:27 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// Patch the new value into place.
|
|
|
|
if (Op->hasName())
|
|
|
|
NewVal->takeName(Op);
|
|
|
|
User->replaceUsesOfWith(Op, NewVal);
|
|
|
|
UI->setOperandValToReplace(NewVal);
|
2009-08-23 04:37:46 +00:00
|
|
|
DEBUG(errs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n'
|
|
|
|
<< " into = " << *NewVal << "\n");
|
2009-05-12 02:17:14 +00:00
|
|
|
++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.
|
|
|
|
while (!DeadInsts.empty()) {
|
|
|
|
Instruction *Inst = dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val());
|
|
|
|
if (Inst)
|
|
|
|
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;
|
|
|
|
|
2009-06-26 22:53:46 +00:00
|
|
|
Instruction *InsertPt = ExitBlock->getFirstNonPHI();
|
2009-05-12 02:17:14 +00:00
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
|
|
|
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;
|
2009-07-15 22:48:29 +00:00
|
|
|
// Don't move instructions which might have side effects, since the side
|
|
|
|
// 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.
|
|
|
|
if (I->mayHaveSideEffects() || I->mayReadFromMemory())
|
2009-06-26 22:53:46 +00:00
|
|
|
continue;
|
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;
|
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) {
|
|
|
|
BasicBlock *UseBB = cast<Instruction>(UI)->getParent();
|
|
|
|
if (PHINode *P = dyn_cast<PHINode>(UI)) {
|
|
|
|
unsigned i =
|
|
|
|
PHINode::getIncomingValueNumForOperand(UI.getOperandNo());
|
|
|
|
UseBB = P->getIncomingBlock(i);
|
|
|
|
}
|
|
|
|
if (UseBB == Preheader || L->contains(UseBB)) {
|
|
|
|
UsedInLoop = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If there is, the def must remain in the preheader.
|
|
|
|
if (UsedInLoop)
|
|
|
|
continue;
|
|
|
|
// Otherwise, sink it to the exit block.
|
|
|
|
Instruction *ToMove = I;
|
|
|
|
bool Done = false;
|
|
|
|
if (I != Preheader->begin())
|
|
|
|
--I;
|
|
|
|
else
|
|
|
|
Done = true;
|
2009-06-26 22:53:46 +00:00
|
|
|
ToMove->moveBefore(InsertPt);
|
2009-05-12 02:17:14 +00:00
|
|
|
if (Done)
|
|
|
|
break;
|
2009-06-26 22:53:46 +00:00
|
|
|
InsertPt = ToMove;
|
2009-05-12 02:17:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-18 00:40:02 +00:00
|
|
|
/// Return true if it is OK to use SIToFPInst for an inducation variable
|
|
|
|
/// with given inital and exit values.
|
|
|
|
static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV,
|
|
|
|
uint64_t intIV, uint64_t intEV) {
|
|
|
|
|
2009-02-17 19:13:57 +00:00
|
|
|
if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative())
|
2008-11-18 00:40:02 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the iteration range can be handled by SIToFPInst then use it.
|
|
|
|
APInt Max = APInt::getSignedMaxValue(32);
|
2009-05-14 16:47:34 +00:00
|
|
|
if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV)))
|
2008-11-18 00:40:02 +00:00
|
|
|
return true;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-18 00:40:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// convertToInt - Convert APF to an integer, if possible.
|
2008-11-17 23:27:13 +00:00
|
|
|
static bool convertToInt(const APFloat &APF, uint64_t *intVal) {
|
|
|
|
|
|
|
|
bool isExact = false;
|
2008-11-26 01:11:57 +00:00
|
|
|
if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
|
|
|
|
return false;
|
2009-02-17 19:13:57 +00:00
|
|
|
if (APF.convertToInteger(intVal, 32, APF.isNegative(),
|
2008-11-17 23:27:13 +00:00
|
|
|
APFloat::rmTowardZero, &isExact)
|
|
|
|
!= APFloat::opOK)
|
|
|
|
return false;
|
2009-02-17 19:13:57 +00:00
|
|
|
if (!isExact)
|
2008-11-17 23:27:13 +00:00
|
|
|
return false;
|
|
|
|
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);
|
|
|
|
///
|
2009-05-12 02:17:14 +00:00
|
|
|
void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) {
|
2008-11-17 21:32:02 +00:00
|
|
|
|
|
|
|
unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
|
|
|
|
unsigned BackEdge = IncomingEdge^1;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Check incoming value.
|
2008-11-17 23:27:13 +00:00
|
|
|
ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge));
|
|
|
|
if (!InitValue) return;
|
2009-08-13 21:58:54 +00:00
|
|
|
uint64_t newInitValue =
|
|
|
|
Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits();
|
2008-11-17 23:27:13 +00:00
|
|
|
if (!convertToInt(InitValue->getValueAPF(), &newInitValue))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check IV increment. Reject this PH if increement operation is not
|
|
|
|
// an add or increment value can not be represented by an integer.
|
2009-02-17 19:13:57 +00:00
|
|
|
BinaryOperator *Incr =
|
2008-11-17 21:32:02 +00:00
|
|
|
dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge));
|
|
|
|
if (!Incr) return;
|
2009-06-04 22:49:04 +00:00
|
|
|
if (Incr->getOpcode() != Instruction::FAdd) return;
|
2008-11-17 21:32:02 +00:00
|
|
|
ConstantFP *IncrValue = NULL;
|
|
|
|
unsigned IncrVIndex = 1;
|
|
|
|
if (Incr->getOperand(1) == PH)
|
|
|
|
IncrVIndex = 0;
|
|
|
|
IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex));
|
|
|
|
if (!IncrValue) return;
|
2009-08-13 21:58:54 +00:00
|
|
|
uint64_t newIncrValue =
|
|
|
|
Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits();
|
2008-11-17 23:27:13 +00:00
|
|
|
if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue))
|
|
|
|
return;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-17 23:27:13 +00:00
|
|
|
// Check Incr uses. One user is PH and the other users is exit condition 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
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Find exit condition.
|
|
|
|
FCmpInst *EC = dyn_cast<FCmpInst>(U1);
|
|
|
|
if (!EC)
|
|
|
|
EC = dyn_cast<FCmpInst>(U2);
|
|
|
|
if (!EC) return;
|
|
|
|
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) {
|
|
|
|
if (!BI->isConditional()) return;
|
|
|
|
if (BI->getCondition() != EC) return;
|
2008-11-03 18:32:19 +00:00
|
|
|
}
|
2008-11-17 21:32:02 +00:00
|
|
|
|
2008-11-17 23:27:13 +00:00
|
|
|
// Find exit value. If exit value can not be represented as an interger then
|
|
|
|
// do not handle this floating point PH.
|
2008-11-17 21:32:02 +00:00
|
|
|
ConstantFP *EV = NULL;
|
|
|
|
unsigned EVIndex = 1;
|
|
|
|
if (EC->getOperand(1) == Incr)
|
|
|
|
EVIndex = 0;
|
|
|
|
EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex));
|
|
|
|
if (!EV) return;
|
2009-08-13 21:58:54 +00:00
|
|
|
uint64_t intEV = Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits();
|
2008-11-17 23:27:13 +00:00
|
|
|
if (!convertToInt(EV->getValueAPF(), &intEV))
|
2008-11-17 21:32:02 +00:00
|
|
|
return;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Find new predicate for integer comparison.
|
|
|
|
CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
|
|
|
|
switch (EC->getPredicate()) {
|
|
|
|
case CmpInst::FCMP_OEQ:
|
|
|
|
case CmpInst::FCMP_UEQ:
|
|
|
|
NewPred = CmpInst::ICMP_EQ;
|
|
|
|
break;
|
|
|
|
case CmpInst::FCMP_OGT:
|
|
|
|
case CmpInst::FCMP_UGT:
|
|
|
|
NewPred = CmpInst::ICMP_UGT;
|
|
|
|
break;
|
|
|
|
case CmpInst::FCMP_OGE:
|
|
|
|
case CmpInst::FCMP_UGE:
|
|
|
|
NewPred = CmpInst::ICMP_UGE;
|
|
|
|
break;
|
|
|
|
case CmpInst::FCMP_OLT:
|
|
|
|
case CmpInst::FCMP_ULT:
|
|
|
|
NewPred = CmpInst::ICMP_ULT;
|
|
|
|
break;
|
|
|
|
case CmpInst::FCMP_OLE:
|
|
|
|
case CmpInst::FCMP_ULE:
|
|
|
|
NewPred = CmpInst::ICMP_ULE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2008-11-03 18:32:19 +00:00
|
|
|
}
|
2008-11-17 21:32:02 +00:00
|
|
|
if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return;
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Insert new integer induction variable.
|
2009-08-13 21:58:54 +00:00
|
|
|
PHINode *NewPHI = PHINode::Create(Type::getInt32Ty(PH->getContext()),
|
2008-11-17 21:32:02 +00:00
|
|
|
PH->getName()+".int", PH);
|
2009-08-13 21:58:54 +00:00
|
|
|
NewPHI->addIncoming(ConstantInt::get(Type::getInt32Ty(PH->getContext()),
|
|
|
|
newInitValue),
|
2008-11-17 21:32:02 +00:00
|
|
|
PH->getIncomingBlock(IncomingEdge));
|
|
|
|
|
2009-02-17 19:13:57 +00:00
|
|
|
Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
|
2009-08-13 21:58:54 +00:00
|
|
|
ConstantInt::get(Type::getInt32Ty(PH->getContext()),
|
2008-11-17 23:27:13 +00:00
|
|
|
newIncrValue),
|
2008-11-17 21:32:02 +00:00
|
|
|
Incr->getName()+".int", Incr);
|
|
|
|
NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));
|
|
|
|
|
2009-04-27 21:03:15 +00:00
|
|
|
// The back edge is edge 1 of newPHI, whatever it may have been in the
|
|
|
|
// original PHI.
|
2009-08-13 21:58:54 +00:00
|
|
|
ConstantInt *NewEV = ConstantInt::get(Type::getInt32Ty(PH->getContext()),
|
|
|
|
intEV);
|
2009-04-27 21:03:15 +00:00
|
|
|
Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV);
|
|
|
|
Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1));
|
2009-07-09 23:48:35 +00:00
|
|
|
ICmpInst *NewEC = new ICmpInst(EC->getParent()->getTerminator(),
|
2009-07-26 09:48:23 +00:00
|
|
|
NewPred, LHS, RHS, EC->getName());
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2009-05-12 02:17:14 +00:00
|
|
|
// In the following deltions, PH may become dead and may be deleted.
|
|
|
|
// Use a WeakVH to observe whether this happens.
|
|
|
|
WeakVH WeakPH = PH;
|
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Delete old, floating point, exit comparision instruction.
|
2009-05-24 18:09:01 +00:00
|
|
|
NewEC->takeName(EC);
|
2008-11-17 21:32:02 +00:00
|
|
|
EC->replaceAllUsesWith(NewEC);
|
2009-05-12 02:17:14 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(EC);
|
2009-02-17 19:13:57 +00:00
|
|
|
|
2008-11-17 21:32:02 +00:00
|
|
|
// Delete old, floating point, increment instruction.
|
2009-07-30 23:03:37 +00:00
|
|
|
Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
|
2009-05-12 02:17:14 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Incr);
|
|
|
|
|
|
|
|
// Replace floating induction variable, if it isn't already deleted.
|
|
|
|
// Give SIToFPInst preference over UIToFPInst because it is faster on
|
|
|
|
// platforms that are widely used.
|
|
|
|
if (WeakPH && !PH->use_empty()) {
|
|
|
|
if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) {
|
|
|
|
SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv",
|
|
|
|
PH->getParent()->getFirstNonPHI());
|
|
|
|
PH->replaceAllUsesWith(Conv);
|
|
|
|
} else {
|
|
|
|
UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv",
|
|
|
|
PH->getParent()->getFirstNonPHI());
|
|
|
|
PH->replaceAllUsesWith(Conv);
|
|
|
|
}
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(PH);
|
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);
|
|
|
|
}
|