blob: 5e86e5a9447e31afc8effd9877c0c1bce07af828 [file] [log] [blame]
Dan Gohman69de1932008-02-06 22:27:42 +00001//===-- 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 Gohman6d69ba82008-07-25 00:02:30 +000014#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000015#include "llvm/CodeGen/PseudoSourceValue.h"
16#include "llvm/DerivedTypes.h"
Chris Lattner75c478a2009-10-27 17:02:08 +000017#include "llvm/LLVMContext.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Dan Gohman69de1932008-02-06 22:27:42 +000019#include "llvm/Support/ManagedStatic.h"
Evan Cheng40ab1642008-08-24 18:51:20 +000020#include "llvm/Support/raw_ostream.h"
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000021#include "llvm/System/Mutex.h"
Dan Gohmana54cf172008-07-11 22:44:52 +000022#include <map>
Chris Lattneredfb72c2008-08-24 20:37:32 +000023using namespace llvm;
Dan Gohman69de1932008-02-06 22:27:42 +000024
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000025namespace {
26struct 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
41static ManagedStatic<PSVGlobalsTy> PSVGlobals;
42
43} // anonymous namespace
Dan Gohman69de1932008-02-06 22:27:42 +000044
Chris Lattneredfb72c2008-08-24 20:37:32 +000045const PseudoSourceValue *PseudoSourceValue::getStack()
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000046{ return &PSVGlobals->PSVs[0]; }
Chris Lattneredfb72c2008-08-24 20:37:32 +000047const PseudoSourceValue *PseudoSourceValue::getGOT()
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000048{ return &PSVGlobals->PSVs[1]; }
Chris Lattneredfb72c2008-08-24 20:37:32 +000049const PseudoSourceValue *PseudoSourceValue::getJumpTable()
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000050{ return &PSVGlobals->PSVs[2]; }
Chris Lattneredfb72c2008-08-24 20:37:32 +000051const PseudoSourceValue *PseudoSourceValue::getConstantPool()
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000052{ return &PSVGlobals->PSVs[3]; }
Dan Gohman69de1932008-02-06 22:27:42 +000053
Chris Lattneredfb72c2008-08-24 20:37:32 +000054static const char *const PSVNames[] = {
55 "Stack",
56 "GOT",
57 "JumpTable",
58 "ConstantPool"
59};
Dan Gohman69de1932008-02-06 22:27:42 +000060
Owen Anderson1d0be152009-08-13 21:58:54 +000061// 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 Greenecf626322009-11-12 20:25:07 +000065PseudoSourceValue::PseudoSourceValue(enum ValueTy Subclass) :
Duncan Sandsac53a0b2009-10-06 15:40:36 +000066 Value(Type::getInt8PtrTy(getGlobalContext()),
David Greenecf626322009-11-12 20:25:07 +000067 Subclass) {}
Dan Gohman69de1932008-02-06 22:27:42 +000068
Dan Gohmancd26ec52009-09-23 01:33:16 +000069void PseudoSourceValue::printCustom(raw_ostream &O) const {
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000070 O << PSVNames[this - PSVGlobals->PSVs];
Chris Lattneredfb72c2008-08-24 20:37:32 +000071}
Dan Gohmana54cf172008-07-11 22:44:52 +000072
Evan Cheng65531552009-10-17 07:53:04 +000073const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000074 PSVGlobalsTy &PG = *PSVGlobals;
75 sys::ScopedLock locked(PG.Lock);
76 const PseudoSourceValue *&V = PG.FSValues[FI];
Chris Lattneredfb72c2008-08-24 20:37:32 +000077 if (!V)
Evan Cheng65531552009-10-17 07:53:04 +000078 V = new FixedStackPseudoSourceValue(FI);
Chris Lattneredfb72c2008-08-24 20:37:32 +000079 return V;
80}
Dan Gohman6d69ba82008-07-25 00:02:30 +000081
Chris Lattneredfb72c2008-08-24 20:37:32 +000082bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
83 if (this == getStack())
Dan Gohman6d69ba82008-07-25 00:02:30 +000084 return false;
Chris Lattneredfb72c2008-08-24 20:37:32 +000085 if (this == getGOT() ||
86 this == getConstantPool() ||
87 this == getJumpTable())
88 return true;
Torok Edwinc23197a2009-07-14 16:55:14 +000089 llvm_unreachable("Unknown PseudoSourceValue!");
Chris Lattneredfb72c2008-08-24 20:37:32 +000090 return false;
91}
Dan Gohman6d69ba82008-07-25 00:02:30 +000092
Evan Cheng38bdfc62009-10-18 19:58:47 +000093bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Chengff89dcb2009-10-18 18:16:27 +000094 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 Chengf57b1ba2009-11-01 23:50:04 +0000103bool 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 Cheng65531552009-10-17 07:53:04 +0000111bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
Chris Lattneredfb72c2008-08-24 20:37:32 +0000112 return MFI && MFI->isImmutableObjectIndex(FI);
Dan Gohman69de1932008-02-06 22:27:42 +0000113}
Evan Chengff89dcb2009-10-18 18:16:27 +0000114
Evan Cheng38bdfc62009-10-18 19:58:47 +0000115bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Chengff89dcb2009-10-18 18:16:27 +0000116 // 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 Cheng38bdfc62009-10-18 19:58:47 +0000119 if (!MFI)
120 return FI >= 0;
121 // Spill slots should not alias others.
122 return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000123}
Evan Chengf57b1ba2009-11-01 23:50:04 +0000124
125bool 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 Greeneb3bc1152009-11-12 21:49:55 +0000131
132void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
133 OS << "FixedStack" << FI;
134}