CallPromotionUtils.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. //===- CallPromotionUtils.cpp - Utilities for call promotion ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements utilities useful for promoting indirect call sites to
  10. // direct call sites.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/CallPromotionUtils.h"
  14. #include "llvm/Analysis/Loads.h"
  15. #include "llvm/Analysis/TypeMetadataUtils.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "call-promotion-utils"
  21. /// Fix-up phi nodes in an invoke instruction's normal destination.
  22. ///
  23. /// After versioning an invoke instruction, values coming from the original
  24. /// block will now be coming from the "merge" block. For example, in the code
  25. /// below:
  26. ///
  27. /// then_bb:
  28. /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
  29. ///
  30. /// else_bb:
  31. /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
  32. ///
  33. /// merge_bb:
  34. /// %t2 = phi i32 [ %t0, %then_bb ], [ %t1, %else_bb ]
  35. /// br %normal_dst
  36. ///
  37. /// normal_dst:
  38. /// %t3 = phi i32 [ %x, %orig_bb ], ...
  39. ///
  40. /// "orig_bb" is no longer a predecessor of "normal_dst", so the phi nodes in
  41. /// "normal_dst" must be fixed to refer to "merge_bb":
  42. ///
  43. /// normal_dst:
  44. /// %t3 = phi i32 [ %x, %merge_bb ], ...
  45. ///
  46. static void fixupPHINodeForNormalDest(InvokeInst *Invoke, BasicBlock *OrigBlock,
  47. BasicBlock *MergeBlock) {
  48. for (PHINode &Phi : Invoke->getNormalDest()->phis()) {
  49. int Idx = Phi.getBasicBlockIndex(OrigBlock);
  50. if (Idx == -1)
  51. continue;
  52. Phi.setIncomingBlock(Idx, MergeBlock);
  53. }
  54. }
  55. /// Fix-up phi nodes in an invoke instruction's unwind destination.
  56. ///
  57. /// After versioning an invoke instruction, values coming from the original
  58. /// block will now be coming from either the "then" block or the "else" block.
  59. /// For example, in the code below:
  60. ///
  61. /// then_bb:
  62. /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
  63. ///
  64. /// else_bb:
  65. /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
  66. ///
  67. /// unwind_dst:
  68. /// %t3 = phi i32 [ %x, %orig_bb ], ...
  69. ///
  70. /// "orig_bb" is no longer a predecessor of "unwind_dst", so the phi nodes in
  71. /// "unwind_dst" must be fixed to refer to "then_bb" and "else_bb":
  72. ///
  73. /// unwind_dst:
  74. /// %t3 = phi i32 [ %x, %then_bb ], [ %x, %else_bb ], ...
  75. ///
  76. static void fixupPHINodeForUnwindDest(InvokeInst *Invoke, BasicBlock *OrigBlock,
  77. BasicBlock *ThenBlock,
  78. BasicBlock *ElseBlock) {
  79. for (PHINode &Phi : Invoke->getUnwindDest()->phis()) {
  80. int Idx = Phi.getBasicBlockIndex(OrigBlock);
  81. if (Idx == -1)
  82. continue;
  83. auto *V = Phi.getIncomingValue(Idx);
  84. Phi.setIncomingBlock(Idx, ThenBlock);
  85. Phi.addIncoming(V, ElseBlock);
  86. }
  87. }
  88. /// Create a phi node for the returned value of a call or invoke instruction.
  89. ///
  90. /// After versioning a call or invoke instruction that returns a value, we have
  91. /// to merge the value of the original and new instructions. We do this by
  92. /// creating a phi node and replacing uses of the original instruction with this
  93. /// phi node.
  94. ///
  95. /// For example, if \p OrigInst is defined in "else_bb" and \p NewInst is
  96. /// defined in "then_bb", we create the following phi node:
  97. ///
  98. /// ; Uses of the original instruction are replaced by uses of the phi node.
  99. /// %t0 = phi i32 [ %orig_inst, %else_bb ], [ %new_inst, %then_bb ],
  100. ///
  101. static void createRetPHINode(Instruction *OrigInst, Instruction *NewInst,
  102. BasicBlock *MergeBlock, IRBuilder<> &Builder) {
  103. if (OrigInst->getType()->isVoidTy() || OrigInst->use_empty())
  104. return;
  105. Builder.SetInsertPoint(&MergeBlock->front());
  106. PHINode *Phi = Builder.CreatePHI(OrigInst->getType(), 0);
  107. SmallVector<User *, 16> UsersToUpdate(OrigInst->users());
  108. for (User *U : UsersToUpdate)
  109. U->replaceUsesOfWith(OrigInst, Phi);
  110. Phi->addIncoming(OrigInst, OrigInst->getParent());
  111. Phi->addIncoming(NewInst, NewInst->getParent());
  112. }
  113. /// Cast a call or invoke instruction to the given type.
  114. ///
  115. /// When promoting a call site, the return type of the call site might not match
  116. /// that of the callee. If this is the case, we have to cast the returned value
  117. /// to the correct type. The location of the cast depends on if we have a call
  118. /// or invoke instruction.
  119. ///
  120. /// For example, if the call instruction below requires a bitcast after
  121. /// promotion:
  122. ///
  123. /// orig_bb:
  124. /// %t0 = call i32 @func()
  125. /// ...
  126. ///
  127. /// The bitcast is placed after the call instruction:
  128. ///
  129. /// orig_bb:
  130. /// ; Uses of the original return value are replaced by uses of the bitcast.
  131. /// %t0 = call i32 @func()
  132. /// %t1 = bitcast i32 %t0 to ...
  133. /// ...
  134. ///
  135. /// A similar transformation is performed for invoke instructions. However,
  136. /// since invokes are terminating, a new block is created for the bitcast. For
  137. /// example, if the invoke instruction below requires a bitcast after promotion:
  138. ///
  139. /// orig_bb:
  140. /// %t0 = invoke i32 @func() to label %normal_dst unwind label %unwind_dst
  141. ///
  142. /// The edge between the original block and the invoke's normal destination is
  143. /// split, and the bitcast is placed there:
  144. ///
  145. /// orig_bb:
  146. /// %t0 = invoke i32 @func() to label %split_bb unwind label %unwind_dst
  147. ///
  148. /// split_bb:
  149. /// ; Uses of the original return value are replaced by uses of the bitcast.
  150. /// %t1 = bitcast i32 %t0 to ...
  151. /// br label %normal_dst
  152. ///
  153. static void createRetBitCast(CallBase &CB, Type *RetTy, CastInst **RetBitCast) {
  154. // Save the users of the calling instruction. These uses will be changed to
  155. // use the bitcast after we create it.
  156. SmallVector<User *, 16> UsersToUpdate(CB.users());
  157. // Determine an appropriate location to create the bitcast for the return
  158. // value. The location depends on if we have a call or invoke instruction.
  159. Instruction *InsertBefore = nullptr;
  160. if (auto *Invoke = dyn_cast<InvokeInst>(&CB))
  161. InsertBefore =
  162. &SplitEdge(Invoke->getParent(), Invoke->getNormalDest())->front();
  163. else
  164. InsertBefore = &*std::next(CB.getIterator());
  165. // Bitcast the return value to the correct type.
  166. auto *Cast = CastInst::CreateBitOrPointerCast(&CB, RetTy, "", InsertBefore);
  167. if (RetBitCast)
  168. *RetBitCast = Cast;
  169. // Replace all the original uses of the calling instruction with the bitcast.
  170. for (User *U : UsersToUpdate)
  171. U->replaceUsesOfWith(&CB, Cast);
  172. }
  173. /// Predicate and clone the given call site.
  174. ///
  175. /// This function creates an if-then-else structure at the location of the call
  176. /// site. The "if" condition compares the call site's called value to the given
  177. /// callee. The original call site is moved into the "else" block, and a clone
  178. /// of the call site is placed in the "then" block. The cloned instruction is
  179. /// returned.
  180. ///
  181. /// For example, the call instruction below:
  182. ///
  183. /// orig_bb:
  184. /// %t0 = call i32 %ptr()
  185. /// ...
  186. ///
  187. /// Is replace by the following:
  188. ///
  189. /// orig_bb:
  190. /// %cond = icmp eq i32 ()* %ptr, @func
  191. /// br i1 %cond, %then_bb, %else_bb
  192. ///
  193. /// then_bb:
  194. /// ; The clone of the original call instruction is placed in the "then"
  195. /// ; block. It is not yet promoted.
  196. /// %t1 = call i32 %ptr()
  197. /// br merge_bb
  198. ///
  199. /// else_bb:
  200. /// ; The original call instruction is moved to the "else" block.
  201. /// %t0 = call i32 %ptr()
  202. /// br merge_bb
  203. ///
  204. /// merge_bb:
  205. /// ; Uses of the original call instruction are replaced by uses of the phi
  206. /// ; node.
  207. /// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]
  208. /// ...
  209. ///
  210. /// A similar transformation is performed for invoke instructions. However,
  211. /// since invokes are terminating, more work is required. For example, the
  212. /// invoke instruction below:
  213. ///
  214. /// orig_bb:
  215. /// %t0 = invoke %ptr() to label %normal_dst unwind label %unwind_dst
  216. ///
  217. /// Is replace by the following:
  218. ///
  219. /// orig_bb:
  220. /// %cond = icmp eq i32 ()* %ptr, @func
  221. /// br i1 %cond, %then_bb, %else_bb
  222. ///
  223. /// then_bb:
  224. /// ; The clone of the original invoke instruction is placed in the "then"
  225. /// ; block, and its normal destination is set to the "merge" block. It is
  226. /// ; not yet promoted.
  227. /// %t1 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
  228. ///
  229. /// else_bb:
  230. /// ; The original invoke instruction is moved into the "else" block, and
  231. /// ; its normal destination is set to the "merge" block.
  232. /// %t0 = invoke i32 %ptr() to label %merge_bb unwind label %unwind_dst
  233. ///
  234. /// merge_bb:
  235. /// ; Uses of the original invoke instruction are replaced by uses of the
  236. /// ; phi node, and the merge block branches to the normal destination.
  237. /// %t2 = phi i32 [ %t0, %else_bb ], [ %t1, %then_bb ]
  238. /// br %normal_dst
  239. ///
  240. /// An indirect musttail call is processed slightly differently in that:
  241. /// 1. No merge block needed for the orginal and the cloned callsite, since
  242. /// either one ends the flow. No phi node is needed either.
  243. /// 2. The return statement following the original call site is duplicated too
  244. /// and placed immediately after the cloned call site per the IR convention.
  245. ///
  246. /// For example, the musttail call instruction below:
  247. ///
  248. /// orig_bb:
  249. /// %t0 = musttail call i32 %ptr()
  250. /// ...
  251. ///
  252. /// Is replaced by the following:
  253. ///
  254. /// cond_bb:
  255. /// %cond = icmp eq i32 ()* %ptr, @func
  256. /// br i1 %cond, %then_bb, %orig_bb
  257. ///
  258. /// then_bb:
  259. /// ; The clone of the original call instruction is placed in the "then"
  260. /// ; block. It is not yet promoted.
  261. /// %t1 = musttail call i32 %ptr()
  262. /// ret %t1
  263. ///
  264. /// orig_bb:
  265. /// ; The original call instruction stays in its original block.
  266. /// %t0 = musttail call i32 %ptr()
  267. /// ret %t0
  268. CallBase &llvm::versionCallSite(CallBase &CB, Value *Callee,
  269. MDNode *BranchWeights) {
  270. IRBuilder<> Builder(&CB);
  271. CallBase *OrigInst = &CB;
  272. BasicBlock *OrigBlock = OrigInst->getParent();
  273. // Create the compare. The called value and callee must have the same type to
  274. // be compared.
  275. if (CB.getCalledOperand()->getType() != Callee->getType())
  276. Callee = Builder.CreateBitCast(Callee, CB.getCalledOperand()->getType());
  277. auto *Cond = Builder.CreateICmpEQ(CB.getCalledOperand(), Callee);
  278. if (OrigInst->isMustTailCall()) {
  279. // Create an if-then structure. The original instruction stays in its block,
  280. // and a clone of the original instruction is placed in the "then" block.
  281. Instruction *ThenTerm =
  282. SplitBlockAndInsertIfThen(Cond, &CB, false, BranchWeights);
  283. BasicBlock *ThenBlock = ThenTerm->getParent();
  284. ThenBlock->setName("if.true.direct_targ");
  285. CallBase *NewInst = cast<CallBase>(OrigInst->clone());
  286. NewInst->insertBefore(ThenTerm);
  287. // Place a clone of the optional bitcast after the new call site.
  288. Value *NewRetVal = NewInst;
  289. auto Next = OrigInst->getNextNode();
  290. if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) {
  291. assert(BitCast->getOperand(0) == OrigInst &&
  292. "bitcast following musttail call must use the call");
  293. auto NewBitCast = BitCast->clone();
  294. NewBitCast->replaceUsesOfWith(OrigInst, NewInst);
  295. NewBitCast->insertBefore(ThenTerm);
  296. NewRetVal = NewBitCast;
  297. Next = BitCast->getNextNode();
  298. }
  299. // Place a clone of the return instruction after the new call site.
  300. ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
  301. assert(Ret && "musttail call must precede a ret with an optional bitcast");
  302. auto NewRet = Ret->clone();
  303. if (Ret->getReturnValue())
  304. NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal);
  305. NewRet->insertBefore(ThenTerm);
  306. // A return instructions is terminating, so we don't need the terminator
  307. // instruction just created.
  308. ThenTerm->eraseFromParent();
  309. return *NewInst;
  310. }
  311. // Create an if-then-else structure. The original instruction is moved into
  312. // the "else" block, and a clone of the original instruction is placed in the
  313. // "then" block.
  314. Instruction *ThenTerm = nullptr;
  315. Instruction *ElseTerm = nullptr;
  316. SplitBlockAndInsertIfThenElse(Cond, &CB, &ThenTerm, &ElseTerm, BranchWeights);
  317. BasicBlock *ThenBlock = ThenTerm->getParent();
  318. BasicBlock *ElseBlock = ElseTerm->getParent();
  319. BasicBlock *MergeBlock = OrigInst->getParent();
  320. ThenBlock->setName("if.true.direct_targ");
  321. ElseBlock->setName("if.false.orig_indirect");
  322. MergeBlock->setName("if.end.icp");
  323. CallBase *NewInst = cast<CallBase>(OrigInst->clone());
  324. OrigInst->moveBefore(ElseTerm);
  325. NewInst->insertBefore(ThenTerm);
  326. // If the original call site is an invoke instruction, we have extra work to
  327. // do since invoke instructions are terminating. We have to fix-up phi nodes
  328. // in the invoke's normal and unwind destinations.
  329. if (auto *OrigInvoke = dyn_cast<InvokeInst>(OrigInst)) {
  330. auto *NewInvoke = cast<InvokeInst>(NewInst);
  331. // Invoke instructions are terminating, so we don't need the terminator
  332. // instructions that were just created.
  333. ThenTerm->eraseFromParent();
  334. ElseTerm->eraseFromParent();
  335. // Branch from the "merge" block to the original normal destination.
  336. Builder.SetInsertPoint(MergeBlock);
  337. Builder.CreateBr(OrigInvoke->getNormalDest());
  338. // Fix-up phi nodes in the original invoke's normal and unwind destinations.
  339. fixupPHINodeForNormalDest(OrigInvoke, OrigBlock, MergeBlock);
  340. fixupPHINodeForUnwindDest(OrigInvoke, MergeBlock, ThenBlock, ElseBlock);
  341. // Now set the normal destinations of the invoke instructions to be the
  342. // "merge" block.
  343. OrigInvoke->setNormalDest(MergeBlock);
  344. NewInvoke->setNormalDest(MergeBlock);
  345. }
  346. // Create a phi node for the returned value of the call site.
  347. createRetPHINode(OrigInst, NewInst, MergeBlock, Builder);
  348. return *NewInst;
  349. }
  350. bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
  351. const char **FailureReason) {
  352. assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");
  353. auto &DL = Callee->getParent()->getDataLayout();
  354. // Check the return type. The callee's return value type must be bitcast
  355. // compatible with the call site's type.
  356. Type *CallRetTy = CB.getType();
  357. Type *FuncRetTy = Callee->getReturnType();
  358. if (CallRetTy != FuncRetTy)
  359. if (!CastInst::isBitOrNoopPointerCastable(FuncRetTy, CallRetTy, DL)) {
  360. if (FailureReason)
  361. *FailureReason = "Return type mismatch";
  362. return false;
  363. }
  364. // The number of formal arguments of the callee.
  365. unsigned NumParams = Callee->getFunctionType()->getNumParams();
  366. // The number of actual arguments in the call.
  367. unsigned NumArgs = CB.arg_size();
  368. // Check the number of arguments. The callee and call site must agree on the
  369. // number of arguments.
  370. if (NumArgs != NumParams && !Callee->isVarArg()) {
  371. if (FailureReason)
  372. *FailureReason = "The number of arguments mismatch";
  373. return false;
  374. }
  375. // Check the argument types. The callee's formal argument types must be
  376. // bitcast compatible with the corresponding actual argument types of the call
  377. // site.
  378. unsigned I = 0;
  379. for (; I < NumParams; ++I) {
  380. // Make sure that the callee and call agree on byval/inalloca. The types do
  381. // not have to match.
  382. if (Callee->hasParamAttribute(I, Attribute::ByVal) !=
  383. CB.getAttributes().hasParamAttr(I, Attribute::ByVal)) {
  384. if (FailureReason)
  385. *FailureReason = "byval mismatch";
  386. return false;
  387. }
  388. if (Callee->hasParamAttribute(I, Attribute::InAlloca) !=
  389. CB.getAttributes().hasParamAttr(I, Attribute::InAlloca)) {
  390. if (FailureReason)
  391. *FailureReason = "inalloca mismatch";
  392. return false;
  393. }
  394. Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I);
  395. Type *ActualTy = CB.getArgOperand(I)->getType();
  396. if (FormalTy == ActualTy)
  397. continue;
  398. if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {
  399. if (FailureReason)
  400. *FailureReason = "Argument type mismatch";
  401. return false;
  402. }
  403. // MustTail call needs stricter type match. See
  404. // Verifier::verifyMustTailCall().
  405. if (CB.isMustTailCall()) {
  406. PointerType *PF = dyn_cast<PointerType>(FormalTy);
  407. PointerType *PA = dyn_cast<PointerType>(ActualTy);
  408. if (!PF || !PA || PF->getAddressSpace() != PA->getAddressSpace()) {
  409. if (FailureReason)
  410. *FailureReason = "Musttail call Argument type mismatch";
  411. return false;
  412. }
  413. }
  414. }
  415. for (; I < NumArgs; I++) {
  416. // Vararg functions can have more arguments than parameters.
  417. assert(Callee->isVarArg());
  418. if (CB.paramHasAttr(I, Attribute::StructRet)) {
  419. if (FailureReason)
  420. *FailureReason = "SRet arg to vararg function";
  421. return false;
  422. }
  423. }
  424. return true;
  425. }
  426. CallBase &llvm::promoteCall(CallBase &CB, Function *Callee,
  427. CastInst **RetBitCast) {
  428. assert(!CB.getCalledFunction() && "Only indirect call sites can be promoted");
  429. // Set the called function of the call site to be the given callee (but don't
  430. // change the type).
  431. CB.setCalledOperand(Callee);
  432. // Since the call site will no longer be direct, we must clear metadata that
  433. // is only appropriate for indirect calls. This includes !prof and !callees
  434. // metadata.
  435. CB.setMetadata(LLVMContext::MD_prof, nullptr);
  436. CB.setMetadata(LLVMContext::MD_callees, nullptr);
  437. // If the function type of the call site matches that of the callee, no
  438. // additional work is required.
  439. if (CB.getFunctionType() == Callee->getFunctionType())
  440. return CB;
  441. // Save the return types of the call site and callee.
  442. Type *CallSiteRetTy = CB.getType();
  443. Type *CalleeRetTy = Callee->getReturnType();
  444. // Change the function type of the call site the match that of the callee.
  445. CB.mutateFunctionType(Callee->getFunctionType());
  446. // Inspect the arguments of the call site. If an argument's type doesn't
  447. // match the corresponding formal argument's type in the callee, bitcast it
  448. // to the correct type.
  449. auto CalleeType = Callee->getFunctionType();
  450. auto CalleeParamNum = CalleeType->getNumParams();
  451. LLVMContext &Ctx = Callee->getContext();
  452. const AttributeList &CallerPAL = CB.getAttributes();
  453. // The new list of argument attributes.
  454. SmallVector<AttributeSet, 4> NewArgAttrs;
  455. bool AttributeChanged = false;
  456. for (unsigned ArgNo = 0; ArgNo < CalleeParamNum; ++ArgNo) {
  457. auto *Arg = CB.getArgOperand(ArgNo);
  458. Type *FormalTy = CalleeType->getParamType(ArgNo);
  459. Type *ActualTy = Arg->getType();
  460. if (FormalTy != ActualTy) {
  461. auto *Cast = CastInst::CreateBitOrPointerCast(Arg, FormalTy, "", &CB);
  462. CB.setArgOperand(ArgNo, Cast);
  463. // Remove any incompatible attributes for the argument.
  464. AttrBuilder ArgAttrs(Ctx, CallerPAL.getParamAttrs(ArgNo));
  465. ArgAttrs.remove(AttributeFuncs::typeIncompatible(FormalTy));
  466. // We may have a different byval/inalloca type.
  467. if (ArgAttrs.getByValType())
  468. ArgAttrs.addByValAttr(Callee->getParamByValType(ArgNo));
  469. if (ArgAttrs.getInAllocaType())
  470. ArgAttrs.addInAllocaAttr(Callee->getParamInAllocaType(ArgNo));
  471. NewArgAttrs.push_back(AttributeSet::get(Ctx, ArgAttrs));
  472. AttributeChanged = true;
  473. } else
  474. NewArgAttrs.push_back(CallerPAL.getParamAttrs(ArgNo));
  475. }
  476. // If the return type of the call site doesn't match that of the callee, cast
  477. // the returned value to the appropriate type.
  478. // Remove any incompatible return value attribute.
  479. AttrBuilder RAttrs(Ctx, CallerPAL.getRetAttrs());
  480. if (!CallSiteRetTy->isVoidTy() && CallSiteRetTy != CalleeRetTy) {
  481. createRetBitCast(CB, CallSiteRetTy, RetBitCast);
  482. RAttrs.remove(AttributeFuncs::typeIncompatible(CalleeRetTy));
  483. AttributeChanged = true;
  484. }
  485. // Set the new callsite attribute.
  486. if (AttributeChanged)
  487. CB.setAttributes(AttributeList::get(Ctx, CallerPAL.getFnAttrs(),
  488. AttributeSet::get(Ctx, RAttrs),
  489. NewArgAttrs));
  490. return CB;
  491. }
  492. CallBase &llvm::promoteCallWithIfThenElse(CallBase &CB, Function *Callee,
  493. MDNode *BranchWeights) {
  494. // Version the indirect call site. If the called value is equal to the given
  495. // callee, 'NewInst' will be executed, otherwise the original call site will
  496. // be executed.
  497. CallBase &NewInst = versionCallSite(CB, Callee, BranchWeights);
  498. // Promote 'NewInst' so that it directly calls the desired function.
  499. return promoteCall(NewInst, Callee);
  500. }
  501. bool llvm::tryPromoteCall(CallBase &CB) {
  502. assert(!CB.getCalledFunction());
  503. Module *M = CB.getCaller()->getParent();
  504. const DataLayout &DL = M->getDataLayout();
  505. Value *Callee = CB.getCalledOperand();
  506. LoadInst *VTableEntryLoad = dyn_cast<LoadInst>(Callee);
  507. if (!VTableEntryLoad)
  508. return false; // Not a vtable entry load.
  509. Value *VTableEntryPtr = VTableEntryLoad->getPointerOperand();
  510. APInt VTableOffset(DL.getTypeSizeInBits(VTableEntryPtr->getType()), 0);
  511. Value *VTableBasePtr = VTableEntryPtr->stripAndAccumulateConstantOffsets(
  512. DL, VTableOffset, /* AllowNonInbounds */ true);
  513. LoadInst *VTablePtrLoad = dyn_cast<LoadInst>(VTableBasePtr);
  514. if (!VTablePtrLoad)
  515. return false; // Not a vtable load.
  516. Value *Object = VTablePtrLoad->getPointerOperand();
  517. APInt ObjectOffset(DL.getTypeSizeInBits(Object->getType()), 0);
  518. Value *ObjectBase = Object->stripAndAccumulateConstantOffsets(
  519. DL, ObjectOffset, /* AllowNonInbounds */ true);
  520. if (!(isa<AllocaInst>(ObjectBase) && ObjectOffset == 0))
  521. // Not an Alloca or the offset isn't zero.
  522. return false;
  523. // Look for the vtable pointer store into the object by the ctor.
  524. BasicBlock::iterator BBI(VTablePtrLoad);
  525. Value *VTablePtr = FindAvailableLoadedValue(
  526. VTablePtrLoad, VTablePtrLoad->getParent(), BBI, 0, nullptr, nullptr);
  527. if (!VTablePtr)
  528. return false; // No vtable found.
  529. APInt VTableOffsetGVBase(DL.getTypeSizeInBits(VTablePtr->getType()), 0);
  530. Value *VTableGVBase = VTablePtr->stripAndAccumulateConstantOffsets(
  531. DL, VTableOffsetGVBase, /* AllowNonInbounds */ true);
  532. GlobalVariable *GV = dyn_cast<GlobalVariable>(VTableGVBase);
  533. if (!(GV && GV->isConstant() && GV->hasDefinitiveInitializer()))
  534. // Not in the form of a global constant variable with an initializer.
  535. return false;
  536. Constant *VTableGVInitializer = GV->getInitializer();
  537. APInt VTableGVOffset = VTableOffsetGVBase + VTableOffset;
  538. if (!(VTableGVOffset.getActiveBits() <= 64))
  539. return false; // Out of range.
  540. Constant *Ptr = getPointerAtOffset(VTableGVInitializer,
  541. VTableGVOffset.getZExtValue(),
  542. *M);
  543. if (!Ptr)
  544. return false; // No constant (function) pointer found.
  545. Function *DirectCallee = dyn_cast<Function>(Ptr->stripPointerCasts());
  546. if (!DirectCallee)
  547. return false; // No function pointer found.
  548. if (!isLegalToPromote(CB, DirectCallee))
  549. return false;
  550. // Success.
  551. promoteCall(CB, DirectCallee);
  552. return true;
  553. }
  554. #undef DEBUG_TYPE