2007-08-07 08:25:56 +08:00
|
|
|
//===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Devang Patel and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements Loop Index Splitting Pass.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loop-index-split"
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2007-08-08 07:17:52 +08:00
|
|
|
#include "llvm/Function.h"
|
2007-08-07 08:25:56 +08:00
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2007-08-09 05:39:47 +08:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2007-08-07 08:25:56 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumIndexSplit, "Number of loops index split");
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass ID, replacement for typeid
|
|
|
|
LoopIndexSplit() : LoopPass((intptr_t)&ID) {}
|
|
|
|
|
|
|
|
// Index split Loop L. Return true if loop is split.
|
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM);
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<ScalarEvolution>();
|
|
|
|
AU.addPreserved<ScalarEvolution>();
|
|
|
|
AU.addRequiredID(LCSSAID);
|
|
|
|
AU.addPreservedID(LCSSAID);
|
|
|
|
AU.addPreserved<LoopInfo>();
|
|
|
|
AU.addRequiredID(LoopSimplifyID);
|
|
|
|
AU.addPreservedID(LoopSimplifyID);
|
2007-08-09 06:25:28 +08:00
|
|
|
AU.addRequired<DominatorTree>();
|
2007-08-09 05:39:47 +08:00
|
|
|
AU.addPreserved<DominatorTree>();
|
|
|
|
AU.addPreserved<DominanceFrontier>();
|
2007-08-07 08:25:56 +08:00
|
|
|
}
|
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
private:
|
|
|
|
|
|
|
|
class SplitInfo {
|
|
|
|
public:
|
|
|
|
SplitInfo() : IndVar(NULL), SplitValue(NULL), ExitValue(NULL),
|
|
|
|
SplitCondition(NULL), ExitCondition(NULL) {}
|
|
|
|
// Induction variable whose range is being split by this transformation.
|
|
|
|
PHINode *IndVar;
|
|
|
|
|
|
|
|
// Induction variable's range is split at this value.
|
|
|
|
Value *SplitValue;
|
|
|
|
|
|
|
|
// Induction variable's final loop exit value.
|
|
|
|
Value *ExitValue;
|
|
|
|
|
|
|
|
// This compare instruction compares IndVar against SplitValue.
|
|
|
|
ICmpInst *SplitCondition;
|
|
|
|
|
|
|
|
// Loop exit condition.
|
|
|
|
ICmpInst *ExitCondition;
|
2007-08-09 05:18:27 +08:00
|
|
|
|
|
|
|
// Clear split info.
|
|
|
|
void clear() {
|
|
|
|
IndVar = NULL;
|
|
|
|
SplitValue = NULL;
|
|
|
|
ExitValue = NULL;
|
|
|
|
SplitCondition = NULL;
|
|
|
|
ExitCondition = NULL;
|
|
|
|
}
|
2007-08-09 05:02:17 +08:00
|
|
|
};
|
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
private:
|
|
|
|
/// Find condition inside a loop that is suitable candidate for index split.
|
|
|
|
void findSplitCondition();
|
|
|
|
|
|
|
|
/// processOneIterationLoop - Current loop L contains compare instruction
|
|
|
|
/// that compares induction variable, IndVar, agains loop invariant. If
|
|
|
|
/// entire (i.e. meaningful) loop body is dominated by this compare
|
|
|
|
/// instruction then loop body is executed only for one iteration. In
|
|
|
|
/// such case eliminate loop structure surrounding this loop body. For
|
2007-08-09 05:02:17 +08:00
|
|
|
bool processOneIterationLoop(SplitInfo &SD, LPPassManager &LPM);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
2007-08-09 06:25:28 +08:00
|
|
|
/// If loop header includes loop variant instruction operands then
|
|
|
|
/// this loop may not be eliminated.
|
2007-08-09 05:02:17 +08:00
|
|
|
bool safeHeader(SplitInfo &SD, BasicBlock *BB);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
2007-08-09 06:25:28 +08:00
|
|
|
/// If Exit block includes loop variant instructions then this
|
|
|
|
/// loop may not be eliminated.
|
2007-08-09 05:02:17 +08:00
|
|
|
bool safeExitBlock(SplitInfo &SD, BasicBlock *BB);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
2007-08-09 06:25:28 +08:00
|
|
|
/// Find cost of spliting loop L.
|
|
|
|
unsigned findSplitCost(Loop *L, SplitInfo &SD);
|
2007-08-09 05:02:17 +08:00
|
|
|
bool splitLoop(SplitInfo &SD);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Current Loop.
|
|
|
|
Loop *L;
|
|
|
|
ScalarEvolution *SE;
|
2007-08-09 06:25:28 +08:00
|
|
|
DominatorTree *DT;
|
2007-08-09 05:02:17 +08:00
|
|
|
SmallVector<SplitInfo, 4> SplitData;
|
2007-08-07 08:25:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
char LoopIndexSplit::ID = 0;
|
|
|
|
RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
|
|
|
|
}
|
|
|
|
|
|
|
|
LoopPass *llvm::createLoopIndexSplitPass() {
|
|
|
|
return new LoopIndexSplit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index split Loop L. Return true if loop is split.
|
|
|
|
bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM) {
|
|
|
|
bool Changed = false;
|
|
|
|
L = IncomingLoop;
|
2007-08-09 05:02:17 +08:00
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
2007-08-09 06:25:28 +08:00
|
|
|
DT = &getAnalysis<DominatorTree>();
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
findSplitCondition();
|
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
if (SplitData.empty())
|
2007-08-07 08:25:56 +08:00
|
|
|
return false;
|
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
// First see if it is possible to eliminate loop itself or not.
|
|
|
|
for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
|
|
|
|
E = SplitData.end(); SI != E; ++SI) {
|
|
|
|
SplitInfo &SD = *SI;
|
|
|
|
if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
|
|
|
|
Changed = processOneIterationLoop(SD,LPM);
|
|
|
|
if (Changed) {
|
|
|
|
++NumIndexSplit;
|
|
|
|
// If is loop is eliminated then nothing else to do here.
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-09 06:25:28 +08:00
|
|
|
unsigned MaxCost = 99;
|
|
|
|
unsigned Index = 0;
|
|
|
|
unsigned MostProfitableSDIndex = 0;
|
2007-08-09 05:02:17 +08:00
|
|
|
for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
|
2007-08-09 06:25:28 +08:00
|
|
|
E = SplitData.end(); SI != E; ++SI, ++Index) {
|
|
|
|
SplitInfo SD = *SI;
|
2007-08-09 05:02:17 +08:00
|
|
|
|
|
|
|
// ICM_EQs are already handled above.
|
2007-08-09 06:25:28 +08:00
|
|
|
if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
|
2007-08-09 05:02:17 +08:00
|
|
|
continue;
|
2007-08-09 06:25:28 +08:00
|
|
|
|
|
|
|
unsigned Cost = findSplitCost(L, SD);
|
|
|
|
if (Cost < MaxCost)
|
|
|
|
MostProfitableSDIndex = Index;
|
2007-08-09 05:02:17 +08:00
|
|
|
}
|
2007-08-07 08:25:56 +08:00
|
|
|
|
2007-08-09 06:25:28 +08:00
|
|
|
// Split most profitiable condition.
|
|
|
|
Changed = splitLoop(SplitData[MostProfitableSDIndex]);
|
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
if (Changed)
|
|
|
|
++NumIndexSplit;
|
2007-08-09 05:02:17 +08:00
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find condition inside a loop that is suitable candidate for index split.
|
|
|
|
void LoopIndexSplit::findSplitCondition() {
|
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
SplitInfo SD;
|
|
|
|
BasicBlock *Header = L->getHeader();
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
|
|
|
|
if (!PN->getType()->isInteger())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SCEVHandle SCEV = SE->getSCEV(PN);
|
|
|
|
if (!isa<SCEVAddRecExpr>(SCEV))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this phi node is used in a compare instruction then it is a
|
|
|
|
// split condition candidate.
|
|
|
|
for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
|
|
|
|
UI != E; ++UI) {
|
|
|
|
if (ICmpInst *CI = dyn_cast<ICmpInst>(*UI)) {
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.SplitCondition = CI;
|
2007-08-07 08:25:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid SplitCondition's one operand is phi node and the other operand
|
|
|
|
// is loop invariant.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (SD.SplitCondition) {
|
|
|
|
if (SD.SplitCondition->getOperand(0) != PN)
|
|
|
|
SD.SplitValue = SD.SplitCondition->getOperand(0);
|
2007-08-07 08:25:56 +08:00
|
|
|
else
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.SplitValue = SD.SplitCondition->getOperand(1);
|
|
|
|
SCEVHandle ValueSCEV = SE->getSCEV(SD.SplitValue);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
// If SplitValue is not invariant then SplitCondition is not appropriate.
|
|
|
|
if (!ValueSCEV->isLoopInvariant(L))
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.SplitCondition = NULL;
|
2007-08-07 08:25:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We are looking for only one split condition.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (SD.SplitCondition) {
|
|
|
|
SD.IndVar = PN;
|
|
|
|
SplitData.push_back(SD);
|
2007-08-09 05:18:27 +08:00
|
|
|
// Before reusing SD for next split condition clear its content.
|
|
|
|
SD.clear();
|
2007-08-07 08:25:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// processOneIterationLoop - Current loop L contains compare instruction
|
|
|
|
/// that compares induction variable, IndVar, against loop invariant. If
|
|
|
|
/// entire (i.e. meaningful) loop body is dominated by this compare
|
|
|
|
/// instruction then loop body is executed only once. In such case eliminate
|
|
|
|
/// loop structure surrounding this loop body. For example,
|
|
|
|
/// for (int i = start; i < end; ++i) {
|
|
|
|
/// if ( i == somevalue) {
|
|
|
|
/// loop_body
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// can be transformed into
|
|
|
|
/// if (somevalue >= start && somevalue < end) {
|
|
|
|
/// i = somevalue;
|
|
|
|
/// loop_body
|
|
|
|
/// }
|
2007-08-09 05:02:17 +08:00
|
|
|
bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD, LPPassManager &LPM) {
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
BasicBlock *Header = L->getHeader();
|
|
|
|
|
|
|
|
// First of all, check if SplitCondition dominates entire loop body
|
|
|
|
// or not.
|
|
|
|
|
|
|
|
// If SplitCondition is not in loop header then this loop is not suitable
|
|
|
|
// for this transformation.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (SD.SplitCondition->getParent() != Header)
|
2007-08-07 08:25:56 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If one of the Header block's successor is not an exit block then this
|
|
|
|
// loop is not a suitable candidate.
|
|
|
|
BasicBlock *ExitBlock = NULL;
|
|
|
|
for (succ_iterator SI = succ_begin(Header), E = succ_end(Header); SI != E; ++SI) {
|
|
|
|
if (L->isLoopExit(*SI)) {
|
|
|
|
ExitBlock = *SI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExitBlock)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If loop header includes loop variant instruction operands then
|
|
|
|
// this loop may not be eliminated.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (!safeHeader(SD, Header))
|
2007-08-07 08:25:56 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// If Exit block includes loop variant instructions then this
|
|
|
|
// loop may not be eliminated.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (!safeExitBlock(SD, ExitBlock))
|
2007-08-07 08:25:56 +08:00
|
|
|
return false;
|
|
|
|
|
2007-08-08 09:51:27 +08:00
|
|
|
// Update CFG.
|
|
|
|
|
|
|
|
// As a first step to break this loop, remove Latch to Header edge.
|
2007-08-07 08:25:56 +08:00
|
|
|
BasicBlock *Latch = L->getLoopLatch();
|
2007-08-08 09:51:27 +08:00
|
|
|
BasicBlock *LatchSucc = NULL;
|
|
|
|
BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
|
|
|
|
if (!BR)
|
|
|
|
return false;
|
|
|
|
Header->removePredecessor(Latch);
|
|
|
|
for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
|
|
|
|
SI != E; ++SI) {
|
|
|
|
if (Header != *SI)
|
|
|
|
LatchSucc = *SI;
|
|
|
|
}
|
|
|
|
BR->setUnconditionalDest(LatchSucc);
|
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
BasicBlock *Preheader = L->getLoopPreheader();
|
|
|
|
Instruction *Terminator = Header->getTerminator();
|
2007-08-09 05:02:17 +08:00
|
|
|
Value *StartValue = SD.IndVar->getIncomingValueForBlock(Preheader);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
// Replace split condition in header.
|
|
|
|
// Transform
|
|
|
|
// SplitCondition : icmp eq i32 IndVar, SplitValue
|
|
|
|
// into
|
|
|
|
// c1 = icmp uge i32 SplitValue, StartValue
|
|
|
|
// c2 = icmp ult i32 vSplitValue, ExitValue
|
|
|
|
// and i32 c1, c2
|
2007-08-09 05:02:17 +08:00
|
|
|
bool SignedPredicate = SD.ExitCondition->isSignedPredicate();
|
2007-08-07 08:25:56 +08:00
|
|
|
Instruction *C1 = new ICmpInst(SignedPredicate ?
|
|
|
|
ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.SplitValue, StartValue, "lisplit",
|
|
|
|
Terminator);
|
2007-08-07 08:25:56 +08:00
|
|
|
Instruction *C2 = new ICmpInst(SignedPredicate ?
|
|
|
|
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.SplitValue, SD.ExitValue, "lisplit",
|
|
|
|
Terminator);
|
|
|
|
Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
|
|
|
|
Terminator);
|
|
|
|
SD.SplitCondition->replaceAllUsesWith(NSplitCond);
|
|
|
|
SD.SplitCondition->eraseFromParent();
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
// Now, clear latch block. Remove instructions that are responsible
|
|
|
|
// to increment induction variable.
|
|
|
|
Instruction *LTerminator = Latch->getTerminator();
|
|
|
|
for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
|
|
|
|
LB != LE; ) {
|
|
|
|
Instruction *I = LB;
|
|
|
|
++LB;
|
|
|
|
if (isa<PHINode>(I) || I == LTerminator)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
I->replaceAllUsesWith(UndefValue::get(I->getType()));
|
2007-08-08 01:45:35 +08:00
|
|
|
I->eraseFromParent();
|
2007-08-07 08:25:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LPM.deleteLoopFromQueue(L);
|
2007-08-09 05:39:47 +08:00
|
|
|
|
|
|
|
// Update Dominator Info.
|
|
|
|
// Only CFG change done is to remove Latch to Header edge. This
|
|
|
|
// does not change dominator tree because Latch did not dominate
|
|
|
|
// Header.
|
|
|
|
if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>()) {
|
|
|
|
DominanceFrontier::iterator HeaderDF = DF->find(Header);
|
|
|
|
if (HeaderDF != DF->end())
|
|
|
|
DF->removeFromFrontier(HeaderDF, Header);
|
|
|
|
|
|
|
|
DominanceFrontier::iterator LatchDF = DF->find(Latch);
|
|
|
|
if (LatchDF != DF->end())
|
|
|
|
DF->removeFromFrontier(LatchDF, Header);
|
|
|
|
}
|
2007-08-07 08:25:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If loop header includes loop variant instruction operands then
|
|
|
|
// this loop can not be eliminated. This is used by processOneIterationLoop().
|
2007-08-09 05:02:17 +08:00
|
|
|
bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
Instruction *Terminator = Header->getTerminator();
|
|
|
|
for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
Instruction *I = BI;
|
|
|
|
|
2007-08-08 09:51:27 +08:00
|
|
|
// PHI Nodes are OK. FIXME : Handle last value assignments.
|
2007-08-07 08:25:56 +08:00
|
|
|
if (isa<PHINode>(I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// SplitCondition itself is OK.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (I == SD.SplitCondition)
|
2007-08-08 09:51:27 +08:00
|
|
|
continue;
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
// Terminator is also harmless.
|
|
|
|
if (I == Terminator)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Otherwise we have a instruction that may not be safe.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Exit block includes loop variant instructions then this
|
|
|
|
// loop may not be eliminated. This is used by processOneIterationLoop().
|
2007-08-09 05:02:17 +08:00
|
|
|
bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) {
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
Instruction *IndVarIncrement = NULL;
|
2007-08-08 09:51:27 +08:00
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
Instruction *I = BI;
|
|
|
|
|
2007-08-08 09:51:27 +08:00
|
|
|
// PHI Nodes are OK. FIXME : Handle last value assignments.
|
2007-08-07 08:25:56 +08:00
|
|
|
if (isa<PHINode>(I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if I is induction variable increment instruction.
|
|
|
|
if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(I)) {
|
|
|
|
if (BOp->getOpcode() != Instruction::Add)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Value *Op0 = BOp->getOperand(0);
|
|
|
|
Value *Op1 = BOp->getOperand(1);
|
|
|
|
PHINode *PN = NULL;
|
|
|
|
ConstantInt *CI = NULL;
|
|
|
|
|
|
|
|
if ((PN = dyn_cast<PHINode>(Op0))) {
|
|
|
|
if ((CI = dyn_cast<ConstantInt>(Op1)))
|
|
|
|
IndVarIncrement = I;
|
|
|
|
} else
|
|
|
|
if ((PN = dyn_cast<PHINode>(Op1))) {
|
|
|
|
if ((CI = dyn_cast<ConstantInt>(Op0)))
|
|
|
|
IndVarIncrement = I;
|
|
|
|
}
|
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
if (IndVarIncrement && PN == SD.IndVar && CI->isOne())
|
2007-08-07 08:25:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
2007-08-08 09:51:27 +08:00
|
|
|
|
2007-08-07 08:25:56 +08:00
|
|
|
// I is an Exit condition if next instruction is block terminator.
|
|
|
|
// Exit condition is OK if it compares loop invariant exit value,
|
|
|
|
// which is checked below.
|
2007-08-08 07:17:52 +08:00
|
|
|
else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
|
2007-08-07 08:25:56 +08:00
|
|
|
++BI;
|
|
|
|
Instruction *N = BI;
|
|
|
|
if (N == ExitBlock->getTerminator()) {
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.ExitCondition = EC;
|
2007-08-08 09:51:27 +08:00
|
|
|
continue;
|
2007-08-07 08:25:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we have instruction that may not be safe.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Exit condition is comparing induction variable against
|
|
|
|
// loop invariant value. If one operand is induction variable and
|
|
|
|
// the other operand is loop invaraint then Exit condition is safe.
|
2007-08-09 05:02:17 +08:00
|
|
|
if (SD.ExitCondition) {
|
|
|
|
Value *Op0 = SD.ExitCondition->getOperand(0);
|
|
|
|
Value *Op1 = SD.ExitCondition->getOperand(1);
|
2007-08-07 08:25:56 +08:00
|
|
|
|
|
|
|
Instruction *Insn0 = dyn_cast<Instruction>(Op0);
|
|
|
|
Instruction *Insn1 = dyn_cast<Instruction>(Op1);
|
|
|
|
|
|
|
|
if (Insn0 && Insn0 == IndVarIncrement)
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.ExitValue = Op1;
|
2007-08-07 08:25:56 +08:00
|
|
|
else if (Insn1 && Insn1 == IndVarIncrement)
|
2007-08-09 05:02:17 +08:00
|
|
|
SD.ExitValue = Op0;
|
2007-08-07 08:25:56 +08:00
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
SCEVHandle ValueSCEV = SE->getSCEV(SD.ExitValue);
|
2007-08-07 08:25:56 +08:00
|
|
|
if (!ValueSCEV->isLoopInvariant(L))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could not find any reason to consider ExitBlock unsafe.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-08-09 06:25:28 +08:00
|
|
|
/// Find cost of spliting loop L. Cost is measured in terms of size growth.
|
|
|
|
/// Size is growth is calculated based on amount of code duplicated in second
|
|
|
|
/// loop.
|
|
|
|
unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) {
|
|
|
|
|
|
|
|
unsigned Cost = 0;
|
|
|
|
BasicBlock *SDBlock = SD.SplitCondition->getParent();
|
|
|
|
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
|
|
|
// If a block is not dominated by split condition block then
|
|
|
|
// it must be duplicated in both loops.
|
|
|
|
if (!DT->dominates(SDBlock, BB))
|
|
|
|
Cost += BB->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Cost;
|
|
|
|
}
|
|
|
|
|
2007-08-09 05:02:17 +08:00
|
|
|
bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
|
2007-08-07 08:25:56 +08:00
|
|
|
// FIXME :)
|
|
|
|
return false;
|
|
|
|
}
|