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