Reformat PseudoSourceValue.cpp and PseudoSourceValue.h. NFC.
[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 PSVs[4];
29   sys::Mutex Lock; // Guards FSValues, but not the values inside it.
30   std::map<int, const PseudoSourceValue *> FSValues;
31
32   PSVGlobalsTy() : PSVs() {}
33   ~PSVGlobalsTy() {
34     for (std::map<int, const PseudoSourceValue *>::iterator
35              I = FSValues.begin(),
36              E = FSValues.end();
37          I != E; ++I) {
38       delete I->second;
39     }
40   }
41 };
42
43 static ManagedStatic<PSVGlobalsTy> PSVGlobals;
44
45 } // anonymous namespace
46
47 const PseudoSourceValue *PseudoSourceValue::getStack() {
48   return &PSVGlobals->PSVs[0];
49 }
50 const PseudoSourceValue *PseudoSourceValue::getGOT() {
51   return &PSVGlobals->PSVs[1];
52 }
53 const PseudoSourceValue *PseudoSourceValue::getJumpTable() {
54   return &PSVGlobals->PSVs[2];
55 }
56 const PseudoSourceValue *PseudoSourceValue::getConstantPool() {
57   return &PSVGlobals->PSVs[3];
58 }
59
60 static const char *const PSVNames[] = {"Stack", "GOT", "JumpTable",
61                                        "ConstantPool"};
62
63 PseudoSourceValue::PseudoSourceValue(bool isFixed) : isFixed(isFixed) {}
64
65 PseudoSourceValue::~PseudoSourceValue() {}
66
67 void PseudoSourceValue::printCustom(raw_ostream &O) const {
68   O << PSVNames[this - PSVGlobals->PSVs];
69 }
70
71 const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
72   PSVGlobalsTy &PG = *PSVGlobals;
73   sys::ScopedLock locked(PG.Lock);
74   const PseudoSourceValue *&V = PG.FSValues[FI];
75   if (!V)
76     V = new FixedStackPseudoSourceValue(FI);
77   return V;
78 }
79
80 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
81   if (this == getStack())
82     return false;
83   if (this == getGOT() || this == getConstantPool() || this == getJumpTable())
84     return true;
85   llvm_unreachable("Unknown PseudoSourceValue!");
86 }
87
88 bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
89   if (this == getStack() || this == getGOT() || this == getConstantPool() ||
90       this == getJumpTable())
91     return false;
92   llvm_unreachable("Unknown PseudoSourceValue!");
93 }
94
95 bool PseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
96   if (this == getGOT() || this == getConstantPool() || this == getJumpTable())
97     return false;
98   return true;
99 }
100
101 bool FixedStackPseudoSourceValue::isConstant(
102     const MachineFrameInfo *MFI) const {
103   return MFI && MFI->isImmutableObjectIndex(FI);
104 }
105
106 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
107   if (!MFI)
108     return true;
109   return MFI->isAliasedObjectIndex(FI);
110 }
111
112 bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
113   if (!MFI)
114     return true;
115   // Spill slots will not alias any LLVM IR value.
116   return !MFI->isSpillSlotObjectIndex(FI);
117 }
118
119 void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
120   OS << "FixedStack" << FI;
121 }