1 //===-- StructRetPromotion.cpp - Promote sret arguments -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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).
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
20 //===----------------------------------------------------------------------===//
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"
40 STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
41 STATISTIC(NumSRET , "Number of sret promoted");
43 /// SRETPromotion - This pass removes sret parameter and updates
44 /// function to use multiple return value.
46 struct SRETPromotion : public CallGraphSCCPass {
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 CallGraphSCCPass::getAnalysisUsage(AU);
51 virtual bool runOnSCC(CallGraphSCC &SCC);
52 static char ID; // Pass identification, replacement for typeid
53 SRETPromotion() : CallGraphSCCPass(ID) {
54 initializeSRETPromotionPass(*PassRegistry::getPassRegistry());
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);
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)
72 Pass *llvm::createStructRetPromotionPass() {
73 return new SRETPromotion();
76 bool SRETPromotion::runOnSCC(CallGraphSCC &SCC) {
79 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
80 if (CallGraphNode *NewNode = PromoteReturn(*I)) {
81 SCC.ReplaceNode(*I, NewNode);
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();
93 if (!F || F->isDeclaration() || !F->hasLocalLinkage())
96 // Make sure that function returns struct.
97 if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
100 DEBUG(dbgs() << "SretPromotion: Looking at sret function "
101 << F->getName() << "\n");
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");
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;
118 DEBUG(dbgs() << "SretPromotion: sret argument will be promoted\n");
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);
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; ) {
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();
139 // [3] Create the new function body and insert it into the module.
140 Function *NF = cloneFunctionBody(F, STy);
142 // [4] Update all call sites to use new function
143 CallGraphNode *NF_CFN = updateCallSites(F, NF);
145 CallGraph &CG = getAnalysis<CallGraph>();
146 NF_CFN->stealCalledFunctionsFrom(CG[F]);
148 delete CG.removeFunctionFromModule(F);
152 // Check if it is ok to perform this promotion.
153 bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
156 // No users. OK to modify signature.
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))
169 CallSite::arg_iterator AI = CS.arg_begin();
170 Value *FirstArg = *AI;
172 if (!isa<AllocaInst>(FirstArg))
175 // Check FirstArg's users.
176 for (Value::use_iterator ArgI = FirstArg->use_begin(),
177 ArgE = FirstArg->use_end(); ArgI != ArgE; ++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)) {
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())
192 for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
193 GEPI != GEPE; ++GEPI)
194 if (!isa<LoadInst>(*GEPI))
197 // Any other FirstArg users make this function unsuitable for sret
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) {
213 const FunctionType *FTy = F->getFunctionType();
214 std::vector<const Type*> Params;
216 // Attributes - Keep track of the parameter attributes for the arguments.
217 SmallVector<AttributeWithIndex, 8> AttributesVec;
218 const AttrListPtr &PAL = F->getAttributes();
220 // Add any return attributes.
221 if (Attributes attrs = PAL.getRetAttributes())
222 AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
224 // Skip first argument.
225 Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
227 // 0th parameter attribute is reserved for return type.
228 // 1th parameter attribute is for first 1st sret argument.
229 unsigned ParamIndex = 2;
231 Params.push_back(I->getType());
232 if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
233 AttributesVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
238 // Add any fn attributes.
239 if (Attributes attrs = PAL.getFnAttributes())
240 AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
243 FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
244 Function *NF = Function::Create(NFTy, F->getLinkage());
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());
254 Function::arg_iterator NI = NF->arg_begin();
257 I->replaceAllUsesWith(NI);
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;
271 // Attributes - Keep track of the parameter attributes for the arguments.
272 SmallVector<AttributeWithIndex, 8> ArgAttrsVec;
274 // Get a new callgraph node for NF.
275 CallGraphNode *NF_CGN = CG.getOrInsertFunction(NF);
277 while (!F->use_empty()) {
278 CallSite CS(*F->use_begin());
279 Instruction *Call = CS.getInstruction();
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));
286 // Copy arguments, however skip first one.
287 CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
288 Value *FirstCArg = *AI;
290 // 0th parameter attribute is reserved for return type.
291 // 1th parameter attribute is for first 1st sret argument.
292 unsigned ParamIndex = 2;
295 if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
296 ArgAttrsVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
301 // Add any function attributes.
302 if (Attributes attrs = PAL.getFnAttributes())
303 ArgAttrsVec.push_back(AttributeWithIndex::get(~0, attrs));
305 AttrListPtr NewPAL = AttrListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
307 // Build new call instruction.
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);
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();
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);
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; ) {
334 CallInst *C2 = dyn_cast<CallInst>(U2);
335 if (C2 && (C2 == Call))
338 GetElementPtrInst *UGEP = cast<GetElementPtrInst>(U2);
339 ConstantInt *Idx = cast<ConstantInt>(UGEP->getOperand(2));
340 Value *GR = ExtractValueInst::Create(New, Idx->getZExtValue(),
342 while(!UGEP->use_empty()) {
343 // isSafeToUpdateAllCallers has checked that all GEP uses are
345 LoadInst *L = cast<LoadInst>(*UGEP->use_begin());
346 L->replaceAllUsesWith(GR);
347 L->eraseFromParent();
349 UGEP->eraseFromParent();
352 Call->eraseFromParent();