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