LLVM API Documentation
00001 //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This transformation implements the well known scalar replacement of 00011 // aggregates transformation. This xform breaks up alloca instructions of 00012 // aggregate type (structure or array) into individual alloca instructions for 00013 // each member (if possible). Then, if possible, it transforms the individual 00014 // alloca instructions into nice clean scalar SSA form. 00015 // 00016 // This combines a simple SRoA algorithm with the Mem2Reg algorithm because 00017 // often interact, especially for C++ programs. As such, iterating between 00018 // SRoA, then Mem2Reg until we run out of things to promote works well. 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #include "llvm/Transforms/Scalar.h" 00023 #include "llvm/Constants.h" 00024 #include "llvm/DerivedTypes.h" 00025 #include "llvm/Function.h" 00026 #include "llvm/Pass.h" 00027 #include "llvm/Instructions.h" 00028 #include "llvm/Analysis/Dominators.h" 00029 #include "llvm/Support/GetElementPtrTypeIterator.h" 00030 #include "llvm/Target/TargetData.h" 00031 #include "llvm/Transforms/Utils/PromoteMemToReg.h" 00032 #include "llvm/Support/Debug.h" 00033 #include "llvm/ADT/Statistic.h" 00034 #include "llvm/ADT/StringExtras.h" 00035 using namespace llvm; 00036 00037 namespace { 00038 Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up"); 00039 Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted"); 00040 00041 struct SROA : public FunctionPass { 00042 bool runOnFunction(Function &F); 00043 00044 bool performScalarRepl(Function &F); 00045 bool performPromotion(Function &F); 00046 00047 // getAnalysisUsage - This pass does not require any passes, but we know it 00048 // will not alter the CFG, so say so. 00049 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00050 AU.addRequired<DominatorTree>(); 00051 AU.addRequired<DominanceFrontier>(); 00052 AU.addRequired<TargetData>(); 00053 AU.setPreservesCFG(); 00054 } 00055 00056 private: 00057 int isSafeElementUse(Value *Ptr); 00058 int isSafeUseOfAllocation(Instruction *User); 00059 int isSafeAllocaToScalarRepl(AllocationInst *AI); 00060 void CanonicalizeAllocaUsers(AllocationInst *AI); 00061 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base); 00062 }; 00063 00064 RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates"); 00065 } 00066 00067 // Public interface to the ScalarReplAggregates pass 00068 FunctionPass *llvm::createScalarReplAggregatesPass() { return new SROA(); } 00069 00070 00071 bool SROA::runOnFunction(Function &F) { 00072 bool Changed = performPromotion(F); 00073 while (1) { 00074 bool LocalChange = performScalarRepl(F); 00075 if (!LocalChange) break; // No need to repromote if no scalarrepl 00076 Changed = true; 00077 LocalChange = performPromotion(F); 00078 if (!LocalChange) break; // No need to re-scalarrepl if no promotion 00079 } 00080 00081 return Changed; 00082 } 00083 00084 00085 bool SROA::performPromotion(Function &F) { 00086 std::vector<AllocaInst*> Allocas; 00087 const TargetData &TD = getAnalysis<TargetData>(); 00088 DominatorTree &DT = getAnalysis<DominatorTree>(); 00089 DominanceFrontier &DF = getAnalysis<DominanceFrontier>(); 00090 00091 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function 00092 00093 bool Changed = false; 00094 00095 while (1) { 00096 Allocas.clear(); 00097 00098 // Find allocas that are safe to promote, by looking at all instructions in 00099 // the entry node 00100 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I) 00101 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca? 00102 if (isAllocaPromotable(AI, TD)) 00103 Allocas.push_back(AI); 00104 00105 if (Allocas.empty()) break; 00106 00107 PromoteMemToReg(Allocas, DT, DF, TD); 00108 NumPromoted += Allocas.size(); 00109 Changed = true; 00110 } 00111 00112 return Changed; 00113 } 00114 00115 00116 // performScalarRepl - This algorithm is a simple worklist driven algorithm, 00117 // which runs on all of the malloc/alloca instructions in the function, removing 00118 // them if they are only used by getelementptr instructions. 00119 // 00120 bool SROA::performScalarRepl(Function &F) { 00121 std::vector<AllocationInst*> WorkList; 00122 00123 // Scan the entry basic block, adding any alloca's and mallocs to the worklist 00124 BasicBlock &BB = F.getEntryBlock(); 00125 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) 00126 if (AllocationInst *A = dyn_cast<AllocationInst>(I)) 00127 WorkList.push_back(A); 00128 00129 // Process the worklist 00130 bool Changed = false; 00131 while (!WorkList.empty()) { 00132 AllocationInst *AI = WorkList.back(); 00133 WorkList.pop_back(); 00134 00135 // We cannot transform the allocation instruction if it is an array 00136 // allocation (allocations OF arrays are ok though), and an allocation of a 00137 // scalar value cannot be decomposed at all. 00138 // 00139 if (AI->isArrayAllocation() || 00140 (!isa<StructType>(AI->getAllocatedType()) && 00141 !isa<ArrayType>(AI->getAllocatedType()))) continue; 00142 00143 // Check that all of the users of the allocation are capable of being 00144 // transformed. 00145 switch (isSafeAllocaToScalarRepl(AI)) { 00146 default: assert(0 && "Unexpected value!"); 00147 case 0: // Not safe to scalar replace. 00148 continue; 00149 case 1: // Safe, but requires cleanup/canonicalizations first 00150 CanonicalizeAllocaUsers(AI); 00151 case 3: // Safe to scalar replace. 00152 break; 00153 } 00154 00155 DEBUG(std::cerr << "Found inst to xform: " << *AI); 00156 Changed = true; 00157 00158 std::vector<AllocaInst*> ElementAllocas; 00159 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) { 00160 ElementAllocas.reserve(ST->getNumContainedTypes()); 00161 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) { 00162 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0, 00163 AI->getName() + "." + utostr(i), AI); 00164 ElementAllocas.push_back(NA); 00165 WorkList.push_back(NA); // Add to worklist for recursive processing 00166 } 00167 } else { 00168 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType()); 00169 ElementAllocas.reserve(AT->getNumElements()); 00170 const Type *ElTy = AT->getElementType(); 00171 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) { 00172 AllocaInst *NA = new AllocaInst(ElTy, 0, 00173 AI->getName() + "." + utostr(i), AI); 00174 ElementAllocas.push_back(NA); 00175 WorkList.push_back(NA); // Add to worklist for recursive processing 00176 } 00177 } 00178 00179 // Now that we have created the alloca instructions that we want to use, 00180 // expand the getelementptr instructions to use them. 00181 // 00182 while (!AI->use_empty()) { 00183 Instruction *User = cast<Instruction>(AI->use_back()); 00184 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); 00185 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst> 00186 uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue(); 00187 00188 assert(Idx < ElementAllocas.size() && "Index out of range?"); 00189 AllocaInst *AllocaToUse = ElementAllocas[Idx]; 00190 00191 Value *RepValue; 00192 if (GEPI->getNumOperands() == 3) { 00193 // Do not insert a new getelementptr instruction with zero indices, only 00194 // to have it optimized out later. 00195 RepValue = AllocaToUse; 00196 } else { 00197 // We are indexing deeply into the structure, so we still need a 00198 // getelement ptr instruction to finish the indexing. This may be 00199 // expanded itself once the worklist is rerun. 00200 // 00201 std::string OldName = GEPI->getName(); // Steal the old name. 00202 std::vector<Value*> NewArgs; 00203 NewArgs.push_back(Constant::getNullValue(Type::IntTy)); 00204 NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end()); 00205 GEPI->setName(""); 00206 RepValue = new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI); 00207 } 00208 00209 // Move all of the users over to the new GEP. 00210 GEPI->replaceAllUsesWith(RepValue); 00211 // Delete the old GEP 00212 GEPI->eraseFromParent(); 00213 } 00214 00215 // Finally, delete the Alloca instruction 00216 AI->getParent()->getInstList().erase(AI); 00217 NumReplaced++; 00218 } 00219 00220 return Changed; 00221 } 00222 00223 00224 /// isSafeElementUse - Check to see if this use is an allowed use for a 00225 /// getelementptr instruction of an array aggregate allocation. 00226 /// 00227 int SROA::isSafeElementUse(Value *Ptr) { 00228 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); 00229 I != E; ++I) { 00230 Instruction *User = cast<Instruction>(*I); 00231 switch (User->getOpcode()) { 00232 case Instruction::Load: break; 00233 case Instruction::Store: 00234 // Store is ok if storing INTO the pointer, not storing the pointer 00235 if (User->getOperand(0) == Ptr) return 0; 00236 break; 00237 case Instruction::GetElementPtr: { 00238 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User); 00239 if (GEP->getNumOperands() > 1) { 00240 if (!isa<Constant>(GEP->getOperand(1)) || 00241 !cast<Constant>(GEP->getOperand(1))->isNullValue()) 00242 return 0; // Using pointer arithmetic to navigate the array... 00243 } 00244 if (!isSafeElementUse(GEP)) return 0; 00245 break; 00246 } 00247 default: 00248 DEBUG(std::cerr << " Transformation preventing inst: " << *User); 00249 return 0; 00250 } 00251 } 00252 return 3; // All users look ok :) 00253 } 00254 00255 /// AllUsersAreLoads - Return true if all users of this value are loads. 00256 static bool AllUsersAreLoads(Value *Ptr) { 00257 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end(); 00258 I != E; ++I) 00259 if (cast<Instruction>(*I)->getOpcode() != Instruction::Load) 00260 return false; 00261 return true; 00262 } 00263 00264 /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an 00265 /// aggregate allocation. 00266 /// 00267 int SROA::isSafeUseOfAllocation(Instruction *User) { 00268 if (!isa<GetElementPtrInst>(User)) return 0; 00269 00270 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User); 00271 gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI); 00272 00273 // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst> 00274 if (I == E || 00275 I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) 00276 return 0; 00277 00278 ++I; 00279 if (I == E) return 0; // ran out of GEP indices?? 00280 00281 // If this is a use of an array allocation, do a bit more checking for sanity. 00282 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { 00283 uint64_t NumElements = AT->getNumElements(); 00284 00285 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { 00286 // Check to make sure that index falls within the array. If not, 00287 // something funny is going on, so we won't do the optimization. 00288 // 00289 if (cast<ConstantInt>(GEPI->getOperand(2))->getRawValue() >= NumElements) 00290 return 0; 00291 00292 } else { 00293 // If this is an array index and the index is not constant, we cannot 00294 // promote... that is unless the array has exactly one or two elements in 00295 // it, in which case we CAN promote it, but we have to canonicalize this 00296 // out if this is the only problem. 00297 if (NumElements == 1 || NumElements == 2) 00298 return AllUsersAreLoads(GEPI) ? 1 : 0; // Canonicalization required! 00299 return 0; 00300 } 00301 } 00302 00303 // If there are any non-simple uses of this getelementptr, make sure to reject 00304 // them. 00305 return isSafeElementUse(GEPI); 00306 } 00307 00308 /// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of 00309 /// an aggregate can be broken down into elements. Return 0 if not, 3 if safe, 00310 /// or 1 if safe after canonicalization has been performed. 00311 /// 00312 int SROA::isSafeAllocaToScalarRepl(AllocationInst *AI) { 00313 // Loop over the use list of the alloca. We can only transform it if all of 00314 // the users are safe to transform. 00315 // 00316 int isSafe = 3; 00317 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); 00318 I != E; ++I) { 00319 isSafe &= isSafeUseOfAllocation(cast<Instruction>(*I)); 00320 if (isSafe == 0) { 00321 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: " 00322 << **I); 00323 return 0; 00324 } 00325 } 00326 // If we require cleanup, isSafe is now 1, otherwise it is 3. 00327 return isSafe; 00328 } 00329 00330 /// CanonicalizeAllocaUsers - If SROA reported that it can promote the specified 00331 /// allocation, but only if cleaned up, perform the cleanups required. 00332 void SROA::CanonicalizeAllocaUsers(AllocationInst *AI) { 00333 // At this point, we know that the end result will be SROA'd and promoted, so 00334 // we can insert ugly code if required so long as sroa+mem2reg will clean it 00335 // up. 00336 for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); 00337 UI != E; ) { 00338 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(*UI++); 00339 gep_type_iterator I = gep_type_begin(GEPI); 00340 ++I; 00341 00342 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { 00343 uint64_t NumElements = AT->getNumElements(); 00344 00345 if (!isa<ConstantInt>(I.getOperand())) { 00346 if (NumElements == 1) { 00347 GEPI->setOperand(2, Constant::getNullValue(Type::IntTy)); 00348 } else { 00349 assert(NumElements == 2 && "Unhandled case!"); 00350 // All users of the GEP must be loads. At each use of the GEP, insert 00351 // two loads of the appropriate indexed GEP and select between them. 00352 Value *IsOne = BinaryOperator::createSetNE(I.getOperand(), 00353 Constant::getNullValue(I.getOperand()->getType()), 00354 "isone", GEPI); 00355 // Insert the new GEP instructions, which are properly indexed. 00356 std::vector<Value*> Indices(GEPI->op_begin()+1, GEPI->op_end()); 00357 Indices[1] = Constant::getNullValue(Type::IntTy); 00358 Value *ZeroIdx = new GetElementPtrInst(GEPI->getOperand(0), Indices, 00359 GEPI->getName()+".0", GEPI); 00360 Indices[1] = ConstantInt::get(Type::IntTy, 1); 00361 Value *OneIdx = new GetElementPtrInst(GEPI->getOperand(0), Indices, 00362 GEPI->getName()+".1", GEPI); 00363 // Replace all loads of the variable index GEP with loads from both 00364 // indexes and a select. 00365 while (!GEPI->use_empty()) { 00366 LoadInst *LI = cast<LoadInst>(GEPI->use_back()); 00367 Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI); 00368 Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI); 00369 Value *R = new SelectInst(IsOne, One, Zero, LI->getName(), LI); 00370 LI->replaceAllUsesWith(R); 00371 LI->eraseFromParent(); 00372 } 00373 GEPI->eraseFromParent(); 00374 } 00375 } 00376 } 00377 } 00378 }