|
|
|||
File indexing completed on 2026-05-10 08:43:13
0001 //===-- InstructionPrecedenceTracking.h -------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // Implements a class that is able to define some instructions as "special" 0009 // (e.g. as having implicit control flow, or writing memory, or having another 0010 // interesting property) and then efficiently answers queries of the types: 0011 // 1. Are there any special instructions in the block of interest? 0012 // 2. Return first of the special instructions in the given block; 0013 // 3. Check if the given instruction is preceeded by the first special 0014 // instruction in the same block. 0015 // The class provides caching that allows to answer these queries quickly. The 0016 // user must make sure that the cached data is invalidated properly whenever 0017 // a content of some tracked block is changed. 0018 //===----------------------------------------------------------------------===// 0019 0020 #ifndef LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H 0021 #define LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H 0022 0023 #include "llvm/ADT/DenseMap.h" 0024 0025 namespace llvm { 0026 0027 class BasicBlock; 0028 class Instruction; 0029 0030 class InstructionPrecedenceTracking { 0031 // Maps a block to the topmost special instruction in it. If the value is 0032 // nullptr, it means that it is known that this block does not contain any 0033 // special instructions. 0034 DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts; 0035 0036 // Fills information about the given block's special instructions. 0037 void fill(const BasicBlock *BB); 0038 0039 #ifndef NDEBUG 0040 /// Asserts that the cached info for \p BB is up-to-date. This helps to catch 0041 /// the usage error of accessing a block without properly invalidating after a 0042 /// previous transform. 0043 void validate(const BasicBlock *BB) const; 0044 0045 /// Asserts whether or not the contents of this tracking is up-to-date. This 0046 /// helps to catch the usage error of accessing a block without properly 0047 /// invalidating after a previous transform. 0048 void validateAll() const; 0049 #endif 0050 0051 protected: 0052 /// Returns the topmost special instruction from the block \p BB. Returns 0053 /// nullptr if there is no special instructions in the block. 0054 const Instruction *getFirstSpecialInstruction(const BasicBlock *BB); 0055 0056 /// Returns true iff at least one instruction from the basic block \p BB is 0057 /// special. 0058 bool hasSpecialInstructions(const BasicBlock *BB); 0059 0060 /// Returns true iff the first special instruction of \p Insn's block exists 0061 /// and dominates \p Insn. 0062 bool isPreceededBySpecialInstruction(const Instruction *Insn); 0063 0064 /// A predicate that defines whether or not the instruction \p Insn is 0065 /// considered special and needs to be tracked. Implementing this method in 0066 /// children classes allows to implement tracking of implicit control flow, 0067 /// memory writing instructions or any other kinds of instructions we might 0068 /// be interested in. 0069 virtual bool isSpecialInstruction(const Instruction *Insn) const = 0; 0070 0071 virtual ~InstructionPrecedenceTracking() = default; 0072 0073 public: 0074 /// Notifies this tracking that we are going to insert a new instruction \p 0075 /// Inst to the basic block \p BB. It makes all necessary updates to internal 0076 /// caches to keep them consistent. 0077 void insertInstructionTo(const Instruction *Inst, const BasicBlock *BB); 0078 0079 /// Notifies this tracking that we are going to remove the instruction \p Inst 0080 /// It makes all necessary updates to internal caches to keep them consistent. 0081 void removeInstruction(const Instruction *Inst); 0082 0083 /// Notifies this tracking that we are going to replace all uses of \p Inst. 0084 /// It makes all necessary updates to internal caches to keep them consistent. 0085 /// Should typically be called before a RAUW. 0086 void removeUsersOf(const Instruction *Inst); 0087 0088 /// Invalidates all information from this tracking. 0089 void clear(); 0090 }; 0091 0092 /// This class allows to keep track on instructions with implicit control flow. 0093 /// These are instructions that may not pass execution to their successors. For 0094 /// example, throwing calls and guards do not always do this. If we need to know 0095 /// for sure that some instruction is guaranteed to execute if the given block 0096 /// is reached, then we need to make sure that there is no implicit control flow 0097 /// instruction (ICFI) preceding it. For example, this check is required if we 0098 /// perform PRE moving non-speculable instruction to other place. 0099 class ImplicitControlFlowTracking : public InstructionPrecedenceTracking { 0100 public: 0101 /// Returns the topmost instruction with implicit control flow from the given 0102 /// basic block. Returns nullptr if there is no such instructions in the block. 0103 const Instruction *getFirstICFI(const BasicBlock *BB) { 0104 return getFirstSpecialInstruction(BB); 0105 } 0106 0107 /// Returns true if at least one instruction from the given basic block has 0108 /// implicit control flow. 0109 bool hasICF(const BasicBlock *BB) { 0110 return hasSpecialInstructions(BB); 0111 } 0112 0113 /// Returns true if the first ICFI of Insn's block exists and dominates Insn. 0114 bool isDominatedByICFIFromSameBlock(const Instruction *Insn) { 0115 return isPreceededBySpecialInstruction(Insn); 0116 } 0117 0118 bool isSpecialInstruction(const Instruction *Insn) const override; 0119 }; 0120 0121 class MemoryWriteTracking : public InstructionPrecedenceTracking { 0122 public: 0123 /// Returns the topmost instruction that may write memory from the given 0124 /// basic block. Returns nullptr if there is no such instructions in the block. 0125 const Instruction *getFirstMemoryWrite(const BasicBlock *BB) { 0126 return getFirstSpecialInstruction(BB); 0127 } 0128 0129 /// Returns true if at least one instruction from the given basic block may 0130 /// write memory. 0131 bool mayWriteToMemory(const BasicBlock *BB) { 0132 return hasSpecialInstructions(BB); 0133 } 0134 0135 /// Returns true if the first memory writing instruction of Insn's block 0136 /// exists and dominates Insn. 0137 bool isDominatedByMemoryWriteFromSameBlock(const Instruction *Insn) { 0138 return isPreceededBySpecialInstruction(Insn); 0139 } 0140 0141 bool isSpecialInstruction(const Instruction *Insn) const override; 0142 }; 0143 0144 } // llvm 0145 0146 #endif // LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|