Mercurial > hg > Members > tobaru > cbc > CbC_llvm
comparison lib/Transforms/IPO/DeadArgumentElimination.cpp @ 80:67baa08a3894
update to LLVM 3.6
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 25 Sep 2014 16:56:18 +0900 |
parents | 54457678186b |
children | 60c9769439b8 |
comparison
equal
deleted
inserted
replaced
76:9e74acfe8c42 | 80:67baa08a3894 |
---|---|
15 // This pass is often useful as a cleanup pass to run after aggressive | 15 // This pass is often useful as a cleanup pass to run after aggressive |
16 // interprocedural passes, which add possibly-dead arguments or return values. | 16 // interprocedural passes, which add possibly-dead arguments or return values. |
17 // | 17 // |
18 //===----------------------------------------------------------------------===// | 18 //===----------------------------------------------------------------------===// |
19 | 19 |
20 #define DEBUG_TYPE "deadargelim" | |
21 #include "llvm/Transforms/IPO.h" | 20 #include "llvm/Transforms/IPO.h" |
22 #include "llvm/ADT/DenseMap.h" | 21 #include "llvm/ADT/DenseMap.h" |
23 #include "llvm/ADT/SmallVector.h" | 22 #include "llvm/ADT/SmallVector.h" |
24 #include "llvm/ADT/Statistic.h" | 23 #include "llvm/ADT/Statistic.h" |
25 #include "llvm/ADT/StringExtras.h" | 24 #include "llvm/ADT/StringExtras.h" |
26 #include "llvm/DIBuilder.h" | 25 #include "llvm/IR/CallSite.h" |
27 #include "llvm/DebugInfo.h" | |
28 #include "llvm/IR/CallingConv.h" | 26 #include "llvm/IR/CallingConv.h" |
29 #include "llvm/IR/Constant.h" | 27 #include "llvm/IR/Constant.h" |
28 #include "llvm/IR/DIBuilder.h" | |
29 #include "llvm/IR/DebugInfo.h" | |
30 #include "llvm/IR/DerivedTypes.h" | 30 #include "llvm/IR/DerivedTypes.h" |
31 #include "llvm/IR/Instructions.h" | 31 #include "llvm/IR/Instructions.h" |
32 #include "llvm/IR/IntrinsicInst.h" | 32 #include "llvm/IR/IntrinsicInst.h" |
33 #include "llvm/IR/LLVMContext.h" | 33 #include "llvm/IR/LLVMContext.h" |
34 #include "llvm/IR/Module.h" | 34 #include "llvm/IR/Module.h" |
35 #include "llvm/Pass.h" | 35 #include "llvm/Pass.h" |
36 #include "llvm/Support/CallSite.h" | |
37 #include "llvm/Support/Debug.h" | 36 #include "llvm/Support/Debug.h" |
38 #include "llvm/Support/raw_ostream.h" | 37 #include "llvm/Support/raw_ostream.h" |
39 #include <map> | 38 #include <map> |
40 #include <set> | 39 #include <set> |
40 #include <tuple> | |
41 using namespace llvm; | 41 using namespace llvm; |
42 | |
43 #define DEBUG_TYPE "deadargelim" | |
42 | 44 |
43 STATISTIC(NumArgumentsEliminated, "Number of unread args removed"); | 45 STATISTIC(NumArgumentsEliminated, "Number of unread args removed"); |
44 STATISTIC(NumRetValsEliminated , "Number of unused return values removed"); | 46 STATISTIC(NumRetValsEliminated , "Number of unused return values removed"); |
45 STATISTIC(NumArgumentsReplacedWithUndef, | 47 STATISTIC(NumArgumentsReplacedWithUndef, |
46 "Number of unread args replaced with undef"); | 48 "Number of unread args replaced with undef"); |
60 unsigned Idx; | 62 unsigned Idx; |
61 bool IsArg; | 63 bool IsArg; |
62 | 64 |
63 /// Make RetOrArg comparable, so we can put it into a map. | 65 /// Make RetOrArg comparable, so we can put it into a map. |
64 bool operator<(const RetOrArg &O) const { | 66 bool operator<(const RetOrArg &O) const { |
65 if (F != O.F) | 67 return std::tie(F, Idx, IsArg) < std::tie(O.F, O.Idx, O.IsArg); |
66 return F < O.F; | |
67 else if (Idx != O.Idx) | |
68 return Idx < O.Idx; | |
69 else | |
70 return IsArg < O.IsArg; | |
71 } | 68 } |
72 | 69 |
73 /// Make RetOrArg comparable, so we can easily iterate the multimap. | 70 /// Make RetOrArg comparable, so we can easily iterate the multimap. |
74 bool operator==(const RetOrArg &O) const { | 71 bool operator==(const RetOrArg &O) const { |
75 return F == O.F && Idx == O.Idx && IsArg == O.IsArg; | 72 return F == O.F && Idx == O.Idx && IsArg == O.IsArg; |
128 // the function is replaced with another one, we should patch the pointer | 125 // the function is replaced with another one, we should patch the pointer |
129 // to LLVM function in metadata. | 126 // to LLVM function in metadata. |
130 // As the code generation for module is finished (and DIBuilder is | 127 // As the code generation for module is finished (and DIBuilder is |
131 // finalized) we assume that subprogram descriptors won't be changed, and | 128 // finalized) we assume that subprogram descriptors won't be changed, and |
132 // they are stored in map for short duration anyway. | 129 // they are stored in map for short duration anyway. |
133 typedef DenseMap<Function*, DISubprogram> FunctionDIMap; | 130 DenseMap<const Function *, DISubprogram> FunctionDIs; |
134 FunctionDIMap FunctionDIs; | |
135 | 131 |
136 protected: | 132 protected: |
137 // DAH uses this to specify a different ID. | 133 // DAH uses this to specify a different ID. |
138 explicit DAE(char &ID) : ModulePass(ID) {} | 134 explicit DAE(char &ID) : ModulePass(ID) {} |
139 | 135 |
141 static char ID; // Pass identification, replacement for typeid | 137 static char ID; // Pass identification, replacement for typeid |
142 DAE() : ModulePass(ID) { | 138 DAE() : ModulePass(ID) { |
143 initializeDAEPass(*PassRegistry::getPassRegistry()); | 139 initializeDAEPass(*PassRegistry::getPassRegistry()); |
144 } | 140 } |
145 | 141 |
146 bool runOnModule(Module &M); | 142 bool runOnModule(Module &M) override; |
147 | 143 |
148 virtual bool ShouldHackArguments() const { return false; } | 144 virtual bool ShouldHackArguments() const { return false; } |
149 | 145 |
150 private: | 146 private: |
151 Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses); | 147 Liveness MarkIfNotLive(RetOrArg Use, UseVector &MaybeLiveUses); |
152 Liveness SurveyUse(Value::const_use_iterator U, UseVector &MaybeLiveUses, | 148 Liveness SurveyUse(const Use *U, UseVector &MaybeLiveUses, |
153 unsigned RetValNum = 0); | 149 unsigned RetValNum = 0); |
154 Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses); | 150 Liveness SurveyUses(const Value *V, UseVector &MaybeLiveUses); |
155 | 151 |
156 void CollectFunctionDIs(Module &M); | |
157 void SurveyFunction(const Function &F); | 152 void SurveyFunction(const Function &F); |
158 void MarkValue(const RetOrArg &RA, Liveness L, | 153 void MarkValue(const RetOrArg &RA, Liveness L, |
159 const UseVector &MaybeLiveUses); | 154 const UseVector &MaybeLiveUses); |
160 void MarkLive(const RetOrArg &RA); | 155 void MarkLive(const RetOrArg &RA); |
161 void MarkLive(const Function &F); | 156 void MarkLive(const Function &F); |
176 /// by bugpoint. | 171 /// by bugpoint. |
177 struct DAH : public DAE { | 172 struct DAH : public DAE { |
178 static char ID; | 173 static char ID; |
179 DAH() : DAE(ID) {} | 174 DAH() : DAE(ID) {} |
180 | 175 |
181 virtual bool ShouldHackArguments() const { return true; } | 176 bool ShouldHackArguments() const override { return true; } |
182 }; | 177 }; |
183 } | 178 } |
184 | 179 |
185 char DAH::ID = 0; | 180 char DAH::ID = 0; |
186 INITIALIZE_PASS(DAH, "deadarghaX0r", | 181 INITIALIZE_PASS(DAH, "deadarghaX0r", |
191 /// which are not used by the body of the function. | 186 /// which are not used by the body of the function. |
192 /// | 187 /// |
193 ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); } | 188 ModulePass *llvm::createDeadArgEliminationPass() { return new DAE(); } |
194 ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); } | 189 ModulePass *llvm::createDeadArgHackingPass() { return new DAH(); } |
195 | 190 |
196 /// CollectFunctionDIs - Map each function in the module to its debug info | |
197 /// descriptor. | |
198 void DAE::CollectFunctionDIs(Module &M) { | |
199 FunctionDIs.clear(); | |
200 | |
201 for (Module::named_metadata_iterator I = M.named_metadata_begin(), | |
202 E = M.named_metadata_end(); I != E; ++I) { | |
203 NamedMDNode &NMD = *I; | |
204 for (unsigned MDIndex = 0, MDNum = NMD.getNumOperands(); | |
205 MDIndex < MDNum; ++MDIndex) { | |
206 MDNode *Node = NMD.getOperand(MDIndex); | |
207 if (!DIDescriptor(Node).isCompileUnit()) | |
208 continue; | |
209 DICompileUnit CU(Node); | |
210 const DIArray &SPs = CU.getSubprograms(); | |
211 for (unsigned SPIndex = 0, SPNum = SPs.getNumElements(); | |
212 SPIndex < SPNum; ++SPIndex) { | |
213 DISubprogram SP(SPs.getElement(SPIndex)); | |
214 assert((!SP || SP.isSubprogram()) && | |
215 "A MDNode in subprograms of a CU should be null or a DISubprogram."); | |
216 if (!SP) | |
217 continue; | |
218 if (Function *F = SP.getFunction()) | |
219 FunctionDIs[F] = SP; | |
220 } | |
221 } | |
222 } | |
223 } | |
224 | |
225 /// DeleteDeadVarargs - If this is an function that takes a ... list, and if | 191 /// DeleteDeadVarargs - If this is an function that takes a ... list, and if |
226 /// llvm.vastart is never called, the varargs list is dead for the function. | 192 /// llvm.vastart is never called, the varargs list is dead for the function. |
227 bool DAE::DeleteDeadVarargs(Function &Fn) { | 193 bool DAE::DeleteDeadVarargs(Function &Fn) { |
228 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!"); | 194 assert(Fn.getFunctionType()->isVarArg() && "Function isn't varargs!"); |
229 if (Fn.isDeclaration() || !Fn.hasLocalLinkage()) return false; | 195 if (Fn.isDeclaration() || !Fn.hasLocalLinkage()) return false; |
231 // Ensure that the function is only directly called. | 197 // Ensure that the function is only directly called. |
232 if (Fn.hasAddressTaken()) | 198 if (Fn.hasAddressTaken()) |
233 return false; | 199 return false; |
234 | 200 |
235 // Okay, we know we can transform this function if safe. Scan its body | 201 // Okay, we know we can transform this function if safe. Scan its body |
236 // looking for calls to llvm.vastart. | 202 // looking for calls marked musttail or calls to llvm.vastart. |
237 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { | 203 for (Function::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) { |
238 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { | 204 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
239 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { | 205 CallInst *CI = dyn_cast<CallInst>(I); |
206 if (!CI) | |
207 continue; | |
208 if (CI->isMustTailCall()) | |
209 return false; | |
210 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI)) { | |
240 if (II->getIntrinsicID() == Intrinsic::vastart) | 211 if (II->getIntrinsicID() == Intrinsic::vastart) |
241 return false; | 212 return false; |
242 } | 213 } |
243 } | 214 } |
244 } | 215 } |
263 | 234 |
264 // Loop over all of the callers of the function, transforming the call sites | 235 // Loop over all of the callers of the function, transforming the call sites |
265 // to pass in a smaller number of arguments into the new function. | 236 // to pass in a smaller number of arguments into the new function. |
266 // | 237 // |
267 std::vector<Value*> Args; | 238 std::vector<Value*> Args; |
268 for (Value::use_iterator I = Fn.use_begin(), E = Fn.use_end(); I != E; ) { | 239 for (Value::user_iterator I = Fn.user_begin(), E = Fn.user_end(); I != E; ) { |
269 CallSite CS(*I++); | 240 CallSite CS(*I++); |
270 if (!CS) | 241 if (!CS) |
271 continue; | 242 continue; |
272 Instruction *Call = CS.getInstruction(); | 243 Instruction *Call = CS.getInstruction(); |
273 | 244 |
328 I->replaceAllUsesWith(I2); | 299 I->replaceAllUsesWith(I2); |
329 I2->takeName(I); | 300 I2->takeName(I); |
330 } | 301 } |
331 | 302 |
332 // Patch the pointer to LLVM function in debug info descriptor. | 303 // Patch the pointer to LLVM function in debug info descriptor. |
333 FunctionDIMap::iterator DI = FunctionDIs.find(&Fn); | 304 auto DI = FunctionDIs.find(&Fn); |
334 if (DI != FunctionDIs.end()) | 305 if (DI != FunctionDIs.end()) |
335 DI->second.replaceFunction(NF); | 306 DI->second.replaceFunction(NF); |
336 | 307 |
337 // Fix up any BlockAddresses that refer to the function. | 308 // Fix up any BlockAddresses that refer to the function. |
338 Fn.replaceAllUsesWith(ConstantExpr::getBitCast(NF, Fn.getType())); | 309 Fn.replaceAllUsesWith(ConstantExpr::getBitCast(NF, Fn.getType())); |
376 SmallVector<unsigned, 8> UnusedArgs; | 347 SmallVector<unsigned, 8> UnusedArgs; |
377 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(); | 348 for (Function::arg_iterator I = Fn.arg_begin(), E = Fn.arg_end(); |
378 I != E; ++I) { | 349 I != E; ++I) { |
379 Argument *Arg = I; | 350 Argument *Arg = I; |
380 | 351 |
381 if (Arg->use_empty() && !Arg->hasByValAttr()) | 352 if (Arg->use_empty() && !Arg->hasByValOrInAllocaAttr()) |
382 UnusedArgs.push_back(Arg->getArgNo()); | 353 UnusedArgs.push_back(Arg->getArgNo()); |
383 } | 354 } |
384 | 355 |
385 if (UnusedArgs.empty()) | 356 if (UnusedArgs.empty()) |
386 return false; | 357 return false; |
387 | 358 |
388 bool Changed = false; | 359 bool Changed = false; |
389 | 360 |
390 for (Function::use_iterator I = Fn.use_begin(), E = Fn.use_end(); | 361 for (Use &U : Fn.uses()) { |
391 I != E; ++I) { | 362 CallSite CS(U.getUser()); |
392 CallSite CS(*I); | 363 if (!CS || !CS.isCallee(&U)) |
393 if (!CS || !CS.isCallee(I)) | |
394 continue; | 364 continue; |
395 | 365 |
396 // Now go through all unused args and replace them with "undef". | 366 // Now go through all unused args and replace them with "undef". |
397 for (unsigned I = 0, E = UnusedArgs.size(); I != E; ++I) { | 367 for (unsigned I = 0, E = UnusedArgs.size(); I != E; ++I) { |
398 unsigned ArgNo = UnusedArgs[I]; | 368 unsigned ArgNo = UnusedArgs[I]; |
439 /// if it causes the used value to become MaybeLive. | 409 /// if it causes the used value to become MaybeLive. |
440 /// | 410 /// |
441 /// RetValNum is the return value number to use when this use is used in a | 411 /// RetValNum is the return value number to use when this use is used in a |
442 /// return instruction. This is used in the recursion, you should always leave | 412 /// return instruction. This is used in the recursion, you should always leave |
443 /// it at 0. | 413 /// it at 0. |
444 DAE::Liveness DAE::SurveyUse(Value::const_use_iterator U, | 414 DAE::Liveness DAE::SurveyUse(const Use *U, |
445 UseVector &MaybeLiveUses, unsigned RetValNum) { | 415 UseVector &MaybeLiveUses, unsigned RetValNum) { |
446 const User *V = *U; | 416 const User *V = U->getUser(); |
447 if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) { | 417 if (const ReturnInst *RI = dyn_cast<ReturnInst>(V)) { |
448 // The value is returned from a function. It's only live when the | 418 // The value is returned from a function. It's only live when the |
449 // function's return value is live. We use RetValNum here, for the case | 419 // function's return value is live. We use RetValNum here, for the case |
450 // that U is really a use of an insertvalue instruction that uses the | 420 // that U is really a use of an insertvalue instruction that uses the |
451 // original Use. | 421 // original Use. |
452 RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum); | 422 RetOrArg Use = CreateRet(RI->getParent()->getParent(), RetValNum); |
453 // We might be live, depending on the liveness of Use. | 423 // We might be live, depending on the liveness of Use. |
454 return MarkIfNotLive(Use, MaybeLiveUses); | 424 return MarkIfNotLive(Use, MaybeLiveUses); |
455 } | 425 } |
456 if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) { | 426 if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(V)) { |
457 if (U.getOperandNo() != InsertValueInst::getAggregateOperandIndex() | 427 if (U->getOperandNo() != InsertValueInst::getAggregateOperandIndex() |
458 && IV->hasIndices()) | 428 && IV->hasIndices()) |
459 // The use we are examining is inserted into an aggregate. Our liveness | 429 // The use we are examining is inserted into an aggregate. Our liveness |
460 // depends on all uses of that aggregate, but if it is used as a return | 430 // depends on all uses of that aggregate, but if it is used as a return |
461 // value, only index at which we were inserted counts. | 431 // value, only index at which we were inserted counts. |
462 RetValNum = *IV->idx_begin(); | 432 RetValNum = *IV->idx_begin(); |
463 | 433 |
464 // Note that if we are used as the aggregate operand to the insertvalue, | 434 // Note that if we are used as the aggregate operand to the insertvalue, |
465 // we don't change RetValNum, but do survey all our uses. | 435 // we don't change RetValNum, but do survey all our uses. |
466 | 436 |
467 Liveness Result = MaybeLive; | 437 Liveness Result = MaybeLive; |
468 for (Value::const_use_iterator I = IV->use_begin(), | 438 for (const Use &UU : IV->uses()) { |
469 E = V->use_end(); I != E; ++I) { | 439 Result = SurveyUse(&UU, MaybeLiveUses, RetValNum); |
470 Result = SurveyUse(I, MaybeLiveUses, RetValNum); | |
471 if (Result == Live) | 440 if (Result == Live) |
472 break; | 441 break; |
473 } | 442 } |
474 return Result; | 443 return Result; |
475 } | 444 } |
488 if (ArgNo >= F->getFunctionType()->getNumParams()) | 457 if (ArgNo >= F->getFunctionType()->getNumParams()) |
489 // The value is passed in through a vararg! Must be live. | 458 // The value is passed in through a vararg! Must be live. |
490 return Live; | 459 return Live; |
491 | 460 |
492 assert(CS.getArgument(ArgNo) | 461 assert(CS.getArgument(ArgNo) |
493 == CS->getOperand(U.getOperandNo()) | 462 == CS->getOperand(U->getOperandNo()) |
494 && "Argument is not where we expected it"); | 463 && "Argument is not where we expected it"); |
495 | 464 |
496 // Value passed to a normal call. It's only live when the corresponding | 465 // Value passed to a normal call. It's only live when the corresponding |
497 // argument to the called function turns out live. | 466 // argument to the called function turns out live. |
498 RetOrArg Use = CreateArg(F, ArgNo); | 467 RetOrArg Use = CreateArg(F, ArgNo); |
511 /// be ignored (since it might not be complete). | 480 /// be ignored (since it might not be complete). |
512 DAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) { | 481 DAE::Liveness DAE::SurveyUses(const Value *V, UseVector &MaybeLiveUses) { |
513 // Assume it's dead (which will only hold if there are no uses at all..). | 482 // Assume it's dead (which will only hold if there are no uses at all..). |
514 Liveness Result = MaybeLive; | 483 Liveness Result = MaybeLive; |
515 // Check each use. | 484 // Check each use. |
516 for (Value::const_use_iterator I = V->use_begin(), | 485 for (const Use &U : V->uses()) { |
517 E = V->use_end(); I != E; ++I) { | 486 Result = SurveyUse(&U, MaybeLiveUses); |
518 Result = SurveyUse(I, MaybeLiveUses); | |
519 if (Result == Live) | 487 if (Result == Live) |
520 break; | 488 break; |
521 } | 489 } |
522 return Result; | 490 return Result; |
523 } | 491 } |
529 // | 497 // |
530 // We consider arguments of non-internal functions to be intrinsically alive as | 498 // We consider arguments of non-internal functions to be intrinsically alive as |
531 // well as arguments to functions which have their "address taken". | 499 // well as arguments to functions which have their "address taken". |
532 // | 500 // |
533 void DAE::SurveyFunction(const Function &F) { | 501 void DAE::SurveyFunction(const Function &F) { |
502 // Functions with inalloca parameters are expecting args in a particular | |
503 // register and memory layout. | |
504 if (F.getAttributes().hasAttrSomewhere(Attribute::InAlloca)) { | |
505 MarkLive(F); | |
506 return; | |
507 } | |
508 | |
534 unsigned RetCount = NumRetVals(&F); | 509 unsigned RetCount = NumRetVals(&F); |
535 // Assume all return values are dead | 510 // Assume all return values are dead |
536 typedef SmallVector<Liveness, 5> RetVals; | 511 typedef SmallVector<Liveness, 5> RetVals; |
537 RetVals RetValLiveness(RetCount, MaybeLive); | 512 RetVals RetValLiveness(RetCount, MaybeLive); |
538 | 513 |
560 // Keep track of the number of live retvals, so we can skip checks once all | 535 // Keep track of the number of live retvals, so we can skip checks once all |
561 // of them turn out to be live. | 536 // of them turn out to be live. |
562 unsigned NumLiveRetVals = 0; | 537 unsigned NumLiveRetVals = 0; |
563 Type *STy = dyn_cast<StructType>(F.getReturnType()); | 538 Type *STy = dyn_cast<StructType>(F.getReturnType()); |
564 // Loop all uses of the function. | 539 // Loop all uses of the function. |
565 for (Value::const_use_iterator I = F.use_begin(), E = F.use_end(); | 540 for (const Use &U : F.uses()) { |
566 I != E; ++I) { | |
567 // If the function is PASSED IN as an argument, its address has been | 541 // If the function is PASSED IN as an argument, its address has been |
568 // taken. | 542 // taken. |
569 ImmutableCallSite CS(*I); | 543 ImmutableCallSite CS(U.getUser()); |
570 if (!CS || !CS.isCallee(I)) { | 544 if (!CS || !CS.isCallee(&U)) { |
571 MarkLive(F); | 545 MarkLive(F); |
572 return; | 546 return; |
573 } | 547 } |
574 | 548 |
575 // If this use is anything other than a call site, the function is alive. | 549 // If this use is anything other than a call site, the function is alive. |
584 // Now, check how our return value(s) is/are used in this caller. Don't | 558 // Now, check how our return value(s) is/are used in this caller. Don't |
585 // bother checking return values if all of them are live already. | 559 // bother checking return values if all of them are live already. |
586 if (NumLiveRetVals != RetCount) { | 560 if (NumLiveRetVals != RetCount) { |
587 if (STy) { | 561 if (STy) { |
588 // Check all uses of the return value. | 562 // Check all uses of the return value. |
589 for (Value::const_use_iterator I = TheCall->use_begin(), | 563 for (const User *U : TheCall->users()) { |
590 E = TheCall->use_end(); I != E; ++I) { | 564 const ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(U); |
591 const ExtractValueInst *Ext = dyn_cast<ExtractValueInst>(*I); | |
592 if (Ext && Ext->hasIndices()) { | 565 if (Ext && Ext->hasIndices()) { |
593 // This use uses a part of our return value, survey the uses of | 566 // This use uses a part of our return value, survey the uses of |
594 // that part and store the results for this index only. | 567 // that part and store the results for this index only. |
595 unsigned Idx = *Ext->idx_begin(); | 568 unsigned Idx = *Ext->idx_begin(); |
596 if (RetValLiveness[Idx] != Live) { | 569 if (RetValLiveness[Idx] != Live) { |
765 } | 738 } |
766 } | 739 } |
767 | 740 |
768 // Find out the new return value. | 741 // Find out the new return value. |
769 Type *RetTy = FTy->getReturnType(); | 742 Type *RetTy = FTy->getReturnType(); |
770 Type *NRetTy = NULL; | 743 Type *NRetTy = nullptr; |
771 unsigned RetCount = NumRetVals(F); | 744 unsigned RetCount = NumRetVals(F); |
772 | 745 |
773 // -1 means unused, other numbers are the new index | 746 // -1 means unused, other numbers are the new index |
774 SmallVector<int, 5> NewRetIdxs(RetCount, -1); | 747 SmallVector<int, 5> NewRetIdxs(RetCount, -1); |
775 std::vector<Type*> RetTypes; | 748 std::vector<Type*> RetTypes; |
889 // Loop over all of the callers of the function, transforming the call sites | 862 // Loop over all of the callers of the function, transforming the call sites |
890 // to pass in a smaller number of arguments into the new function. | 863 // to pass in a smaller number of arguments into the new function. |
891 // | 864 // |
892 std::vector<Value*> Args; | 865 std::vector<Value*> Args; |
893 while (!F->use_empty()) { | 866 while (!F->use_empty()) { |
894 CallSite CS(F->use_back()); | 867 CallSite CS(F->user_back()); |
895 Instruction *Call = CS.getInstruction(); | 868 Instruction *Call = CS.getInstruction(); |
896 | 869 |
897 AttributesVec.clear(); | 870 AttributesVec.clear(); |
898 const AttributeSet &CallPAL = CS.getAttributes(); | 871 const AttributeSet &CallPAL = CS.getAttributes(); |
899 | 872 |
1051 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB) | 1024 for (Function::iterator BB = NF->begin(), E = NF->end(); BB != E; ++BB) |
1052 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { | 1025 if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) { |
1053 Value *RetVal; | 1026 Value *RetVal; |
1054 | 1027 |
1055 if (NFTy->getReturnType()->isVoidTy()) { | 1028 if (NFTy->getReturnType()->isVoidTy()) { |
1056 RetVal = 0; | 1029 RetVal = nullptr; |
1057 } else { | 1030 } else { |
1058 assert (RetTy->isStructTy()); | 1031 assert (RetTy->isStructTy()); |
1059 // The original return value was a struct, insert | 1032 // The original return value was a struct, insert |
1060 // extractvalue/insertvalue chains to extract only the values we need | 1033 // extractvalue/insertvalue chains to extract only the values we need |
1061 // to return and insert them into our new result. | 1034 // to return and insert them into our new result. |
1086 ReturnInst::Create(F->getContext(), RetVal, RI); | 1059 ReturnInst::Create(F->getContext(), RetVal, RI); |
1087 BB->getInstList().erase(RI); | 1060 BB->getInstList().erase(RI); |
1088 } | 1061 } |
1089 | 1062 |
1090 // Patch the pointer to LLVM function in debug info descriptor. | 1063 // Patch the pointer to LLVM function in debug info descriptor. |
1091 FunctionDIMap::iterator DI = FunctionDIs.find(F); | 1064 auto DI = FunctionDIs.find(F); |
1092 if (DI != FunctionDIs.end()) | 1065 if (DI != FunctionDIs.end()) |
1093 DI->second.replaceFunction(NF); | 1066 DI->second.replaceFunction(NF); |
1094 | 1067 |
1095 // Now that the old function is dead, delete it. | 1068 // Now that the old function is dead, delete it. |
1096 F->eraseFromParent(); | 1069 F->eraseFromParent(); |
1100 | 1073 |
1101 bool DAE::runOnModule(Module &M) { | 1074 bool DAE::runOnModule(Module &M) { |
1102 bool Changed = false; | 1075 bool Changed = false; |
1103 | 1076 |
1104 // Collect debug info descriptors for functions. | 1077 // Collect debug info descriptors for functions. |
1105 CollectFunctionDIs(M); | 1078 FunctionDIs = makeSubprogramMap(M); |
1106 | 1079 |
1107 // First pass: Do a simple check to see if any functions can have their "..." | 1080 // First pass: Do a simple check to see if any functions can have their "..." |
1108 // removed. We can do this if they never call va_start. This loop cannot be | 1081 // removed. We can do this if they never call va_start. This loop cannot be |
1109 // fused with the next loop, because deleting a function invalidates | 1082 // fused with the next loop, because deleting a function invalidates |
1110 // information computed while surveying other functions. | 1083 // information computed while surveying other functions. |