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