30a8eafefd15350d55de4333c50bc1796139553d
[oota-llvm.git] / lib / Target / ARM / ARMConstantPoolValue.cpp
1 //===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Evan Cheng and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ARM specific constantpool value class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARMConstantPoolValue.h"
15 #include "llvm/ADT/FoldingSet.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/Type.h"
18 using namespace llvm;
19
20 ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv, unsigned id,
21                                            ARMCP::ARMCPKind k,
22                                            unsigned char PCAdj,
23                                            const char *Modif,
24                                            bool AddCA)
25   : MachineConstantPoolValue((const Type*)gv->getType()),
26     GV(gv), S(NULL), LabelId(id), Kind(k), PCAdjust(PCAdj),
27     Modifier(Modif), AddCurrentAddress(AddCA) {}
28
29 ARMConstantPoolValue::ARMConstantPoolValue(const char *s, unsigned id,
30                                            ARMCP::ARMCPKind k,
31                                            unsigned char PCAdj,
32                                            const char *Modif,
33                                            bool AddCA)
34   : MachineConstantPoolValue((const Type*)Type::Int32Ty),
35     GV(NULL), S(s), LabelId(id), Kind(k), PCAdjust(PCAdj),
36     Modifier(Modif), AddCurrentAddress(AddCA) {}
37
38 ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv,
39                                            ARMCP::ARMCPKind k,
40                                            const char *Modif)
41   : MachineConstantPoolValue((const Type*)Type::Int32Ty),
42     GV(gv), S(NULL), LabelId(0), Kind(k), PCAdjust(0),
43     Modifier(Modif) {}
44
45 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
46                                                     unsigned Alignment) {
47   unsigned AlignMask = (1 << Alignment)-1;
48   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
49   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
50     if (Constants[i].isMachineConstantPoolEntry() &&
51         (Constants[i].Offset & AlignMask) == 0) {
52       ARMConstantPoolValue *CPV =
53         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
54       if (CPV->GV == GV &&
55           CPV->S == S &&
56           CPV->LabelId == LabelId &&
57           CPV->Kind == Kind &&
58           CPV->PCAdjust == PCAdjust)
59         return i;
60     }
61   }
62
63   return -1;
64 }
65
66 void
67 ARMConstantPoolValue::AddSelectionDAGCSEId(FoldingSetNodeID &ID) {
68   ID.AddPointer(GV);
69   ID.AddPointer(S);
70   ID.AddInteger(LabelId);
71   ID.AddInteger((unsigned)Kind);
72   ID.AddInteger(PCAdjust);
73 }
74
75 void ARMConstantPoolValue::print(std::ostream &O) const {
76   if (GV)
77     O << GV->getName();
78   else
79     O << S;
80   if (isNonLazyPointer()) O << "$non_lazy_ptr";
81   else if (isStub()) O << "$stub";
82   if (Modifier) O << "(" << Modifier << ")";
83   if (PCAdjust != 0) {
84     O << "-(LPIC" << LabelId << "+"
85       << (unsigned)PCAdjust;
86     if (AddCurrentAddress)
87       O << "-.";
88     O << ")";
89   }
90 }