PseudoSourceValue.h 6.2 KB

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