Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 1 | //===- PromoteMemoryToRegister.cpp - Convert allocas to registers ---------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | d3db022 | 2002-02-12 17:16:22 +0000 | [diff] [blame] | 9 | // |
Gordon Henriksen | c86b677 | 2007-11-04 16:15:04 +0000 | [diff] [blame] | 10 | // This file promotes memory references to be register references. It promotes |
Chris Lattner | a744b77 | 2004-09-19 18:51:51 +0000 | [diff] [blame] | 11 | // alloca instructions which only have loads and stores as uses. An alloca is |
| 12 | // transformed by using dominator frontiers to place PHI nodes, then traversing |
| 13 | // the function in depth-first order to rewrite loads and stores as appropriate. |
| 14 | // This is just the standard SSA construction algorithm to construct "pruned" |
| 15 | // SSA form. |
Chris Lattner | d3db022 | 2002-02-12 17:16:22 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "mem2reg" |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | b20724d | 2004-10-16 18:10:06 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/Instructions.h" |
Devang Patel | 180ffae | 2008-11-07 01:30:07 +0000 | [diff] [blame] | 25 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 26 | #include "llvm/LLVMContext.h" |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/Dominators.h" |
| 28 | #include "llvm/Analysis/AliasSetTracker.h" |
Chris Lattner | d387404 | 2007-02-05 23:37:20 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | c837615 | 2007-02-05 22:13:11 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 40b6555 | 2007-02-05 21:58:48 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Bill Wendling | 068a795 | 2008-11-07 01:59:41 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 393689a | 2003-04-18 19:25:22 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CFG.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Compiler.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 38 | |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 39 | STATISTIC(NumLocalPromoted, "Number of alloca's promoted within one block"); |
| 40 | STATISTIC(NumSingleStore, "Number of alloca's promoted with a single store"); |
| 41 | STATISTIC(NumDeadAlloca, "Number of dead alloca's removed"); |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 42 | STATISTIC(NumPHIInsert, "Number of PHI nodes inserted"); |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 44 | // Provide DenseMapInfo for all pointers. |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 45 | namespace llvm { |
| 46 | template<> |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 47 | struct DenseMapInfo<std::pair<BasicBlock*, unsigned> > { |
| 48 | typedef std::pair<BasicBlock*, unsigned> EltTy; |
| 49 | static inline EltTy getEmptyKey() { |
| 50 | return EltTy(reinterpret_cast<BasicBlock*>(-1), ~0U); |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 52 | static inline EltTy getTombstoneKey() { |
| 53 | return EltTy(reinterpret_cast<BasicBlock*>(-2), 0U); |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 54 | } |
| 55 | static unsigned getHashValue(const std::pair<BasicBlock*, unsigned> &Val) { |
Chris Lattner | 76c1b97 | 2007-09-17 18:34:04 +0000 | [diff] [blame] | 56 | return DenseMapInfo<void*>::getHashValue(Val.first) + Val.second*2; |
| 57 | } |
| 58 | static bool isEqual(const EltTy &LHS, const EltTy &RHS) { |
| 59 | return LHS == RHS; |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 60 | } |
| 61 | static bool isPod() { return true; } |
| 62 | }; |
| 63 | } |
| 64 | |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 65 | /// isAllocaPromotable - Return true if this alloca is legal for promotion. |
Chris Lattner | a744b77 | 2004-09-19 18:51:51 +0000 | [diff] [blame] | 66 | /// This is true if there are only loads and stores to the alloca. |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 67 | /// |
Devang Patel | 41968df | 2007-04-25 17:15:20 +0000 | [diff] [blame] | 68 | bool llvm::isAllocaPromotable(const AllocaInst *AI) { |
Chris Lattner | fb743a9 | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 69 | // FIXME: If the memory unit is of pointer or integer type, we can permit |
| 70 | // assignments to subsections of the memory unit. |
| 71 | |
Anton Korobeynikov | 9f528e6 | 2007-08-26 21:43:30 +0000 | [diff] [blame] | 72 | // Only allow direct and non-volatile loads and stores... |
Chris Lattner | b9ddce6 | 2002-04-28 19:12:38 +0000 | [diff] [blame] | 73 | for (Value::use_const_iterator UI = AI->use_begin(), UE = AI->use_end(); |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 74 | UI != UE; ++UI) // Loop over all of the uses of the alloca |
Anton Korobeynikov | 9f528e6 | 2007-08-26 21:43:30 +0000 | [diff] [blame] | 75 | if (const LoadInst *LI = dyn_cast<LoadInst>(*UI)) { |
| 76 | if (LI->isVolatile()) |
| 77 | return false; |
Chris Lattner | 2d11f16 | 2004-01-12 01:18:32 +0000 | [diff] [blame] | 78 | } else if (const StoreInst *SI = dyn_cast<StoreInst>(*UI)) { |
| 79 | if (SI->getOperand(0) == AI) |
| 80 | return false; // Don't allow a store OF the AI, only INTO the AI. |
Anton Korobeynikov | 9f528e6 | 2007-08-26 21:43:30 +0000 | [diff] [blame] | 81 | if (SI->isVolatile()) |
| 82 | return false; |
Daniel Dunbar | 746867c | 2008-11-08 04:12:17 +0000 | [diff] [blame] | 83 | } else if (const BitCastInst *BC = dyn_cast<BitCastInst>(*UI)) { |
Dale Johannesen | 2511abf | 2009-03-06 00:42:50 +0000 | [diff] [blame] | 84 | // A bitcast that does not feed into debug info inhibits promotion. |
Daniel Dunbar | 746867c | 2008-11-08 04:12:17 +0000 | [diff] [blame] | 85 | if (!BC->hasOneUse() || !isa<DbgInfoIntrinsic>(*BC->use_begin())) |
| 86 | return false; |
Dale Johannesen | 2511abf | 2009-03-06 00:42:50 +0000 | [diff] [blame] | 87 | // If the only use is by debug info, this alloca will not exist in |
| 88 | // non-debug code, so don't try to promote; this ensures the same |
| 89 | // codegen with debug info. Otherwise, debug info should not |
| 90 | // inhibit promotion (but we must examine other uses). |
| 91 | if (AI->hasOneUse()) |
| 92 | return false; |
Chris Lattner | 2d11f16 | 2004-01-12 01:18:32 +0000 | [diff] [blame] | 93 | } else { |
Daniel Dunbar | 746867c | 2008-11-08 04:12:17 +0000 | [diff] [blame] | 94 | return false; |
Chris Lattner | 2d11f16 | 2004-01-12 01:18:32 +0000 | [diff] [blame] | 95 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 96 | |
Chris Lattner | b9ddce6 | 2002-04-28 19:12:38 +0000 | [diff] [blame] | 97 | return true; |
Chris Lattner | 0adb9f9 | 2002-04-28 18:54:01 +0000 | [diff] [blame] | 98 | } |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 99 | |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 100 | namespace { |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 101 | struct AllocaInfo; |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 102 | |
| 103 | // Data package used by RenamePass() |
| 104 | class VISIBILITY_HIDDEN RenamePassData { |
| 105 | public: |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 106 | typedef std::vector<Value *> ValVector; |
| 107 | |
Chris Lattner | 63cdcaa | 2007-08-04 01:07:49 +0000 | [diff] [blame] | 108 | RenamePassData() {} |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 109 | RenamePassData(BasicBlock *B, BasicBlock *P, |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 110 | const ValVector &V) : BB(B), Pred(P), Values(V) {} |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 111 | BasicBlock *BB; |
| 112 | BasicBlock *Pred; |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 113 | ValVector Values; |
Chris Lattner | 63cdcaa | 2007-08-04 01:07:49 +0000 | [diff] [blame] | 114 | |
| 115 | void swap(RenamePassData &RHS) { |
| 116 | std::swap(BB, RHS.BB); |
| 117 | std::swap(Pred, RHS.Pred); |
| 118 | Values.swap(RHS.Values); |
| 119 | } |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 120 | }; |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 121 | |
| 122 | /// LargeBlockInfo - This assigns and keeps a per-bb relative ordering of |
| 123 | /// load/store instructions in the block that directly load or store an alloca. |
| 124 | /// |
| 125 | /// This functionality is important because it avoids scanning large basic |
| 126 | /// blocks multiple times when promoting many allocas in the same block. |
| 127 | class VISIBILITY_HIDDEN LargeBlockInfo { |
| 128 | /// InstNumbers - For each instruction that we track, keep the index of the |
| 129 | /// instruction. The index starts out as the number of the instruction from |
| 130 | /// the start of the block. |
| 131 | DenseMap<const Instruction *, unsigned> InstNumbers; |
| 132 | public: |
| 133 | |
| 134 | /// isInterestingInstruction - This code only looks at accesses to allocas. |
| 135 | static bool isInterestingInstruction(const Instruction *I) { |
| 136 | return (isa<LoadInst>(I) && isa<AllocaInst>(I->getOperand(0))) || |
| 137 | (isa<StoreInst>(I) && isa<AllocaInst>(I->getOperand(1))); |
| 138 | } |
| 139 | |
| 140 | /// getInstructionIndex - Get or calculate the index of the specified |
| 141 | /// instruction. |
| 142 | unsigned getInstructionIndex(const Instruction *I) { |
| 143 | assert(isInterestingInstruction(I) && |
| 144 | "Not a load/store to/from an alloca?"); |
| 145 | |
| 146 | // If we already have this instruction number, return it. |
| 147 | DenseMap<const Instruction *, unsigned>::iterator It = InstNumbers.find(I); |
| 148 | if (It != InstNumbers.end()) return It->second; |
| 149 | |
| 150 | // Scan the whole block to get the instruction. This accumulates |
| 151 | // information for every interesting instruction in the block, in order to |
| 152 | // avoid gratuitus rescans. |
| 153 | const BasicBlock *BB = I->getParent(); |
| 154 | unsigned InstNo = 0; |
| 155 | for (BasicBlock::const_iterator BBI = BB->begin(), E = BB->end(); |
| 156 | BBI != E; ++BBI) |
| 157 | if (isInterestingInstruction(BBI)) |
| 158 | InstNumbers[BBI] = InstNo++; |
| 159 | It = InstNumbers.find(I); |
| 160 | |
| 161 | assert(It != InstNumbers.end() && "Didn't insert instruction?"); |
| 162 | return It->second; |
| 163 | } |
| 164 | |
| 165 | void deleteValue(const Instruction *I) { |
| 166 | InstNumbers.erase(I); |
| 167 | } |
| 168 | |
| 169 | void clear() { |
| 170 | InstNumbers.clear(); |
| 171 | } |
| 172 | }; |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 9525528 | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 174 | struct VISIBILITY_HIDDEN PromoteMem2Reg { |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 175 | /// Allocas - The alloca instructions being promoted. |
| 176 | /// |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 177 | std::vector<AllocaInst*> Allocas; |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 178 | DominatorTree &DT; |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 179 | DominanceFrontier &DF; |
| 180 | |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 181 | /// AST - An AliasSetTracker object to update. If null, don't update it. |
| 182 | /// |
| 183 | AliasSetTracker *AST; |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 184 | |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 185 | LLVMContext &Context; |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 186 | |
| 187 | /// AllocaLookup - Reverse mapping of Allocas. |
| 188 | /// |
Chris Lattner | 9e38fbf | 2003-10-05 03:16:07 +0000 | [diff] [blame] | 189 | std::map<AllocaInst*, unsigned> AllocaLookup; |
| 190 | |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 191 | /// NewPhiNodes - The PhiNodes we're adding. |
| 192 | /// |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 193 | DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*> NewPhiNodes; |
| 194 | |
| 195 | /// PhiToAllocaMap - For each PHI node, keep track of which entry in Allocas |
| 196 | /// it corresponds to. |
| 197 | DenseMap<PHINode*, unsigned> PhiToAllocaMap; |
| 198 | |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 199 | /// PointerAllocaValues - If we are updating an AliasSetTracker, then for |
| 200 | /// each alloca that is of pointer type, we keep track of what to copyValue |
| 201 | /// to the inserted PHI nodes here. |
| 202 | /// |
| 203 | std::vector<Value*> PointerAllocaValues; |
| 204 | |
| 205 | /// Visited - The set of basic blocks the renamer has already visited. |
| 206 | /// |
Chris Lattner | c670f3d | 2007-02-05 22:15:21 +0000 | [diff] [blame] | 207 | SmallPtrSet<BasicBlock*, 16> Visited; |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 208 | |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 209 | /// BBNumbers - Contains a stable numbering of basic blocks to avoid |
| 210 | /// non-determinstic behavior. |
Chris Lattner | d387404 | 2007-02-05 23:37:20 +0000 | [diff] [blame] | 211 | DenseMap<BasicBlock*, unsigned> BBNumbers; |
Chris Lattner | 63168d2 | 2004-06-19 07:40:14 +0000 | [diff] [blame] | 212 | |
Chris Lattner | e7b653d | 2007-08-04 20:24:50 +0000 | [diff] [blame] | 213 | /// BBNumPreds - Lazily compute the number of predecessors a block has. |
| 214 | DenseMap<const BasicBlock*, unsigned> BBNumPreds; |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 215 | public: |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 216 | PromoteMem2Reg(const std::vector<AllocaInst*> &A, DominatorTree &dt, |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 217 | DominanceFrontier &df, AliasSetTracker *ast, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 218 | LLVMContext &C) |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 219 | : Allocas(A), DT(dt), DF(df), AST(ast), Context(C) {} |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 220 | |
| 221 | void run(); |
| 222 | |
Chris Lattner | fed40df | 2005-11-18 07:29:44 +0000 | [diff] [blame] | 223 | /// properlyDominates - Return true if I1 properly dominates I2. |
Chris Lattner | 7e40f63 | 2004-10-17 21:25:56 +0000 | [diff] [blame] | 224 | /// |
Chris Lattner | fed40df | 2005-11-18 07:29:44 +0000 | [diff] [blame] | 225 | bool properlyDominates(Instruction *I1, Instruction *I2) const { |
Chris Lattner | 28e792c | 2004-10-18 01:21:17 +0000 | [diff] [blame] | 226 | if (InvokeInst *II = dyn_cast<InvokeInst>(I1)) |
| 227 | I1 = II->getNormalDest()->begin(); |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 228 | return DT.properlyDominates(I1->getParent(), I2->getParent()); |
Chris Lattner | fed40df | 2005-11-18 07:29:44 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 231 | /// dominates - Return true if BB1 dominates BB2 using the DominatorTree. |
Chris Lattner | fed40df | 2005-11-18 07:29:44 +0000 | [diff] [blame] | 232 | /// |
| 233 | bool dominates(BasicBlock *BB1, BasicBlock *BB2) const { |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 234 | return DT.dominates(BB1, BB2); |
Chris Lattner | 7e40f63 | 2004-10-17 21:25:56 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 237 | private: |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 238 | void RemoveFromAllocasList(unsigned &AllocaIdx) { |
| 239 | Allocas[AllocaIdx] = Allocas.back(); |
| 240 | Allocas.pop_back(); |
| 241 | --AllocaIdx; |
| 242 | } |
Chris Lattner | e7b653d | 2007-08-04 20:24:50 +0000 | [diff] [blame] | 243 | |
| 244 | unsigned getNumPreds(const BasicBlock *BB) { |
| 245 | unsigned &NP = BBNumPreds[BB]; |
| 246 | if (NP == 0) |
| 247 | NP = std::distance(pred_begin(BB), pred_end(BB))+1; |
| 248 | return NP-1; |
| 249 | } |
| 250 | |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 251 | void DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum, |
| 252 | AllocaInfo &Info); |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 253 | void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info, |
| 254 | const SmallPtrSet<BasicBlock*, 32> &DefBlocks, |
| 255 | SmallPtrSet<BasicBlock*, 32> &LiveInBlocks); |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 257 | void RewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info, |
| 258 | LargeBlockInfo &LBI); |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 259 | void PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info, |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 260 | LargeBlockInfo &LBI); |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 262 | |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 263 | void RenamePass(BasicBlock *BB, BasicBlock *Pred, |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 264 | RenamePassData::ValVector &IncVals, |
Chris Lattner | ac4aa4b | 2007-08-04 01:04:40 +0000 | [diff] [blame] | 265 | std::vector<RenamePassData> &Worklist); |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 266 | bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx, unsigned &Version, |
Chris Lattner | 6a1a28d | 2007-02-05 23:11:37 +0000 | [diff] [blame] | 267 | SmallPtrSet<PHINode*, 16> &InsertedPHINodes); |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 268 | }; |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 269 | |
| 270 | struct AllocaInfo { |
| 271 | std::vector<BasicBlock*> DefiningBlocks; |
| 272 | std::vector<BasicBlock*> UsingBlocks; |
| 273 | |
| 274 | StoreInst *OnlyStore; |
| 275 | BasicBlock *OnlyBlock; |
| 276 | bool OnlyUsedInOneBlock; |
| 277 | |
| 278 | Value *AllocaPointerVal; |
| 279 | |
| 280 | void clear() { |
| 281 | DefiningBlocks.clear(); |
| 282 | UsingBlocks.clear(); |
| 283 | OnlyStore = 0; |
| 284 | OnlyBlock = 0; |
| 285 | OnlyUsedInOneBlock = true; |
| 286 | AllocaPointerVal = 0; |
| 287 | } |
| 288 | |
| 289 | /// AnalyzeAlloca - Scan the uses of the specified alloca, filling in our |
| 290 | /// ivars. |
| 291 | void AnalyzeAlloca(AllocaInst *AI) { |
| 292 | clear(); |
Devang Patel | 180ffae | 2008-11-07 01:30:07 +0000 | [diff] [blame] | 293 | |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 294 | // As we scan the uses of the alloca instruction, keep track of stores, |
| 295 | // and decide whether all of the loads and stores to the alloca are within |
| 296 | // the same basic block. |
| 297 | for (Value::use_iterator U = AI->use_begin(), E = AI->use_end(); |
Devang Patel | b8c564f | 2008-11-17 18:37:53 +0000 | [diff] [blame] | 298 | U != E;) { |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 299 | Instruction *User = cast<Instruction>(*U); |
Devang Patel | b8c564f | 2008-11-17 18:37:53 +0000 | [diff] [blame] | 300 | ++U; |
| 301 | if (BitCastInst *BC = dyn_cast<BitCastInst>(User)) { |
| 302 | // Remove any uses of this alloca in DbgInfoInstrinsics. |
| 303 | assert(BC->hasOneUse() && "Unexpected alloca uses!"); |
| 304 | DbgInfoIntrinsic *DI = cast<DbgInfoIntrinsic>(*BC->use_begin()); |
| 305 | DI->eraseFromParent(); |
| 306 | BC->eraseFromParent(); |
| 307 | continue; |
| 308 | } |
| 309 | else if (StoreInst *SI = dyn_cast<StoreInst>(User)) { |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 310 | // Remember the basic blocks which define new values for the alloca |
| 311 | DefiningBlocks.push_back(SI->getParent()); |
| 312 | AllocaPointerVal = SI->getOperand(0); |
| 313 | OnlyStore = SI; |
| 314 | } else { |
| 315 | LoadInst *LI = cast<LoadInst>(User); |
Chris Lattner | e7b653d | 2007-08-04 20:24:50 +0000 | [diff] [blame] | 316 | // Otherwise it must be a load instruction, keep track of variable |
| 317 | // reads. |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 318 | UsingBlocks.push_back(LI->getParent()); |
| 319 | AllocaPointerVal = LI; |
| 320 | } |
| 321 | |
| 322 | if (OnlyUsedInOneBlock) { |
| 323 | if (OnlyBlock == 0) |
| 324 | OnlyBlock = User->getParent(); |
| 325 | else if (OnlyBlock != User->getParent()) |
| 326 | OnlyUsedInOneBlock = false; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | }; |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 331 | } // end of anonymous namespace |
Chris Lattner | d3db022 | 2002-02-12 17:16:22 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 333 | |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 334 | void PromoteMem2Reg::run() { |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 335 | Function &F = *DF.getRoot()->getParent(); |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 337 | if (AST) PointerAllocaValues.resize(Allocas.size()); |
Chris Lattner | 63168d2 | 2004-06-19 07:40:14 +0000 | [diff] [blame] | 338 | |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 339 | AllocaInfo Info; |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 340 | LargeBlockInfo LBI; |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 342 | for (unsigned AllocaNum = 0; AllocaNum != Allocas.size(); ++AllocaNum) { |
| 343 | AllocaInst *AI = Allocas[AllocaNum]; |
Chris Lattner | 9e38fbf | 2003-10-05 03:16:07 +0000 | [diff] [blame] | 344 | |
Devang Patel | 41968df | 2007-04-25 17:15:20 +0000 | [diff] [blame] | 345 | assert(isAllocaPromotable(AI) && |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 346 | "Cannot promote non-promotable alloca!"); |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 347 | assert(AI->getParent()->getParent() == &F && |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 348 | "All allocas should be in the same function, which is same as DF!"); |
Chris Lattner | 9157f04 | 2003-10-05 02:37:36 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 350 | if (AI->use_empty()) { |
| 351 | // If there are no uses of the alloca, just delete it now. |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 352 | if (AST) AST->deleteValue(AI); |
Chris Lattner | 634c76c | 2006-04-27 01:14:43 +0000 | [diff] [blame] | 353 | AI->eraseFromParent(); |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 354 | |
| 355 | // Remove the alloca from the Allocas list, since it has been processed |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 356 | RemoveFromAllocasList(AllocaNum); |
| 357 | ++NumDeadAlloca; |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 358 | continue; |
| 359 | } |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 361 | // Calculate the set of read and write-locations for each alloca. This is |
Chris Lattner | 2d11f16 | 2004-01-12 01:18:32 +0000 | [diff] [blame] | 362 | // analogous to finding the 'uses' and 'definitions' of each variable. |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 363 | Info.AnalyzeAlloca(AI); |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 36ba500 | 2005-11-18 07:31:42 +0000 | [diff] [blame] | 365 | // If there is only a single store to this value, replace any loads of |
| 366 | // it that are directly dominated by the definition with the value stored. |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 367 | if (Info.DefiningBlocks.size() == 1) { |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 368 | RewriteSingleStoreAlloca(AI, Info, LBI); |
Chris Lattner | 36ba500 | 2005-11-18 07:31:42 +0000 | [diff] [blame] | 369 | |
| 370 | // Finally, after the scan, check to see if the store is all that is left. |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 371 | if (Info.UsingBlocks.empty()) { |
Chris Lattner | 4f63e76 | 2007-08-04 02:38:38 +0000 | [diff] [blame] | 372 | // Remove the (now dead) store and alloca. |
| 373 | Info.OnlyStore->eraseFromParent(); |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 374 | LBI.deleteValue(Info.OnlyStore); |
| 375 | |
Chris Lattner | 4f63e76 | 2007-08-04 02:38:38 +0000 | [diff] [blame] | 376 | if (AST) AST->deleteValue(AI); |
| 377 | AI->eraseFromParent(); |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 378 | LBI.deleteValue(AI); |
Chris Lattner | 4f63e76 | 2007-08-04 02:38:38 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 36ba500 | 2005-11-18 07:31:42 +0000 | [diff] [blame] | 380 | // The alloca has been processed, move on. |
Chris Lattner | bbe1040 | 2007-08-04 01:41:18 +0000 | [diff] [blame] | 381 | RemoveFromAllocasList(AllocaNum); |
Chris Lattner | 4f63e76 | 2007-08-04 02:38:38 +0000 | [diff] [blame] | 382 | |
| 383 | ++NumSingleStore; |
Chris Lattner | 36ba500 | 2005-11-18 07:31:42 +0000 | [diff] [blame] | 384 | continue; |
| 385 | } |
| 386 | } |
| 387 | |
Chris Lattner | fb312c7 | 2007-08-04 20:03:23 +0000 | [diff] [blame] | 388 | // If the alloca is only read and written in one basic block, just perform a |
| 389 | // linear sweep over the block to eliminate it. |
| 390 | if (Info.OnlyUsedInOneBlock) { |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 391 | PromoteSingleBlockAlloca(AI, Info, LBI); |
Chris Lattner | fb312c7 | 2007-08-04 20:03:23 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 393 | // Finally, after the scan, check to see if the stores are all that is |
| 394 | // left. |
| 395 | if (Info.UsingBlocks.empty()) { |
| 396 | |
| 397 | // Remove the (now dead) stores and alloca. |
| 398 | while (!AI->use_empty()) { |
| 399 | StoreInst *SI = cast<StoreInst>(AI->use_back()); |
| 400 | SI->eraseFromParent(); |
| 401 | LBI.deleteValue(SI); |
| 402 | } |
| 403 | |
| 404 | if (AST) AST->deleteValue(AI); |
| 405 | AI->eraseFromParent(); |
| 406 | LBI.deleteValue(AI); |
| 407 | |
| 408 | // The alloca has been processed, move on. |
| 409 | RemoveFromAllocasList(AllocaNum); |
| 410 | |
| 411 | ++NumLocalPromoted; |
| 412 | continue; |
| 413 | } |
Chris Lattner | fb312c7 | 2007-08-04 20:03:23 +0000 | [diff] [blame] | 414 | } |
Chris Lattner | fed40df | 2005-11-18 07:29:44 +0000 | [diff] [blame] | 415 | |
Chris Lattner | 63168d2 | 2004-06-19 07:40:14 +0000 | [diff] [blame] | 416 | // If we haven't computed a numbering for the BB's in the function, do so |
| 417 | // now. |
Chris Lattner | d387404 | 2007-02-05 23:37:20 +0000 | [diff] [blame] | 418 | if (BBNumbers.empty()) { |
| 419 | unsigned ID = 0; |
| 420 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
| 421 | BBNumbers[I] = ID++; |
| 422 | } |
Chris Lattner | 63168d2 | 2004-06-19 07:40:14 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 424 | // If we have an AST to keep updated, remember some pointer value that is |
| 425 | // stored into the alloca. |
| 426 | if (AST) |
| 427 | PointerAllocaValues[AllocaNum] = Info.AllocaPointerVal; |
| 428 | |
| 429 | // Keep the reverse mapping of the 'Allocas' array for the rename pass. |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 430 | AllocaLookup[Allocas[AllocaNum]] = AllocaNum; |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 431 | |
| 432 | // At this point, we're committed to promoting the alloca using IDF's, and |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 433 | // the standard SSA construction algorithm. Determine which blocks need PHI |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 434 | // nodes and see if we can optimize out some work by avoiding insertion of |
| 435 | // dead phi nodes. |
| 436 | DetermineInsertionPoint(AI, AllocaNum, Info); |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 437 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 439 | if (Allocas.empty()) |
| 440 | return; // All of the allocas must have been trivial! |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 442 | LBI.clear(); |
| 443 | |
| 444 | |
Chris Lattner | 5b5df17 | 2002-04-28 18:39:46 +0000 | [diff] [blame] | 445 | // Set the incoming values for the basic block to be null values for all of |
| 446 | // the alloca's. We do this in case there is a load of a value that has not |
| 447 | // been stored yet. In this case, it will get this null value. |
| 448 | // |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 449 | RenamePassData::ValVector Values(Allocas.size()); |
Chris Lattner | 5b5df17 | 2002-04-28 18:39:46 +0000 | [diff] [blame] | 450 | for (unsigned i = 0, e = Allocas.size(); i != e; ++i) |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 451 | Values[i] = Context.getUndef(Allocas[i]->getAllocatedType()); |
Chris Lattner | 5b5df17 | 2002-04-28 18:39:46 +0000 | [diff] [blame] | 452 | |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 453 | // Walks all basic blocks in the function performing the SSA rename algorithm |
| 454 | // and inserting the phi nodes we marked as necessary |
| 455 | // |
Chris Lattner | ac4aa4b | 2007-08-04 01:04:40 +0000 | [diff] [blame] | 456 | std::vector<RenamePassData> RenamePassWorkList; |
Devang Patel | d64d3a1 | 2007-03-26 23:19:29 +0000 | [diff] [blame] | 457 | RenamePassWorkList.push_back(RenamePassData(F.begin(), 0, Values)); |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 458 | while (!RenamePassWorkList.empty()) { |
Chris Lattner | 63cdcaa | 2007-08-04 01:07:49 +0000 | [diff] [blame] | 459 | RenamePassData RPD; |
| 460 | RPD.swap(RenamePassWorkList.back()); |
Devang Patel | d64d3a1 | 2007-03-26 23:19:29 +0000 | [diff] [blame] | 461 | RenamePassWorkList.pop_back(); |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 462 | // RenamePass may add new worklist entries. |
Chris Lattner | ac4aa4b | 2007-08-04 01:04:40 +0000 | [diff] [blame] | 463 | RenamePass(RPD.BB, RPD.Pred, RPD.Values, RenamePassWorkList); |
Devang Patel | a5b7dc5 | 2007-03-09 23:39:14 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 466 | // The renamer uses the Visited set to avoid infinite loops. Clear it now. |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 467 | Visited.clear(); |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 634c76c | 2006-04-27 01:14:43 +0000 | [diff] [blame] | 469 | // Remove the allocas themselves from the function. |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 470 | for (unsigned i = 0, e = Allocas.size(); i != e; ++i) { |
| 471 | Instruction *A = Allocas[i]; |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 472 | |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 473 | // If there are any uses of the alloca instructions left, they must be in |
Chris Lattner | d4bd3eb | 2003-04-10 19:41:13 +0000 | [diff] [blame] | 474 | // sections of dead code that were not processed on the dominance frontier. |
| 475 | // Just delete the users now. |
| 476 | // |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 477 | if (!A->use_empty()) |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 478 | A->replaceAllUsesWith(Context.getUndef(A->getType())); |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 479 | if (AST) AST->deleteValue(A); |
Chris Lattner | 634c76c | 2006-04-27 01:14:43 +0000 | [diff] [blame] | 480 | A->eraseFromParent(); |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 481 | } |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 634c76c | 2006-04-27 01:14:43 +0000 | [diff] [blame] | 483 | |
| 484 | // Loop over all of the PHI nodes and see if there are any that we can get |
| 485 | // rid of because they merge all of the same incoming values. This can |
| 486 | // happen due to undef values coming into the PHI nodes. This process is |
| 487 | // iterative, because eliminating one PHI node can cause others to be removed. |
| 488 | bool EliminatedAPHI = true; |
| 489 | while (EliminatedAPHI) { |
| 490 | EliminatedAPHI = false; |
| 491 | |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 492 | for (DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*>::iterator I = |
| 493 | NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E;) { |
| 494 | PHINode *PN = I->second; |
| 495 | |
| 496 | // If this PHI node merges one value and/or undefs, get the value. |
| 497 | if (Value *V = PN->hasConstantValue(true)) { |
| 498 | if (!isa<Instruction>(V) || |
| 499 | properlyDominates(cast<Instruction>(V), PN)) { |
| 500 | if (AST && isa<PointerType>(PN->getType())) |
| 501 | AST->deleteValue(PN); |
| 502 | PN->replaceAllUsesWith(V); |
| 503 | PN->eraseFromParent(); |
| 504 | NewPhiNodes.erase(I++); |
| 505 | EliminatedAPHI = true; |
| 506 | continue; |
Chris Lattner | 634c76c | 2006-04-27 01:14:43 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 509 | ++I; |
Chris Lattner | 634c76c | 2006-04-27 01:14:43 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 513 | // At this point, the renamer has added entries to PHI nodes for all reachable |
Chris Lattner | c837615 | 2007-02-05 22:13:11 +0000 | [diff] [blame] | 514 | // code. Unfortunately, there may be unreachable blocks which the renamer |
| 515 | // hasn't traversed. If this is the case, the PHI nodes may not |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 516 | // have incoming values for all predecessors. Loop over all PHI nodes we have |
Chris Lattner | 7e40f63 | 2004-10-17 21:25:56 +0000 | [diff] [blame] | 517 | // created, inserting undef values if they are missing any incoming values. |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 518 | // |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 519 | for (DenseMap<std::pair<BasicBlock*, unsigned>, PHINode*>::iterator I = |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 520 | NewPhiNodes.begin(), E = NewPhiNodes.end(); I != E; ++I) { |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 521 | // We want to do this once per basic block. As such, only process a block |
| 522 | // when we find the PHI that is the first entry in the block. |
| 523 | PHINode *SomePHI = I->second; |
| 524 | BasicBlock *BB = SomePHI->getParent(); |
| 525 | if (&BB->front() != SomePHI) |
| 526 | continue; |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 527 | |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 528 | // Only do work here if there the PHI nodes are missing incoming values. We |
| 529 | // know that all PHI nodes that were inserted in a block will have the same |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 530 | // number of incoming values, so we can just check any of them. |
Chris Lattner | 127ed3c | 2007-08-04 21:06:15 +0000 | [diff] [blame] | 531 | if (SomePHI->getNumIncomingValues() == getNumPreds(BB)) |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 532 | continue; |
Chris Lattner | 127ed3c | 2007-08-04 21:06:15 +0000 | [diff] [blame] | 533 | |
| 534 | // Get the preds for BB. |
| 535 | SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB)); |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 536 | |
| 537 | // Ok, now we know that all of the PHI nodes are missing entries for some |
| 538 | // basic blocks. Start by sorting the incoming predecessors for efficient |
| 539 | // access. |
| 540 | std::sort(Preds.begin(), Preds.end()); |
| 541 | |
| 542 | // Now we loop through all BB's which have entries in SomePHI and remove |
| 543 | // them from the Preds list. |
| 544 | for (unsigned i = 0, e = SomePHI->getNumIncomingValues(); i != e; ++i) { |
| 545 | // Do a log(n) search of the Preds list for the entry we want. |
| 546 | SmallVector<BasicBlock*, 16>::iterator EntIt = |
| 547 | std::lower_bound(Preds.begin(), Preds.end(), |
| 548 | SomePHI->getIncomingBlock(i)); |
| 549 | assert(EntIt != Preds.end() && *EntIt == SomePHI->getIncomingBlock(i)&& |
| 550 | "PHI node has entry for a block which is not a predecessor!"); |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 551 | |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 552 | // Remove the entry |
| 553 | Preds.erase(EntIt); |
| 554 | } |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 555 | |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 556 | // At this point, the blocks left in the preds list must have dummy |
| 557 | // entries inserted into every PHI nodes for the block. Update all the phi |
| 558 | // nodes in this block that we are inserting (there could be phis before |
| 559 | // mem2reg runs). |
| 560 | unsigned NumBadPreds = SomePHI->getNumIncomingValues(); |
| 561 | BasicBlock::iterator BBI = BB->begin(); |
| 562 | while ((SomePHI = dyn_cast<PHINode>(BBI++)) && |
| 563 | SomePHI->getNumIncomingValues() == NumBadPreds) { |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 564 | Value *UndefVal = Context.getUndef(SomePHI->getType()); |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 565 | for (unsigned pred = 0, e = Preds.size(); pred != e; ++pred) |
| 566 | SomePHI->addIncoming(UndefVal, Preds[pred]); |
Chris Lattner | afa060e | 2003-10-05 04:26:39 +0000 | [diff] [blame] | 567 | } |
| 568 | } |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 569 | |
| 570 | NewPhiNodes.clear(); |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 573 | |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 574 | /// ComputeLiveInBlocks - Determine which blocks the value is live in. These |
| 575 | /// are blocks which lead to uses. Knowing this allows us to avoid inserting |
| 576 | /// PHI nodes into blocks which don't lead to uses (thus, the inserted phi nodes |
| 577 | /// would be dead). |
| 578 | void PromoteMem2Reg:: |
| 579 | ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info, |
| 580 | const SmallPtrSet<BasicBlock*, 32> &DefBlocks, |
| 581 | SmallPtrSet<BasicBlock*, 32> &LiveInBlocks) { |
| 582 | |
| 583 | // To determine liveness, we must iterate through the predecessors of blocks |
| 584 | // where the def is live. Blocks are added to the worklist if we need to |
| 585 | // check their predecessors. Start with all the using blocks. |
| 586 | SmallVector<BasicBlock*, 64> LiveInBlockWorklist; |
| 587 | LiveInBlockWorklist.insert(LiveInBlockWorklist.end(), |
| 588 | Info.UsingBlocks.begin(), Info.UsingBlocks.end()); |
| 589 | |
| 590 | // If any of the using blocks is also a definition block, check to see if the |
| 591 | // definition occurs before or after the use. If it happens before the use, |
| 592 | // the value isn't really live-in. |
| 593 | for (unsigned i = 0, e = LiveInBlockWorklist.size(); i != e; ++i) { |
| 594 | BasicBlock *BB = LiveInBlockWorklist[i]; |
| 595 | if (!DefBlocks.count(BB)) continue; |
| 596 | |
| 597 | // Okay, this is a block that both uses and defines the value. If the first |
| 598 | // reference to the alloca is a def (store), then we know it isn't live-in. |
| 599 | for (BasicBlock::iterator I = BB->begin(); ; ++I) { |
| 600 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 601 | if (SI->getOperand(1) != AI) continue; |
| 602 | |
| 603 | // We found a store to the alloca before a load. The alloca is not |
| 604 | // actually live-in here. |
| 605 | LiveInBlockWorklist[i] = LiveInBlockWorklist.back(); |
| 606 | LiveInBlockWorklist.pop_back(); |
| 607 | --i, --e; |
| 608 | break; |
| 609 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 610 | if (LI->getOperand(0) != AI) continue; |
| 611 | |
| 612 | // Okay, we found a load before a store to the alloca. It is actually |
| 613 | // live into this block. |
| 614 | break; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // Now that we have a set of blocks where the phi is live-in, recursively add |
| 620 | // their predecessors until we find the full region the value is live. |
| 621 | while (!LiveInBlockWorklist.empty()) { |
Dan Gohman | e9d87f4 | 2009-05-06 17:22:41 +0000 | [diff] [blame] | 622 | BasicBlock *BB = LiveInBlockWorklist.pop_back_val(); |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 623 | |
| 624 | // The block really is live in here, insert it into the set. If already in |
| 625 | // the set, then it has already been processed. |
| 626 | if (!LiveInBlocks.insert(BB)) |
| 627 | continue; |
| 628 | |
| 629 | // Since the value is live into BB, it is either defined in a predecessor or |
| 630 | // live into it to. Add the preds to the worklist unless they are a |
| 631 | // defining block. |
| 632 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 633 | BasicBlock *P = *PI; |
| 634 | |
| 635 | // The value is not live into a predecessor if it defines the value. |
| 636 | if (DefBlocks.count(P)) |
| 637 | continue; |
| 638 | |
| 639 | // Otherwise it is, add to the worklist. |
| 640 | LiveInBlockWorklist.push_back(P); |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 645 | /// DetermineInsertionPoint - At this point, we're committed to promoting the |
| 646 | /// alloca using IDF's, and the standard SSA construction algorithm. Determine |
| 647 | /// which blocks need phi nodes and see if we can optimize out some work by |
| 648 | /// avoiding insertion of dead phi nodes. |
| 649 | void PromoteMem2Reg::DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum, |
| 650 | AllocaInfo &Info) { |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 651 | |
| 652 | // Unique the set of defining blocks for efficient lookup. |
| 653 | SmallPtrSet<BasicBlock*, 32> DefBlocks; |
| 654 | DefBlocks.insert(Info.DefiningBlocks.begin(), Info.DefiningBlocks.end()); |
| 655 | |
| 656 | // Determine which blocks the value is live in. These are blocks which lead |
| 657 | // to uses. |
| 658 | SmallPtrSet<BasicBlock*, 32> LiveInBlocks; |
| 659 | ComputeLiveInBlocks(AI, Info, DefBlocks, LiveInBlocks); |
| 660 | |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 661 | // Compute the locations where PhiNodes need to be inserted. Look at the |
| 662 | // dominance frontier of EACH basic-block we have a write in. |
| 663 | unsigned CurrentVersion = 0; |
| 664 | SmallPtrSet<PHINode*, 16> InsertedPHINodes; |
| 665 | std::vector<std::pair<unsigned, BasicBlock*> > DFBlocks; |
| 666 | while (!Info.DefiningBlocks.empty()) { |
| 667 | BasicBlock *BB = Info.DefiningBlocks.back(); |
| 668 | Info.DefiningBlocks.pop_back(); |
| 669 | |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 670 | // Look up the DF for this write, add it to defining blocks. |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 671 | DominanceFrontier::const_iterator it = DF.find(BB); |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 672 | if (it == DF.end()) continue; |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 673 | |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 674 | const DominanceFrontier::DomSetType &S = it->second; |
| 675 | |
| 676 | // In theory we don't need the indirection through the DFBlocks vector. |
| 677 | // In practice, the order of calling QueuePhiNode would depend on the |
| 678 | // (unspecified) ordering of basic blocks in the dominance frontier, |
| 679 | // which would give PHI nodes non-determinstic subscripts. Fix this by |
| 680 | // processing blocks in order of the occurance in the function. |
| 681 | for (DominanceFrontier::DomSetType::const_iterator P = S.begin(), |
| 682 | PE = S.end(); P != PE; ++P) { |
| 683 | // If the frontier block is not in the live-in set for the alloca, don't |
| 684 | // bother processing it. |
| 685 | if (!LiveInBlocks.count(*P)) |
| 686 | continue; |
| 687 | |
| 688 | DFBlocks.push_back(std::make_pair(BBNumbers[*P], *P)); |
| 689 | } |
| 690 | |
| 691 | // Sort by which the block ordering in the function. |
| 692 | if (DFBlocks.size() > 1) |
| 693 | std::sort(DFBlocks.begin(), DFBlocks.end()); |
| 694 | |
| 695 | for (unsigned i = 0, e = DFBlocks.size(); i != e; ++i) { |
| 696 | BasicBlock *BB = DFBlocks[i].second; |
| 697 | if (QueuePhiNode(BB, AllocaNum, CurrentVersion, InsertedPHINodes)) |
| 698 | Info.DefiningBlocks.push_back(BB); |
| 699 | } |
| 700 | DFBlocks.clear(); |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
Chris Lattner | 0ec8df3 | 2007-08-04 21:14:29 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 704 | /// RewriteSingleStoreAlloca - If there is only a single store to this value, |
| 705 | /// replace any loads of it that are directly dominated by the definition with |
| 706 | /// the value stored. |
| 707 | void PromoteMem2Reg::RewriteSingleStoreAlloca(AllocaInst *AI, |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 708 | AllocaInfo &Info, |
| 709 | LargeBlockInfo &LBI) { |
Chris Lattner | b776a33 | 2007-08-04 02:15:24 +0000 | [diff] [blame] | 710 | StoreInst *OnlyStore = Info.OnlyStore; |
Chris Lattner | d0458e5 | 2007-08-04 02:32:22 +0000 | [diff] [blame] | 711 | bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0)); |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 712 | BasicBlock *StoreBB = OnlyStore->getParent(); |
| 713 | int StoreIndex = -1; |
| 714 | |
| 715 | // Clear out UsingBlocks. We will reconstruct it here if needed. |
| 716 | Info.UsingBlocks.clear(); |
Chris Lattner | b776a33 | 2007-08-04 02:15:24 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 718 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E; ) { |
| 719 | Instruction *UserInst = cast<Instruction>(*UI++); |
| 720 | if (!isa<LoadInst>(UserInst)) { |
| 721 | assert(UserInst == OnlyStore && "Should only have load/stores"); |
Chris Lattner | d0458e5 | 2007-08-04 02:32:22 +0000 | [diff] [blame] | 722 | continue; |
| 723 | } |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 724 | LoadInst *LI = cast<LoadInst>(UserInst); |
Chris Lattner | d0458e5 | 2007-08-04 02:32:22 +0000 | [diff] [blame] | 725 | |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 726 | // Okay, if we have a load from the alloca, we want to replace it with the |
| 727 | // only value stored to the alloca. We can do this if the value is |
| 728 | // dominated by the store. If not, we use the rest of the mem2reg machinery |
| 729 | // to insert the phi nodes as needed. |
| 730 | if (!StoringGlobalVal) { // Non-instructions are always dominated. |
| 731 | if (LI->getParent() == StoreBB) { |
| 732 | // If we have a use that is in the same block as the store, compare the |
| 733 | // indices of the two instructions to see which one came first. If the |
| 734 | // load came before the store, we can't handle it. |
| 735 | if (StoreIndex == -1) |
| 736 | StoreIndex = LBI.getInstructionIndex(OnlyStore); |
| 737 | |
| 738 | if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) { |
| 739 | // Can't handle this load, bail out. |
| 740 | Info.UsingBlocks.push_back(StoreBB); |
| 741 | continue; |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 742 | } |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 743 | |
| 744 | } else if (LI->getParent() != StoreBB && |
| 745 | !dominates(StoreBB, LI->getParent())) { |
| 746 | // If the load and store are in different blocks, use BB dominance to |
| 747 | // check their relationships. If the store doesn't dom the use, bail |
| 748 | // out. |
| 749 | Info.UsingBlocks.push_back(LI->getParent()); |
| 750 | continue; |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 754 | // Otherwise, we *can* safely rewrite this load. |
| 755 | LI->replaceAllUsesWith(OnlyStore->getOperand(0)); |
| 756 | if (AST && isa<PointerType>(LI->getType())) |
| 757 | AST->deleteValue(LI); |
| 758 | LI->eraseFromParent(); |
| 759 | LBI.deleteValue(LI); |
Chris Lattner | 5dd75b4 | 2007-08-04 01:47:41 +0000 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 764 | /// StoreIndexSearchPredicate - This is a helper predicate used to search by the |
| 765 | /// first element of a pair. |
| 766 | struct StoreIndexSearchPredicate { |
| 767 | bool operator()(const std::pair<unsigned, StoreInst*> &LHS, |
| 768 | const std::pair<unsigned, StoreInst*> &RHS) { |
| 769 | return LHS.first < RHS.first; |
| 770 | } |
| 771 | }; |
| 772 | |
| 773 | /// PromoteSingleBlockAlloca - Many allocas are only used within a single basic |
Chris Lattner | e47f78e | 2004-02-03 22:34:12 +0000 | [diff] [blame] | 774 | /// block. If this is the case, avoid traversing the CFG and inserting a lot of |
| 775 | /// potentially useless PHI nodes by just performing a single linear pass over |
| 776 | /// the basic block using the Alloca. |
| 777 | /// |
Chris Lattner | 6cfd1eb | 2005-06-30 07:29:44 +0000 | [diff] [blame] | 778 | /// If we cannot promote this alloca (because it is read before it is written), |
| 779 | /// return true. This is necessary in cases where, due to control flow, the |
| 780 | /// alloca is potentially undefined on some control flow paths. e.g. code like |
| 781 | /// this is potentially correct: |
| 782 | /// |
| 783 | /// for (...) { if (c) { A = undef; undef = B; } } |
| 784 | /// |
| 785 | /// ... so long as A is not used before undef is set. |
| 786 | /// |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 787 | void PromoteMem2Reg::PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info, |
Chris Lattner | 3321060 | 2008-10-27 06:05:26 +0000 | [diff] [blame] | 788 | LargeBlockInfo &LBI) { |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 789 | // The trickiest case to handle is when we have large blocks. Because of this, |
| 790 | // this code is optimized assuming that large blocks happen. This does not |
| 791 | // significantly pessimize the small block case. This uses LargeBlockInfo to |
| 792 | // make it efficient to get the index of various operations in the block. |
| 793 | |
| 794 | // Clear out UsingBlocks. We will reconstruct it here if needed. |
| 795 | Info.UsingBlocks.clear(); |
| 796 | |
| 797 | // Walk the use-def list of the alloca, getting the locations of all stores. |
| 798 | typedef SmallVector<std::pair<unsigned, StoreInst*>, 64> StoresByIndexTy; |
| 799 | StoresByIndexTy StoresByIndex; |
| 800 | |
| 801 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 802 | UI != E; ++UI) |
| 803 | if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) |
| 804 | StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI)); |
Chris Lattner | 7fecc2e | 2004-02-03 22:00:33 +0000 | [diff] [blame] | 805 | |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 806 | // If there are no stores to the alloca, just replace any loads with undef. |
| 807 | if (StoresByIndex.empty()) { |
| 808 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) |
| 809 | if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) { |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 810 | LI->replaceAllUsesWith(Context.getUndef(LI->getType())); |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 811 | if (AST && isa<PointerType>(LI->getType())) |
| 812 | AST->deleteValue(LI); |
| 813 | LBI.deleteValue(LI); |
| 814 | LI->eraseFromParent(); |
Chris Lattner | 24011be | 2003-10-05 20:54:03 +0000 | [diff] [blame] | 815 | } |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 816 | return; |
Chris Lattner | e47f78e | 2004-02-03 22:34:12 +0000 | [diff] [blame] | 817 | } |
Chris Lattner | 7a5745b | 2007-08-04 20:01:43 +0000 | [diff] [blame] | 818 | |
Chris Lattner | 0fd77a5 | 2008-10-27 07:05:53 +0000 | [diff] [blame] | 819 | // Sort the stores by their index, making it efficient to do a lookup with a |
| 820 | // binary search. |
| 821 | std::sort(StoresByIndex.begin(), StoresByIndex.end()); |
| 822 | |
| 823 | // Walk all of the loads from this alloca, replacing them with the nearest |
| 824 | // store above them, if any. |
| 825 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) { |
| 826 | LoadInst *LI = dyn_cast<LoadInst>(*UI++); |
| 827 | if (!LI) continue; |
| 828 | |
| 829 | unsigned LoadIdx = LBI.getInstructionIndex(LI); |
| 830 | |
| 831 | // Find the nearest store that has a lower than this load. |
| 832 | StoresByIndexTy::iterator I = |
| 833 | std::lower_bound(StoresByIndex.begin(), StoresByIndex.end(), |
| 834 | std::pair<unsigned, StoreInst*>(LoadIdx, 0), |
| 835 | StoreIndexSearchPredicate()); |
| 836 | |
| 837 | // If there is no store before this load, then we can't promote this load. |
| 838 | if (I == StoresByIndex.begin()) { |
| 839 | // Can't handle this load, bail out. |
| 840 | Info.UsingBlocks.push_back(LI->getParent()); |
| 841 | continue; |
| 842 | } |
| 843 | |
| 844 | // Otherwise, there was a store before this load, the load takes its value. |
| 845 | --I; |
| 846 | LI->replaceAllUsesWith(I->second->getOperand(0)); |
| 847 | if (AST && isa<PointerType>(LI->getType())) |
| 848 | AST->deleteValue(LI); |
| 849 | LI->eraseFromParent(); |
| 850 | LBI.deleteValue(LI); |
Chris Lattner | 7a5745b | 2007-08-04 20:01:43 +0000 | [diff] [blame] | 851 | } |
Chris Lattner | e47f78e | 2004-02-03 22:34:12 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 855 | // QueuePhiNode - queues a phi-node to be added to a basic-block for a specific |
| 856 | // Alloca returns true if there wasn't already a phi-node for that variable |
| 857 | // |
Chris Lattner | 3c881cb | 2003-10-05 04:33:22 +0000 | [diff] [blame] | 858 | bool PromoteMem2Reg::QueuePhiNode(BasicBlock *BB, unsigned AllocaNo, |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 859 | unsigned &Version, |
Chris Lattner | 6a1a28d | 2007-02-05 23:11:37 +0000 | [diff] [blame] | 860 | SmallPtrSet<PHINode*, 16> &InsertedPHINodes) { |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 861 | // Look up the basic-block in question. |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 862 | PHINode *&PN = NewPhiNodes[std::make_pair(BB, AllocaNo)]; |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 863 | |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 864 | // If the BB already has a phi node added for the i'th alloca then we're done! |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 865 | if (PN) return false; |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 866 | |
Chris Lattner | 1d608ab | 2002-09-10 22:38:47 +0000 | [diff] [blame] | 867 | // Create a PhiNode using the dereferenced type... and add the phi-node to the |
Chris Lattner | 393689a | 2003-04-18 19:25:22 +0000 | [diff] [blame] | 868 | // BasicBlock. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 869 | PN = PHINode::Create(Allocas[AllocaNo]->getAllocatedType(), |
Daniel Dunbar | 7f93dc8 | 2009-07-30 04:20:37 +0000 | [diff] [blame^] | 870 | Allocas[AllocaNo]->getName() + "." + Version++, |
| 871 | BB->begin()); |
Chris Lattner | f12f8de | 2007-08-04 22:50:14 +0000 | [diff] [blame] | 872 | ++NumPHIInsert; |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 873 | PhiToAllocaMap[PN] = AllocaNo; |
Chris Lattner | e7b653d | 2007-08-04 20:24:50 +0000 | [diff] [blame] | 874 | PN->reserveOperandSpace(getNumPreds(BB)); |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 875 | |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 876 | InsertedPHINodes.insert(PN); |
| 877 | |
| 878 | if (AST && isa<PointerType>(PN->getType())) |
| 879 | AST->copyValue(PointerAllocaValues[AllocaNo], PN); |
| 880 | |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 881 | return true; |
Cameron Buschardt | 98a37c2 | 2002-03-27 23:17:37 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Chris Lattner | 69091be | 2003-10-05 22:19:20 +0000 | [diff] [blame] | 884 | // RenamePass - Recursively traverse the CFG of the function, renaming loads and |
| 885 | // stores to the allocas which we are promoting. IncomingVals indicates what |
| 886 | // value each Alloca contains on exit from the predecessor block Pred. |
| 887 | // |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 888 | void PromoteMem2Reg::RenamePass(BasicBlock *BB, BasicBlock *Pred, |
Chris Lattner | 483ce14 | 2007-08-04 01:19:38 +0000 | [diff] [blame] | 889 | RenamePassData::ValVector &IncomingVals, |
Chris Lattner | ac4aa4b | 2007-08-04 01:04:40 +0000 | [diff] [blame] | 890 | std::vector<RenamePassData> &Worklist) { |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 891 | NextIteration: |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 892 | // If we are inserting any phi nodes into this BB, they will already be in the |
| 893 | // block. |
| 894 | if (PHINode *APN = dyn_cast<PHINode>(BB->begin())) { |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 895 | // If we have PHI nodes to update, compute the number of edges from Pred to |
| 896 | // BB. |
Eli Friedman | bde6fda | 2009-04-16 21:40:28 +0000 | [diff] [blame] | 897 | if (PhiToAllocaMap.count(APN)) { |
Chris Lattner | 2663ffe | 2008-02-05 21:26:23 +0000 | [diff] [blame] | 898 | // We want to be able to distinguish between PHI nodes being inserted by |
| 899 | // this invocation of mem2reg from those phi nodes that already existed in |
| 900 | // the IR before mem2reg was run. We determine that APN is being inserted |
| 901 | // because it is missing incoming edges. All other PHI nodes being |
| 902 | // inserted by this pass of mem2reg will have the same number of incoming |
| 903 | // operands so far. Remember this count. |
| 904 | unsigned NewPHINumOperands = APN->getNumOperands(); |
| 905 | |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 906 | unsigned NumEdges = 0; |
Nick Lewycky | 6e7aeb1 | 2008-03-13 02:42:41 +0000 | [diff] [blame] | 907 | for (succ_iterator I = succ_begin(Pred), E = succ_end(Pred); I != E; ++I) |
| 908 | if (*I == BB) |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 909 | ++NumEdges; |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 910 | assert(NumEdges && "Must be at least one edge from Pred to BB!"); |
| 911 | |
| 912 | // Add entries for all the phis. |
| 913 | BasicBlock::iterator PNI = BB->begin(); |
| 914 | do { |
| 915 | unsigned AllocaNo = PhiToAllocaMap[APN]; |
| 916 | |
| 917 | // Add N incoming values to the PHI node. |
| 918 | for (unsigned i = 0; i != NumEdges; ++i) |
| 919 | APN->addIncoming(IncomingVals[AllocaNo], Pred); |
| 920 | |
| 921 | // The currently active variable for this block is now the PHI. |
| 922 | IncomingVals[AllocaNo] = APN; |
| 923 | |
| 924 | // Get the next phi node. |
| 925 | ++PNI; |
| 926 | APN = dyn_cast<PHINode>(PNI); |
| 927 | if (APN == 0) break; |
| 928 | |
Chris Lattner | 2663ffe | 2008-02-05 21:26:23 +0000 | [diff] [blame] | 929 | // Verify that it is missing entries. If not, it is not being inserted |
| 930 | // by this mem2reg invocation so we want to ignore it. |
| 931 | } while (APN->getNumOperands() == NewPHINumOperands); |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 932 | } |
Chris Lattner | 9157f04 | 2003-10-05 02:37:36 +0000 | [diff] [blame] | 933 | } |
Chris Lattner | dfb22c3 | 2007-02-07 01:15:04 +0000 | [diff] [blame] | 934 | |
| 935 | // Don't revisit blocks. |
| 936 | if (!Visited.insert(BB)) return; |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 937 | |
Chris Lattner | 521c16a | 2003-10-05 03:45:44 +0000 | [diff] [blame] | 938 | for (BasicBlock::iterator II = BB->begin(); !isa<TerminatorInst>(II); ) { |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 939 | Instruction *I = II++; // get the instruction, increment iterator |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 940 | |
| 941 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 942 | AllocaInst *Src = dyn_cast<AllocaInst>(LI->getPointerOperand()); |
| 943 | if (!Src) continue; |
| 944 | |
| 945 | std::map<AllocaInst*, unsigned>::iterator AI = AllocaLookup.find(Src); |
| 946 | if (AI == AllocaLookup.end()) continue; |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 947 | |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 948 | Value *V = IncomingVals[AI->second]; |
| 949 | |
| 950 | // Anything using the load now uses the current value. |
| 951 | LI->replaceAllUsesWith(V); |
| 952 | if (AST && isa<PointerType>(LI->getType())) |
| 953 | AST->deleteValue(LI); |
| 954 | BB->getInstList().erase(LI); |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 955 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Chris Lattner | cc139de | 2003-02-22 22:25:17 +0000 | [diff] [blame] | 956 | // Delete this instruction and mark the name as the current holder of the |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 957 | // value |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 958 | AllocaInst *Dest = dyn_cast<AllocaInst>(SI->getPointerOperand()); |
| 959 | if (!Dest) continue; |
| 960 | |
| 961 | std::map<AllocaInst *, unsigned>::iterator ai = AllocaLookup.find(Dest); |
| 962 | if (ai == AllocaLookup.end()) |
| 963 | continue; |
| 964 | |
| 965 | // what value were we writing? |
| 966 | IncomingVals[ai->second] = SI->getOperand(0); |
| 967 | BB->getInstList().erase(SI); |
Chris Lattner | 9f4eb01 | 2002-04-28 18:27:55 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
Chris Lattner | 521c16a | 2003-10-05 03:45:44 +0000 | [diff] [blame] | 970 | |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 971 | // 'Recurse' to our successors. |
Nick Lewycky | 6e7aeb1 | 2008-03-13 02:42:41 +0000 | [diff] [blame] | 972 | succ_iterator I = succ_begin(BB), E = succ_end(BB); |
| 973 | if (I == E) return; |
| 974 | |
Eli Friedman | bde6fda | 2009-04-16 21:40:28 +0000 | [diff] [blame] | 975 | // Keep track of the successors so we don't visit the same successor twice |
| 976 | SmallPtrSet<BasicBlock*, 8> VisitedSuccs; |
Nick Lewycky | 6e7aeb1 | 2008-03-13 02:42:41 +0000 | [diff] [blame] | 977 | |
Eli Friedman | bde6fda | 2009-04-16 21:40:28 +0000 | [diff] [blame] | 978 | // Handle the first successor without using the worklist. |
| 979 | VisitedSuccs.insert(*I); |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 980 | Pred = BB; |
Nick Lewycky | 6e7aeb1 | 2008-03-13 02:42:41 +0000 | [diff] [blame] | 981 | BB = *I; |
Eli Friedman | bde6fda | 2009-04-16 21:40:28 +0000 | [diff] [blame] | 982 | ++I; |
| 983 | |
| 984 | for (; I != E; ++I) |
| 985 | if (VisitedSuccs.insert(*I)) |
| 986 | Worklist.push_back(RenamePassData(*I, Pred, IncomingVals)); |
| 987 | |
Chris Lattner | 1e76af3 | 2007-08-04 20:40:27 +0000 | [diff] [blame] | 988 | goto NextIteration; |
Chris Lattner | d3db022 | 2002-02-12 17:16:22 +0000 | [diff] [blame] | 989 | } |
Cameron Buschardt | b1be061 | 2002-03-27 23:28:40 +0000 | [diff] [blame] | 990 | |
Chris Lattner | d99bf49 | 2003-02-22 23:57:48 +0000 | [diff] [blame] | 991 | /// PromoteMemToReg - Promote the specified list of alloca instructions into |
| 992 | /// scalar registers, inserting PHI nodes as appropriate. This function makes |
| 993 | /// use of DominanceFrontier information. This function does not modify the CFG |
| 994 | /// of the function at all. All allocas must be from the same function. |
| 995 | /// |
Chris Lattner | 62e29b5 | 2004-09-15 01:02:54 +0000 | [diff] [blame] | 996 | /// If AST is specified, the specified tracker is updated to reflect changes |
| 997 | /// made to the IR. |
| 998 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 999 | void llvm::PromoteMemToReg(const std::vector<AllocaInst*> &Allocas, |
Devang Patel | 326821e | 2007-06-07 21:57:03 +0000 | [diff] [blame] | 1000 | DominatorTree &DT, DominanceFrontier &DF, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1001 | LLVMContext &Context, AliasSetTracker *AST) { |
Chris Lattner | 0fa1571 | 2003-10-05 01:52:53 +0000 | [diff] [blame] | 1002 | // If there is nothing to do, bail out... |
| 1003 | if (Allocas.empty()) return; |
Chris Lattner | 6cfd1eb | 2005-06-30 07:29:44 +0000 | [diff] [blame] | 1004 | |
Owen Anderson | 0a205a4 | 2009-07-05 22:41:43 +0000 | [diff] [blame] | 1005 | PromoteMem2Reg(Allocas, DT, DF, AST, Context).run(); |
Cameron Buschardt | b1be061 | 2002-03-27 23:28:40 +0000 | [diff] [blame] | 1006 | } |