Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:31

0001 //===-- CodeGen/MachineJumpTableInfo.h - Abstract Jump Tables  --*- 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 //
0009 // The MachineJumpTableInfo class keeps track of jump tables referenced by
0010 // lowered switch instructions in the MachineFunction.
0011 //
0012 // Instructions reference the address of these jump tables through the use of
0013 // MO_JumpTableIndex values.  When emitting assembly or machine code, these
0014 // virtual address references are converted to refer to the address of the
0015 // function jump tables.
0016 //
0017 //===----------------------------------------------------------------------===//
0018 
0019 #ifndef LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
0020 #define LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
0021 
0022 #include "llvm/Support/Printable.h"
0023 #include <cassert>
0024 #include <vector>
0025 
0026 namespace llvm {
0027 
0028 class MachineBasicBlock;
0029 class DataLayout;
0030 class raw_ostream;
0031 enum class MachineFunctionDataHotness;
0032 
0033 /// MachineJumpTableEntry - One jump table in the jump table info.
0034 ///
0035 struct MachineJumpTableEntry {
0036   /// MBBs - The vector of basic blocks from which to create the jump table.
0037   std::vector<MachineBasicBlock*> MBBs;
0038 
0039   /// The hotness of MJTE is inferred from the hotness of the source basic
0040   /// block(s) that reference it.
0041   MachineFunctionDataHotness Hotness;
0042 
0043   explicit MachineJumpTableEntry(const std::vector<MachineBasicBlock *> &M);
0044 };
0045 
0046 class MachineJumpTableInfo {
0047 public:
0048   /// JTEntryKind - This enum indicates how each entry of the jump table is
0049   /// represented and emitted.
0050   enum JTEntryKind {
0051     /// EK_BlockAddress - Each entry is a plain address of block, e.g.:
0052     ///     .word LBB123
0053     EK_BlockAddress,
0054 
0055     /// EK_GPRel64BlockAddress - Each entry is an address of block, encoded
0056     /// with a relocation as gp-relative, e.g.:
0057     ///     .gpdword LBB123
0058     EK_GPRel64BlockAddress,
0059 
0060     /// EK_GPRel32BlockAddress - Each entry is an address of block, encoded
0061     /// with a relocation as gp-relative, e.g.:
0062     ///     .gprel32 LBB123
0063     EK_GPRel32BlockAddress,
0064 
0065     /// EK_LabelDifference32 - Each entry is the address of the block minus
0066     /// the address of the jump table.  This is used for PIC jump tables where
0067     /// gprel32 is not supported.  e.g.:
0068     ///      .word LBB123 - LJTI1_2
0069     /// If the .set directive is supported, this is emitted as:
0070     ///      .set L4_5_set_123, LBB123 - LJTI1_2
0071     ///      .word L4_5_set_123
0072     EK_LabelDifference32,
0073 
0074     /// EK_LabelDifference64 - Each entry is the address of the block minus
0075     /// the address of the jump table.  This is used for PIC jump tables where
0076     /// gprel64 is not supported.  e.g.:
0077     ///      .quad LBB123 - LJTI1_2
0078     EK_LabelDifference64,
0079 
0080     /// EK_Inline - Jump table entries are emitted inline at their point of
0081     /// use. It is the responsibility of the target to emit the entries.
0082     EK_Inline,
0083 
0084     /// EK_Custom32 - Each entry is a 32-bit value that is custom lowered by the
0085     /// TargetLowering::LowerCustomJumpTableEntry hook.
0086     EK_Custom32
0087   };
0088 
0089 private:
0090   JTEntryKind EntryKind;
0091   std::vector<MachineJumpTableEntry> JumpTables;
0092 public:
0093   explicit MachineJumpTableInfo(JTEntryKind Kind): EntryKind(Kind) {}
0094 
0095   JTEntryKind getEntryKind() const { return EntryKind; }
0096 
0097   /// getEntrySize - Return the size of each entry in the jump table.
0098   unsigned getEntrySize(const DataLayout &TD) const;
0099   /// getEntryAlignment - Return the alignment of each entry in the jump table.
0100   unsigned getEntryAlignment(const DataLayout &TD) const;
0101 
0102   /// createJumpTableIndex - Create a new jump table.
0103   ///
0104   unsigned createJumpTableIndex(const std::vector<MachineBasicBlock*> &DestBBs);
0105 
0106   /// isEmpty - Return true if there are no jump tables.
0107   ///
0108   bool isEmpty() const { return JumpTables.empty(); }
0109 
0110   const std::vector<MachineJumpTableEntry> &getJumpTables() const {
0111     return JumpTables;
0112   }
0113 
0114   // Update machine jump table entry's hotness. Return true if the hotness is
0115   // updated.
0116   bool updateJumpTableEntryHotness(size_t JTI,
0117                                    MachineFunctionDataHotness Hotness);
0118 
0119   /// RemoveJumpTable - Mark the specific index as being dead.  This will
0120   /// prevent it from being emitted.
0121   void RemoveJumpTable(unsigned Idx) {
0122     JumpTables[Idx].MBBs.clear();
0123   }
0124 
0125   /// RemoveMBBFromJumpTables - If MBB is present in any jump tables, remove it.
0126   bool RemoveMBBFromJumpTables(MachineBasicBlock *MBB);
0127 
0128   /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
0129   /// the jump tables to branch to New instead.
0130   bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New);
0131 
0132   /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
0133   /// the jump table to branch to New instead.
0134   bool ReplaceMBBInJumpTable(unsigned Idx, MachineBasicBlock *Old,
0135                              MachineBasicBlock *New);
0136 
0137   /// print - Used by the MachineFunction printer to print information about
0138   /// jump tables.  Implemented in MachineFunction.cpp
0139   ///
0140   void print(raw_ostream &OS) const;
0141 
0142   /// dump - Call to stderr.
0143   ///
0144   void dump() const;
0145 };
0146 
0147 
0148 /// Prints a jump table entry reference.
0149 ///
0150 /// The format is:
0151 ///   %jump-table.5       - a jump table entry with index == 5.
0152 ///
0153 /// Usage: OS << printJumpTableEntryReference(Idx) << '\n';
0154 Printable printJumpTableEntryReference(unsigned Idx);
0155 
0156 } // End llvm namespace
0157 
0158 #endif