LLVM 20.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
38#include "llvm/IR/Intrinsics.h"
39#include "llvm/IR/Module.h"
41#include "llvm/Pass.h"
43
44using namespace llvm;
45
46static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic) {
47 // Replace the guard with an explicit branch (just like in GuardWidening).
48 BasicBlock *OriginalBB = Guard->getParent();
49 (void)OriginalBB;
50 makeGuardControlFlowExplicit(DeoptIntrinsic, Guard, true);
51 assert(isWidenableBranch(OriginalBB->getTerminator()) && "should hold");
52
53 Guard->eraseFromParent();
54}
55
56static bool explicifyGuards(Function &F) {
57 // Check if we can cheaply rule out the possibility of not having any work to
58 // do.
59 auto *GuardDecl = F.getParent()->getFunction(
60 Intrinsic::getName(Intrinsic::experimental_guard));
61 if (!GuardDecl || GuardDecl->use_empty())
62 return false;
63
64 SmallVector<CallInst *, 8> GuardIntrinsics;
65 for (auto &I : instructions(F))
66 if (isGuard(&I))
67 GuardIntrinsics.push_back(cast<CallInst>(&I));
68
69 if (GuardIntrinsics.empty())
70 return false;
71
72 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
73 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
74 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
75
76 for (auto *Guard : GuardIntrinsics)
77 turnToExplicitForm(Guard, DeoptIntrinsic);
78
79 return true;
80}
81
84 if (explicifyGuards(F))
87}
Expand Atomic instructions
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
static void turnToExplicitForm(CallInst *Guard, Function *DeoptIntrinsic)
static bool explicifyGuards(Function &F)
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
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.h:239
This class represents a function call, abstracting a target machine's calling convention.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
const ParentTy * getParent() const
Definition: ilist_node.h:32
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:1096
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1539
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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
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
bool isWidenableBranch(const User *U)
Returns true iff U is a widenable branch (that is, extractWidenableCondition returns widenable condit...
Definition: GuardUtils.cpp:26
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)