blob: 2b5470e8ce78cf4526b2fa5bfa76f0cb205c8eea [file] [log] [blame]
Dan Gohman12a9c082008-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 Gohmane3427532008-07-25 00:02:30 +000014#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000015#include "llvm/CodeGen/PseudoSourceValue.h"
16#include "llvm/DerivedTypes.h"
Edwin Török675d5622009-07-11 20:10:48 +000017#include "llvm/Support/ErrorHandling.h"
Dan Gohman12a9c082008-02-06 22:27:42 +000018#include "llvm/Support/ManagedStatic.h"
Evan Cheng2a65caa2008-08-24 18:51:20 +000019#include "llvm/Support/raw_ostream.h"
Dan Gohman1fc34bc2008-07-11 22:44:52 +000020#include <map>
Chris Lattner24ae2a92008-08-24 20:37:32 +000021using namespace llvm;
Dan Gohman12a9c082008-02-06 22:27:42 +000022
Chris Lattner24ae2a92008-08-24 20:37:32 +000023static ManagedStatic<PseudoSourceValue[4]> PSVs;
Dan Gohman12a9c082008-02-06 22:27:42 +000024
Chris Lattner24ae2a92008-08-24 20:37:32 +000025const PseudoSourceValue *PseudoSourceValue::getStack()
26{ return &(*PSVs)[0]; }
27const PseudoSourceValue *PseudoSourceValue::getGOT()
28{ return &(*PSVs)[1]; }
29const PseudoSourceValue *PseudoSourceValue::getJumpTable()
30{ return &(*PSVs)[2]; }
31const PseudoSourceValue *PseudoSourceValue::getConstantPool()
32{ return &(*PSVs)[3]; }
Dan Gohman12a9c082008-02-06 22:27:42 +000033
Chris Lattner24ae2a92008-08-24 20:37:32 +000034static const char *const PSVNames[] = {
35 "Stack",
36 "GOT",
37 "JumpTable",
38 "ConstantPool"
39};
Dan Gohman12a9c082008-02-06 22:27:42 +000040
Owen Anderson35b47072009-08-13 21:58:54 +000041// 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.
Chris Lattner24ae2a92008-08-24 20:37:32 +000045PseudoSourceValue::PseudoSourceValue() :
Duncan Sandsf2519d62009-10-06 15:40:36 +000046 Value(Type::getInt8PtrTy(getGlobalContext()),
Owen Anderson35b47072009-08-13 21:58:54 +000047 PseudoSourceValueVal) {}
Dan Gohman12a9c082008-02-06 22:27:42 +000048
Dan Gohman915d8722009-09-23 01:33:16 +000049void PseudoSourceValue::printCustom(raw_ostream &O) const {
50 O << PSVNames[this - *PSVs];
Chris Lattner24ae2a92008-08-24 20:37:32 +000051}
Dan Gohman1fc34bc2008-07-11 22:44:52 +000052
Chris Lattner24ae2a92008-08-24 20:37:32 +000053namespace {
Evan Cheng1f996572009-10-17 07:53:04 +000054 /// FixedStackPseudoSourceValue - A specialized PseudoSourceValue
55 /// for holding FixedStack values, which must include a frame
Dan Gohman1fc34bc2008-07-11 22:44:52 +000056 /// index.
Nick Lewycky492d06e2009-10-25 06:33:48 +000057 class FixedStackPseudoSourceValue : public PseudoSourceValue {
Dan Gohman1fc34bc2008-07-11 22:44:52 +000058 const int FI;
59 public:
Evan Cheng1f996572009-10-17 07:53:04 +000060 explicit FixedStackPseudoSourceValue(int fi) : FI(fi) {}
Dan Gohmane3427532008-07-25 00:02:30 +000061
62 virtual bool isConstant(const MachineFrameInfo *MFI) const;
63
Evan Chengc9131782009-10-18 19:58:47 +000064 virtual bool isAliased(const MachineFrameInfo *MFI) const;
Evan Cheng174e2cf2009-10-18 18:16:27 +000065
Dan Gohman915d8722009-09-23 01:33:16 +000066 virtual void printCustom(raw_ostream &OS) const {
Evan Cheng1f996572009-10-17 07:53:04 +000067 OS << "FixedStack" << FI;
Evan Cheng2a65caa2008-08-24 18:51:20 +000068 }
Dan Gohman1fc34bc2008-07-11 22:44:52 +000069 };
Chris Lattner24ae2a92008-08-24 20:37:32 +000070}
Dan Gohman1fc34bc2008-07-11 22:44:52 +000071
Chris Lattner24ae2a92008-08-24 20:37:32 +000072static ManagedStatic<std::map<int, const PseudoSourceValue *> > FSValues;
Dan Gohman1fc34bc2008-07-11 22:44:52 +000073
Evan Cheng1f996572009-10-17 07:53:04 +000074const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
Chris Lattner24ae2a92008-08-24 20:37:32 +000075 const PseudoSourceValue *&V = (*FSValues)[FI];
76 if (!V)
Evan Cheng1f996572009-10-17 07:53:04 +000077 V = new FixedStackPseudoSourceValue(FI);
Chris Lattner24ae2a92008-08-24 20:37:32 +000078 return V;
79}
Dan Gohmane3427532008-07-25 00:02:30 +000080
Chris Lattner24ae2a92008-08-24 20:37:32 +000081bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
82 if (this == getStack())
Dan Gohmane3427532008-07-25 00:02:30 +000083 return false;
Chris Lattner24ae2a92008-08-24 20:37:32 +000084 if (this == getGOT() ||
85 this == getConstantPool() ||
86 this == getJumpTable())
87 return true;
Edwin Törökbd448e32009-07-14 16:55:14 +000088 llvm_unreachable("Unknown PseudoSourceValue!");
Chris Lattner24ae2a92008-08-24 20:37:32 +000089 return false;
90}
Dan Gohmane3427532008-07-25 00:02:30 +000091
Evan Chengc9131782009-10-18 19:58:47 +000092bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Cheng174e2cf2009-10-18 18:16:27 +000093 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
Evan Cheng1f996572009-10-17 07:53:04 +0000102bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
Chris Lattner24ae2a92008-08-24 20:37:32 +0000103 return MFI && MFI->isImmutableObjectIndex(FI);
Dan Gohman12a9c082008-02-06 22:27:42 +0000104}
Evan Cheng174e2cf2009-10-18 18:16:27 +0000105
Evan Chengc9131782009-10-18 19:58:47 +0000106bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
Evan Cheng174e2cf2009-10-18 18:16:27 +0000107 // 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.
Evan Chengc9131782009-10-18 19:58:47 +0000110 if (!MFI)
111 return FI >= 0;
112 // Spill slots should not alias others.
113 return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
Evan Cheng174e2cf2009-10-18 18:16:27 +0000114}