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