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