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