PreISelIntrinsicLowering.cpp 8.6 KB

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