LLVM  8.0.1
MakeGuardsExplicit.cpp
Go to the documentation of this file.
1 //===- MakeGuardsExplicit.cpp - Turn guard intrinsics into guard branches -===//
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 the new form of
11 // guard represented as widenable explicit branch to the deopt block. The
12 // difference between this pass and LowerGuardIntrinsic is that after this pass
13 // the guard represented as intrinsic:
14 //
15 // call void(i1, ...) @llvm.experimental.guard(i1 %old_cond) [ "deopt"() ]
16 //
17 // transforms to a guard represented as widenable explicit branch:
18 //
19 // %widenable_cond = call i1 @llvm.experimental.widenable.condition()
20 // br i1 (%old_cond & %widenable_cond), label %guarded, label %deopt
21 //
22 // Here:
23 // - The semantics of @llvm.experimental.widenable.condition allows to replace
24 // %widenable_cond with the construction (%widenable_cond & %any_other_cond)
25 // without loss of correctness;
26 // - %guarded is the lower part of old guard intrinsic's parent block split by
27 // the intrinsic call;
28 // - %deopt is a block containing a sole call to @llvm.experimental.deoptimize
29 // intrinsic.
30 //
31 // Therefore, this branch preserves the property of widenability.
32 //
33 //===----------------------------------------------------------------------===//
34 
37 #include "llvm/IR/InstIterator.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/IR/IRBuilder.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Transforms/Scalar.h"
44 
45 using namespace llvm;
46 
47 namespace {
48 struct MakeGuardsExplicitLegacyPass : public FunctionPass {
49  static char ID;
50  MakeGuardsExplicitLegacyPass() : FunctionPass(ID) {
52  }
53 
54  bool runOnFunction(Function &F) override;
55 };
56 }
57 
58 static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic) {
59  // Replace the guard with an explicit branch (just like in GuardWidening).
60  BasicBlock *BB = Guard->getParent();
61  makeGuardControlFlowExplicit(DeoptIntrinsic, Guard);
62  BranchInst *ExplicitGuard = cast<BranchInst>(BB->getTerminator());
63  assert(ExplicitGuard->isConditional() && "Must be!");
64 
65  // We want the guard to be expressed as explicit control flow, but still be
66  // widenable. For that, we add Widenable Condition intrinsic call to the
67  // guard's condition.
68  IRBuilder<> B(ExplicitGuard);
69  auto *WidenableCondition =
71  {}, {}, nullptr, "widenable_cond");
72  WidenableCondition->setCallingConv(Guard->getCallingConv());
73  auto *NewCond =
74  B.CreateAnd(ExplicitGuard->getCondition(), WidenableCondition);
75  NewCond->setName("exiplicit_guard_cond");
76  ExplicitGuard->setCondition(NewCond);
77  Guard->eraseFromParent();
78 }
79 
80 static bool explicifyGuards(Function &F) {
81  // Check if we can cheaply rule out the possibility of not having any work to
82  // do.
83  auto *GuardDecl = F.getParent()->getFunction(
85  if (!GuardDecl || GuardDecl->use_empty())
86  return false;
87 
88  SmallVector<CallInst *, 8> GuardIntrinsics;
89  for (auto &I : instructions(F))
90  if (isGuard(&I))
91  GuardIntrinsics.push_back(cast<CallInst>(&I));
92 
93  if (GuardIntrinsics.empty())
94  return false;
95 
96  auto *DeoptIntrinsic = Intrinsic::getDeclaration(
98  DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
99 
100  for (auto *Guard : GuardIntrinsics)
101  turnToExplicitForm(Guard, DeoptIntrinsic);
102 
103  return true;
104 }
105 
107  return explicifyGuards(F);
108 }
109 
111 INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit",
112  "Lower the guard intrinsic to explicit control flow form",
113  false, false)
114 
117  if (explicifyGuards(F))
118  return PreservedAnalyses::none();
119  return PreservedAnalyses::all();
120 }
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:68
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
This class represents a function call, abstracting a target machine&#39;s calling convention.
F(f)
Value * getCondition() const
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:138
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:626
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
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
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
Conditional or Unconditional Branch instruction.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
void initializeMakeGuardsExplicitLegacyPassPass(PassRegistry &)
static bool explicifyGuards(Function &F)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
bool isConditional() const
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
CallingConv::ID getCallingConv() const
Definition: InstrTypes.h:1225
#define I(x, y, z)
Definition: MD5.cpp:58
void setCondition(Value *V)
static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit", "Lower the guard intrinsic to explicit control flow form", false, false) PreservedAnalyses MakeGuardsExplicitPass
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.
const BasicBlock * getParent() const
Definition: Instruction.h:67