LLVM 17.0.0git
LowerGuardIntrinsic.cpp
Go to the documentation of this file.
1//===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
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 lowers the llvm.experimental.guard intrinsic to a conditional call
10// to @llvm.experimental.deoptimize. Once this happens, the guard can no longer
11// be widened.
12//
13//===----------------------------------------------------------------------===//
14
18#include "llvm/IR/Function.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/IR/Module.h"
24#include "llvm/Pass.h"
27
28using namespace llvm;
29
30namespace {
31struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
32 static char ID;
33 LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
36 }
37
38 bool runOnFunction(Function &F) override;
39};
40}
41
43 // Check if we can cheaply rule out the possibility of not having any work to
44 // do.
45 auto *GuardDecl = F.getParent()->getFunction(
46 Intrinsic::getName(Intrinsic::experimental_guard));
47 if (!GuardDecl || GuardDecl->use_empty())
48 return false;
49
51 // Traverse through the users of GuardDecl.
52 // This is presumably cheaper than traversing all instructions in the
53 // function.
54 for (auto *U : GuardDecl->users())
55 if (auto *CI = dyn_cast<CallInst>(U))
56 if (CI->getFunction() == &F)
57 ToLower.push_back(CI);
58
59 if (ToLower.empty())
60 return false;
61
62 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
63 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
64 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
65
66 for (auto *CI : ToLower) {
67 makeGuardControlFlowExplicit(DeoptIntrinsic, CI, false);
68 CI->eraseFromParent();
69 }
70
71 return true;
72}
73
74bool LowerGuardIntrinsicLegacyPass::runOnFunction(Function &F) {
75 return lowerGuardIntrinsic(F);
76}
77
78char LowerGuardIntrinsicLegacyPass::ID = 0;
79INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
80 "Lower the guard intrinsic to normal control flow", false,
81 false)
82
84 return new LowerGuardIntrinsicLegacyPass();
85}
86
91
93}
static bool lowerGuardIntrinsic(Function &F)
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the SmallVector class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:992
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1465
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void makeGuardControlFlowExplicit(Function *DeoptIntrinsic, CallInst *Guard, bool UseWC)
Splits control flow at point of Guard, replacing it with explicit branch by the condition of guard's ...
Definition: GuardUtils.cpp:30
Pass * createLowerGuardIntrinsicPass()
void initializeLowerGuardIntrinsicLegacyPassPass(PassRegistry &)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)