InlineFunction.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. //===- InlineFunction.cpp - Code to perform function inlining -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements inlining of a function into a call site, resolving
  11. // parameters and the return value as appropriate.
  12. //
  13. // The code in this file for handling inlines through invoke
  14. // instructions preserves semantics only under some assumptions about
  15. // the behavior of unwinders which correspond to gcc-style libUnwind
  16. // exception personality functions. Eventually the IR will be
  17. // improved to make this unnecessary, but until then, this code is
  18. // marked [LIBUNWIND].
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "llvm/Transforms/Utils/Cloning.h"
  22. #include "llvm/Constants.h"
  23. #include "llvm/DerivedTypes.h"
  24. #include "llvm/Module.h"
  25. #include "llvm/Instructions.h"
  26. #include "llvm/IntrinsicInst.h"
  27. #include "llvm/Intrinsics.h"
  28. #include "llvm/Attributes.h"
  29. #include "llvm/Analysis/CallGraph.h"
  30. #include "llvm/Analysis/DebugInfo.h"
  31. #include "llvm/Analysis/InstructionSimplify.h"
  32. #include "llvm/Target/TargetData.h"
  33. #include "llvm/Transforms/Utils/Local.h"
  34. #include "llvm/ADT/SmallVector.h"
  35. #include "llvm/ADT/StringExtras.h"
  36. #include "llvm/Support/CallSite.h"
  37. #include "llvm/Support/IRBuilder.h"
  38. using namespace llvm;
  39. bool llvm::InlineFunction(CallInst *CI, InlineFunctionInfo &IFI) {
  40. return InlineFunction(CallSite(CI), IFI);
  41. }
  42. bool llvm::InlineFunction(InvokeInst *II, InlineFunctionInfo &IFI) {
  43. return InlineFunction(CallSite(II), IFI);
  44. }
  45. /// [LIBUNWIND] Look for an llvm.eh.exception call in the given block.
  46. static EHExceptionInst *findExceptionInBlock(BasicBlock *bb) {
  47. for (BasicBlock::iterator i = bb->begin(), e = bb->end(); i != e; i++) {
  48. EHExceptionInst *exn = dyn_cast<EHExceptionInst>(i);
  49. if (exn) return exn;
  50. }
  51. return 0;
  52. }
  53. /// [LIBUNWIND] Look for the 'best' llvm.eh.selector instruction for
  54. /// the given llvm.eh.exception call.
  55. static EHSelectorInst *findSelectorForException(EHExceptionInst *exn) {
  56. BasicBlock *exnBlock = exn->getParent();
  57. EHSelectorInst *outOfBlockSelector = 0;
  58. for (Instruction::use_iterator
  59. ui = exn->use_begin(), ue = exn->use_end(); ui != ue; ++ui) {
  60. EHSelectorInst *sel = dyn_cast<EHSelectorInst>(*ui);
  61. if (!sel) continue;
  62. // Immediately accept an eh.selector in the same block as the
  63. // excepton call.
  64. if (sel->getParent() == exnBlock) return sel;
  65. // Otherwise, use the first selector we see.
  66. if (!outOfBlockSelector) outOfBlockSelector = sel;
  67. }
  68. return outOfBlockSelector;
  69. }
  70. /// [LIBUNWIND] Find the (possibly absent) call to @llvm.eh.selector
  71. /// in the given landing pad. In principle, llvm.eh.exception is
  72. /// required to be in the landing pad; in practice, SplitCriticalEdge
  73. /// can break that invariant, and then inlining can break it further.
  74. /// There's a real need for a reliable solution here, but until that
  75. /// happens, we have some fragile workarounds here.
  76. static EHSelectorInst *findSelectorForLandingPad(BasicBlock *lpad) {
  77. // Look for an exception call in the actual landing pad.
  78. EHExceptionInst *exn = findExceptionInBlock(lpad);
  79. if (exn) return findSelectorForException(exn);
  80. // Okay, if that failed, look for one in an obvious successor. If
  81. // we find one, we'll fix the IR by moving things back to the
  82. // landing pad.
  83. bool dominates = true; // does the lpad dominate the exn call
  84. BasicBlock *nonDominated = 0; // if not, the first non-dominated block
  85. BasicBlock *lastDominated = 0; // and the block which branched to it
  86. BasicBlock *exnBlock = lpad;
  87. // We need to protect against lpads that lead into infinite loops.
  88. SmallPtrSet<BasicBlock*,4> visited;
  89. visited.insert(exnBlock);
  90. do {
  91. // We're not going to apply this hack to anything more complicated
  92. // than a series of unconditional branches, so if the block
  93. // doesn't terminate in an unconditional branch, just fail. More
  94. // complicated cases can arise when, say, sinking a call into a
  95. // split unwind edge and then inlining it; but that can do almost
  96. // *anything* to the CFG, including leaving the selector
  97. // completely unreachable. The only way to fix that properly is
  98. // to (1) prohibit transforms which move the exception or selector
  99. // values away from the landing pad, e.g. by producing them with
  100. // instructions that are pinned to an edge like a phi, or
  101. // producing them with not-really-instructions, and (2) making
  102. // transforms which split edges deal with that.
  103. BranchInst *branch = dyn_cast<BranchInst>(&exnBlock->back());
  104. if (!branch || branch->isConditional()) return 0;
  105. BasicBlock *successor = branch->getSuccessor(0);
  106. // Fail if we found an infinite loop.
  107. if (!visited.insert(successor)) return 0;
  108. // If the successor isn't dominated by exnBlock:
  109. if (!successor->getSinglePredecessor()) {
  110. // We don't want to have to deal with threading the exception
  111. // through multiple levels of phi, so give up if we've already
  112. // followed a non-dominating edge.
  113. if (!dominates) return 0;
  114. // Otherwise, remember this as a non-dominating edge.
  115. dominates = false;
  116. nonDominated = successor;
  117. lastDominated = exnBlock;
  118. }
  119. exnBlock = successor;
  120. // Can we stop here?
  121. exn = findExceptionInBlock(exnBlock);
  122. } while (!exn);
  123. // Look for a selector call for the exception we found.
  124. EHSelectorInst *selector = findSelectorForException(exn);
  125. if (!selector) return 0;
  126. // The easy case is when the landing pad still dominates the
  127. // exception call, in which case we can just move both calls back to
  128. // the landing pad.
  129. if (dominates) {
  130. selector->moveBefore(lpad->getFirstNonPHI());
  131. exn->moveBefore(selector);
  132. return selector;
  133. }
  134. // Otherwise, we have to split at the first non-dominating block.
  135. // The CFG looks basically like this:
  136. // lpad:
  137. // phis_0
  138. // insnsAndBranches_1
  139. // br label %nonDominated
  140. // nonDominated:
  141. // phis_2
  142. // insns_3
  143. // %exn = call i8* @llvm.eh.exception()
  144. // insnsAndBranches_4
  145. // %selector = call @llvm.eh.selector(i8* %exn, ...
  146. // We need to turn this into:
  147. // lpad:
  148. // phis_0
  149. // %exn0 = call i8* @llvm.eh.exception()
  150. // %selector0 = call @llvm.eh.selector(i8* %exn0, ...
  151. // insnsAndBranches_1
  152. // br label %split // from lastDominated
  153. // nonDominated:
  154. // phis_2 (without edge from lastDominated)
  155. // %exn1 = call i8* @llvm.eh.exception()
  156. // %selector1 = call i8* @llvm.eh.selector(i8* %exn1, ...
  157. // br label %split
  158. // split:
  159. // phis_2 (edge from lastDominated, edge from split)
  160. // %exn = phi ...
  161. // %selector = phi ...
  162. // insns_3
  163. // insnsAndBranches_4
  164. assert(nonDominated);
  165. assert(lastDominated);
  166. // First, make clones of the intrinsics to go in lpad.
  167. EHExceptionInst *lpadExn = cast<EHExceptionInst>(exn->clone());
  168. EHSelectorInst *lpadSelector = cast<EHSelectorInst>(selector->clone());
  169. lpadSelector->setArgOperand(0, lpadExn);
  170. lpadSelector->insertBefore(lpad->getFirstNonPHI());
  171. lpadExn->insertBefore(lpadSelector);
  172. // Split the non-dominated block.
  173. BasicBlock *split =
  174. nonDominated->splitBasicBlock(nonDominated->getFirstNonPHI(),
  175. nonDominated->getName() + ".lpad-fix");
  176. // Redirect the last dominated branch there.
  177. cast<BranchInst>(lastDominated->back()).setSuccessor(0, split);
  178. // Move the existing intrinsics to the end of the old block.
  179. selector->moveBefore(&nonDominated->back());
  180. exn->moveBefore(selector);
  181. Instruction *splitIP = &split->front();
  182. // For all the phis in nonDominated, make a new phi in split to join
  183. // that phi with the edge from lastDominated.
  184. for (BasicBlock::iterator
  185. i = nonDominated->begin(), e = nonDominated->end(); i != e; ++i) {
  186. PHINode *phi = dyn_cast<PHINode>(i);
  187. if (!phi) break;
  188. PHINode *splitPhi = PHINode::Create(phi->getType(), 2, phi->getName(),
  189. splitIP);
  190. phi->replaceAllUsesWith(splitPhi);
  191. splitPhi->addIncoming(phi, nonDominated);
  192. splitPhi->addIncoming(phi->removeIncomingValue(lastDominated),
  193. lastDominated);
  194. }
  195. // Make new phis for the exception and selector.
  196. PHINode *exnPhi = PHINode::Create(exn->getType(), 2, "", splitIP);
  197. exn->replaceAllUsesWith(exnPhi);
  198. selector->setArgOperand(0, exn); // except for this use
  199. exnPhi->addIncoming(exn, nonDominated);
  200. exnPhi->addIncoming(lpadExn, lastDominated);
  201. PHINode *selectorPhi = PHINode::Create(selector->getType(), 2, "", splitIP);
  202. selector->replaceAllUsesWith(selectorPhi);
  203. selectorPhi->addIncoming(selector, nonDominated);
  204. selectorPhi->addIncoming(lpadSelector, lastDominated);
  205. return lpadSelector;
  206. }
  207. namespace {
  208. /// A class for recording information about inlining through an invoke.
  209. class InvokeInliningInfo {
  210. BasicBlock *OuterUnwindDest;
  211. EHSelectorInst *OuterSelector;
  212. BasicBlock *InnerUnwindDest;
  213. PHINode *InnerExceptionPHI;
  214. PHINode *InnerSelectorPHI;
  215. SmallVector<Value*, 8> UnwindDestPHIValues;
  216. public:
  217. InvokeInliningInfo(InvokeInst *II) :
  218. OuterUnwindDest(II->getUnwindDest()), OuterSelector(0),
  219. InnerUnwindDest(0), InnerExceptionPHI(0), InnerSelectorPHI(0) {
  220. // If there are PHI nodes in the unwind destination block, we
  221. // need to keep track of which values came into them from the
  222. // invoke before removing the edge from this block.
  223. llvm::BasicBlock *invokeBB = II->getParent();
  224. for (BasicBlock::iterator I = OuterUnwindDest->begin();
  225. isa<PHINode>(I); ++I) {
  226. // Save the value to use for this edge.
  227. PHINode *phi = cast<PHINode>(I);
  228. UnwindDestPHIValues.push_back(phi->getIncomingValueForBlock(invokeBB));
  229. }
  230. }
  231. /// The outer unwind destination is the target of unwind edges
  232. /// introduced for calls within the inlined function.
  233. BasicBlock *getOuterUnwindDest() const {
  234. return OuterUnwindDest;
  235. }
  236. EHSelectorInst *getOuterSelector() {
  237. if (!OuterSelector)
  238. OuterSelector = findSelectorForLandingPad(OuterUnwindDest);
  239. return OuterSelector;
  240. }
  241. BasicBlock *getInnerUnwindDest();
  242. bool forwardEHResume(CallInst *call, BasicBlock *src);
  243. /// Add incoming-PHI values to the unwind destination block for
  244. /// the given basic block, using the values for the original
  245. /// invoke's source block.
  246. void addIncomingPHIValuesFor(BasicBlock *BB) const {
  247. addIncomingPHIValuesForInto(BB, OuterUnwindDest);
  248. }
  249. void addIncomingPHIValuesForInto(BasicBlock *src, BasicBlock *dest) const {
  250. BasicBlock::iterator I = dest->begin();
  251. for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
  252. PHINode *phi = cast<PHINode>(I);
  253. phi->addIncoming(UnwindDestPHIValues[i], src);
  254. }
  255. }
  256. };
  257. }
  258. /// Get or create a target for the branch out of rewritten calls to
  259. /// llvm.eh.resume.
  260. BasicBlock *InvokeInliningInfo::getInnerUnwindDest() {
  261. if (InnerUnwindDest) return InnerUnwindDest;
  262. // Find and hoist the llvm.eh.exception and llvm.eh.selector calls
  263. // in the outer landing pad to immediately following the phis.
  264. EHSelectorInst *selector = getOuterSelector();
  265. if (!selector) return 0;
  266. // The call to llvm.eh.exception *must* be in the landing pad.
  267. Instruction *exn = cast<Instruction>(selector->getArgOperand(0));
  268. assert(exn->getParent() == OuterUnwindDest);
  269. // TODO: recognize when we've already done this, so that we don't
  270. // get a linear number of these when inlining calls into lots of
  271. // invokes with the same landing pad.
  272. // Do the hoisting.
  273. Instruction *splitPoint = exn->getParent()->getFirstNonPHI();
  274. assert(splitPoint != selector && "selector-on-exception dominance broken!");
  275. if (splitPoint == exn) {
  276. selector->removeFromParent();
  277. selector->insertAfter(exn);
  278. splitPoint = selector->getNextNode();
  279. } else {
  280. exn->moveBefore(splitPoint);
  281. selector->moveBefore(splitPoint);
  282. }
  283. // Split the landing pad.
  284. InnerUnwindDest = OuterUnwindDest->splitBasicBlock(splitPoint,
  285. OuterUnwindDest->getName() + ".body");
  286. // The number of incoming edges we expect to the inner landing pad.
  287. const unsigned phiCapacity = 2;
  288. // Create corresponding new phis for all the phis in the outer landing pad.
  289. BasicBlock::iterator insertPoint = InnerUnwindDest->begin();
  290. BasicBlock::iterator I = OuterUnwindDest->begin();
  291. for (unsigned i = 0, e = UnwindDestPHIValues.size(); i != e; ++i, ++I) {
  292. PHINode *outerPhi = cast<PHINode>(I);
  293. PHINode *innerPhi = PHINode::Create(outerPhi->getType(), phiCapacity,
  294. outerPhi->getName() + ".lpad-body",
  295. insertPoint);
  296. outerPhi->replaceAllUsesWith(innerPhi);
  297. innerPhi->addIncoming(outerPhi, OuterUnwindDest);
  298. }
  299. // Create a phi for the exception value...
  300. InnerExceptionPHI = PHINode::Create(exn->getType(), phiCapacity,
  301. "exn.lpad-body", insertPoint);
  302. exn->replaceAllUsesWith(InnerExceptionPHI);
  303. selector->setArgOperand(0, exn); // restore this use
  304. InnerExceptionPHI->addIncoming(exn, OuterUnwindDest);
  305. // ...and the selector.
  306. InnerSelectorPHI = PHINode::Create(selector->getType(), phiCapacity,
  307. "selector.lpad-body", insertPoint);
  308. selector->replaceAllUsesWith(InnerSelectorPHI);
  309. InnerSelectorPHI->addIncoming(selector, OuterUnwindDest);
  310. // All done.
  311. return InnerUnwindDest;
  312. }
  313. /// [LIBUNWIND] Try to forward the given call, which logically occurs
  314. /// at the end of the given block, as a branch to the inner unwind
  315. /// block. Returns true if the call was forwarded.
  316. bool InvokeInliningInfo::forwardEHResume(CallInst *call, BasicBlock *src) {
  317. // First, check whether this is a call to the intrinsic.
  318. Function *fn = dyn_cast<Function>(call->getCalledValue());
  319. if (!fn || fn->getName() != "llvm.eh.resume")
  320. return false;
  321. // At this point, we need to return true on all paths, because
  322. // otherwise we'll construct an invoke of the intrinsic, which is
  323. // not well-formed.
  324. // Try to find or make an inner unwind dest, which will fail if we
  325. // can't find a selector call for the outer unwind dest.
  326. BasicBlock *dest = getInnerUnwindDest();
  327. bool hasSelector = (dest != 0);
  328. // If we failed, just use the outer unwind dest, dropping the
  329. // exception and selector on the floor.
  330. if (!hasSelector)
  331. dest = OuterUnwindDest;
  332. // Make a branch.
  333. BranchInst::Create(dest, src);
  334. // Update the phis in the destination. They were inserted in an
  335. // order which makes this work.
  336. addIncomingPHIValuesForInto(src, dest);
  337. if (hasSelector) {
  338. InnerExceptionPHI->addIncoming(call->getArgOperand(0), src);
  339. InnerSelectorPHI->addIncoming(call->getArgOperand(1), src);
  340. }
  341. return true;
  342. }
  343. /// [LIBUNWIND] Check whether this selector is "only cleanups":
  344. /// call i32 @llvm.eh.selector(blah, blah, i32 0)
  345. static bool isCleanupOnlySelector(EHSelectorInst *selector) {
  346. if (selector->getNumArgOperands() != 3) return false;
  347. ConstantInt *val = dyn_cast<ConstantInt>(selector->getArgOperand(2));
  348. return (val && val->isZero());
  349. }
  350. /// HandleCallsInBlockInlinedThroughInvoke - When we inline a basic block into
  351. /// an invoke, we have to turn all of the calls that can throw into
  352. /// invokes. This function analyze BB to see if there are any calls, and if so,
  353. /// it rewrites them to be invokes that jump to InvokeDest and fills in the PHI
  354. /// nodes in that block with the values specified in InvokeDestPHIValues.
  355. ///
  356. /// Returns true to indicate that the next block should be skipped.
  357. static bool HandleCallsInBlockInlinedThroughInvoke(BasicBlock *BB,
  358. InvokeInliningInfo &Invoke) {
  359. for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ) {
  360. Instruction *I = BBI++;
  361. // We only need to check for function calls: inlined invoke
  362. // instructions require no special handling.
  363. CallInst *CI = dyn_cast<CallInst>(I);
  364. if (CI == 0) continue;
  365. // LIBUNWIND: merge selector instructions.
  366. if (EHSelectorInst *Inner = dyn_cast<EHSelectorInst>(CI)) {
  367. EHSelectorInst *Outer = Invoke.getOuterSelector();
  368. if (!Outer) continue;
  369. bool innerIsOnlyCleanup = isCleanupOnlySelector(Inner);
  370. bool outerIsOnlyCleanup = isCleanupOnlySelector(Outer);
  371. // If both selectors contain only cleanups, we don't need to do
  372. // anything. TODO: this is really just a very specific instance
  373. // of a much more general optimization.
  374. if (innerIsOnlyCleanup && outerIsOnlyCleanup) continue;
  375. // Otherwise, we just append the outer selector to the inner selector.
  376. SmallVector<Value*, 16> NewSelector;
  377. for (unsigned i = 0, e = Inner->getNumArgOperands(); i != e; ++i)
  378. NewSelector.push_back(Inner->getArgOperand(i));
  379. for (unsigned i = 2, e = Outer->getNumArgOperands(); i != e; ++i)
  380. NewSelector.push_back(Outer->getArgOperand(i));
  381. CallInst *NewInner =
  382. IRBuilder<>(Inner).CreateCall(Inner->getCalledValue(),
  383. NewSelector.begin(),
  384. NewSelector.end());
  385. // No need to copy attributes, calling convention, etc.
  386. NewInner->takeName(Inner);
  387. Inner->replaceAllUsesWith(NewInner);
  388. Inner->eraseFromParent();
  389. continue;
  390. }
  391. // If this call cannot unwind, don't convert it to an invoke.
  392. if (CI->doesNotThrow())
  393. continue;
  394. // Convert this function call into an invoke instruction.
  395. // First, split the basic block.
  396. BasicBlock *Split = BB->splitBasicBlock(CI, CI->getName()+".noexc");
  397. // Delete the unconditional branch inserted by splitBasicBlock
  398. BB->getInstList().pop_back();
  399. // LIBUNWIND: If this is a call to @llvm.eh.resume, just branch
  400. // directly to the new landing pad.
  401. if (Invoke.forwardEHResume(CI, BB)) {
  402. // TODO: 'Split' is now unreachable; clean it up.
  403. // We want to leave the original call intact so that the call
  404. // graph and other structures won't get misled. We also have to
  405. // avoid processing the next block, or we'll iterate here forever.
  406. return true;
  407. }
  408. // Otherwise, create the new invoke instruction.
  409. ImmutableCallSite CS(CI);
  410. SmallVector<Value*, 8> InvokeArgs(CS.arg_begin(), CS.arg_end());
  411. InvokeInst *II =
  412. InvokeInst::Create(CI->getCalledValue(), Split,
  413. Invoke.getOuterUnwindDest(),
  414. InvokeArgs.begin(), InvokeArgs.end(),
  415. CI->getName(), BB);
  416. II->setCallingConv(CI->getCallingConv());
  417. II->setAttributes(CI->getAttributes());
  418. // Make sure that anything using the call now uses the invoke! This also
  419. // updates the CallGraph if present, because it uses a WeakVH.
  420. CI->replaceAllUsesWith(II);
  421. Split->getInstList().pop_front(); // Delete the original call
  422. // Update any PHI nodes in the exceptional block to indicate that
  423. // there is now a new entry in them.
  424. Invoke.addIncomingPHIValuesFor(BB);
  425. return false;
  426. }
  427. return false;
  428. }
  429. /// HandleInlinedInvoke - If we inlined an invoke site, we need to convert calls
  430. /// in the body of the inlined function into invokes and turn unwind
  431. /// instructions into branches to the invoke unwind dest.
  432. ///
  433. /// II is the invoke instruction being inlined. FirstNewBlock is the first
  434. /// block of the inlined code (the last block is the end of the function),
  435. /// and InlineCodeInfo is information about the code that got inlined.
  436. static void HandleInlinedInvoke(InvokeInst *II, BasicBlock *FirstNewBlock,
  437. ClonedCodeInfo &InlinedCodeInfo) {
  438. BasicBlock *InvokeDest = II->getUnwindDest();
  439. Function *Caller = FirstNewBlock->getParent();
  440. // The inlined code is currently at the end of the function, scan from the
  441. // start of the inlined code to its end, checking for stuff we need to
  442. // rewrite. If the code doesn't have calls or unwinds, we know there is
  443. // nothing to rewrite.
  444. if (!InlinedCodeInfo.ContainsCalls && !InlinedCodeInfo.ContainsUnwinds) {
  445. // Now that everything is happy, we have one final detail. The PHI nodes in
  446. // the exception destination block still have entries due to the original
  447. // invoke instruction. Eliminate these entries (which might even delete the
  448. // PHI node) now.
  449. InvokeDest->removePredecessor(II->getParent());
  450. return;
  451. }
  452. InvokeInliningInfo Invoke(II);
  453. for (Function::iterator BB = FirstNewBlock, E = Caller->end(); BB != E; ++BB){
  454. if (InlinedCodeInfo.ContainsCalls)
  455. if (HandleCallsInBlockInlinedThroughInvoke(BB, Invoke)) {
  456. // Honor a request to skip the next block. We don't need to
  457. // consider UnwindInsts in this case either.
  458. ++BB;
  459. continue;
  460. }
  461. if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
  462. // An UnwindInst requires special handling when it gets inlined into an
  463. // invoke site. Once this happens, we know that the unwind would cause
  464. // a control transfer to the invoke exception destination, so we can
  465. // transform it into a direct branch to the exception destination.
  466. BranchInst::Create(InvokeDest, UI);
  467. // Delete the unwind instruction!
  468. UI->eraseFromParent();
  469. // Update any PHI nodes in the exceptional block to indicate that
  470. // there is now a new entry in them.
  471. Invoke.addIncomingPHIValuesFor(BB);
  472. }
  473. }
  474. // Now that everything is happy, we have one final detail. The PHI nodes in
  475. // the exception destination block still have entries due to the original
  476. // invoke instruction. Eliminate these entries (which might even delete the
  477. // PHI node) now.
  478. InvokeDest->removePredecessor(II->getParent());
  479. }
  480. /// UpdateCallGraphAfterInlining - Once we have cloned code over from a callee
  481. /// into the caller, update the specified callgraph to reflect the changes we
  482. /// made. Note that it's possible that not all code was copied over, so only
  483. /// some edges of the callgraph may remain.
  484. static void UpdateCallGraphAfterInlining(CallSite CS,
  485. Function::iterator FirstNewBlock,
  486. ValueToValueMapTy &VMap,
  487. InlineFunctionInfo &IFI) {
  488. CallGraph &CG = *IFI.CG;
  489. const Function *Caller = CS.getInstruction()->getParent()->getParent();
  490. const Function *Callee = CS.getCalledFunction();
  491. CallGraphNode *CalleeNode = CG[Callee];
  492. CallGraphNode *CallerNode = CG[Caller];
  493. // Since we inlined some uninlined call sites in the callee into the caller,
  494. // add edges from the caller to all of the callees of the callee.
  495. CallGraphNode::iterator I = CalleeNode->begin(), E = CalleeNode->end();
  496. // Consider the case where CalleeNode == CallerNode.
  497. CallGraphNode::CalledFunctionsVector CallCache;
  498. if (CalleeNode == CallerNode) {
  499. CallCache.assign(I, E);
  500. I = CallCache.begin();
  501. E = CallCache.end();
  502. }
  503. for (; I != E; ++I) {
  504. const Value *OrigCall = I->first;
  505. ValueToValueMapTy::iterator VMI = VMap.find(OrigCall);
  506. // Only copy the edge if the call was inlined!
  507. if (VMI == VMap.end() || VMI->second == 0)
  508. continue;
  509. // If the call was inlined, but then constant folded, there is no edge to
  510. // add. Check for this case.
  511. Instruction *NewCall = dyn_cast<Instruction>(VMI->second);
  512. if (NewCall == 0) continue;
  513. // Remember that this call site got inlined for the client of
  514. // InlineFunction.
  515. IFI.InlinedCalls.push_back(NewCall);
  516. // It's possible that inlining the callsite will cause it to go from an
  517. // indirect to a direct call by resolving a function pointer. If this
  518. // happens, set the callee of the new call site to a more precise
  519. // destination. This can also happen if the call graph node of the caller
  520. // was just unnecessarily imprecise.
  521. if (I->second->getFunction() == 0)
  522. if (Function *F = CallSite(NewCall).getCalledFunction()) {
  523. // Indirect call site resolved to direct call.
  524. CallerNode->addCalledFunction(CallSite(NewCall), CG[F]);
  525. continue;
  526. }
  527. CallerNode->addCalledFunction(CallSite(NewCall), I->second);
  528. }
  529. // Update the call graph by deleting the edge from Callee to Caller. We must
  530. // do this after the loop above in case Caller and Callee are the same.
  531. CallerNode->removeCallEdgeFor(CS);
  532. }
  533. /// HandleByValArgument - When inlining a call site that has a byval argument,
  534. /// we have to make the implicit memcpy explicit by adding it.
  535. static Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
  536. const Function *CalledFunc,
  537. InlineFunctionInfo &IFI,
  538. unsigned ByValAlignment) {
  539. const Type *AggTy = cast<PointerType>(Arg->getType())->getElementType();
  540. // If the called function is readonly, then it could not mutate the caller's
  541. // copy of the byval'd memory. In this case, it is safe to elide the copy and
  542. // temporary.
  543. if (CalledFunc->onlyReadsMemory()) {
  544. // If the byval argument has a specified alignment that is greater than the
  545. // passed in pointer, then we either have to round up the input pointer or
  546. // give up on this transformation.
  547. if (ByValAlignment <= 1) // 0 = unspecified, 1 = no particular alignment.
  548. return Arg;
  549. // If the pointer is already known to be sufficiently aligned, or if we can
  550. // round it up to a larger alignment, then we don't need a temporary.
  551. if (getOrEnforceKnownAlignment(Arg, ByValAlignment,
  552. IFI.TD) >= ByValAlignment)
  553. return Arg;
  554. // Otherwise, we have to make a memcpy to get a safe alignment. This is bad
  555. // for code quality, but rarely happens and is required for correctness.
  556. }
  557. LLVMContext &Context = Arg->getContext();
  558. Type *VoidPtrTy = Type::getInt8PtrTy(Context);
  559. // Create the alloca. If we have TargetData, use nice alignment.
  560. unsigned Align = 1;
  561. if (IFI.TD)
  562. Align = IFI.TD->getPrefTypeAlignment(AggTy);
  563. // If the byval had an alignment specified, we *must* use at least that
  564. // alignment, as it is required by the byval argument (and uses of the
  565. // pointer inside the callee).
  566. Align = std::max(Align, ByValAlignment);
  567. Function *Caller = TheCall->getParent()->getParent();
  568. Value *NewAlloca = new AllocaInst(AggTy, 0, Align, Arg->getName(),
  569. &*Caller->begin()->begin());
  570. // Emit a memcpy.
  571. Type *Tys[3] = {VoidPtrTy, VoidPtrTy, Type::getInt64Ty(Context)};
  572. Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
  573. Intrinsic::memcpy,
  574. Tys);
  575. Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
  576. Value *SrcCast = new BitCastInst(Arg, VoidPtrTy, "tmp", TheCall);
  577. Value *Size;
  578. if (IFI.TD == 0)
  579. Size = ConstantExpr::getSizeOf(AggTy);
  580. else
  581. Size = ConstantInt::get(Type::getInt64Ty(Context),
  582. IFI.TD->getTypeStoreSize(AggTy));
  583. // Always generate a memcpy of alignment 1 here because we don't know
  584. // the alignment of the src pointer. Other optimizations can infer
  585. // better alignment.
  586. Value *CallArgs[] = {
  587. DestCast, SrcCast, Size,
  588. ConstantInt::get(Type::getInt32Ty(Context), 1),
  589. ConstantInt::getFalse(Context) // isVolatile
  590. };
  591. IRBuilder<>(TheCall).CreateCall(MemCpyFn, CallArgs, CallArgs+5);
  592. // Uses of the argument in the function should use our new alloca
  593. // instead.
  594. return NewAlloca;
  595. }
  596. // isUsedByLifetimeMarker - Check whether this Value is used by a lifetime
  597. // intrinsic.
  598. static bool isUsedByLifetimeMarker(Value *V) {
  599. for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
  600. ++UI) {
  601. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI)) {
  602. switch (II->getIntrinsicID()) {
  603. default: break;
  604. case Intrinsic::lifetime_start:
  605. case Intrinsic::lifetime_end:
  606. return true;
  607. }
  608. }
  609. }
  610. return false;
  611. }
  612. // hasLifetimeMarkers - Check whether the given alloca already has
  613. // lifetime.start or lifetime.end intrinsics.
  614. static bool hasLifetimeMarkers(AllocaInst *AI) {
  615. const Type *Int8PtrTy = Type::getInt8PtrTy(AI->getType()->getContext());
  616. if (AI->getType() == Int8PtrTy)
  617. return isUsedByLifetimeMarker(AI);
  618. // Do a scan to find all the casts to i8*.
  619. for (Value::use_iterator I = AI->use_begin(), E = AI->use_end(); I != E;
  620. ++I) {
  621. if (I->getType() != Int8PtrTy) continue;
  622. if (I->stripPointerCasts() != AI) continue;
  623. if (isUsedByLifetimeMarker(*I))
  624. return true;
  625. }
  626. return false;
  627. }
  628. /// updateInlinedAtInfo - Helper function used by fixupLineNumbers to recursively
  629. /// update InlinedAtEntry of a DebugLoc.
  630. static DebugLoc updateInlinedAtInfo(const DebugLoc &DL,
  631. const DebugLoc &InlinedAtDL,
  632. LLVMContext &Ctx) {
  633. if (MDNode *IA = DL.getInlinedAt(Ctx)) {
  634. DebugLoc NewInlinedAtDL
  635. = updateInlinedAtInfo(DebugLoc::getFromDILocation(IA), InlinedAtDL, Ctx);
  636. return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
  637. NewInlinedAtDL.getAsMDNode(Ctx));
  638. }
  639. return DebugLoc::get(DL.getLine(), DL.getCol(), DL.getScope(Ctx),
  640. InlinedAtDL.getAsMDNode(Ctx));
  641. }
  642. /// fixupLineNumbers - Update inlined instructions' line numbers to
  643. /// to encode location where these instructions are inlined.
  644. static void fixupLineNumbers(Function *Fn, Function::iterator FI,
  645. Instruction *TheCall) {
  646. DebugLoc TheCallDL = TheCall->getDebugLoc();
  647. if (TheCallDL.isUnknown())
  648. return;
  649. for (; FI != Fn->end(); ++FI) {
  650. for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
  651. BI != BE; ++BI) {
  652. DebugLoc DL = BI->getDebugLoc();
  653. if (!DL.isUnknown())
  654. BI->setDebugLoc(updateInlinedAtInfo(DL, TheCallDL, BI->getContext()));
  655. }
  656. }
  657. }
  658. // InlineFunction - This function inlines the called function into the basic
  659. // block of the caller. This returns false if it is not possible to inline this
  660. // call. The program is still in a well defined state if this occurs though.
  661. //
  662. // Note that this only does one level of inlining. For example, if the
  663. // instruction 'call B' is inlined, and 'B' calls 'C', then the call to 'C' now
  664. // exists in the instruction stream. Similarly this will inline a recursive
  665. // function by one level.
  666. //
  667. bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
  668. Instruction *TheCall = CS.getInstruction();
  669. LLVMContext &Context = TheCall->getContext();
  670. assert(TheCall->getParent() && TheCall->getParent()->getParent() &&
  671. "Instruction not in function!");
  672. // If IFI has any state in it, zap it before we fill it in.
  673. IFI.reset();
  674. const Function *CalledFunc = CS.getCalledFunction();
  675. if (CalledFunc == 0 || // Can't inline external function or indirect
  676. CalledFunc->isDeclaration() || // call, or call to a vararg function!
  677. CalledFunc->getFunctionType()->isVarArg()) return false;
  678. // If the call to the callee is not a tail call, we must clear the 'tail'
  679. // flags on any calls that we inline.
  680. bool MustClearTailCallFlags =
  681. !(isa<CallInst>(TheCall) && cast<CallInst>(TheCall)->isTailCall());
  682. // If the call to the callee cannot throw, set the 'nounwind' flag on any
  683. // calls that we inline.
  684. bool MarkNoUnwind = CS.doesNotThrow();
  685. BasicBlock *OrigBB = TheCall->getParent();
  686. Function *Caller = OrigBB->getParent();
  687. // GC poses two hazards to inlining, which only occur when the callee has GC:
  688. // 1. If the caller has no GC, then the callee's GC must be propagated to the
  689. // caller.
  690. // 2. If the caller has a differing GC, it is invalid to inline.
  691. if (CalledFunc->hasGC()) {
  692. if (!Caller->hasGC())
  693. Caller->setGC(CalledFunc->getGC());
  694. else if (CalledFunc->getGC() != Caller->getGC())
  695. return false;
  696. }
  697. // Get an iterator to the last basic block in the function, which will have
  698. // the new function inlined after it.
  699. //
  700. Function::iterator LastBlock = &Caller->back();
  701. // Make sure to capture all of the return instructions from the cloned
  702. // function.
  703. SmallVector<ReturnInst*, 8> Returns;
  704. ClonedCodeInfo InlinedFunctionInfo;
  705. Function::iterator FirstNewBlock;
  706. { // Scope to destroy VMap after cloning.
  707. ValueToValueMapTy VMap;
  708. assert(CalledFunc->arg_size() == CS.arg_size() &&
  709. "No varargs calls can be inlined!");
  710. // Calculate the vector of arguments to pass into the function cloner, which
  711. // matches up the formal to the actual argument values.
  712. CallSite::arg_iterator AI = CS.arg_begin();
  713. unsigned ArgNo = 0;
  714. for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
  715. E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
  716. Value *ActualArg = *AI;
  717. // When byval arguments actually inlined, we need to make the copy implied
  718. // by them explicit. However, we don't do this if the callee is readonly
  719. // or readnone, because the copy would be unneeded: the callee doesn't
  720. // modify the struct.
  721. if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) {
  722. ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI,
  723. CalledFunc->getParamAlignment(ArgNo+1));
  724. // Calls that we inline may use the new alloca, so we need to clear
  725. // their 'tail' flags if HandleByValArgument introduced a new alloca and
  726. // the callee has calls.
  727. MustClearTailCallFlags |= ActualArg != *AI;
  728. }
  729. VMap[I] = ActualArg;
  730. }
  731. // We want the inliner to prune the code as it copies. We would LOVE to
  732. // have no dead or constant instructions leftover after inlining occurs
  733. // (which can happen, e.g., because an argument was constant), but we'll be
  734. // happy with whatever the cloner can do.
  735. CloneAndPruneFunctionInto(Caller, CalledFunc, VMap,
  736. /*ModuleLevelChanges=*/false, Returns, ".i",
  737. &InlinedFunctionInfo, IFI.TD, TheCall);
  738. // Remember the first block that is newly cloned over.
  739. FirstNewBlock = LastBlock; ++FirstNewBlock;
  740. // Update the callgraph if requested.
  741. if (IFI.CG)
  742. UpdateCallGraphAfterInlining(CS, FirstNewBlock, VMap, IFI);
  743. // Update inlined instructions' line number information.
  744. fixupLineNumbers(Caller, FirstNewBlock, TheCall);
  745. }
  746. // If there are any alloca instructions in the block that used to be the entry
  747. // block for the callee, move them to the entry block of the caller. First
  748. // calculate which instruction they should be inserted before. We insert the
  749. // instructions at the end of the current alloca list.
  750. //
  751. {
  752. BasicBlock::iterator InsertPoint = Caller->begin()->begin();
  753. for (BasicBlock::iterator I = FirstNewBlock->begin(),
  754. E = FirstNewBlock->end(); I != E; ) {
  755. AllocaInst *AI = dyn_cast<AllocaInst>(I++);
  756. if (AI == 0) continue;
  757. // If the alloca is now dead, remove it. This often occurs due to code
  758. // specialization.
  759. if (AI->use_empty()) {
  760. AI->eraseFromParent();
  761. continue;
  762. }
  763. if (!isa<Constant>(AI->getArraySize()))
  764. continue;
  765. // Keep track of the static allocas that we inline into the caller.
  766. IFI.StaticAllocas.push_back(AI);
  767. // Scan for the block of allocas that we can move over, and move them
  768. // all at once.
  769. while (isa<AllocaInst>(I) &&
  770. isa<Constant>(cast<AllocaInst>(I)->getArraySize())) {
  771. IFI.StaticAllocas.push_back(cast<AllocaInst>(I));
  772. ++I;
  773. }
  774. // Transfer all of the allocas over in a block. Using splice means
  775. // that the instructions aren't removed from the symbol table, then
  776. // reinserted.
  777. Caller->getEntryBlock().getInstList().splice(InsertPoint,
  778. FirstNewBlock->getInstList(),
  779. AI, I);
  780. }
  781. }
  782. // Leave lifetime markers for the static alloca's, scoping them to the
  783. // function we just inlined.
  784. if (!IFI.StaticAllocas.empty()) {
  785. IRBuilder<> builder(FirstNewBlock->begin());
  786. for (unsigned ai = 0, ae = IFI.StaticAllocas.size(); ai != ae; ++ai) {
  787. AllocaInst *AI = IFI.StaticAllocas[ai];
  788. // If the alloca is already scoped to something smaller than the whole
  789. // function then there's no need to add redundant, less accurate markers.
  790. if (hasLifetimeMarkers(AI))
  791. continue;
  792. builder.CreateLifetimeStart(AI);
  793. for (unsigned ri = 0, re = Returns.size(); ri != re; ++ri) {
  794. IRBuilder<> builder(Returns[ri]);
  795. builder.CreateLifetimeEnd(AI);
  796. }
  797. }
  798. }
  799. // If the inlined code contained dynamic alloca instructions, wrap the inlined
  800. // code with llvm.stacksave/llvm.stackrestore intrinsics.
  801. if (InlinedFunctionInfo.ContainsDynamicAllocas) {
  802. Module *M = Caller->getParent();
  803. // Get the two intrinsics we care about.
  804. Function *StackSave = Intrinsic::getDeclaration(M, Intrinsic::stacksave);
  805. Function *StackRestore=Intrinsic::getDeclaration(M,Intrinsic::stackrestore);
  806. // Insert the llvm.stacksave.
  807. CallInst *SavedPtr = IRBuilder<>(FirstNewBlock, FirstNewBlock->begin())
  808. .CreateCall(StackSave, "savedstack");
  809. // Insert a call to llvm.stackrestore before any return instructions in the
  810. // inlined function.
  811. for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
  812. IRBuilder<>(Returns[i]).CreateCall(StackRestore, SavedPtr);
  813. }
  814. // Count the number of StackRestore calls we insert.
  815. unsigned NumStackRestores = Returns.size();
  816. // If we are inlining an invoke instruction, insert restores before each
  817. // unwind. These unwinds will be rewritten into branches later.
  818. if (InlinedFunctionInfo.ContainsUnwinds && isa<InvokeInst>(TheCall)) {
  819. for (Function::iterator BB = FirstNewBlock, E = Caller->end();
  820. BB != E; ++BB)
  821. if (UnwindInst *UI = dyn_cast<UnwindInst>(BB->getTerminator())) {
  822. IRBuilder<>(UI).CreateCall(StackRestore, SavedPtr);
  823. ++NumStackRestores;
  824. }
  825. }
  826. }
  827. // If we are inlining tail call instruction through a call site that isn't
  828. // marked 'tail', we must remove the tail marker for any calls in the inlined
  829. // code. Also, calls inlined through a 'nounwind' call site should be marked
  830. // 'nounwind'.
  831. if (InlinedFunctionInfo.ContainsCalls &&
  832. (MustClearTailCallFlags || MarkNoUnwind)) {
  833. for (Function::iterator BB = FirstNewBlock, E = Caller->end();
  834. BB != E; ++BB)
  835. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  836. if (CallInst *CI = dyn_cast<CallInst>(I)) {
  837. if (MustClearTailCallFlags)
  838. CI->setTailCall(false);
  839. if (MarkNoUnwind)
  840. CI->setDoesNotThrow();
  841. }
  842. }
  843. // If we are inlining through a 'nounwind' call site then any inlined 'unwind'
  844. // instructions are unreachable.
  845. if (InlinedFunctionInfo.ContainsUnwinds && MarkNoUnwind)
  846. for (Function::iterator BB = FirstNewBlock, E = Caller->end();
  847. BB != E; ++BB) {
  848. TerminatorInst *Term = BB->getTerminator();
  849. if (isa<UnwindInst>(Term)) {
  850. new UnreachableInst(Context, Term);
  851. BB->getInstList().erase(Term);
  852. }
  853. }
  854. // If we are inlining for an invoke instruction, we must make sure to rewrite
  855. // any inlined 'unwind' instructions into branches to the invoke exception
  856. // destination, and call instructions into invoke instructions.
  857. if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
  858. HandleInlinedInvoke(II, FirstNewBlock, InlinedFunctionInfo);
  859. // If we cloned in _exactly one_ basic block, and if that block ends in a
  860. // return instruction, we splice the body of the inlined callee directly into
  861. // the calling basic block.
  862. if (Returns.size() == 1 && std::distance(FirstNewBlock, Caller->end()) == 1) {
  863. // Move all of the instructions right before the call.
  864. OrigBB->getInstList().splice(TheCall, FirstNewBlock->getInstList(),
  865. FirstNewBlock->begin(), FirstNewBlock->end());
  866. // Remove the cloned basic block.
  867. Caller->getBasicBlockList().pop_back();
  868. // If the call site was an invoke instruction, add a branch to the normal
  869. // destination.
  870. if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall))
  871. BranchInst::Create(II->getNormalDest(), TheCall);
  872. // If the return instruction returned a value, replace uses of the call with
  873. // uses of the returned value.
  874. if (!TheCall->use_empty()) {
  875. ReturnInst *R = Returns[0];
  876. if (TheCall == R->getReturnValue())
  877. TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
  878. else
  879. TheCall->replaceAllUsesWith(R->getReturnValue());
  880. }
  881. // Since we are now done with the Call/Invoke, we can delete it.
  882. TheCall->eraseFromParent();
  883. // Since we are now done with the return instruction, delete it also.
  884. Returns[0]->eraseFromParent();
  885. // We are now done with the inlining.
  886. return true;
  887. }
  888. // Otherwise, we have the normal case, of more than one block to inline or
  889. // multiple return sites.
  890. // We want to clone the entire callee function into the hole between the
  891. // "starter" and "ender" blocks. How we accomplish this depends on whether
  892. // this is an invoke instruction or a call instruction.
  893. BasicBlock *AfterCallBB;
  894. if (InvokeInst *II = dyn_cast<InvokeInst>(TheCall)) {
  895. // Add an unconditional branch to make this look like the CallInst case...
  896. BranchInst *NewBr = BranchInst::Create(II->getNormalDest(), TheCall);
  897. // Split the basic block. This guarantees that no PHI nodes will have to be
  898. // updated due to new incoming edges, and make the invoke case more
  899. // symmetric to the call case.
  900. AfterCallBB = OrigBB->splitBasicBlock(NewBr,
  901. CalledFunc->getName()+".exit");
  902. } else { // It's a call
  903. // If this is a call instruction, we need to split the basic block that
  904. // the call lives in.
  905. //
  906. AfterCallBB = OrigBB->splitBasicBlock(TheCall,
  907. CalledFunc->getName()+".exit");
  908. }
  909. // Change the branch that used to go to AfterCallBB to branch to the first
  910. // basic block of the inlined function.
  911. //
  912. TerminatorInst *Br = OrigBB->getTerminator();
  913. assert(Br && Br->getOpcode() == Instruction::Br &&
  914. "splitBasicBlock broken!");
  915. Br->setOperand(0, FirstNewBlock);
  916. // Now that the function is correct, make it a little bit nicer. In
  917. // particular, move the basic blocks inserted from the end of the function
  918. // into the space made by splitting the source basic block.
  919. Caller->getBasicBlockList().splice(AfterCallBB, Caller->getBasicBlockList(),
  920. FirstNewBlock, Caller->end());
  921. // Handle all of the return instructions that we just cloned in, and eliminate
  922. // any users of the original call/invoke instruction.
  923. const Type *RTy = CalledFunc->getReturnType();
  924. PHINode *PHI = 0;
  925. if (Returns.size() > 1) {
  926. // The PHI node should go at the front of the new basic block to merge all
  927. // possible incoming values.
  928. if (!TheCall->use_empty()) {
  929. PHI = PHINode::Create(RTy, Returns.size(), TheCall->getName(),
  930. AfterCallBB->begin());
  931. // Anything that used the result of the function call should now use the
  932. // PHI node as their operand.
  933. TheCall->replaceAllUsesWith(PHI);
  934. }
  935. // Loop over all of the return instructions adding entries to the PHI node
  936. // as appropriate.
  937. if (PHI) {
  938. for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
  939. ReturnInst *RI = Returns[i];
  940. assert(RI->getReturnValue()->getType() == PHI->getType() &&
  941. "Ret value not consistent in function!");
  942. PHI->addIncoming(RI->getReturnValue(), RI->getParent());
  943. }
  944. }
  945. // Add a branch to the merge points and remove return instructions.
  946. for (unsigned i = 0, e = Returns.size(); i != e; ++i) {
  947. ReturnInst *RI = Returns[i];
  948. BranchInst::Create(AfterCallBB, RI);
  949. RI->eraseFromParent();
  950. }
  951. } else if (!Returns.empty()) {
  952. // Otherwise, if there is exactly one return value, just replace anything
  953. // using the return value of the call with the computed value.
  954. if (!TheCall->use_empty()) {
  955. if (TheCall == Returns[0]->getReturnValue())
  956. TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
  957. else
  958. TheCall->replaceAllUsesWith(Returns[0]->getReturnValue());
  959. }
  960. // Update PHI nodes that use the ReturnBB to use the AfterCallBB.
  961. BasicBlock *ReturnBB = Returns[0]->getParent();
  962. ReturnBB->replaceAllUsesWith(AfterCallBB);
  963. // Splice the code from the return block into the block that it will return
  964. // to, which contains the code that was after the call.
  965. AfterCallBB->getInstList().splice(AfterCallBB->begin(),
  966. ReturnBB->getInstList());
  967. // Delete the return instruction now and empty ReturnBB now.
  968. Returns[0]->eraseFromParent();
  969. ReturnBB->eraseFromParent();
  970. } else if (!TheCall->use_empty()) {
  971. // No returns, but something is using the return value of the call. Just
  972. // nuke the result.
  973. TheCall->replaceAllUsesWith(UndefValue::get(TheCall->getType()));
  974. }
  975. // Since we are now done with the Call/Invoke, we can delete it.
  976. TheCall->eraseFromParent();
  977. // We should always be able to fold the entry block of the function into the
  978. // single predecessor of the block...
  979. assert(cast<BranchInst>(Br)->isUnconditional() && "splitBasicBlock broken!");
  980. BasicBlock *CalleeEntry = cast<BranchInst>(Br)->getSuccessor(0);
  981. // Splice the code entry block into calling block, right before the
  982. // unconditional branch.
  983. CalleeEntry->replaceAllUsesWith(OrigBB); // Update PHI nodes
  984. OrigBB->getInstList().splice(Br, CalleeEntry->getInstList());
  985. // Remove the unconditional branch.
  986. OrigBB->getInstList().erase(Br);
  987. // Now we can remove the CalleeEntry block, which is now empty.
  988. Caller->getBasicBlockList().erase(CalleeEntry);
  989. // If we inserted a phi node, check to see if it has a single value (e.g. all
  990. // the entries are the same or undef). If so, remove the PHI so it doesn't
  991. // block other optimizations.
  992. if (PHI)
  993. if (Value *V = SimplifyInstruction(PHI, IFI.TD)) {
  994. PHI->replaceAllUsesWith(V);
  995. PHI->eraseFromParent();
  996. }
  997. return true;
  998. }