blob: 2a6bddfb325e455f1982ee7325028fd94e715277 [file] [log] [blame]
Chris Lattnered7b41e2003-05-27 15:45:27 +00001//===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===//
2//
3// This transformation implements the well known scalar replacement of
4// aggregates transformation. This xform breaks up alloca instructions of
5// aggregate type (structure or array) into individual alloca instructions for
Chris Lattner38aec322003-09-11 16:45:55 +00006// each member (if possible). Then, if possible, it transforms the individual
7// alloca instructions into nice clean scalar SSA form.
8//
9// This combines a simple SRoA algorithm with the Mem2Reg algorithm because
10// often interact, especially for C++ programs. As such, iterating between
11// SRoA, then Mem2Reg until we run out of things to promote works well.
Chris Lattnered7b41e2003-05-27 15:45:27 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Scalar.h"
Chris Lattner38aec322003-09-11 16:45:55 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
Chris Lattnered7b41e2003-05-27 15:45:27 +000018#include "llvm/Function.h"
19#include "llvm/Pass.h"
20#include "llvm/iMemory.h"
Chris Lattner38aec322003-09-11 16:45:55 +000021#include "llvm/Analysis/Dominators.h"
22#include "llvm/Target/TargetData.h"
23#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Chris Lattner6806f562003-08-01 22:15:03 +000024#include "Support/Debug.h"
Chris Lattnered7b41e2003-05-27 15:45:27 +000025#include "Support/Statistic.h"
Chris Lattner6806f562003-08-01 22:15:03 +000026#include "Support/StringExtras.h"
Chris Lattnered7b41e2003-05-27 15:45:27 +000027
28namespace {
Misha Brukman3cfb6b12003-09-11 16:58:31 +000029 Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up");
30 Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted");
Chris Lattnered7b41e2003-05-27 15:45:27 +000031
32 struct SROA : public FunctionPass {
33 bool runOnFunction(Function &F);
34
Chris Lattner38aec322003-09-11 16:45:55 +000035 bool performScalarRepl(Function &F);
36 bool performPromotion(Function &F);
37
Chris Lattnera15854c2003-08-31 00:45:13 +000038 // getAnalysisUsage - This pass does not require any passes, but we know it
39 // will not alter the CFG, so say so.
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner38aec322003-09-11 16:45:55 +000041 AU.addRequired<DominanceFrontier>();
42 AU.addRequired<TargetData>();
Chris Lattnera15854c2003-08-31 00:45:13 +000043 AU.setPreservesCFG();
44 }
45
Chris Lattnered7b41e2003-05-27 15:45:27 +000046 private:
Chris Lattnerb37923f2003-05-30 19:22:14 +000047 bool isSafeElementUse(Value *Ptr);
Chris Lattner5e062a12003-05-30 04:15:41 +000048 bool isSafeUseOfAllocation(Instruction *User);
49 bool isSafeStructAllocaToPromote(AllocationInst *AI);
50 bool isSafeArrayAllocaToPromote(AllocationInst *AI);
Chris Lattnered7b41e2003-05-27 15:45:27 +000051 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
52 };
53
54 RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
55}
56
57Pass *createScalarReplAggregatesPass() { return new SROA(); }
58
59
Chris Lattnered7b41e2003-05-27 15:45:27 +000060bool SROA::runOnFunction(Function &F) {
Chris Lattnerfe7ea0d2003-09-12 15:36:03 +000061 bool Changed = performPromotion(F);
62 while (1) {
63 bool LocalChange = performScalarRepl(F);
64 if (!LocalChange) break; // No need to repromote if no scalarrepl
65 Changed = true;
66 LocalChange = performPromotion(F);
67 if (!LocalChange) break; // No need to re-scalarrepl if no promotion
68 }
Chris Lattner38aec322003-09-11 16:45:55 +000069
70 return Changed;
71}
72
73
74bool SROA::performPromotion(Function &F) {
75 std::vector<AllocaInst*> Allocas;
76 const TargetData &TD = getAnalysis<TargetData>();
77
78 BasicBlock &BB = F.getEntryNode(); // Get the entry node for the function
79
Chris Lattnerfe7ea0d2003-09-12 15:36:03 +000080 bool Changed = false;
Chris Lattner38aec322003-09-11 16:45:55 +000081
82 while (1) {
83 Allocas.clear();
84
85 // Find allocas that are safe to promote, by looking at all instructions in
86 // the entry node
87 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
88 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
89 if (isAllocaPromotable(AI, TD))
90 Allocas.push_back(AI);
91
92 if (Allocas.empty()) break;
93
94 PromoteMemToReg(Allocas, getAnalysis<DominanceFrontier>(), TD);
95 NumPromoted += Allocas.size();
96 Changed = true;
97 }
98
99 return Changed;
100}
101
102
103// performScalarRepl - This algorithm is a simple worklist driven algorithm,
104// which runs on all of the malloc/alloca instructions in the function, removing
105// them if they are only used by getelementptr instructions.
106//
107bool SROA::performScalarRepl(Function &F) {
Chris Lattnered7b41e2003-05-27 15:45:27 +0000108 std::vector<AllocationInst*> WorkList;
109
110 // Scan the entry basic block, adding any alloca's and mallocs to the worklist
111 BasicBlock &BB = F.getEntryNode();
112 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
113 if (AllocationInst *A = dyn_cast<AllocationInst>(I))
114 WorkList.push_back(A);
115
116 // Process the worklist
117 bool Changed = false;
118 while (!WorkList.empty()) {
119 AllocationInst *AI = WorkList.back();
120 WorkList.pop_back();
121
122 // We cannot transform the allocation instruction if it is an array
Chris Lattnerd10376b2003-05-27 16:09:27 +0000123 // allocation (allocations OF arrays are ok though), and an allocation of a
124 // scalar value cannot be decomposed at all.
125 //
Chris Lattnered7b41e2003-05-27 15:45:27 +0000126 if (AI->isArrayAllocation() ||
Chris Lattnerd10376b2003-05-27 16:09:27 +0000127 (!isa<StructType>(AI->getAllocatedType()) &&
128 !isa<ArrayType>(AI->getAllocatedType()))) continue;
129
Chris Lattner5e062a12003-05-30 04:15:41 +0000130 // Check that all of the users of the allocation are capable of being
131 // transformed.
132 if (isa<StructType>(AI->getAllocatedType())) {
133 if (!isSafeStructAllocaToPromote(AI))
134 continue;
135 } else if (!isSafeArrayAllocaToPromote(AI))
136 continue;
Chris Lattnered7b41e2003-05-27 15:45:27 +0000137
138 DEBUG(std::cerr << "Found inst to xform: " << *AI);
139 Changed = true;
140
141 std::vector<AllocaInst*> ElementAllocas;
142 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
143 ElementAllocas.reserve(ST->getNumContainedTypes());
144 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
145 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
146 AI->getName() + "." + utostr(i), AI);
147 ElementAllocas.push_back(NA);
148 WorkList.push_back(NA); // Add to worklist for recursive processing
149 }
150 } else {
Chris Lattner5e062a12003-05-30 04:15:41 +0000151 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
Chris Lattnered7b41e2003-05-27 15:45:27 +0000152 ElementAllocas.reserve(AT->getNumElements());
153 const Type *ElTy = AT->getElementType();
154 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
155 AllocaInst *NA = new AllocaInst(ElTy, 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 }
161
162 // Now that we have created the alloca instructions that we want to use,
163 // expand the getelementptr instructions to use them.
164 //
165 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
166 I != E; ++I) {
167 Instruction *User = cast<Instruction>(*I);
168 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
169 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
Chris Lattnerc07736a2003-07-23 15:22:26 +0000170 uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue();
Chris Lattnered7b41e2003-05-27 15:45:27 +0000171
172 assert(Idx < ElementAllocas.size() && "Index out of range?");
173 AllocaInst *AllocaToUse = ElementAllocas[Idx];
174
175 Value *RepValue;
176 if (GEPI->getNumOperands() == 3) {
177 // Do not insert a new getelementptr instruction with zero indices,
178 // only to have it optimized out later.
179 RepValue = AllocaToUse;
180 } else {
181 // We are indexing deeply into the structure, so we still need a
182 // getelement ptr instruction to finish the indexing. This may be
183 // expanded itself once the worklist is rerun.
184 //
185 std::string OldName = GEPI->getName(); // Steal the old name...
Chris Lattner261d6862003-05-30 05:26:30 +0000186 std::vector<Value*> NewArgs;
187 NewArgs.push_back(Constant::getNullValue(Type::LongTy));
188 NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end());
Chris Lattnered7b41e2003-05-27 15:45:27 +0000189 GEPI->setName("");
190 RepValue =
Chris Lattner261d6862003-05-30 05:26:30 +0000191 new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI);
Chris Lattnered7b41e2003-05-27 15:45:27 +0000192 }
193
194 // Move all of the users over to the new GEP.
195 GEPI->replaceAllUsesWith(RepValue);
196 // Delete the old GEP
197 GEPI->getParent()->getInstList().erase(GEPI);
198 } else {
199 assert(0 && "Unexpected instruction type!");
200 }
201 }
202
203 // Finally, delete the Alloca instruction
204 AI->getParent()->getInstList().erase(AI);
Chris Lattnerd10376b2003-05-27 16:09:27 +0000205 NumReplaced++;
Chris Lattnered7b41e2003-05-27 15:45:27 +0000206 }
207
208 return Changed;
209}
Chris Lattner5e062a12003-05-30 04:15:41 +0000210
211
212/// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
213/// aggregate allocation.
214///
215bool SROA::isSafeUseOfAllocation(Instruction *User) {
216 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
217 // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
218 if (GEPI->getNumOperands() <= 2 ||
219 GEPI->getOperand(1) != Constant::getNullValue(Type::LongTy) ||
220 !isa<Constant>(GEPI->getOperand(2)) ||
221 isa<ConstantExpr>(GEPI->getOperand(2)))
222 return false;
223 } else {
224 return false;
225 }
226 return true;
227}
228
Chris Lattnerb37923f2003-05-30 19:22:14 +0000229/// isSafeElementUse - Check to see if this use is an allowed use for a
Chris Lattner5e062a12003-05-30 04:15:41 +0000230/// getelementptr instruction of an array aggregate allocation.
231///
Chris Lattnerb37923f2003-05-30 19:22:14 +0000232bool SROA::isSafeElementUse(Value *Ptr) {
Chris Lattner5e062a12003-05-30 04:15:41 +0000233 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
234 I != E; ++I) {
235 Instruction *User = cast<Instruction>(*I);
236 switch (User->getOpcode()) {
Chris Lattner8fce16e2003-09-12 16:02:12 +0000237 case Instruction::Load: break;
238 case Instruction::Store:
239 // Store is ok if storing INTO the pointer, not storing the pointer
240 if (User->getOperand(0) == Ptr) return false;
241 break;
Chris Lattner5e062a12003-05-30 04:15:41 +0000242 case Instruction::GetElementPtr: {
243 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User);
244 if (GEP->getNumOperands() > 1) {
245 if (!isa<Constant>(GEP->getOperand(1)) ||
246 !cast<Constant>(GEP->getOperand(1))->isNullValue())
247 return false; // Using pointer arithmetic to navigate the array...
Chris Lattner5e062a12003-05-30 04:15:41 +0000248 }
Chris Lattner8fce16e2003-09-12 16:02:12 +0000249 if (!isSafeElementUse(GEP)) return false;
250 break;
Chris Lattner5e062a12003-05-30 04:15:41 +0000251 }
252 default:
253 DEBUG(std::cerr << " Transformation preventing inst: " << *User);
254 return false;
255 }
256 }
257 return true; // All users look ok :)
258}
259
260
261/// isSafeStructAllocaToPromote - Check to see if the specified allocation of a
262/// structure can be broken down into elements.
263///
264bool SROA::isSafeStructAllocaToPromote(AllocationInst *AI) {
265 // Loop over the use list of the alloca. We can only transform it if all of
266 // the users are safe to transform.
267 //
268 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
Chris Lattner26d2ca12003-05-30 18:09:57 +0000269 I != E; ++I) {
Chris Lattner5e062a12003-05-30 04:15:41 +0000270 if (!isSafeUseOfAllocation(cast<Instruction>(*I))) {
271 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
272 << *I);
273 return false;
274 }
Chris Lattner26d2ca12003-05-30 18:09:57 +0000275
276 // Pedantic check to avoid breaking broken programs...
277 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*I))
Chris Lattnerb37923f2003-05-30 19:22:14 +0000278 if (GEPI->getNumOperands() == 3 && !isSafeElementUse(GEPI))
Chris Lattner26d2ca12003-05-30 18:09:57 +0000279 return false;
280 }
Chris Lattner5e062a12003-05-30 04:15:41 +0000281 return true;
282}
283
284
285/// isSafeArrayAllocaToPromote - Check to see if the specified allocation of a
286/// structure can be broken down into elements.
287///
288bool SROA::isSafeArrayAllocaToPromote(AllocationInst *AI) {
289 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
290 int64_t NumElements = AT->getNumElements();
291
292 // Loop over the use list of the alloca. We can only transform it if all of
293 // the users are safe to transform. Array allocas have extra constraints to
294 // meet though.
295 //
296 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
297 I != E; ++I) {
298 Instruction *User = cast<Instruction>(*I);
299 if (!isSafeUseOfAllocation(User)) {
300 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
301 << User);
302 return false;
303 }
304
305 // Check to make sure that getelementptr follow the extra rules for arrays:
306 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
307 // Check to make sure that index falls within the array. If not,
308 // something funny is going on, so we won't do the optimization.
309 //
310 if (cast<ConstantSInt>(GEPI->getOperand(2))->getValue() >= NumElements)
311 return false;
312
313 // Check to make sure that the only thing that uses the resultant pointer
314 // is safe for an array access. For example, code that looks like:
315 // P = &A[0]; P = P + 1
316 // is legal, and should prevent promotion.
317 //
Chris Lattnerb37923f2003-05-30 19:22:14 +0000318 if (!isSafeElementUse(GEPI)) {
Chris Lattner5e062a12003-05-30 04:15:41 +0000319 DEBUG(std::cerr << "Cannot transform: " << *AI
320 << " due to uses of user: " << *GEPI);
321 return false;
322 }
323 }
324 }
325 return true;
326}
327