ScalarReplAggregates.cpp revision 8430a4545c2743568aee94c39e3912795ce463ec
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/Support/GetElementPtrTypeIterator.h"
30#include "llvm/Target/TargetData.h"
31#include "llvm/Transforms/Utils/PromoteMemToReg.h"
32#include "Support/Debug.h"
33#include "Support/Statistic.h"
34#include "Support/StringExtras.h"
35using namespace llvm;
36
37namespace {
38  Statistic<> NumReplaced("scalarrepl", "Number of allocas broken up");
39  Statistic<> NumPromoted("scalarrepl", "Number of allocas promoted");
40
41  struct SROA : public FunctionPass {
42    bool runOnFunction(Function &F);
43
44    bool performScalarRepl(Function &F);
45    bool performPromotion(Function &F);
46
47    // 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 {
50      AU.addRequired<DominatorTree>();
51      AU.addRequired<DominanceFrontier>();
52      AU.addRequired<TargetData>();
53      AU.setPreservesCFG();
54    }
55
56  private:
57    bool isSafeElementUse(Value *Ptr);
58    bool isSafeUseOfAllocation(Instruction *User);
59    bool isSafeAllocaToPromote(AllocationInst *AI);
60    AllocaInst *AddNewAlloca(Function &F, const Type *Ty, AllocationInst *Base);
61  };
62
63  RegisterOpt<SROA> X("scalarrepl", "Scalar Replacement of Aggregates");
64}
65
66// Public interface to the ScalarReplAggregates pass
67Pass *llvm::createScalarReplAggregatesPass() { return new SROA(); }
68
69
70bool SROA::runOnFunction(Function &F) {
71  bool Changed = performPromotion(F);
72  while (1) {
73    bool LocalChange = performScalarRepl(F);
74    if (!LocalChange) break;   // No need to repromote if no scalarrepl
75    Changed = true;
76    LocalChange = performPromotion(F);
77    if (!LocalChange) break;   // No need to re-scalarrepl if no promotion
78  }
79
80  return Changed;
81}
82
83
84bool SROA::performPromotion(Function &F) {
85  std::vector<AllocaInst*> Allocas;
86  const TargetData &TD = getAnalysis<TargetData>();
87  DominatorTree     &DT = getAnalysis<DominatorTree>();
88  DominanceFrontier &DF = getAnalysis<DominanceFrontier>();
89
90  BasicBlock &BB = F.getEntryBlock();  // Get the entry node for the function
91
92  bool Changed = false;
93
94  while (1) {
95    Allocas.clear();
96
97    // Find allocas that are safe to promote, by looking at all instructions in
98    // the entry node
99    for (BasicBlock::iterator I = BB.begin(), E = --BB.end(); I != E; ++I)
100      if (AllocaInst *AI = dyn_cast<AllocaInst>(I))       // Is it an alloca?
101        if (isAllocaPromotable(AI, TD))
102          Allocas.push_back(AI);
103
104    if (Allocas.empty()) break;
105
106    PromoteMemToReg(Allocas, DT, DF, TD);
107    NumPromoted += Allocas.size();
108    Changed = true;
109  }
110
111  return Changed;
112}
113
114
115// performScalarRepl - This algorithm is a simple worklist driven algorithm,
116// which runs on all of the malloc/alloca instructions in the function, removing
117// them if they are only used by getelementptr instructions.
118//
119bool SROA::performScalarRepl(Function &F) {
120  std::vector<AllocationInst*> WorkList;
121
122  // Scan the entry basic block, adding any alloca's and mallocs to the worklist
123  BasicBlock &BB = F.getEntryBlock();
124  for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I)
125    if (AllocationInst *A = dyn_cast<AllocationInst>(I))
126      WorkList.push_back(A);
127
128  // Process the worklist
129  bool Changed = false;
130  while (!WorkList.empty()) {
131    AllocationInst *AI = WorkList.back();
132    WorkList.pop_back();
133
134    // We cannot transform the allocation instruction if it is an array
135    // allocation (allocations OF arrays are ok though), and an allocation of a
136    // scalar value cannot be decomposed at all.
137    //
138    if (AI->isArrayAllocation() ||
139        (!isa<StructType>(AI->getAllocatedType()) &&
140         !isa<ArrayType>(AI->getAllocatedType()))) continue;
141
142    // Check that all of the users of the allocation are capable of being
143    // transformed.
144    if (!isSafeAllocaToPromote(AI))
145      continue;
146
147    DEBUG(std::cerr << "Found inst to xform: " << *AI);
148    Changed = true;
149
150    std::vector<AllocaInst*> ElementAllocas;
151    if (const StructType *ST = dyn_cast<StructType>(AI->getAllocatedType())) {
152      ElementAllocas.reserve(ST->getNumContainedTypes());
153      for (unsigned i = 0, e = ST->getNumContainedTypes(); i != e; ++i) {
154        AllocaInst *NA = new AllocaInst(ST->getContainedType(i), 0,
155                                        AI->getName() + "." + utostr(i), AI);
156        ElementAllocas.push_back(NA);
157        WorkList.push_back(NA);  // Add to worklist for recursive processing
158      }
159    } else {
160      const ArrayType *AT = cast<ArrayType>(AI->getAllocatedType());
161      ElementAllocas.reserve(AT->getNumElements());
162      const Type *ElTy = AT->getElementType();
163      for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
164        AllocaInst *NA = new AllocaInst(ElTy, 0,
165                                        AI->getName() + "." + utostr(i), AI);
166        ElementAllocas.push_back(NA);
167        WorkList.push_back(NA);  // Add to worklist for recursive processing
168      }
169    }
170
171    // Now that we have created the alloca instructions that we want to use,
172    // expand the getelementptr instructions to use them.
173    //
174    while (!AI->use_empty()) {
175      Instruction *User = cast<Instruction>(AI->use_back());
176      if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(User)) {
177        // We now know that the GEP is of the form: GEP <ptr>, 0, <cst>
178        uint64_t Idx = cast<ConstantInt>(GEPI->getOperand(2))->getRawValue();
179
180        assert(Idx < ElementAllocas.size() && "Index out of range?");
181        AllocaInst *AllocaToUse = ElementAllocas[Idx];
182
183        Value *RepValue;
184        if (GEPI->getNumOperands() == 3) {
185          // Do not insert a new getelementptr instruction with zero indices,
186          // only to have it optimized out later.
187          RepValue = AllocaToUse;
188        } else {
189          // We are indexing deeply into the structure, so we still need a
190          // getelement ptr instruction to finish the indexing.  This may be
191          // expanded itself once the worklist is rerun.
192          //
193          std::string OldName = GEPI->getName();  // Steal the old name...
194          std::vector<Value*> NewArgs;
195          NewArgs.push_back(Constant::getNullValue(Type::IntTy));
196          NewArgs.insert(NewArgs.end(), GEPI->op_begin()+3, GEPI->op_end());
197          GEPI->setName("");
198          RepValue =
199            new GetElementPtrInst(AllocaToUse, NewArgs, OldName, GEPI);
200        }
201
202        // Move all of the users over to the new GEP.
203        GEPI->replaceAllUsesWith(RepValue);
204        // Delete the old GEP
205        GEPI->getParent()->getInstList().erase(GEPI);
206      } else {
207        assert(0 && "Unexpected instruction type!");
208      }
209    }
210
211    // Finally, delete the Alloca instruction
212    AI->getParent()->getInstList().erase(AI);
213    NumReplaced++;
214  }
215
216  return Changed;
217}
218
219
220/// isSafeUseOfAllocation - Check to see if this user is an allowed use for an
221/// aggregate allocation.
222///
223bool SROA::isSafeUseOfAllocation(Instruction *User) {
224  if (!isa<GetElementPtrInst>(User)) return false;
225
226  GetElementPtrInst *GEPI = cast<GetElementPtrInst>(User);
227  gep_type_iterator I = gep_type_begin(GEPI), E = gep_type_end(GEPI);
228
229  // The GEP is safe to transform if it is of the form GEP <ptr>, 0, <cst>
230  if (I == E ||
231      I.getOperand() != Constant::getNullValue(I.getOperand()->getType()))
232    return false;
233
234  ++I;
235  if (I == E || !isa<ConstantInt>(I.getOperand()))
236    return false;
237
238  // If this is a use of an array allocation, do a bit more checking for sanity.
239  if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) {
240    uint64_t NumElements = AT->getNumElements();
241
242    // Check to make sure that index falls within the array.  If not,
243    // something funny is going on, so we won't do the optimization.
244    //
245    if (cast<ConstantInt>(GEPI->getOperand(2))->getRawValue() >= NumElements)
246      return false;
247  }
248
249  // If there are any non-simple uses of this getelementptr, make sure to reject
250  // them.
251  return isSafeElementUse(GEPI);
252}
253
254/// isSafeElementUse - Check to see if this use is an allowed use for a
255/// getelementptr instruction of an array aggregate allocation.
256///
257bool SROA::isSafeElementUse(Value *Ptr) {
258  for (Value::use_iterator I = Ptr->use_begin(), E = Ptr->use_end();
259       I != E; ++I) {
260    Instruction *User = cast<Instruction>(*I);
261    switch (User->getOpcode()) {
262    case Instruction::Load:  break;
263    case Instruction::Store:
264      // Store is ok if storing INTO the pointer, not storing the pointer
265      if (User->getOperand(0) == Ptr) return false;
266      break;
267    case Instruction::GetElementPtr: {
268      GetElementPtrInst *GEP = cast<GetElementPtrInst>(User);
269      if (GEP->getNumOperands() > 1) {
270        if (!isa<Constant>(GEP->getOperand(1)) ||
271            !cast<Constant>(GEP->getOperand(1))->isNullValue())
272          return false;  // Using pointer arithmetic to navigate the array...
273      }
274      if (!isSafeElementUse(GEP)) return false;
275      break;
276    }
277    default:
278      DEBUG(std::cerr << "  Transformation preventing inst: " << *User);
279      return false;
280    }
281  }
282  return true;  // All users look ok :)
283}
284
285
286/// isSafeStructAllocaToPromote - Check to see if the specified allocation of a
287/// structure can be broken down into elements.
288///
289bool SROA::isSafeAllocaToPromote(AllocationInst *AI) {
290  // Loop over the use list of the alloca.  We can only transform it if all of
291  // the users are safe to transform.
292  //
293  for (Value::use_iterator I = AI->use_begin(), E = AI->use_end();
294       I != E; ++I)
295    if (!isSafeUseOfAllocation(cast<Instruction>(*I))) {
296      DEBUG(std::cerr << "Cannot transform: " << *AI << "  due to user: "
297                      << *I);
298      return false;
299    }
300  return true;
301}
302