ARMConstantPoolValue.cpp revision 198090
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/GlobalValue.h"
17#include "llvm/Type.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cstdlib>
20using namespace llvm;
21
22ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv, unsigned id,
23                                           ARMCP::ARMCPKind K,
24                                           unsigned char PCAdj,
25                                           const char *Modif,
26                                           bool AddCA)
27  : MachineConstantPoolValue((const Type*)gv->getType()),
28    GV(gv), S(NULL), LabelId(id), Kind(K), PCAdjust(PCAdj),
29    Modifier(Modif), AddCurrentAddress(AddCA) {}
30
31ARMConstantPoolValue::ARMConstantPoolValue(LLVMContext &C,
32                                           const char *s, unsigned id,
33                                           unsigned char PCAdj,
34                                           const char *Modif,
35                                           bool AddCA)
36  : MachineConstantPoolValue((const Type*)Type::getInt32Ty(C)),
37    GV(NULL), S(strdup(s)), LabelId(id), Kind(ARMCP::CPValue), PCAdjust(PCAdj),
38    Modifier(Modif), AddCurrentAddress(AddCA) {}
39
40ARMConstantPoolValue::ARMConstantPoolValue(GlobalValue *gv, const char *Modif)
41  : MachineConstantPoolValue((const Type*)Type::getInt32Ty(gv->getContext())),
42    GV(gv), S(NULL), LabelId(0), Kind(ARMCP::CPValue), PCAdjust(0),
43    Modifier(Modif) {}
44
45int ARMConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
46                                                    unsigned Alignment) {
47  unsigned AlignMask = Alignment - 1;
48  const std::vector<MachineConstantPoolEntry> Constants = CP->getConstants();
49  for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
50    if (Constants[i].isMachineConstantPoolEntry() &&
51        (Constants[i].getAlignment() & AlignMask) == 0) {
52      ARMConstantPoolValue *CPV =
53        (ARMConstantPoolValue *)Constants[i].Val.MachineCPVal;
54      if (CPV->GV == GV &&
55          CPV->S == S &&
56          CPV->LabelId == LabelId &&
57          CPV->PCAdjust == PCAdjust)
58        return i;
59    }
60  }
61
62  return -1;
63}
64
65ARMConstantPoolValue::~ARMConstantPoolValue() {
66  free((void*)S);
67}
68
69void
70ARMConstantPoolValue::AddSelectionDAGCSEId(FoldingSetNodeID &ID) {
71  ID.AddPointer(GV);
72  ID.AddPointer(S);
73  ID.AddInteger(LabelId);
74  ID.AddInteger(PCAdjust);
75}
76
77void ARMConstantPoolValue::dump() const {
78  errs() << "  " << *this;
79}
80
81
82void ARMConstantPoolValue::print(raw_ostream &O) const {
83  if (GV)
84    O << GV->getName();
85  else
86    O << S;
87  if (Modifier) O << "(" << Modifier << ")";
88  if (PCAdjust != 0) {
89    O << "-(LPC" << LabelId << "+" << (unsigned)PCAdjust;
90    if (AddCurrentAddress) O << "-.";
91    O << ")";
92  }
93}
94