PseudoSourceValue.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 TargetMachine;
  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 TargetMachine &TM);
  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 TargetMachine &TM)
  84. : PseudoSourceValue(FixedStack, TM), 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 TargetMachine &TM);
  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, const TargetMachine &TM);
  107. static bool classof(const PseudoSourceValue *V) {
  108. return V->kind() == GlobalValueCallEntry;
  109. }
  110. const GlobalValue *getValue() const { return GV; }
  111. };
  112. /// A specialized pseudo source value for holding external symbol values.
  113. class ExternalSymbolPseudoSourceValue : public CallEntryPseudoSourceValue {
  114. const char *ES;
  115. public:
  116. ExternalSymbolPseudoSourceValue(const char *ES, const TargetMachine &TM);
  117. static bool classof(const PseudoSourceValue *V) {
  118. return V->kind() == ExternalSymbolCallEntry;
  119. }
  120. const char *getSymbol() const { return ES; }
  121. };
  122. /// Manages creation of pseudo source values.
  123. class PseudoSourceValueManager {
  124. const TargetMachine &TM;
  125. const PseudoSourceValue StackPSV, GOTPSV, JumpTablePSV, ConstantPoolPSV;
  126. std::map<int, std::unique_ptr<FixedStackPseudoSourceValue>> FSValues;
  127. StringMap<std::unique_ptr<const ExternalSymbolPseudoSourceValue>>
  128. ExternalCallEntries;
  129. ValueMap<const GlobalValue *,
  130. std::unique_ptr<const GlobalValuePseudoSourceValue>>
  131. GlobalCallEntries;
  132. public:
  133. PseudoSourceValueManager(const TargetMachine &TM);
  134. /// Return a pseudo source value referencing the area below the stack frame of
  135. /// a function, e.g., the argument space.
  136. const PseudoSourceValue *getStack();
  137. /// Return a pseudo source value referencing the global offset table
  138. /// (or something the like).
  139. const PseudoSourceValue *getGOT();
  140. /// Return a pseudo source value referencing the constant pool. Since constant
  141. /// pools are constant, this doesn't need to identify a specific constant
  142. /// pool entry.
  143. const PseudoSourceValue *getConstantPool();
  144. /// Return a pseudo source value referencing a jump table. Since jump tables
  145. /// are constant, this doesn't need to identify a specific jump table.
  146. const PseudoSourceValue *getJumpTable();
  147. /// Return a pseudo source value referencing a fixed stack frame entry,
  148. /// e.g., a spill slot.
  149. const PseudoSourceValue *getFixedStack(int FI);
  150. const PseudoSourceValue *getGlobalValueCallEntry(const GlobalValue *GV);
  151. const PseudoSourceValue *getExternalSymbolCallEntry(const char *ES);
  152. };
  153. } // end namespace llvm
  154. #endif
  155. #ifdef __GNUC__
  156. #pragma GCC diagnostic pop
  157. #endif