2b5470e8ce78cf4526b2fa5bfa76f0cb205c8eea
[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/MachineFrameInfo.h"
15 #include "llvm/CodeGen/PseudoSourceValue.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <map>
21 using namespace llvm;
22
23 static ManagedStatic<PseudoSourceValue[4]> PSVs;
24
25 const PseudoSourceValue *PseudoSourceValue::getStack()
26 { return &(*PSVs)[0]; }
27 const PseudoSourceValue *PseudoSourceValue::getGOT()
28 { return &(*PSVs)[1]; }
29 const PseudoSourceValue *PseudoSourceValue::getJumpTable()
30 { return &(*PSVs)[2]; }
31 const PseudoSourceValue *PseudoSourceValue::getConstantPool()
32 { return &(*PSVs)[3]; }
33
34 static const char *const PSVNames[] = {
35   "Stack",
36   "GOT",
37   "JumpTable",
38   "ConstantPool"
39 };
40
41 // FIXME: THIS IS A HACK!!!!
42 // Eventually these should be uniqued on LLVMContext rather than in a managed
43 // static.  For now, we can safely use the global context for the time being to
44 // squeak by.
45 PseudoSourceValue::PseudoSourceValue() :
46   Value(Type::getInt8PtrTy(getGlobalContext()),
47         PseudoSourceValueVal) {}
48
49 void PseudoSourceValue::printCustom(raw_ostream &O) const {
50   O << PSVNames[this - *PSVs];
51 }
52
53 namespace {
54   /// FixedStackPseudoSourceValue - A specialized PseudoSourceValue
55   /// for holding FixedStack values, which must include a frame
56   /// index.
57   class FixedStackPseudoSourceValue : public PseudoSourceValue {
58     const int FI;
59   public:
60     explicit FixedStackPseudoSourceValue(int fi) : FI(fi) {}
61
62     virtual bool isConstant(const MachineFrameInfo *MFI) const;
63
64     virtual bool isAliased(const MachineFrameInfo *MFI) const;
65
66     virtual void printCustom(raw_ostream &OS) const {
67       OS << "FixedStack" << FI;
68     }
69   };
70 }
71
72 static ManagedStatic<std::map<int, const PseudoSourceValue *> > FSValues;
73
74 const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
75   const PseudoSourceValue *&V = (*FSValues)[FI];
76   if (!V)
77     V = new FixedStackPseudoSourceValue(FI);
78   return V;
79 }
80
81 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
82   if (this == getStack())
83     return false;
84   if (this == getGOT() ||
85       this == getConstantPool() ||
86       this == getJumpTable())
87     return true;
88   llvm_unreachable("Unknown PseudoSourceValue!");
89   return false;
90 }
91
92 bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
93   if (this == getStack() ||
94       this == getGOT() ||
95       this == getConstantPool() ||
96       this == getJumpTable())
97     return false;
98   llvm_unreachable("Unknown PseudoSourceValue!");
99   return true;
100 }
101
102 bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
103   return MFI && MFI->isImmutableObjectIndex(FI);
104 }
105
106 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
107   // Negative frame indices are used for special things that don't
108   // appear in LLVM IR. Non-negative indices may be used for things
109   // like static allocas.
110   if (!MFI)
111     return FI >= 0;
112   // Spill slots should not alias others.
113   return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
114 }