ARMConstantPoolValue.cpp revision 207618
1//===- ARMConstantPoolValue.cpp - ARM constantpool value --------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
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"
16#include "llvm/Constant.h"
17#include "llvm/Constants.h"
18#include "llvm/GlobalValue.h"
19#include "llvm/Type.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cstdlib>
22using namespace llvm;
23
24ARMConstantPoolValue::ARMConstantPoolValue(const Constant *cval, unsigned id,
25                                           ARMCP::ARMCPKind K,
26                                           unsigned char PCAdj,
27                                           const char *Modif,
28                                           bool AddCA)
29  : MachineConstantPoolValue((const Type*)cval->getType()),
30    CVal(cval), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
31    Modifier(Modif), AddCurrentAddress(AddCA) {}
32
33ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
34                                           const char *s, unsigned id,
35                                           unsigned char PCAdj,
36                                           const char *Modif,
37                                           bool AddCA)
38  : MachineConstantPoolValue((const Type*)Type::getInt32Ty(C)),
39    CVal(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPExtSymbol),
40    PCAdjust(PCAdj), Modifier(Modif), AddCurrentAddress(AddCA) {}
41
42ARMConstantPoolValue::ARMConstantPoolValue(const GlobalValue *gv,
43                                           const char *Modif)
44  : MachineConstantPoolValue((const Type*)Type::getInt32Ty(gv->getContext())),
45    CVal(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
46    Modifier(Modif) {}
47
48const GlobalValue *ARMConstantPoolValue::getGV() const {
49  return dyn_cast_or_null<GlobalValue>(CVal);
50}
51
52const BlockAddress *ARMConstantPoolValue::getBlockAddress() const {
53  return dyn_cast_or_null<BlockAddress>(CVal);
54}
55
56int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
57                                                    unsigned Alignment) {
58  unsigned AlignMask = Alignment - 1;
59  const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
60  for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
61    if (Constants[i].isMachineConstantPoolEntry() &&
62        (Constants[i].getAlignment() & AlignMask) == 0) {
63      ARMConstantPoolValue *CPV =
64        (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
65      if (CPV->CVal == CVal &&
66          CPV->LabelId == LabelId &&
67          CPV->PCAdjust == PCAdjust &&
68          (CPV->S == S || strcmp(CPV->S, S) == 0) &&
69          (CPV->Modifier == Modifier || strcmp(CPV->Modifier, Modifier) == 0))
70        return i;
71    }
72  }
73
74  return -1;
75}
76
77ARMConstantPoolValue::~ARMConstantPoolValue() {
78  free((void*)S);
79}
80
81void
82ARMConstantPoolValue::AddSelectionDAGCSEId(FoldingSetNodeID &ID) {
83  ID.AddPointer(CVal);
84  ID.AddPointer(S);
85  ID.AddInteger(LabelId);
86  ID.AddInteger(PCAdjust);
87}
88
89bool
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
106void ARMConstantPoolValue::dump() const {
107  errs() << "  " << *this;
108}
109
110
111void ARMConstantPoolValue::print(raw_ostream &O) const {
112  if (CVal)
113    O << CVal->getName();
114  else
115    O << S;
116  if (Modifier) O << "(" << Modifier << ")";
117  if (PCAdjust != 0) {
118    O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
119    if (AddCurrentAddress) O << "-.";
120    O << ")";
121  }
122}
123