1 //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
13 // each member (if possible). Then, if possible, it transforms the individual
14 // alloca instructions into nice clean scalar SSA form.
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.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Transforms/Scalar.h"
23 #include "llvm/Constants.h"
24 #include "llvm/DerivedTypes.h"
25 #include "llvm/Function.h"
26 #include "llvm/Pass.h"
27 #include "llvm/iMemory.h"
28 #include "llvm/Analysis/Dominators.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
31 #include "Support/Debug.h"
32 #include "Support/Statistic.h"
33 #include "Support/StringExtras.h"
36 Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up");
37 Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted");
39 struct SROA : public FunctionPass {
40 bool runOnFunction(Function &F);
42 bool performScalarRepl(Function &F);
43 bool performPromotion(Function &F);
45 // getAnalysisUsage - This pass does not require any passes, but we know it
46 // will not alter the CFG, so say so.
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<DominatorTree>();
49 AU.addRequired<DominanceFrontier>();
50 AU.addRequired<TargetData>();
55 bool isSafeElementUse(Value *Ptr);
56 bool isSafeUseOfAllocation(Instruction *User);
57 bool isSafeStructAllocaToPromote(AllocationInst *AI);
58 bool isSafeArrayAllocaToPromote(AllocationInst *AI);
59 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
62 RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
65 Pass *createScalarReplAggregatesPass() { return new SROA(); }
68 bool SROA::runOnFunction(Function &F) {
69 bool Changed = performPromotion(F);
71 bool LocalChange = performScalarRepl(F);
72 if (!LocalChange) break; // No need to repromote if no scalarrepl
74 LocalChange = performPromotion(F);
75 if (!LocalChange) break; // No need to re-scalarrepl if no promotion
82 bool SROA::performPromotion(Function &F) {
83 std::vector<AllocaInst*> Allocas;
84 const TargetData &TD = getAnalysis<TargetData>();
85 DominatorTree &DT = getAnalysis<DominatorTree>();
86 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
88 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
95 // Find allocas that are safe to promote, by looking at all instructions in
97 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
98 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
99 if (isAllocaPromotable(AI, TD))
100 Allocas.push_back(AI);
102 if (Allocas.empty()) break;
104 PromoteMemToReg(Allocas, DT, DF, TD);
105 NumPromoted += Allocas.size();
113 // performScalarRepl - This algorithm is a simple worklist driven algorithm,
114 // which runs on all of the malloc/alloca instructions in the function, removing
115 // them if they are only used by getelementptr instructions.
117 bool SROA::performScalarRepl(Function &F) {
118 std::vector<AllocationInst*> WorkList;
120 // Scan the entry basic block, adding any alloca's and mallocs to the worklist
121 BasicBlock &BB = F.getEntryBlock();
122 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
123 if (AllocationInst *A = dyn_cast<AllocationInst>(I))
124 WorkList.push_back(A);
126 // Process the worklist
127 bool Changed = false;
128 while (!WorkList.empty()) {
129 AllocationInst *AI = WorkList.back();
132 // We cannot transform the allocation instruction if it is an array
133 // allocation (allocations OF arrays are ok though), and an allocation of a
134 // scalar value cannot be decomposed at all.
136 if (AI->isArrayAllocation() ||
137 (!isa<StructType>(AI->getAllocatedType()) &&
138 !isa<ArrayType>(AI->getAllocatedType()))) continue;
140 // Check that all of the users of the allocation are capable of being
142 if (isa<StructType>(AI->getAllocatedType())) {
143 if (!isSafeStructAllocaToPromote(AI))
145 } else if (!isSafeArrayAllocaToPromote(AI))
148 DEBUG(std::cerr << "Found inst to xform: " << *AI);
151 std::vector<AllocaInst*> ElementAllocas;
152 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
153 ElementAllocas.reserve(ST->getNumContainedTypes());
154 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
155 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
156 AI->getName() + "." + utostr(i), AI);
157 ElementAllocas.push_back(NA);
158 WorkList.push_back(NA); // Add to worklist for recursive processing
161 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
162 ElementAllocas.reserve(AT->getNumElements());
163 const Type *ElTy = AT->getElementType();
164 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
165 AllocaInst *NA = new AllocaInst(ElTy, 0,
166 AI->getName() + "." + utostr(i), AI);
167 ElementAllocas.push_back(NA);
168 WorkList.push_back(NA); // Add to worklist for recursive processing
172 // Now that we have created the alloca instructions that we want to use,
173 // expand the getelementptr instructions to use them.
175 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
177 Instruction *User = cast<Instruction>(*I);
178 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
179 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
180 uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue();
182 assert(Idx < ElementAllocas.size() && "Index out of range?");
183 AllocaInst *AllocaToUse = ElementAllocas[Idx];
186 if (GEPI->getNumOperands() == 3) {
187 // Do not insert a new getelementptr instruction with zero indices,
188 // only to have it optimized out later.
189 RepValue = AllocaToUse;
191 // We are indexing deeply into the structure, so we still need a
192 // getelement ptr instruction to finish the indexing. This may be
193 // expanded itself once the worklist is rerun.
195 std::string OldName = GEPI->getName(); // Steal the old name...
196 std::vector<Value*> NewArgs;
197 NewArgs.push_back(Constant::getNullValue(Type::LongTy));
198 NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end());
201 new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI);
204 // Move all of the users over to the new GEP.
205 GEPI->replaceAllUsesWith(RepValue);
206 // Delete the old GEP
207 GEPI->getParent()->getInstList().erase(GEPI);
209 assert(0 && "Unexpected instruction type!");
213 // Finally, delete the Alloca instruction
214 AI->getParent()->getInstList().erase(AI);
222 /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
223 /// aggregate allocation.
225 bool SROA::isSafeUseOfAllocation(Instruction *User) {
226 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
227 // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
228 if (GEPI->getNumOperands() <= 2 ||
229 GEPI->getOperand(1) != Constant::getNullValue(Type::LongTy) ||
230 !isa<Constant>(GEPI->getOperand(2)) ||
231 isa<ConstantExpr>(GEPI->getOperand(2)))
239 /// isSafeElementUse - Check to see if this use is an allowed use for a
240 /// getelementptr instruction of an array aggregate allocation.
242 bool SROA::isSafeElementUse(Value *Ptr) {
243 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
245 Instruction *User = cast<Instruction>(*I);
246 switch (User->getOpcode()) {
247 case Instruction::Load: break;
248 case Instruction::Store:
249 // Store is ok if storing INTO the pointer, not storing the pointer
250 if (User->getOperand(0) == Ptr) return false;
252 case Instruction::GetElementPtr: {
253 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User);
254 if (GEP->getNumOperands() > 1) {
255 if (!isa<Constant>(GEP->getOperand(1)) ||
256 !cast<Constant>(GEP->getOperand(1))->isNullValue())
257 return false; // Using pointer arithmetic to navigate the array...
259 if (!isSafeElementUse(GEP)) return false;
263 DEBUG(std::cerr << " Transformation preventing inst: " << *User);
267 return true; // All users look ok :)
271 /// isSafeStructAllocaToPromote - Check to see if the specified allocation of a
272 /// structure can be broken down into elements.
274 bool SROA::isSafeStructAllocaToPromote(AllocationInst *AI) {
275 // Loop over the use list of the alloca. We can only transform it if all of
276 // the users are safe to transform.
278 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
280 if (!isSafeUseOfAllocation(cast<Instruction>(*I))) {
281 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
286 // Pedantic check to avoid breaking broken programs...
287 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*I))
288 if (GEPI->getNumOperands() == 3 && !isSafeElementUse(GEPI))
295 /// isSafeArrayAllocaToPromote - Check to see if the specified allocation of a
296 /// structure can be broken down into elements.
298 bool SROA::isSafeArrayAllocaToPromote(AllocationInst *AI) {
299 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
300 int64_t NumElements = AT->getNumElements();
302 // Loop over the use list of the alloca. We can only transform it if all of
303 // the users are safe to transform. Array allocas have extra constraints to
306 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
308 Instruction *User = cast<Instruction>(*I);
309 if (!isSafeUseOfAllocation(User)) {
310 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
315 // Check to make sure that getelementptr follow the extra rules for arrays:
316 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
317 // Check to make sure that index falls within the array. If not,
318 // something funny is going on, so we won't do the optimization.
320 if (cast<ConstantSInt>(GEPI->getOperand(2))->getValue() >= NumElements)
323 // Check to make sure that the only thing that uses the resultant pointer
324 // is safe for an array access. For example, code that looks like:
325 // P = &A[0]; P = P + 1
326 // is legal, and should prevent promotion.
328 if (!isSafeElementUse(GEPI)) {
329 DEBUG(std::cerr << "Cannot transform: " << *AI
330 << " due to uses of user: " << *GEPI);