Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 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 | |
Dan Gohman | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 75c478a | 2009-10-27 17:02:08 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
Evan Cheng | 40ab164 | 2008-08-24 18:51:20 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 21 | #include "llvm/System/Mutex.h" |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 22 | #include <map> |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 24 | |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 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(), E = FSValues.end(); I != E; ++I) { |
| 36 | delete I->second; |
| 37 | } |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | static ManagedStatic<PSVGlobalsTy> PSVGlobals; |
| 42 | |
| 43 | } // anonymous namespace |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 44 | |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 45 | const PseudoSourceValue *PseudoSourceValue::getStack() |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 46 | { return &PSVGlobals->PSVs[0]; } |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 47 | const PseudoSourceValue *PseudoSourceValue::getGOT() |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 48 | { return &PSVGlobals->PSVs[1]; } |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 49 | const PseudoSourceValue *PseudoSourceValue::getJumpTable() |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 50 | { return &PSVGlobals->PSVs[2]; } |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 51 | const PseudoSourceValue *PseudoSourceValue::getConstantPool() |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 52 | { return &PSVGlobals->PSVs[3]; } |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 53 | |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 54 | static const char *const PSVNames[] = { |
| 55 | "Stack", |
| 56 | "GOT", |
| 57 | "JumpTable", |
| 58 | "ConstantPool" |
| 59 | }; |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 60 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 61 | // FIXME: THIS IS A HACK!!!! |
| 62 | // Eventually these should be uniqued on LLVMContext rather than in a managed |
| 63 | // static. For now, we can safely use the global context for the time being to |
| 64 | // squeak by. |
David Greene | cf62632 | 2009-11-12 20:25:07 +0000 | [diff] [blame] | 65 | PseudoSourceValue::PseudoSourceValue(enum ValueTy Subclass) : |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 66 | Value(Type::getInt8PtrTy(getGlobalContext()), |
David Greene | cf62632 | 2009-11-12 20:25:07 +0000 | [diff] [blame] | 67 | Subclass) {} |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 68 | |
Dan Gohman | cd26ec5 | 2009-09-23 01:33:16 +0000 | [diff] [blame] | 69 | void PseudoSourceValue::printCustom(raw_ostream &O) const { |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 70 | O << PSVNames[this - PSVGlobals->PSVs]; |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 71 | } |
Dan Gohman | a54cf17 | 2008-07-11 22:44:52 +0000 | [diff] [blame] | 72 | |
Evan Cheng | 6553155 | 2009-10-17 07:53:04 +0000 | [diff] [blame] | 73 | const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) { |
Jeffrey Yasskin | e8cfa63 | 2010-03-04 22:15:01 +0000 | [diff] [blame] | 74 | PSVGlobalsTy &PG = *PSVGlobals; |
| 75 | sys::ScopedLock locked(PG.Lock); |
| 76 | const PseudoSourceValue *&V = PG.FSValues[FI]; |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 77 | if (!V) |
Evan Cheng | 6553155 | 2009-10-17 07:53:04 +0000 | [diff] [blame] | 78 | V = new FixedStackPseudoSourceValue(FI); |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 79 | return V; |
| 80 | } |
Dan Gohman | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 81 | |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 82 | bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const { |
| 83 | if (this == getStack()) |
Dan Gohman | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 84 | return false; |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 85 | if (this == getGOT() || |
| 86 | this == getConstantPool() || |
| 87 | this == getJumpTable()) |
| 88 | return true; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 89 | llvm_unreachable("Unknown PseudoSourceValue!"); |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 90 | return false; |
| 91 | } |
Dan Gohman | 6d69ba8 | 2008-07-25 00:02:30 +0000 | [diff] [blame] | 92 | |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 93 | bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const { |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 94 | if (this == getStack() || |
| 95 | this == getGOT() || |
| 96 | this == getConstantPool() || |
| 97 | this == getJumpTable()) |
| 98 | return false; |
| 99 | llvm_unreachable("Unknown PseudoSourceValue!"); |
| 100 | return true; |
| 101 | } |
| 102 | |
Evan Cheng | f57b1ba | 2009-11-01 23:50:04 +0000 | [diff] [blame] | 103 | bool PseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const { |
| 104 | if (this == getGOT() || |
| 105 | this == getConstantPool() || |
| 106 | this == getJumpTable()) |
| 107 | return false; |
| 108 | return true; |
| 109 | } |
| 110 | |
Evan Cheng | 6553155 | 2009-10-17 07:53:04 +0000 | [diff] [blame] | 111 | bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{ |
Chris Lattner | edfb72c | 2008-08-24 20:37:32 +0000 | [diff] [blame] | 112 | return MFI && MFI->isImmutableObjectIndex(FI); |
Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 113 | } |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 114 | |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 115 | bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const { |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 116 | // Negative frame indices are used for special things that don't |
| 117 | // appear in LLVM IR. Non-negative indices may be used for things |
| 118 | // like static allocas. |
Evan Cheng | 38bdfc6 | 2009-10-18 19:58:47 +0000 | [diff] [blame] | 119 | if (!MFI) |
| 120 | return FI >= 0; |
| 121 | // Spill slots should not alias others. |
| 122 | return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI); |
Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 123 | } |
Evan Cheng | f57b1ba | 2009-11-01 23:50:04 +0000 | [diff] [blame] | 124 | |
| 125 | bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const { |
| 126 | if (!MFI) |
| 127 | return true; |
| 128 | // Spill slots will not alias any LLVM IR value. |
| 129 | return !MFI->isSpillSlotObjectIndex(FI); |
| 130 | } |
David Greene | b3bc115 | 2009-11-12 21:49:55 +0000 | [diff] [blame] | 131 | |
| 132 | void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const { |
| 133 | OS << "FixedStack" << FI; |
| 134 | } |