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/Compiler.h"
38 #include "llvm/Support/raw_ostream.h"
41 STATISTIC(NumRejectedSRETUses , "Number of sret rejected due to unexpected uses");
42 STATISTIC(NumSRET , "Number of sret promoted");
44 /// SRETPromotion - This pass removes sret parameter and updates
45 /// function to use multiple return value.
47 struct VISIBILITY_HIDDEN SRETPromotion : public CallGraphSCCPass {
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 CallGraphSCCPass::getAnalysisUsage(AU);
52 virtual bool runOnSCC(const std::vector<CallGraphNode *> &SCC);
53 static char ID; // Pass identification, replacement for typeid
54 SRETPromotion() : CallGraphSCCPass(&ID) {}
57 bool PromoteReturn(CallGraphNode *CGN);
58 bool isSafeToUpdateAllCallers(Function *F);
59 Function *cloneFunctionBody(Function *F, const StructType *STy);
60 void updateCallSites(Function *F, Function *NF);
61 bool nestedStructType(const StructType *STy);
65 char SRETPromotion::ID = 0;
66 static RegisterPass<SRETPromotion>
67 X("sretpromotion", "Promote sret arguments to multiple ret values");
69 Pass *llvm::createStructRetPromotionPass() {
70 return new SRETPromotion();
73 bool SRETPromotion::runOnSCC(const std::vector<CallGraphNode *> &SCC) {
76 for (unsigned i = 0, e = SCC.size(); i != e; ++i)
77 Changed |= PromoteReturn(SCC[i]);
82 /// PromoteReturn - This method promotes function that uses StructRet paramater
83 /// into a function that uses mulitple return value.
84 bool SRETPromotion::PromoteReturn(CallGraphNode *CGN) {
85 Function *F = CGN->getFunction();
87 if (!F || F->isDeclaration() || !F->hasLocalLinkage())
90 // Make sure that function returns struct.
91 if (F->arg_size() == 0 || !F->hasStructRetAttr() || F->doesNotReturn())
94 DEBUG(errs() << "SretPromotion: Looking at sret function "
95 << F->getName() << "\n");
97 assert (F->getReturnType() == Type::getVoidTy(F->getContext()) &&
98 "Invalid function return type");
99 Function::arg_iterator AI = F->arg_begin();
100 const llvm::PointerType *FArgType = dyn_cast<PointerType>(AI->getType());
101 assert (FArgType && "Invalid sret parameter type");
102 const llvm::StructType *STy =
103 dyn_cast<StructType>(FArgType->getElementType());
104 assert (STy && "Invalid sret parameter element type");
106 // Check if it is ok to perform this promotion.
107 if (isSafeToUpdateAllCallers(F) == false) {
108 DOUT << "SretPromotion: Not all callers can be updated\n";
109 NumRejectedSRETUses++;
113 DOUT << "SretPromotion: sret argument will be promoted\n";
115 // [1] Replace use of sret parameter
116 AllocaInst *TheAlloca = new AllocaInst(STy, NULL, "mrv",
117 F->getEntryBlock().begin());
118 Value *NFirstArg = F->arg_begin();
119 NFirstArg->replaceAllUsesWith(TheAlloca);
121 // [2] Find and replace ret instructions
122 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
123 for(BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE; ) {
126 if (isa<ReturnInst>(I)) {
127 Value *NV = new LoadInst(TheAlloca, "mrv.ld", I);
128 ReturnInst *NR = ReturnInst::Create(F->getContext(), NV, I);
129 I->replaceAllUsesWith(NR);
130 I->eraseFromParent();
134 // [3] Create the new function body and insert it into the module.
135 Function *NF = cloneFunctionBody(F, STy);
137 // [4] Update all call sites to use new function
138 updateCallSites(F, NF);
140 F->eraseFromParent();
141 getAnalysis<CallGraph>().changeFunction(F, NF);
145 // Check if it is ok to perform this promotion.
146 bool SRETPromotion::isSafeToUpdateAllCallers(Function *F) {
149 // No users. OK to modify signature.
152 for (Value::use_iterator FnUseI = F->use_begin(), FnUseE = F->use_end();
153 FnUseI != FnUseE; ++FnUseI) {
154 // The function is passed in as an argument to (possibly) another function,
155 // we can't change it!
156 CallSite CS = CallSite::get(*FnUseI);
157 Instruction *Call = CS.getInstruction();
158 // The function is used by something else than a call or invoke instruction,
159 // we can't change it!
160 if (!Call || !CS.isCallee(FnUseI))
162 CallSite::arg_iterator AI = CS.arg_begin();
163 Value *FirstArg = *AI;
165 if (!isa<AllocaInst>(FirstArg))
168 // Check FirstArg's users.
169 for (Value::use_iterator ArgI = FirstArg->use_begin(),
170 ArgE = FirstArg->use_end(); ArgI != ArgE; ++ArgI) {
172 // If FirstArg user is a CallInst that does not correspond to current
173 // call site then this function F is not suitable for sret promotion.
174 if (CallInst *CI = dyn_cast<CallInst>(ArgI)) {
178 // If FirstArg user is a GEP whose all users are not LoadInst then
179 // this function F is not suitable for sret promotion.
180 else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(ArgI)) {
181 // TODO : Use dom info and insert PHINodes to collect get results
182 // from multiple call sites for this GEP.
183 if (GEP->getParent() != Call->getParent())
185 for (Value::use_iterator GEPI = GEP->use_begin(), GEPE = GEP->use_end();
186 GEPI != GEPE; ++GEPI)
187 if (!isa<LoadInst>(GEPI))
190 // Any other FirstArg users make this function unsuitable for sret
200 /// cloneFunctionBody - Create a new function based on F and
201 /// insert it into module. Remove first argument. Use STy as
202 /// the return type for new function.
203 Function *SRETPromotion::cloneFunctionBody(Function *F,
204 const StructType *STy) {
206 const FunctionType *FTy = F->getFunctionType();
207 std::vector<const Type*> Params;
209 // Attributes - Keep track of the parameter attributes for the arguments.
210 SmallVector<AttributeWithIndex, 8> AttributesVec;
211 const AttrListPtr &PAL = F->getAttributes();
213 // Add any return attributes.
214 if (Attributes attrs = PAL.getRetAttributes())
215 AttributesVec.push_back(AttributeWithIndex::get(0, attrs));
217 // Skip first argument.
218 Function::arg_iterator I = F->arg_begin(), E = F->arg_end();
220 // 0th parameter attribute is reserved for return type.
221 // 1th parameter attribute is for first 1st sret argument.
222 unsigned ParamIndex = 2;
224 Params.push_back(I->getType());
225 if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
226 AttributesVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
231 // Add any fn attributes.
232 if (Attributes attrs = PAL.getFnAttributes())
233 AttributesVec.push_back(AttributeWithIndex::get(~0, attrs));
236 FunctionType *NFTy = FunctionType::get(STy, Params, FTy->isVarArg());
237 Function *NF = Function::Create(NFTy, F->getLinkage());
239 NF->copyAttributesFrom(F);
240 NF->setAttributes(AttrListPtr::get(AttributesVec.begin(), AttributesVec.end()));
241 F->getParent()->getFunctionList().insert(F, NF);
242 NF->getBasicBlockList().splice(NF->begin(), F->getBasicBlockList());
247 Function::arg_iterator NI = NF->arg_begin();
250 I->replaceAllUsesWith(NI);
259 /// updateCallSites - Update all sites that call F to use NF.
260 void SRETPromotion::updateCallSites(Function *F, Function *NF) {
261 CallGraph &CG = getAnalysis<CallGraph>();
262 SmallVector<Value*, 16> Args;
264 // Attributes - Keep track of the parameter attributes for the arguments.
265 SmallVector<AttributeWithIndex, 8> ArgAttrsVec;
267 while (!F->use_empty()) {
268 CallSite CS = CallSite::get(*F->use_begin());
269 Instruction *Call = CS.getInstruction();
271 const AttrListPtr &PAL = F->getAttributes();
272 // Add any return attributes.
273 if (Attributes attrs = PAL.getRetAttributes())
274 ArgAttrsVec.push_back(AttributeWithIndex::get(0, attrs));
276 // Copy arguments, however skip first one.
277 CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
278 Value *FirstCArg = *AI;
280 // 0th parameter attribute is reserved for return type.
281 // 1th parameter attribute is for first 1st sret argument.
282 unsigned ParamIndex = 2;
285 if (Attributes Attrs = PAL.getParamAttributes(ParamIndex))
286 ArgAttrsVec.push_back(AttributeWithIndex::get(ParamIndex - 1, Attrs));
291 // Add any function attributes.
292 if (Attributes attrs = PAL.getFnAttributes())
293 ArgAttrsVec.push_back(AttributeWithIndex::get(~0, attrs));
295 AttrListPtr NewPAL = AttrListPtr::get(ArgAttrsVec.begin(), ArgAttrsVec.end());
297 // Build new call instruction.
299 if (InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
300 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
301 Args.begin(), Args.end(), "", Call);
302 cast<InvokeInst>(New)->setCallingConv(CS.getCallingConv());
303 cast<InvokeInst>(New)->setAttributes(NewPAL);
305 New = CallInst::Create(NF, Args.begin(), Args.end(), "", Call);
306 cast<CallInst>(New)->setCallingConv(CS.getCallingConv());
307 cast<CallInst>(New)->setAttributes(NewPAL);
308 if (cast<CallInst>(Call)->isTailCall())
309 cast<CallInst>(New)->setTailCall();
315 // Update the callgraph to know that the callsite has been transformed.
316 CG[Call->getParent()->getParent()]->replaceCallSite(Call, New);
318 // Update all users of sret parameter to extract value using extractvalue.
319 for (Value::use_iterator UI = FirstCArg->use_begin(),
320 UE = FirstCArg->use_end(); UI != UE; ) {
322 CallInst *C2 = dyn_cast<CallInst>(U2);
323 if (C2 && (C2 == Call))
325 else if (GetElementPtrInst *UGEP = dyn_cast<GetElementPtrInst>(U2)) {
326 ConstantInt *Idx = dyn_cast<ConstantInt>(UGEP->getOperand(2));
327 assert (Idx && "Unexpected getelementptr index!");
328 Value *GR = ExtractValueInst::Create(New, Idx->getZExtValue(),
330 while(!UGEP->use_empty()) {
331 // isSafeToUpdateAllCallers has checked that all GEP uses are
333 LoadInst *L = cast<LoadInst>(*UGEP->use_begin());
334 L->replaceAllUsesWith(GR);
335 L->eraseFromParent();
337 UGEP->eraseFromParent();
339 else assert( 0 && "Unexpected sret parameter use");
341 Call->eraseFromParent();
345 /// nestedStructType - Return true if STy includes any
346 /// other aggregate types
347 bool SRETPromotion::nestedStructType(const StructType *STy) {
348 unsigned Num = STy->getNumElements();
349 for (unsigned i = 0; i < Num; i++) {
350 const Type *Ty = STy->getElementType(i);
351 if (!Ty->isSingleValueType() && Ty != Type::getVoidTy(STy->getContext()))