blob: 5507646878cbc34cc9f740eeb799b23d17e23604 [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"
Dan Gohmana54cf172008-07-11 22:44:52 +000021#include <map>
Chris Lattneredfb72c2008-08-24 20:37:32 +000022using namespace llvm;
Dan Gohman69de1932008-02-06 22:27:42 +000023
Chris Lattneredfb72c2008-08-24 20:37:32 +000024static ManagedStatic<PseudoSourceValue[4]> PSVs;
Dan Gohman69de1932008-02-06 22:27:42 +000025
Chris Lattneredfb72c2008-08-24 20:37:32 +000026const PseudoSourceValue *PseudoSourceValue::getStack()
27{ return &(*PSVs)[0]; }
28const PseudoSourceValue *PseudoSourceValue::getGOT()
29{ return &(*PSVs)[1]; }
30const PseudoSourceValue *PseudoSourceValue::getJumpTable()
31{ return &(*PSVs)[2]; }
32const PseudoSourceValue *PseudoSourceValue::getConstantPool()
33{ return &(*PSVs)[3]; }
Dan Gohman69de1932008-02-06 22:27:42 +000034
Chris Lattneredfb72c2008-08-24 20:37:32 +000035static const char *const PSVNames[] = {
36 "Stack",
37 "GOT",
38 "JumpTable",
39 "ConstantPool"
40};
Dan Gohman69de1932008-02-06 22:27:42 +000041
Owen Anderson1d0be152009-08-13 21:58:54 +000042// FIXME: THIS IS A HACK!!!!
43// Eventually these should be uniqued on LLVMContext rather than in a managed
44// static. For now, we can safely use the global context for the time being to
45// squeak by.
Chris Lattneredfb72c2008-08-24 20:37:32 +000046PseudoSourceValue::PseudoSourceValue() :
Duncan Sandsac53a0b2009-10-06 15:40:36 +000047 Value(Type::getInt8PtrTy(getGlobalContext()),
Owen Anderson1d0be152009-08-13 21:58:54 +000048 PseudoSourceValueVal) {}
Dan Gohman69de1932008-02-06 22:27:42 +000049
Dan Gohmancd26ec52009-09-23 01:33:16 +000050void PseudoSourceValue::printCustom(raw_ostream &O) const {
51 O << PSVNames[this - *PSVs];
Chris Lattneredfb72c2008-08-24 20:37:32 +000052}
Dan Gohmana54cf172008-07-11 22:44:52 +000053
Chris Lattneredfb72c2008-08-24 20:37:32 +000054namespace {
Evan Cheng65531552009-10-17 07:53:04 +000055 /// FixedStackPseudoSourceValue - A specialized PseudoSourceValue
56 /// for holding FixedStack values, which must include a frame
Dan Gohmana54cf172008-07-11 22:44:52 +000057 /// index.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class FixedStackPseudoSourceValue : public PseudoSourceValue {
Dan Gohmana54cf172008-07-11 22:44:52 +000059 const int FI;
60 public:
Evan Cheng65531552009-10-17 07:53:04 +000061 explicit FixedStackPseudoSourceValue(int fi) : FI(fi) {}
Dan Gohman6d69ba82008-07-25 00:02:30 +000062
63 virtual bool isConstant(const MachineFrameInfo *MFI) const;
64
Evan Cheng38bdfc62009-10-18 19:58:47 +000065 virtual bool isAliased(const MachineFrameInfo *MFI) const;
Evan Chengff89dcb2009-10-18 18:16:27 +000066
Evan Chengf57b1ba2009-11-01 23:50:04 +000067 virtual bool mayAlias(const MachineFrameInfo *) const;
68
Dan Gohmancd26ec52009-09-23 01:33:16 +000069 virtual void printCustom(raw_ostream &OS) const {
Evan Cheng65531552009-10-17 07:53:04 +000070 OS << "FixedStack" << FI;
Evan Cheng40ab1642008-08-24 18:51:20 +000071 }
Dan Gohmana54cf172008-07-11 22:44:52 +000072 };
Chris Lattneredfb72c2008-08-24 20:37:32 +000073}
Dan Gohmana54cf172008-07-11 22:44:52 +000074
Chris Lattneredfb72c2008-08-24 20:37:32 +000075static ManagedStatic<std::map<int, const PseudoSourceValue *> > FSValues;
Dan Gohmana54cf172008-07-11 22:44:52 +000076
Evan Cheng65531552009-10-17 07:53:04 +000077const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
Chris Lattneredfb72c2008-08-24 20:37:32 +000078 const PseudoSourceValue *&V = (*FSValues)[FI];
79 if (!V)
Evan Cheng65531552009-10-17 07:53:04 +000080 V = new FixedStackPseudoSourceValue(FI);
Chris Lattneredfb72c2008-08-24 20:37:32 +000081 return V;
82}
Dan Gohman6d69ba82008-07-25 00:02:30 +000083
Chris Lattneredfb72c2008-08-24 20:37:32 +000084bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
85 if (this == getStack())
Dan Gohman6d69ba82008-07-25 00:02:30 +000086 return false;
Chris Lattneredfb72c2008-08-24 20:37:32 +000087 if (this == getGOT() ||
88 this == getConstantPool() ||
89 this == getJumpTable())
90 return true;
Torok Edwinc23197a2009-07-14 16:55:14 +000091 llvm_unreachable("Unknown PseudoSourceValue!");
Chris Lattneredfb72c2008-08-24 20:37:32 +000092 return false;
93}
Dan Gohman6d69ba82008-07-25 00:02:30 +000094
Evan Cheng38bdfc62009-10-18 19:58:47 +000095bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Chengff89dcb2009-10-18 18:16:27 +000096 if (this == getStack() ||
97 this == getGOT() ||
98 this == getConstantPool() ||
99 this == getJumpTable())
100 return false;
101 llvm_unreachable("Unknown PseudoSourceValue!");
102 return true;
103}
104
Evan Chengf57b1ba2009-11-01 23:50:04 +0000105bool PseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
106 if (this == getGOT() ||
107 this == getConstantPool() ||
108 this == getJumpTable())
109 return false;
110 return true;
111}
112
Evan Cheng65531552009-10-17 07:53:04 +0000113bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
Chris Lattneredfb72c2008-08-24 20:37:32 +0000114 return MFI && MFI->isImmutableObjectIndex(FI);
Dan Gohman69de1932008-02-06 22:27:42 +0000115}
Evan Chengff89dcb2009-10-18 18:16:27 +0000116
Evan Cheng38bdfc62009-10-18 19:58:47 +0000117bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Chengff89dcb2009-10-18 18:16:27 +0000118 // Negative frame indices are used for special things that don't
119 // appear in LLVM IR. Non-negative indices may be used for things
120 // like static allocas.
Evan Cheng38bdfc62009-10-18 19:58:47 +0000121 if (!MFI)
122 return FI >= 0;
123 // Spill slots should not alias others.
124 return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
Evan Chengff89dcb2009-10-18 18:16:27 +0000125}
Evan Chengf57b1ba2009-11-01 23:50:04 +0000126
127bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
128 if (!MFI)
129 return true;
130 // Spill slots will not alias any LLVM IR value.
131 return !MFI->isSpillSlotObjectIndex(FI);
132}