PreISelIntrinsicLowering.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //===- PreISelIntrinsicLowering.cpp - Pre-ISel intrinsic lowering pass ----===//
  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 pass implements IR lowering for the llvm.load.relative and llvm.objc.*
  10. // intrinsics.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
  14. #include "llvm/Analysis/ObjCARCInstKind.h"
  15. #include "llvm/Analysis/ObjCARCUtil.h"
  16. #include "llvm/CodeGen/Passes.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/IRBuilder.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/IR/Intrinsics.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/IR/Type.h"
  23. #include "llvm/IR/User.h"
  24. #include "llvm/InitializePasses.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/Casting.h"
  27. using namespace llvm;
  28. static bool lowerLoadRelative(Function &F) {
  29. if (F.use_empty())
  30. return false;
  31. bool Changed = false;
  32. Type *Int32Ty = Type::getInt32Ty(F.getContext());
  33. Type *Int32PtrTy = Int32Ty->getPointerTo();
  34. Type *Int8Ty = Type::getInt8Ty(F.getContext());
  35. for (Use &U : llvm::make_early_inc_range(F.uses())) {
  36. auto CI = dyn_cast<CallInst>(U.getUser());
  37. if (!CI || CI->getCalledOperand() != &F)
  38. continue;
  39. IRBuilder<> B(CI);
  40. Value *OffsetPtr =
  41. B.CreateGEP(Int8Ty, CI->getArgOperand(0), CI->getArgOperand(1));
  42. Value *OffsetPtrI32 = B.CreateBitCast(OffsetPtr, Int32PtrTy);
  43. Value *OffsetI32 = B.CreateAlignedLoad(Int32Ty, OffsetPtrI32, Align(4));
  44. Value *ResultPtr = B.CreateGEP(Int8Ty, CI->getArgOperand(0), OffsetI32);
  45. CI->replaceAllUsesWith(ResultPtr);
  46. CI->eraseFromParent();
  47. Changed = true;
  48. }
  49. return Changed;
  50. }
  51. // ObjCARC has knowledge about whether an obj-c runtime function needs to be
  52. // always tail-called or never tail-called.
  53. static CallInst::TailCallKind getOverridingTailCallKind(const Function &F) {
  54. objcarc::ARCInstKind Kind = objcarc::GetFunctionClass(&F);
  55. if (objcarc::IsAlwaysTail(Kind))
  56. return CallInst::TCK_Tail;
  57. else if (objcarc::IsNeverTail(Kind))
  58. return CallInst::TCK_NoTail;
  59. return CallInst::TCK_None;
  60. }
  61. static bool lowerObjCCall(Function &F, const char *NewFn,
  62. bool setNonLazyBind = false) {
  63. if (F.use_empty())
  64. return false;
  65. // If we haven't already looked up this function, check to see if the
  66. // program already contains a function with this name.
  67. Module *M = F.getParent();
  68. FunctionCallee FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
  69. if (Function *Fn = dyn_cast<Function>(FCache.getCallee())) {
  70. Fn->setLinkage(F.getLinkage());
  71. if (setNonLazyBind && !Fn->isWeakForLinker()) {
  72. // If we have Native ARC, set nonlazybind attribute for these APIs for
  73. // performance.
  74. Fn->addFnAttr(Attribute::NonLazyBind);
  75. }
  76. }
  77. CallInst::TailCallKind OverridingTCK = getOverridingTailCallKind(F);
  78. for (Use &U : llvm::make_early_inc_range(F.uses())) {
  79. auto *CB = cast<CallBase>(U.getUser());
  80. if (CB->getCalledFunction() != &F) {
  81. objcarc::ARCInstKind Kind = objcarc::getAttachedARCFunctionKind(CB);
  82. (void)Kind;
  83. assert((Kind == objcarc::ARCInstKind::RetainRV ||
  84. Kind == objcarc::ARCInstKind::UnsafeClaimRV) &&
  85. "use expected to be the argument of operand bundle "
  86. "\"clang.arc.attachedcall\"");
  87. U.set(FCache.getCallee());
  88. continue;
  89. }
  90. auto *CI = cast<CallInst>(CB);
  91. assert(CI->getCalledFunction() && "Cannot lower an indirect call!");
  92. IRBuilder<> Builder(CI->getParent(), CI->getIterator());
  93. SmallVector<Value *, 8> Args(CI->args());
  94. CallInst *NewCI = Builder.CreateCall(FCache, Args);
  95. NewCI->setName(CI->getName());
  96. // Try to set the most appropriate TailCallKind based on both the current
  97. // attributes and the ones that we could get from ObjCARC's special
  98. // knowledge of the runtime functions.
  99. //
  100. // std::max respects both requirements of notail and tail here:
  101. // * notail on either the call or from ObjCARC becomes notail
  102. // * tail on either side is stronger than none, but not notail
  103. CallInst::TailCallKind TCK = CI->getTailCallKind();
  104. NewCI->setTailCallKind(std::max(TCK, OverridingTCK));
  105. if (!CI->use_empty())
  106. CI->replaceAllUsesWith(NewCI);
  107. CI->eraseFromParent();
  108. }
  109. return true;
  110. }
  111. static bool lowerIntrinsics(Module &M) {
  112. bool Changed = false;
  113. for (Function &F : M) {
  114. if (F.getName().startswith("llvm.load.relative.")) {
  115. Changed |= lowerLoadRelative(F);
  116. continue;
  117. }
  118. switch (F.getIntrinsicID()) {
  119. default:
  120. break;
  121. case Intrinsic::objc_autorelease:
  122. Changed |= lowerObjCCall(F, "objc_autorelease");
  123. break;
  124. case Intrinsic::objc_autoreleasePoolPop:
  125. Changed |= lowerObjCCall(F, "objc_autoreleasePoolPop");
  126. break;
  127. case Intrinsic::objc_autoreleasePoolPush:
  128. Changed |= lowerObjCCall(F, "objc_autoreleasePoolPush");
  129. break;
  130. case Intrinsic::objc_autoreleaseReturnValue:
  131. Changed |= lowerObjCCall(F, "objc_autoreleaseReturnValue");
  132. break;
  133. case Intrinsic::objc_copyWeak:
  134. Changed |= lowerObjCCall(F, "objc_copyWeak");
  135. break;
  136. case Intrinsic::objc_destroyWeak:
  137. Changed |= lowerObjCCall(F, "objc_destroyWeak");
  138. break;
  139. case Intrinsic::objc_initWeak:
  140. Changed |= lowerObjCCall(F, "objc_initWeak");
  141. break;
  142. case Intrinsic::objc_loadWeak:
  143. Changed |= lowerObjCCall(F, "objc_loadWeak");
  144. break;
  145. case Intrinsic::objc_loadWeakRetained:
  146. Changed |= lowerObjCCall(F, "objc_loadWeakRetained");
  147. break;
  148. case Intrinsic::objc_moveWeak:
  149. Changed |= lowerObjCCall(F, "objc_moveWeak");
  150. break;
  151. case Intrinsic::objc_release:
  152. Changed |= lowerObjCCall(F, "objc_release", true);
  153. break;
  154. case Intrinsic::objc_retain:
  155. Changed |= lowerObjCCall(F, "objc_retain", true);
  156. break;
  157. case Intrinsic::objc_retainAutorelease:
  158. Changed |= lowerObjCCall(F, "objc_retainAutorelease");
  159. break;
  160. case Intrinsic::objc_retainAutoreleaseReturnValue:
  161. Changed |= lowerObjCCall(F, "objc_retainAutoreleaseReturnValue");
  162. break;
  163. case Intrinsic::objc_retainAutoreleasedReturnValue:
  164. Changed |= lowerObjCCall(F, "objc_retainAutoreleasedReturnValue");
  165. break;
  166. case Intrinsic::objc_retainBlock:
  167. Changed |= lowerObjCCall(F, "objc_retainBlock");
  168. break;
  169. case Intrinsic::objc_storeStrong:
  170. Changed |= lowerObjCCall(F, "objc_storeStrong");
  171. break;
  172. case Intrinsic::objc_storeWeak:
  173. Changed |= lowerObjCCall(F, "objc_storeWeak");
  174. break;
  175. case Intrinsic::objc_unsafeClaimAutoreleasedReturnValue:
  176. Changed |= lowerObjCCall(F, "objc_unsafeClaimAutoreleasedReturnValue");
  177. break;
  178. case Intrinsic::objc_retainedObject:
  179. Changed |= lowerObjCCall(F, "objc_retainedObject");
  180. break;
  181. case Intrinsic::objc_unretainedObject:
  182. Changed |= lowerObjCCall(F, "objc_unretainedObject");
  183. break;
  184. case Intrinsic::objc_unretainedPointer:
  185. Changed |= lowerObjCCall(F, "objc_unretainedPointer");
  186. break;
  187. case Intrinsic::objc_retain_autorelease:
  188. Changed |= lowerObjCCall(F, "objc_retain_autorelease");
  189. break;
  190. case Intrinsic::objc_sync_enter:
  191. Changed |= lowerObjCCall(F, "objc_sync_enter");
  192. break;
  193. case Intrinsic::objc_sync_exit:
  194. Changed |= lowerObjCCall(F, "objc_sync_exit");
  195. break;
  196. }
  197. }
  198. return Changed;
  199. }
  200. namespace {
  201. class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
  202. public:
  203. static char ID;
  204. PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
  205. bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
  206. };
  207. } // end anonymous namespace
  208. char PreISelIntrinsicLoweringLegacyPass::ID;
  209. INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
  210. "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
  211. false, false)
  212. ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
  213. return new PreISelIntrinsicLoweringLegacyPass;
  214. }
  215. PreservedAnalyses PreISelIntrinsicLoweringPass::run(Module &M,
  216. ModuleAnalysisManager &AM) {
  217. if (!lowerIntrinsics(M))
  218. return PreservedAnalyses::all();
  219. else
  220. return PreservedAnalyses::none();
  221. }