Added LLVM project notice to the top of every C++ source file.
[oota-llvm.git] / lib / Transforms / Scalar / ScalarReplAggregates.cpp
1 //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===//
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 //===----------------------------------------------------------------------===//
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
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.
19 //
20 //===----------------------------------------------------------------------===//
21
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"
34
35 namespace {
36   Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up");
37   Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted");
38
39   struct SROA : public FunctionPass {
40     bool runOnFunction(Function &F);
41
42     bool performScalarRepl(Function &F);
43     bool performPromotion(Function &F);
44
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>();
51       AU.setPreservesCFG();
52     }
53
54   private:
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);
60   };
61
62   RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
63 }
64
65 Pass *createScalarReplAggregatesPass() { return new SROA(); }
66
67
68 bool SROA::runOnFunction(Function &F) {
69   bool Changed = performPromotion(F);
70   while (1) {
71     bool LocalChange = performScalarRepl(F);
72     if (!LocalChange) break;   // No need to repromote if no scalarrepl
73     Changed = true;
74     LocalChange = performPromotion(F);
75     if (!LocalChange) break;   // No need to re-scalarrepl if no promotion
76   }
77
78   return Changed;
79 }
80
81
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>();
87
88   BasicBlock &BB = F.getEntryBlock();  // Get the entry node for the function
89
90   bool Changed = false;
91   
92   while (1) {
93     Allocas.clear();
94
95     // Find allocas that are safe to promote, by looking at all instructions in
96     // the entry node
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);
101
102     if (Allocas.empty()) break;
103
104     PromoteMemToReg(Allocas, DT, DF, TD);
105     NumPromoted += Allocas.size();
106     Changed = true;
107   }
108
109   return Changed;
110 }
111
112
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.
116 //
117 bool SROA::performScalarRepl(Function &F) {
118   std::vector<AllocationInst*> WorkList;
119
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);
125
126   // Process the worklist
127   bool Changed = false;
128   while (!WorkList.empty()) {
129     AllocationInst *AI = WorkList.back();
130     WorkList.pop_back();
131
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.
135     //
136     if (AI->isArrayAllocation() ||
137         (!isa<StructType>(AI->getAllocatedType()) &&
138          !isa<ArrayType>(AI->getAllocatedType()))) continue;
139
140     // Check that all of the users of the allocation are capable of being
141     // transformed.
142     if (isa<StructType>(AI->getAllocatedType())) {
143       if (!isSafeStructAllocaToPromote(AI))
144         continue;
145     } else if (!isSafeArrayAllocaToPromote(AI))
146       continue;
147
148     DEBUG(std::cerr << "Found inst to xform: " << *AI);
149     Changed = true;
150     
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
159       }
160     } else {
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
169       }
170     }
171     
172     // Now that we have created the alloca instructions that we want to use,
173     // expand the getelementptr instructions to use them.
174     //
175     for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
176          I != E; ++I) {
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();
181         
182         assert(Idx < ElementAllocas.size() && "Index out of range?");
183         AllocaInst *AllocaToUse = ElementAllocas[Idx];
184
185         Value *RepValue;
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;
190         } else {
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.
194           //
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());
199           GEPI->setName("");
200           RepValue =
201             new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI);
202         }
203
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);
208       } else {
209         assert(0 && "Unexpected instruction type!");
210       }
211     }
212
213     // Finally, delete the Alloca instruction
214     AI->getParent()->getInstList().erase(AI);
215     NumReplaced++;
216   }
217
218   return Changed;
219 }
220
221
222 /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
223 /// aggregate allocation.
224 ///
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)))
232       return false;
233   } else {
234     return false;
235   }
236   return true;
237 }
238
239 /// isSafeElementUse - Check to see if this use is an allowed use for a
240 /// getelementptr instruction of an array aggregate allocation.
241 ///
242 bool SROA::isSafeElementUse(Value *Ptr) {
243   for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
244        I != E; ++I) {
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;
251       break;
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...
258       }
259       if (!isSafeElementUse(GEP)) return false;
260       break;
261     }
262     default:
263       DEBUG(std::cerr << "  Transformation preventing inst: " << *User);
264       return false;
265     }
266   }
267   return true;  // All users look ok :)
268 }
269
270
271 /// isSafeStructAllocaToPromote - Check to see if the specified allocation of a
272 /// structure can be broken down into elements.
273 ///
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.
277   //
278   for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
279        I != E; ++I) {
280     if (!isSafeUseOfAllocation(cast<Instruction>(*I))) {
281       DEBUG(std::cerr << "Cannot transform: " << *AI << "  due to user: "
282                       << *I);
283       return false;
284     }
285
286     // Pedantic check to avoid breaking broken programs...
287     if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*I))
288       if (GEPI->getNumOperands() == 3 && !isSafeElementUse(GEPI))
289         return false;
290   }
291   return true;
292 }
293
294
295 /// isSafeArrayAllocaToPromote - Check to see if the specified allocation of a
296 /// structure can be broken down into elements.
297 ///
298 bool SROA::isSafeArrayAllocaToPromote(AllocationInst *AI) {
299   const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
300   int64_t NumElements = AT->getNumElements();
301
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
304   // meet though.
305   //
306   for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
307        I != E; ++I) {
308     Instruction *User = cast<Instruction>(*I);
309     if (!isSafeUseOfAllocation(User)) {
310       DEBUG(std::cerr << "Cannot transform: " << *AI << "  due to user: "
311                       << User);
312       return false;
313     }
314
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.
319       //
320       if (cast<ConstantSInt>(GEPI->getOperand(2))->getValue() >= NumElements)
321         return false;
322
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.
327       //
328       if (!isSafeElementUse(GEPI)) {
329         DEBUG(std::cerr << "Cannot transform: " << *AI
330                         << "  due to uses of user: " << *GEPI);
331         return false;
332       }
333     }
334   }
335   return true;
336 }
337