PseudoSourceValue: Introduce a 'PSVKind' enumerator.
[oota-llvm.git] / lib / CodeGen / PseudoSourceValue.cpp
1 //===-- llvm/CodeGen/PseudoSourceValue.cpp ----------------------*- 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 PseudoSourceValue class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/PseudoSourceValue.h"
15 #include "llvm/CodeGen/MachineFrameInfo.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/Mutex.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <map>
23 using namespace llvm;
24
25 namespace {
26 struct PSVGlobalsTy {
27   // PseudoSourceValues are immutable so don't need locking.
28   const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
29   sys::Mutex Lock; // Guards FSValues, but not the values inside it.
30   std::map<int, const PseudoSourceValue *> FSValues;
31
32   PSVGlobalsTy()
33       : StackPSV(PseudoSourceValue::Stack), GOTPSV(PseudoSourceValue::GOT),
34         JumpTablePSV(PseudoSourceValue::JumpTable),
35         ConstantPoolPSV(PseudoSourceValue::ConstantPool) {}
36   ~PSVGlobalsTy() {
37     for (std::map<int, const PseudoSourceValue *>::iterator
38              I = FSValues.begin(),
39              E = FSValues.end();
40          I != E; ++I) {
41       delete I->second;
42     }
43   }
44 };
45
46 static ManagedStatic<PSVGlobalsTy> PSVGlobals;
47
48 } // anonymous namespace
49
50 const PseudoSourceValue *PseudoSourceValue::getStack() {
51   return &PSVGlobals->StackPSV;
52 }
53 const PseudoSourceValue *PseudoSourceValue::getGOT() {
54   return &PSVGlobals->GOTPSV;
55 }
56 const PseudoSourceValue *PseudoSourceValue::getJumpTable() {
57   return &PSVGlobals->JumpTablePSV;
58 }
59 const PseudoSourceValue *PseudoSourceValue::getConstantPool() {
60   return &PSVGlobals->ConstantPoolPSV;
61 }
62
63 static const char *const PSVNames[] = {
64     "Stack", "GOT", "JumpTable", "ConstantPool", "FixedStack", "MipsCallEntry"};
65
66 PseudoSourceValue::PseudoSourceValue(PSVKind Kind) : Kind(Kind) {}
67
68 PseudoSourceValue::~PseudoSourceValue() {}
69
70 void PseudoSourceValue::printCustom(raw_ostream &O) const {
71   O << PSVNames[Kind];
72 }
73
74 const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
75   PSVGlobalsTy &PG = *PSVGlobals;
76   sys::ScopedLock locked(PG.Lock);
77   const PseudoSourceValue *&V = PG.FSValues[FI];
78   if (!V)
79     V = new FixedStackPseudoSourceValue(FI);
80   return V;
81 }
82
83 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
84   if (isStack())
85     return false;
86   if (isGOT() || isConstantPool() || isJumpTable())
87     return true;
88   llvm_unreachable("Unknown PseudoSourceValue!");
89 }
90
91 bool PseudoSourceValue::isAliased(const MachineFrameInfo *) const {
92   if (isStack() || isGOT() || isConstantPool() || isJumpTable())
93     return false;
94   llvm_unreachable("Unknown PseudoSourceValue!");
95 }
96
97 bool PseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
98   if (isGOT() || isConstantPool() || isJumpTable())
99     return false;
100   return true;
101 }
102
103 bool FixedStackPseudoSourceValue::isConstant(
104     const MachineFrameInfo *MFI) const {
105   return MFI && MFI->isImmutableObjectIndex(FI);
106 }
107
108 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
109   if (!MFI)
110     return true;
111   return MFI->isAliasedObjectIndex(FI);
112 }
113
114 bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
115   if (!MFI)
116     return true;
117   // Spill slots will not alias any LLVM IR value.
118   return !MFI->isSpillSlotObjectIndex(FI);
119 }
120
121 void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
122   OS << "FixedStack" << FI;
123 }