Improve pass documentation and comments.
[oota-llvm.git] / lib / Transforms / IPO / StructRetPromotion.cpp
1 //===-- StructRetPromotion.cpp - Promote sret arguments ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass finds functions that return a struct (using a pointer to the struct
11 // as the first argument of the function, marked with the 'sret' attribute) and
12 // replaces them with a new function that simply returns each of the elements of
13 // that struct (using multiple return values).
14 //
15 // This pass works under a number of conditions:
16 //  1. The returned struct must not contain other structs
17 //  2. The returned struct must only be used to load values from
18 //  3. The placeholder struct passed in is the result of an alloca
19 //
20 //===----------------------------------------------------------------------===//
21
22 #define DEBUG_TYPE "sretpromotion"
23 #include "llvm/Transforms/IPO.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Module.h"
27 #include "llvm/CallGraphSCCPass.h"
28 #include "llvm/Instructions.h"
29 #include "llvm/Analysis/CallGraph.h"
30 #include "llvm/Support/CallSite.h"
31 #include "llvm/Support/CFG.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Support/Compiler.h"
37 using namespace llvm;
38
39 STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
40 STATISTIC(NumSRET , "Number of sret promoted");
41 namespace {
42   /// SRETPromotion - This pass removes sret parameter and updates
43   /// function to use multiple return value.
44   ///
45   struct VISIBILITY_HIDDEN SRETPromotion : public CallGraphSCCPass {
46     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47       CallGraphSCCPass::getAnalysisUsage(AU);
48     }
49
50     virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
51     static char ID; // Pass identification, replacement for typeid
52     SRETPromotion() : CallGraphSCCPass((intptr_t)&ID) {}
53
54   private:
55     bool PromoteReturn(CallGraphNode *CGN);
56     bool isSafeToUpdateAllCallers(Function *F);
57     Function *cloneFunctionBody(Function *F, const StructType *STy);
58     void updateCallSites(Function *F, Function *NF);
59     bool nestedStructType(const StructType *STy);
60   };
61
62   char SRETPromotion::ID = 0;
63   RegisterPass<SRETPromotion> X("sretpromotion",
64                                "Promote sret arguments to multiple ret values");
65 }
66
67 Pass *llvm::createStructRetPromotionPass() {
68   return new SRETPromotion();
69 }
70
71 bool SRETPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
72   bool Changed = false;
73
74   for (unsigned i = 0, e = SCC.size(); i != e; ++i)
75     Changed |= PromoteReturn(SCC[i]);
76
77   return Changed;
78 }
79
80 /// PromoteReturn - This method promotes function that uses StructRet paramater 
81 /// into a function that uses mulitple return value.
82 bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
83   Function *F = CGN->getFunction();
84
85   if (!F || F->isDeclaration() || !F->hasInternalLinkage())
86     return false;
87
88   // Make sure that function returns struct.
89   if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
90     return false;
91
92   assert (F->getReturnType() == Type::VoidTy && "Invalid function return type");
93   Function::arg_iterator AI = F->arg_begin();
94   const llvm::PointerType *FArgType = dyn_cast<PointerType>(AI->getType());
95   assert (FArgType && "Invalid sret paramater type");
96   const llvm::StructType *STy = 
97     dyn_cast<StructType>(FArgType->getElementType());
98   assert (STy && "Invalid sret parameter element type");
99
100   if (nestedStructType(STy))
101     return false;
102
103   // Check if it is ok to perform this promotion.
104   if (isSafeToUpdateAllCallers(F) == false) {
105     NumRejectedSRETUses++;
106     return false;
107   }
108
109   NumSRET++;
110   // [1] Replace use of sret parameter 
111   AllocaInst *TheAlloca = new AllocaInst (STy, NULL, "mrv", 
112                                           F->getEntryBlock().begin());
113   Value *NFirstArg = F->arg_begin();
114   NFirstArg->replaceAllUsesWith(TheAlloca);
115
116   // [2] Find and replace ret instructions
117   SmallVector<Value *,4> RetVals;
118   for (Function::iterator FI = F->begin(), FE = F->end();  FI != FE; ++FI) 
119     for(BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
120       Instruction *I = BI;
121       ++BI;
122       if (isa<ReturnInst>(I)) {
123         RetVals.clear();
124         for (unsigned idx = 0; idx < STy->getNumElements(); ++idx) {
125           SmallVector<Value*, 2> GEPIdx;
126           GEPIdx.push_back(ConstantInt::get(Type::Int32Ty, 0));
127           GEPIdx.push_back(ConstantInt::get(Type::Int32Ty, idx));
128           Value *NGEPI = GetElementPtrInst::Create(TheAlloca, GEPIdx.begin(),
129                                                    GEPIdx.end(),
130                                                    "mrv.gep", I);
131           Value *NV = new LoadInst(NGEPI, "mrv.ld", I);
132           RetVals.push_back(NV);
133         }
134     
135         ReturnInst *NR = ReturnInst::Create(&RetVals[0], RetVals.size(), I);
136         I->replaceAllUsesWith(NR);
137         I->eraseFromParent();
138       }
139     }
140
141   // [3] Create the new function body and insert it into the module.
142   Function *NF = cloneFunctionBody(F, STy);
143
144   // [4] Update all call sites to use new function
145   updateCallSites(F, NF);
146
147   F->eraseFromParent();
148   getAnalysis<CallGraph>().changeFunction(F, NF);
149   return true;
150 }
151
152   // Check if it is ok to perform this promotion.
153 bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
154
155   if (F->use_empty())
156     // No users. OK to modify signature.
157     return true;
158
159   for (Value::use_iterator FnUseI = F->use_begin(), FnUseE = F->use_end();
160        FnUseI != FnUseE; ++FnUseI) {
161
162     CallSite CS = CallSite::get(*FnUseI);
163     Instruction *Call = CS.getInstruction();
164     CallSite::arg_iterator AI = CS.arg_begin();
165     Value *FirstArg = *AI;
166
167     if (!isa<AllocaInst>(FirstArg))
168       return false;
169
170     // Check FirstArg's users.
171     for (Value::use_iterator ArgI = FirstArg->use_begin(), 
172            ArgE = FirstArg->use_end(); ArgI != ArgE; ++ArgI) {
173
174       // If FirstArg user is a CallInst that does not correspond to current
175       // call site then this function F is not suitable for sret promotion.
176       if (CallInst *CI = dyn_cast<CallInst>(ArgI)) {
177         if (CI != Call)
178           return false;
179       }
180       // If FirstArg user is a GEP whose all users are not LoadInst then
181       // this function F is not suitable for sret promotion.
182       else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(ArgI)) {
183         // TODO : Use dom info and insert PHINodes to collect get results
184         // from multiple call sites for this GEP.
185         if (GEP->getParent() != Call->getParent())
186           return false;
187         for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
188              GEPI != GEPE; ++GEPI) 
189           if (!isa<LoadInst>(GEPI))
190             return false;
191       } 
192       // Any other FirstArg users make this function unsuitable for sret 
193       // promotion.
194       else
195         return false;
196     }
197   }
198
199   return true;
200 }
201
202 /// cloneFunctionBody - Create a new function based on F and
203 /// insert it into module. Remove first argument. Use STy as
204 /// the return type for new function.
205 Function *SRETPromotion::cloneFunctionBody(Function *F, 
206                                            const StructType *STy) {
207
208   const FunctionType *FTy = F->getFunctionType();
209   std::vector<const Type*> Params;
210
211   // ParamAttrs - Keep track of the parameter attributes for the arguments.
212   SmallVector<ParamAttrsWithIndex, 8> ParamAttrsVec;
213   const PAListPtr &PAL = F->getParamAttrs();
214
215   // Add any return attributes.
216   if (ParameterAttributes attrs = PAL.getParamAttrs(0))
217     ParamAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
218
219   // Skip first argument.
220   Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
221   ++I;
222   // 0th parameter attribute is reserved for return type.
223   // 1th parameter attribute is for first 1st sret argument.
224   unsigned ParamIndex = 2; 
225   while (I != E) {
226     Params.push_back(I->getType());
227     if (ParameterAttributes Attrs = PAL.getParamAttrs(ParamIndex))
228       ParamAttrsVec.push_back(ParamAttrsWithIndex::get(ParamIndex - 1, Attrs));
229     ++I;
230     ++ParamIndex;
231   }
232
233   FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
234   Function *NF = Function::Create(NFTy, F->getLinkage(), F->getName());
235   NF->setCallingConv(F->getCallingConv());
236   NF->setParamAttrs(PAListPtr::get(ParamAttrsVec.begin(), ParamAttrsVec.end()));
237   F->getParent()->getFunctionList().insert(F, NF);
238   NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
239
240   // Replace arguments
241   I = F->arg_begin();
242   E = F->arg_end();
243   Function::arg_iterator NI = NF->arg_begin();
244   ++I;
245   while (I != E) {
246       I->replaceAllUsesWith(NI);
247       NI->takeName(I);
248       ++I;
249       ++NI;
250   }
251
252   return NF;
253 }
254
255 /// updateCallSites - Update all sites that call F to use NF.
256 void SRETPromotion::updateCallSites(Function *F, Function *NF) {
257
258   SmallVector<Value*, 16> Args;
259
260   // ParamAttrs - Keep track of the parameter attributes for the arguments.
261   SmallVector<ParamAttrsWithIndex, 8> ArgAttrsVec;
262
263   for (Value::use_iterator FUI = F->use_begin(), FUE = F->use_end();
264        FUI != FUE;) {
265     CallSite CS = CallSite::get(*FUI);
266     ++FUI;
267     Instruction *Call = CS.getInstruction();
268
269     const PAListPtr &PAL = F->getParamAttrs();
270     // Add any return attributes.
271     if (ParameterAttributes attrs = PAL.getParamAttrs(0))
272       ArgAttrsVec.push_back(ParamAttrsWithIndex::get(0, attrs));
273
274     // Copy arguments, however skip first one.
275     CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
276     Value *FirstCArg = *AI;
277     ++AI;
278     // 0th parameter attribute is reserved for return type.
279     // 1th parameter attribute is for first 1st sret argument.
280     unsigned ParamIndex = 2; 
281     while (AI != AE) {
282       Args.push_back(*AI); 
283       if (ParameterAttributes Attrs = PAL.getParamAttrs(ParamIndex))
284         ArgAttrsVec.push_back(ParamAttrsWithIndex::get(ParamIndex - 1, Attrs));
285       ++ParamIndex;
286       ++AI;
287     }
288
289     
290     PAListPtr NewPAL = PAListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
291     
292     // Build new call instruction.
293     Instruction *New;
294     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
295       New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
296                                Args.begin(), Args.end(), "", Call);
297       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
298       cast<InvokeInst>(New)->setParamAttrs(NewPAL);
299     } else {
300       New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
301       cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
302       cast<CallInst>(New)->setParamAttrs(NewPAL);
303       if (cast<CallInst>(Call)->isTailCall())
304         cast<CallInst>(New)->setTailCall();
305     }
306     Args.clear();
307     ArgAttrsVec.clear();
308     New->takeName(Call);
309
310     // Update all users of sret parameter to extract value using getresult.
311     for (Value::use_iterator UI = FirstCArg->use_begin(), 
312            UE = FirstCArg->use_end(); UI != UE; ) {
313       User *U2 = *UI++;
314       CallInst *C2 = dyn_cast<CallInst>(U2);
315       if (C2 && (C2 == Call))
316         continue;
317       else if (GetElementPtrInst *UGEP = dyn_cast<GetElementPtrInst>(U2)) {
318         ConstantInt *Idx = dyn_cast<ConstantInt>(UGEP->getOperand(2));
319         assert (Idx && "Unexpected getelementptr index!");
320         Value *GR = new GetResultInst(New, Idx->getZExtValue(), "gr", UGEP);
321         for (Value::use_iterator GI = UGEP->use_begin(),
322                GE = UGEP->use_end(); GI != GE; ++GI) {
323           if (LoadInst *L = dyn_cast<LoadInst>(*GI)) {
324             L->replaceAllUsesWith(GR);
325             L->eraseFromParent();
326           }
327         }
328         UGEP->eraseFromParent();
329       }
330       else assert( 0 && "Unexpected sret parameter use");
331     }
332     Call->eraseFromParent();
333   }
334 }
335
336 /// nestedStructType - Return true if STy includes any
337 /// other aggregate types
338 bool SRETPromotion::nestedStructType(const StructType *STy) {
339   unsigned Num = STy->getNumElements();
340   for (unsigned i = 0; i < Num; i++) {
341     const Type *Ty = STy->getElementType(i);
342     if (!Ty->isFirstClassType() && Ty != Type::VoidTy)
343       return true;
344   }
345   return false;
346 }