1. Tidy up jump table info.
[oota-llvm.git] / include / llvm / CodeGen / MachineJumpTableInfo.h
1 //===-- CodeGen/MachineJumpTableInfo.h - Abstract Jump Tables  --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Nate Begeman and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The MachineJumpTableInfo class keeps track of jump tables referenced by
11 // lowered switch instructions in the MachineFunction.
12 //
13 // Instructions reference the address of these jump tables through the use of 
14 // MO_JumpTableIndex values.  When emitting assembly or machine code, these 
15 // virtual address references are converted to refer to the address of the 
16 // function jump tables.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
21 #define LLVM_CODEGEN_MACHINEJUMPTABLEINFO_H
22
23 #include <vector>
24 #include <iosfwd>
25
26 namespace llvm {
27
28 class MachineBasicBlock;
29 class TargetData;
30
31 /// MachineJumpTableEntry - One jump table in the jump table info.
32 ///
33 struct MachineJumpTableEntry {
34   /// MBBs - The vector of basic blocks from which to create the jump table.
35   std::vector<MachineBasicBlock*> MBBs;
36   
37   MachineJumpTableEntry(const std::vector<MachineBasicBlock*> &M) : MBBs(M) {}
38 };
39   
40 class MachineJumpTableInfo {
41   const TargetData *TD;
42   unsigned EntrySize;
43   std::vector<MachineJumpTableEntry> JumpTables;
44 public:
45   MachineJumpTableInfo(const TargetData *td, unsigned ES)
46   : TD(td), EntrySize(ES) {}
47     
48   /// getJumpTableIndex - Create a new jump table or return an existing one.
49   ///
50   unsigned getJumpTableIndex(const std::vector<MachineBasicBlock*> &DestBBs);
51   
52   /// isEmpty - Return true if there are no jump tables.
53   ///
54   bool isEmpty() const { return JumpTables.empty(); }
55
56   const std::vector<MachineJumpTableEntry> &getJumpTables() const {
57     return JumpTables;
58   }
59   
60   /// RemoveJumpTable - Mark the specific index as being dead.  This will cause
61   /// it to not be emitted.
62   void RemoveJumpTable(unsigned Idx) {
63     JumpTables[Idx].MBBs.clear();
64   }
65   
66   /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
67   /// the jump tables to branch to New instead.
68   bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New) {
69     assert(Old != New && "Not making a change?");
70     bool MadeChange = false;
71     for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
72       MachineJumpTableEntry &JTE = JumpTables[i];
73       for (unsigned j = 0, e = JTE.MBBs.size(); j != e; ++j)
74         if (JTE.MBBs[j] == Old) {
75           JTE.MBBs[j] = New;
76           MadeChange = true;
77         }
78     }
79     return MadeChange;
80   }
81   
82   /// getEntrySize - Returns the size of an individual field in a jump table. 
83   ///
84   unsigned getEntrySize() const { return EntrySize; }
85   
86   /// getAlignment - returns the target's preferred alignment for jump tables
87   unsigned getAlignment() const;
88   
89   /// print - Used by the MachineFunction printer to print information about
90   /// jump tables.  Implemented in MachineFunction.cpp
91   ///
92   void print(std::ostream &OS) const;
93
94   /// dump - Call print(std::cerr) to be called from the debugger.
95   ///
96   void dump() const;
97 };
98
99 } // End llvm namespace
100
101 #endif