Handle multiple split conditions.

llvm-svn: 40941
This commit is contained in:
Devang Patel 2007-08-08 21:02:17 +00:00
parent 8574ac95f0
commit 593bf9ceb3
1 changed files with 93 additions and 60 deletions

View File

@ -45,6 +45,28 @@ namespace {
AU.addPreservedID(LoopSimplifyID); AU.addPreservedID(LoopSimplifyID);
} }
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;
};
private: private:
/// Find condition inside a loop that is suitable candidate for index split. /// Find condition inside a loop that is suitable candidate for index split.
void findSplitCondition(); void findSplitCondition();
@ -54,17 +76,17 @@ namespace {
/// entire (i.e. meaningful) loop body is dominated by this compare /// entire (i.e. meaningful) loop body is dominated by this compare
/// instruction then loop body is executed only for one iteration. In /// instruction then loop body is executed only for one iteration. In
/// such case eliminate loop structure surrounding this loop body. For /// such case eliminate loop structure surrounding this loop body. For
bool processOneIterationLoop(LPPassManager &LPM); bool processOneIterationLoop(SplitInfo &SD, LPPassManager &LPM);
// If loop header includes loop variant instruction operands then // If loop header includes loop variant instruction operands then
// this loop may not be eliminated. // this loop may not be eliminated.
bool safeHeader(BasicBlock *BB); bool safeHeader(SplitInfo &SD, BasicBlock *BB);
// If Exit block includes loop variant instructions then this // If Exit block includes loop variant instructions then this
// loop may not be eliminated. // loop may not be eliminated.
bool safeExitBlock(BasicBlock *BB); bool safeExitBlock(SplitInfo &SD, BasicBlock *BB);
bool splitLoop(); bool splitLoop(SplitInfo &SD);
private: private:
@ -72,20 +94,7 @@ namespace {
Loop *L; Loop *L;
ScalarEvolution *SE; ScalarEvolution *SE;
// Induction variable whose range is being split by this transformation. SmallVector<SplitInfo, 4> SplitData;
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;
}; };
char LoopIndexSplit::ID = 0; char LoopIndexSplit::ID = 0;
@ -100,30 +109,51 @@ LoopPass *llvm::createLoopIndexSplitPass() {
bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM) { bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM) {
bool Changed = false; bool Changed = false;
L = IncomingLoop; L = IncomingLoop;
SplitCondition = NULL;
SE = &getAnalysis<ScalarEvolution>(); SE = &getAnalysis<ScalarEvolution>();
findSplitCondition(); findSplitCondition();
if (!SplitCondition) if (SplitData.empty())
return false; return false;
if (SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) // First see if it is possible to eliminate loop itself or not.
// If it is possible to eliminate loop then do so. for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
Changed = processOneIterationLoop(LPM); E = SplitData.end(); SI != E; ++SI) {
else SplitInfo &SD = *SI;
Changed = splitLoop(); 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;
}
}
}
for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
E = SplitData.end(); SI != E; ++SI) {
SplitInfo &SD = *SI;
// ICM_EQs are already handled above.
if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
continue;
// FIXME : Collect Spliting cost for all SD. Only operate on profitable SDs.
Changed = splitLoop(SD);
}
if (Changed) if (Changed)
++NumIndexSplit; ++NumIndexSplit;
return Changed; return Changed;
} }
/// Find condition inside a loop that is suitable candidate for index split. /// Find condition inside a loop that is suitable candidate for index split.
void LoopIndexSplit::findSplitCondition() { void LoopIndexSplit::findSplitCondition() {
BasicBlock *Header = L->getHeader(); SplitInfo SD;
BasicBlock *Header = L->getHeader();
for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
PHINode *PN = cast<PHINode>(I); PHINode *PN = cast<PHINode>(I);
@ -140,29 +170,29 @@ void LoopIndexSplit::findSplitCondition() {
for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
UI != E; ++UI) { UI != E; ++UI) {
if (ICmpInst *CI = dyn_cast<ICmpInst>(*UI)) { if (ICmpInst *CI = dyn_cast<ICmpInst>(*UI)) {
SplitCondition = CI; SD.SplitCondition = CI;
break; break;
} }
} }
// Valid SplitCondition's one operand is phi node and the other operand // Valid SplitCondition's one operand is phi node and the other operand
// is loop invariant. // is loop invariant.
if (SplitCondition) { if (SD.SplitCondition) {
if (SplitCondition->getOperand(0) != PN) if (SD.SplitCondition->getOperand(0) != PN)
SplitValue = SplitCondition->getOperand(0); SD.SplitValue = SD.SplitCondition->getOperand(0);
else else
SplitValue = SplitCondition->getOperand(1); SD.SplitValue = SD.SplitCondition->getOperand(1);
SCEVHandle ValueSCEV = SE->getSCEV(SplitValue); SCEVHandle ValueSCEV = SE->getSCEV(SD.SplitValue);
// If SplitValue is not invariant then SplitCondition is not appropriate. // If SplitValue is not invariant then SplitCondition is not appropriate.
if (!ValueSCEV->isLoopInvariant(L)) if (!ValueSCEV->isLoopInvariant(L))
SplitCondition = NULL; SD.SplitCondition = NULL;
} }
// We are looking for only one split condition. // We are looking for only one split condition.
if (SplitCondition) { if (SD.SplitCondition) {
IndVar = PN; SD.IndVar = PN;
break; SplitData.push_back(SD);
} }
} }
} }
@ -182,7 +212,7 @@ void LoopIndexSplit::findSplitCondition() {
/// i = somevalue; /// i = somevalue;
/// loop_body /// loop_body
/// } /// }
bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) { bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD, LPPassManager &LPM) {
BasicBlock *Header = L->getHeader(); BasicBlock *Header = L->getHeader();
@ -191,7 +221,7 @@ bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) {
// If SplitCondition is not in loop header then this loop is not suitable // If SplitCondition is not in loop header then this loop is not suitable
// for this transformation. // for this transformation.
if (SplitCondition->getParent() != Header) if (SD.SplitCondition->getParent() != Header)
return false; return false;
// If one of the Header block's successor is not an exit block then this // If one of the Header block's successor is not an exit block then this
@ -209,12 +239,12 @@ bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) {
// If loop header includes loop variant instruction operands then // If loop header includes loop variant instruction operands then
// this loop may not be eliminated. // this loop may not be eliminated.
if (!safeHeader(Header)) if (!safeHeader(SD, Header))
return false; return false;
// If Exit block includes loop variant instructions then this // If Exit block includes loop variant instructions then this
// loop may not be eliminated. // loop may not be eliminated.
if (!safeExitBlock(ExitBlock)) if (!safeExitBlock(SD, ExitBlock))
return false; return false;
// Update CFG. // Update CFG.
@ -235,7 +265,7 @@ bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) {
BasicBlock *Preheader = L->getLoopPreheader(); BasicBlock *Preheader = L->getLoopPreheader();
Instruction *Terminator = Header->getTerminator(); Instruction *Terminator = Header->getTerminator();
Value *StartValue = IndVar->getIncomingValueForBlock(Preheader); Value *StartValue = SD.IndVar->getIncomingValueForBlock(Preheader);
// Replace split condition in header. // Replace split condition in header.
// Transform // Transform
@ -244,16 +274,19 @@ bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) {
// c1 = icmp uge i32 SplitValue, StartValue // c1 = icmp uge i32 SplitValue, StartValue
// c2 = icmp ult i32 vSplitValue, ExitValue // c2 = icmp ult i32 vSplitValue, ExitValue
// and i32 c1, c2 // and i32 c1, c2
bool SignedPredicate = ExitCondition->isSignedPredicate(); bool SignedPredicate = SD.ExitCondition->isSignedPredicate();
Instruction *C1 = new ICmpInst(SignedPredicate ? Instruction *C1 = new ICmpInst(SignedPredicate ?
ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
SplitValue, StartValue, "lisplit", Terminator); SD.SplitValue, StartValue, "lisplit",
Terminator);
Instruction *C2 = new ICmpInst(SignedPredicate ? Instruction *C2 = new ICmpInst(SignedPredicate ?
ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
SplitValue, ExitValue, "lisplit", Terminator); SD.SplitValue, SD.ExitValue, "lisplit",
Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit", Terminator); Terminator);
SplitCondition->replaceAllUsesWith(NSplitCond); Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
SplitCondition->eraseFromParent(); Terminator);
SD.SplitCondition->replaceAllUsesWith(NSplitCond);
SD.SplitCondition->eraseFromParent();
// Now, clear latch block. Remove instructions that are responsible // Now, clear latch block. Remove instructions that are responsible
// to increment induction variable. // to increment induction variable.
@ -275,7 +308,7 @@ bool LoopIndexSplit::processOneIterationLoop(LPPassManager &LPM) {
// If loop header includes loop variant instruction operands then // If loop header includes loop variant instruction operands then
// this loop can not be eliminated. This is used by processOneIterationLoop(). // this loop can not be eliminated. This is used by processOneIterationLoop().
bool LoopIndexSplit::safeHeader(BasicBlock *Header) { bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
Instruction *Terminator = Header->getTerminator(); Instruction *Terminator = Header->getTerminator();
for(BasicBlock::iterator BI = Header->begin(), BE = Header->end(); for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
@ -287,7 +320,7 @@ bool LoopIndexSplit::safeHeader(BasicBlock *Header) {
continue; continue;
// SplitCondition itself is OK. // SplitCondition itself is OK.
if (I == SplitCondition) if (I == SD.SplitCondition)
continue; continue;
// Terminator is also harmless. // Terminator is also harmless.
@ -303,7 +336,7 @@ bool LoopIndexSplit::safeHeader(BasicBlock *Header) {
// If Exit block includes loop variant instructions then this // If Exit block includes loop variant instructions then this
// loop may not be eliminated. This is used by processOneIterationLoop(). // loop may not be eliminated. This is used by processOneIterationLoop().
bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) { bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) {
Instruction *IndVarIncrement = NULL; Instruction *IndVarIncrement = NULL;
@ -334,7 +367,7 @@ bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) {
IndVarIncrement = I; IndVarIncrement = I;
} }
if (IndVarIncrement && PN == IndVar && CI->isOne()) if (IndVarIncrement && PN == SD.IndVar && CI->isOne())
continue; continue;
} }
@ -345,7 +378,7 @@ bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) {
++BI; ++BI;
Instruction *N = BI; Instruction *N = BI;
if (N == ExitBlock->getTerminator()) { if (N == ExitBlock->getTerminator()) {
ExitCondition = EC; SD.ExitCondition = EC;
continue; continue;
} }
} }
@ -357,19 +390,19 @@ bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) {
// Check if Exit condition is comparing induction variable against // Check if Exit condition is comparing induction variable against
// loop invariant value. If one operand is induction variable and // loop invariant value. If one operand is induction variable and
// the other operand is loop invaraint then Exit condition is safe. // the other operand is loop invaraint then Exit condition is safe.
if (ExitCondition) { if (SD.ExitCondition) {
Value *Op0 = ExitCondition->getOperand(0); Value *Op0 = SD.ExitCondition->getOperand(0);
Value *Op1 = ExitCondition->getOperand(1); Value *Op1 = SD.ExitCondition->getOperand(1);
Instruction *Insn0 = dyn_cast<Instruction>(Op0); Instruction *Insn0 = dyn_cast<Instruction>(Op0);
Instruction *Insn1 = dyn_cast<Instruction>(Op1); Instruction *Insn1 = dyn_cast<Instruction>(Op1);
if (Insn0 && Insn0 == IndVarIncrement) if (Insn0 && Insn0 == IndVarIncrement)
ExitValue = Op1; SD.ExitValue = Op1;
else if (Insn1 && Insn1 == IndVarIncrement) else if (Insn1 && Insn1 == IndVarIncrement)
ExitValue = Op0; SD.ExitValue = Op0;
SCEVHandle ValueSCEV = SE->getSCEV(ExitValue); SCEVHandle ValueSCEV = SE->getSCEV(SD.ExitValue);
if (!ValueSCEV->isLoopInvariant(L)) if (!ValueSCEV->isLoopInvariant(L))
return false; return false;
} }
@ -378,7 +411,7 @@ bool LoopIndexSplit::safeExitBlock(BasicBlock *ExitBlock) {
return true; return true;
} }
bool LoopIndexSplit::splitLoop() { bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
// FIXME :) // FIXME :)
return false; return false;
} }