blob: f13ccc638448425930191260847ba2a3b714a252 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Wilson62acbf12009-11-02 16:59:06 +000016#include "llvm/Constant.h"
17#include "llvm/Constants.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/GlobalValue.h"
19#include "llvm/Type.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000020#include "llvm/Support/raw_ostream.h"
Jim Grosbach3fe420b2009-08-11 15:26:27 +000021#include <cstdlib>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
Dan Gohman36c56d02010-04-15 01:51:59 +000024ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
Jim Grosbach5e0257f2009-09-01 01:57:56 +000025 ARMCP::ARMCPKind K,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026 unsigned char PCAdj,
27 const char *Modif,
28 bool AddCA)
Bob Wilson62acbf12009-11-02 16:59:06 +000029 : MachineConstantPoolValue((const Type*)cval->getType()),
30 CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 Modifier(Modif), AddCurrentAddress(AddCA) {}
32
Owen Anderson35b47072009-08-13 21:58:54 +000033ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
34 const char *s, unsigned id,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 unsigned char PCAdj,
36 const char *Modif,
37 bool AddCA)
Owen Anderson35b47072009-08-13 21:58:54 +000038 : MachineConstantPoolValue((const Type*)Type::getInt32Ty(C)),
Bob Wilson62acbf12009-11-02 16:59:06 +000039 CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
40 PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041
Dan Gohman36c56d02010-04-15 01:51:59 +000042ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
43 const char *Modif)
Owen Anderson35b47072009-08-13 21:58:54 +000044 : MachineConstantPoolValue((const Type*)Type::getInt32Ty(gv->getContext())),
Bob Wilson62acbf12009-11-02 16:59:06 +000045 CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 Modifier(Modif) {}
47
Dan Gohman36c56d02010-04-15 01:51:59 +000048const GlobalValue *ARMConstantPoolValue::getGV() const {
Bob Wilson62acbf12009-11-02 16:59:06 +000049 return dyn_cast_or_null<GlobalValue>(CVal);
50}
51
Dan Gohman36c56d02010-04-15 01:51:59 +000052const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
Bob Wilson62acbf12009-11-02 16:59:06 +000053 return dyn_cast_or_null<BlockAddress>(CVal);
54}
55
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
57 unsigned Alignment) {
Evan Cheng68c18682009-03-13 07:51:59 +000058 unsigned AlignMask = Alignment - 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
60 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
61 if (Constants[i].isMachineConstantPoolEntry() &&
Evan Cheng68c18682009-03-13 07:51:59 +000062 (Constants[i].getAlignment() & AlignMask) == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 ARMConstantPoolValue *CPV =
64 (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
Bob Wilson62acbf12009-11-02 16:59:06 +000065 if (CPV->CVal == CVal &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 CPV->LabelId == LabelId &&
Evan Cheng626474d2009-11-07 03:52:02 +000067 CPV->PCAdjust == PCAdjust &&
68 (CPV->S == S || strcmp(CPV->S, S) == 0) &&
69 (CPV->Modifier == Modifier || strcmp(CPV->Modifier, Modifier) == 0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 return i;
71 }
72 }
73
74 return -1;
75}
76
Benjamin Kramer909610a2009-08-11 16:03:08 +000077ARMConstantPoolValue::~ARMConstantPoolValue() {
Jim Grosbach3fe420b2009-08-11 15:26:27 +000078 free((void*)S);
79}
80
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081void
82ARMConstantPoolValue::AddSelectionDAGCSEId(FoldingSetNodeID &ID) {
Bob Wilson62acbf12009-11-02 16:59:06 +000083 ID.AddPointer(CVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 ID.AddPointer(S);
85 ID.AddInteger(LabelId);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 ID.AddInteger(PCAdjust);
87}
88
Evan Cheng626474d2009-11-07 03:52:02 +000089bool
90ARMConstantPoolValue::hasSameValue(ARMConstantPoolValue *ACPV) {
91 if (ACPV->Kind == Kind &&
92 ACPV->CVal == CVal &&
93 ACPV->PCAdjust == PCAdjust &&
94 (ACPV->S == S || strcmp(ACPV->S, S) == 0) &&
95 (ACPV->Modifier == Modifier || strcmp(ACPV->Modifier, Modifier) == 0)) {
96 if (ACPV->LabelId == LabelId)
97 return true;
98 // Two PC relative constpool entries containing the same GV address or
99 // external symbols. FIXME: What about blockaddress?
100 if (Kind == ARMCP::CPValue || Kind == ARMCP::CPExtSymbol)
101 return true;
102 }
103 return false;
104}
105
Evan Chengf3aaeed2008-10-29 23:55:17 +0000106void ARMConstantPoolValue::dump() const {
Chris Lattnerd71b0b02009-08-23 03:41:05 +0000107 errs() << " " << *this;
Evan Chengf3aaeed2008-10-29 23:55:17 +0000108}
109
Evan Chengf3aaeed2008-10-29 23:55:17 +0000110
Chris Lattner1fefaac2008-08-23 22:23:09 +0000111void ARMConstantPoolValue::print(raw_ostream &O) const {
Bob Wilson62acbf12009-11-02 16:59:06 +0000112 if (CVal)
113 O << CVal->getName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 else
115 O << S;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 if (Modifier) O << "(" << Modifier << ")";
117 if (PCAdjust != 0) {
Evan Cheng5a033a62008-11-04 00:50:32 +0000118 O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
119 if (AddCurrentAddress) O << "-.";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 O << ")";
121 }
122}