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