123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
- #include "llvm/Analysis/GuardUtils.h"
- #include "llvm/IR/IRBuilder.h"
- #include "llvm/IR/InstIterator.h"
- #include "llvm/IR/IntrinsicInst.h"
- #include "llvm/IR/Intrinsics.h"
- #include "llvm/InitializePasses.h"
- #include "llvm/Pass.h"
- #include "llvm/Transforms/Scalar.h"
- #include "llvm/Transforms/Utils/GuardUtils.h"
- using namespace llvm;
- namespace {
- struct MakeGuardsExplicitLegacyPass : public FunctionPass {
- static char ID;
- MakeGuardsExplicitLegacyPass() : FunctionPass(ID) {
- initializeMakeGuardsExplicitLegacyPassPass(*PassRegistry::getPassRegistry());
- }
- bool runOnFunction(Function &F) override;
- };
- }
- static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic) {
-
- BasicBlock *OriginalBB = Guard->getParent();
- (void)OriginalBB;
- makeGuardControlFlowExplicit(DeoptIntrinsic, Guard, true);
- assert(isWidenableBranch(OriginalBB->getTerminator()) && "should hold");
- Guard->eraseFromParent();
- }
- static bool explicifyGuards(Function &F) {
-
-
- auto *GuardDecl = F.getParent()->getFunction(
- Intrinsic::getName(Intrinsic::experimental_guard));
- if (!GuardDecl || GuardDecl->use_empty())
- return false;
- SmallVector<CallInst *, 8> GuardIntrinsics;
- for (auto &I : instructions(F))
- if (isGuard(&I))
- GuardIntrinsics.push_back(cast<CallInst>(&I));
- if (GuardIntrinsics.empty())
- return false;
- auto *DeoptIntrinsic = Intrinsic::getDeclaration(
- F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
- DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
- for (auto *Guard : GuardIntrinsics)
- turnToExplicitForm(Guard, DeoptIntrinsic);
- return true;
- }
- bool MakeGuardsExplicitLegacyPass::runOnFunction(Function &F) {
- return explicifyGuards(F);
- }
- char MakeGuardsExplicitLegacyPass::ID = 0;
- INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit",
- "Lower the guard intrinsic to explicit control flow form",
- false, false)
- PreservedAnalyses MakeGuardsExplicitPass::run(Function &F,
- FunctionAnalysisManager &) {
- if (explicifyGuards(F))
- return PreservedAnalyses::none();
- return PreservedAnalyses::all();
- }
|