2017-09-14 05:15:20 +08:00
|
|
|
//===- PreISelIntrinsicLowering.cpp - Pre-ISel intrinsic lowering pass ----===//
|
2016-04-23 05:18:02 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-04-23 05:18:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-12-19 06:20:03 +08:00
|
|
|
// This pass implements IR lowering for the llvm.load.relative and llvm.objc.*
|
|
|
|
// intrinsics.
|
2016-04-23 05:18:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-06-25 04:13:42 +08:00
|
|
|
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
|
2019-11-07 09:09:55 +08:00
|
|
|
#include "llvm/Analysis/ObjCARCInstKind.h"
|
2016-04-23 05:18:02 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2016-04-23 05:18:02 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/IR/Type.h"
|
|
|
|
#include "llvm/IR/User.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2016-04-23 05:18:02 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-04-23 05:18:02 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
static bool lowerLoadRelative(Function &F) {
|
2016-04-23 05:18:02 +08:00
|
|
|
if (F.use_empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(F.getContext());
|
|
|
|
Type *Int32PtrTy = Int32Ty->getPointerTo();
|
|
|
|
Type *Int8Ty = Type::getInt8Ty(F.getContext());
|
|
|
|
|
|
|
|
for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
|
|
|
|
auto CI = dyn_cast<CallInst>(I->getUser());
|
|
|
|
++I;
|
|
|
|
if (!CI || CI->getCalledValue() != &F)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
IRBuilder<> B(CI);
|
|
|
|
Value *OffsetPtr =
|
|
|
|
B.CreateGEP(Int8Ty, CI->getArgOperand(0), CI->getArgOperand(1));
|
|
|
|
Value *OffsetPtrI32 = B.CreateBitCast(OffsetPtr, Int32PtrTy);
|
2020-01-23 18:33:12 +08:00
|
|
|
Value *OffsetI32 = B.CreateAlignedLoad(Int32Ty, OffsetPtrI32, Align(4));
|
2016-04-23 05:18:02 +08:00
|
|
|
|
|
|
|
Value *ResultPtr = B.CreateGEP(Int8Ty, CI->getArgOperand(0), OffsetI32);
|
|
|
|
|
|
|
|
CI->replaceAllUsesWith(ResultPtr);
|
|
|
|
CI->eraseFromParent();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:09:55 +08:00
|
|
|
// ObjCARC has knowledge about whether an obj-c runtime function needs to be
|
|
|
|
// always tail-called or never tail-called.
|
|
|
|
static CallInst::TailCallKind getOverridingTailCallKind(const Function &F) {
|
|
|
|
objcarc::ARCInstKind Kind = objcarc::GetFunctionClass(&F);
|
|
|
|
if (objcarc::IsAlwaysTail(Kind))
|
|
|
|
return CallInst::TCK_Tail;
|
|
|
|
else if (objcarc::IsNeverTail(Kind))
|
|
|
|
return CallInst::TCK_NoTail;
|
|
|
|
return CallInst::TCK_None;
|
|
|
|
}
|
|
|
|
|
2018-12-19 06:31:34 +08:00
|
|
|
static bool lowerObjCCall(Function &F, const char *NewFn,
|
|
|
|
bool setNonLazyBind = false) {
|
2018-12-19 06:20:03 +08:00
|
|
|
if (F.use_empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we haven't already looked up this function, check to see if the
|
|
|
|
// program already contains a function with this name.
|
|
|
|
Module *M = F.getParent();
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee FCache = M->getOrInsertFunction(NewFn, F.getFunctionType());
|
2018-12-19 06:42:08 +08:00
|
|
|
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
if (Function *Fn = dyn_cast<Function>(FCache.getCallee())) {
|
2018-12-19 06:42:08 +08:00
|
|
|
Fn->setLinkage(F.getLinkage());
|
|
|
|
if (setNonLazyBind && !Fn->isWeakForLinker()) {
|
|
|
|
// If we have Native ARC, set nonlazybind attribute for these APIs for
|
|
|
|
// performance.
|
2018-12-19 06:31:34 +08:00
|
|
|
Fn->addFnAttr(Attribute::NonLazyBind);
|
2018-12-19 06:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
2018-12-19 06:20:03 +08:00
|
|
|
|
2019-11-07 09:09:55 +08:00
|
|
|
CallInst::TailCallKind OverridingTCK = getOverridingTailCallKind(F);
|
|
|
|
|
2018-12-19 06:20:03 +08:00
|
|
|
for (auto I = F.use_begin(), E = F.use_end(); I != E;) {
|
2019-09-24 18:46:30 +08:00
|
|
|
auto *CI = cast<CallInst>(I->getUser());
|
2018-12-19 06:20:03 +08:00
|
|
|
assert(CI->getCalledFunction() && "Cannot lower an indirect call!");
|
|
|
|
++I;
|
|
|
|
|
|
|
|
IRBuilder<> Builder(CI->getParent(), CI->getIterator());
|
|
|
|
SmallVector<Value *, 8> Args(CI->arg_begin(), CI->arg_end());
|
|
|
|
CallInst *NewCI = Builder.CreateCall(FCache, Args);
|
|
|
|
NewCI->setName(CI->getName());
|
2019-11-07 09:09:55 +08:00
|
|
|
|
|
|
|
// Try to set the most appropriate TailCallKind based on both the current
|
|
|
|
// attributes and the ones that we could get from ObjCARC's special
|
|
|
|
// knowledge of the runtime functions.
|
|
|
|
//
|
|
|
|
// std::max respects both requirements of notail and tail here:
|
|
|
|
// * notail on either the call or from ObjCARC becomes notail
|
|
|
|
// * tail on either side is stronger than none, but not notail
|
|
|
|
CallInst::TailCallKind TCK = CI->getTailCallKind();
|
|
|
|
NewCI->setTailCallKind(std::max(TCK, OverridingTCK));
|
|
|
|
|
2018-12-19 06:20:03 +08:00
|
|
|
if (!CI->use_empty())
|
|
|
|
CI->replaceAllUsesWith(NewCI);
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
static bool lowerIntrinsics(Module &M) {
|
2016-04-23 05:18:02 +08:00
|
|
|
bool Changed = false;
|
|
|
|
for (Function &F : M) {
|
2018-12-19 06:20:03 +08:00
|
|
|
if (F.getName().startswith("llvm.load.relative.")) {
|
2016-04-23 05:18:02 +08:00
|
|
|
Changed |= lowerLoadRelative(F);
|
2018-12-19 06:20:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (F.getIntrinsicID()) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_autorelease:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_autorelease");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_autoreleasePoolPop:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_autoreleasePoolPop");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_autoreleasePoolPush:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_autoreleasePoolPush");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_autoreleaseReturnValue:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_autoreleaseReturnValue");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_copyWeak:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_copyWeak");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_destroyWeak:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_destroyWeak");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_initWeak:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_initWeak");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_loadWeak:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_loadWeak");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_loadWeakRetained:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_loadWeakRetained");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_moveWeak:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_moveWeak");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_release:
|
2018-12-19 06:31:34 +08:00
|
|
|
Changed |= lowerObjCCall(F, "objc_release", true);
|
2018-12-19 06:20:03 +08:00
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retain:
|
2018-12-19 06:31:34 +08:00
|
|
|
Changed |= lowerObjCCall(F, "objc_retain", true);
|
2018-12-19 06:20:03 +08:00
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retainAutorelease:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_retainAutorelease");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retainAutoreleaseReturnValue:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_retainAutoreleaseReturnValue");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retainAutoreleasedReturnValue:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_retainAutoreleasedReturnValue");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retainBlock:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_retainBlock");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_storeStrong:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_storeStrong");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_storeWeak:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_storeWeak");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_unsafeClaimAutoreleasedReturnValue:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_unsafeClaimAutoreleasedReturnValue");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retainedObject:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_retainedObject");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_unretainedObject:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_unretainedObject");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_unretainedPointer:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_unretainedPointer");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_retain_autorelease:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_retain_autorelease");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_sync_enter:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_sync_enter");
|
|
|
|
break;
|
|
|
|
case Intrinsic::objc_sync_exit:
|
|
|
|
Changed |= lowerObjCCall(F, "objc_sync_exit");
|
|
|
|
break;
|
|
|
|
}
|
2016-04-23 05:18:02 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
namespace {
|
|
|
|
|
2016-06-25 04:13:42 +08:00
|
|
|
class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
|
2016-04-23 05:18:02 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
2017-09-14 05:15:20 +08:00
|
|
|
|
2016-06-25 04:13:42 +08:00
|
|
|
PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
|
2016-04-23 05:18:02 +08:00
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
|
2016-04-23 05:18:02 +08:00
|
|
|
};
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2016-06-25 04:13:42 +08:00
|
|
|
char PreISelIntrinsicLoweringLegacyPass::ID;
|
2016-04-23 05:18:02 +08:00
|
|
|
|
2016-06-25 04:13:42 +08:00
|
|
|
INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
|
|
|
|
"pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
|
|
|
|
false, false)
|
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
|
2016-06-25 04:13:42 +08:00
|
|
|
return new PreISelIntrinsicLoweringLegacyPass;
|
|
|
|
}
|
2016-04-23 05:18:02 +08:00
|
|
|
|
2016-06-25 04:13:42 +08:00
|
|
|
PreservedAnalyses PreISelIntrinsicLoweringPass::run(Module &M,
|
|
|
|
ModuleAnalysisManager &AM) {
|
|
|
|
if (!lowerIntrinsics(M))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
else
|
|
|
|
return PreservedAnalyses::none();
|
2016-04-23 05:18:02 +08:00
|
|
|
}
|