Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 1 | //===- CloneLoop.cpp - Clone loop nest ------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the CloneLoop interface which makes a copy of a loop. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/Cloning.h" |
| 15 | #include "llvm/BasicBlock.h" |
| 16 | #include "llvm/Analysis/LoopPass.h" |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Dominators.h" |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 22 | /// CloneDominatorInfo - Clone a basic block's dominator tree. It is expected |
| 23 | /// that the basic block is already cloned. |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 24 | static void CloneDominatorInfo(BasicBlock *BB, |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 25 | ValueToValueMapTy &VMap, |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 26 | DominatorTree *DT) { |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 27 | |
| 28 | assert (DT && "DominatorTree is not available"); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 29 | ValueToValueMapTy::iterator BI = VMap.find(BB); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 30 | assert (BI != VMap.end() && "BasicBlock clone is missing"); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 31 | BasicBlock *NewBB = cast<BasicBlock>(BI->second); |
| 32 | |
| 33 | // NewBB already got dominator info. |
| 34 | if (DT->getNode(NewBB)) |
| 35 | return; |
| 36 | |
| 37 | assert (DT->getNode(BB) && "BasicBlock does not have dominator info"); |
| 38 | // Entry block is not expected here. Infinite loops are not to cloned. |
| 39 | assert (DT->getNode(BB)->getIDom() && "BasicBlock does not have immediate dominator"); |
| 40 | BasicBlock *BBDom = DT->getNode(BB)->getIDom()->getBlock(); |
| 41 | |
| 42 | // NewBB's dominator is either BB's dominator or BB's dominator's clone. |
| 43 | BasicBlock *NewBBDom = BBDom; |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 44 | ValueToValueMapTy::iterator BBDomI = VMap.find(BBDom); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 45 | if (BBDomI != VMap.end()) { |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 46 | NewBBDom = cast<BasicBlock>(BBDomI->second); |
| 47 | if (!DT->getNode(NewBBDom)) |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 48 | CloneDominatorInfo(BBDom, VMap, DT); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 49 | } |
| 50 | DT->addNewBlock(NewBB, NewBBDom); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 53 | /// CloneLoop - Clone Loop. Clone dominator info. Populate VMap |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 54 | /// using old blocks to new blocks mapping. |
| 55 | Loop *llvm::CloneLoop(Loop *OrigL, LPPassManager *LPM, LoopInfo *LI, |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 56 | ValueToValueMapTy &VMap, Pass *P) { |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 57 | |
| 58 | DominatorTree *DT = NULL; |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 59 | if (P) |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 60 | DT = P->getAnalysisIfAvailable<DominatorTree>(); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 61 | |
| 62 | SmallVector<BasicBlock *, 16> NewBlocks; |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 63 | |
Devang Patel | 4f5d78e | 2007-08-14 23:59:17 +0000 | [diff] [blame] | 64 | // Populate loop nest. |
| 65 | SmallVector<Loop *, 8> LoopNest; |
| 66 | LoopNest.push_back(OrigL); |
| 67 | |
| 68 | |
| 69 | Loop *NewParentLoop = NULL; |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 70 | do { |
Dan Gohman | e9d87f4 | 2009-05-06 17:22:41 +0000 | [diff] [blame] | 71 | Loop *L = LoopNest.pop_back_val(); |
Devang Patel | 4f5d78e | 2007-08-14 23:59:17 +0000 | [diff] [blame] | 72 | Loop *NewLoop = new Loop(); |
| 73 | |
| 74 | if (!NewParentLoop) |
| 75 | NewParentLoop = NewLoop; |
| 76 | |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 77 | LPM->insertLoop(NewLoop, L->getParentLoop()); |
| 78 | |
| 79 | // Clone Basic Blocks. |
| 80 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 81 | I != E; ++I) { |
| 82 | BasicBlock *BB = *I; |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 83 | BasicBlock *NewBB = CloneBasicBlock(BB, VMap, ".clone"); |
| 84 | VMap[BB] = NewBB; |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 85 | if (P) |
| 86 | LPM->cloneBasicBlockSimpleAnalysis(BB, NewBB, L); |
Owen Anderson | d735ee8 | 2007-11-27 03:43:35 +0000 | [diff] [blame] | 87 | NewLoop->addBasicBlockToLoop(NewBB, LI->getBase()); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 88 | NewBlocks.push_back(NewBB); |
| 89 | } |
| 90 | |
| 91 | // Clone dominator info. |
| 92 | if (DT) |
| 93 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 94 | I != E; ++I) { |
| 95 | BasicBlock *BB = *I; |
Cameron Zwarich | 3012787 | 2011-01-18 04:11:31 +0000 | [diff] [blame] | 96 | CloneDominatorInfo(BB, VMap, DT); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Devang Patel | 4f5d78e | 2007-08-14 23:59:17 +0000 | [diff] [blame] | 99 | // Process sub loops |
| 100 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 101 | LoopNest.push_back(*I); |
Dan Gohman | 321a813 | 2010-01-05 16:27:25 +0000 | [diff] [blame] | 102 | } while (!LoopNest.empty()); |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 103 | |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 104 | // Remap instructions to reference operands from VMap. |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 105 | for(SmallVector<BasicBlock *, 16>::iterator NBItr = NewBlocks.begin(), |
| 106 | NBE = NewBlocks.end(); NBItr != NBE; ++NBItr) { |
| 107 | BasicBlock *NB = *NBItr; |
| 108 | for(BasicBlock::iterator BI = NB->begin(), BE = NB->end(); |
| 109 | BI != BE; ++BI) { |
| 110 | Instruction *Insn = BI; |
| 111 | for (unsigned index = 0, num_ops = Insn->getNumOperands(); |
| 112 | index != num_ops; ++index) { |
| 113 | Value *Op = Insn->getOperand(index); |
Rafael Espindola | 1ed219a | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 114 | ValueToValueMapTy::iterator OpItr = VMap.find(Op); |
Devang Patel | 29d3dd8 | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 115 | if (OpItr != VMap.end()) |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 116 | Insn->setOperand(index, OpItr->second); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | BasicBlock *Latch = OrigL->getLoopLatch(); |
| 122 | Function *F = Latch->getParent(); |
Devang Patel | d24e599 | 2007-09-04 20:46:35 +0000 | [diff] [blame] | 123 | F->getBasicBlockList().insert(OrigL->getHeader(), |
| 124 | NewBlocks.begin(), NewBlocks.end()); |
| 125 | |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 126 | |
Devang Patel | 4f5d78e | 2007-08-14 23:59:17 +0000 | [diff] [blame] | 127 | return NewParentLoop; |
Devang Patel | 4bc2a0b | 2007-08-10 17:59:47 +0000 | [diff] [blame] | 128 | } |