PseudoSourceValue.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/PseudoSourceValue.h ------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file contains the declaration of the PseudoSourceValue class.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
  18. #define LLVM_CODEGEN_PSEUDOSOURCEVALUE_H
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/IR/ValueMap.h"
  21. #include <map>
  22. namespace llvm {
  23. class GlobalValue;
  24. class MachineFrameInfo;
  25. class MachineMemOperand;
  26. class MIRFormatter;
  27. class PseudoSourceValue;
  28. class raw_ostream;
  29. class TargetInstrInfo;
  30. raw_ostream &operator<<(raw_ostream &OS, const PseudoSourceValue* PSV);
  31. /// Special value supplied for machine level alias analysis. It indicates that
  32. /// a memory access references the functions stack frame (e.g., a spill slot),
  33. /// below the stack frame (e.g., argument space), or constant pool.
  34. class PseudoSourceValue {
  35. public:
  36. enum PSVKind : unsigned {
  37. Stack,
  38. GOT,
  39. JumpTable,
  40. ConstantPool,
  41. FixedStack,
  42. GlobalValueCallEntry,
  43. ExternalSymbolCallEntry,
  44. TargetCustom
  45. };
  46. private:
  47. unsigned Kind;
  48. unsigned AddressSpace;
  49. friend raw_ostream &llvm::operator<<(raw_ostream &OS,
  50. const PseudoSourceValue* PSV);
  51. friend class MachineMemOperand; // For printCustom().
  52. friend class MIRFormatter; // For printCustom().
  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. public:
  57. explicit PseudoSourceValue(unsigned Kind, const TargetInstrInfo &TII);
  58. virtual ~PseudoSourceValue();
  59. unsigned kind() const { return Kind; }
  60. bool isStack() const { return Kind == Stack; }
  61. bool isGOT() const { return Kind == GOT; }
  62. bool isConstantPool() const { return Kind == ConstantPool; }
  63. bool isJumpTable() const { return Kind == JumpTable; }
  64. unsigned getAddressSpace() const { return AddressSpace; }
  65. unsigned getTargetCustom() const {
  66. return (Kind >= TargetCustom) ? ((Kind+1) - TargetCustom) : 0;
  67. }
  68. /// Test whether the memory pointed to by this PseudoSourceValue has a
  69. /// constant value.
  70. virtual bool isConstant(const MachineFrameInfo *) const;
  71. /// Test whether the memory pointed to by this PseudoSourceValue may also be
  72. /// pointed to by an LLVM IR Value.
  73. virtual bool isAliased(const MachineFrameInfo *) const;
  74. /// Return true if the memory pointed to by this PseudoSourceValue can ever
  75. /// alias an LLVM IR Value.
  76. virtual bool mayAlias(const MachineFrameInfo *) const;
  77. };
  78. /// A specialized PseudoSourceValue for holding FixedStack values, which must
  79. /// include a frame index.
  80. class FixedStackPseudoSourceValue : public PseudoSourceValue {
  81. const int FI;
  82. public:
  83. explicit FixedStackPseudoSourceValue(int FI, const TargetInstrInfo &TII)
  84. : PseudoSourceValue(FixedStack, TII), FI(FI) {}
  85. static bool classof(const PseudoSourceValue *V) {
  86. return V->kind() == FixedStack;
  87. }
  88. bool isConstant(const MachineFrameInfo *MFI) const override;
  89. bool isAliased(const MachineFrameInfo *MFI) const override;
  90. bool mayAlias(const MachineFrameInfo *) const override;
  91. void printCustom(raw_ostream &OS) const override;
  92. int getFrameIndex() const { return FI; }
  93. };
  94. class CallEntryPseudoSourceValue : public PseudoSourceValue {
  95. protected:
  96. CallEntryPseudoSourceValue(unsigned Kind, const TargetInstrInfo &TII);
  97. public:
  98. bool isConstant(const MachineFrameInfo *) const override;
  99. bool isAliased(const MachineFrameInfo *) const override;
  100. bool mayAlias(const MachineFrameInfo *) const override;
  101. };
  102. /// A specialized pseudo source value for holding GlobalValue values.
  103. class GlobalValuePseudoSourceValue : public CallEntryPseudoSourceValue {
  104. const GlobalValue *GV;
  105. public:
  106. GlobalValuePseudoSourceValue(const GlobalValue *GV,
  107. const TargetInstrInfo &TII);
  108. static bool classof(const PseudoSourceValue *V) {
  109. return V->kind() == GlobalValueCallEntry;
  110. }
  111. const GlobalValue *getValue() const { return GV; }
  112. };
  113. /// A specialized pseudo source value for holding external symbol values.
  114. class ExternalSymbolPseudoSourceValue : public CallEntryPseudoSourceValue {
  115. const char *ES;
  116. public:
  117. ExternalSymbolPseudoSourceValue(const char *ES, const TargetInstrInfo &TII);
  118. static bool classof(const PseudoSourceValue *V) {
  119. return V->kind() == ExternalSymbolCallEntry;
  120. }
  121. const char *getSymbol() const { return ES; }
  122. };
  123. /// Manages creation of pseudo source values.
  124. class PseudoSourceValueManager {
  125. const TargetInstrInfo &TII;
  126. const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
  127. std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
  128. StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
  129. ExternalCallEntries;
  130. ValueMap<const GlobalValue *,
  131. std::unique_ptr<const GlobalValuePseudoSourceValue>>
  132. GlobalCallEntries;
  133. public:
  134. PseudoSourceValueManager(const TargetInstrInfo &TII);
  135. /// Return a pseudo source value referencing the area below the stack frame of
  136. /// a function, e.g., the argument space.
  137. const PseudoSourceValue *getStack();
  138. /// Return a pseudo source value referencing the global offset table
  139. /// (or something the like).
  140. const PseudoSourceValue *getGOT();
  141. /// Return a pseudo source value referencing the constant pool. Since constant
  142. /// pools are constant, this doesn't need to identify a specific constant
  143. /// pool entry.
  144. const PseudoSourceValue *getConstantPool();
  145. /// Return a pseudo source value referencing a jump table. Since jump tables
  146. /// are constant, this doesn't need to identify a specific jump table.
  147. const PseudoSourceValue *getJumpTable();
  148. /// Return a pseudo source value referencing a fixed stack frame entry,
  149. /// e.g., a spill slot.
  150. const PseudoSourceValue *getFixedStack(int FI);
  151. const PseudoSourceValue *getGlobalValueCallEntry(const GlobalValue *GV);
  152. const PseudoSourceValue *getExternalSymbolCallEntry(const char *ES);
  153. };
  154. } // end namespace llvm
  155. #endif
  156. #ifdef __GNUC__
  157. #pragma GCC diagnostic pop
  158. #endif