1 //===-- CodeGen/MachineJumpTableInfo.h - Abstract Jump Tables --*- 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 // 9 // The MachineJumpTableInfo class keeps track of jump tables referenced by 10 // lowered switch instructions in the MachineFunction. 11 // 12 // Instructions reference the address of these jump tables through the use of 13 // MO_JumpTableIndex values. When emitting assembly or machine code, these 14 // virtual address references are converted to refer to the address of the 15 // function jump tables. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #ifndef LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H 20 #define LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H 21 22 #include "llvm/Support/Compiler.h" 23 #include "llvm/Support/Printable.h" 24 #include <cassert> 25 #include <vector> 26 27 namespace llvm { 28 29 class MachineBasicBlock; 30 class DataLayout; 31 class raw_ostream; 32 enum class MachineFunctionDataHotness; 33 34 /// MachineJumpTableEntry - One jump table in the jump table info. 35 /// 36 struct MachineJumpTableEntry { 37 /// MBBs - The vector of basic blocks from which to create the jump table. 38 std::vector<MachineBasicBlock*> MBBs; 39 40 /// The hotness of MJTE is inferred from the hotness of the source basic 41 /// block(s) that reference it. 42 MachineFunctionDataHotness Hotness; 43 44 LLVM_ABI explicit MachineJumpTableEntry( 45 const std::vector<MachineBasicBlock *> &M); 46 }; 47 48 class MachineJumpTableInfo { 49 public: 50 /// JTEntryKind - This enum indicates how each entry of the jump table is 51 /// represented and emitted. 52 enum JTEntryKind { 53 /// EK_BlockAddress - Each entry is a plain address of block, e.g.: 54 /// .word LBB123 55 EK_BlockAddress, 56 57 /// EK_GPRel64BlockAddress - Each entry is an address of block, encoded 58 /// with a relocation as gp-relative, e.g.: 59 /// .gpdword LBB123 60 EK_GPRel64BlockAddress, 61 62 /// EK_GPRel32BlockAddress - Each entry is an address of block, encoded 63 /// with a relocation as gp-relative, e.g.: 64 /// .gprel32 LBB123 65 EK_GPRel32BlockAddress, 66 67 /// EK_LabelDifference32 - Each entry is the address of the block minus 68 /// the address of the jump table. This is used for PIC jump tables where 69 /// gprel32 is not supported. e.g.: 70 /// .word LBB123 - LJTI1_2 71 /// If the .set directive is supported, this is emitted as: 72 /// .set L4_5_set_123, LBB123 - LJTI1_2 73 /// .word L4_5_set_123 74 EK_LabelDifference32, 75 76 /// EK_LabelDifference64 - Each entry is the address of the block minus 77 /// the address of the jump table. This is used for PIC jump tables where 78 /// gprel64 is not supported. e.g.: 79 /// .quad LBB123 - LJTI1_2 80 EK_LabelDifference64, 81 82 /// EK_Inline - Jump table entries are emitted inline at their point of 83 /// use. It is the responsibility of the target to emit the entries. 84 EK_Inline, 85 86 /// EK_Custom32 - Each entry is a 32-bit value that is custom lowered by the 87 /// TargetLowering::LowerCustomJumpTableEntry hook. 88 EK_Custom32 89 }; 90 91 private: 92 JTEntryKind EntryKind; 93 std::vector<MachineJumpTableEntry> JumpTables; 94 public: MachineJumpTableInfo(JTEntryKind Kind)95 explicit MachineJumpTableInfo(JTEntryKind Kind): EntryKind(Kind) {} 96 getEntryKind()97 JTEntryKind getEntryKind() const { return EntryKind; } 98 99 /// getEntrySize - Return the size of each entry in the jump table. 100 LLVM_ABI unsigned getEntrySize(const DataLayout &TD) const; 101 /// getEntryAlignment - Return the alignment of each entry in the jump table. 102 LLVM_ABI unsigned getEntryAlignment(const DataLayout &TD) const; 103 104 /// createJumpTableIndex - Create a new jump table. 105 /// 106 LLVM_ABI unsigned 107 createJumpTableIndex(const std::vector<MachineBasicBlock *> &DestBBs); 108 109 /// isEmpty - Return true if there are no jump tables. 110 /// isEmpty()111 bool isEmpty() const { return JumpTables.empty(); } 112 getJumpTables()113 const std::vector<MachineJumpTableEntry> &getJumpTables() const { 114 return JumpTables; 115 } 116 117 // Update machine jump table entry's hotness. Return true if the hotness is 118 // updated. 119 LLVM_ABI bool updateJumpTableEntryHotness(size_t JTI, 120 MachineFunctionDataHotness Hotness); 121 122 /// RemoveJumpTable - Mark the specific index as being dead. This will 123 /// prevent it from being emitted. RemoveJumpTable(unsigned Idx)124 void RemoveJumpTable(unsigned Idx) { 125 JumpTables[Idx].MBBs.clear(); 126 } 127 128 /// RemoveMBBFromJumpTables - If MBB is present in any jump tables, remove it. 129 LLVM_ABI bool RemoveMBBFromJumpTables(MachineBasicBlock *MBB); 130 131 /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update 132 /// the jump tables to branch to New instead. 133 LLVM_ABI bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, 134 MachineBasicBlock *New); 135 136 /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update 137 /// the jump table to branch to New instead. 138 LLVM_ABI bool ReplaceMBBInJumpTable(unsigned Idx, MachineBasicBlock *Old, 139 MachineBasicBlock *New); 140 141 /// print - Used by the MachineFunction printer to print information about 142 /// jump tables. Implemented in MachineFunction.cpp 143 /// 144 LLVM_ABI void print(raw_ostream &OS) const; 145 146 /// dump - Call to stderr. 147 /// 148 LLVM_ABI void dump() const; 149 }; 150 151 152 /// Prints a jump table entry reference. 153 /// 154 /// The format is: 155 /// %jump-table.5 - a jump table entry with index == 5. 156 /// 157 /// Usage: OS << printJumpTableEntryReference(Idx) << '\n'; 158 LLVM_ABI Printable printJumpTableEntryReference(unsigned Idx); 159 160 } // End llvm namespace 161 162 #endif 163