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