LLVM 24.0.0git
InstructionPrecedenceTracking.h
Go to the documentation of this file.
1//===-- InstructionPrecedenceTracking.h -------------------------*- 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
20#ifndef LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
21#define LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
22
23#include "llvm/ADT/DenseMap.h"
25
26namespace llvm {
27
28class BasicBlock;
29class Instruction;
30
32 // Maps a block to the topmost special instruction in it. If the value is
33 // nullptr, it means that it is known that this block does not contain any
34 // special instructions. The cached instruction may conservatively be earlier
35 // than the actual topmost special instruction (e.g. if it is no longer
36 // special after one of its operands was replaced), but never later.
38
39#ifndef NDEBUG
40 /// Asserts that the cached info for \p BB is conservatively correct, i.e.
41 /// the cached instruction is not later than the actual first special
42 /// instruction. This helps to catch the usage error of accessing a block
43 /// without properly invalidating after a previous transform.
44 void validate(const BasicBlock *BB) const;
45
46 /// Asserts whether or not the contents of this tracking is up-to-date. This
47 /// helps to catch the usage error of accessing a block without properly
48 /// invalidating after a previous transform.
49 void validateAll() const;
50#endif
51
52protected:
53 /// Returns the topmost special instruction from the block \p BB. Returns
54 /// nullptr if there is no special instructions in the block.
56
57 /// Returns true iff at least one instruction from the basic block \p BB is
58 /// special.
60
61 /// Returns true iff the first special instruction of \p Insn's block exists
62 /// and dominates \p Insn.
64
65 /// A predicate that defines whether or not the instruction \p Insn is
66 /// considered special and needs to be tracked. Implementing this method in
67 /// children classes allows to implement tracking of implicit control flow,
68 /// memory writing instructions or any other kinds of instructions we might
69 /// be interested in.
70 virtual bool isSpecialInstruction(const Instruction *Insn) const = 0;
71
72 virtual ~InstructionPrecedenceTracking() = default;
73
74public:
75 /// Notifies this tracking that we are going to insert a new instruction \p
76 /// Inst to the basic block \p BB. It makes all necessary updates to internal
77 /// caches to keep them consistent.
79 const BasicBlock *BB);
80
81 /// Notifies this tracking that we are going to remove the instruction \p Inst
82 /// It makes all necessary updates to internal caches to keep them consistent.
83 LLVM_ABI void removeInstruction(const Instruction *Inst);
84
85 /// Notifies this tracking that we are going to replace all uses of \p Inst.
86 /// It makes all necessary updates to internal caches to keep them consistent.
87 /// Should typically be called before a RAUW.
88 LLVM_ABI void removeUsersOf(const Instruction *Inst);
89
90 /// Invalidates all information from this tracking.
91 LLVM_ABI void clear();
92};
93
94/// This class allows to keep track on instructions with implicit control flow.
95/// These are instructions that may not pass execution to their successors. For
96/// example, throwing calls and guards do not always do this. If we need to know
97/// for sure that some instruction is guaranteed to execute if the given block
98/// is reached, then we need to make sure that there is no implicit control flow
99/// instruction (ICFI) preceding it. For example, this check is required if we
100/// perform PRE moving non-speculable instruction to other place.
103public:
104 /// Returns the topmost instruction with implicit control flow from the given
105 /// basic block. Returns nullptr if there is no such instructions in the block.
108 }
109
110 /// Returns true if at least one instruction from the given basic block has
111 /// implicit control flow.
112 bool hasICF(const BasicBlock *BB) {
113 return hasSpecialInstructions(BB);
114 }
115
116 /// Returns true if the first ICFI of Insn's block exists and dominates Insn.
120
121 bool isSpecialInstruction(const Instruction *Insn) const override;
122};
123
125public:
126 /// Returns the topmost instruction that may write memory from the given
127 /// basic block. Returns nullptr if there is no such instructions in the block.
130 }
131
132 /// Returns true if at least one instruction from the given basic block may
133 /// write memory.
134 bool mayWriteToMemory(const BasicBlock *BB) {
135 return hasSpecialInstructions(BB);
136 }
137
138 /// Returns true if the first memory writing instruction of Insn's block
139 /// exists and dominates Insn.
143
144 bool isSpecialInstruction(const Instruction *Insn) const override;
145};
146
147} // llvm
148
149#endif // LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
This class allows to keep track on instructions with implicit control flow.
const Instruction * getFirstICFI(const BasicBlock *BB)
Returns the topmost instruction with implicit control flow from the given basic block.
bool isDominatedByICFIFromSameBlock(const Instruction *Insn)
Returns true if the first ICFI of Insn's block exists and dominates Insn.
bool hasICF(const BasicBlock *BB)
Returns true if at least one instruction from the given basic block has implicit control flow.
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.
virtual ~InstructionPrecedenceTracking()=default
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.
bool mayWriteToMemory(const BasicBlock *BB)
Returns true if at least one instruction from the given basic block may write memory.
bool isDominatedByMemoryWriteFromSameBlock(const Instruction *Insn)
Returns true if the first memory writing instruction of Insn's block exists and dominates Insn.
const Instruction * getFirstMemoryWrite(const BasicBlock *BB)
Returns the topmost instruction that may write memory from the given basic block.
This is an optimization pass for GlobalISel generic memory operations.