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