2007-08-10 17:59:47 +00:00
|
|
|
//===- CloneLoop.cpp - Clone loop nest ------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2007-08-10 17:59:47 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the CloneLoop interface which makes a copy of a loop.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/Dominators.h"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// CloneDominatorInfo - Clone basicblock's dominator tree and, if available,
|
|
|
|
/// dominance info. It is expected that basic block is already cloned.
|
|
|
|
static void CloneDominatorInfo(BasicBlock *BB,
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy &VMap,
|
2007-08-10 17:59:47 +00:00
|
|
|
DominatorTree *DT,
|
|
|
|
DominanceFrontier *DF) {
|
|
|
|
|
|
|
|
assert (DT && "DominatorTree is not available");
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy::iterator BI = VMap.find(BB);
|
2010-06-23 23:55:51 +00:00
|
|
|
assert (BI != VMap.end() && "BasicBlock clone is missing");
|
2007-08-10 17:59:47 +00:00
|
|
|
BasicBlock *NewBB = cast<BasicBlock>(BI->second);
|
|
|
|
|
|
|
|
// NewBB already got dominator info.
|
|
|
|
if (DT->getNode(NewBB))
|
|
|
|
return;
|
|
|
|
|
|
|
|
assert (DT->getNode(BB) && "BasicBlock does not have dominator info");
|
|
|
|
// Entry block is not expected here. Infinite loops are not to cloned.
|
|
|
|
assert (DT->getNode(BB)->getIDom() && "BasicBlock does not have immediate dominator");
|
|
|
|
BasicBlock *BBDom = DT->getNode(BB)->getIDom()->getBlock();
|
|
|
|
|
|
|
|
// NewBB's dominator is either BB's dominator or BB's dominator's clone.
|
|
|
|
BasicBlock *NewBBDom = BBDom;
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy::iterator BBDomI = VMap.find(BBDom);
|
2010-06-23 23:55:51 +00:00
|
|
|
if (BBDomI != VMap.end()) {
|
2007-08-10 17:59:47 +00:00
|
|
|
NewBBDom = cast<BasicBlock>(BBDomI->second);
|
|
|
|
if (!DT->getNode(NewBBDom))
|
2010-06-23 23:55:51 +00:00
|
|
|
CloneDominatorInfo(BBDom, VMap, DT, DF);
|
2007-08-10 17:59:47 +00:00
|
|
|
}
|
|
|
|
DT->addNewBlock(NewBB, NewBBDom);
|
|
|
|
|
|
|
|
// Copy cloned dominance frontiner set
|
|
|
|
if (DF) {
|
|
|
|
DominanceFrontier::DomSetType NewDFSet;
|
|
|
|
DominanceFrontier::iterator DFI = DF->find(BB);
|
|
|
|
if ( DFI != DF->end()) {
|
|
|
|
DominanceFrontier::DomSetType S = DFI->second;
|
|
|
|
for (DominanceFrontier::DomSetType::iterator I = S.begin(), E = S.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *DB = *I;
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy::iterator IDM = VMap.find(DB);
|
2010-06-23 23:55:51 +00:00
|
|
|
if (IDM != VMap.end())
|
2007-08-10 17:59:47 +00:00
|
|
|
NewDFSet.insert(cast<BasicBlock>(IDM->second));
|
|
|
|
else
|
|
|
|
NewDFSet.insert(DB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DF->addBasicBlock(NewBB, NewDFSet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:55:51 +00:00
|
|
|
/// CloneLoop - Clone Loop. Clone dominator info. Populate VMap
|
2007-08-10 17:59:47 +00:00
|
|
|
/// using old blocks to new blocks mapping.
|
|
|
|
Loop *llvm::CloneLoop(Loop *OrigL, LPPassManager *LPM, LoopInfo *LI,
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy &VMap, Pass *P) {
|
2007-08-10 17:59:47 +00:00
|
|
|
|
|
|
|
DominatorTree *DT = NULL;
|
|
|
|
DominanceFrontier *DF = NULL;
|
|
|
|
if (P) {
|
2009-01-28 13:14:17 +00:00
|
|
|
DT = P->getAnalysisIfAvailable<DominatorTree>();
|
|
|
|
DF = P->getAnalysisIfAvailable<DominanceFrontier>();
|
2007-08-10 17:59:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<BasicBlock *, 16> NewBlocks;
|
|
|
|
|
2007-08-14 23:59:17 +00:00
|
|
|
// Populate loop nest.
|
|
|
|
SmallVector<Loop *, 8> LoopNest;
|
|
|
|
LoopNest.push_back(OrigL);
|
|
|
|
|
|
|
|
|
|
|
|
Loop *NewParentLoop = NULL;
|
2010-01-05 16:27:25 +00:00
|
|
|
do {
|
2009-05-06 17:22:41 +00:00
|
|
|
Loop *L = LoopNest.pop_back_val();
|
2007-08-14 23:59:17 +00:00
|
|
|
Loop *NewLoop = new Loop();
|
|
|
|
|
|
|
|
if (!NewParentLoop)
|
|
|
|
NewParentLoop = NewLoop;
|
|
|
|
|
2007-08-10 17:59:47 +00:00
|
|
|
LPM->insertLoop(NewLoop, L->getParentLoop());
|
|
|
|
|
|
|
|
// Clone Basic Blocks.
|
|
|
|
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
2010-06-23 23:55:51 +00:00
|
|
|
BasicBlock *NewBB = CloneBasicBlock(BB, VMap, ".clone");
|
|
|
|
VMap[BB] = NewBB;
|
2007-08-10 17:59:47 +00:00
|
|
|
if (P)
|
|
|
|
LPM->cloneBasicBlockSimpleAnalysis(BB, NewBB, L);
|
2007-11-27 03:43:35 +00:00
|
|
|
NewLoop->addBasicBlockToLoop(NewBB, LI->getBase());
|
2007-08-10 17:59:47 +00:00
|
|
|
NewBlocks.push_back(NewBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone dominator info.
|
|
|
|
if (DT)
|
|
|
|
for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
2010-06-23 23:55:51 +00:00
|
|
|
CloneDominatorInfo(BB, VMap, DT, DF);
|
2007-08-10 17:59:47 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 23:59:17 +00:00
|
|
|
// Process sub loops
|
|
|
|
for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
|
|
|
|
LoopNest.push_back(*I);
|
2010-01-05 16:27:25 +00:00
|
|
|
} while (!LoopNest.empty());
|
2007-08-10 17:59:47 +00:00
|
|
|
|
2010-06-23 23:55:51 +00:00
|
|
|
// Remap instructions to reference operands from VMap.
|
2007-08-10 17:59:47 +00:00
|
|
|
for(SmallVector<BasicBlock *, 16>::iterator NBItr = NewBlocks.begin(),
|
|
|
|
NBE = NewBlocks.end(); NBItr != NBE; ++NBItr) {
|
|
|
|
BasicBlock *NB = *NBItr;
|
|
|
|
for(BasicBlock::iterator BI = NB->begin(), BE = NB->end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
Instruction *Insn = BI;
|
|
|
|
for (unsigned index = 0, num_ops = Insn->getNumOperands();
|
|
|
|
index != num_ops; ++index) {
|
|
|
|
Value *Op = Insn->getOperand(index);
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy::iterator OpItr = VMap.find(Op);
|
2010-06-23 23:55:51 +00:00
|
|
|
if (OpItr != VMap.end())
|
2007-08-10 17:59:47 +00:00
|
|
|
Insn->setOperand(index, OpItr->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock *Latch = OrigL->getLoopLatch();
|
|
|
|
Function *F = Latch->getParent();
|
2007-09-04 20:46:35 +00:00
|
|
|
F->getBasicBlockList().insert(OrigL->getHeader(),
|
|
|
|
NewBlocks.begin(), NewBlocks.end());
|
|
|
|
|
2007-08-10 17:59:47 +00:00
|
|
|
|
2007-08-14 23:59:17 +00:00
|
|
|
return NewParentLoop;
|
2007-08-10 17:59:47 +00:00
|
|
|
}
|