Support creating a constant pool value for a machine basic block.
[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 is distributed under the University of Illinois Open Source
6 // 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/Constant.h"
17 #include "llvm/Constants.h"
18 #include "llvm/GlobalValue.h"
19 #include "llvm/Type.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cstdlib>
23 using namespace llvm;
24
25 ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
26                                            ARMCP::ARMCPKind K,
27                                            unsigned char PCAdj,
28                                            ARMCP::ARMCPModifier Modif,
29                                            bool AddCA)
30   : MachineConstantPoolValue((Type*)cval->getType()),
31     CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
32     Modifier(Modif), AddCurrentAddress(AddCA) {}
33
34 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
35                                            const MachineBasicBlock *mbb,
36                                            unsigned id,
37                                            ARMCP::ARMCPKind K,
38                                            unsigned char PCAdj,
39                                            ARMCP::ARMCPModifier Modif,
40                                            bool AddCA)
41   : MachineConstantPoolValue((Type*)Type::getInt8PtrTy(C)),
42     CVal(NULL), MBB(mbb), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
43     Modifier(Modif), AddCurrentAddress(AddCA) {}
44
45 ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
46                                            const char *s, unsigned id,
47                                            unsigned char PCAdj,
48                                            ARMCP::ARMCPModifier Modif,
49                                            bool AddCA)
50   : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
51     CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
52     PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
53
54 ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
55                                            ARMCP::ARMCPModifier Modif)
56   : MachineConstantPoolValue((Type*)Type::getInt32Ty(gv->getContext())),
57     CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
58     Modifier(Modif), AddCurrentAddress(false) {}
59
60 const GlobalValue *ARMConstantPoolValue::getGV() const {
61   return dyn_cast_or_null<GlobalValue>(CVal);
62 }
63
64 const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
65   return dyn_cast_or_null<BlockAddress>(CVal);
66 }
67
68 const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
69   return MBB;
70 }
71
72 static bool CPV_streq(const char *S1, const char *S2) {
73   if (S1 == S2)
74     return true;
75   if (S1 && S2 && strcmp(S1, S2) == 0)
76     return true;
77   return false;
78 }
79
80 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
81                                                     unsigned Alignment) {
82   unsigned AlignMask = Alignment - 1;
83   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
84   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
85     if (Constants[i].isMachineConstantPoolEntry() &&
86         (Constants[i].getAlignment() & AlignMask) == 0) {
87       ARMConstantPoolValue *CPV =
88         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
89       if (CPV->CVal == CVal &&
90           CPV->LabelId == LabelId &&
91           CPV->PCAdjust == PCAdjust &&
92           CPV_streq(CPV->S, S) &&
93           CPV->Modifier == Modifier)
94         return i;
95     }
96   }
97
98   return -1;
99 }
100
101 ARMConstantPoolValue::~ARMConstantPoolValue() {
102   free((void*)S);
103 }
104
105 void
106 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
107   ID.AddPointer(CVal);
108   ID.AddPointer(S);
109   ID.AddInteger(LabelId);
110   ID.AddInteger(PCAdjust);
111 }
112
113 bool
114 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
115   if (ACPV->Kind == Kind &&
116       ACPV->CVal == CVal &&
117       ACPV->PCAdjust == PCAdjust &&
118       CPV_streq(ACPV->S, S) &&
119       ACPV->Modifier == Modifier) {
120     if (ACPV->LabelId == LabelId)
121       return true;
122     // Two PC relative constpool entries containing the same GV address or
123     // external symbols. FIXME: What about blockaddress?
124     if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
125       return true;
126   }
127   return false;
128 }
129
130 void ARMConstantPoolValue::dump() const {
131   errs() << "  " << *this;
132 }
133
134
135 void ARMConstantPoolValue::print(raw_ostream &O) const {
136   if (CVal)
137     O << CVal->getName();
138   else if (MBB)
139     O << "";
140   else
141     O << S;
142   if (Modifier) O << "(" << getModifierText() << ")";
143   if (PCAdjust != 0) {
144     O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
145     if (AddCurrentAddress) O << "-.";
146     O << ")";
147   }
148 }