LLVM  mainline
PseudoSourceValue.h
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/PseudoSourceValue.h ------------------------*- 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 contains the declaration of the PseudoSourceValue class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
00015 #define LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
00016 
00017 #include "llvm/ADT/StringMap.h"
00018 #include "llvm/IR/GlobalValue.h"
00019 #include "llvm/IR/Value.h"
00020 #include "llvm/IR/ValueMap.h"
00021 #include <map>
00022 
00023 namespace llvm {
00024 
00025 class MachineFrameInfo;
00026 class MachineMemOperand;
00027 class raw_ostream;
00028 
00029 raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MMO);
00030 
00031 /// Special value supplied for machine level alias analysis. It indicates that
00032 /// a memory access references the functions stack frame (e.g., a spill slot),
00033 /// below the stack frame (e.g., argument space), or constant pool.
00034 class PseudoSourceValue {
00035 public:
00036   enum PSVKind {
00037     Stack,
00038     GOT,
00039     JumpTable,
00040     ConstantPool,
00041     FixedStack,
00042     GlobalValueCallEntry,
00043     ExternalSymbolCallEntry
00044   };
00045 
00046 private:
00047   PSVKind Kind;
00048 
00049   friend class MachineMemOperand; // For printCustom().
00050 
00051   /// Implement printing for PseudoSourceValue. This is called from
00052   /// Value::print or Value's operator<<.
00053   virtual void printCustom(raw_ostream &O) const;
00054 
00055 public:
00056   explicit PseudoSourceValue(PSVKind Kind);
00057 
00058   virtual ~PseudoSourceValue();
00059 
00060   PSVKind kind() const { return Kind; }
00061 
00062   bool isStack() const { return Kind == Stack; }
00063   bool isGOT() const { return Kind == GOT; }
00064   bool isConstantPool() const { return Kind == ConstantPool; }
00065   bool isJumpTable() const { return Kind == JumpTable; }
00066 
00067   /// Test whether the memory pointed to by this PseudoSourceValue has a
00068   /// constant value.
00069   virtual bool isConstant(const MachineFrameInfo *) const;
00070 
00071   /// Test whether the memory pointed to by this PseudoSourceValue may also be
00072   /// pointed to by an LLVM IR Value.
00073   virtual bool isAliased(const MachineFrameInfo *) const;
00074 
00075   /// Return true if the memory pointed to by this PseudoSourceValue can ever
00076   /// alias an LLVM IR Value.
00077   virtual bool mayAlias(const MachineFrameInfo *) const;
00078 };
00079 
00080 /// A specialized PseudoSourceValue for holding FixedStack values, which must
00081 /// include a frame index.
00082 class FixedStackPseudoSourceValue : public PseudoSourceValue {
00083   const int FI;
00084 
00085 public:
00086   explicit FixedStackPseudoSourceValue(int FI)
00087       : PseudoSourceValue(FixedStack), FI(FI) {}
00088 
00089   static inline bool classof(const PseudoSourceValue *V) {
00090     return V->kind() == FixedStack;
00091   }
00092 
00093   bool isConstant(const MachineFrameInfo *MFI) const override;
00094 
00095   bool isAliased(const MachineFrameInfo *MFI) const override;
00096 
00097   bool mayAlias(const MachineFrameInfo *) const override;
00098 
00099   void printCustom(raw_ostream &OS) const override;
00100 
00101   int getFrameIndex() const { return FI; }
00102 };
00103 
00104 class CallEntryPseudoSourceValue : public PseudoSourceValue {
00105 protected:
00106   CallEntryPseudoSourceValue(PSVKind Kind);
00107 
00108 public:
00109   bool isConstant(const MachineFrameInfo *) const override;
00110   bool isAliased(const MachineFrameInfo *) const override;
00111   bool mayAlias(const MachineFrameInfo *) const override;
00112 };
00113 
00114 /// A specialized pseudo soruce value for holding GlobalValue values.
00115 class GlobalValuePseudoSourceValue : public CallEntryPseudoSourceValue {
00116   const GlobalValue *GV;
00117 
00118 public:
00119   GlobalValuePseudoSourceValue(const GlobalValue *GV);
00120 
00121   static inline bool classof(const PseudoSourceValue *V) {
00122     return V->kind() == GlobalValueCallEntry;
00123   }
00124 
00125   const GlobalValue *getValue() const { return GV; }
00126 };
00127 
00128 /// A specialized pseudo source value for holding external symbol values.
00129 class ExternalSymbolPseudoSourceValue : public CallEntryPseudoSourceValue {
00130   const char *ES;
00131 
00132 public:
00133   ExternalSymbolPseudoSourceValue(const char *ES);
00134 
00135   static inline bool classof(const PseudoSourceValue *V) {
00136     return V->kind() == ExternalSymbolCallEntry;
00137   }
00138 
00139   const char *getSymbol() const { return ES; }
00140 };
00141 
00142 /// Manages creation of pseudo source values.
00143 class PseudoSourceValueManager {
00144   const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
00145   std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
00146   StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
00147       ExternalCallEntries;
00148   ValueMap<const GlobalValue *,
00149            std::unique_ptr<const GlobalValuePseudoSourceValue>>
00150       GlobalCallEntries;
00151 
00152 public:
00153   PseudoSourceValueManager();
00154 
00155   /// Return a pseudo source value referencing the area below the stack frame of
00156   /// a function, e.g., the argument space.
00157   const PseudoSourceValue *getStack();
00158 
00159   /// Return a pseudo source value referencing the global offset table
00160   /// (or something the like).
00161   const PseudoSourceValue *getGOT();
00162 
00163   /// Return a pseudo source value referencing the constant pool. Since constant
00164   /// pools are constant, this doesn't need to identify a specific constant
00165   /// pool entry.
00166   const PseudoSourceValue *getConstantPool();
00167 
00168   /// Return a pseudo source value referencing a jump table. Since jump tables
00169   /// are constant, this doesn't need to identify a specific jump table.
00170   const PseudoSourceValue *getJumpTable();
00171 
00172   /// Return a pseudo source value referencing a fixed stack frame entry,
00173   /// e.g., a spill slot.
00174   const PseudoSourceValue *getFixedStack(int FI);
00175 
00176   const PseudoSourceValue *getGlobalValueCallEntry(const GlobalValue *GV);
00177 
00178   const PseudoSourceValue *getExternalSymbolCallEntry(const char *ES);
00179 };
00180 
00181 } // end namespace llvm
00182 
00183 #endif