1 //===-- llvm/CodeGen/PseudoSourceValue.h ------------------------*- 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 contains the declaration of the PseudoSourceValue class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
14 #define LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
15 
16 namespace llvm {
17 
18 class GlobalValue;
19 class MachineFrameInfo;
20 class MachineMemOperand;
21 class MIRFormatter;
22 class PseudoSourceValue;
23 class raw_ostream;
24 class TargetMachine;
25 
26 raw_ostream &operator<<(raw_ostream &OS, const PseudoSourceValue* PSV);
27 
28 /// Special value supplied for machine level alias analysis. It indicates that
29 /// a memory access references the functions stack frame (e.g., a spill slot),
30 /// below the stack frame (e.g., argument space), or constant pool.
31 class PseudoSourceValue {
32 public:
33   enum PSVKind : unsigned {
34     Stack,
35     GOT,
36     JumpTable,
37     ConstantPool,
38     FixedStack,
39     GlobalValueCallEntry,
40     ExternalSymbolCallEntry,
41     TargetCustom
42   };
43 
44 private:
45   unsigned Kind;
46   unsigned AddressSpace;
47   friend raw_ostream &llvm::operator<<(raw_ostream &OS,
48                                        const PseudoSourceValue* PSV);
49 
50   friend class MachineMemOperand; // For printCustom().
51   friend class MIRFormatter;      // For printCustom().
52 
53   /// Implement printing for PseudoSourceValue. This is called from
54   /// Value::print or Value's operator<<.
55   virtual void printCustom(raw_ostream &O) const;
56 
57 public:
58   explicit PseudoSourceValue(unsigned Kind, const TargetMachine &TM);
59 
60   virtual ~PseudoSourceValue();
61 
kind()62   unsigned kind() const { return Kind; }
63 
isStack()64   bool isStack() const { return Kind == Stack; }
isGOT()65   bool isGOT() const { return Kind == GOT; }
isConstantPool()66   bool isConstantPool() const { return Kind == ConstantPool; }
isJumpTable()67   bool isJumpTable() const { return Kind == JumpTable; }
68 
getAddressSpace()69   unsigned getAddressSpace() const { return AddressSpace; }
70 
getTargetCustom()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:
FixedStackPseudoSourceValue(int FI,const TargetMachine & TM)94   explicit FixedStackPseudoSourceValue(int FI, const TargetMachine &TM)
95       : PseudoSourceValue(FixedStack, TM), FI(FI) {}
96 
classof(const PseudoSourceValue * V)97   static 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 
getFrameIndex()109   int getFrameIndex() const { return FI; }
110 };
111 
112 class CallEntryPseudoSourceValue : public PseudoSourceValue {
113 protected:
114   CallEntryPseudoSourceValue(unsigned Kind, const TargetMachine &TM);
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 source value for holding GlobalValue values.
123 class GlobalValuePseudoSourceValue : public CallEntryPseudoSourceValue {
124   const GlobalValue *GV;
125 
126 public:
127   GlobalValuePseudoSourceValue(const GlobalValue *GV, const TargetMachine &TM);
128 
classof(const PseudoSourceValue * V)129   static bool classof(const PseudoSourceValue *V) {
130     return V->kind() == GlobalValueCallEntry;
131   }
132 
getValue()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, const TargetMachine &TM);
142 
classof(const PseudoSourceValue * V)143   static bool classof(const PseudoSourceValue *V) {
144     return V->kind() == ExternalSymbolCallEntry;
145   }
146 
getSymbol()147   const char *getSymbol() const { return ES; }
148 };
149 
150 } // end namespace llvm
151 
152 #endif
153