1 //===- ScalarReplAggregates.cpp - Scalar Replacement of Aggregates --------===//
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
6 // each member (if possible).
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/Function.h"
12 #include "llvm/Pass.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/Constants.h"
16 #include "Support/StringExtras.h"
17 #include "Support/Statistic.h"
20 Statistic<> NumReplaced("scalarrepl", "Number of alloca's broken up");
22 struct SROA : public FunctionPass {
23 bool runOnFunction(Function &F);
26 bool isSafeArrayElementUse(Value *Ptr);
27 bool isSafeUseOfAllocation(Instruction *User);
28 bool isSafeStructAllocaToPromote(AllocationInst *AI);
29 bool isSafeArrayAllocaToPromote(AllocationInst *AI);
30 AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
33 RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
36 Pass *createScalarReplAggregatesPass() { return new SROA(); }
39 // runOnFunction - This algorithm is a simple worklist driven algorithm, which
40 // runs on all of the malloc/alloca instructions in the function, removing them
41 // if they are only used by getelementptr instructions.
43 bool SROA::runOnFunction(Function &F) {
44 std::vector<AllocationInst*> WorkList;
46 // Scan the entry basic block, adding any alloca's and mallocs to the worklist
47 BasicBlock &BB = F.getEntryNode();
48 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
49 if (AllocationInst *A = dyn_cast<AllocationInst>(I))
50 WorkList.push_back(A);
52 // Process the worklist
54 while (!WorkList.empty()) {
55 AllocationInst *AI = WorkList.back();
58 // We cannot transform the allocation instruction if it is an array
59 // allocation (allocations OF arrays are ok though), and an allocation of a
60 // scalar value cannot be decomposed at all.
62 if (AI->isArrayAllocation() ||
63 (!isa<StructType>(AI->getAllocatedType()) &&
64 !isa<ArrayType>(AI->getAllocatedType()))) continue;
66 // Check that all of the users of the allocation are capable of being
68 if (isa<StructType>(AI->getAllocatedType())) {
69 if (!isSafeStructAllocaToPromote(AI))
71 } else if (!isSafeArrayAllocaToPromote(AI))
74 DEBUG(std::cerr << "Found inst to xform: " << *AI);
77 std::vector<AllocaInst*> ElementAllocas;
78 if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
79 ElementAllocas.reserve(ST->getNumContainedTypes());
80 for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
81 AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
82 AI->getName() + "." + utostr(i), AI);
83 ElementAllocas.push_back(NA);
84 WorkList.push_back(NA); // Add to worklist for recursive processing
87 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
88 ElementAllocas.reserve(AT->getNumElements());
89 const Type *ElTy = AT->getElementType();
90 for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
91 AllocaInst *NA = new AllocaInst(ElTy, 0,
92 AI->getName() + "." + utostr(i), AI);
93 ElementAllocas.push_back(NA);
94 WorkList.push_back(NA); // Add to worklist for recursive processing
98 // Now that we have created the alloca instructions that we want to use,
99 // expand the getelementptr instructions to use them.
101 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
103 Instruction *User = cast<Instruction>(*I);
104 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
105 // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
107 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(GEPI->getOperand(2)))
108 Idx = CSI->getValue();
110 Idx = cast<ConstantUInt>(GEPI->getOperand(2))->getValue();
112 assert(Idx < ElementAllocas.size() && "Index out of range?");
113 AllocaInst *AllocaToUse = ElementAllocas[Idx];
116 if (GEPI->getNumOperands() == 3) {
117 // Do not insert a new getelementptr instruction with zero indices,
118 // only to have it optimized out later.
119 RepValue = AllocaToUse;
121 // We are indexing deeply into the structure, so we still need a
122 // getelement ptr instruction to finish the indexing. This may be
123 // expanded itself once the worklist is rerun.
125 std::string OldName = GEPI->getName(); // Steal the old name...
126 std::vector<Value*> NewArgs;
127 NewArgs.push_back(Constant::getNullValue(Type::LongTy));
128 NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end());
131 new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI);
134 // Move all of the users over to the new GEP.
135 GEPI->replaceAllUsesWith(RepValue);
136 // Delete the old GEP
137 GEPI->getParent()->getInstList().erase(GEPI);
139 assert(0 && "Unexpected instruction type!");
143 // Finally, delete the Alloca instruction
144 AI->getParent()->getInstList().erase(AI);
152 /// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
153 /// aggregate allocation.
155 bool SROA::isSafeUseOfAllocation(Instruction *User) {
156 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
157 // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
158 if (GEPI->getNumOperands() <= 2 ||
159 GEPI->getOperand(1) != Constant::getNullValue(Type::LongTy) ||
160 !isa<Constant>(GEPI->getOperand(2)) ||
161 isa<ConstantExpr>(GEPI->getOperand(2)))
170 /// isSafeArrayElementUse - Check to see if this use is an allowed use for a
171 /// getelementptr instruction of an array aggregate allocation.
173 bool SROA::isSafeArrayElementUse(Value *Ptr) {
174 for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
176 Instruction *User = cast<Instruction>(*I);
177 switch (User->getOpcode()) {
178 case Instruction::Load: return true;
179 case Instruction::Store: return User->getOperand(0) != Ptr;
180 case Instruction::GetElementPtr: {
181 GetElementPtrInst *GEP = cast<GetElementPtrInst>(User);
182 if (GEP->getNumOperands() > 1) {
183 if (!isa<Constant>(GEP->getOperand(1)) ||
184 !cast<Constant>(GEP->getOperand(1))->isNullValue())
185 return false; // Using pointer arithmetic to navigate the array...
187 // Check to see if there are any structure indexes involved in this GEP.
188 // If so, then we can safely break the array up until at least the
190 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
191 if (GEP->getOperand(i)->getType()->isUnsigned())
194 return isSafeArrayElementUse(GEP);
197 DEBUG(std::cerr << " Transformation preventing inst: " << *User);
201 return true; // All users look ok :)
205 /// isSafeStructAllocaToPromote - Check to see if the specified allocation of a
206 /// structure can be broken down into elements.
208 bool SROA::isSafeStructAllocaToPromote(AllocationInst *AI) {
209 // Loop over the use list of the alloca. We can only transform it if all of
210 // the users are safe to transform.
212 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
214 if (!isSafeUseOfAllocation(cast<Instruction>(*I))) {
215 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
223 /// isSafeArrayAllocaToPromote - Check to see if the specified allocation of a
224 /// structure can be broken down into elements.
226 bool SROA::isSafeArrayAllocaToPromote(AllocationInst *AI) {
227 const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
228 int64_t NumElements = AT->getNumElements();
230 // Loop over the use list of the alloca. We can only transform it if all of
231 // the users are safe to transform. Array allocas have extra constraints to
234 for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
236 Instruction *User = cast<Instruction>(*I);
237 if (!isSafeUseOfAllocation(User)) {
238 DEBUG(std::cerr << "Cannot transform: " << *AI << " due to user: "
243 // Check to make sure that getelementptr follow the extra rules for arrays:
244 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
245 // Check to make sure that index falls within the array. If not,
246 // something funny is going on, so we won't do the optimization.
248 if (cast<ConstantSInt>(GEPI->getOperand(2))->getValue() >= NumElements)
251 // Check to make sure that the only thing that uses the resultant pointer
252 // is safe for an array access. For example, code that looks like:
253 // P = &A[0]; P = P + 1
254 // is legal, and should prevent promotion.
256 if (!isSafeArrayElementUse(GEPI)) {
257 DEBUG(std::cerr << "Cannot transform: " << *AI
258 << " due to uses of user: " << *GEPI);