MachineJumpTableInfo.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- CodeGen/MachineJumpTableInfo.h - Abstract Jump Tables --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // The MachineJumpTableInfo class keeps track of jump tables referenced by
  15. // lowered switch instructions in the MachineFunction.
  16. //
  17. // Instructions reference the address of these jump tables through the use of
  18. // MO_JumpTableIndex values. When emitting assembly or machine code, these
  19. // virtual address references are converted to refer to the address of the
  20. // function jump tables.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
  24. #define LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
  25. #include "llvm/Support/Printable.h"
  26. #include <cassert>
  27. #include <vector>
  28. namespace llvm {
  29. class MachineBasicBlock;
  30. class DataLayout;
  31. class raw_ostream;
  32. /// MachineJumpTableEntry - One jump table in the jump table info.
  33. ///
  34. struct MachineJumpTableEntry {
  35. /// MBBs - The vector of basic blocks from which to create the jump table.
  36. std::vector<MachineBasicBlock*> MBBs;
  37. explicit MachineJumpTableEntry(const std::vector<MachineBasicBlock*> &M)
  38. : MBBs(M) {}
  39. };
  40. class MachineJumpTableInfo {
  41. public:
  42. /// JTEntryKind - This enum indicates how each entry of the jump table is
  43. /// represented and emitted.
  44. enum JTEntryKind {
  45. /// EK_BlockAddress - Each entry is a plain address of block, e.g.:
  46. /// .word LBB123
  47. EK_BlockAddress,
  48. /// EK_GPRel64BlockAddress - Each entry is an address of block, encoded
  49. /// with a relocation as gp-relative, e.g.:
  50. /// .gpdword LBB123
  51. EK_GPRel64BlockAddress,
  52. /// EK_GPRel32BlockAddress - Each entry is an address of block, encoded
  53. /// with a relocation as gp-relative, e.g.:
  54. /// .gprel32 LBB123
  55. EK_GPRel32BlockAddress,
  56. /// EK_LabelDifference32 - Each entry is the address of the block minus
  57. /// the address of the jump table. This is used for PIC jump tables where
  58. /// gprel32 is not supported. e.g.:
  59. /// .word LBB123 - LJTI1_2
  60. /// If the .set directive is supported, this is emitted as:
  61. /// .set L4_5_set_123, LBB123 - LJTI1_2
  62. /// .word L4_5_set_123
  63. EK_LabelDifference32,
  64. /// EK_Inline - Jump table entries are emitted inline at their point of
  65. /// use. It is the responsibility of the target to emit the entries.
  66. EK_Inline,
  67. /// EK_Custom32 - Each entry is a 32-bit value that is custom lowered by the
  68. /// TargetLowering::LowerCustomJumpTableEntry hook.
  69. EK_Custom32
  70. };
  71. private:
  72. JTEntryKind EntryKind;
  73. std::vector<MachineJumpTableEntry> JumpTables;
  74. public:
  75. explicit MachineJumpTableInfo(JTEntryKind Kind): EntryKind(Kind) {}
  76. JTEntryKind getEntryKind() const { return EntryKind; }
  77. /// getEntrySize - Return the size of each entry in the jump table.
  78. unsigned getEntrySize(const DataLayout &TD) const;
  79. /// getEntryAlignment - Return the alignment of each entry in the jump table.
  80. unsigned getEntryAlignment(const DataLayout &TD) const;
  81. /// createJumpTableIndex - Create a new jump table.
  82. ///
  83. unsigned createJumpTableIndex(const std::vector<MachineBasicBlock*> &DestBBs);
  84. /// isEmpty - Return true if there are no jump tables.
  85. ///
  86. bool isEmpty() const { return JumpTables.empty(); }
  87. const std::vector<MachineJumpTableEntry> &getJumpTables() const {
  88. return JumpTables;
  89. }
  90. /// RemoveJumpTable - Mark the specific index as being dead. This will
  91. /// prevent it from being emitted.
  92. void RemoveJumpTable(unsigned Idx) {
  93. JumpTables[Idx].MBBs.clear();
  94. }
  95. /// RemoveMBBFromJumpTables - If MBB is present in any jump tables, remove it.
  96. bool RemoveMBBFromJumpTables(MachineBasicBlock *MBB);
  97. /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
  98. /// the jump tables to branch to New instead.
  99. bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New);
  100. /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
  101. /// the jump table to branch to New instead.
  102. bool ReplaceMBBInJumpTable(unsigned Idx, MachineBasicBlock *Old,
  103. MachineBasicBlock *New);
  104. /// print - Used by the MachineFunction printer to print information about
  105. /// jump tables. Implemented in MachineFunction.cpp
  106. ///
  107. void print(raw_ostream &OS) const;
  108. /// dump - Call to stderr.
  109. ///
  110. void dump() const;
  111. };
  112. /// Prints a jump table entry reference.
  113. ///
  114. /// The format is:
  115. /// %jump-table.5 - a jump table entry with index == 5.
  116. ///
  117. /// Usage: OS << printJumpTableEntryReference(Idx) << '\n';
  118. Printable printJumpTableEntryReference(unsigned Idx);
  119. } // End llvm namespace
  120. #endif
  121. #ifdef __GNUC__
  122. #pragma GCC diagnostic pop
  123. #endif