blob: 90dd0c7fd96225b76762f46b8ca1f332e2143c05 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
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"
Bob Wilson28989a82009-11-02 16:59:06 +000016#include "llvm/Constant.h"
17#include "llvm/Constants.h"
Evan Chenga8e29892007-01-19 07:51:42 +000018#include "llvm/GlobalValue.h"
Evan Chengc60e76d2007-01-30 20:37:08 +000019#include "llvm/Type.h"
Chris Lattner944fac72008-08-23 22:23:09 +000020#include "llvm/Support/raw_ostream.h"
Jim Grosbachf1287872009-08-11 15:26:27 +000021#include <cstdlib>
Evan Chenga8e29892007-01-19 07:51:42 +000022using namespace llvm;
23
Bob Wilson28989a82009-11-02 16:59:06 +000024ARMConstantPoolValue::ARMConstantPoolValue(Constant *cval, unsigned id,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000025 ARMCP::ARMCPKind K,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000026 unsigned char PCAdj,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000027 const char *Modif,
28 bool AddCA)
Bob Wilson28989a82009-11-02 16:59:06 +000029 : MachineConstantPoolValue((const Type*)cval->getType()),
30 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000031 Modifier(Modif), AddCurrentAddress(AddCA) {}
Evan Chengc60e76d2007-01-30 20:37:08 +000032
Owen Anderson1d0be152009-08-13 21:58:54 +000033ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
34 const char *s, unsigned id,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000035 unsigned char PCAdj,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000036 const char *Modif,
37 bool AddCA)
Owen Anderson1d0be152009-08-13 21:58:54 +000038 : MachineConstantPoolValue((const Type*)Type::getInt32Ty(C)),
Bob Wilson28989a82009-11-02 16:59:06 +000039 CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
40 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000041
Evan Chenge4e4ed32009-08-28 23:18:09 +000042ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv, const char *Modif)
Owen Anderson1d0be152009-08-13 21:58:54 +000043 : MachineConstantPoolValue((const Type*)Type::getInt32Ty(gv->getContext())),
Bob Wilson28989a82009-11-02 16:59:06 +000044 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000045 Modifier(Modif) {}
Evan Chenga8e29892007-01-19 07:51:42 +000046
Bob Wilson28989a82009-11-02 16:59:06 +000047GlobalValue *ARMConstantPoolValue::getGV() const {
48 return dyn_cast_or_null<GlobalValue>(CVal);
49}
50
51BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
52 return dyn_cast_or_null<BlockAddress>(CVal);
53}
54
Evan Chenga8e29892007-01-19 07:51:42 +000055int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
56 unsigned Alignment) {
Evan Cheng1606e8e2009-03-13 07:51:59 +000057 unsigned AlignMask = Alignment - 1;
Evan Chenga8e29892007-01-19 07:51:42 +000058 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
59 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
60 if (Constants[i].isMachineConstantPoolEntry() &&
Evan Cheng1606e8e2009-03-13 07:51:59 +000061 (Constants[i].getAlignment() & AlignMask) == 0) {
Evan Chenga8e29892007-01-19 07:51:42 +000062 ARMConstantPoolValue *CPV =
63 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
Bob Wilson28989a82009-11-02 16:59:06 +000064 if (CPV->CVal == CVal &&
Evan Chengc60e76d2007-01-30 20:37:08 +000065 CPV->LabelId == LabelId &&
Evan Cheng78e5c112009-11-07 03:52:02 +000066 CPV->PCAdjust == PCAdjust &&
67 (CPV->S == S || strcmp(CPV->S, S) == 0) &&
68 (CPV->Modifier == Modifier || strcmp(CPV->Modifier, Modifier) == 0))
Evan Chenga8e29892007-01-19 07:51:42 +000069 return i;
70 }
71 }
72
73 return -1;
74}
75
Benjamin Kramer327365e2009-08-11 16:03:08 +000076ARMConstantPoolValue::~ARMConstantPoolValue() {
Jim Grosbachf1287872009-08-11 15:26:27 +000077 free((void*)S);
78}
79
Evan Chenga8e29892007-01-19 07:51:42 +000080void
81ARMConstantPoolValue::AddSelectionDAGCSEId(FoldingSetNodeID &ID) {
Bob Wilson28989a82009-11-02 16:59:06 +000082 ID.AddPointer(CVal);
Evan Chengc60e76d2007-01-30 20:37:08 +000083 ID.AddPointer(S);
Evan Chenga8e29892007-01-19 07:51:42 +000084 ID.AddInteger(LabelId);
Evan Chenga8e29892007-01-19 07:51:42 +000085 ID.AddInteger(PCAdjust);
86}
87
Evan Cheng78e5c112009-11-07 03:52:02 +000088bool
89ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
90 if (ACPV->Kind == Kind &&
91 ACPV->CVal == CVal &&
92 ACPV->PCAdjust == PCAdjust &&
93 (ACPV->S == S || strcmp(ACPV->S, S) == 0) &&
94 (ACPV->Modifier == Modifier || strcmp(ACPV->Modifier, Modifier) == 0)) {
95 if (ACPV->LabelId == LabelId)
96 return true;
97 // Two PC relative constpool entries containing the same GV address or
98 // external symbols. FIXME: What about blockaddress?
99 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
100 return true;
101 }
102 return false;
103}
104
Evan Cheng5be59ea2008-10-29 23:55:17 +0000105void ARMConstantPoolValue::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000106 errs() << " " << *this;
Evan Cheng5be59ea2008-10-29 23:55:17 +0000107}
108
Evan Cheng5be59ea2008-10-29 23:55:17 +0000109
Chris Lattner944fac72008-08-23 22:23:09 +0000110void ARMConstantPoolValue::print(raw_ostream &O) const {
Bob Wilson28989a82009-11-02 16:59:06 +0000111 if (CVal)
112 O << CVal->getName();
Evan Chengc60e76d2007-01-30 20:37:08 +0000113 else
114 O << S;
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +0000115 if (Modifier) O << "(" << Modifier << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000116 if (PCAdjust != 0) {
Evan Cheng25e04782008-11-04 00:50:32 +0000117 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
118 if (AddCurrentAddress) O << "-.";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000119 O << ")";
120 }
Evan Chenga8e29892007-01-19 07:51:42 +0000121}