Add an "alignment" field to the MachineFunction object. It makes more sense to
[oota-llvm.git] / include / llvm / CodeGen / MachineConstantPool.h
1 //===-- CodeGen/MachineConstantPool.h - Abstract Constant Pool --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// @file
11 /// This file declares the MachineConstantPool class which is an abstract
12 /// constant pool to keep track of constants referenced by a function.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
17 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
18
19 #include <cassert>
20 #include <climits>
21 #include <vector>
22
23 namespace llvm {
24
25 class Constant;
26 class FoldingSetNodeID;
27 class TargetData;
28 class TargetMachine;
29 class Type;
30 class MachineConstantPool;
31 class raw_ostream;
32
33 /// Abstract base class for all machine specific constantpool value subclasses.
34 ///
35 class MachineConstantPoolValue {
36   const Type *Ty;
37
38 public:
39   explicit MachineConstantPoolValue(const Type *ty) : Ty(ty) {}
40   virtual ~MachineConstantPoolValue() {}
41
42   /// getType - get type of this MachineConstantPoolValue.
43   ///
44   inline const Type *getType() const { return Ty; }
45
46   virtual int getExistingMachineCPValue(MachineConstantPool *CP,
47                                         unsigned Alignment) = 0;
48
49   virtual void AddSelectionDAGCSEId(FoldingSetNodeID &ID) = 0;
50
51   /// print - Implement operator<<
52   virtual void print(raw_ostream &O) const = 0;
53 };
54
55 inline raw_ostream &operator<<(raw_ostream &OS,
56                                const MachineConstantPoolValue &V) {
57   V.print(OS);
58   return OS;
59 }
60   
61
62 /// This class is a data container for one entry in a MachineConstantPool.
63 /// It contains a pointer to the value and an offset from the start of
64 /// the constant pool.
65 /// @brief An entry in a MachineConstantPool
66 class MachineConstantPoolEntry {
67 public:
68   /// The constant itself.
69   union {
70     Constant *ConstVal;
71     MachineConstantPoolValue *MachineCPVal;
72   } Val;
73
74   /// The required alignment for this entry. The top bit is set when Val is
75   /// a MachineConstantPoolValue.
76   unsigned Alignment;
77
78   MachineConstantPoolEntry(Constant *V, unsigned A)
79     : Alignment(A) {
80     Val.ConstVal = V;
81   }
82   MachineConstantPoolEntry(MachineConstantPoolValue *V, unsigned A)
83     : Alignment(A) {
84     Val.MachineCPVal = V; 
85     Alignment |= 1 << (sizeof(unsigned)*CHAR_BIT-1);
86   }
87
88   bool isMachineConstantPoolEntry() const {
89     return (int)Alignment < 0;
90   }
91
92   int getAlignment() const { 
93     return Alignment & ~(1 << (sizeof(unsigned)*CHAR_BIT-1));
94   }
95
96   const Type *getType() const;
97 };
98   
99 /// The MachineConstantPool class keeps track of constants referenced by a
100 /// function which must be spilled to memory.  This is used for constants which
101 /// are unable to be used directly as operands to instructions, which typically
102 /// include floating point and large integer constants.
103 ///
104 /// Instructions reference the address of these constant pool constants through
105 /// the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
106 /// code, these virtual address references are converted to refer to the
107 /// address of the function constant pool values.
108 /// @brief The machine constant pool.
109 class MachineConstantPool {
110   const TargetData *TD;   ///< The machine's TargetData.
111   unsigned PoolAlignment; ///< The alignment for the pool.
112   std::vector<MachineConstantPoolEntry> Constants; ///< The pool of constants.
113 public:
114   /// @brief The only constructor.
115   explicit MachineConstantPool(const TargetData *td)
116     : TD(td), PoolAlignment(1) {}
117   ~MachineConstantPool();
118     
119   /// getConstantPoolAlignment - Return the the alignment required by
120   /// the whole constant pool, of which the first element must be aligned.
121   unsigned getConstantPoolAlignment() const { return PoolAlignment; }
122   
123   /// getConstantPoolIndex - Create a new entry in the constant pool or return
124   /// an existing one.  User must specify the minimum required alignment for
125   /// the object.
126   unsigned getConstantPoolIndex(Constant *C, unsigned Alignment);
127   unsigned getConstantPoolIndex(MachineConstantPoolValue *V,unsigned Alignment);
128   
129   /// isEmpty - Return true if this constant pool contains no constants.
130   bool isEmpty() const { return Constants.empty(); }
131
132   const std::vector<MachineConstantPoolEntry> &getConstants() const {
133     return Constants;
134   }
135
136   /// print - Used by the MachineFunction printer to print information about
137   /// constant pool objects.  Implemented in MachineFunction.cpp
138   ///
139   void print(raw_ostream &OS) const;
140
141   /// dump - Call print(cerr) to be called from the debugger.
142   void dump() const;
143 };
144
145 } // End llvm namespace
146
147 #endif