SelectionDAGAddressAnalysis.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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. bool hasValidOffset() const { return Offset.hasValue(); }
  51. int64_t getOffset() const { return *Offset; }
  52. // Returns true if `Other` and `*this` are both some offset from the same base
  53. // pointer. In that case, `Off` is set to the offset between `*this` and
  54. // `Other` (negative if `Other` is before `*this`).
  55. bool equalBaseIndex(const BaseIndexOffset &Other, const SelectionDAG &DAG,
  56. int64_t &Off) const;
  57. bool equalBaseIndex(const BaseIndexOffset &Other,
  58. const SelectionDAG &DAG) const {
  59. int64_t Off;
  60. return equalBaseIndex(Other, DAG, Off);
  61. }
  62. // Returns true if `Other` (with size `OtherSize`) can be proven to be fully
  63. // contained in `*this` (with size `Size`).
  64. bool contains(const SelectionDAG &DAG, int64_t BitSize,
  65. const BaseIndexOffset &Other, int64_t OtherBitSize,
  66. int64_t &BitOffset) const;
  67. bool contains(const SelectionDAG &DAG, int64_t BitSize,
  68. const BaseIndexOffset &Other, int64_t OtherBitSize) const {
  69. int64_t BitOffset;
  70. return contains(DAG, BitSize, Other, OtherBitSize, BitOffset);
  71. }
  72. // Returns true `Op0` and `Op1` can be proven to alias/not alias, in
  73. // which case `IsAlias` is set to true/false.
  74. static bool computeAliasing(const SDNode *Op0,
  75. const Optional<int64_t> NumBytes0,
  76. const SDNode *Op1,
  77. const Optional<int64_t> NumBytes1,
  78. const SelectionDAG &DAG, bool &IsAlias);
  79. /// Parses tree in N for base, index, offset addresses.
  80. static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG);
  81. void print(raw_ostream& OS) const;
  82. void dump() const;
  83. };
  84. } // end namespace llvm
  85. #endif // LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
  86. #ifdef __GNUC__
  87. #pragma GCC diagnostic pop
  88. #endif