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