LLVM  8.0.1
LowerGuardIntrinsic.cpp
Go to the documentation of this file.
1 //===- LowerGuardIntrinsic.cpp - Lower the guard intrinsic ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass lowers the llvm.experimental.guard intrinsic to a conditional call
11 // to @llvm.experimental.deoptimize. Once this happens, the guard can no longer
12 // be widened.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Transforms/Scalar.h"
28 
29 using namespace llvm;
30 
31 namespace {
32 struct LowerGuardIntrinsicLegacyPass : public FunctionPass {
33  static char ID;
34  LowerGuardIntrinsicLegacyPass() : FunctionPass(ID) {
37  }
38 
39  bool runOnFunction(Function &F) override;
40 };
41 }
42 
44  // Check if we can cheaply rule out the possibility of not having any work to
45  // do.
46  auto *GuardDecl = F.getParent()->getFunction(
48  if (!GuardDecl || GuardDecl->use_empty())
49  return false;
50 
52  for (auto &I : instructions(F))
53  if (isGuard(&I))
54  ToLower.push_back(cast<CallInst>(&I));
55 
56  if (ToLower.empty())
57  return false;
58 
59  auto *DeoptIntrinsic = Intrinsic::getDeclaration(
61  DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
62 
63  for (auto *CI : ToLower) {
64  makeGuardControlFlowExplicit(DeoptIntrinsic, CI);
65  CI->eraseFromParent();
66  }
67 
68  return true;
69 }
70 
72  return lowerGuardIntrinsic(F);
73 }
74 
76 INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
77  "Lower the guard intrinsic to normal control flow", false,
78  false)
79 
81  return new LowerGuardIntrinsicLegacyPass();
82 }
83 
86  if (lowerGuardIntrinsic(F))
87  return PreservedAnalyses::none();
88 
89  return PreservedAnalyses::all();
90 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:81
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void makeGuardControlFlowExplicit(Function *DeoptIntrinsic, CallInst *Guard)
Splits control flow at point of Guard, replacing it with explicit branch by the condition of guard&#39;s ...
Definition: GuardUtils.cpp:27
F(f)
static bool lowerGuardIntrinsic(Function &F)
INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic", "Lower the guard intrinsic to normal control flow", false, false) Pass *llvm
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:626
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1020
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:157
static bool runOnFunction(Function &F, bool PostInlining)
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:217
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:169
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
Pass * createLowerGuardIntrinsicPass()
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
bool isGuard(const User *U)
Returns true iff U has semantics of a guard.
Definition: GuardUtils.cpp:18
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:176
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:56
#define I(x, y, z)
Definition: MD5.cpp:58
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:566
inst_range instructions(Function *F)
Definition: InstIterator.h:134
A container for analyses that lazily runs them and caches their results.
void initializeLowerGuardIntrinsicLegacyPassPass(PassRegistry &)