1193323Sed//===-- CodeGen/MachineJumpTableInfo.h - Abstract Jump Tables  --*- C++ -*-===//
2193323Sed//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6193323Sed//
7193323Sed//===----------------------------------------------------------------------===//
8193323Sed//
9193323Sed// The MachineJumpTableInfo class keeps track of jump tables referenced by
10193323Sed// lowered switch instructions in the MachineFunction.
11193323Sed//
12239462Sdim// Instructions reference the address of these jump tables through the use of
13239462Sdim// MO_JumpTableIndex values.  When emitting assembly or machine code, these
14239462Sdim// virtual address references are converted to refer to the address of the
15193323Sed// function jump tables.
16193323Sed//
17193323Sed//===----------------------------------------------------------------------===//
18193323Sed
19193323Sed#ifndef LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
20193323Sed#define LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
21193323Sed
22327952Sdim#include "llvm/Support/Printable.h"
23249423Sdim#include <cassert>
24193323Sed#include <vector>
25193323Sed
26193323Sednamespace llvm {
27193323Sed
28193323Sedclass MachineBasicBlock;
29243830Sdimclass DataLayout;
30198090Srdivackyclass raw_ostream;
31193323Sed
32193323Sed/// MachineJumpTableEntry - One jump table in the jump table info.
33193323Sed///
34193323Sedstruct MachineJumpTableEntry {
35193323Sed  /// MBBs - The vector of basic blocks from which to create the jump table.
36193323Sed  std::vector<MachineBasicBlock*> MBBs;
37239462Sdim
38193323Sed  explicit MachineJumpTableEntry(const std::vector<MachineBasicBlock*> &M)
39193323Sed  : MBBs(M) {}
40193323Sed};
41239462Sdim
42193323Sedclass MachineJumpTableInfo {
43203954Srdivackypublic:
44203954Srdivacky  /// JTEntryKind - This enum indicates how each entry of the jump table is
45203954Srdivacky  /// represented and emitted.
46203954Srdivacky  enum JTEntryKind {
47203954Srdivacky    /// EK_BlockAddress - Each entry is a plain address of block, e.g.:
48203954Srdivacky    ///     .word LBB123
49203954Srdivacky    EK_BlockAddress,
50234353Sdim
51234353Sdim    /// EK_GPRel64BlockAddress - Each entry is an address of block, encoded
52234353Sdim    /// with a relocation as gp-relative, e.g.:
53234353Sdim    ///     .gpdword LBB123
54234353Sdim    EK_GPRel64BlockAddress,
55234353Sdim
56203954Srdivacky    /// EK_GPRel32BlockAddress - Each entry is an address of block, encoded
57203954Srdivacky    /// with a relocation as gp-relative, e.g.:
58203954Srdivacky    ///     .gprel32 LBB123
59203954Srdivacky    EK_GPRel32BlockAddress,
60239462Sdim
61203954Srdivacky    /// EK_LabelDifference32 - Each entry is the address of the block minus
62203954Srdivacky    /// the address of the jump table.  This is used for PIC jump tables where
63203954Srdivacky    /// gprel32 is not supported.  e.g.:
64203954Srdivacky    ///      .word LBB123 - LJTI1_2
65203954Srdivacky    /// If the .set directive is supported, this is emitted as:
66203954Srdivacky    ///      .set L4_5_set_123, LBB123 - LJTI1_2
67203954Srdivacky    ///      .word L4_5_set_123
68203954Srdivacky    EK_LabelDifference32,
69205218Srdivacky
70205218Srdivacky    /// EK_Inline - Jump table entries are emitted inline at their point of
71205218Srdivacky    /// use. It is the responsibility of the target to emit the entries.
72205218Srdivacky    EK_Inline,
73205218Srdivacky
74203954Srdivacky    /// EK_Custom32 - Each entry is a 32-bit value that is custom lowered by the
75203954Srdivacky    /// TargetLowering::LowerCustomJumpTableEntry hook.
76203954Srdivacky    EK_Custom32
77203954Srdivacky  };
78203954Srdivackyprivate:
79203954Srdivacky  JTEntryKind EntryKind;
80193323Sed  std::vector<MachineJumpTableEntry> JumpTables;
81193323Sedpublic:
82210299Sed  explicit MachineJumpTableInfo(JTEntryKind Kind): EntryKind(Kind) {}
83239462Sdim
84203954Srdivacky  JTEntryKind getEntryKind() const { return EntryKind; }
85203954Srdivacky
86203954Srdivacky  /// getEntrySize - Return the size of each entry in the jump table.
87243830Sdim  unsigned getEntrySize(const DataLayout &TD) const;
88203954Srdivacky  /// getEntryAlignment - Return the alignment of each entry in the jump table.
89243830Sdim  unsigned getEntryAlignment(const DataLayout &TD) const;
90239462Sdim
91205407Srdivacky  /// createJumpTableIndex - Create a new jump table.
92193323Sed  ///
93205407Srdivacky  unsigned createJumpTableIndex(const std::vector<MachineBasicBlock*> &DestBBs);
94239462Sdim
95193323Sed  /// isEmpty - Return true if there are no jump tables.
96193323Sed  ///
97193323Sed  bool isEmpty() const { return JumpTables.empty(); }
98193323Sed
99193323Sed  const std::vector<MachineJumpTableEntry> &getJumpTables() const {
100193323Sed    return JumpTables;
101193323Sed  }
102203954Srdivacky
103203954Srdivacky  /// RemoveJumpTable - Mark the specific index as being dead.  This will
104203954Srdivacky  /// prevent it from being emitted.
105193323Sed  void RemoveJumpTable(unsigned Idx) {
106193323Sed    JumpTables[Idx].MBBs.clear();
107193323Sed  }
108239462Sdim
109193323Sed  /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
110193323Sed  /// the jump tables to branch to New instead.
111193323Sed  bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New);
112193323Sed
113199481Srdivacky  /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
114199481Srdivacky  /// the jump table to branch to New instead.
115199481Srdivacky  bool ReplaceMBBInJumpTable(unsigned Idx, MachineBasicBlock *Old,
116199481Srdivacky                             MachineBasicBlock *New);
117199481Srdivacky
118193323Sed  /// print - Used by the MachineFunction printer to print information about
119193323Sed  /// jump tables.  Implemented in MachineFunction.cpp
120193323Sed  ///
121198090Srdivacky  void print(raw_ostream &OS) const;
122193323Sed
123198090Srdivacky  /// dump - Call to stderr.
124193323Sed  ///
125193323Sed  void dump() const;
126193323Sed};
127193323Sed
128327952Sdim
129327952Sdim/// Prints a jump table entry reference.
130327952Sdim///
131327952Sdim/// The format is:
132327952Sdim///   %jump-table.5       - a jump table entry with index == 5.
133327952Sdim///
134327952Sdim/// Usage: OS << printJumpTableEntryReference(Idx) << '\n';
135327952SdimPrintable printJumpTableEntryReference(unsigned Idx);
136327952Sdim
137193323Sed} // End llvm namespace
138193323Sed
139193323Sed#endif
140