blob: 476d59f988b7fe352c728b99f118b22c6475a719 [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"
Chris Lattner0c19df42008-08-23 22:23:09 +000020#include "llvm/Support/raw_ostream.h"
Jim Grosbach74eb9e72009-08-11 15:26:27 +000021#include <cstdlib>
Evan Cheng10043e22007-01-19 07:51:42 +000022using namespace llvm;
23
Dan Gohmanbcaf6812010-04-15 01:51:59 +000024ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
Jim Grosbach20eac922009-09-01 01:57:56 +000025 ARMCP::ARMCPKind K,
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +000026 unsigned char PCAdj,
Jim Grosbacha942ad42010-11-09 21:36:17 +000027 ARMCP::ARMCPModifier Modif,
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000028 bool AddCA)
Chris Lattner229907c2011-07-18 04:54:35 +000029 : MachineConstantPoolValue((Type*)cval->getType()),
Bob Wilson433ab092009-11-02 16:59:06 +000030 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000031 Modifier(Modif), AddCurrentAddress(AddCA) {}
Evan Cheng83f35172007-01-30 20:37:08 +000032
Owen Anderson55f1c092009-08-13 21:58:54 +000033ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
34 const char *s, unsigned id,
Lauro Ramos Venancioee2d1642007-04-22 00:04:12 +000035 unsigned char PCAdj,
Jim Grosbacha942ad42010-11-09 21:36:17 +000036 ARMCP::ARMCPModifier Modif,
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +000037 bool AddCA)
Chris Lattner229907c2011-07-18 04:54:35 +000038 : MachineConstantPoolValue((Type*)Type::getInt32Ty(C)),
Bob Wilson433ab092009-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 Venancioee2d1642007-04-22 00:04:12 +000041
Dan Gohmanbcaf6812010-04-15 01:51:59 +000042ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
Jim Grosbacha942ad42010-11-09 21:36:17 +000043 ARMCP::ARMCPModifier Modif)
Chris Lattner229907c2011-07-18 04:54:35 +000044 : MachineConstantPoolValue((Type*)Type::getInt32Ty(gv->getContext())),
Bob Wilson433ab092009-11-02 16:59:06 +000045 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
Jan Wen Voungd602c2c2010-12-01 01:38:58 +000046 Modifier(Modif), AddCurrentAddress(false) {}
Evan Cheng10043e22007-01-19 07:51:42 +000047
Dan Gohmanbcaf6812010-04-15 01:51:59 +000048const GlobalValue *ARMConstantPoolValue::getGV() const {
Bob Wilson433ab092009-11-02 16:59:06 +000049 return dyn_cast_or_null<GlobalValue>(CVal);
50}
51
Dan Gohmanbcaf6812010-04-15 01:51:59 +000052const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
Bob Wilson433ab092009-11-02 16:59:06 +000053 return dyn_cast_or_null<BlockAddress>(CVal);
54}
55
Evan Cheng40a42222010-09-24 05:18:35 +000056static 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
Evan Cheng10043e22007-01-19 07:51:42 +000064int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
65 unsigned Alignment) {
Evan Cheng1fb8aed2009-03-13 07:51:59 +000066 unsigned AlignMask = Alignment - 1;
Evan Cheng10043e22007-01-19 07:51:42 +000067 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
68 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
69 if (Constants[i].isMachineConstantPoolEntry() &&
Evan Cheng1fb8aed2009-03-13 07:51:59 +000070 (Constants[i].getAlignment() & AlignMask) == 0) {
Evan Cheng10043e22007-01-19 07:51:42 +000071 ARMConstantPoolValue *CPV =
72 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
Bob Wilson433ab092009-11-02 16:59:06 +000073 if (CPV->CVal == CVal &&
Evan Cheng83f35172007-01-30 20:37:08 +000074 CPV->LabelId == LabelId &&
Evan Cheng7ff83192009-11-07 03:52:02 +000075 CPV->PCAdjust == PCAdjust &&
Evan Cheng40a42222010-09-24 05:18:35 +000076 CPV_streq(CPV->S, S) &&
Jim Grosbacha942ad42010-11-09 21:36:17 +000077 CPV->Modifier == Modifier)
Evan Cheng10043e22007-01-19 07:51:42 +000078 return i;
79 }
80 }
81
82 return -1;
83}
84
Benjamin Kramereda08012009-08-11 16:03:08 +000085ARMConstantPoolValue::~ARMConstantPoolValue() {
Jim Grosbach74eb9e72009-08-11 15:26:27 +000086 free((void*)S);
87}
88
Evan Cheng10043e22007-01-19 07:51:42 +000089void
Jim Grosbachaf136f72011-09-27 20:59:33 +000090ARMConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
Bob Wilson433ab092009-11-02 16:59:06 +000091 ID.AddPointer(CVal);
Evan Cheng83f35172007-01-30 20:37:08 +000092 ID.AddPointer(S);
Evan Cheng10043e22007-01-19 07:51:42 +000093 ID.AddInteger(LabelId);
Evan Cheng10043e22007-01-19 07:51:42 +000094 ID.AddInteger(PCAdjust);
95}
96
Evan Cheng7ff83192009-11-07 03:52:02 +000097bool
98ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
99 if (ACPV->Kind == Kind &&
100 ACPV->CVal == CVal &&
101 ACPV->PCAdjust == PCAdjust &&
Evan Cheng40a42222010-09-24 05:18:35 +0000102 CPV_streq(ACPV->S, S) &&
Jim Grosbacha942ad42010-11-09 21:36:17 +0000103 ACPV->Modifier == Modifier) {
Evan Cheng7ff83192009-11-07 03:52:02 +0000104 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
Evan Chengde9dbc52008-10-29 23:55:17 +0000114void ARMConstantPoolValue::dump() const {
Chris Lattnera6f074f2009-08-23 03:41:05 +0000115 errs() << " " << *this;
Evan Chengde9dbc52008-10-29 23:55:17 +0000116}
117
Evan Chengde9dbc52008-10-29 23:55:17 +0000118
Chris Lattner0c19df42008-08-23 22:23:09 +0000119void ARMConstantPoolValue::print(raw_ostream &O) const {
Bob Wilson433ab092009-11-02 16:59:06 +0000120 if (CVal)
121 O << CVal->getName();
Evan Cheng83f35172007-01-30 20:37:08 +0000122 else
123 O << S;
Jim Grosbacha942ad42010-11-09 21:36:17 +0000124 if (Modifier) O << "(" << getModifierText() << ")";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000125 if (PCAdjust != 0) {
Evan Cheng6dd08b62008-11-04 00:50:32 +0000126 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
127 if (AddCurrentAddress) O << "-.";
Lauro Ramos Venancioc39c12a2007-04-27 13:54:47 +0000128 O << ")";
129 }
Evan Cheng10043e22007-01-19 07:51:42 +0000130}