Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1 | //===- InlineFunction.cpp - Code to perform function inlining -------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements inlining of a function into a call site, resolving |
| 11 | // parameters and the return value as appropriate. |
| 12 | // |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/Cloning.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame^] | 16 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame^] | 18 | #include "llvm/ADT/SetVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
| 20 | #include "llvm/Analysis/CallGraph.h" |
| 21 | #include "llvm/Analysis/InstructionSimplify.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame^] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Attributes.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 24 | #include "llvm/IR/CallSite.h" |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
| 27 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DerivedTypes.h" |
| 30 | #include "llvm/IR/IRBuilder.h" |
| 31 | #include "llvm/IR/Instructions.h" |
| 32 | #include "llvm/IR/IntrinsicInst.h" |
| 33 | #include "llvm/IR/Intrinsics.h" |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame^] | 34 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Module.h" |
Chandler Carruth | aafe091 | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | df3c342 | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 37 | using namespace llvm; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 38 | |
Eric Christopher | f16bee8 | 2012-03-26 19:09:38 +0000 | [diff] [blame] | 39 | bool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI, |
| 40 | bool InsertLifetime) { |
Chad Rosier | 07d37bc | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 41 | return InlineFunction(CallSite(CI), IFI, InsertLifetime); |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 42 | } |
Eric Christopher | f16bee8 | 2012-03-26 19:09:38 +0000 | [diff] [blame] | 43 | bool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI, |
| 44 | bool InsertLifetime) { |
Chad Rosier | 07d37bc | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 45 | return InlineFunction(CallSite(II), IFI, InsertLifetime); |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 47 | |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 48 | namespace { |
| 49 | /// A class for recording information about inlining through an invoke. |
| 50 | class InvokeInliningInfo { |
Dmitri Gribenko | dbeafa7 | 2012-06-09 00:01:45 +0000 | [diff] [blame] | 51 | BasicBlock *OuterResumeDest; ///< Destination of the invoke's unwind. |
| 52 | BasicBlock *InnerResumeDest; ///< Destination for the callee's resume. |
| 53 | LandingPadInst *CallerLPad; ///< LandingPadInst associated with the invoke. |
| 54 | PHINode *InnerEHValuesPHI; ///< PHI for EH values from landingpad insts. |
Bill Wendling | 0c2d82b | 2012-01-31 01:22:03 +0000 | [diff] [blame] | 55 | SmallVector<Value*, 8> UnwindDestPHIValues; |
Bill Wendling | fa28440 | 2011-07-28 07:31:46 +0000 | [diff] [blame] | 56 | |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 57 | public: |
| 58 | InvokeInliningInfo(InvokeInst *II) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 59 | : OuterResumeDest(II->getUnwindDest()), InnerResumeDest(nullptr), |
| 60 | CallerLPad(nullptr), InnerEHValuesPHI(nullptr) { |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 61 | // If there are PHI nodes in the unwind destination block, we need to keep |
| 62 | // track of which values came into them from the invoke before removing |
| 63 | // the edge from this block. |
| 64 | llvm::BasicBlock *InvokeBB = II->getParent(); |
Bill Wendling | ea6e935 | 2012-01-31 01:25:54 +0000 | [diff] [blame] | 65 | BasicBlock::iterator I = OuterResumeDest->begin(); |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 66 | for (; isa<PHINode>(I); ++I) { |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 67 | // Save the value to use for this edge. |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 68 | PHINode *PHI = cast<PHINode>(I); |
| 69 | UnwindDestPHIValues.push_back(PHI->getIncomingValueForBlock(InvokeBB)); |
| 70 | } |
| 71 | |
Bill Wendling | f3cae51 | 2012-01-31 00:56:53 +0000 | [diff] [blame] | 72 | CallerLPad = cast<LandingPadInst>(I); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Bill Wendling | ea6e935 | 2012-01-31 01:25:54 +0000 | [diff] [blame] | 75 | /// getOuterResumeDest - The outer unwind destination is the target of |
| 76 | /// unwind edges introduced for calls within the inlined function. |
Bill Wendling | 0c2d82b | 2012-01-31 01:22:03 +0000 | [diff] [blame] | 77 | BasicBlock *getOuterResumeDest() const { |
Bill Wendling | ea6e935 | 2012-01-31 01:25:54 +0000 | [diff] [blame] | 78 | return OuterResumeDest; |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Bill Wendling | 3fd879d | 2012-01-31 01:48:40 +0000 | [diff] [blame] | 81 | BasicBlock *getInnerResumeDest(); |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 82 | |
| 83 | LandingPadInst *getLandingPadInst() const { return CallerLPad; } |
| 84 | |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 85 | /// forwardResume - Forward the 'resume' instruction to the caller's landing |
| 86 | /// pad block. When the landing pad block has only one predecessor, this is |
| 87 | /// a simple branch. When there is more than one predecessor, we need to |
| 88 | /// split the landing pad block after the landingpad instruction and jump |
| 89 | /// to there. |
Bill Wendling | 56f15bf | 2013-03-22 20:31:05 +0000 | [diff] [blame] | 90 | void forwardResume(ResumeInst *RI, |
| 91 | SmallPtrSet<LandingPadInst*, 16> &InlinedLPads); |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 92 | |
| 93 | /// addIncomingPHIValuesFor - Add incoming-PHI values to the unwind |
| 94 | /// destination block for the given basic block, using the values for the |
| 95 | /// original invoke's source block. |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 96 | void addIncomingPHIValuesFor(BasicBlock *BB) const { |
Bill Wendling | ea6e935 | 2012-01-31 01:25:54 +0000 | [diff] [blame] | 97 | addIncomingPHIValuesForInto(BB, OuterResumeDest); |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 98 | } |
Bill Wendling | ad088e6 | 2011-07-30 05:42:50 +0000 | [diff] [blame] | 99 | |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 100 | void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const { |
| 101 | BasicBlock::iterator I = dest->begin(); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 102 | for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { |
Bill Wendling | ad088e6 | 2011-07-30 05:42:50 +0000 | [diff] [blame] | 103 | PHINode *phi = cast<PHINode>(I); |
| 104 | phi->addIncoming(UnwindDestPHIValues[i], src); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | }; |
| 108 | } |
| 109 | |
Bill Wendling | 3fd879d | 2012-01-31 01:48:40 +0000 | [diff] [blame] | 110 | /// getInnerResumeDest - Get or create a target for the branch from ResumeInsts. |
| 111 | BasicBlock *InvokeInliningInfo::getInnerResumeDest() { |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 112 | if (InnerResumeDest) return InnerResumeDest; |
| 113 | |
| 114 | // Split the landing pad. |
| 115 | BasicBlock::iterator SplitPoint = CallerLPad; ++SplitPoint; |
| 116 | InnerResumeDest = |
| 117 | OuterResumeDest->splitBasicBlock(SplitPoint, |
| 118 | OuterResumeDest->getName() + ".body"); |
| 119 | |
| 120 | // The number of incoming edges we expect to the inner landing pad. |
| 121 | const unsigned PHICapacity = 2; |
| 122 | |
| 123 | // Create corresponding new PHIs for all the PHIs in the outer landing pad. |
| 124 | BasicBlock::iterator InsertPoint = InnerResumeDest->begin(); |
| 125 | BasicBlock::iterator I = OuterResumeDest->begin(); |
| 126 | for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) { |
| 127 | PHINode *OuterPHI = cast<PHINode>(I); |
| 128 | PHINode *InnerPHI = PHINode::Create(OuterPHI->getType(), PHICapacity, |
| 129 | OuterPHI->getName() + ".lpad-body", |
| 130 | InsertPoint); |
| 131 | OuterPHI->replaceAllUsesWith(InnerPHI); |
| 132 | InnerPHI->addIncoming(OuterPHI, OuterResumeDest); |
| 133 | } |
| 134 | |
| 135 | // Create a PHI for the exception values. |
| 136 | InnerEHValuesPHI = PHINode::Create(CallerLPad->getType(), PHICapacity, |
| 137 | "eh.lpad-body", InsertPoint); |
| 138 | CallerLPad->replaceAllUsesWith(InnerEHValuesPHI); |
| 139 | InnerEHValuesPHI->addIncoming(CallerLPad, OuterResumeDest); |
| 140 | |
| 141 | // All done. |
| 142 | return InnerResumeDest; |
| 143 | } |
| 144 | |
| 145 | /// forwardResume - Forward the 'resume' instruction to the caller's landing pad |
| 146 | /// block. When the landing pad block has only one predecessor, this is a simple |
| 147 | /// branch. When there is more than one predecessor, we need to split the |
| 148 | /// landing pad block after the landingpad instruction and jump to there. |
Bill Wendling | 173c71f | 2013-03-21 23:30:12 +0000 | [diff] [blame] | 149 | void InvokeInliningInfo::forwardResume(ResumeInst *RI, |
Bill Wendling | 56f15bf | 2013-03-22 20:31:05 +0000 | [diff] [blame] | 150 | SmallPtrSet<LandingPadInst*, 16> &InlinedLPads) { |
Bill Wendling | 3fd879d | 2012-01-31 01:48:40 +0000 | [diff] [blame] | 151 | BasicBlock *Dest = getInnerResumeDest(); |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 152 | BasicBlock *Src = RI->getParent(); |
| 153 | |
| 154 | BranchInst::Create(Dest, Src); |
| 155 | |
| 156 | // Update the PHIs in the destination. They were inserted in an order which |
| 157 | // makes this work. |
| 158 | addIncomingPHIValuesForInto(Src, Dest); |
| 159 | |
| 160 | InnerEHValuesPHI->addIncoming(RI->getOperand(0), Src); |
| 161 | RI->eraseFromParent(); |
| 162 | } |
| 163 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 164 | /// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into |
Eric Christopher | 66d8555f | 2009-09-06 22:20:54 +0000 | [diff] [blame] | 165 | /// an invoke, we have to turn all of the calls that can throw into |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 166 | /// invokes. This function analyze BB to see if there are any calls, and if so, |
| 167 | /// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI |
Chris Lattner | 8900f3e | 2009-09-01 18:44:06 +0000 | [diff] [blame] | 168 | /// nodes in that block with the values specified in InvokeDestPHIValues. |
Mark Seaborn | d91fa22 | 2013-12-02 20:50:59 +0000 | [diff] [blame] | 169 | static void HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB, |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 170 | InvokeInliningInfo &Invoke) { |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 171 | for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) { |
| 172 | Instruction *I = BBI++; |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 174 | // We only need to check for function calls: inlined invoke |
| 175 | // instructions require no special handling. |
| 176 | CallInst *CI = dyn_cast<CallInst>(I); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 178 | // If this call cannot unwind, don't convert it to an invoke. |
Manman Ren | 87a2adc | 2013-10-31 21:56:03 +0000 | [diff] [blame] | 179 | // Inline asm calls cannot throw. |
| 180 | if (!CI || CI->doesNotThrow() || isa<InlineAsm>(CI->getCalledValue())) |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 181 | continue; |
Bill Wendling | 518a205 | 2012-01-31 01:05:20 +0000 | [diff] [blame] | 182 | |
| 183 | // Convert this function call into an invoke instruction. First, split the |
| 184 | // basic block. |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 185 | BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc"); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 186 | |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 187 | // Delete the unconditional branch inserted by splitBasicBlock |
| 188 | BB->getInstList().pop_back(); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 189 | |
Bill Wendling | 621699d | 2012-01-31 01:14:49 +0000 | [diff] [blame] | 190 | // Create the new invoke instruction. |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 191 | ImmutableCallSite CS(CI); |
| 192 | SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end()); |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 193 | InvokeInst *II = InvokeInst::Create(CI->getCalledValue(), Split, |
Bill Wendling | 0c2d82b | 2012-01-31 01:22:03 +0000 | [diff] [blame] | 194 | Invoke.getOuterResumeDest(), |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 195 | InvokeArgs, CI->getName(), BB); |
David Blaikie | 644d2ee | 2014-06-30 20:30:39 +0000 | [diff] [blame] | 196 | II->setDebugLoc(CI->getDebugLoc()); |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 197 | II->setCallingConv(CI->getCallingConv()); |
| 198 | II->setAttributes(CI->getAttributes()); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 199 | |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 200 | // Make sure that anything using the call now uses the invoke! This also |
| 201 | // updates the CallGraph if present, because it uses a WeakVH. |
| 202 | CI->replaceAllUsesWith(II); |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 203 | |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 204 | // Delete the original call |
| 205 | Split->getInstList().pop_front(); |
John McCall | 046c47e | 2011-05-28 07:45:59 +0000 | [diff] [blame] | 206 | |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 207 | // Update any PHI nodes in the exceptional block to indicate that there is |
| 208 | // now a new entry in them. |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 209 | Invoke.addIncomingPHIValuesFor(BB); |
Mark Seaborn | d91fa22 | 2013-12-02 20:50:59 +0000 | [diff] [blame] | 210 | return; |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 214 | /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 215 | /// in the body of the inlined function into invokes. |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 216 | /// |
Nick Lewycky | 12a130b | 2009-02-03 04:34:40 +0000 | [diff] [blame] | 217 | /// II is the invoke instruction being inlined. FirstNewBlock is the first |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 218 | /// block of the inlined code (the last block is the end of the function), |
| 219 | /// and InlineCodeInfo is information about the code that got inlined. |
| 220 | static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock, |
Chris Lattner | 8900f3e | 2009-09-01 18:44:06 +0000 | [diff] [blame] | 221 | ClonedCodeInfo &InlinedCodeInfo) { |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 222 | BasicBlock *InvokeDest = II->getUnwindDest(); |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 223 | |
| 224 | Function *Caller = FirstNewBlock->getParent(); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 226 | // The inlined code is currently at the end of the function, scan from the |
| 227 | // start of the inlined code to its end, checking for stuff we need to |
Bill Wendling | 173c71f | 2013-03-21 23:30:12 +0000 | [diff] [blame] | 228 | // rewrite. |
John McCall | bd04b74 | 2011-05-27 18:34:38 +0000 | [diff] [blame] | 229 | InvokeInliningInfo Invoke(II); |
Bill Wendling | 173c71f | 2013-03-21 23:30:12 +0000 | [diff] [blame] | 230 | |
Bill Wendling | 56f15bf | 2013-03-22 20:31:05 +0000 | [diff] [blame] | 231 | // Get all of the inlined landing pad instructions. |
| 232 | SmallPtrSet<LandingPadInst*, 16> InlinedLPads; |
| 233 | for (Function::iterator I = FirstNewBlock, E = Caller->end(); I != E; ++I) |
| 234 | if (InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) |
| 235 | InlinedLPads.insert(II->getLandingPadInst()); |
| 236 | |
Mark Seaborn | ef3dbb9 | 2013-12-08 00:50:58 +0000 | [diff] [blame] | 237 | // Append the clauses from the outer landing pad instruction into the inlined |
| 238 | // landing pad instructions. |
| 239 | LandingPadInst *OuterLPad = Invoke.getLandingPadInst(); |
| 240 | for (SmallPtrSet<LandingPadInst*, 16>::iterator I = InlinedLPads.begin(), |
| 241 | E = InlinedLPads.end(); I != E; ++I) { |
| 242 | LandingPadInst *InlinedLPad = *I; |
| 243 | unsigned OuterNum = OuterLPad->getNumClauses(); |
| 244 | InlinedLPad->reserveClauses(OuterNum); |
| 245 | for (unsigned OuterIdx = 0; OuterIdx != OuterNum; ++OuterIdx) |
| 246 | InlinedLPad->addClause(OuterLPad->getClause(OuterIdx)); |
Mark Seaborn | 1b3dd35 | 2013-12-08 00:51:21 +0000 | [diff] [blame] | 247 | if (OuterLPad->isCleanup()) |
| 248 | InlinedLPad->setCleanup(true); |
Mark Seaborn | ef3dbb9 | 2013-12-08 00:50:58 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 251 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){ |
| 252 | if (InlinedCodeInfo.ContainsCalls) |
Mark Seaborn | d91fa22 | 2013-12-02 20:50:59 +0000 | [diff] [blame] | 253 | HandleCallsInBlockInlinedThroughInvoke(BB, Invoke); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 254 | |
Bill Wendling | 173c71f | 2013-03-21 23:30:12 +0000 | [diff] [blame] | 255 | // Forward any resumes that are remaining here. |
Bill Wendling | 621699d | 2012-01-31 01:14:49 +0000 | [diff] [blame] | 256 | if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) |
Bill Wendling | 56f15bf | 2013-03-22 20:31:05 +0000 | [diff] [blame] | 257 | Invoke.forwardResume(RI, InlinedLPads); |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Now that everything is happy, we have one final detail. The PHI nodes in |
| 261 | // the exception destination block still have entries due to the original |
Bill Wendling | 173c71f | 2013-03-21 23:30:12 +0000 | [diff] [blame] | 262 | // invoke instruction. Eliminate these entries (which might even delete the |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 263 | // PHI node) now. |
| 264 | InvokeDest->removePredecessor(II->getParent()); |
| 265 | } |
| 266 | |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame^] | 267 | /// CloneAliasScopeMetadata - When inlining a function that contains noalias |
| 268 | /// scope metadata, this metadata needs to be cloned so that the inlined blocks |
| 269 | /// have different "unqiue scopes" at every call site. Were this not done, then |
| 270 | /// aliasing scopes from a function inlined into a caller multiple times could |
| 271 | /// not be differentiated (and this would lead to miscompiles because the |
| 272 | /// non-aliasing property communicated by the metadata could have |
| 273 | /// call-site-specific control dependencies). |
| 274 | static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) { |
| 275 | const Function *CalledFunc = CS.getCalledFunction(); |
| 276 | SetVector<const MDNode *> MD; |
| 277 | |
| 278 | // Note: We could only clone the metadata if it is already used in the |
| 279 | // caller. I'm omitting that check here because it might confuse |
| 280 | // inter-procedural alias analysis passes. We can revisit this if it becomes |
| 281 | // an efficiency or overhead problem. |
| 282 | |
| 283 | for (Function::const_iterator I = CalledFunc->begin(), IE = CalledFunc->end(); |
| 284 | I != IE; ++I) |
| 285 | for (BasicBlock::const_iterator J = I->begin(), JE = I->end(); J != JE; ++J) { |
| 286 | if (const MDNode *M = J->getMetadata(LLVMContext::MD_alias_scope)) |
| 287 | MD.insert(M); |
| 288 | if (const MDNode *M = J->getMetadata(LLVMContext::MD_noalias)) |
| 289 | MD.insert(M); |
| 290 | } |
| 291 | |
| 292 | if (MD.empty()) |
| 293 | return; |
| 294 | |
| 295 | // Walk the existing metadata, adding the complete (perhaps cyclic) chain to |
| 296 | // the set. |
| 297 | SmallVector<const Value *, 16> Queue(MD.begin(), MD.end()); |
| 298 | while (!Queue.empty()) { |
| 299 | const MDNode *M = cast<MDNode>(Queue.pop_back_val()); |
| 300 | for (unsigned i = 0, ie = M->getNumOperands(); i != ie; ++i) |
| 301 | if (const MDNode *M1 = dyn_cast<MDNode>(M->getOperand(i))) |
| 302 | if (MD.insert(M1)) |
| 303 | Queue.push_back(M1); |
| 304 | } |
| 305 | |
| 306 | // Now we have a complete set of all metadata in the chains used to specify |
| 307 | // the noalias scopes and the lists of those scopes. |
| 308 | SmallVector<MDNode *, 16> DummyNodes; |
| 309 | DenseMap<const MDNode *, TrackingVH<MDNode> > MDMap; |
| 310 | for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end(); |
| 311 | I != IE; ++I) { |
| 312 | MDNode *Dummy = MDNode::getTemporary(CalledFunc->getContext(), |
| 313 | ArrayRef<Value*>()); |
| 314 | DummyNodes.push_back(Dummy); |
| 315 | MDMap[*I] = Dummy; |
| 316 | } |
| 317 | |
| 318 | // Create new metadata nodes to replace the dummy nodes, replacing old |
| 319 | // metadata references with either a dummy node or an already-created new |
| 320 | // node. |
| 321 | for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end(); |
| 322 | I != IE; ++I) { |
| 323 | SmallVector<Value *, 4> NewOps; |
| 324 | for (unsigned i = 0, ie = (*I)->getNumOperands(); i != ie; ++i) { |
| 325 | const Value *V = (*I)->getOperand(i); |
| 326 | if (const MDNode *M = dyn_cast<MDNode>(V)) |
| 327 | NewOps.push_back(MDMap[M]); |
| 328 | else |
| 329 | NewOps.push_back(const_cast<Value *>(V)); |
| 330 | } |
| 331 | |
| 332 | MDNode *NewM = MDNode::get(CalledFunc->getContext(), NewOps), |
| 333 | *TempM = MDMap[*I]; |
| 334 | |
| 335 | TempM->replaceAllUsesWith(NewM); |
| 336 | } |
| 337 | |
| 338 | // Now replace the metadata in the new inlined instructions with the |
| 339 | // repacements from the map. |
| 340 | for (ValueToValueMapTy::iterator VMI = VMap.begin(), VMIE = VMap.end(); |
| 341 | VMI != VMIE; ++VMI) { |
| 342 | if (!VMI->second) |
| 343 | continue; |
| 344 | |
| 345 | Instruction *NI = dyn_cast<Instruction>(VMI->second); |
| 346 | if (!NI) |
| 347 | continue; |
| 348 | |
| 349 | if (MDNode *M = NI->getMetadata(LLVMContext::MD_alias_scope)) |
| 350 | NI->setMetadata(LLVMContext::MD_alias_scope, MDMap[M]); |
| 351 | |
| 352 | if (MDNode *M = NI->getMetadata(LLVMContext::MD_noalias)) |
| 353 | NI->setMetadata(LLVMContext::MD_noalias, MDMap[M]); |
| 354 | } |
| 355 | |
| 356 | // Now that everything has been replaced, delete the dummy nodes. |
| 357 | for (unsigned i = 0, ie = DummyNodes.size(); i != ie; ++i) |
| 358 | MDNode::deleteTemporary(DummyNodes[i]); |
| 359 | } |
| 360 | |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 361 | /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee |
| 362 | /// into the caller, update the specified callgraph to reflect the changes we |
| 363 | /// made. Note that it's possible that not all code was copied over, so only |
Duncan Sands | 46911f1 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 364 | /// some edges of the callgraph may remain. |
| 365 | static void UpdateCallGraphAfterInlining(CallSite CS, |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 366 | Function::iterator FirstNewBlock, |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 367 | ValueToValueMapTy &VMap, |
Chris Lattner | 2eee5d3 | 2010-04-22 23:37:35 +0000 | [diff] [blame] | 368 | InlineFunctionInfo &IFI) { |
| 369 | CallGraph &CG = *IFI.CG; |
Duncan Sands | 46911f1 | 2008-09-08 11:05:51 +0000 | [diff] [blame] | 370 | const Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 371 | const Function *Callee = CS.getCalledFunction(); |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 372 | CallGraphNode *CalleeNode = CG[Callee]; |
| 373 | CallGraphNode *CallerNode = CG[Caller]; |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 375 | // Since we inlined some uninlined call sites in the callee into the caller, |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 376 | // add edges from the caller to all of the callees of the callee. |
Gabor Greif | 5aa1922 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 377 | CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end(); |
| 378 | |
| 379 | // Consider the case where CalleeNode == CallerNode. |
Gabor Greif | f1abfdc | 2009-01-17 00:09:08 +0000 | [diff] [blame] | 380 | CallGraphNode::CalledFunctionsVector CallCache; |
Gabor Greif | 5aa1922 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 381 | if (CalleeNode == CallerNode) { |
| 382 | CallCache.assign(I, E); |
| 383 | I = CallCache.begin(); |
| 384 | E = CallCache.end(); |
| 385 | } |
| 386 | |
| 387 | for (; I != E; ++I) { |
Chris Lattner | 063d065 | 2009-09-01 06:31:31 +0000 | [diff] [blame] | 388 | const Value *OrigCall = I->first; |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 389 | |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 390 | ValueToValueMapTy::iterator VMI = VMap.find(OrigCall); |
Chris Lattner | b3c64f7 | 2006-07-12 21:37:11 +0000 | [diff] [blame] | 391 | // Only copy the edge if the call was inlined! |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 392 | if (VMI == VMap.end() || VMI->second == nullptr) |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 393 | continue; |
| 394 | |
| 395 | // If the call was inlined, but then constant folded, there is no edge to |
| 396 | // add. Check for this case. |
Chris Lattner | 016c00a | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 397 | Instruction *NewCall = dyn_cast<Instruction>(VMI->second); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 398 | if (!NewCall) continue; |
Chris Lattner | c2432b9 | 2010-05-01 01:26:13 +0000 | [diff] [blame] | 399 | |
| 400 | // Remember that this call site got inlined for the client of |
| 401 | // InlineFunction. |
| 402 | IFI.InlinedCalls.push_back(NewCall); |
| 403 | |
Chris Lattner | 016c00a | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 404 | // It's possible that inlining the callsite will cause it to go from an |
| 405 | // indirect to a direct call by resolving a function pointer. If this |
| 406 | // happens, set the callee of the new call site to a more precise |
| 407 | // destination. This can also happen if the call graph node of the caller |
| 408 | // was just unnecessarily imprecise. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 409 | if (!I->second->getFunction()) |
Chris Lattner | 016c00a | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 410 | if (Function *F = CallSite(NewCall).getCalledFunction()) { |
| 411 | // Indirect call site resolved to direct call. |
Gabor Greif | 7b0a5fd | 2010-07-27 15:02:37 +0000 | [diff] [blame] | 412 | CallerNode->addCalledFunction(CallSite(NewCall), CG[F]); |
| 413 | |
Chris Lattner | 016c00a | 2010-04-22 21:31:00 +0000 | [diff] [blame] | 414 | continue; |
| 415 | } |
Gabor Greif | 7b0a5fd | 2010-07-27 15:02:37 +0000 | [diff] [blame] | 416 | |
| 417 | CallerNode->addCalledFunction(CallSite(NewCall), I->second); |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 418 | } |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 419 | |
Dale Johannesen | 0aeabdf | 2009-01-13 22:43:37 +0000 | [diff] [blame] | 420 | // Update the call graph by deleting the edge from Callee to Caller. We must |
| 421 | // do this after the loop above in case Caller and Callee are the same. |
| 422 | CallerNode->removeCallEdgeFor(CS); |
Chris Lattner | 0841fb1 | 2006-01-14 20:07:50 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Julien Lerouge | 957e91c | 2014-04-15 18:01:54 +0000 | [diff] [blame] | 425 | static void HandleByValArgumentInit(Value *Dst, Value *Src, Module *M, |
| 426 | BasicBlock *InsertBlock, |
| 427 | InlineFunctionInfo &IFI) { |
| 428 | LLVMContext &Context = Src->getContext(); |
| 429 | Type *VoidPtrTy = Type::getInt8PtrTy(Context); |
| 430 | Type *AggTy = cast<PointerType>(Src->getType())->getElementType(); |
| 431 | Type *Tys[3] = { VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context) }; |
| 432 | Function *MemCpyFn = Intrinsic::getDeclaration(M, Intrinsic::memcpy, Tys); |
| 433 | IRBuilder<> builder(InsertBlock->begin()); |
| 434 | Value *DstCast = builder.CreateBitCast(Dst, VoidPtrTy, "tmp"); |
| 435 | Value *SrcCast = builder.CreateBitCast(Src, VoidPtrTy, "tmp"); |
| 436 | |
| 437 | Value *Size; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 438 | if (IFI.DL == nullptr) |
Julien Lerouge | 957e91c | 2014-04-15 18:01:54 +0000 | [diff] [blame] | 439 | Size = ConstantExpr::getSizeOf(AggTy); |
| 440 | else |
| 441 | Size = ConstantInt::get(Type::getInt64Ty(Context), |
| 442 | IFI.DL->getTypeStoreSize(AggTy)); |
| 443 | |
| 444 | // Always generate a memcpy of alignment 1 here because we don't know |
| 445 | // the alignment of the src pointer. Other optimizations can infer |
| 446 | // better alignment. |
| 447 | Value *CallArgs[] = { |
| 448 | DstCast, SrcCast, Size, |
| 449 | ConstantInt::get(Type::getInt32Ty(Context), 1), |
| 450 | ConstantInt::getFalse(Context) // isVolatile |
| 451 | }; |
| 452 | builder.CreateCall(MemCpyFn, CallArgs); |
| 453 | } |
| 454 | |
Chris Lattner | 0f11495 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 455 | /// HandleByValArgument - When inlining a call site that has a byval argument, |
| 456 | /// we have to make the implicit memcpy explicit by adding it. |
David Majnemer | 120f4a0 | 2013-11-03 12:22:13 +0000 | [diff] [blame] | 457 | static Value *HandleByValArgument(Value *Arg, Instruction *TheCall, |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 458 | const Function *CalledFunc, |
| 459 | InlineFunctionInfo &IFI, |
| 460 | unsigned ByValAlignment) { |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 461 | PointerType *ArgTy = cast<PointerType>(Arg->getType()); |
| 462 | Type *AggTy = ArgTy->getElementType(); |
Chris Lattner | 0f11495 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 463 | |
| 464 | // If the called function is readonly, then it could not mutate the caller's |
| 465 | // copy of the byval'd memory. In this case, it is safe to elide the copy and |
| 466 | // temporary. |
David Majnemer | 120f4a0 | 2013-11-03 12:22:13 +0000 | [diff] [blame] | 467 | if (CalledFunc->onlyReadsMemory()) { |
Chris Lattner | 0f11495 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 468 | // If the byval argument has a specified alignment that is greater than the |
| 469 | // passed in pointer, then we either have to round up the input pointer or |
| 470 | // give up on this transformation. |
| 471 | if (ByValAlignment <= 1) // 0 = unspecified, 1 = no particular alignment. |
David Majnemer | 120f4a0 | 2013-11-03 12:22:13 +0000 | [diff] [blame] | 472 | return Arg; |
Chris Lattner | 0f11495 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 20fca48 | 2010-12-25 20:42:38 +0000 | [diff] [blame] | 474 | // If the pointer is already known to be sufficiently aligned, or if we can |
| 475 | // round it up to a larger alignment, then we don't need a temporary. |
David Majnemer | 120f4a0 | 2013-11-03 12:22:13 +0000 | [diff] [blame] | 476 | if (getOrEnforceKnownAlignment(Arg, ByValAlignment, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 477 | IFI.DL) >= ByValAlignment) |
David Majnemer | 120f4a0 | 2013-11-03 12:22:13 +0000 | [diff] [blame] | 478 | return Arg; |
Chris Lattner | 0f11495 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 479 | |
Chris Lattner | 20fca48 | 2010-12-25 20:42:38 +0000 | [diff] [blame] | 480 | // Otherwise, we have to make a memcpy to get a safe alignment. This is bad |
| 481 | // for code quality, but rarely happens and is required for correctness. |
Chris Lattner | 0f11495 | 2010-12-20 08:10:40 +0000 | [diff] [blame] | 482 | } |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 483 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 484 | // Create the alloca. If we have DataLayout, use nice alignment. |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 485 | unsigned Align = 1; |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 486 | if (IFI.DL) |
| 487 | Align = IFI.DL->getPrefTypeAlignment(AggTy); |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 488 | |
| 489 | // If the byval had an alignment specified, we *must* use at least that |
| 490 | // alignment, as it is required by the byval argument (and uses of the |
| 491 | // pointer inside the callee). |
| 492 | Align = std::max(Align, ByValAlignment); |
| 493 | |
| 494 | Function *Caller = TheCall->getParent()->getParent(); |
| 495 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 496 | Value *NewAlloca = new AllocaInst(AggTy, nullptr, Align, Arg->getName(), |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 497 | &*Caller->begin()->begin()); |
Julien Lerouge | be4fe32 | 2014-04-15 18:06:46 +0000 | [diff] [blame] | 498 | IFI.StaticAllocas.push_back(cast<AllocaInst>(NewAlloca)); |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 499 | |
| 500 | // Uses of the argument in the function should use our new alloca |
| 501 | // instead. |
| 502 | return NewAlloca; |
| 503 | } |
| 504 | |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 505 | // isUsedByLifetimeMarker - Check whether this Value is used by a lifetime |
| 506 | // intrinsic. |
| 507 | static bool isUsedByLifetimeMarker(Value *V) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 508 | for (User *U : V->users()) { |
| 509 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) { |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 510 | switch (II->getIntrinsicID()) { |
| 511 | default: break; |
| 512 | case Intrinsic::lifetime_start: |
| 513 | case Intrinsic::lifetime_end: |
| 514 | return true; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | // hasLifetimeMarkers - Check whether the given alloca already has |
| 522 | // lifetime.start or lifetime.end intrinsics. |
| 523 | static bool hasLifetimeMarkers(AllocaInst *AI) { |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 524 | Type *Ty = AI->getType(); |
| 525 | Type *Int8PtrTy = Type::getInt8PtrTy(Ty->getContext(), |
| 526 | Ty->getPointerAddressSpace()); |
| 527 | if (Ty == Int8PtrTy) |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 528 | return isUsedByLifetimeMarker(AI); |
| 529 | |
Nick Lewycky | 9711b5c | 2011-06-14 00:59:24 +0000 | [diff] [blame] | 530 | // Do a scan to find all the casts to i8*. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 531 | for (User *U : AI->users()) { |
| 532 | if (U->getType() != Int8PtrTy) continue; |
| 533 | if (U->stripPointerCasts() != AI) continue; |
| 534 | if (isUsedByLifetimeMarker(U)) |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 535 | return true; |
| 536 | } |
| 537 | return false; |
| 538 | } |
| 539 | |
Eric Christopher | f16bee8 | 2012-03-26 19:09:38 +0000 | [diff] [blame] | 540 | /// updateInlinedAtInfo - Helper function used by fixupLineNumbers to |
| 541 | /// recursively update InlinedAtEntry of a DebugLoc. |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 542 | static DebugLoc updateInlinedAtInfo(const DebugLoc &DL, |
| 543 | const DebugLoc &InlinedAtDL, |
| 544 | LLVMContext &Ctx) { |
| 545 | if (MDNode *IA = DL.getInlinedAt(Ctx)) { |
| 546 | DebugLoc NewInlinedAtDL |
| 547 | = updateInlinedAtInfo(DebugLoc::getFromDILocation(IA), InlinedAtDL, Ctx); |
| 548 | return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx), |
| 549 | NewInlinedAtDL.getAsMDNode(Ctx)); |
| 550 | } |
Eric Christopher | f16bee8 | 2012-03-26 19:09:38 +0000 | [diff] [blame] | 551 | |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 552 | return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx), |
| 553 | InlinedAtDL.getAsMDNode(Ctx)); |
| 554 | } |
| 555 | |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 556 | /// fixupLineNumbers - Update inlined instructions' line numbers to |
| 557 | /// to encode location where these instructions are inlined. |
| 558 | static void fixupLineNumbers(Function *Fn, Function::iterator FI, |
Eric Christopher | 2b40fdf | 2012-03-26 19:09:40 +0000 | [diff] [blame] | 559 | Instruction *TheCall) { |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 560 | DebugLoc TheCallDL = TheCall->getDebugLoc(); |
| 561 | if (TheCallDL.isUnknown()) |
| 562 | return; |
| 563 | |
| 564 | for (; FI != Fn->end(); ++FI) { |
| 565 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); |
| 566 | BI != BE; ++BI) { |
| 567 | DebugLoc DL = BI->getDebugLoc(); |
Evgeniy Stepanov | 2be2992 | 2014-06-09 09:09:19 +0000 | [diff] [blame] | 568 | if (DL.isUnknown()) { |
| 569 | // If the inlined instruction has no line number, make it look as if it |
| 570 | // originates from the call location. This is important for |
| 571 | // ((__always_inline__, __nodebug__)) functions which must use caller |
| 572 | // location for all instructions in their function body. |
| 573 | BI->setDebugLoc(TheCallDL); |
| 574 | } else { |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 575 | BI->setDebugLoc(updateInlinedAtInfo(DL, TheCallDL, BI->getContext())); |
Devang Patel | bb23a4a | 2011-08-10 21:50:54 +0000 | [diff] [blame] | 576 | if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) { |
| 577 | LLVMContext &Ctx = BI->getContext(); |
| 578 | MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx); |
| 579 | DVI->setOperand(2, createInlinedVariable(DVI->getVariable(), |
| 580 | InlinedAt, Ctx)); |
| 581 | } |
| 582 | } |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 587 | /// Returns a musttail call instruction if one immediately precedes the given |
| 588 | /// return instruction with an optional bitcast instruction between them. |
| 589 | static CallInst *getPrecedingMustTailCall(ReturnInst *RI) { |
| 590 | Instruction *Prev = RI->getPrevNode(); |
| 591 | if (!Prev) |
| 592 | return nullptr; |
| 593 | |
| 594 | if (Value *RV = RI->getReturnValue()) { |
| 595 | if (RV != Prev) |
| 596 | return nullptr; |
| 597 | |
| 598 | // Look through the optional bitcast. |
| 599 | if (auto *BI = dyn_cast<BitCastInst>(Prev)) { |
| 600 | RV = BI->getOperand(0); |
| 601 | Prev = BI->getPrevNode(); |
| 602 | if (!Prev || RV != Prev) |
| 603 | return nullptr; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (auto *CI = dyn_cast<CallInst>(Prev)) { |
| 608 | if (CI->isMustTailCall()) |
| 609 | return CI; |
| 610 | } |
| 611 | return nullptr; |
| 612 | } |
| 613 | |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 614 | /// InlineFunction - This function inlines the called function into the basic |
| 615 | /// block of the caller. This returns false if it is not possible to inline |
| 616 | /// this call. The program is still in a well defined state if this occurs |
| 617 | /// though. |
| 618 | /// |
| 619 | /// Note that this only does one level of inlining. For example, if the |
| 620 | /// instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now |
| 621 | /// exists in the instruction stream. Similarly this will inline a recursive |
| 622 | /// function by one level. |
Eric Christopher | f16bee8 | 2012-03-26 19:09:38 +0000 | [diff] [blame] | 623 | bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI, |
| 624 | bool InsertLifetime) { |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 625 | Instruction *TheCall = CS.getInstruction(); |
| 626 | assert(TheCall->getParent() && TheCall->getParent()->getParent() && |
| 627 | "Instruction not in function!"); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 629 | // If IFI has any state in it, zap it before we fill it in. |
| 630 | IFI.reset(); |
| 631 | |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 632 | const Function *CalledFunc = CS.getCalledFunction(); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 633 | if (!CalledFunc || // Can't inline external function or indirect |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 634 | CalledFunc->isDeclaration() || // call, or call to a vararg function! |
Eric Christopher | 1d38538 | 2010-03-24 23:35:21 +0000 | [diff] [blame] | 635 | CalledFunc->getFunctionType()->isVarArg()) return false; |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 636 | |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 637 | // If the call to the callee cannot throw, set the 'nounwind' flag on any |
| 638 | // calls that we inline. |
| 639 | bool MarkNoUnwind = CS.doesNotThrow(); |
| 640 | |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 641 | BasicBlock *OrigBB = TheCall->getParent(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 642 | Function *Caller = OrigBB->getParent(); |
| 643 | |
Gordon Henriksen | b969c59 | 2007-12-25 03:10:07 +0000 | [diff] [blame] | 644 | // GC poses two hazards to inlining, which only occur when the callee has GC: |
| 645 | // 1. If the caller has no GC, then the callee's GC must be propagated to the |
| 646 | // caller. |
| 647 | // 2. If the caller has a differing GC, it is invalid to inline. |
Gordon Henriksen | d930f91 | 2008-08-17 18:44:35 +0000 | [diff] [blame] | 648 | if (CalledFunc->hasGC()) { |
| 649 | if (!Caller->hasGC()) |
| 650 | Caller->setGC(CalledFunc->getGC()); |
| 651 | else if (CalledFunc->getGC() != Caller->getGC()) |
Gordon Henriksen | b969c59 | 2007-12-25 03:10:07 +0000 | [diff] [blame] | 652 | return false; |
| 653 | } |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 654 | |
Benjamin Kramer | 4d2b871 | 2011-12-02 18:37:31 +0000 | [diff] [blame] | 655 | // Get the personality function from the callee if it contains a landing pad. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 656 | Value *CalleePersonality = nullptr; |
Benjamin Kramer | 4d2b871 | 2011-12-02 18:37:31 +0000 | [diff] [blame] | 657 | for (Function::const_iterator I = CalledFunc->begin(), E = CalledFunc->end(); |
| 658 | I != E; ++I) |
| 659 | if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) { |
| 660 | const BasicBlock *BB = II->getUnwindDest(); |
Bill Wendling | f3cae51 | 2012-01-31 00:56:53 +0000 | [diff] [blame] | 661 | const LandingPadInst *LP = BB->getLandingPadInst(); |
| 662 | CalleePersonality = LP->getPersonalityFn(); |
Benjamin Kramer | 4d2b871 | 2011-12-02 18:37:31 +0000 | [diff] [blame] | 663 | break; |
| 664 | } |
| 665 | |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 666 | // Find the personality function used by the landing pads of the caller. If it |
| 667 | // exists, then check to see that it matches the personality function used in |
| 668 | // the callee. |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 669 | if (CalleePersonality) { |
Benjamin Kramer | 4d2b871 | 2011-12-02 18:37:31 +0000 | [diff] [blame] | 670 | for (Function::const_iterator I = Caller->begin(), E = Caller->end(); |
| 671 | I != E; ++I) |
| 672 | if (const InvokeInst *II = dyn_cast<InvokeInst>(I->getTerminator())) { |
| 673 | const BasicBlock *BB = II->getUnwindDest(); |
Bill Wendling | f3cae51 | 2012-01-31 00:56:53 +0000 | [diff] [blame] | 674 | const LandingPadInst *LP = BB->getLandingPadInst(); |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 675 | |
Benjamin Kramer | 4d2b871 | 2011-12-02 18:37:31 +0000 | [diff] [blame] | 676 | // If the personality functions match, then we can perform the |
| 677 | // inlining. Otherwise, we can't inline. |
| 678 | // TODO: This isn't 100% true. Some personality functions are proper |
| 679 | // supersets of others and can be used in place of the other. |
| 680 | if (LP->getPersonalityFn() != CalleePersonality) |
| 681 | return false; |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 682 | |
Benjamin Kramer | 4d2b871 | 2011-12-02 18:37:31 +0000 | [diff] [blame] | 683 | break; |
| 684 | } |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 685 | } |
Bill Wendling | 55421f0 | 2011-08-14 08:01:36 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 9fc977e | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 687 | // Get an iterator to the last basic block in the function, which will have |
| 688 | // the new function inlined after it. |
Chris Lattner | 9fc977e | 2004-02-04 01:41:09 +0000 | [diff] [blame] | 689 | Function::iterator LastBlock = &Caller->back(); |
| 690 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 691 | // Make sure to capture all of the return instructions from the cloned |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 692 | // function. |
Chris Lattner | d84dbb3 | 2009-08-27 04:02:30 +0000 | [diff] [blame] | 693 | SmallVector<ReturnInst*, 8> Returns; |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 694 | ClonedCodeInfo InlinedFunctionInfo; |
Dale Johannesen | 845e582 | 2009-03-04 02:09:48 +0000 | [diff] [blame] | 695 | Function::iterator FirstNewBlock; |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 696 | |
Devang Patel | b8f11de | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 697 | { // Scope to destroy VMap after cloning. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 698 | ValueToValueMapTy VMap; |
Julien Lerouge | 957e91c | 2014-04-15 18:01:54 +0000 | [diff] [blame] | 699 | // Keep a list of pair (dst, src) to emit byval initializations. |
| 700 | SmallVector<std::pair<Value*, Value*>, 4> ByValInit; |
Chris Lattner | be853d7 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 701 | |
Dan Gohman | 3ada1e1 | 2008-06-20 17:11:32 +0000 | [diff] [blame] | 702 | assert(CalledFunc->arg_size() == CS.arg_size() && |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 703 | "No varargs calls can be inlined!"); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 908117b | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 705 | // Calculate the vector of arguments to pass into the function cloner, which |
| 706 | // matches up the formal to the actual argument values. |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 707 | CallSite::arg_iterator AI = CS.arg_begin(); |
Chris Lattner | 908117b | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 708 | unsigned ArgNo = 0; |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 709 | for (Function::const_arg_iterator I = CalledFunc->arg_begin(), |
Chris Lattner | 908117b | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 710 | E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) { |
| 711 | Value *ActualArg = *AI; |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 712 | |
Duncan Sands | 053c987 | 2008-01-27 18:12:58 +0000 | [diff] [blame] | 713 | // When byval arguments actually inlined, we need to make the copy implied |
| 714 | // by them explicit. However, we don't do this if the callee is readonly |
| 715 | // or readnone, because the copy would be unneeded: the callee doesn't |
| 716 | // modify the struct. |
Nick Lewycky | 612d70b | 2011-11-20 19:09:04 +0000 | [diff] [blame] | 717 | if (CS.isByValArgument(ArgNo)) { |
David Majnemer | 120f4a0 | 2013-11-03 12:22:13 +0000 | [diff] [blame] | 718 | ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI, |
Chris Lattner | 0099744 | 2010-12-20 07:57:41 +0000 | [diff] [blame] | 719 | CalledFunc->getParamAlignment(ArgNo+1)); |
Reid Kleckner | 9b2cc64 | 2014-04-21 20:48:47 +0000 | [diff] [blame] | 720 | if (ActualArg != *AI) |
Julien Lerouge | 957e91c | 2014-04-15 18:01:54 +0000 | [diff] [blame] | 721 | ByValInit.push_back(std::make_pair(ActualArg, (Value*) *AI)); |
Chris Lattner | 908117b | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 722 | } |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 723 | |
Devang Patel | b8f11de | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 724 | VMap[I] = ActualArg; |
Chris Lattner | 908117b | 2008-01-11 06:09:30 +0000 | [diff] [blame] | 725 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 726 | |
Chris Lattner | be853d7 | 2006-05-27 01:28:04 +0000 | [diff] [blame] | 727 | // We want the inliner to prune the code as it copies. We would LOVE to |
| 728 | // have no dead or constant instructions leftover after inlining occurs |
| 729 | // (which can happen, e.g., because an argument was constant), but we'll be |
| 730 | // happy with whatever the cloner can do. |
Dan Gohman | ca26f79 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 731 | CloneAndPruneFunctionInto(Caller, CalledFunc, VMap, |
| 732 | /*ModuleLevelChanges=*/false, Returns, ".i", |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 733 | &InlinedFunctionInfo, IFI.DL, TheCall); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 734 | |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 735 | // Remember the first block that is newly cloned over. |
| 736 | FirstNewBlock = LastBlock; ++FirstNewBlock; |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 737 | |
Julien Lerouge | 957e91c | 2014-04-15 18:01:54 +0000 | [diff] [blame] | 738 | // Inject byval arguments initialization. |
| 739 | for (std::pair<Value*, Value*> &Init : ByValInit) |
| 740 | HandleByValArgumentInit(Init.first, Init.second, Caller->getParent(), |
| 741 | FirstNewBlock, IFI); |
| 742 | |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 743 | // Update the callgraph if requested. |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 744 | if (IFI.CG) |
Devang Patel | b8f11de | 2010-06-23 23:55:51 +0000 | [diff] [blame] | 745 | UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI); |
Devang Patel | 3579740 | 2011-07-08 18:01:31 +0000 | [diff] [blame] | 746 | |
| 747 | // Update inlined instructions' line number information. |
| 748 | fixupLineNumbers(Caller, FirstNewBlock, TheCall); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame^] | 749 | |
| 750 | // Clone existing noalias metadata if necessary. |
| 751 | CloneAliasScopeMetadata(CS, VMap); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 752 | } |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 754 | // If there are any alloca instructions in the block that used to be the entry |
| 755 | // block for the callee, move them to the entry block of the caller. First |
| 756 | // calculate which instruction they should be inserted before. We insert the |
| 757 | // instructions at the end of the current alloca list. |
Chris Lattner | 257492c | 2006-01-13 18:16:48 +0000 | [diff] [blame] | 758 | { |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 759 | BasicBlock::iterator InsertPoint = Caller->begin()->begin(); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 760 | for (BasicBlock::iterator I = FirstNewBlock->begin(), |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 761 | E = FirstNewBlock->end(); I != E; ) { |
| 762 | AllocaInst *AI = dyn_cast<AllocaInst>(I++); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 763 | if (!AI) continue; |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 764 | |
| 765 | // If the alloca is now dead, remove it. This often occurs due to code |
| 766 | // specialization. |
| 767 | if (AI->use_empty()) { |
| 768 | AI->eraseFromParent(); |
| 769 | continue; |
Chris Lattner | 6ef6d06 | 2006-09-13 19:23:57 +0000 | [diff] [blame] | 770 | } |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 771 | |
| 772 | if (!isa<Constant>(AI->getArraySize())) |
| 773 | continue; |
| 774 | |
Chris Lattner | cd3af96 | 2010-12-06 07:43:04 +0000 | [diff] [blame] | 775 | // Keep track of the static allocas that we inline into the caller. |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 776 | IFI.StaticAllocas.push_back(AI); |
Chris Lattner | b1cba3f | 2009-08-27 04:20:52 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 778 | // Scan for the block of allocas that we can move over, and move them |
| 779 | // all at once. |
| 780 | while (isa<AllocaInst>(I) && |
Chris Lattner | b1cba3f | 2009-08-27 04:20:52 +0000 | [diff] [blame] | 781 | isa<Constant>(cast<AllocaInst>(I)->getArraySize())) { |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 782 | IFI.StaticAllocas.push_back(cast<AllocaInst>(I)); |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 783 | ++I; |
Chris Lattner | b1cba3f | 2009-08-27 04:20:52 +0000 | [diff] [blame] | 784 | } |
Chris Lattner | 5eef6ad | 2009-08-27 03:51:50 +0000 | [diff] [blame] | 785 | |
| 786 | // Transfer all of the allocas over in a block. Using splice means |
| 787 | // that the instructions aren't removed from the symbol table, then |
| 788 | // reinserted. |
| 789 | Caller->getEntryBlock().getInstList().splice(InsertPoint, |
| 790 | FirstNewBlock->getInstList(), |
| 791 | AI, I); |
| 792 | } |
Chris Lattner | 0cc265e | 2003-08-24 06:59:16 +0000 | [diff] [blame] | 793 | } |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 794 | |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 795 | bool InlinedMustTailCalls = false; |
| 796 | if (InlinedFunctionInfo.ContainsCalls) { |
Reid Kleckner | 6af2124 | 2014-05-15 20:39:42 +0000 | [diff] [blame] | 797 | CallInst::TailCallKind CallSiteTailKind = CallInst::TCK_None; |
| 798 | if (CallInst *CI = dyn_cast<CallInst>(TheCall)) |
| 799 | CallSiteTailKind = CI->getTailCallKind(); |
| 800 | |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 801 | for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; |
| 802 | ++BB) { |
| 803 | for (Instruction &I : *BB) { |
| 804 | CallInst *CI = dyn_cast<CallInst>(&I); |
| 805 | if (!CI) |
| 806 | continue; |
| 807 | |
| 808 | // We need to reduce the strength of any inlined tail calls. For |
| 809 | // musttail, we have to avoid introducing potential unbounded stack |
| 810 | // growth. For example, if functions 'f' and 'g' are mutually recursive |
| 811 | // with musttail, we can inline 'g' into 'f' so long as we preserve |
| 812 | // musttail on the cloned call to 'f'. If either the inlined call site |
| 813 | // or the cloned call site is *not* musttail, the program already has |
| 814 | // one frame of stack growth, so it's safe to remove musttail. Here is |
| 815 | // a table of example transformations: |
| 816 | // |
| 817 | // f -> musttail g -> musttail f ==> f -> musttail f |
| 818 | // f -> musttail g -> tail f ==> f -> tail f |
| 819 | // f -> g -> musttail f ==> f -> f |
| 820 | // f -> g -> tail f ==> f -> f |
| 821 | CallInst::TailCallKind ChildTCK = CI->getTailCallKind(); |
| 822 | ChildTCK = std::min(CallSiteTailKind, ChildTCK); |
| 823 | CI->setTailCallKind(ChildTCK); |
| 824 | InlinedMustTailCalls |= CI->isMustTailCall(); |
| 825 | |
| 826 | // Calls inlined through a 'nounwind' call site should be marked |
| 827 | // 'nounwind'. |
| 828 | if (MarkNoUnwind) |
| 829 | CI->setDoesNotThrow(); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 834 | // Leave lifetime markers for the static alloca's, scoping them to the |
| 835 | // function we just inlined. |
Chad Rosier | 07d37bc | 2012-02-25 02:56:01 +0000 | [diff] [blame] | 836 | if (InsertLifetime && !IFI.StaticAllocas.empty()) { |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 837 | IRBuilder<> builder(FirstNewBlock->begin()); |
| 838 | for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) { |
| 839 | AllocaInst *AI = IFI.StaticAllocas[ai]; |
| 840 | |
| 841 | // If the alloca is already scoped to something smaller than the whole |
| 842 | // function then there's no need to add redundant, less accurate markers. |
| 843 | if (hasLifetimeMarkers(AI)) |
| 844 | continue; |
| 845 | |
Alexey Samsonov | cfd662f | 2012-11-13 07:15:32 +0000 | [diff] [blame] | 846 | // Try to determine the size of the allocation. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 847 | ConstantInt *AllocaSize = nullptr; |
Alexey Samsonov | cfd662f | 2012-11-13 07:15:32 +0000 | [diff] [blame] | 848 | if (ConstantInt *AIArraySize = |
| 849 | dyn_cast<ConstantInt>(AI->getArraySize())) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 850 | if (IFI.DL) { |
Alexey Samsonov | cfd662f | 2012-11-13 07:15:32 +0000 | [diff] [blame] | 851 | Type *AllocaType = AI->getAllocatedType(); |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 852 | uint64_t AllocaTypeSize = IFI.DL->getTypeAllocSize(AllocaType); |
Alexey Samsonov | cfd662f | 2012-11-13 07:15:32 +0000 | [diff] [blame] | 853 | uint64_t AllocaArraySize = AIArraySize->getLimitedValue(); |
| 854 | assert(AllocaArraySize > 0 && "array size of AllocaInst is zero"); |
| 855 | // Check that array size doesn't saturate uint64_t and doesn't |
| 856 | // overflow when it's multiplied by type size. |
| 857 | if (AllocaArraySize != ~0ULL && |
| 858 | UINT64_MAX / AllocaArraySize >= AllocaTypeSize) { |
| 859 | AllocaSize = ConstantInt::get(Type::getInt64Ty(AI->getContext()), |
| 860 | AllocaArraySize * AllocaTypeSize); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | builder.CreateLifetimeStart(AI, AllocaSize); |
Reid Kleckner | 900d46f | 2014-05-15 21:10:46 +0000 | [diff] [blame] | 866 | for (ReturnInst *RI : Returns) { |
| 867 | // Don't insert llvm.lifetime.end calls between a musttail call and a |
| 868 | // return. The return kills all local allocas. |
| 869 | if (InlinedMustTailCalls && getPrecedingMustTailCall(RI)) |
| 870 | continue; |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 871 | IRBuilder<>(RI).CreateLifetimeEnd(AI, AllocaSize); |
Reid Kleckner | 900d46f | 2014-05-15 21:10:46 +0000 | [diff] [blame] | 872 | } |
Nick Lewycky | a68ec83 | 2011-05-22 05:22:10 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 876 | // If the inlined code contained dynamic alloca instructions, wrap the inlined |
| 877 | // code with llvm.stacksave/llvm.stackrestore intrinsics. |
| 878 | if (InlinedFunctionInfo.ContainsDynamicAllocas) { |
| 879 | Module *M = Caller->getParent(); |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 880 | // Get the two intrinsics we care about. |
Chris Lattner | 88b36f1 | 2009-10-17 05:39:39 +0000 | [diff] [blame] | 881 | Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave); |
| 882 | Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore); |
Chris Lattner | 5de3b8b | 2006-07-12 18:29:36 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 884 | // Insert the llvm.stacksave. |
John McCall | 5af8452 | 2011-06-14 02:51:53 +0000 | [diff] [blame] | 885 | CallInst *SavedPtr = IRBuilder<>(FirstNewBlock, FirstNewBlock->begin()) |
| 886 | .CreateCall(StackSave, "savedstack"); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 887 | |
Chris Lattner | 2be0607 | 2006-01-13 19:34:14 +0000 | [diff] [blame] | 888 | // Insert a call to llvm.stackrestore before any return instructions in the |
| 889 | // inlined function. |
Reid Kleckner | 900d46f | 2014-05-15 21:10:46 +0000 | [diff] [blame] | 890 | for (ReturnInst *RI : Returns) { |
| 891 | // Don't insert llvm.stackrestore calls between a musttail call and a |
| 892 | // return. The return will restore the stack pointer. |
| 893 | if (InlinedMustTailCalls && getPrecedingMustTailCall(RI)) |
| 894 | continue; |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 895 | IRBuilder<>(RI).CreateCall(StackRestore, SavedPtr); |
Reid Kleckner | 900d46f | 2014-05-15 21:10:46 +0000 | [diff] [blame] | 896 | } |
Chris Lattner | 9f3dced | 2005-05-06 06:47:52 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 899 | // If we are inlining for an invoke instruction, we must make sure to rewrite |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 900 | // any call instructions into invoke instructions. |
Chris Lattner | 908d795 | 2006-01-13 19:05:59 +0000 | [diff] [blame] | 901 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) |
Chris Lattner | 8900f3e | 2009-09-01 18:44:06 +0000 | [diff] [blame] | 902 | HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 903 | |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 904 | // Handle any inlined musttail call sites. In order for a new call site to be |
| 905 | // musttail, the source of the clone and the inlined call site must have been |
| 906 | // musttail. Therefore it's safe to return without merging control into the |
| 907 | // phi below. |
| 908 | if (InlinedMustTailCalls) { |
| 909 | // Check if we need to bitcast the result of any musttail calls. |
| 910 | Type *NewRetTy = Caller->getReturnType(); |
| 911 | bool NeedBitCast = !TheCall->use_empty() && TheCall->getType() != NewRetTy; |
| 912 | |
| 913 | // Handle the returns preceded by musttail calls separately. |
| 914 | SmallVector<ReturnInst *, 8> NormalReturns; |
| 915 | for (ReturnInst *RI : Returns) { |
| 916 | CallInst *ReturnedMustTail = getPrecedingMustTailCall(RI); |
| 917 | if (!ReturnedMustTail) { |
| 918 | NormalReturns.push_back(RI); |
| 919 | continue; |
| 920 | } |
| 921 | if (!NeedBitCast) |
| 922 | continue; |
| 923 | |
| 924 | // Delete the old return and any preceding bitcast. |
| 925 | BasicBlock *CurBB = RI->getParent(); |
| 926 | auto *OldCast = dyn_cast_or_null<BitCastInst>(RI->getReturnValue()); |
| 927 | RI->eraseFromParent(); |
| 928 | if (OldCast) |
| 929 | OldCast->eraseFromParent(); |
| 930 | |
| 931 | // Insert a new bitcast and return with the right type. |
| 932 | IRBuilder<> Builder(CurBB); |
| 933 | Builder.CreateRet(Builder.CreateBitCast(ReturnedMustTail, NewRetTy)); |
| 934 | } |
| 935 | |
| 936 | // Leave behind the normal returns so we can merge control flow. |
| 937 | std::swap(Returns, NormalReturns); |
| 938 | } |
| 939 | |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 940 | // If we cloned in _exactly one_ basic block, and if that block ends in a |
| 941 | // return instruction, we splice the body of the inlined callee directly into |
| 942 | // the calling basic block. |
| 943 | if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) { |
| 944 | // Move all of the instructions right before the call. |
| 945 | OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(), |
| 946 | FirstNewBlock->begin(), FirstNewBlock->end()); |
| 947 | // Remove the cloned basic block. |
| 948 | Caller->getBasicBlockList().pop_back(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 950 | // If the call site was an invoke instruction, add a branch to the normal |
| 951 | // destination. |
Adrian Prantl | 15db52b | 2013-04-23 19:56:03 +0000 | [diff] [blame] | 952 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
| 953 | BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall); |
| 954 | NewBr->setDebugLoc(Returns[0]->getDebugLoc()); |
| 955 | } |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 956 | |
| 957 | // If the return instruction returned a value, replace uses of the call with |
| 958 | // uses of the returned value. |
Devang Patel | 841322b | 2008-03-04 21:15:15 +0000 | [diff] [blame] | 959 | if (!TheCall->use_empty()) { |
| 960 | ReturnInst *R = Returns[0]; |
Eli Friedman | 36b9026 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 961 | if (TheCall == R->getReturnValue()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 962 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Eli Friedman | 36b9026 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 963 | else |
| 964 | TheCall->replaceAllUsesWith(R->getReturnValue()); |
Devang Patel | 841322b | 2008-03-04 21:15:15 +0000 | [diff] [blame] | 965 | } |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 966 | // Since we are now done with the Call/Invoke, we can delete it. |
Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 967 | TheCall->eraseFromParent(); |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 968 | |
| 969 | // Since we are now done with the return instruction, delete it also. |
Dan Gohman | 158ff2c | 2008-06-21 22:08:46 +0000 | [diff] [blame] | 970 | Returns[0]->eraseFromParent(); |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 971 | |
| 972 | // We are now done with the inlining. |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | // Otherwise, we have the normal case, of more than one block to inline or |
| 977 | // multiple return sites. |
| 978 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 979 | // We want to clone the entire callee function into the hole between the |
| 980 | // "starter" and "ender" blocks. How we accomplish this depends on whether |
| 981 | // this is an invoke instruction or a call instruction. |
| 982 | BasicBlock *AfterCallBB; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 983 | BranchInst *CreatedBranchToNormalDest = nullptr; |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 984 | if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 986 | // Add an unconditional branch to make this look like the CallInst case... |
Adrian Prantl | 15db52b | 2013-04-23 19:56:03 +0000 | [diff] [blame] | 987 | CreatedBranchToNormalDest = BranchInst::Create(II->getNormalDest(), TheCall); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 988 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 989 | // Split the basic block. This guarantees that no PHI nodes will have to be |
| 990 | // updated due to new incoming edges, and make the invoke case more |
| 991 | // symmetric to the call case. |
Adrian Prantl | 15db52b | 2013-04-23 19:56:03 +0000 | [diff] [blame] | 992 | AfterCallBB = OrigBB->splitBasicBlock(CreatedBranchToNormalDest, |
Chris Lattner | ffefea0 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 993 | CalledFunc->getName()+".exit"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 995 | } else { // It's a call |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 996 | // If this is a call instruction, we need to split the basic block that |
| 997 | // the call lives in. |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 998 | // |
| 999 | AfterCallBB = OrigBB->splitBasicBlock(TheCall, |
Chris Lattner | ffefea0 | 2004-12-11 16:59:54 +0000 | [diff] [blame] | 1000 | CalledFunc->getName()+".exit"); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 1003 | // Change the branch that used to go to AfterCallBB to branch to the first |
| 1004 | // basic block of the inlined function. |
| 1005 | // |
| 1006 | TerminatorInst *Br = OrigBB->getTerminator(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1007 | assert(Br && Br->getOpcode() == Instruction::Br && |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 1008 | "splitBasicBlock broken!"); |
| 1009 | Br->setOperand(0, FirstNewBlock); |
| 1010 | |
| 1011 | |
| 1012 | // Now that the function is correct, make it a little bit nicer. In |
| 1013 | // particular, move the basic blocks inserted from the end of the function |
| 1014 | // into the space made by splitting the source basic block. |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 1015 | Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(), |
| 1016 | FirstNewBlock, Caller->end()); |
| 1017 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1018 | // Handle all of the return instructions that we just cloned in, and eliminate |
| 1019 | // any users of the original call/invoke instruction. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1020 | Type *RTy = CalledFunc->getReturnType(); |
Dan Gohman | 3b18fd7 | 2008-06-20 01:03:44 +0000 | [diff] [blame] | 1021 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1022 | PHINode *PHI = nullptr; |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1023 | if (Returns.size() > 1) { |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1024 | // The PHI node should go at the front of the new basic block to merge all |
| 1025 | // possible incoming values. |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1026 | if (!TheCall->use_empty()) { |
Jay Foad | 5213134 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1027 | PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(), |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1028 | AfterCallBB->begin()); |
| 1029 | // Anything that used the result of the function call should now use the |
| 1030 | // PHI node as their operand. |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 1031 | TheCall->replaceAllUsesWith(PHI); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1032 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1033 | |
Gabor Greif | 5aa1922 | 2009-01-15 18:40:09 +0000 | [diff] [blame] | 1034 | // Loop over all of the return instructions adding entries to the PHI node |
| 1035 | // as appropriate. |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1036 | if (PHI) { |
| 1037 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
| 1038 | ReturnInst *RI = Returns[i]; |
| 1039 | assert(RI->getReturnValue()->getType() == PHI->getType() && |
| 1040 | "Ret value not consistent in function!"); |
| 1041 | PHI->addIncoming(RI->getReturnValue(), RI->getParent()); |
Devang Patel | 780b3ca6 | 2008-03-07 20:06:16 +0000 | [diff] [blame] | 1042 | } |
| 1043 | } |
| 1044 | |
Chris Lattner | c6b3b25 | 2009-10-27 05:39:41 +0000 | [diff] [blame] | 1045 | |
Gabor Greif | 8c573f7 | 2009-01-16 23:08:50 +0000 | [diff] [blame] | 1046 | // Add a branch to the merge points and remove return instructions. |
Richard Trieu | 624c2eb | 2013-04-30 22:45:10 +0000 | [diff] [blame] | 1047 | DebugLoc Loc; |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1048 | for (unsigned i = 0, e = Returns.size(); i != e; ++i) { |
Richard Trieu | 624c2eb | 2013-04-30 22:45:10 +0000 | [diff] [blame] | 1049 | ReturnInst *RI = Returns[i]; |
Adrian Prantl | 0941638 | 2013-04-30 17:08:16 +0000 | [diff] [blame] | 1050 | BranchInst* BI = BranchInst::Create(AfterCallBB, RI); |
Richard Trieu | 624c2eb | 2013-04-30 22:45:10 +0000 | [diff] [blame] | 1051 | Loc = RI->getDebugLoc(); |
| 1052 | BI->setDebugLoc(Loc); |
Devang Patel | 64d0f07 | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 1053 | RI->eraseFromParent(); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1054 | } |
Adrian Prantl | 0941638 | 2013-04-30 17:08:16 +0000 | [diff] [blame] | 1055 | // We need to set the debug location to *somewhere* inside the |
Adrian Prantl | 8beccf9 | 2013-04-30 17:33:32 +0000 | [diff] [blame] | 1056 | // inlined function. The line number may be nonsensical, but the |
Adrian Prantl | 0941638 | 2013-04-30 17:08:16 +0000 | [diff] [blame] | 1057 | // instruction will at least be associated with the right |
| 1058 | // function. |
| 1059 | if (CreatedBranchToNormalDest) |
Richard Trieu | 624c2eb | 2013-04-30 22:45:10 +0000 | [diff] [blame] | 1060 | CreatedBranchToNormalDest->setDebugLoc(Loc); |
Devang Patel | 64d0f07 | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 1061 | } else if (!Returns.empty()) { |
| 1062 | // Otherwise, if there is exactly one return value, just replace anything |
| 1063 | // using the return value of the call with the computed value. |
Eli Friedman | 36b9026 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 1064 | if (!TheCall->use_empty()) { |
| 1065 | if (TheCall == Returns[0]->getReturnValue()) |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1066 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Eli Friedman | 36b9026 | 2009-05-08 00:22:04 +0000 | [diff] [blame] | 1067 | else |
| 1068 | TheCall->replaceAllUsesWith(Returns[0]->getReturnValue()); |
| 1069 | } |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 1070 | |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 1071 | // Update PHI nodes that use the ReturnBB to use the AfterCallBB. |
| 1072 | BasicBlock *ReturnBB = Returns[0]->getParent(); |
| 1073 | ReturnBB->replaceAllUsesWith(AfterCallBB); |
| 1074 | |
Devang Patel | 64d0f07 | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 1075 | // Splice the code from the return block into the block that it will return |
| 1076 | // to, which contains the code that was after the call. |
Devang Patel | 64d0f07 | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 1077 | AfterCallBB->getInstList().splice(AfterCallBB->begin(), |
| 1078 | ReturnBB->getInstList()); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 1079 | |
Adrian Prantl | 15db52b | 2013-04-23 19:56:03 +0000 | [diff] [blame] | 1080 | if (CreatedBranchToNormalDest) |
| 1081 | CreatedBranchToNormalDest->setDebugLoc(Returns[0]->getDebugLoc()); |
| 1082 | |
Devang Patel | 64d0f07 | 2008-03-10 18:34:00 +0000 | [diff] [blame] | 1083 | // Delete the return instruction now and empty ReturnBB now. |
| 1084 | Returns[0]->eraseFromParent(); |
| 1085 | ReturnBB->eraseFromParent(); |
Chris Lattner | 6e79e55 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 1086 | } else if (!TheCall->use_empty()) { |
| 1087 | // No returns, but something is using the return value of the call. Just |
| 1088 | // nuke the result. |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1089 | TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType())); |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1090 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | 18ef3fd | 2004-02-04 02:51:48 +0000 | [diff] [blame] | 1092 | // Since we are now done with the Call/Invoke, we can delete it. |
Chris Lattner | 6e79e55 | 2004-10-17 23:21:07 +0000 | [diff] [blame] | 1093 | TheCall->eraseFromParent(); |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1094 | |
Reid Kleckner | f0915aa | 2014-05-15 20:11:28 +0000 | [diff] [blame] | 1095 | // If we inlined any musttail calls and the original return is now |
| 1096 | // unreachable, delete it. It can only contain a bitcast and ret. |
| 1097 | if (InlinedMustTailCalls && pred_begin(AfterCallBB) == pred_end(AfterCallBB)) |
| 1098 | AfterCallBB->eraseFromParent(); |
| 1099 | |
Chris Lattner | fc3fe5c | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 1100 | // We should always be able to fold the entry block of the function into the |
| 1101 | // single predecessor of the block... |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 1102 | assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!"); |
Chris Lattner | fc3fe5c | 2003-08-24 04:06:56 +0000 | [diff] [blame] | 1103 | BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0); |
Chris Lattner | 0fa8c7c | 2004-02-04 04:17:06 +0000 | [diff] [blame] | 1104 | |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 1105 | // Splice the code entry block into calling block, right before the |
| 1106 | // unconditional branch. |
Eric Christopher | 9651312 | 2011-06-23 06:24:52 +0000 | [diff] [blame] | 1107 | CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 1108 | OrigBB->getInstList().splice(Br, CalleeEntry->getInstList()); |
Chris Lattner | 0328d75 | 2004-04-16 05:17:59 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Remove the unconditional branch. |
| 1111 | OrigBB->getInstList().erase(Br); |
| 1112 | |
| 1113 | // Now we can remove the CalleeEntry block, which is now empty. |
| 1114 | Caller->getBasicBlockList().erase(CalleeEntry); |
Duncan Sands | 7c8fb1a | 2008-09-05 12:37:12 +0000 | [diff] [blame] | 1115 | |
Duncan Sands | 9d9a4e2 | 2010-11-17 11:16:23 +0000 | [diff] [blame] | 1116 | // If we inserted a phi node, check to see if it has a single value (e.g. all |
| 1117 | // the entries are the same or undef). If so, remove the PHI so it doesn't |
| 1118 | // block other optimizations. |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 1119 | if (PHI) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1120 | if (Value *V = SimplifyInstruction(PHI, IFI.DL)) { |
Duncan Sands | 9d9a4e2 | 2010-11-17 11:16:23 +0000 | [diff] [blame] | 1121 | PHI->replaceAllUsesWith(V); |
| 1122 | PHI->eraseFromParent(); |
| 1123 | } |
Bill Wendling | ce0c229 | 2012-01-31 01:01:16 +0000 | [diff] [blame] | 1124 | } |
Duncan Sands | 9d9a4e2 | 2010-11-17 11:16:23 +0000 | [diff] [blame] | 1125 | |
Chris Lattner | 530d4bf | 2003-05-29 15:11:31 +0000 | [diff] [blame] | 1126 | return true; |
| 1127 | } |