LLVM 24.0.0git
InstructionPrecedenceTracking.cpp
Go to the documentation of this file.
1//===-- InstructionPrecedenceTracking.cpp -----------------------*- C++ -*-===//
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// Implements a class that is able to define some instructions as "special"
9// (e.g. as having implicit control flow, or writing memory, or having another
10// interesting property) and then efficiently answers queries of the types:
11// 1. Are there any special instructions in the block of interest?
12// 2. Return first of the special instructions in the given block;
13// 3. Check if the given instruction is preceeded by the first special
14// instruction in the same block.
15// The class provides caching that allows to answer these queries quickly. The
16// user must make sure that the cached data is invalidated properly whenever
17// a content of some tracked block is changed.
18//===----------------------------------------------------------------------===//
19
21#include "AnalysisOptions.h"
24
25using namespace llvm;
26
28 const BasicBlock *BB) {
29#ifndef NDEBUG
30 // If there is a bug connected to invalid cache, turn on
31 // -ipt-expensive-asserts to catch this situation as early as possible.
32 if (getAnalysisOptions(BB->getContext()).ipt_expensive_asserts)
33 validateAll();
34 else
35 validate(BB);
36#endif
37
38 auto [It, Inserted] = FirstSpecialInsts.try_emplace(BB);
39 if (Inserted) {
40 for (const auto &I : *BB) {
41 if (isSpecialInstruction(&I)) {
42 It->second = &I;
43 break;
44 }
45 }
46 }
47 return It->second;
48}
49
54
56 const Instruction *Insn) {
57 const Instruction *MaybeFirstSpecial =
59 return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore(Insn);
60}
61
62#ifndef NDEBUG
63void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
64 auto It = FirstSpecialInsts.find(BB);
65 // Bail if we don't have anything cached for this block.
66 if (It == FirstSpecialInsts.end())
67 return;
68
69 const Instruction *Cached = It->second;
70 assert((!Cached || Cached->getParent() == BB) &&
71 "Cached first special instruction is not in the block!");
72
73 // The cache is allowed to be conservative: the cached instruction may be
74 // earlier than the actual first special instruction (e.g. if an instruction
75 // stopped being special after a RAUW of one of its operands), but it must
76 // never be later than it.
77 for (const Instruction &Insn : *BB) {
78 if (&Insn == Cached)
79 return;
80 if (isSpecialInstruction(&Insn)) {
81 assert(Cached && "Block is marked as having no special instructions but "
82 "in fact it has some!");
83 llvm_unreachable("Cached first special instruction is too late!");
84 }
85 }
86}
87
88void InstructionPrecedenceTracking::validateAll() const {
89 // Check that for every known block the cached value is correct.
90 for (const auto &It : FirstSpecialInsts)
91 validate(It.first);
92}
93#endif
94
96 const BasicBlock *BB) {
97 if (isSpecialInstruction(Inst))
98 FirstSpecialInsts.erase(BB);
99}
100
102 auto *BB = Inst->getParent();
103 assert(BB && "must be called before instruction is actually removed");
104 auto It = FirstSpecialInsts.find(BB);
105 if (It != FirstSpecialInsts.end() && It->second == Inst)
106 FirstSpecialInsts.erase(It);
107}
108
110 for (const auto *U : Inst->users()) {
111 if (const auto *UI = dyn_cast<Instruction>(U))
113 }
114}
115
117 FirstSpecialInsts.clear();
118#ifndef NDEBUG
119 // The map should be valid after clearing (at least empty).
120 validateAll();
121#endif
122}
123
125 const Instruction *Insn) const {
126 // If a block's instruction doesn't always pass the control to its successor
127 // instruction, mark the block as having implicit control flow. We use them
128 // to avoid wrong assumptions of sort "if A is executed and B post-dominates
129 // A, then B is also executed". This is not true is there is an implicit
130 // control flow instruction (e.g. a guard) between them.
132}
133
135 const Instruction *Insn) const {
136 using namespace PatternMatch;
138 return false;
139 return Insn->mayWriteToMemory();
140}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
bool isSpecialInstruction(const Instruction *Insn) const override
A predicate that defines whether or not the instruction Insn is considered special and needs to be tr...
LLVM_ABI void clear()
Invalidates all information from this tracking.
LLVM_ABI void removeUsersOf(const Instruction *Inst)
Notifies this tracking that we are going to replace all uses of Inst.
LLVM_ABI void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB)
Notifies this tracking that we are going to insert a new instruction Inst to the basic block BB.
LLVM_ABI void removeInstruction(const Instruction *Inst)
Notifies this tracking that we are going to remove the instruction Inst It makes all necessary update...
LLVM_ABI const Instruction * getFirstSpecialInstruction(const BasicBlock *BB)
Returns the topmost special instruction from the block BB.
virtual bool isSpecialInstruction(const Instruction *Insn) const =0
A predicate that defines whether or not the instruction Insn is considered special and needs to be tr...
LLVM_ABI bool hasSpecialInstructions(const BasicBlock *BB)
Returns true iff at least one instruction from the basic block BB is special.
LLVM_ABI bool isPreceededBySpecialInstruction(const Instruction *Insn)
Returns true iff the first special instruction of Insn's block exists and dominates Insn.
LLVM_ABI bool mayWriteToMemory() const LLVM_READONLY
Return true if this instruction may modify memory.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
iterator_range< user_iterator > users()
bool isSpecialInstruction(const Instruction *Insn) const override
A predicate that defines whether or not the instruction Insn is considered special and needs to be tr...
const ParentTy * getParent() const
Definition ilist_node.h:34
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool match(Val *V, const Pattern &P)
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
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:643
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...