bcfa40c18cb33babbbe1041b2edb59f7935f461e
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The MachineConstantPool class keeps track of constants referenced by a
11 // function which must be spilled to memory.  This is used for constants which
12 // are unable to be used directly as operands to instructions, which typically
13 // include floating point and large integer constants.
14 //
15 // Instructions reference the address of these constant pool constants through
16 // the use of MO_ConstantPoolIndex values.  When emitting assembly or machine
17 // code, these virtual address references are converted to refer to the
18 // address of the function constant pool values.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_CODEGEN_MACHINECONSTANTPOOL_H
23 #define LLVM_CODEGEN_MACHINECONSTANTPOOL_H
24
25 #include <vector>
26 #include <iosfwd>
27 #include <cassert>
28
29 namespace llvm {
30
31 class Constant;
32
33 /// MachineConstantPoolEntry - One entry in the constant pool.
34 ///
35 struct MachineConstantPoolEntry {
36   /// Val - The constant itself.
37   Constant *Val;
38   /// Alignment - The alignment of the constant.
39   unsigned Alignment;
40   
41   MachineConstantPoolEntry(Constant *V, unsigned A) : Val(V), Alignment(A) {}
42 };
43   
44 class MachineConstantPool {
45   std::vector<MachineConstantPoolEntry> Constants;
46 public:
47   /// getConstantPoolIndex - Create a new entry in the constant pool or return
48   /// an existing one.  User must specify an alignment in bytes for the object.
49   ///
50   unsigned getConstantPoolIndex(Constant *C, unsigned Alignment) {
51     assert(Alignment && "Alignment must be specified!");
52     
53     // Check to see if we already have this constant.
54     //
55     // FIXME, this could be made much more efficient for large constant pools.
56     for (unsigned i = 0, e = Constants.size(); i != e; ++i)
57       if (Constants[i].Val == C && Constants[i].Alignment >= Alignment)
58         return i;
59     Constants.push_back(MachineConstantPoolEntry(C, Alignment));
60     return Constants.size()-1;
61   }
62
63   /// isEmpty - Return true if this constant pool contains no constants.
64   ///
65   bool isEmpty() const { return Constants.empty(); }
66
67   const std::vector<MachineConstantPoolEntry> &getConstants() const {
68     return Constants;
69   }
70
71   /// print - Used by the MachineFunction printer to print information about
72   /// stack objects.  Implemented in MachineFunction.cpp
73   ///
74   void print(std::ostream &OS) const;
75
76   /// dump - Call print(std::cerr) to be called from the debugger.
77   void dump() const;
78 };
79
80 } // End llvm namespace
81
82 #endif