LLVM 20.0.0git
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
18#include "llvm/IR/Function.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/IR/Module.h"
24
25using namespace llvm;
26
28 // Check if we can cheaply rule out the possibility of not having any work to
29 // do.
30 auto *GuardDecl = F.getParent()->getFunction(
31 Intrinsic::getName(Intrinsic::experimental_guard));
32 if (!GuardDecl || GuardDecl->use_empty())
33 return false;
34
36 // Traverse through the users of GuardDecl.
37 // This is presumably cheaper than traversing all instructions in the
38 // function.
39 for (auto *U : GuardDecl->users())
40 if (auto *CI = dyn_cast<CallInst>(U))
41 if (CI->getFunction() == &F)
42 ToLower.push_back(CI);
43
44 if (ToLower.empty())
45 return false;
46
47 auto *DeoptIntrinsic = Intrinsic::getDeclaration(
48 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
49 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
50
51 for (auto *CI : ToLower) {
52 makeGuardControlFlowExplicit(DeoptIntrinsic, CI, false);
53 CI->eraseFromParent();
54 }
55
56 return true;
57}
58
63
65}
static bool lowerGuardIntrinsic(Function &F)
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
This file defines the SmallVector class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
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
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
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)