SelectionDAGAddressAnalysis.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SelectionDAGAddressAnalysis.h - DAG Address Analysis -----*- 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. #ifndef LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
  14. #define LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
  15. #include "llvm/CodeGen/SelectionDAGNodes.h"
  16. #include <cstdint>
  17. namespace llvm {
  18. class SelectionDAG;
  19. /// Helper struct to parse and store a memory address as base + index + offset.
  20. /// We ignore sign extensions when it is safe to do so.
  21. /// The following two expressions are not equivalent. To differentiate we need
  22. /// to store whether there was a sign extension involved in the index
  23. /// computation.
  24. /// (load (i64 add (i64 copyfromreg %c)
  25. /// (i64 signextend (add (i8 load %index)
  26. /// (i8 1))))
  27. /// vs
  28. ///
  29. /// (load (i64 add (i64 copyfromreg %c)
  30. /// (i64 signextend (i32 add (i32 signextend (i8 load %index))
  31. /// (i32 1)))))
  32. class BaseIndexOffset {
  33. private:
  34. SDValue Base;
  35. SDValue Index;
  36. std::optional<int64_t> Offset;
  37. bool IsIndexSignExt = false;
  38. public:
  39. BaseIndexOffset() = default;
  40. BaseIndexOffset(SDValue Base, SDValue Index, bool IsIndexSignExt)
  41. : Base(Base), Index(Index), IsIndexSignExt(IsIndexSignExt) {}
  42. BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
  43. bool IsIndexSignExt)
  44. : Base(Base), Index(Index), Offset(Offset),
  45. IsIndexSignExt(IsIndexSignExt) {}
  46. SDValue getBase() { return Base; }
  47. SDValue getBase() const { return Base; }
  48. SDValue getIndex() { return Index; }
  49. SDValue getIndex() const { return Index; }
  50. void addToOffset(int64_t VectorOff) {
  51. Offset = Offset.value_or(0) + VectorOff;
  52. }
  53. bool hasValidOffset() const { return Offset.has_value(); }
  54. int64_t getOffset() const { return *Offset; }
  55. // Returns true if `Other` and `*this` are both some offset from the same base
  56. // pointer. In that case, `Off` is set to the offset between `*this` and
  57. // `Other` (negative if `Other` is before `*this`).
  58. bool equalBaseIndex(const BaseIndexOffset &Other, const SelectionDAG &DAG,
  59. int64_t &Off) const;
  60. bool equalBaseIndex(const BaseIndexOffset &Other,
  61. const SelectionDAG &DAG) const {
  62. int64_t Off;
  63. return equalBaseIndex(Other, DAG, Off);
  64. }
  65. // Returns true if `Other` (with size `OtherSize`) can be proven to be fully
  66. // contained in `*this` (with size `Size`).
  67. bool contains(const SelectionDAG &DAG, int64_t BitSize,
  68. const BaseIndexOffset &Other, int64_t OtherBitSize,
  69. int64_t &BitOffset) const;
  70. bool contains(const SelectionDAG &DAG, int64_t BitSize,
  71. const BaseIndexOffset &Other, int64_t OtherBitSize) const {
  72. int64_t BitOffset;
  73. return contains(DAG, BitSize, Other, OtherBitSize, BitOffset);
  74. }
  75. // Returns true `Op0` and `Op1` can be proven to alias/not alias, in
  76. // which case `IsAlias` is set to true/false.
  77. static bool computeAliasing(const SDNode *Op0,
  78. const std::optional<int64_t> NumBytes0,
  79. const SDNode *Op1,
  80. const std::optional<int64_t> NumBytes1,
  81. const SelectionDAG &DAG, bool &IsAlias);
  82. /// Parses tree in N for base, index, offset addresses.
  83. static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG);
  84. void print(raw_ostream& OS) const;
  85. void dump() const;
  86. };
  87. } // end namespace llvm
  88. #endif // LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
  89. #ifdef __GNUC__
  90. #pragma GCC diagnostic pop
  91. #endif