LLVM API Documentation

PseudoSourceValue.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/PseudoSourceValue.cpp ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the PseudoSourceValue class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/PseudoSourceValue.h"
00015 #include "llvm/CodeGen/MachineFrameInfo.h"
00016 #include "llvm/IR/DerivedTypes.h"
00017 #include "llvm/IR/LLVMContext.h"
00018 #include "llvm/Support/ErrorHandling.h"
00019 #include "llvm/Support/ManagedStatic.h"
00020 #include "llvm/Support/Mutex.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 #include <map>
00023 using namespace llvm;
00024 
00025 namespace {
00026 struct PSVGlobalsTy {
00027   // PseudoSourceValues are immutable so don't need locking.
00028   const PseudoSourceValue PSVs[4];
00029   sys::Mutex Lock;  // Guards FSValues, but not the values inside it.
00030   std::map<int, const PseudoSourceValue *> FSValues;
00031 
00032   PSVGlobalsTy() : PSVs() {}
00033   ~PSVGlobalsTy() {
00034     for (std::map<int, const PseudoSourceValue *>::iterator
00035            I = FSValues.begin(), E = FSValues.end(); I != E; ++I) {
00036       delete I->second;
00037     }
00038   }
00039 };
00040 
00041 static ManagedStatic<PSVGlobalsTy> PSVGlobals;
00042 
00043 }  // anonymous namespace
00044 
00045 const PseudoSourceValue *PseudoSourceValue::getStack()
00046 { return &PSVGlobals->PSVs[0]; }
00047 const PseudoSourceValue *PseudoSourceValue::getGOT()
00048 { return &PSVGlobals->PSVs[1]; }
00049 const PseudoSourceValue *PseudoSourceValue::getJumpTable()
00050 { return &PSVGlobals->PSVs[2]; }
00051 const PseudoSourceValue *PseudoSourceValue::getConstantPool()
00052 { return &PSVGlobals->PSVs[3]; }
00053 
00054 static const char *const PSVNames[] = {
00055   "Stack",
00056   "GOT",
00057   "JumpTable",
00058   "ConstantPool"
00059 };
00060 
00061 // FIXME: THIS IS A HACK!!!!
00062 // Eventually these should be uniqued on LLVMContext rather than in a managed
00063 // static.  For now, we can safely use the global context for the time being to
00064 // squeak by.
00065 PseudoSourceValue::PseudoSourceValue(enum ValueTy Subclass) :
00066   Value(Type::getInt8PtrTy(getGlobalContext()),
00067         Subclass) {}
00068 
00069 void PseudoSourceValue::printCustom(raw_ostream &O) const {
00070   O << PSVNames[this - PSVGlobals->PSVs];
00071 }
00072 
00073 const PseudoSourceValue *PseudoSourceValue::getFixedStack(int FI) {
00074   PSVGlobalsTy &PG = *PSVGlobals;
00075   sys::ScopedLock locked(PG.Lock);
00076   const PseudoSourceValue *&V = PG.FSValues[FI];
00077   if (!V)
00078     V = new FixedStackPseudoSourceValue(FI);
00079   return V;
00080 }
00081 
00082 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
00083   if (this == getStack())
00084     return false;
00085   if (this == getGOT() ||
00086       this == getConstantPool() ||
00087       this == getJumpTable())
00088     return true;
00089   llvm_unreachable("Unknown PseudoSourceValue!");
00090 }
00091 
00092 bool PseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
00093   if (this == getStack() ||
00094       this == getGOT() ||
00095       this == getConstantPool() ||
00096       this == getJumpTable())
00097     return false;
00098   llvm_unreachable("Unknown PseudoSourceValue!");
00099 }
00100 
00101 bool PseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
00102   if (this == getGOT() ||
00103       this == getConstantPool() ||
00104       this == getJumpTable())
00105     return false;
00106   return true;
00107 }
00108 
00109 bool FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const{
00110   return MFI && MFI->isImmutableObjectIndex(FI);
00111 }
00112 
00113 bool FixedStackPseudoSourceValue::isAliased(const MachineFrameInfo *MFI) const {
00114   // Negative frame indices are used for special things that don't
00115   // appear in LLVM IR. Non-negative indices may be used for things
00116   // like static allocas.
00117   if (!MFI)
00118     return FI >= 0;
00119   // Spill slots should not alias others.
00120   return !MFI->isFixedObjectIndex(FI) && !MFI->isSpillSlotObjectIndex(FI);
00121 }
00122 
00123 bool FixedStackPseudoSourceValue::mayAlias(const MachineFrameInfo *MFI) const {
00124   if (!MFI)
00125     return true;
00126   // Spill slots will not alias any LLVM IR value.
00127   return !MFI->isSpillSlotObjectIndex(FI);
00128 }
00129 
00130 void FixedStackPseudoSourceValue::printCustom(raw_ostream &OS) const {
00131   OS << "FixedStack" << FI;
00132 }