LLVM  10.0.0svn
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 
16 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/InstIterator.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Intrinsics.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Transforms/Scalar.h"
27 
28 using namespace llvm;
29 
30 namespace {
31 struct 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  for (auto &I : instructions(F))
52  if (isGuard(&I))
53  ToLower.push_back(cast<CallInst>(&I));
54 
55  if (ToLower.empty())
56  return false;
57 
58  auto *DeoptIntrinsic = Intrinsic::getDeclaration(
59  F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
60  DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
61 
62  for (auto *CI : ToLower) {
63  makeGuardControlFlowExplicit(DeoptIntrinsic, CI);
64  CI->eraseFromParent();
65  }
66 
67  return true;
68 }
69 
71  return lowerGuardIntrinsic(F);
72 }
73 
75 INITIALIZE_PASS(LowerGuardIntrinsicLegacyPass, "lower-guard-intrinsic",
76  "Lower the guard intrinsic to normal control flow", false,
77  false)
78 
80  return new LowerGuardIntrinsicLegacyPass();
81 }
82 
85  if (lowerGuardIntrinsic(F))
86  return PreservedAnalyses::none();
87 
88  return PreservedAnalyses::all();
89 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
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:23
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:26
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:639
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:1079
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
static bool runOnFunction(Function &F, bool PostInlining)
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:216
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:168
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
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:837
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 expressed in a form of call of llvm.experimental.guard intrinsic.
Definition: GuardUtils.cpp:17
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:174
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
#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:575
inst_range instructions(Function *F)
Definition: InstIterator.h:133
A container for analyses that lazily runs them and caches their results.
void initializeLowerGuardIntrinsicLegacyPassPass(PassRegistry &)