1 //===- PreISelIntrinsicLowering.cpp - Pre-ISel intrinsic lowering pass ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass implements IR lowering for the llvm.load.relative and llvm.objc.*
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/Type.h"
23 #include "llvm/IR/User.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/Casting.h"
29 static bool lowerLoadRelative(Function &F) {
34 Type *Int32Ty = Type::getInt32Ty(F.getContext());
35 Type *Int32PtrTy = Int32Ty->getPointerTo();
36 Type *Int8Ty = Type::getInt8Ty(F.getContext());
38 for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
39 auto CI = dyn_cast<CallInst>(I->getUser());
41 if (!CI || CI->getCalledValue() != &F)
46 B.CreateGEP(Int8Ty, CI->getArgOperand(0), CI->getArgOperand(1));
47 Value *OffsetPtrI32 = B.CreateBitCast(OffsetPtr, Int32PtrTy);
48 Value *OffsetI32 = B.CreateAlignedLoad(OffsetPtrI32, 4);
50 Value *ResultPtr = B.CreateGEP(Int8Ty, CI->getArgOperand(0), OffsetI32);
52 CI->replaceAllUsesWith(ResultPtr);
53 CI->eraseFromParent();
60 static bool lowerObjCCall(Function &F, const char *NewFn,
61 bool setNonLazyBind = 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 Constant* FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
70 if (Function* Fn = dyn_cast<Function>(FCache)) {
71 Fn->setLinkage(F.getLinkage());
72 if (setNonLazyBind && !Fn->isWeakForLinker()) {
73 // If we have Native ARC, set nonlazybind attribute for these APIs for
75 Fn->addFnAttr(Attribute::NonLazyBind);
79 for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
80 auto *CI = dyn_cast<CallInst>(I->getUser());
81 assert(CI->getCalledFunction() && "Cannot lower an indirect call!");
84 IRBuilder<> Builder(CI->getParent(), CI->getIterator());
85 SmallVector<Value *, 8> Args(CI->arg_begin(), CI->arg_end());
86 CallInst *NewCI = Builder.CreateCall(FCache, Args);
87 NewCI->setName(CI->getName());
88 NewCI->setTailCallKind(CI->getTailCallKind());
90 CI->replaceAllUsesWith(NewCI);
91 CI->eraseFromParent();
97 static bool lowerIntrinsics(Module &M) {
99 for (Function &F : M) {
100 if (F.getName().startswith("llvm.load.relative.")) {
101 Changed |= lowerLoadRelative(F);
104 switch (F.getIntrinsicID()) {
107 case Intrinsic::objc_autorelease:
108 Changed |= lowerObjCCall(F, "objc_autorelease");
110 case Intrinsic::objc_autoreleasePoolPop:
111 Changed |= lowerObjCCall(F, "objc_autoreleasePoolPop");
113 case Intrinsic::objc_autoreleasePoolPush:
114 Changed |= lowerObjCCall(F, "objc_autoreleasePoolPush");
116 case Intrinsic::objc_autoreleaseReturnValue:
117 Changed |= lowerObjCCall(F, "objc_autoreleaseReturnValue");
119 case Intrinsic::objc_copyWeak:
120 Changed |= lowerObjCCall(F, "objc_copyWeak");
122 case Intrinsic::objc_destroyWeak:
123 Changed |= lowerObjCCall(F, "objc_destroyWeak");
125 case Intrinsic::objc_initWeak:
126 Changed |= lowerObjCCall(F, "objc_initWeak");
128 case Intrinsic::objc_loadWeak:
129 Changed |= lowerObjCCall(F, "objc_loadWeak");
131 case Intrinsic::objc_loadWeakRetained:
132 Changed |= lowerObjCCall(F, "objc_loadWeakRetained");
134 case Intrinsic::objc_moveWeak:
135 Changed |= lowerObjCCall(F, "objc_moveWeak");
137 case Intrinsic::objc_release:
138 Changed |= lowerObjCCall(F, "objc_release", true);
140 case Intrinsic::objc_retain:
141 Changed |= lowerObjCCall(F, "objc_retain", true);
143 case Intrinsic::objc_retainAutorelease:
144 Changed |= lowerObjCCall(F, "objc_retainAutorelease");
146 case Intrinsic::objc_retainAutoreleaseReturnValue:
147 Changed |= lowerObjCCall(F, "objc_retainAutoreleaseReturnValue");
149 case Intrinsic::objc_retainAutoreleasedReturnValue:
150 Changed |= lowerObjCCall(F, "objc_retainAutoreleasedReturnValue");
152 case Intrinsic::objc_retainBlock:
153 Changed |= lowerObjCCall(F, "objc_retainBlock");
155 case Intrinsic::objc_storeStrong:
156 Changed |= lowerObjCCall(F, "objc_storeStrong");
158 case Intrinsic::objc_storeWeak:
159 Changed |= lowerObjCCall(F, "objc_storeWeak");
161 case Intrinsic::objc_unsafeClaimAutoreleasedReturnValue:
162 Changed |= lowerObjCCall(F, "objc_unsafeClaimAutoreleasedReturnValue");
164 case Intrinsic::objc_retainedObject:
165 Changed |= lowerObjCCall(F, "objc_retainedObject");
167 case Intrinsic::objc_unretainedObject:
168 Changed |= lowerObjCCall(F, "objc_unretainedObject");
170 case Intrinsic::objc_unretainedPointer:
171 Changed |= lowerObjCCall(F, "objc_unretainedPointer");
173 case Intrinsic::objc_retain_autorelease:
174 Changed |= lowerObjCCall(F, "objc_retain_autorelease");
176 case Intrinsic::objc_sync_enter:
177 Changed |= lowerObjCCall(F, "objc_sync_enter");
179 case Intrinsic::objc_sync_exit:
180 Changed |= lowerObjCCall(F, "objc_sync_exit");
189 class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
193 PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
195 bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
198 } // end anonymous namespace
200 char PreISelIntrinsicLoweringLegacyPass::ID;
202 INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
203 "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
206 ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
207 return new PreISelIntrinsicLoweringLegacyPass;
210 PreservedAnalyses PreISelIntrinsicLoweringPass::run(Module &M,
211 ModuleAnalysisManager &AM) {
212 if (!lowerIntrinsics(M))
213 return PreservedAnalyses::all();
215 return PreservedAnalyses::none();