LLVM 22.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
17#include "llvm/IR/Function.h"
19#include "llvm/IR/Intrinsics.h"
21
22using namespace llvm;
23
25 // Check if we can cheaply rule out the possibility of not having any work to
26 // do.
27 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
28 F.getParent(), Intrinsic::experimental_guard);
29 if (!GuardDecl || GuardDecl->use_empty())
30 return false;
31
33 // Traverse through the users of GuardDecl.
34 // This is presumably cheaper than traversing all instructions in the
35 // function.
36 for (auto *U : GuardDecl->users())
37 if (auto *CI = dyn_cast<CallInst>(U))
38 if (CI->getFunction() == &F)
39 ToLower.push_back(CI);
40
41 if (ToLower.empty())
42 return false;
43
44 auto *DeoptIntrinsic = Intrinsic::getOrInsertDeclaration(
45 F.getParent(), Intrinsic::experimental_deoptimize, {F.getReturnType()});
46 DeoptIntrinsic->setCallingConv(GuardDecl->getCallingConv());
47
48 for (auto *CI : ToLower) {
49 makeGuardControlFlowExplicit(DeoptIntrinsic, CI, false);
50 CI->eraseFromParent();
51 }
52
53 return true;
54}
55
static bool lowerGuardIntrinsic(Function &F)
#define F(x, y, z)
Definition MD5.cpp:55
This file defines the SmallVector class.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
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 ...
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)