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