Home | History | Annotate | Line # | Download | only in CodeGen
      1 //===-- llvm/CodeGen/PseudoSourceValue.cpp ----------------------*- C++ -*-===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 //
      9 // This file implements the PseudoSourceValue class.
     10 //
     11 //===----------------------------------------------------------------------===//
     12 
     13 #include "llvm/CodeGen/PseudoSourceValue.h"
     14 #include "llvm/ADT/STLExtras.h"
     15 #include "llvm/CodeGen/MachineFrameInfo.h"
     16 #include "llvm/CodeGen/TargetInstrInfo.h"
     17 #include "llvm/IR/DerivedTypes.h"
     18 #include "llvm/IR/LLVMContext.h"
     19 #include "llvm/Support/ErrorHandling.h"
     20 #include "llvm/Support/raw_ostream.h"
     21 using namespace llvm;
     22 
     23 static const char *const PSVNames[] = {
     24     "Stack", "GOT", "JumpTable", "ConstantPool", "FixedStack",
     25     "GlobalValueCallEntry", "ExternalSymbolCallEntry"};
     26 
     27 PseudoSourceValue::PseudoSourceValue(unsigned Kind, const TargetInstrInfo &TII)
     28     : Kind(Kind) {
     29   AddressSpace = TII.getAddressSpaceForPseudoSourceKind(Kind);
     30 }
     31 
     32 
     33 PseudoSourceValue::~PseudoSourceValue() {}
     34 
     35 void PseudoSourceValue::printCustom(raw_ostream &O) const {
     36   if (Kind < TargetCustom)
     37     O << PSVNames[Kind];
     38   else
     39     O << "TargetCustom" << Kind;
     40 }
     41 
     42 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
     43   if (isStack())
     44     return false;
     45   if (isGOT() || isConstantPool() || isJumpTable())
     46     return true;
     47   llvm_unreachable("Unknown PseudoSourceValue!");
     48 }
     49 
     50 bool PseudoSourceValue::isAliased(const MachineFrameInfo *) const {
     51   if (isStack() || isGOT() || isConstantPool() || isJumpTable())
     52     return false;
     53   llvm_unreachable("Unknown PseudoSourceValue!");
     54 }
     55 
     56 bool PseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
     57   return !(isGOT() || isConstantPool() || isJumpTable());
     58 }
     59 
     60 bool FixedStackPseudoSourceValue::isConstant(
     61     const MachineFrameInfo *MFI) const {
     62   return MFI && MFI->isImmutableObjectIndex(FI);
     63 }
     64 
     65 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
     66   if (!MFI)
     67     return true;
     68   return MFI->isAliasedObjectIndex(FI);
     69 }
     70 
     71 bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
     72   if (!MFI)
     73     return true;
     74   // Spill slots will not alias any LLVM IR value.
     75   return !MFI->isSpillSlotObjectIndex(FI);
     76 }
     77 
     78 void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
     79   OS << "FixedStack" << FI;
     80 }
     81 
     82 CallEntryPseudoSourceValue::CallEntryPseudoSourceValue(
     83     unsigned Kind, const TargetInstrInfo &TII)
     84     : PseudoSourceValue(Kind, TII) {}
     85 
     86 bool CallEntryPseudoSourceValue::isConstant(const MachineFrameInfo *) const {
     87   return false;
     88 }
     89 
     90 bool CallEntryPseudoSourceValue::isAliased(const MachineFrameInfo *) const {
     91   return false;
     92 }
     93 
     94 bool CallEntryPseudoSourceValue::mayAlias(const MachineFrameInfo *) const {
     95   return false;
     96 }
     97 
     98 GlobalValuePseudoSourceValue::GlobalValuePseudoSourceValue(
     99     const GlobalValue *GV,
    100     const TargetInstrInfo &TII)
    101     : CallEntryPseudoSourceValue(GlobalValueCallEntry, TII), GV(GV) {}
    102 ExternalSymbolPseudoSourceValue::ExternalSymbolPseudoSourceValue(
    103     const char *ES, const TargetInstrInfo &TII)
    104     : CallEntryPseudoSourceValue(ExternalSymbolCallEntry, TII), ES(ES) {}
    105 
    106 PseudoSourceValueManager::PseudoSourceValueManager(
    107     const TargetInstrInfo &TIInfo)
    108     : TII(TIInfo),
    109       StackPSV(PseudoSourceValue::Stack, TII),
    110       GOTPSV(PseudoSourceValue::GOT, TII),
    111       JumpTablePSV(PseudoSourceValue::JumpTable, TII),
    112       ConstantPoolPSV(PseudoSourceValue::ConstantPool, TII) {}
    113 
    114 const PseudoSourceValue *PseudoSourceValueManager::getStack() {
    115   return &StackPSV;
    116 }
    117 
    118 const PseudoSourceValue *PseudoSourceValueManager::getGOT() { return &GOTPSV; }
    119 
    120 const PseudoSourceValue *PseudoSourceValueManager::getConstantPool() {
    121   return &ConstantPoolPSV;
    122 }
    123 
    124 const PseudoSourceValue *PseudoSourceValueManager::getJumpTable() {
    125   return &JumpTablePSV;
    126 }
    127 
    128 const PseudoSourceValue *
    129 PseudoSourceValueManager::getFixedStack(int FI) {
    130   std::unique_ptr<FixedStackPseudoSourceValue> &V = FSValues[FI];
    131   if (!V)
    132     V = std::make_unique<FixedStackPseudoSourceValue>(FI, TII);
    133   return V.get();
    134 }
    135 
    136 const PseudoSourceValue *
    137 PseudoSourceValueManager::getGlobalValueCallEntry(const GlobalValue *GV) {
    138   std::unique_ptr<const GlobalValuePseudoSourceValue> &E =
    139       GlobalCallEntries[GV];
    140   if (!E)
    141     E = std::make_unique<GlobalValuePseudoSourceValue>(GV, TII);
    142   return E.get();
    143 }
    144 
    145 const PseudoSourceValue *
    146 PseudoSourceValueManager::getExternalSymbolCallEntry(const char *ES) {
    147   std::unique_ptr<const ExternalSymbolPseudoSourceValue> &E =
    148       ExternalCallEntries[ES];
    149   if (!E)
    150     E = std::make_unique<ExternalSymbolPseudoSourceValue>(ES, TII);
    151   return E.get();
    152 }
    153