Home | History | Annotate | Download | only in CodeGen
      1 //===-- llvm/CodeGen/PseudoSourceValue.h ------------------------*- 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 contains the declaration of the PseudoSourceValue class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
     15 #define LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
     16 
     17 #include "llvm/ADT/StringMap.h"
     18 #include "llvm/IR/GlobalValue.h"
     19 #include "llvm/IR/Value.h"
     20 #include "llvm/IR/ValueMap.h"
     21 #include <map>
     22 
     23 namespace llvm {
     24 
     25 class MachineFrameInfo;
     26 class MachineMemOperand;
     27 class raw_ostream;
     28 
     29 raw_ostream &operator<<(raw_ostream &OS, const MachineMemOperand &MMO);
     30 class PseudoSourceValue;
     31 raw_ostream &operator<<(raw_ostream &OS, const PseudoSourceValue* PSV);
     32 
     33 /// Special value supplied for machine level alias analysis. It indicates that
     34 /// a memory access references the functions stack frame (e.g., a spill slot),
     35 /// below the stack frame (e.g., argument space), or constant pool.
     36 class PseudoSourceValue {
     37 public:
     38   enum PSVKind {
     39     Stack,
     40     GOT,
     41     JumpTable,
     42     ConstantPool,
     43     FixedStack,
     44     GlobalValueCallEntry,
     45     ExternalSymbolCallEntry,
     46     TargetCustom
     47   };
     48 
     49 private:
     50   PSVKind Kind;
     51   friend raw_ostream &llvm::operator<<(raw_ostream &OS,
     52                                        const PseudoSourceValue* PSV);
     53 
     54   friend class MachineMemOperand; // For printCustom().
     55 
     56   /// Implement printing for PseudoSourceValue. This is called from
     57   /// Value::print or Value's operator<<.
     58   virtual void printCustom(raw_ostream &O) const;
     59 
     60 public:
     61   explicit PseudoSourceValue(PSVKind Kind);
     62 
     63   virtual ~PseudoSourceValue();
     64 
     65   PSVKind kind() const { return Kind; }
     66 
     67   bool isStack() const { return Kind == Stack; }
     68   bool isGOT() const { return Kind == GOT; }
     69   bool isConstantPool() const { return Kind == ConstantPool; }
     70   bool isJumpTable() const { return Kind == JumpTable; }
     71   unsigned getTargetCustom() const {
     72     return (Kind >= TargetCustom) ? ((Kind+1) - TargetCustom) : 0;
     73   }
     74 
     75   /// Test whether the memory pointed to by this PseudoSourceValue has a
     76   /// constant value.
     77   virtual bool isConstant(const MachineFrameInfo *) const;
     78 
     79   /// Test whether the memory pointed to by this PseudoSourceValue may also be
     80   /// pointed to by an LLVM IR Value.
     81   virtual bool isAliased(const MachineFrameInfo *) const;
     82 
     83   /// Return true if the memory pointed to by this PseudoSourceValue can ever
     84   /// alias an LLVM IR Value.
     85   virtual bool mayAlias(const MachineFrameInfo *) const;
     86 };
     87 
     88 /// A specialized PseudoSourceValue for holding FixedStack values, which must
     89 /// include a frame index.
     90 class FixedStackPseudoSourceValue : public PseudoSourceValue {
     91   const int FI;
     92 
     93 public:
     94   explicit FixedStackPseudoSourceValue(int FI)
     95       : PseudoSourceValue(FixedStack), FI(FI) {}
     96 
     97   static inline bool classof(const PseudoSourceValue *V) {
     98     return V->kind() == FixedStack;
     99   }
    100 
    101   bool isConstant(const MachineFrameInfo *MFI) const override;
    102 
    103   bool isAliased(const MachineFrameInfo *MFI) const override;
    104 
    105   bool mayAlias(const MachineFrameInfo *) const override;
    106 
    107   void printCustom(raw_ostream &OS) const override;
    108 
    109   int getFrameIndex() const { return FI; }
    110 };
    111 
    112 class CallEntryPseudoSourceValue : public PseudoSourceValue {
    113 protected:
    114   CallEntryPseudoSourceValue(PSVKind Kind);
    115 
    116 public:
    117   bool isConstant(const MachineFrameInfo *) const override;
    118   bool isAliased(const MachineFrameInfo *) const override;
    119   bool mayAlias(const MachineFrameInfo *) const override;
    120 };
    121 
    122 /// A specialized pseudo soruce value for holding GlobalValue values.
    123 class GlobalValuePseudoSourceValue : public CallEntryPseudoSourceValue {
    124   const GlobalValue *GV;
    125 
    126 public:
    127   GlobalValuePseudoSourceValue(const GlobalValue *GV);
    128 
    129   static inline bool classof(const PseudoSourceValue *V) {
    130     return V->kind() == GlobalValueCallEntry;
    131   }
    132 
    133   const GlobalValue *getValue() const { return GV; }
    134 };
    135 
    136 /// A specialized pseudo source value for holding external symbol values.
    137 class ExternalSymbolPseudoSourceValue : public CallEntryPseudoSourceValue {
    138   const char *ES;
    139 
    140 public:
    141   ExternalSymbolPseudoSourceValue(const char *ES);
    142 
    143   static inline bool classof(const PseudoSourceValue *V) {
    144     return V->kind() == ExternalSymbolCallEntry;
    145   }
    146 
    147   const char *getSymbol() const { return ES; }
    148 };
    149 
    150 /// Manages creation of pseudo source values.
    151 class PseudoSourceValueManager {
    152   const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
    153   std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
    154   StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
    155       ExternalCallEntries;
    156   ValueMap<const GlobalValue *,
    157            std::unique_ptr<const GlobalValuePseudoSourceValue>>
    158       GlobalCallEntries;
    159 
    160 public:
    161   PseudoSourceValueManager();
    162 
    163   /// Return a pseudo source value referencing the area below the stack frame of
    164   /// a function, e.g., the argument space.
    165   const PseudoSourceValue *getStack();
    166 
    167   /// Return a pseudo source value referencing the global offset table
    168   /// (or something the like).
    169   const PseudoSourceValue *getGOT();
    170 
    171   /// Return a pseudo source value referencing the constant pool. Since constant
    172   /// pools are constant, this doesn't need to identify a specific constant
    173   /// pool entry.
    174   const PseudoSourceValue *getConstantPool();
    175 
    176   /// Return a pseudo source value referencing a jump table. Since jump tables
    177   /// are constant, this doesn't need to identify a specific jump table.
    178   const PseudoSourceValue *getJumpTable();
    179 
    180   /// Return a pseudo source value referencing a fixed stack frame entry,
    181   /// e.g., a spill slot.
    182   const PseudoSourceValue *getFixedStack(int FI);
    183 
    184   const PseudoSourceValue *getGlobalValueCallEntry(const GlobalValue *GV);
    185 
    186   const PseudoSourceValue *getExternalSymbolCallEntry(const char *ES);
    187 };
    188 
    189 } // end namespace llvm
    190 
    191 #endif
    192