Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 1 | //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 9 | // |
| 10 | // This transformation implements the well known scalar replacement of |
| 11 | // aggregates transformation. This xform breaks up alloca instructions of |
| 12 | // aggregate type (structure or array) into individual alloca instructions for |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 13 | // each member (if possible). Then, if possible, it transforms the individual |
| 14 | // alloca instructions into nice clean scalar SSA form. |
| 15 | // |
| 16 | // This combines a simple SRoA algorithm with the Mem2Reg algorithm because |
| 17 | // often interact, especially for C++ programs. As such, iterating between |
| 18 | // SRoA, then Mem2Reg until we run out of things to promote works well. |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
| 24 | #include "llvm/DerivedTypes.h" |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 25 | #include "llvm/Function.h" |
| 26 | #include "llvm/Pass.h" |
| 27 | #include "llvm/iMemory.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 29 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
| 31 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 32 | #include "Support/Debug.h" |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 33 | #include "Support/Statistic.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 34 | #include "Support/StringExtras.h" |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 37 | namespace { |
Misha Brukman | 3cfb6b1 | 2003-09-11 16:58:31 +0000 | [diff] [blame] | 38 | Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up"); |
| 39 | Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted"); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 40 | |
| 41 | struct SROA : public FunctionPass { |
| 42 | bool runOnFunction(Function &F); |
| 43 | |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 44 | bool performScalarRepl(Function &F); |
| 45 | bool performPromotion(Function &F); |
| 46 | |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 47 | // getAnalysisUsage - This pass does not require any passes, but we know it |
| 48 | // will not alter the CFG, so say so. |
| 49 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 50 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 51 | AU.addRequired<DominanceFrontier>(); |
| 52 | AU.addRequired<TargetData>(); |
Chris Lattner | a15854c | 2003-08-31 00:45:13 +0000 | [diff] [blame] | 53 | AU.setPreservesCFG(); |
| 54 | } |
| 55 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 56 | private: |
Chris Lattner | b37923f | 2003-05-30 19:22:14 +0000 | [diff] [blame] | 57 | bool isSafeElementUse(Value *Ptr); |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 58 | bool isSafeUseOfAllocation(Instruction *User); |
Chris Lattner | 546fc40 | 2003-10-29 17:55:44 +0000 | [diff] [blame] | 59 | bool isSafeAllocaToPromote(AllocationInst *AI); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 60 | AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base); |
| 61 | }; |
| 62 | |
| 63 | RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates"); |
| 64 | } |
| 65 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 66 | // Public interface to the ScalarReplAggregates pass |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 67 | Pass *llvm::createScalarReplAggregatesPass() { return new SROA(); } |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 68 | |
| 69 | |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 70 | bool SROA::runOnFunction(Function &F) { |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 71 | bool Changed = performPromotion(F); |
| 72 | while (1) { |
| 73 | bool LocalChange = performScalarRepl(F); |
| 74 | if (!LocalChange) break; // No need to repromote if no scalarrepl |
| 75 | Changed = true; |
| 76 | LocalChange = performPromotion(F); |
| 77 | if (!LocalChange) break; // No need to re-scalarrepl if no promotion |
| 78 | } |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 79 | |
| 80 | return Changed; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | bool SROA::performPromotion(Function &F) { |
| 85 | std::vector<AllocaInst*> Allocas; |
| 86 | const TargetData &TD = getAnalysis<TargetData>(); |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 87 | DominatorTree &DT = getAnalysis<DominatorTree>(); |
| 88 | DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 90 | BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 91 | |
Chris Lattner | fe7ea0d | 2003-09-12 15:36:03 +0000 | [diff] [blame] | 92 | bool Changed = false; |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 93 | |
| 94 | while (1) { |
| 95 | Allocas.clear(); |
| 96 | |
| 97 | // Find allocas that are safe to promote, by looking at all instructions in |
| 98 | // the entry node |
| 99 | for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) |
| 100 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? |
| 101 | if (isAllocaPromotable(AI, TD)) |
| 102 | Allocas.push_back(AI); |
| 103 | |
| 104 | if (Allocas.empty()) break; |
| 105 | |
Chris Lattner | 43f820d | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 106 | PromoteMemToReg(Allocas, DT, DF, TD); |
Chris Lattner | 38aec32 | 2003-09-11 16:45:55 +0000 | [diff] [blame] | 107 | NumPromoted += Allocas.size(); |
| 108 | Changed = true; |
| 109 | } |
| 110 | |
| 111 | return Changed; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // performScalarRepl - This algorithm is a simple worklist driven algorithm, |
| 116 | // which runs on all of the malloc/alloca instructions in the function, removing |
| 117 | // them if they are only used by getelementptr instructions. |
| 118 | // |
| 119 | bool SROA::performScalarRepl(Function &F) { |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 120 | std::vector<AllocationInst*> WorkList; |
| 121 | |
| 122 | // Scan the entry basic block, adding any alloca's and mallocs to the worklist |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 123 | BasicBlock &BB = F.getEntryBlock(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 124 | for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) |
| 125 | if (AllocationInst *A = dyn_cast<AllocationInst>(I)) |
| 126 | WorkList.push_back(A); |
| 127 | |
| 128 | // Process the worklist |
| 129 | bool Changed = false; |
| 130 | while (!WorkList.empty()) { |
| 131 | AllocationInst *AI = WorkList.back(); |
| 132 | WorkList.pop_back(); |
| 133 | |
| 134 | // We cannot transform the allocation instruction if it is an array |
Chris Lattner | d10376b | 2003-05-27 16:09:27 +0000 | [diff] [blame] | 135 | // allocation (allocations OF arrays are ok though), and an allocation of a |
| 136 | // scalar value cannot be decomposed at all. |
| 137 | // |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 138 | if (AI->isArrayAllocation() || |
Chris Lattner | d10376b | 2003-05-27 16:09:27 +0000 | [diff] [blame] | 139 | (!isa<StructType>(AI->getAllocatedType()) && |
| 140 | !isa<ArrayType>(AI->getAllocatedType()))) continue; |
| 141 | |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 142 | // Check that all of the users of the allocation are capable of being |
| 143 | // transformed. |
Chris Lattner | 546fc40 | 2003-10-29 17:55:44 +0000 | [diff] [blame] | 144 | if (!isSafeAllocaToPromote(AI)) |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 145 | continue; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 146 | |
| 147 | DEBUG(std::cerr << "Found inst to xform: " << *AI); |
| 148 | Changed = true; |
| 149 | |
| 150 | std::vector<AllocaInst*> ElementAllocas; |
| 151 | if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { |
| 152 | ElementAllocas.reserve(ST->getNumContainedTypes()); |
| 153 | for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { |
| 154 | AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, |
| 155 | AI->getName() + "." + utostr(i), AI); |
| 156 | ElementAllocas.push_back(NA); |
| 157 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 158 | } |
| 159 | } else { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 160 | const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 161 | ElementAllocas.reserve(AT->getNumElements()); |
| 162 | const Type *ElTy = AT->getElementType(); |
| 163 | for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { |
| 164 | AllocaInst *NA = new AllocaInst(ElTy, 0, |
| 165 | AI->getName() + "." + utostr(i), AI); |
| 166 | ElementAllocas.push_back(NA); |
| 167 | WorkList.push_back(NA); // Add to worklist for recursive processing |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Now that we have created the alloca instructions that we want to use, |
| 172 | // expand the getelementptr instructions to use them. |
| 173 | // |
| 174 | for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); |
| 175 | I != E; ++I) { |
| 176 | Instruction *User = cast<Instruction>(*I); |
| 177 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) { |
| 178 | // We now know that the GEP is of the form: GEP <ptr>, 0, <cst> |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 179 | uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue(); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 180 | |
| 181 | assert(Idx < ElementAllocas.size() && "Index out of range?"); |
| 182 | AllocaInst *AllocaToUse = ElementAllocas[Idx]; |
| 183 | |
| 184 | Value *RepValue; |
| 185 | if (GEPI->getNumOperands() == 3) { |
| 186 | // Do not insert a new getelementptr instruction with zero indices, |
| 187 | // only to have it optimized out later. |
| 188 | RepValue = AllocaToUse; |
| 189 | } else { |
| 190 | // We are indexing deeply into the structure, so we still need a |
| 191 | // getelement ptr instruction to finish the indexing. This may be |
| 192 | // expanded itself once the worklist is rerun. |
| 193 | // |
| 194 | std::string OldName = GEPI->getName(); // Steal the old name... |
Chris Lattner | 261d686 | 2003-05-30 05:26:30 +0000 | [diff] [blame] | 195 | std::vector<Value*> NewArgs; |
| 196 | NewArgs.push_back(Constant::getNullValue(Type::LongTy)); |
| 197 | NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end()); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 198 | GEPI->setName(""); |
| 199 | RepValue = |
Chris Lattner | 261d686 | 2003-05-30 05:26:30 +0000 | [diff] [blame] | 200 | new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI); |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Move all of the users over to the new GEP. |
| 204 | GEPI->replaceAllUsesWith(RepValue); |
| 205 | // Delete the old GEP |
| 206 | GEPI->getParent()->getInstList().erase(GEPI); |
| 207 | } else { |
| 208 | assert(0 && "Unexpected instruction type!"); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Finally, delete the Alloca instruction |
| 213 | AI->getParent()->getInstList().erase(AI); |
Chris Lattner | d10376b | 2003-05-27 16:09:27 +0000 | [diff] [blame] | 214 | NumReplaced++; |
Chris Lattner | ed7b41e | 2003-05-27 15:45:27 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return Changed; |
| 218 | } |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 219 | |
| 220 | |
| 221 | /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an |
| 222 | /// aggregate allocation. |
| 223 | /// |
| 224 | bool SROA::isSafeUseOfAllocation(Instruction *User) { |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 225 | if (!isa<GetElementPtrInst>(User)) return false; |
| 226 | |
| 227 | GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); |
| 228 | gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); |
| 229 | |
| 230 | // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst> |
| 231 | if (I == E || |
| 232 | I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) |
| 233 | return false; |
| 234 | |
| 235 | ++I; |
Chris Lattner | d866473 | 2003-12-02 17:43:55 +0000 | [diff] [blame] | 236 | if (I == E || !isa<ConstantInt>(I.getOperand())) |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 237 | return false; |
| 238 | |
| 239 | // If this is a use of an array allocation, do a bit more checking for sanity. |
| 240 | if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { |
| 241 | uint64_t NumElements = AT->getNumElements(); |
| 242 | |
| 243 | // Check to make sure that index falls within the array. If not, |
| 244 | // something funny is going on, so we won't do the optimization. |
| 245 | // |
| 246 | if (cast<ConstantInt>(GEPI->getOperand(2))->getRawValue() >= NumElements) |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 247 | return false; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 248 | } |
Chris Lattner | be883a2 | 2003-11-25 21:09:18 +0000 | [diff] [blame] | 249 | |
| 250 | // If there are any non-simple uses of this getelementptr, make sure to reject |
| 251 | // them. |
| 252 | return isSafeElementUse(GEPI); |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | b37923f | 2003-05-30 19:22:14 +0000 | [diff] [blame] | 255 | /// isSafeElementUse - Check to see if this use is an allowed use for a |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 256 | /// getelementptr instruction of an array aggregate allocation. |
| 257 | /// |
Chris Lattner | b37923f | 2003-05-30 19:22:14 +0000 | [diff] [blame] | 258 | bool SROA::isSafeElementUse(Value *Ptr) { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 259 | for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); |
| 260 | I != E; ++I) { |
| 261 | Instruction *User = cast<Instruction>(*I); |
| 262 | switch (User->getOpcode()) { |
Chris Lattner | 8fce16e | 2003-09-12 16:02:12 +0000 | [diff] [blame] | 263 | case Instruction::Load: break; |
| 264 | case Instruction::Store: |
| 265 | // Store is ok if storing INTO the pointer, not storing the pointer |
| 266 | if (User->getOperand(0) == Ptr) return false; |
| 267 | break; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 268 | case Instruction::GetElementPtr: { |
| 269 | GetElementPtrInst *GEP = cast<GetElementPtrInst>(User); |
| 270 | if (GEP->getNumOperands() > 1) { |
| 271 | if (!isa<Constant>(GEP->getOperand(1)) || |
| 272 | !cast<Constant>(GEP->getOperand(1))->isNullValue()) |
| 273 | return false; // Using pointer arithmetic to navigate the array... |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | 8fce16e | 2003-09-12 16:02:12 +0000 | [diff] [blame] | 275 | if (!isSafeElementUse(GEP)) return false; |
| 276 | break; |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 277 | } |
| 278 | default: |
| 279 | DEBUG(std::cerr << " Transformation preventing inst: " << *User); |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | return true; // All users look ok :) |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /// isSafeStructAllocaToPromote - Check to see if the specified allocation of a |
| 288 | /// structure can be broken down into elements. |
| 289 | /// |
Chris Lattner | 546fc40 | 2003-10-29 17:55:44 +0000 | [diff] [blame] | 290 | bool SROA::isSafeAllocaToPromote(AllocationInst *AI) { |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 291 | // Loop over the use list of the alloca. We can only transform it if all of |
| 292 | // the users are safe to transform. |
| 293 | // |
| 294 | for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); |
Chris Lattner | 546fc40 | 2003-10-29 17:55:44 +0000 | [diff] [blame] | 295 | I != E; ++I) |
Chris Lattner | 5e062a1 | 2003-05-30 04:15:41 +0000 | [diff] [blame] | 296 | if (!isSafeUseOfAllocation(cast<Instruction>(*I))) { |
| 297 | DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: " |
| 298 | << *I); |
| 299 | return false; |
| 300 | } |
| 301 | return true; |
| 302 | } |