blob: 4164bc94cc6e609a48a8c956f164f96304d19e07 [file] [log] [blame]
Chris Lattnerfb41a502003-05-27 15:45:27 +00001//===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
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 Lattnerfb41a502003-05-27 15:45:27 +00009//
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 Lattner5d8a12e2003-09-11 16:45:55 +000013// 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 Lattnerfb41a502003-05-27 15:45:27 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "llvm/Transforms/Scalar.h"
Chris Lattner5d8a12e2003-09-11 16:45:55 +000023#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
Chris Lattnerfb41a502003-05-27 15:45:27 +000025#include "llvm/Function.h"
26#include "llvm/Pass.h"
Misha Brukman2b3387a2004-07-29 17:05:13 +000027#include "llvm/Instructions.h"
Chris Lattner5d8a12e2003-09-11 16:45:55 +000028#include "llvm/Analysis/Dominators.h"
Chris Lattner52310702003-11-25 21:09:18 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner5d8a12e2003-09-11 16:45:55 +000030#include "llvm/Target/TargetData.h"
31#include "llvm/Transforms/Utils/PromoteMemToReg.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/Support/Debug.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/StringExtras.h"
Chris Lattner40d2aeb2003-12-02 17:43:55 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattnerfb41a502003-05-27 15:45:27 +000037namespace {
Misha Brukman217ca0b2003-09-11 16:58:31 +000038 Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up");
39 Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted");
Chris Lattnerfb41a502003-05-27 15:45:27 +000040
41 struct SROA : public FunctionPass {
42 bool runOnFunction(Function &F);
43
Chris Lattner5d8a12e2003-09-11 16:45:55 +000044 bool performScalarRepl(Function &F);
45 bool performPromotion(Function &F);
46
Chris Lattnerc8174582003-08-31 00:45:13 +000047 // 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 Lattnera906bac2003-10-05 21:20:13 +000050 AU.addRequired<DominatorTree>();
Chris Lattner5d8a12e2003-09-11 16:45:55 +000051 AU.addRequired<DominanceFrontier>();
52 AU.addRequired<TargetData>();
Chris Lattnerc8174582003-08-31 00:45:13 +000053 AU.setPreservesCFG();
54 }
55
Chris Lattnerfb41a502003-05-27 15:45:27 +000056 private:
Chris Lattner88819122004-11-14 04:24:28 +000057 int isSafeElementUse(Value *Ptr);
58 int isSafeUseOfAllocation(Instruction *User);
59 int isSafeAllocaToScalarRepl(AllocationInst *AI);
60 void CanonicalizeAllocaUsers(AllocationInst *AI);
Chris Lattnerfb41a502003-05-27 15:45:27 +000061 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
62 };
63
64 RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
65}
66
Brian Gaeke960707c2003-11-11 22:41:34 +000067// Public interface to the ScalarReplAggregates pass
Chris Lattner3e860842004-09-20 04:43:15 +000068FunctionPass *llvm::createScalarReplAggregatesPass() { return new SROA(); }
Chris Lattnerfb41a502003-05-27 15:45:27 +000069
70
Chris Lattnerfb41a502003-05-27 15:45:27 +000071bool SROA::runOnFunction(Function &F) {
Chris Lattner9a95f2a2003-09-12 15:36:03 +000072 bool Changed = performPromotion(F);
73 while (1) {
74 bool LocalChange = performScalarRepl(F);
75 if (!LocalChange) break; // No need to repromote if no scalarrepl
76 Changed = true;
77 LocalChange = performPromotion(F);
78 if (!LocalChange) break; // No need to re-scalarrepl if no promotion
79 }
Chris Lattner5d8a12e2003-09-11 16:45:55 +000080
81 return Changed;
82}
83
84
85bool SROA::performPromotion(Function &F) {
86 std::vector<AllocaInst*> Allocas;
87 const TargetData &TD = getAnalysis<TargetData>();
Chris Lattnera906bac2003-10-05 21:20:13 +000088 DominatorTree &DT = getAnalysis<DominatorTree>();
89 DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
Chris Lattner5d8a12e2003-09-11 16:45:55 +000090
Chris Lattner5dac64f2003-09-20 14:39:18 +000091 BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
Chris Lattner5d8a12e2003-09-11 16:45:55 +000092
Chris Lattner9a95f2a2003-09-12 15:36:03 +000093 bool Changed = false;
Chris Lattner5d8a12e2003-09-11 16:45:55 +000094
95 while (1) {
96 Allocas.clear();
97
98 // Find allocas that are safe to promote, by looking at all instructions in
99 // the entry node
100 for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
101 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) // Is it an alloca?
102 if (isAllocaPromotable(AI, TD))
103 Allocas.push_back(AI);
104
105 if (Allocas.empty()) break;
106
Chris Lattnera906bac2003-10-05 21:20:13 +0000107 PromoteMemToReg(Allocas, DT, DF, TD);
Chris Lattner5d8a12e2003-09-11 16:45:55 +0000108 NumPromoted += Allocas.size();
109 Changed = true;
110 }
111
112 return Changed;
113}
114
115
116// performScalarRepl - This algorithm is a simple worklist driven algorithm,
117// which runs on all of the malloc/alloca instructions in the function, removing
118// them if they are only used by getelementptr instructions.
119//
120bool SROA::performScalarRepl(Function &F) {
Chris Lattnerfb41a502003-05-27 15:45:27 +0000121 std::vector<AllocationInst*> WorkList;
122
123 // Scan the entry basic block, adding any alloca's and mallocs to the worklist
Chris Lattner5dac64f2003-09-20 14:39:18 +0000124 BasicBlock &BB = F.getEntryBlock();
Chris Lattnerfb41a502003-05-27 15:45:27 +0000125 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
126 if (AllocationInst *A = dyn_cast<AllocationInst>(I))
127 WorkList.push_back(A);
128
129 // Process the worklist
130 bool Changed = false;
131 while (!WorkList.empty()) {
132 AllocationInst *AI = WorkList.back();
133 WorkList.pop_back();
134
135 // We cannot transform the allocation instruction if it is an array
Chris Lattnerc16b2102003-05-27 16:09:27 +0000136 // allocation (allocations OF arrays are ok though), and an allocation of a
137 // scalar value cannot be decomposed at all.
138 //
Chris Lattnerfb41a502003-05-27 15:45:27 +0000139 if (AI->isArrayAllocation() ||
Chris Lattnerc16b2102003-05-27 16:09:27 +0000140 (!isa<StructType>(AI->getAllocatedType()) &&
141 !isa<ArrayType>(AI->getAllocatedType()))) continue;
142
Chris Lattner6e5398d2003-05-30 04:15:41 +0000143 // Check that all of the users of the allocation are capable of being
144 // transformed.
Chris Lattner88819122004-11-14 04:24:28 +0000145 switch (isSafeAllocaToScalarRepl(AI)) {
146 default: assert(0 && "Unexpected value!");
147 case 0: // Not safe to scalar replace.
Chris Lattner6e5398d2003-05-30 04:15:41 +0000148 continue;
Chris Lattner88819122004-11-14 04:24:28 +0000149 case 1: // Safe, but requires cleanup/canonicalizations first
150 CanonicalizeAllocaUsers(AI);
151 case 3: // Safe to scalar replace.
152 break;
153 }
Chris Lattnerfb41a502003-05-27 15:45:27 +0000154
155 DEBUG(std::cerr << "Found inst to xform: " << *AI);
156 Changed = true;
157
158 std::vector<AllocaInst*> ElementAllocas;
159 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
160 ElementAllocas.reserve(ST->getNumContainedTypes());
161 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
162 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
163 AI->getName() + "." + utostr(i), AI);
164 ElementAllocas.push_back(NA);
165 WorkList.push_back(NA); // Add to worklist for recursive processing
166 }
167 } else {
Chris Lattner6e5398d2003-05-30 04:15:41 +0000168 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
Chris Lattnerfb41a502003-05-27 15:45:27 +0000169 ElementAllocas.reserve(AT->getNumElements());
170 const Type *ElTy = AT->getElementType();
171 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
172 AllocaInst *NA = new AllocaInst(ElTy, 0,
173 AI->getName() + "." + utostr(i), AI);
174 ElementAllocas.push_back(NA);
175 WorkList.push_back(NA); // Add to worklist for recursive processing
176 }
177 }
178
179 // Now that we have created the alloca instructions that we want to use,
180 // expand the getelementptr instructions to use them.
181 //
Chris Lattnerb5f8eb82004-06-19 02:02:22 +0000182 while (!AI->use_empty()) {
183 Instruction *User = cast<Instruction>(AI->use_back());
Chris Lattnerfb41a502003-05-27 15:45:27 +0000184 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
185 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
Chris Lattner6077c312003-07-23 15:22:26 +0000186 uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue();
Chris Lattnerfb41a502003-05-27 15:45:27 +0000187
188 assert(Idx < ElementAllocas.size() && "Index out of range?");
189 AllocaInst *AllocaToUse = ElementAllocas[Idx];
190
191 Value *RepValue;
192 if (GEPI->getNumOperands() == 3) {
193 // Do not insert a new getelementptr instruction with zero indices,
194 // only to have it optimized out later.
195 RepValue = AllocaToUse;
196 } else {
197 // We are indexing deeply into the structure, so we still need a
198 // getelement ptr instruction to finish the indexing. This may be
199 // expanded itself once the worklist is rerun.
200 //
201 std::string OldName = GEPI->getName(); // Steal the old name...
Chris Lattner38d88c02003-05-30 05:26:30 +0000202 std::vector<Value*> NewArgs;
Chris Lattner69193f92004-04-05 01:30:19 +0000203 NewArgs.push_back(Constant::getNullValue(Type::IntTy));
Chris Lattner38d88c02003-05-30 05:26:30 +0000204 NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end());
Chris Lattnerfb41a502003-05-27 15:45:27 +0000205 GEPI->setName("");
206 RepValue =
Chris Lattner38d88c02003-05-30 05:26:30 +0000207 new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI);
Chris Lattnerfb41a502003-05-27 15:45:27 +0000208 }
209
210 // Move all of the users over to the new GEP.
211 GEPI->replaceAllUsesWith(RepValue);
212 // Delete the old GEP
213 GEPI->getParent()->getInstList().erase(GEPI);
214 } else {
215 assert(0 && "Unexpected instruction type!");
216 }
217 }
218
219 // Finally, delete the Alloca instruction
220 AI->getParent()->getInstList().erase(AI);
Chris Lattnerc16b2102003-05-27 16:09:27 +0000221 NumReplaced++;
Chris Lattnerfb41a502003-05-27 15:45:27 +0000222 }
223
224 return Changed;
225}
Chris Lattner6e5398d2003-05-30 04:15:41 +0000226
227
Chris Lattner88819122004-11-14 04:24:28 +0000228/// isSafeElementUse - Check to see if this use is an allowed use for a
229/// getelementptr instruction of an array aggregate allocation.
230///
231int SROA::isSafeElementUse(Value *Ptr) {
232 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
233 I != E; ++I) {
234 Instruction *User = cast<Instruction>(*I);
235 switch (User->getOpcode()) {
236 case Instruction::Load: break;
237 case Instruction::Store:
238 // Store is ok if storing INTO the pointer, not storing the pointer
239 if (User->getOperand(0) == Ptr) return 0;
240 break;
241 case Instruction::GetElementPtr: {
242 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User);
243 if (GEP->getNumOperands() > 1) {
244 if (!isa<Constant>(GEP->getOperand(1)) ||
245 !cast<Constant>(GEP->getOperand(1))->isNullValue())
246 return 0; // Using pointer arithmetic to navigate the array...
247 }
248 if (!isSafeElementUse(GEP)) return 0;
249 break;
250 }
251 default:
252 DEBUG(std::cerr << " Transformation preventing inst: " << *User);
253 return 0;
254 }
255 }
256 return 3; // All users look ok :)
257}
258
Chris Lattner6e5398d2003-05-30 04:15:41 +0000259/// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
260/// aggregate allocation.
261///
Chris Lattner88819122004-11-14 04:24:28 +0000262int SROA::isSafeUseOfAllocation(Instruction *User) {
263 if (!isa<GetElementPtrInst>(User)) return 0;
Chris Lattner52310702003-11-25 21:09:18 +0000264
265 GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User);
266 gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI);
267
268 // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
269 if (I == E ||
270 I.getOperand() != Constant::getNullValue(I.getOperand()->getType()))
Chris Lattner88819122004-11-14 04:24:28 +0000271 return 0;
Chris Lattner52310702003-11-25 21:09:18 +0000272
273 ++I;
Chris Lattner40d2aeb2003-12-02 17:43:55 +0000274 if (I == E || !isa<ConstantInt>(I.getOperand()))
Chris Lattner88819122004-11-14 04:24:28 +0000275 return 0;
Chris Lattner52310702003-11-25 21:09:18 +0000276
277 // If this is a use of an array allocation, do a bit more checking for sanity.
278 if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
279 uint64_t NumElements = AT->getNumElements();
280
281 // Check to make sure that index falls within the array. If not,
282 // something funny is going on, so we won't do the optimization.
283 //
284 if (cast<ConstantInt>(GEPI->getOperand(2))->getRawValue() >= NumElements)
Chris Lattner88819122004-11-14 04:24:28 +0000285 return 0;
Chris Lattner6e5398d2003-05-30 04:15:41 +0000286 }
Chris Lattner52310702003-11-25 21:09:18 +0000287
288 // If there are any non-simple uses of this getelementptr, make sure to reject
289 // them.
290 return isSafeElementUse(GEPI);
Chris Lattner6e5398d2003-05-30 04:15:41 +0000291}
292
Chris Lattner88819122004-11-14 04:24:28 +0000293/// isSafeStructAllocaToScalarRepl - Check to see if the specified allocation of
294/// an aggregate can be broken down into elements. Return 0 if not, 3 if safe,
295/// or 1 if safe after canonicalization has been performed.
Chris Lattner6e5398d2003-05-30 04:15:41 +0000296///
Chris Lattner88819122004-11-14 04:24:28 +0000297int SROA::isSafeAllocaToScalarRepl(AllocationInst *AI) {
Chris Lattner6e5398d2003-05-30 04:15:41 +0000298 // Loop over the use list of the alloca. We can only transform it if all of
299 // the users are safe to transform.
300 //
Chris Lattner88819122004-11-14 04:24:28 +0000301 int isSafe = 3;
Chris Lattner6e5398d2003-05-30 04:15:41 +0000302 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
Chris Lattner88819122004-11-14 04:24:28 +0000303 I != E; ++I) {
304 isSafe &= isSafeUseOfAllocation(cast<Instruction>(*I));
305 if (isSafe == 0) {
Chris Lattner6e5398d2003-05-30 04:15:41 +0000306 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
Chris Lattner88819122004-11-14 04:24:28 +0000307 << **I);
308 return 0;
Chris Lattner6e5398d2003-05-30 04:15:41 +0000309 }
Chris Lattner88819122004-11-14 04:24:28 +0000310 }
311 // If we require cleanup, isSafe is now 1, otherwise it is 3.
312 return isSafe;
313}
314
315/// CanonicalizeAllocaUsers - If SROA reported that it can promote the specified
316/// allocation, but only if cleaned up, perform the cleanups required.
317void SROA::CanonicalizeAllocaUsers(AllocationInst *AI) {
318
319
Chris Lattner6e5398d2003-05-30 04:15:41 +0000320}