2001-12-04 01:28:42 +08:00
|
|
|
//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-04 01:28:42 +08:00
|
|
|
//
|
2004-04-03 04:24:31 +08: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 17:01:07 +08:00
|
|
|
// This transformation makes the following changes to each loop with an
|
2004-04-03 04:24:31 +08: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-15 06:38:41 +08: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-03 04:24:31 +08: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-20 04:38:47 +08:00
|
|
|
// desired loop transformations have been performed.
|
2001-12-04 01:28:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 05:40:18 +08:00
|
|
|
#define DEBUG_TYPE "indvars"
|
2002-05-08 04:03:00 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-04-03 04:24:31 +08: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.
llvm-svn: 12961
2004-04-15 23:21:43 +08:00
|
|
|
#include "llvm/Constants.h"
|
2003-12-22 13:02:01 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2004-04-03 04:24:31 +08:00
|
|
|
#include "llvm/Type.h"
|
2009-05-12 10:17:14 +08:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
#include "llvm/Analysis/IVUsers.h"
|
2005-07-30 08:12:19 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2003-12-19 01:19:19 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2007-03-07 14:39:01 +08:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2002-02-13 06:39:50 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2007-02-06 07:32:05 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-01-07 09:14:12 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2003-12-19 01:19:19 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2009-05-12 10:17:14 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.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.
llvm-svn: 33113
2007-01-12 15:05:14 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-05-12 10:17:14 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2003-12-19 01:19:19 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2006-12-20 05:40:18 +08: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 11:58:44 +08:00
|
|
|
|
2006-12-20 05:40:18 +08:00
|
|
|
namespace {
|
2007-03-07 14:39:01 +08:00
|
|
|
class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
|
2009-05-12 10:17:14 +08:00
|
|
|
IVUsers *IU;
|
2004-04-03 04:24:31 +08:00
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
2003-12-23 15:47:09 +08:00
|
|
|
bool Changed;
|
2003-12-22 11:58:44 +08:00
|
|
|
public:
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-05 01:05:41 +08:00
|
|
|
IndVarSimplify() : LoopPass(&ID) {}
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2009-02-18 04:49:49 +08:00
|
|
|
virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
|
|
|
|
|
2007-03-07 14:39:01 +08:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-05-12 10:17:14 +08:00
|
|
|
AU.addRequired<DominatorTree>();
|
2007-09-11 02:08:23 +08:00
|
|
|
AU.addRequired<ScalarEvolution>();
|
2007-03-07 14:39:01 +08:00
|
|
|
AU.addRequiredID(LCSSAID);
|
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
|
|
|
AU.addRequired<LoopInfo>();
|
2009-05-12 10:17:14 +08:00
|
|
|
AU.addRequired<IVUsers>();
|
2009-02-24 00:29:41 +08:00
|
|
|
AU.addPreserved<ScalarEvolution>();
|
2007-03-07 14:39:01 +08:00
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2009-05-12 10:17:14 +08:00
|
|
|
AU.addPreserved<IVUsers>();
|
2007-03-07 14:39:01 +08:00
|
|
|
AU.addPreservedID(LCSSAID);
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
2003-12-22 11:58:44 +08:00
|
|
|
|
2004-04-03 04:24:31 +08:00
|
|
|
private:
|
2007-03-07 14:39:01 +08:00
|
|
|
|
2009-02-18 04:49:49 +08:00
|
|
|
void RewriteNonIntegerIVs(Loop *L);
|
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
ICmpInst *LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount,
|
2009-02-17 23:57:39 +08:00
|
|
|
Value *IndVar,
|
2009-02-13 06:19:27 +08:00
|
|
|
BasicBlock *ExitingBlock,
|
|
|
|
BranchInst *BI,
|
2009-02-24 07:20:35 +08:00
|
|
|
SCEVExpander &Rewriter);
|
2009-04-19 01:56:28 +08:00
|
|
|
void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount);
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
void RewriteIVExpressions(Loop *L, const Type *LargestType,
|
|
|
|
SCEVExpander &Rewriter);
|
2008-09-10 05:41:07 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
void SinkUnusedInvariants(Loop *L, SCEVExpander &Rewriter);
|
|
|
|
|
|
|
|
void FixUsesBeforeDefs(Loop *L, SCEVExpander &Rewriter);
|
|
|
|
|
|
|
|
void HandleFloatingPointIV(Loop *L, PHINode *PH);
|
2003-12-22 11:58:44 +08:00
|
|
|
};
|
2002-09-10 13:24:05 +08:00
|
|
|
}
|
2001-12-04 12:32:29 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char IndVarSimplify::ID = 0;
|
|
|
|
static RegisterPass<IndVarSimplify>
|
|
|
|
X("indvars", "Canonicalize Induction Variables");
|
|
|
|
|
2008-10-23 07:32:42 +08:00
|
|
|
Pass *llvm::createIndVarSimplifyPass() {
|
2003-12-22 11:58:44 +08:00
|
|
|
return new IndVarSimplify();
|
2001-12-04 12:32:29 +08:00
|
|
|
}
|
|
|
|
|
2004-04-03 04:24:31 +08: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.
llvm-svn: 12961
2004-04-15 23:21:43 +08: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 10:17:14 +08:00
|
|
|
ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
|
2009-02-25 02:55:53 +08:00
|
|
|
SCEVHandle BackedgeTakenCount,
|
2009-02-13 06:19:27 +08:00
|
|
|
Value *IndVar,
|
|
|
|
BasicBlock *ExitingBlock,
|
|
|
|
BranchInst *BI,
|
2009-02-24 07:20:35 +08:00
|
|
|
SCEVExpander &Rewriter) {
|
2004-04-16 04:26:22 +08: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-13 06:19:27 +08:00
|
|
|
Value *CmpIndVar;
|
2009-02-25 02:55:53 +08:00
|
|
|
SCEVHandle RHS = BackedgeTakenCount;
|
2009-02-13 06:19:27 +08:00
|
|
|
if (ExitingBlock == L->getLoopLatch()) {
|
2009-02-25 02:55:53 +08: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.
|
|
|
|
SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
|
2009-02-13 06:19:27 +08:00
|
|
|
SCEVHandle N =
|
2009-02-25 02:55:53 +08:00
|
|
|
SE->getAddExpr(BackedgeTakenCount,
|
|
|
|
SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
|
2009-02-13 06:19:27 +08:00
|
|
|
if ((isa<SCEVConstant>(N) && !N->isZero()) ||
|
|
|
|
SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
|
|
|
|
// No overflow. Cast the sum.
|
2009-02-25 02:55:53 +08:00
|
|
|
RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
|
2009-02-13 06:19:27 +08:00
|
|
|
} else {
|
|
|
|
// Potential overflow. Cast before doing the add.
|
2009-02-25 02:55:53 +08:00
|
|
|
RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
|
|
|
|
IndVar->getType());
|
|
|
|
RHS = SE->getAddExpr(RHS,
|
|
|
|
SE->getIntegerSCEV(1, IndVar->getType()));
|
2009-02-13 06:19:27 +08:00
|
|
|
}
|
|
|
|
|
2009-02-25 02:55:53 +08: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-13 06:19:27 +08:00
|
|
|
CmpIndVar = L->getCanonicalInductionVariableIncrement();
|
2004-04-16 04:26:22 +08:00
|
|
|
} else {
|
|
|
|
// We have to use the preincremented value...
|
2009-02-25 02:55:53 +08:00
|
|
|
RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
|
|
|
|
IndVar->getType());
|
2009-02-13 06:19:27 +08:00
|
|
|
CmpIndVar = IndVar;
|
2004-04-16 04:26:22 +08: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.
llvm-svn: 12961
2004-04-15 23:21:43 +08:00
|
|
|
|
2004-04-03 04:24:31 +08:00
|
|
|
// Expand the code for the iteration count into the preheader of the loop.
|
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
2009-06-14 00:25:49 +08:00
|
|
|
Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(),
|
2009-02-13 06:19:27 +08:00
|
|
|
Preheader->getTerminator());
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2006-12-23 14:05:41 +08:00
|
|
|
// Insert a new icmp_ne or icmp_eq instruction before the branch.
|
|
|
|
ICmpInst::Predicate Opcode;
|
2004-04-03 04:24:31 +08:00
|
|
|
if (L->contains(BI->getSuccessor(0)))
|
2006-12-23 14:05:41 +08:00
|
|
|
Opcode = ICmpInst::ICMP_NE;
|
2004-04-03 04:24:31 +08:00
|
|
|
else
|
2006-12-23 14:05:41 +08:00
|
|
|
Opcode = ICmpInst::ICMP_EQ;
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2009-02-13 06:19:27 +08:00
|
|
|
DOUT << "INDVARS: Rewriting loop exit condition to:\n"
|
|
|
|
<< " LHS:" << *CmpIndVar // includes a newline
|
|
|
|
<< " op:\t"
|
2009-02-14 10:26:50 +08:00
|
|
|
<< (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
|
2009-02-25 02:55:53 +08:00
|
|
|
<< " RHS:\t" << *RHS << "\n";
|
2009-02-13 06:19:27 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
ICmpInst *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI);
|
|
|
|
|
|
|
|
Instruction *OrigCond = cast<Instruction>(BI->getCondition());
|
2009-05-25 03:11:38 +08: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 10:17:14 +08:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(OrigCond);
|
|
|
|
|
2004-04-03 04:24:31 +08:00
|
|
|
++NumLFTR;
|
|
|
|
Changed = true;
|
2009-05-12 10:17:14 +08:00
|
|
|
return Cond;
|
2004-04-03 04:24:31 +08:00
|
|
|
}
|
2003-12-22 11:58:44 +08:00
|
|
|
|
2004-04-03 04:24:31 +08: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 10:17:14 +08: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-19 01:56:28 +08:00
|
|
|
void IndVarSimplify::RewriteLoopExitValues(Loop *L,
|
|
|
|
const SCEV *BackedgeTakenCount) {
|
2009-05-12 10:17:14 +08:00
|
|
|
// Verify the input to the pass in already in LCSSA form.
|
|
|
|
assert(L->isLCSSAForm());
|
|
|
|
|
2004-04-03 04:24:31 +08:00
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
|
|
|
|
|
|
|
// Scan all of the instructions in the loop, looking at those that have
|
|
|
|
// extra-loop users and which are recurrences.
|
2009-05-19 10:15:55 +08:00
|
|
|
SCEVExpander Rewriter(*SE);
|
2004-04-03 04:24:31 +08:00
|
|
|
|
|
|
|
// We insert the code into the preheader of the loop if the loop contains
|
|
|
|
// multiple exit blocks, or in the exit block if there is exactly one.
|
|
|
|
BasicBlock *BlockToInsertInto;
|
2007-08-21 08:31:24 +08:00
|
|
|
SmallVector<BasicBlock*, 8> ExitBlocks;
|
2007-03-04 11:43:23 +08:00
|
|
|
L->getUniqueExitBlocks(ExitBlocks);
|
2004-04-19 06:14:10 +08:00
|
|
|
if (ExitBlocks.size() == 1)
|
|
|
|
BlockToInsertInto = ExitBlocks[0];
|
2004-04-03 04:24:31 +08:00
|
|
|
else
|
|
|
|
BlockToInsertInto = Preheader;
|
2008-05-24 05:05:58 +08:00
|
|
|
BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
std::map<Instruction*, Value*> ExitValues;
|
|
|
|
|
|
|
|
// 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-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08: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-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
unsigned NumPreds = PN->getNumIncomingValues();
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
// Iterate over all of the PHI nodes.
|
|
|
|
BasicBlock::iterator BBI = ExitBB->begin();
|
|
|
|
while ((PN = dyn_cast<PHINode>(BBI++))) {
|
2009-05-25 03:36:09 +08:00
|
|
|
if (PN->use_empty())
|
|
|
|
continue; // dead use, don't replace it
|
2007-03-04 11:43:23 +08: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 11:18:22 +08:00
|
|
|
(!isa<IntegerType>(InVal->getType()) &&
|
|
|
|
!isa<PointerType>(InVal->getType())))
|
2007-03-04 11:43:23 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this pred is for a subloop, not L itself, skip it.
|
2009-02-18 03:13:57 +08:00
|
|
|
if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
|
2007-03-04 11:43:23 +08: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-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08: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-05-25 07:25:42 +08:00
|
|
|
SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
|
|
|
|
if (!ExitValue->isLoopInvariant(L))
|
2007-03-04 11:43:23 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
++NumReplaced;
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
// See if we already computed the exit value for the instruction, if so,
|
|
|
|
// just reuse it.
|
|
|
|
Value *&ExitVal = ExitValues[Inst];
|
|
|
|
if (!ExitVal)
|
2009-04-16 11:18:22 +08:00
|
|
|
ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), InsertPt);
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
|
|
|
|
<< " LoopVal = " << *Inst << "\n";
|
2007-03-04 09:00:28 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
PN->setIncomingValue(i, ExitVal);
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
// If this instruction is dead now, delete it.
|
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Inst);
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2007-03-04 11:43:23 +08:00
|
|
|
// See if this is a single-entry LCSSA PHI node. If so, we can (and
|
|
|
|
// have to) remove
|
2007-03-04 09:00:28 +08:00
|
|
|
// the PHI entirely. 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 11:43:23 +08:00
|
|
|
if (NumPreds == 1) {
|
|
|
|
PN->replaceAllUsesWith(ExitVal);
|
2009-05-12 10:17:14 +08:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(PN);
|
2007-03-04 11:43:23 +08:00
|
|
|
break;
|
2007-03-04 06:48:48 +08:00
|
|
|
}
|
2005-06-16 05:29:31 +08:00
|
|
|
}
|
2007-03-04 06:48:48 +08:00
|
|
|
}
|
|
|
|
}
|
2004-04-03 04:24:31 +08:00
|
|
|
}
|
2003-12-23 15:47:09 +08:00
|
|
|
|
2009-02-18 04:49:49 +08:00
|
|
|
void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
|
2009-04-16 11:18:22 +08:00
|
|
|
// First step. Check to see if there are any floating-point recurrences.
|
2004-04-03 04:24:31 +08:00
|
|
|
// If there are, change them into integer recurrences, permitting analysis by
|
|
|
|
// the SCEV routines.
|
2003-12-23 15:47:09 +08:00
|
|
|
//
|
2004-04-03 04:24:31 +08:00
|
|
|
BasicBlock *Header = L->getHeader();
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2009-05-12 10:17:14 +08: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 15:47:09 +08:00
|
|
|
|
2009-04-16 11:18:22 +08:00
|
|
|
// If the loop previously had floating-point IV, ScalarEvolution
|
2009-02-18 04:49:49 +08: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-25 02:55:53 +08:00
|
|
|
SE->forgetLoopBackedgeTakenCount(L);
|
2009-04-16 07:31:51 +08:00
|
|
|
}
|
|
|
|
|
2009-02-13 06:19:27 +08:00
|
|
|
bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
|
2009-05-12 10:17:14 +08:00
|
|
|
IU = &getAnalysis<IVUsers>();
|
2007-03-07 14:39:01 +08:00
|
|
|
LI = &getAnalysis<LoopInfo>();
|
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
|
|
|
Changed = false;
|
2009-02-18 04:49:49 +08:00
|
|
|
|
2009-04-16 11:18:22 +08:00
|
|
|
// If there are any floating-point recurrences, attempt to
|
2009-02-18 04:49:49 +08:00
|
|
|
// transform them to use integer recurrences.
|
|
|
|
RewriteNonIntegerIVs(L);
|
|
|
|
|
2009-02-13 06:19:27 +08:00
|
|
|
BasicBlock *Header = L->getHeader();
|
2009-05-12 10:17:14 +08:00
|
|
|
BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null
|
|
|
|
SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L);
|
2007-03-04 09:00:28 +08:00
|
|
|
|
2004-04-03 04:24:31 +08: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 12:32:29 +08:00
|
|
|
//
|
2009-02-25 02:55:53 +08:00
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
|
|
|
|
RewriteLoopExitValues(L, BackedgeTakenCount);
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
// Compute the type of the largest recurrence expression, and decide whether
|
|
|
|
// a canonical induction variable should be inserted.
|
2009-02-13 06:19:27 +08:00
|
|
|
const Type *LargestType = 0;
|
2009-05-12 10:17:14 +08:00
|
|
|
bool NeedCannIV = false;
|
2009-02-25 02:55:53 +08:00
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
|
|
|
|
LargestType = BackedgeTakenCount->getType();
|
2009-04-21 09:07:12 +08:00
|
|
|
LargestType = SE->getEffectiveSCEVType(LargestType);
|
2009-05-12 10:17:14 +08: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-18 02:08:33 +08:00
|
|
|
}
|
2009-05-12 10:17:14 +08:00
|
|
|
for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
|
|
|
|
SCEVHandle Stride = IU->StrideOrder[i];
|
|
|
|
const Type *Ty = SE->getEffectiveSCEVType(Stride->getType());
|
2009-02-13 06:19:27 +08:00
|
|
|
if (!LargestType ||
|
2009-05-12 10:17:14 +08:00
|
|
|
SE->getTypeSizeInBits(Ty) >
|
2009-04-21 09:07:12 +08:00
|
|
|
SE->getTypeSizeInBits(LargestType))
|
2009-05-12 10:17:14 +08:00
|
|
|
LargestType = Ty;
|
|
|
|
|
|
|
|
std::map<SCEVHandle, IVUsersOfOneStride *>::iterator SI =
|
|
|
|
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 17:53:29 +08:00
|
|
|
}
|
2001-12-04 12:32:29 +08:00
|
|
|
|
2004-04-03 04:24:31 +08:00
|
|
|
// Create a rewriter object which we'll use to transform the code with.
|
2009-05-19 10:15:55 +08:00
|
|
|
SCEVExpander Rewriter(*SE);
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2009-05-12 10:17:14 +08: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-13 06:19:27 +08:00
|
|
|
Value *IndVar = 0;
|
2009-05-12 10:17:14 +08:00
|
|
|
if (NeedCannIV) {
|
2009-06-14 00:25:49 +08: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-02-13 06:19:27 +08:00
|
|
|
IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
|
2009-06-14 00:25:49 +08:00
|
|
|
|
2009-02-13 06:19:27 +08:00
|
|
|
++NumInserted;
|
|
|
|
Changed = true;
|
|
|
|
DOUT << "INDVARS: New CanIV: " << *IndVar;
|
2009-06-14 00:25:49 +08: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 22:38:12 +08:00
|
|
|
}
|
2004-04-03 04:24:31 +08:00
|
|
|
|
2009-02-13 06:19:27 +08: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 10:17:14 +08:00
|
|
|
ICmpInst *NewICmp = 0;
|
|
|
|
if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) {
|
|
|
|
assert(NeedCannIV &&
|
|
|
|
"LinearFunctionTestReplace requires a canonical induction variable");
|
2009-02-13 06:19:27 +08:00
|
|
|
// Can't rewrite non-branch yet.
|
2009-05-12 10:17:14 +08:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
|
|
|
|
NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
|
|
|
|
ExitingBlock, BI, Rewriter);
|
|
|
|
}
|
2009-02-13 06:19:27 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
Rewriter.setInsertionPoint(Header->getFirstNonPHI());
|
2009-02-13 06:19:27 +08:00
|
|
|
|
2009-05-25 04:08:21 +08:00
|
|
|
// Rewrite IV-derived expressions. Clears the rewriter cache.
|
2009-05-12 10:17:14 +08:00
|
|
|
RewriteIVExpressions(L, LargestType, Rewriter);
|
2004-04-22 22:59:40 +08:00
|
|
|
|
2009-05-25 04:08:21 +08:00
|
|
|
// The Rewriter may only be used for isInsertedInstruction queries from this
|
|
|
|
// point on.
|
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
// Loop-invariant instructions in the preheader that aren't used in the
|
|
|
|
// loop may be sunk below the loop to reduce register pressure.
|
|
|
|
SinkUnusedInvariants(L, Rewriter);
|
|
|
|
|
|
|
|
// Reorder instructions to avoid use-before-def conditions.
|
|
|
|
FixUsesBeforeDefs(L, Rewriter);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
SCEVExpander &Rewriter) {
|
|
|
|
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) {
|
|
|
|
SCEVHandle Stride = IU->StrideOrder[i];
|
|
|
|
|
|
|
|
std::map<SCEVHandle, IVUsersOfOneStride *>::iterator SI =
|
|
|
|
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) {
|
|
|
|
SCEVHandle Offset = UI->getOffset();
|
|
|
|
Value *Op = UI->getOperandValToReplace();
|
2009-06-14 00:25:49 +08:00
|
|
|
const Type *UseTy = Op->getType();
|
2009-05-12 10:17:14 +08:00
|
|
|
Instruction *User = UI->getUser();
|
|
|
|
|
|
|
|
// Compute the final addrec to expand into code.
|
|
|
|
SCEVHandle AR = IU->getReplacementExpr(*UI);
|
|
|
|
|
|
|
|
Value *NewVal = 0;
|
|
|
|
if (AR->isLoopInvariant(L)) {
|
|
|
|
BasicBlock::iterator I = Rewriter.getInsertionPoint();
|
|
|
|
// Expand loop-invariant values in the loop preheader. They will
|
|
|
|
// be sunk to the exit block later, if possible.
|
2009-05-19 10:15:55 +08:00
|
|
|
NewVal =
|
2009-06-14 00:25:49 +08:00
|
|
|
Rewriter.expandCodeFor(AR, UseTy,
|
2009-05-12 10:17:14 +08:00
|
|
|
L->getLoopPreheader()->getTerminator());
|
|
|
|
Rewriter.setInsertionPoint(I);
|
|
|
|
++NumReplaced;
|
|
|
|
} else {
|
2009-06-04 03:11:31 +08: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 (!Stride->isLoopInvariant(L))
|
|
|
|
continue;
|
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
// Now expand it into actual Instructions and patch it into place.
|
|
|
|
NewVal = Rewriter.expandCodeFor(AR, UseTy);
|
2009-02-14 10:31:09 +08:00
|
|
|
}
|
2009-02-13 06:19:27 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
// Patch the new value into place.
|
|
|
|
if (Op->hasName())
|
|
|
|
NewVal->takeName(Op);
|
|
|
|
User->replaceUsesOfWith(Op, NewVal);
|
|
|
|
UI->setOperandValToReplace(NewVal);
|
|
|
|
DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *Op
|
|
|
|
<< " into = " << *NewVal << "\n";
|
|
|
|
++NumRemoved;
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
// The old value may be dead now.
|
|
|
|
DeadInsts.push_back(Op);
|
|
|
|
}
|
2003-12-22 17:53:29 +08:00
|
|
|
}
|
2003-12-11 02:06:47 +08:00
|
|
|
|
2009-05-25 04:08:21 +08: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 10:17:14 +08: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.
|
|
|
|
void IndVarSimplify::SinkUnusedInvariants(Loop *L, SCEVExpander &Rewriter) {
|
|
|
|
BasicBlock *ExitBlock = L->getExitBlock();
|
|
|
|
if (!ExitBlock) return;
|
|
|
|
|
|
|
|
Instruction *NonPHI = ExitBlock->getFirstNonPHI();
|
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
|
|
|
BasicBlock::iterator I = Preheader->getTerminator();
|
|
|
|
while (I != Preheader->begin()) {
|
|
|
|
--I;
|
|
|
|
// New instructions were inserted at the end of the preheader. Only
|
|
|
|
// consider those new instructions.
|
|
|
|
if (!Rewriter.isInsertedInstruction(I))
|
|
|
|
break;
|
|
|
|
// 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;
|
|
|
|
ToMove->moveBefore(NonPHI);
|
|
|
|
if (Done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Re-schedule the inserted instructions to put defs before uses. This
|
|
|
|
/// fixes problems that arrise when SCEV expressions contain loop-variant
|
|
|
|
/// values unrelated to the induction variable which are defined inside the
|
|
|
|
/// loop. FIXME: It would be better to insert instructions in the right
|
|
|
|
/// place so that this step isn't needed.
|
|
|
|
void IndVarSimplify::FixUsesBeforeDefs(Loop *L, SCEVExpander &Rewriter) {
|
|
|
|
// Visit all the blocks in the loop in pre-order dom-tree dfs order.
|
|
|
|
DominatorTree *DT = &getAnalysis<DominatorTree>();
|
|
|
|
std::map<Instruction *, unsigned> NumPredsLeft;
|
|
|
|
SmallVector<DomTreeNode *, 16> Worklist;
|
|
|
|
Worklist.push_back(DT->getNode(L->getHeader()));
|
|
|
|
do {
|
|
|
|
DomTreeNode *Node = Worklist.pop_back_val();
|
|
|
|
for (DomTreeNode::iterator I = Node->begin(), E = Node->end(); I != E; ++I)
|
|
|
|
if (L->contains((*I)->getBlock()))
|
|
|
|
Worklist.push_back(*I);
|
|
|
|
BasicBlock *BB = Node->getBlock();
|
|
|
|
// Visit all the instructions in the block top down.
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
|
|
|
|
// Count the number of operands that aren't properly dominating.
|
|
|
|
unsigned NumPreds = 0;
|
|
|
|
if (Rewriter.isInsertedInstruction(I) && !isa<PHINode>(I))
|
|
|
|
for (User::op_iterator OI = I->op_begin(), OE = I->op_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(OI))
|
|
|
|
if (L->contains(Inst->getParent()) && !NumPredsLeft.count(Inst))
|
|
|
|
++NumPreds;
|
|
|
|
NumPredsLeft[I] = NumPreds;
|
|
|
|
// Notify uses of the position of this instruction, and move the
|
|
|
|
// users (and their dependents, recursively) into place after this
|
|
|
|
// instruction if it is their last outstanding operand.
|
|
|
|
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
|
|
|
|
UI != UE; ++UI) {
|
|
|
|
Instruction *Inst = cast<Instruction>(UI);
|
|
|
|
std::map<Instruction *, unsigned>::iterator Z = NumPredsLeft.find(Inst);
|
|
|
|
if (Z != NumPredsLeft.end() && Z->second != 0 && --Z->second == 0) {
|
|
|
|
SmallVector<Instruction *, 4> UseWorkList;
|
|
|
|
UseWorkList.push_back(Inst);
|
2009-05-23 00:47:11 +08:00
|
|
|
BasicBlock::iterator InsertPt = I;
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(InsertPt))
|
|
|
|
InsertPt = II->getNormalDest()->begin();
|
|
|
|
else
|
|
|
|
++InsertPt;
|
2009-05-12 10:17:14 +08:00
|
|
|
while (isa<PHINode>(InsertPt)) ++InsertPt;
|
|
|
|
do {
|
|
|
|
Instruction *Use = UseWorkList.pop_back_val();
|
|
|
|
Use->moveBefore(InsertPt);
|
|
|
|
NumPredsLeft.erase(Use);
|
|
|
|
for (Value::use_iterator IUI = Use->use_begin(),
|
|
|
|
IUE = Use->use_end(); IUI != IUE; ++IUI) {
|
|
|
|
Instruction *IUIInst = cast<Instruction>(IUI);
|
|
|
|
if (L->contains(IUIInst->getParent()) &&
|
|
|
|
Rewriter.isInsertedInstruction(IUIInst) &&
|
|
|
|
!isa<PHINode>(IUIInst))
|
|
|
|
UseWorkList.push_back(IUIInst);
|
|
|
|
}
|
|
|
|
} while (!UseWorkList.empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (!Worklist.empty());
|
2001-12-04 01:28:42 +08:00
|
|
|
}
|
2008-09-10 05:41:07 +08:00
|
|
|
|
2008-11-18 08:40:02 +08: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-18 03:13:57 +08:00
|
|
|
if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative())
|
2008-11-18 08:40:02 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the iteration range can be handled by SIToFPInst then use it.
|
|
|
|
APInt Max = APInt::getSignedMaxValue(32);
|
2009-05-15 00:47:34 +08:00
|
|
|
if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV)))
|
2008-11-18 08:40:02 +08:00
|
|
|
return true;
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2008-11-18 08:40:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// convertToInt - Convert APF to an integer, if possible.
|
2008-11-18 07:27:13 +08:00
|
|
|
static bool convertToInt(const APFloat &APF, uint64_t *intVal) {
|
|
|
|
|
|
|
|
bool isExact = false;
|
2008-11-26 09:11:57 +08:00
|
|
|
if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
|
|
|
|
return false;
|
2009-02-18 03:13:57 +08:00
|
|
|
if (APF.convertToInteger(intVal, 32, APF.isNegative(),
|
2008-11-18 07:27:13 +08:00
|
|
|
APFloat::rmTowardZero, &isExact)
|
|
|
|
!= APFloat::opOK)
|
|
|
|
return false;
|
2009-02-18 03:13:57 +08:00
|
|
|
if (!isExact)
|
2008-11-18 07:27:13 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-11-04 02:32:19 +08:00
|
|
|
/// HandleFloatingPointIV - If the loop has floating induction variable
|
|
|
|
/// then insert corresponding integer induction variable if possible.
|
2008-11-18 05:32:02 +08: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 10:17:14 +08:00
|
|
|
void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) {
|
2008-11-18 05:32:02 +08:00
|
|
|
|
|
|
|
unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
|
|
|
|
unsigned BackEdge = IncomingEdge^1;
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2008-11-18 05:32:02 +08:00
|
|
|
// Check incoming value.
|
2008-11-18 07:27:13 +08:00
|
|
|
ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge));
|
|
|
|
if (!InitValue) return;
|
|
|
|
uint64_t newInitValue = Type::Int32Ty->getPrimitiveSizeInBits();
|
|
|
|
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-18 03:13:57 +08:00
|
|
|
BinaryOperator *Incr =
|
2008-11-18 05:32:02 +08:00
|
|
|
dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge));
|
|
|
|
if (!Incr) return;
|
2009-06-05 06:49:04 +08:00
|
|
|
if (Incr->getOpcode() != Instruction::FAdd) return;
|
2008-11-18 05:32:02 +08:00
|
|
|
ConstantFP *IncrValue = NULL;
|
|
|
|
unsigned IncrVIndex = 1;
|
|
|
|
if (Incr->getOperand(1) == PH)
|
|
|
|
IncrVIndex = 0;
|
|
|
|
IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex));
|
|
|
|
if (!IncrValue) return;
|
2008-11-18 07:27:13 +08:00
|
|
|
uint64_t newIncrValue = Type::Int32Ty->getPrimitiveSizeInBits();
|
|
|
|
if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue))
|
|
|
|
return;
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2008-11-18 07:27:13 +08:00
|
|
|
// Check Incr uses. One user is PH and the other users is exit condition used
|
|
|
|
// by the conditional terminator.
|
2008-11-18 05:32:02 +08: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-18 03:13:57 +08:00
|
|
|
|
2008-11-18 05:32:02 +08: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-04 02:32:19 +08:00
|
|
|
}
|
2008-11-18 05:32:02 +08:00
|
|
|
|
2008-11-18 07:27:13 +08:00
|
|
|
// Find exit value. If exit value can not be represented as an interger then
|
|
|
|
// do not handle this floating point PH.
|
2008-11-18 05:32:02 +08:00
|
|
|
ConstantFP *EV = NULL;
|
|
|
|
unsigned EVIndex = 1;
|
|
|
|
if (EC->getOperand(1) == Incr)
|
|
|
|
EVIndex = 0;
|
|
|
|
EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex));
|
|
|
|
if (!EV) return;
|
|
|
|
uint64_t intEV = Type::Int32Ty->getPrimitiveSizeInBits();
|
2008-11-18 07:27:13 +08:00
|
|
|
if (!convertToInt(EV->getValueAPF(), &intEV))
|
2008-11-18 05:32:02 +08:00
|
|
|
return;
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2008-11-18 05:32:02 +08: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-04 02:32:19 +08:00
|
|
|
}
|
2008-11-18 05:32:02 +08:00
|
|
|
if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return;
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2008-11-18 05:32:02 +08:00
|
|
|
// Insert new integer induction variable.
|
|
|
|
PHINode *NewPHI = PHINode::Create(Type::Int32Ty,
|
|
|
|
PH->getName()+".int", PH);
|
2008-11-18 07:27:13 +08:00
|
|
|
NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue),
|
2008-11-18 05:32:02 +08:00
|
|
|
PH->getIncomingBlock(IncomingEdge));
|
|
|
|
|
2009-02-18 03:13:57 +08:00
|
|
|
Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
|
|
|
|
ConstantInt::get(Type::Int32Ty,
|
2008-11-18 07:27:13 +08:00
|
|
|
newIncrValue),
|
2008-11-18 05:32:02 +08:00
|
|
|
Incr->getName()+".int", Incr);
|
|
|
|
NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));
|
|
|
|
|
2009-04-28 05:03:15 +08:00
|
|
|
// The back edge is edge 1 of newPHI, whatever it may have been in the
|
|
|
|
// original PHI.
|
2008-11-18 05:32:02 +08:00
|
|
|
ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV);
|
2009-04-28 05:03:15 +08:00
|
|
|
Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV);
|
|
|
|
Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1));
|
2009-02-18 03:13:57 +08:00
|
|
|
ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(),
|
2008-11-18 05:32:02 +08:00
|
|
|
EC->getParent()->getTerminator());
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2009-05-12 10:17:14 +08: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-18 05:32:02 +08:00
|
|
|
// Delete old, floating point, exit comparision instruction.
|
2009-05-25 02:09:01 +08:00
|
|
|
NewEC->takeName(EC);
|
2008-11-18 05:32:02 +08:00
|
|
|
EC->replaceAllUsesWith(NewEC);
|
2009-05-12 10:17:14 +08:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(EC);
|
2009-02-18 03:13:57 +08:00
|
|
|
|
2008-11-18 05:32:02 +08:00
|
|
|
// Delete old, floating point, increment instruction.
|
|
|
|
Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
|
2009-05-12 10:17:14 +08: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-18 07:27:13 +08:00
|
|
|
}
|
2008-11-04 02:32:19 +08:00
|
|
|
|
2009-05-12 10:17:14 +08:00
|
|
|
// Add a new IVUsers entry for the newly-created integer PHI.
|
|
|
|
IU->AddUsersIfInteresting(NewPHI);
|
|
|
|
}
|