blob: 12b2c902f8d33d128b52eb5a0a17a932e2924d29 [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
14#include "llvm/CodeGen/PseudoSourceValue.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000016#include "llvm/IR/DerivedTypes.h"
17#include "llvm/IR/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"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000020#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/Support/raw_ostream.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
Stephen Hinesdce4a402014-05-29 02:49:00 -070061PseudoSourceValue::PseudoSourceValue(bool isFixed) : isFixed(isFixed) {}
62
63PseudoSourceValue::~PseudoSourceValue() {}
Dan Gohman69de1932008-02-06 22:27:42 +000064
Dan Gohmancd26ec52009-09-23 01:33:16 +000065void PseudoSourceValue::printCustom(raw_ostream &O) const {
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000066 O << PSVNames[this - PSVGlobals->PSVs];
Chris Lattneredfb72c2008-08-24 20:37:32 +000067}
Dan Gohmana54cf172008-07-11 22:44:52 +000068
Evan Cheng65531552009-10-17 07:53:04 +000069const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
Jeffrey Yasskine8cfa632010-03-04 22:15:01 +000070 PSVGlobalsTy &PG = *PSVGlobals;
71 sys::ScopedLock locked(PG.Lock);
72 const PseudoSourceValue *&V = PG.FSValues[FI];
Chris Lattneredfb72c2008-08-24 20:37:32 +000073 if (!V)
Evan Cheng65531552009-10-17 07:53:04 +000074 V = new FixedStackPseudoSourceValue(FI);
Chris Lattneredfb72c2008-08-24 20:37:32 +000075 return V;
76}
Dan Gohman6d69ba82008-07-25 00:02:30 +000077
Chris Lattneredfb72c2008-08-24 20:37:32 +000078bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
79 if (this == getStack())
Dan Gohman6d69ba82008-07-25 00:02:30 +000080 return false;
Chris Lattneredfb72c2008-08-24 20:37:32 +000081 if (this == getGOT() ||
82 this == getConstantPool() ||
83 this == getJumpTable())
84 return true;
Torok Edwinc23197a2009-07-14 16:55:14 +000085 llvm_unreachable("Unknown PseudoSourceValue!");
Chris Lattneredfb72c2008-08-24 20:37:32 +000086}
Dan Gohman6d69ba82008-07-25 00:02:30 +000087
Evan Cheng38bdfc62009-10-18 19:58:47 +000088bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Chengff89dcb2009-10-18 18:16:27 +000089 if (this == getStack() ||
90 this == getGOT() ||
91 this == getConstantPool() ||
92 this == getJumpTable())
93 return false;
94 llvm_unreachable("Unknown PseudoSourceValue!");
Evan Chengff89dcb2009-10-18 18:16:27 +000095}
96
Evan Chengf57b1ba2009-11-01 23:50:04 +000097bool PseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
98 if (this == getGOT() ||
99 this == getConstantPool() ||
100 this == getJumpTable())
101 return false;
102 return true;
103}
104
Evan Cheng65531552009-10-17 07:53:04 +0000105bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
Chris Lattneredfb72c2008-08-24 20:37:32 +0000106 return MFI && MFI->isImmutableObjectIndex(FI);
Dan Gohman69de1932008-02-06 22:27:42 +0000107}
Evan Chengff89dcb2009-10-18 18:16:27 +0000108
Evan Cheng38bdfc62009-10-18 19:58:47 +0000109bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Chengff89dcb2009-10-18 18:16:27 +0000110 // Negative frame indices are used for special things that don't
111 // appear in LLVM IR. Non-negative indices may be used for things
112 // like static allocas.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000113 if (!MFI)
114 return FI >= 0;
115 // Spill slots should not alias others.
116 return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000117}
Evan Chengf57b1ba2009-11-01 23:50:04 +0000118
119bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
120 if (!MFI)
121 return true;
122 // Spill slots will not alias any LLVM IR value.
123 return !MFI->isSpillSlotObjectIndex(FI);
124}
David Greeneb3bc1152009-11-12 21:49:55 +0000125
126void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
127 OS << "FixedStack" << FI;
128}