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" 24 #include "llvm/Support/Compiler.h" 25 26 namespace llvm { 27 28 class BasicBlock; 29 class Instruction; 30 31 class InstructionPrecedenceTracking { 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. 35 DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts; 36 37 #ifndef NDEBUG 38 /// Asserts that the cached info for \p BB is up-to-date. This helps to catch 39 /// the usage error of accessing a block without properly invalidating after a 40 /// previous transform. 41 void validate(const BasicBlock *BB) const; 42 43 /// Asserts whether or not the contents of this tracking is up-to-date. This 44 /// helps to catch the usage error of accessing a block without properly 45 /// invalidating after a previous transform. 46 void validateAll() const; 47 #endif 48 49 protected: 50 /// Returns the topmost special instruction from the block \p BB. Returns 51 /// nullptr if there is no special instructions in the block. 52 LLVM_ABI const Instruction *getFirstSpecialInstruction(const BasicBlock *BB); 53 54 /// Returns true iff at least one instruction from the basic block \p BB is 55 /// special. 56 LLVM_ABI bool hasSpecialInstructions(const BasicBlock *BB); 57 58 /// Returns true iff the first special instruction of \p Insn's block exists 59 /// and dominates \p Insn. 60 LLVM_ABI bool isPreceededBySpecialInstruction(const Instruction *Insn); 61 62 /// A predicate that defines whether or not the instruction \p Insn is 63 /// considered special and needs to be tracked. Implementing this method in 64 /// children classes allows to implement tracking of implicit control flow, 65 /// memory writing instructions or any other kinds of instructions we might 66 /// be interested in. 67 virtual bool isSpecialInstruction(const Instruction *Insn) const = 0; 68 69 virtual ~InstructionPrecedenceTracking() = default; 70 71 public: 72 /// Notifies this tracking that we are going to insert a new instruction \p 73 /// Inst to the basic block \p BB. It makes all necessary updates to internal 74 /// caches to keep them consistent. 75 LLVM_ABI void insertInstructionTo(const Instruction *Inst, 76 const BasicBlock *BB); 77 78 /// Notifies this tracking that we are going to remove the instruction \p Inst 79 /// It makes all necessary updates to internal caches to keep them consistent. 80 LLVM_ABI void removeInstruction(const Instruction *Inst); 81 82 /// Notifies this tracking that we are going to replace all uses of \p Inst. 83 /// It makes all necessary updates to internal caches to keep them consistent. 84 /// Should typically be called before a RAUW. 85 LLVM_ABI void removeUsersOf(const Instruction *Inst); 86 87 /// Invalidates all information from this tracking. 88 LLVM_ABI void clear(); 89 }; 90 91 /// This class allows to keep track on instructions with implicit control flow. 92 /// These are instructions that may not pass execution to their successors. For 93 /// example, throwing calls and guards do not always do this. If we need to know 94 /// for sure that some instruction is guaranteed to execute if the given block 95 /// is reached, then we need to make sure that there is no implicit control flow 96 /// instruction (ICFI) preceding it. For example, this check is required if we 97 /// perform PRE moving non-speculable instruction to other place. 98 class LLVM_ABI ImplicitControlFlowTracking 99 : public InstructionPrecedenceTracking { 100 public: 101 /// Returns the topmost instruction with implicit control flow from the given 102 /// basic block. Returns nullptr if there is no such instructions in the block. getFirstICFI(const BasicBlock * BB)103 const Instruction *getFirstICFI(const BasicBlock *BB) { 104 return getFirstSpecialInstruction(BB); 105 } 106 107 /// Returns true if at least one instruction from the given basic block has 108 /// implicit control flow. hasICF(const BasicBlock * BB)109 bool hasICF(const BasicBlock *BB) { 110 return hasSpecialInstructions(BB); 111 } 112 113 /// Returns true if the first ICFI of Insn's block exists and dominates Insn. isDominatedByICFIFromSameBlock(const Instruction * Insn)114 bool isDominatedByICFIFromSameBlock(const Instruction *Insn) { 115 return isPreceededBySpecialInstruction(Insn); 116 } 117 118 bool isSpecialInstruction(const Instruction *Insn) const override; 119 }; 120 121 class LLVM_ABI MemoryWriteTracking : public InstructionPrecedenceTracking { 122 public: 123 /// Returns the topmost instruction that may write memory from the given 124 /// basic block. Returns nullptr if there is no such instructions in the block. getFirstMemoryWrite(const BasicBlock * BB)125 const Instruction *getFirstMemoryWrite(const BasicBlock *BB) { 126 return getFirstSpecialInstruction(BB); 127 } 128 129 /// Returns true if at least one instruction from the given basic block may 130 /// write memory. mayWriteToMemory(const BasicBlock * BB)131 bool mayWriteToMemory(const BasicBlock *BB) { 132 return hasSpecialInstructions(BB); 133 } 134 135 /// Returns true if the first memory writing instruction of Insn's block 136 /// exists and dominates Insn. isDominatedByMemoryWriteFromSameBlock(const Instruction * Insn)137 bool isDominatedByMemoryWriteFromSameBlock(const Instruction *Insn) { 138 return isPreceededBySpecialInstruction(Insn); 139 } 140 141 bool isSpecialInstruction(const Instruction *Insn) const override; 142 }; 143 144 } // llvm 145 146 #endif // LLVM_ANALYSIS_INSTRUCTIONPRECEDENCETRACKING_H 147