Home | History | Annotate | Download | only in CodeGen
      1 //===-- 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"
     15 #include "llvm/CodeGen/MachineFrameInfo.h"
     16 #include "llvm/IR/DerivedTypes.h"
     17 #include "llvm/IR/LLVMContext.h"
     18 #include "llvm/Support/ErrorHandling.h"
     19 #include "llvm/Support/ManagedStatic.h"
     20 #include "llvm/Support/Mutex.h"
     21 #include "llvm/Support/raw_ostream.h"
     22 #include <map>
     23 using namespace llvm;
     24 
     25 namespace {
     26 struct 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 
     41 static ManagedStatic<PSVGlobalsTy> PSVGlobals;
     42 
     43 }  // anonymous namespace
     44 
     45 const PseudoSourceValue *PseudoSourceValue::getStack()
     46 { return &PSVGlobals->PSVs[0]; }
     47 const PseudoSourceValue *PseudoSourceValue::getGOT()
     48 { return &PSVGlobals->PSVs[1]; }
     49 const PseudoSourceValue *PseudoSourceValue::getJumpTable()
     50 { return &PSVGlobals->PSVs[2]; }
     51 const PseudoSourceValue *PseudoSourceValue::getConstantPool()
     52 { return &PSVGlobals->PSVs[3]; }
     53 
     54 static const char *const PSVNames[] = {
     55   "Stack",
     56   "GOT",
     57   "JumpTable",
     58   "ConstantPool"
     59 };
     60 
     61 PseudoSourceValue::PseudoSourceValue(bool isFixed) : isFixed(isFixed) {}
     62 
     63 PseudoSourceValue::~PseudoSourceValue() {}
     64 
     65 void PseudoSourceValue::printCustom(raw_ostream &O) const {
     66   O << PSVNames[this - PSVGlobals->PSVs];
     67 }
     68 
     69 const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
     70   PSVGlobalsTy &PG = *PSVGlobals;
     71   sys::ScopedLock locked(PG.Lock);
     72   const PseudoSourceValue *&V = PG.FSValues[FI];
     73   if (!V)
     74     V = new FixedStackPseudoSourceValue(FI);
     75   return V;
     76 }
     77 
     78 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
     79   if (this == getStack())
     80     return false;
     81   if (this == getGOT() ||
     82       this == getConstantPool() ||
     83       this == getJumpTable())
     84     return true;
     85   llvm_unreachable("Unknown PseudoSourceValue!");
     86 }
     87 
     88 bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
     89   if (this == getStack() ||
     90       this == getGOT() ||
     91       this == getConstantPool() ||
     92       this == getJumpTable())
     93     return false;
     94   llvm_unreachable("Unknown PseudoSourceValue!");
     95 }
     96 
     97 bool PseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
     98   if (this == getGOT() ||
     99       this == getConstantPool() ||
    100       this == getJumpTable())
    101     return false;
    102   return true;
    103 }
    104 
    105 bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
    106   return MFI && MFI->isImmutableObjectIndex(FI);
    107 }
    108 
    109 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
    110   // 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.
    113   if (!MFI)
    114     return FI >= 0;
    115   // Spill slots should not alias others.
    116   return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
    117 }
    118 
    119 bool 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 }
    125 
    126 void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
    127   OS << "FixedStack" << FI;
    128 }
    129