56896466c931eee0ee652bcaa70d0d879dd44653
[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/LLVMContext.h"
27 #include "llvm/Module.h"
28 #include "llvm/CallGraphSCCPass.h"
29 #include "llvm/Instructions.h"
30 #include "llvm/Analysis/CallGraph.h"
31 #include "llvm/Support/CallSite.h"
32 #include "llvm/Support/CFG.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Support/raw_ostream.h"
38 using namespace llvm;
39
40 STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
41 STATISTIC(NumSRET , "Number of sret promoted");
42 namespace {
43   /// SRETPromotion - This pass removes sret parameter and updates
44   /// function to use multiple return value.
45   ///
46   struct SRETPromotion : public CallGraphSCCPass {
47     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48       CallGraphSCCPass::getAnalysisUsage(AU);
49     }
50
51     virtual bool runOnSCC(CallGraphSCC &SCC);
52     static char ID; // Pass identification, replacement for typeid
53     SRETPromotion() : CallGraphSCCPass(&ID) {}
54
55   private:
56     CallGraphNode *PromoteReturn(CallGraphNode *CGN);
57     bool isSafeToUpdateAllCallers(Function *F);
58     Function *cloneFunctionBody(Function *F, const StructType *STy);
59     CallGraphNode *updateCallSites(Function *F, Function *NF);
60     bool nestedStructType(const StructType *STy);
61   };
62 }
63
64 char SRETPromotion::ID = 0;
65 INITIALIZE_PASS(SRETPromotion, "sretpromotion",
66                 "Promote sret arguments to multiple ret values", false, false);
67
68 Pass *llvm::createStructRetPromotionPass() {
69   return new SRETPromotion();
70 }
71
72 bool SRETPromotion::runOnSCC(CallGraphSCC &SCC) {
73   bool Changed = false;
74
75   for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
76     if (CallGraphNode *NewNode = PromoteReturn(*I)) {
77       SCC.ReplaceNode(*I, NewNode);
78       Changed = true;
79     }
80
81   return Changed;
82 }
83
84 /// PromoteReturn - This method promotes function that uses StructRet paramater 
85 /// into a function that uses multiple return values.
86 CallGraphNode *SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
87   Function *F = CGN->getFunction();
88
89   if (!F || F->isDeclaration() || !F->hasLocalLinkage())
90     return 0;
91
92   // Make sure that function returns struct.
93   if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
94     return 0;
95
96   DEBUG(dbgs() << "SretPromotion: Looking at sret function " 
97         << F->getName() << "\n");
98
99   assert(F->getReturnType()->isVoidTy() && "Invalid function return type");
100   Function::arg_iterator AI = F->arg_begin();
101   const llvm::PointerType *FArgType = dyn_cast<PointerType>(AI->getType());
102   assert(FArgType && "Invalid sret parameter type");
103   const llvm::StructType *STy = 
104     dyn_cast<StructType>(FArgType->getElementType());
105   assert(STy && "Invalid sret parameter element type");
106
107   // Check if it is ok to perform this promotion.
108   if (isSafeToUpdateAllCallers(F) == false) {
109     DEBUG(dbgs() << "SretPromotion: Not all callers can be updated\n");
110     ++NumRejectedSRETUses;
111     return 0;
112   }
113
114   DEBUG(dbgs() << "SretPromotion: sret argument will be promoted\n");
115   ++NumSRET;
116   // [1] Replace use of sret parameter 
117   AllocaInst *TheAlloca = new AllocaInst(STy, NULL, "mrv", 
118                                          F->getEntryBlock().begin());
119   Value *NFirstArg = F->arg_begin();
120   NFirstArg->replaceAllUsesWith(TheAlloca);
121
122   // [2] Find and replace ret instructions
123   for (Function::iterator FI = F->begin(), FE = F->end();  FI != FE; ++FI) 
124     for(BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
125       Instruction *I = BI;
126       ++BI;
127       if (isa<ReturnInst>(I)) {
128         Value *NV = new LoadInst(TheAlloca, "mrv.ld", I);
129         ReturnInst *NR = ReturnInst::Create(F->getContext(), NV, I);
130         I->replaceAllUsesWith(NR);
131         I->eraseFromParent();
132       }
133     }
134
135   // [3] Create the new function body and insert it into the module.
136   Function *NF = cloneFunctionBody(F, STy);
137
138   // [4] Update all call sites to use new function
139   CallGraphNode *NF_CFN = updateCallSites(F, NF);
140
141   CallGraph &CG = getAnalysis<CallGraph>();
142   NF_CFN->stealCalledFunctionsFrom(CG[F]);
143
144   delete CG.removeFunctionFromModule(F);
145   return NF_CFN;
146 }
147
148 // Check if it is ok to perform this promotion.
149 bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
150
151   if (F->use_empty())
152     // No users. OK to modify signature.
153     return true;
154
155   for (Value::use_iterator FnUseI = F->use_begin(), FnUseE = F->use_end();
156        FnUseI != FnUseE; ++FnUseI) {
157     // The function is passed in as an argument to (possibly) another function,
158     // we can't change it!
159     CallSite CS(*FnUseI);
160     Instruction *Call = CS.getInstruction();
161     // The function is used by something else than a call or invoke instruction,
162     // we can't change it!
163     if (!Call || !CS.isCallee(FnUseI))
164       return false;
165     CallSite::arg_iterator AI = CS.arg_begin();
166     Value *FirstArg = *AI;
167
168     if (!isa<AllocaInst>(FirstArg))
169       return false;
170
171     // Check FirstArg's users.
172     for (Value::use_iterator ArgI = FirstArg->use_begin(), 
173            ArgE = FirstArg->use_end(); ArgI != ArgE; ++ArgI) {
174       User *U = *ArgI;
175       // If FirstArg user is a CallInst that does not correspond to current
176       // call site then this function F is not suitable for sret promotion.
177       if (CallInst *CI = dyn_cast<CallInst>(U)) {
178         if (CI != Call)
179           return false;
180       }
181       // If FirstArg user is a GEP whose all users are not LoadInst then
182       // this function F is not suitable for sret promotion.
183       else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
184         // TODO : Use dom info and insert PHINodes to collect get results
185         // from multiple call sites for this GEP.
186         if (GEP->getParent() != Call->getParent())
187           return false;
188         for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
189              GEPI != GEPE; ++GEPI) 
190           if (!isa<LoadInst>(*GEPI))
191             return false;
192       } 
193       // Any other FirstArg users make this function unsuitable for sret 
194       // promotion.
195       else
196         return false;
197     }
198   }
199
200   return true;
201 }
202
203 /// cloneFunctionBody - Create a new function based on F and
204 /// insert it into module. Remove first argument. Use STy as
205 /// the return type for new function.
206 Function *SRETPromotion::cloneFunctionBody(Function *F, 
207                                            const StructType *STy) {
208
209   const FunctionType *FTy = F->getFunctionType();
210   std::vector<const Type*> Params;
211
212   // Attributes - Keep track of the parameter attributes for the arguments.
213   SmallVector<AttributeWithIndex, 8> AttributesVec;
214   const AttrListPtr &PAL = F->getAttributes();
215
216   // Add any return attributes.
217   if (Attributes attrs = PAL.getRetAttributes())
218     AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
219
220   // Skip first argument.
221   Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
222   ++I;
223   // 0th parameter attribute is reserved for return type.
224   // 1th parameter attribute is for first 1st sret argument.
225   unsigned ParamIndex = 2; 
226   while (I != E) {
227     Params.push_back(I->getType());
228     if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
229       AttributesVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
230     ++I;
231     ++ParamIndex;
232   }
233
234   // Add any fn attributes.
235   if (Attributes attrs = PAL.getFnAttributes())
236     AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
237
238
239   FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
240   Function *NF = Function::Create(NFTy, F->getLinkage());
241   NF->takeName(F);
242   NF->copyAttributesFrom(F);
243   NF->setAttributes(AttrListPtr::get(AttributesVec.begin(), AttributesVec.end()));
244   F->getParent()->getFunctionList().insert(F, NF);
245   NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
246
247   // Replace arguments
248   I = F->arg_begin();
249   E = F->arg_end();
250   Function::arg_iterator NI = NF->arg_begin();
251   ++I;
252   while (I != E) {
253     I->replaceAllUsesWith(NI);
254     NI->takeName(I);
255     ++I;
256     ++NI;
257   }
258
259   return NF;
260 }
261
262 /// updateCallSites - Update all sites that call F to use NF.
263 CallGraphNode *SRETPromotion::updateCallSites(Function *F, Function *NF) {
264   CallGraph &CG = getAnalysis<CallGraph>();
265   SmallVector<Value*, 16> Args;
266
267   // Attributes - Keep track of the parameter attributes for the arguments.
268   SmallVector<AttributeWithIndex, 8> ArgAttrsVec;
269
270   // Get a new callgraph node for NF.
271   CallGraphNode *NF_CGN = CG.getOrInsertFunction(NF);
272
273   while (!F->use_empty()) {
274     CallSite CS(*F->use_begin());
275     Instruction *Call = CS.getInstruction();
276
277     const AttrListPtr &PAL = F->getAttributes();
278     // Add any return attributes.
279     if (Attributes attrs = PAL.getRetAttributes())
280       ArgAttrsVec.push_back(AttributeWithIndex::get(0, attrs));
281
282     // Copy arguments, however skip first one.
283     CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
284     Value *FirstCArg = *AI;
285     ++AI;
286     // 0th parameter attribute is reserved for return type.
287     // 1th parameter attribute is for first 1st sret argument.
288     unsigned ParamIndex = 2; 
289     while (AI != AE) {
290       Args.push_back(*AI); 
291       if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
292         ArgAttrsVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
293       ++ParamIndex;
294       ++AI;
295     }
296
297     // Add any function attributes.
298     if (Attributes attrs = PAL.getFnAttributes())
299       ArgAttrsVec.push_back(AttributeWithIndex::get(~0, attrs));
300     
301     AttrListPtr NewPAL = AttrListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
302     
303     // Build new call instruction.
304     Instruction *New;
305     if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
306       New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
307                                Args.begin(), Args.end(), "", Call);
308       cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
309       cast<InvokeInst>(New)->setAttributes(NewPAL);
310     } else {
311       New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
312       cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
313       cast<CallInst>(New)->setAttributes(NewPAL);
314       if (cast<CallInst>(Call)->isTailCall())
315         cast<CallInst>(New)->setTailCall();
316     }
317     Args.clear();
318     ArgAttrsVec.clear();
319     New->takeName(Call);
320
321     // Update the callgraph to know that the callsite has been transformed.
322     CallGraphNode *CalleeNode = CG[Call->getParent()->getParent()];
323     CalleeNode->removeCallEdgeFor(Call);
324     CalleeNode->addCalledFunction(New, NF_CGN);
325     
326     // Update all users of sret parameter to extract value using extractvalue.
327     for (Value::use_iterator UI = FirstCArg->use_begin(), 
328            UE = FirstCArg->use_end(); UI != UE; ) {
329       User *U2 = *UI++;
330       CallInst *C2 = dyn_cast<CallInst>(U2);
331       if (C2 && (C2 == Call))
332         continue;
333       
334       GetElementPtrInst *UGEP = cast<GetElementPtrInst>(U2);
335       ConstantInt *Idx = cast<ConstantInt>(UGEP->getOperand(2));
336       Value *GR = ExtractValueInst::Create(New, Idx->getZExtValue(),
337                                            "evi", UGEP);
338       while(!UGEP->use_empty()) {
339         // isSafeToUpdateAllCallers has checked that all GEP uses are
340         // LoadInsts
341         LoadInst *L = cast<LoadInst>(*UGEP->use_begin());
342         L->replaceAllUsesWith(GR);
343         L->eraseFromParent();
344       }
345       UGEP->eraseFromParent();
346       continue;
347     }
348     Call->eraseFromParent();
349   }
350   
351   return NF_CGN;
352 }
353
354 /// nestedStructType - Return true if STy includes any
355 /// other aggregate types
356 bool SRETPromotion::nestedStructType(const StructType *STy) {
357   unsigned Num = STy->getNumElements();
358   for (unsigned i = 0; i < Num; i++) {
359     const Type *Ty = STy->getElementType(i);
360     if (!Ty->isSingleValueType() && !Ty->isVoidTy())
361       return true;
362   }
363   return false;
364 }