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