LLVM  14.0.0git
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/IRBuilder.h"
37 #include "llvm/IR/InstIterator.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Intrinsics.h"
40 #include "llvm/InitializePasses.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 *OriginalBB = Guard->getParent();
61  (void)OriginalBB;
62  makeGuardControlFlowExplicit(DeoptIntrinsic, Guard, true);
63  assert(isWidenableBranch(OriginalBB->getTerminator()) && "should hold");
64 
65  Guard->eraseFromParent();
66 }
67 
68 static bool explicifyGuards(Function &F) {
69  // Check if we can cheaply rule out the possibility of not having any work to
70  // do.
71  auto *GuardDecl = F.getParent()->getFunction(
72  Intrinsic::getName(Intrinsic::experimental_guard));
73  if (!GuardDecl || GuardDecl->use_empty())
74  return false;
75 
76  SmallVector<CallInst *, 8> GuardIntrinsics;
77  for (auto &I : instructions(F))
78  if (isGuard(&I))
79  GuardIntrinsics.push_back(cast<CallInst>(&I));
80 
81  if (GuardIntrinsics.empty())
82  return false;
83 
84  auto *DeoptIntrinsic = Intrinsic::getDeclaration(
85  F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
86  DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
87 
88  for (auto *Guard : GuardIntrinsics)
89  turnToExplicitForm(Guard, DeoptIntrinsic);
90 
91  return true;
92 }
93 
95  return explicifyGuards(F);
96 }
97 
99 INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit",
100  "Lower the guard intrinsic to explicit control flow form",
101  false, false)
102 
105  if (explicifyGuards(F))
106  return PreservedAnalyses::none();
107  return PreservedAnalyses::all();
108 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::makeGuardControlFlowExplicit
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
llvm::Intrinsic::getDeclaration
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:1379
IntrinsicInst.h
Scalar.h
InstIterator.h
llvm::Function
Definition: Function.h:61
explicifyGuards
static bool explicifyGuards(Function &F)
Definition: MakeGuardsExplicit.cpp:68
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::Intrinsic::getName
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:879
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::isGuard
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
Definition: GuardUtils.cpp:18
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Intrinsics.h
GuardUtils.h
turnToExplicitForm
static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic)
Definition: MakeGuardsExplicit.cpp:58
MakeGuardsExplicit.h
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
INITIALIZE_PASS
INITIALIZE_PASS(MakeGuardsExplicitLegacyPass, "make-guards-explicit", "Lower the guard intrinsic to explicit control flow form", false, false) PreservedAnalyses MakeGuardsExplicitPass
Definition: MakeGuardsExplicit.cpp:99
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:78
I
#define I(x, y, z)
Definition: MD5.cpp:59
IRBuilder.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::isWidenableBranch
bool isWidenableBranch(const User *U)
Returns true iff U is a widenable branch (that is, parseWidenableBranch returns true).
Definition: GuardUtils.cpp:22
llvm::BasicBlock::getTerminator
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:148
llvm::MakeGuardsExplicitPass
Definition: MakeGuardsExplicit.h:40
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
GuardUtils.h
llvm::initializeMakeGuardsExplicitLegacyPassPass
void initializeMakeGuardsExplicitLegacyPassPass(PassRegistry &)
llvm::Instruction::getParent
const BasicBlock * getParent() const
Definition: Instruction.h:94
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1475
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37