blob: 8dae56ab5e18956f234f9dc6a2129fbbf6f4398c [file] [log] [blame]
Evan Cheng10043e22007-01-19 07:51:42 +00001//===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Cheng10043e22007-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 Wilson433ab092009-11-02 16:59:06 +000016#include "llvm/Constant.h"
17#include "llvm/Constants.h"
Evan Cheng10043e22007-01-19 07:51:42 +000018#include "llvm/GlobalValue.h"
Evan Cheng83f35172007-01-30 20:37:08 +000019#include "llvm/Type.h"
Bill Wendlinga1127b22011-09-29 23:48:44 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000021#include "llvm/Support/raw_ostream.h"
Jim Grosbach74eb9e72009-08-11 15:26:27 +000022#include <cstdlib>
Evan Cheng10043e22007-01-19 07:51:42 +000023using namespace llvm;
24
Dan Gohmanbcaf6812010-04-15 01:51:59 +000025ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
Jim Grosbach20eac922009-09-01 01:57:56 +000026 ARMCP::ARMCPKind K,
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +000027 unsigned char PCAdj,
Jim Grosbacha942ad42010-11-09 21:36:17 +000028 ARMCP::ARMCPModifier Modif,
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000029 bool AddCA)
Chris Lattner229907c2011-07-18 04:54:35 +000030 : MachineConstantPoolValue((Type*)cval->getType()),
Bob Wilson433ab092009-11-02 16:59:06 +000031 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000032 Modifier(Modif), AddCurrentAddress(AddCA) {}
Evan Cheng83f35172007-01-30 20:37:08 +000033
Owen Anderson55f1c092009-08-13 21:58:54 +000034ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
Bill Wendlinga1127b22011-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 Anderson55f1c092009-08-13 21:58:54 +000046 const char *s, unsigned id,
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +000047 unsigned char PCAdj,
Jim Grosbacha942ad42010-11-09 21:36:17 +000048 ARMCP::ARMCPModifier Modif,
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000049 bool AddCA)
Chris Lattner229907c2011-07-18 04:54:35 +000050 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
Bob Wilson433ab092009-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 Venancioee2d1642007-04-22 00:04:12 +000053
Dan Gohmanbcaf6812010-04-15 01:51:59 +000054ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
Jim Grosbacha942ad42010-11-09 21:36:17 +000055 ARMCP::ARMCPModifier Modif)
Chris Lattner229907c2011-07-18 04:54:35 +000056 : MachineConstantPoolValue((Type*)Type::getInt32Ty(gv->getContext())),
Bob Wilson433ab092009-11-02 16:59:06 +000057 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
Jan Wen Voungd602c2c2010-12-01 01:38:58 +000058 Modifier(Modif), AddCurrentAddress(false) {}
Evan Cheng10043e22007-01-19 07:51:42 +000059
Dan Gohmanbcaf6812010-04-15 01:51:59 +000060const GlobalValue *ARMConstantPoolValue::getGV() const {
Bob Wilson433ab092009-11-02 16:59:06 +000061 return dyn_cast_or_null<GlobalValue>(CVal);
62}
63
Dan Gohmanbcaf6812010-04-15 01:51:59 +000064const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
Bob Wilson433ab092009-11-02 16:59:06 +000065 return dyn_cast_or_null<BlockAddress>(CVal);
66}
67
Bill Wendlinga1127b22011-09-29 23:48:44 +000068const MachineBasicBlock *ARMConstantPoolValue::getMBB() const {
69 return MBB;
70}
71
Bill Wendlinge8e4dbf2011-09-30 18:42:06 +000072const 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
Evan Cheng40a42222010-09-24 05:18:35 +000086static 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
Evan Cheng10043e22007-01-19 07:51:42 +000094int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
95 unsigned Alignment) {
Evan Cheng1fb8aed2009-03-13 07:51:59 +000096 unsigned AlignMask = Alignment - 1;
Evan Cheng10043e22007-01-19 07:51:42 +000097 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
98 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
99 if (Constants[i].isMachineConstantPoolEntry() &&
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000100 (Constants[i].getAlignment() & AlignMask) == 0) {
Evan Cheng10043e22007-01-19 07:51:42 +0000101 ARMConstantPoolValue *CPV =
102 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
Bob Wilson433ab092009-11-02 16:59:06 +0000103 if (CPV->CVal == CVal &&
Evan Cheng83f35172007-01-30 20:37:08 +0000104 CPV->LabelId == LabelId &&
Evan Cheng7ff83192009-11-07 03:52:02 +0000105 CPV->PCAdjust == PCAdjust &&
Evan Cheng40a42222010-09-24 05:18:35 +0000106 CPV_streq(CPV->S, S) &&
Jim Grosbacha942ad42010-11-09 21:36:17 +0000107 CPV->Modifier == Modifier)
Evan Cheng10043e22007-01-19 07:51:42 +0000108 return i;
109 }
110 }
111
112 return -1;
113}
114
Benjamin Kramereda08012009-08-11 16:03:08 +0000115ARMConstantPoolValue::~ARMConstantPoolValue() {
Jim Grosbach74eb9e72009-08-11 15:26:27 +0000116 free((void*)S);
117}
118
Evan Cheng10043e22007-01-19 07:51:42 +0000119void
Jim Grosbachaf136f72011-09-27 20:59:33 +0000120ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
Bob Wilson433ab092009-11-02 16:59:06 +0000121 ID.AddPointer(CVal);
Evan Cheng83f35172007-01-30 20:37:08 +0000122 ID.AddPointer(S);
Evan Cheng10043e22007-01-19 07:51:42 +0000123 ID.AddInteger(LabelId);
Evan Cheng10043e22007-01-19 07:51:42 +0000124 ID.AddInteger(PCAdjust);
125}
126
Evan Cheng7ff83192009-11-07 03:52:02 +0000127bool
128ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
129 if (ACPV->Kind == Kind &&
130 ACPV->CVal == CVal &&
131 ACPV->PCAdjust == PCAdjust &&
Evan Cheng40a42222010-09-24 05:18:35 +0000132 CPV_streq(ACPV->S, S) &&
Jim Grosbacha942ad42010-11-09 21:36:17 +0000133 ACPV->Modifier == Modifier) {
Evan Cheng7ff83192009-11-07 03:52:02 +0000134 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
Evan Chengde9dbc52008-10-29 23:55:17 +0000144void ARMConstantPoolValue::dump() const {
Chris Lattnera6f074f2009-08-23 03:41:05 +0000145 errs() << " " << *this;
Evan Chengde9dbc52008-10-29 23:55:17 +0000146}
147
Evan Chengde9dbc52008-10-29 23:55:17 +0000148
Chris Lattner0c19df42008-08-23 22:23:09 +0000149void ARMConstantPoolValue::print(raw_ostream &O) const {
Bob Wilson433ab092009-11-02 16:59:06 +0000150 if (CVal)
151 O << CVal->getName();
Bill Wendlinga1127b22011-09-29 23:48:44 +0000152 else if (MBB)
153 O << "";
Evan Cheng83f35172007-01-30 20:37:08 +0000154 else
155 O << S;
Jim Grosbacha942ad42010-11-09 21:36:17 +0000156 if (Modifier) O << "(" << getModifierText() << ")";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000157 if (PCAdjust != 0) {
Evan Cheng6dd08b62008-11-04 00:50:32 +0000158 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
159 if (AddCurrentAddress) O << "-.";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000160 O << ")";
161 }
Evan Cheng10043e22007-01-19 07:51:42 +0000162}