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