Constify 'isLSDA' and move a method out-of-line.
[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 const char *ARMConstantPoolValue::getModifierText() const {
73   switch (Modifier) {
74   default: llvm_unreachable("Unknown modifier!");
75     // FIXME: Are these case sensitive? It'd be nice to lower-case all the
76     // strings if that's legal.
77   case ARMCP::no_modifier: return "none";
78   case ARMCP::TLSGD:       return "tlsgd";
79   case ARMCP::GOT:         return "GOT";
80   case ARMCP::GOTOFF:      return "GOTOFF";
81   case ARMCP::GOTTPOFF:    return "gottpoff";
82   case ARMCP::TPOFF:       return "tpoff";
83   }
84 }
85
86 static bool CPV_streq(const char *S1, const char *S2) {
87   if (S1 == S2)
88     return true;
89   if (S1 && S2 && strcmp(S1, S2) == 0)
90     return true;
91   return false;
92 }
93
94 int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
95                                                     unsigned Alignment) {
96   unsigned AlignMask = Alignment - 1;
97   const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
98   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
99     if (Constants[i].isMachineConstantPoolEntry() &&
100         (Constants[i].getAlignment() & AlignMask) == 0) {
101       ARMConstantPoolValue *CPV =
102         (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
103       if (CPV->CVal == CVal &&
104           CPV->LabelId == LabelId &&
105           CPV->PCAdjust == PCAdjust &&
106           CPV_streq(CPV->S, S) &&
107           CPV->Modifier == Modifier)
108         return i;
109     }
110   }
111
112   return -1;
113 }
114
115 ARMConstantPoolValue::~ARMConstantPoolValue() {
116   free((void*)S);
117 }
118
119 void
120 ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
121   ID.AddPointer(CVal);
122   ID.AddPointer(S);
123   ID.AddInteger(LabelId);
124   ID.AddInteger(PCAdjust);
125 }
126
127 bool
128 ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
129   if (ACPV->Kind == Kind &&
130       ACPV->CVal == CVal &&
131       ACPV->PCAdjust == PCAdjust &&
132       CPV_streq(ACPV->S, S) &&
133       ACPV->Modifier == Modifier) {
134     if (ACPV->LabelId == LabelId)
135       return true;
136     // Two PC relative constpool entries containing the same GV address or
137     // external symbols. FIXME: What about blockaddress?
138     if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
139       return true;
140   }
141   return false;
142 }
143
144 void ARMConstantPoolValue::dump() const {
145   errs() << "  " << *this;
146 }
147
148
149 void ARMConstantPoolValue::print(raw_ostream &O) const {
150   if (CVal)
151     O << CVal->getName();
152   else if (MBB)
153     O << "";
154   else
155     O << S;
156   if (Modifier) O << "(" << getModifierText() << ")";
157   if (PCAdjust != 0) {
158     O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
159     if (AddCurrentAddress) O << "-.";
160     O << ")";
161   }
162 }