blob: dccdefbfe89e38a283a397e2e24de8429c79c5bd [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"
Bill Wendling4dd9b092011-09-29 23:48:44 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner944fac72008-08-23 22:23:09 +000021#include "llvm/Support/raw_ostream.h"
Jim Grosbachf1287872009-08-11 15:26:27 +000022#include <cstdlib>
Evan Chenga8e29892007-01-19 07:51:42 +000023using namespace llvm;
24
Dan Gohman46510a72010-04-15 01:51:59 +000025ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
Jim Grosbach3fb2b1e2009-09-01 01:57:56 +000026 ARMCP::ARMCPKind K,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000027 unsigned char PCAdj,
Jim Grosbach3a2429a2010-11-09 21:36:17 +000028 ARMCP::ARMCPModifier Modif,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000029 bool AddCA)
Chris Lattnerdb125cf2011-07-18 04:54:35 +000030 : MachineConstantPoolValue((Type*)cval->getType()),
Bob Wilson28989a82009-11-02 16:59:06 +000031 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000032 Modifier(Modif), AddCurrentAddress(AddCA) {}
Evan Chengc60e76d2007-01-30 20:37:08 +000033
Owen Anderson1d0be152009-08-13 21:58:54 +000034ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
Bill Wendling4dd9b092011-09-29 23:48:44 +000035 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
45ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
Owen Anderson1d0be152009-08-13 21:58:54 +000046 const char *s, unsigned id,
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000047 unsigned char PCAdj,
Jim Grosbach3a2429a2010-11-09 21:36:17 +000048 ARMCP::ARMCPModifier Modif,
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +000049 bool AddCA)
Chris Lattnerdb125cf2011-07-18 04:54:35 +000050 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
Bob Wilson28989a82009-11-02 16:59:06 +000051 CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
52 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
Lauro Ramos Venancio0ae4a332007-04-22 00:04:12 +000053
Dan Gohman46510a72010-04-15 01:51:59 +000054ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
Jim Grosbach3a2429a2010-11-09 21:36:17 +000055 ARMCP::ARMCPModifier Modif)
Chris Lattnerdb125cf2011-07-18 04:54:35 +000056 : MachineConstantPoolValue((Type*)Type::getInt32Ty(gv->getContext())),
Bob Wilson28989a82009-11-02 16:59:06 +000057 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
Jan Wen Vounga63bf702010-12-01 01:38:58 +000058 Modifier(Modif), AddCurrentAddress(false) {}
Evan Chenga8e29892007-01-19 07:51:42 +000059
Dan Gohman46510a72010-04-15 01:51:59 +000060const GlobalValue *ARMConstantPoolValue::getGV() const {
Bob Wilson28989a82009-11-02 16:59:06 +000061 return dyn_cast_or_null<GlobalValue>(CVal);
62}
63
Dan Gohman46510a72010-04-15 01:51:59 +000064const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
Bob Wilson28989a82009-11-02 16:59:06 +000065 return dyn_cast_or_null<BlockAddress>(CVal);
66}
67
Bill Wendling4dd9b092011-09-29 23:48:44 +000068const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
69 return MBB;
70}
71
Evan Chengde0e11c2010-09-24 05:18:35 +000072static 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
Evan Chenga8e29892007-01-19 07:51:42 +000080int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
81 unsigned Alignment) {
Evan Cheng1606e8e2009-03-13 07:51:59 +000082 unsigned AlignMask = Alignment - 1;
Evan Chenga8e29892007-01-19 07:51:42 +000083 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
84 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
85 if (Constants[i].isMachineConstantPoolEntry() &&
Evan Cheng1606e8e2009-03-13 07:51:59 +000086 (Constants[i].getAlignment() & AlignMask) == 0) {
Evan Chenga8e29892007-01-19 07:51:42 +000087 ARMConstantPoolValue *CPV =
88 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
Bob Wilson28989a82009-11-02 16:59:06 +000089 if (CPV->CVal == CVal &&
Evan Chengc60e76d2007-01-30 20:37:08 +000090 CPV->LabelId == LabelId &&
Evan Cheng78e5c112009-11-07 03:52:02 +000091 CPV->PCAdjust == PCAdjust &&
Evan Chengde0e11c2010-09-24 05:18:35 +000092 CPV_streq(CPV->S, S) &&
Jim Grosbach3a2429a2010-11-09 21:36:17 +000093 CPV->Modifier == Modifier)
Evan Chenga8e29892007-01-19 07:51:42 +000094 return i;
95 }
96 }
97
98 return -1;
99}
100
Benjamin Kramer327365e2009-08-11 16:03:08 +0000101ARMConstantPoolValue::~ARMConstantPoolValue() {
Jim Grosbachf1287872009-08-11 15:26:27 +0000102 free((void*)S);
103}
104
Evan Chenga8e29892007-01-19 07:51:42 +0000105void
Jim Grosbach5405d582011-09-27 20:59:33 +0000106ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
Bob Wilson28989a82009-11-02 16:59:06 +0000107 ID.AddPointer(CVal);
Evan Chengc60e76d2007-01-30 20:37:08 +0000108 ID.AddPointer(S);
Evan Chenga8e29892007-01-19 07:51:42 +0000109 ID.AddInteger(LabelId);
Evan Chenga8e29892007-01-19 07:51:42 +0000110 ID.AddInteger(PCAdjust);
111}
112
Evan Cheng78e5c112009-11-07 03:52:02 +0000113bool
114ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
115 if (ACPV->Kind == Kind &&
116 ACPV->CVal == CVal &&
117 ACPV->PCAdjust == PCAdjust &&
Evan Chengde0e11c2010-09-24 05:18:35 +0000118 CPV_streq(ACPV->S, S) &&
Jim Grosbach3a2429a2010-11-09 21:36:17 +0000119 ACPV->Modifier == Modifier) {
Evan Cheng78e5c112009-11-07 03:52:02 +0000120 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
Evan Cheng5be59ea2008-10-29 23:55:17 +0000130void ARMConstantPoolValue::dump() const {
Chris Lattner705e07f2009-08-23 03:41:05 +0000131 errs() << " " << *this;
Evan Cheng5be59ea2008-10-29 23:55:17 +0000132}
133
Evan Cheng5be59ea2008-10-29 23:55:17 +0000134
Chris Lattner944fac72008-08-23 22:23:09 +0000135void ARMConstantPoolValue::print(raw_ostream &O) const {
Bob Wilson28989a82009-11-02 16:59:06 +0000136 if (CVal)
137 O << CVal->getName();
Bill Wendling4dd9b092011-09-29 23:48:44 +0000138 else if (MBB)
139 O << "";
Evan Chengc60e76d2007-01-30 20:37:08 +0000140 else
141 O << S;
Jim Grosbach3a2429a2010-11-09 21:36:17 +0000142 if (Modifier) O << "(" << getModifierText() << ")";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000143 if (PCAdjust != 0) {
Evan Cheng25e04782008-11-04 00:50:32 +0000144 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
145 if (AddCurrentAddress) O << "-.";
Lauro Ramos Venancio64f4fa52007-04-27 13:54:47 +0000146 O << ")";
147 }
Evan Chenga8e29892007-01-19 07:51:42 +0000148}