1f22ef01cSRoman Divacky //===-- llvm/CodeGen/PseudoSourceValue.cpp ----------------------*- C++ -*-===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements the PseudoSourceValue class.
11f22ef01cSRoman Divacky //
12f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
13f22ef01cSRoman Divacky 
14f22ef01cSRoman Divacky #include "llvm/CodeGen/PseudoSourceValue.h"
153ca95b02SDimitry Andric #include "llvm/ADT/STLExtras.h"
16139f7f9bSDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
172cab237bSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
18139f7f9bSDimitry Andric #include "llvm/IR/DerivedTypes.h"
19139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h"
20f22ef01cSRoman Divacky #include "llvm/Support/ErrorHandling.h"
21139f7f9bSDimitry Andric #include "llvm/Support/raw_ostream.h"
22f22ef01cSRoman Divacky using namespace llvm;
23f22ef01cSRoman Divacky 
24f22ef01cSRoman Divacky static const char *const PSVNames[] = {
257d523365SDimitry Andric     "Stack", "GOT", "JumpTable", "ConstantPool", "FixedStack",
267d523365SDimitry Andric     "GlobalValueCallEntry", "ExternalSymbolCallEntry"};
27f22ef01cSRoman Divacky 
PseudoSourceValue(unsigned Kind,const TargetInstrInfo & TII)28*b5893f02SDimitry Andric PseudoSourceValue::PseudoSourceValue(unsigned Kind, const TargetInstrInfo &TII)
292cab237bSDimitry Andric     : Kind(Kind) {
302cab237bSDimitry Andric   AddressSpace = TII.getAddressSpaceForPseudoSourceKind(Kind);
312cab237bSDimitry Andric }
322cab237bSDimitry Andric 
3391bc56edSDimitry Andric 
~PseudoSourceValue()3491bc56edSDimitry Andric PseudoSourceValue::~PseudoSourceValue() {}
35f22ef01cSRoman Divacky 
printCustom(raw_ostream & O) const36f22ef01cSRoman Divacky void PseudoSourceValue::printCustom(raw_ostream &O) const {
377a7e6055SDimitry Andric   if (Kind < TargetCustom)
387d523365SDimitry Andric     O << PSVNames[Kind];
397a7e6055SDimitry Andric   else
407a7e6055SDimitry Andric     O << "TargetCustom" << Kind;
41f22ef01cSRoman Divacky }
42f22ef01cSRoman Divacky 
isConstant(const MachineFrameInfo *) const43f22ef01cSRoman Divacky bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
447d523365SDimitry Andric   if (isStack())
45f22ef01cSRoman Divacky     return false;
467d523365SDimitry Andric   if (isGOT() || isConstantPool() || isJumpTable())
47f22ef01cSRoman Divacky     return true;
48f22ef01cSRoman Divacky   llvm_unreachable("Unknown PseudoSourceValue!");
49f22ef01cSRoman Divacky }
50f22ef01cSRoman Divacky 
isAliased(const MachineFrameInfo *) const517d523365SDimitry Andric bool PseudoSourceValue::isAliased(const MachineFrameInfo *) const {
527d523365SDimitry Andric   if (isStack() || isGOT() || isConstantPool() || isJumpTable())
53f22ef01cSRoman Divacky     return false;
54f22ef01cSRoman Divacky   llvm_unreachable("Unknown PseudoSourceValue!");
55f22ef01cSRoman Divacky }
56f22ef01cSRoman Divacky 
mayAlias(const MachineFrameInfo *) const577d523365SDimitry Andric bool PseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
587d523365SDimitry Andric   return !(isGOT() || isConstantPool() || isJumpTable());
59f22ef01cSRoman Divacky }
60f22ef01cSRoman Divacky 
isConstant(const MachineFrameInfo * MFI) const617d523365SDimitry Andric bool FixedStackPseudoSourceValue::isConstant(
627d523365SDimitry Andric     const MachineFrameInfo *MFI) const {
63f22ef01cSRoman Divacky   return MFI && MFI->isImmutableObjectIndex(FI);
64f22ef01cSRoman Divacky }
65f22ef01cSRoman Divacky 
isAliased(const MachineFrameInfo * MFI) const66f22ef01cSRoman Divacky bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
67f22ef01cSRoman Divacky   if (!MFI)
6839d628a0SDimitry Andric     return true;
6939d628a0SDimitry Andric   return MFI->isAliasedObjectIndex(FI);
70f22ef01cSRoman Divacky }
71f22ef01cSRoman Divacky 
mayAlias(const MachineFrameInfo * MFI) const72f22ef01cSRoman Divacky bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
73f22ef01cSRoman Divacky   if (!MFI)
74f22ef01cSRoman Divacky     return true;
75f22ef01cSRoman Divacky   // Spill slots will not alias any LLVM IR value.
76f22ef01cSRoman Divacky   return !MFI->isSpillSlotObjectIndex(FI);
77f22ef01cSRoman Divacky }
78f22ef01cSRoman Divacky 
printCustom(raw_ostream & OS) const79f22ef01cSRoman Divacky void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
80f22ef01cSRoman Divacky   OS << "FixedStack" << FI;
81f22ef01cSRoman Divacky }
827d523365SDimitry Andric 
CallEntryPseudoSourceValue(unsigned Kind,const TargetInstrInfo & TII)832cab237bSDimitry Andric CallEntryPseudoSourceValue::CallEntryPseudoSourceValue(
84*b5893f02SDimitry Andric     unsigned Kind, const TargetInstrInfo &TII)
852cab237bSDimitry Andric     : PseudoSourceValue(Kind, TII) {}
867d523365SDimitry Andric 
isConstant(const MachineFrameInfo *) const877d523365SDimitry Andric bool CallEntryPseudoSourceValue::isConstant(const MachineFrameInfo *) const {
887d523365SDimitry Andric   return false;
897d523365SDimitry Andric }
907d523365SDimitry Andric 
isAliased(const MachineFrameInfo *) const917d523365SDimitry Andric bool CallEntryPseudoSourceValue::isAliased(const MachineFrameInfo *) const {
927d523365SDimitry Andric   return false;
937d523365SDimitry Andric }
947d523365SDimitry Andric 
mayAlias(const MachineFrameInfo *) const957d523365SDimitry Andric bool CallEntryPseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
967d523365SDimitry Andric   return false;
977d523365SDimitry Andric }
987d523365SDimitry Andric 
GlobalValuePseudoSourceValue(const GlobalValue * GV,const TargetInstrInfo & TII)997d523365SDimitry Andric GlobalValuePseudoSourceValue::GlobalValuePseudoSourceValue(
1002cab237bSDimitry Andric     const GlobalValue *GV,
1012cab237bSDimitry Andric     const TargetInstrInfo &TII)
1022cab237bSDimitry Andric     : CallEntryPseudoSourceValue(GlobalValueCallEntry, TII), GV(GV) {}
ExternalSymbolPseudoSourceValue(const char * ES,const TargetInstrInfo & TII)1032cab237bSDimitry Andric ExternalSymbolPseudoSourceValue::ExternalSymbolPseudoSourceValue(
1042cab237bSDimitry Andric     const char *ES, const TargetInstrInfo &TII)
1052cab237bSDimitry Andric     : CallEntryPseudoSourceValue(ExternalSymbolCallEntry, TII), ES(ES) {}
1067d523365SDimitry Andric 
PseudoSourceValueManager(const TargetInstrInfo & TIInfo)1072cab237bSDimitry Andric PseudoSourceValueManager::PseudoSourceValueManager(
1082cab237bSDimitry Andric     const TargetInstrInfo &TIInfo)
1092cab237bSDimitry Andric     : TII(TIInfo),
1102cab237bSDimitry Andric       StackPSV(PseudoSourceValue::Stack, TII),
1112cab237bSDimitry Andric       GOTPSV(PseudoSourceValue::GOT, TII),
1122cab237bSDimitry Andric       JumpTablePSV(PseudoSourceValue::JumpTable, TII),
1132cab237bSDimitry Andric       ConstantPoolPSV(PseudoSourceValue::ConstantPool, TII) {}
1147d523365SDimitry Andric 
getStack()1157d523365SDimitry Andric const PseudoSourceValue *PseudoSourceValueManager::getStack() {
1167d523365SDimitry Andric   return &StackPSV;
1177d523365SDimitry Andric }
1187d523365SDimitry Andric 
getGOT()1197d523365SDimitry Andric const PseudoSourceValue *PseudoSourceValueManager::getGOT() { return &GOTPSV; }
1207d523365SDimitry Andric 
getConstantPool()1217d523365SDimitry Andric const PseudoSourceValue *PseudoSourceValueManager::getConstantPool() {
1227d523365SDimitry Andric   return &ConstantPoolPSV;
1237d523365SDimitry Andric }
1247d523365SDimitry Andric 
getJumpTable()1257d523365SDimitry Andric const PseudoSourceValue *PseudoSourceValueManager::getJumpTable() {
1267d523365SDimitry Andric   return &JumpTablePSV;
1277d523365SDimitry Andric }
1287d523365SDimitry Andric 
1292cab237bSDimitry Andric const PseudoSourceValue *
getFixedStack(int FI)1302cab237bSDimitry Andric PseudoSourceValueManager::getFixedStack(int FI) {
1317d523365SDimitry Andric   std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[FI];
1327d523365SDimitry Andric   if (!V)
1332cab237bSDimitry Andric     V = llvm::make_unique<FixedStackPseudoSourceValue>(FI, TII);
1347d523365SDimitry Andric   return V.get();
1357d523365SDimitry Andric }
1367d523365SDimitry Andric 
1377d523365SDimitry Andric const PseudoSourceValue *
getGlobalValueCallEntry(const GlobalValue * GV)1387d523365SDimitry Andric PseudoSourceValueManager::getGlobalValueCallEntry(const GlobalValue *GV) {
1397d523365SDimitry Andric   std::unique_ptr<const GlobalValuePseudoSourceValue> &E =
1407d523365SDimitry Andric       GlobalCallEntries[GV];
1417d523365SDimitry Andric   if (!E)
1422cab237bSDimitry Andric     E = llvm::make_unique<GlobalValuePseudoSourceValue>(GV, TII);
1437d523365SDimitry Andric   return E.get();
1447d523365SDimitry Andric }
1457d523365SDimitry Andric 
1467d523365SDimitry Andric const PseudoSourceValue *
getExternalSymbolCallEntry(const char * ES)1477d523365SDimitry Andric PseudoSourceValueManager::getExternalSymbolCallEntry(const char *ES) {
1487d523365SDimitry Andric   std::unique_ptr<const ExternalSymbolPseudoSourceValue> &E =
1497d523365SDimitry Andric       ExternalCallEntries[ES];
1507d523365SDimitry Andric   if (!E)
1512cab237bSDimitry Andric     E = llvm::make_unique<ExternalSymbolPseudoSourceValue>(ES, TII);
1527d523365SDimitry Andric   return E.get();
1537d523365SDimitry Andric }
154