GISelKnownBits.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/GlobalISel/GISelKnownBits.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. /// \file
  14. /// Provides analysis for querying information about KnownBits during GISel
  15. /// passes.
  16. ///
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
  19. #define LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
  20. #include "llvm/ADT/DenseMap.h"
  21. #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
  22. #include "llvm/CodeGen/MachineFunctionPass.h"
  23. #include "llvm/CodeGen/Register.h"
  24. #include "llvm/InitializePasses.h"
  25. #include "llvm/Support/KnownBits.h"
  26. namespace llvm {
  27. class TargetLowering;
  28. class DataLayout;
  29. class GISelKnownBits : public GISelChangeObserver {
  30. MachineFunction &MF;
  31. MachineRegisterInfo &MRI;
  32. const TargetLowering &TL;
  33. const DataLayout &DL;
  34. unsigned MaxDepth;
  35. /// Cache maintained during a computeKnownBits request.
  36. SmallDenseMap<Register, KnownBits, 16> ComputeKnownBitsCache;
  37. void computeKnownBitsMin(Register Src0, Register Src1, KnownBits &Known,
  38. const APInt &DemandedElts,
  39. unsigned Depth = 0);
  40. unsigned computeNumSignBitsMin(Register Src0, Register Src1,
  41. const APInt &DemandedElts, unsigned Depth = 0);
  42. public:
  43. GISelKnownBits(MachineFunction &MF, unsigned MaxDepth = 6);
  44. virtual ~GISelKnownBits() = default;
  45. const MachineFunction &getMachineFunction() const {
  46. return MF;
  47. }
  48. const DataLayout &getDataLayout() const {
  49. return DL;
  50. }
  51. virtual void computeKnownBitsImpl(Register R, KnownBits &Known,
  52. const APInt &DemandedElts,
  53. unsigned Depth = 0);
  54. unsigned computeNumSignBits(Register R, const APInt &DemandedElts,
  55. unsigned Depth = 0);
  56. unsigned computeNumSignBits(Register R, unsigned Depth = 0);
  57. // KnownBitsAPI
  58. KnownBits getKnownBits(Register R);
  59. KnownBits getKnownBits(Register R, const APInt &DemandedElts,
  60. unsigned Depth = 0);
  61. // Calls getKnownBits for first operand def of MI.
  62. KnownBits getKnownBits(MachineInstr &MI);
  63. APInt getKnownZeroes(Register R);
  64. APInt getKnownOnes(Register R);
  65. /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use
  66. /// this predicate to simplify operations downstream.
  67. /// Mask is known to be zero for bits that V cannot have.
  68. bool maskedValueIsZero(Register Val, const APInt &Mask) {
  69. return Mask.isSubsetOf(getKnownBits(Val).Zero);
  70. }
  71. /// \return true if the sign bit of Op is known to be zero. We use this
  72. /// predicate to simplify operations downstream.
  73. bool signBitIsZero(Register Op);
  74. static void computeKnownBitsForAlignment(KnownBits &Known,
  75. Align Alignment) {
  76. // The low bits are known zero if the pointer is aligned.
  77. Known.Zero.setLowBits(Log2(Alignment));
  78. }
  79. /// \return The known alignment for the pointer-like value \p R.
  80. Align computeKnownAlignment(Register R, unsigned Depth = 0);
  81. // Observer API. No-op for non-caching implementation.
  82. void erasingInstr(MachineInstr &MI) override{};
  83. void createdInstr(MachineInstr &MI) override{};
  84. void changingInstr(MachineInstr &MI) override{};
  85. void changedInstr(MachineInstr &MI) override{};
  86. protected:
  87. unsigned getMaxDepth() const { return MaxDepth; }
  88. };
  89. /// To use KnownBitsInfo analysis in a pass,
  90. /// KnownBitsInfo &Info = getAnalysis<GISelKnownBitsInfoAnalysis>().get(MF);
  91. /// Add to observer if the Info is caching.
  92. /// WrapperObserver.addObserver(Info);
  93. /// Eventually add other features such as caching/ser/deserializing
  94. /// to MIR etc. Those implementations can derive from GISelKnownBits
  95. /// and override computeKnownBitsImpl.
  96. class GISelKnownBitsAnalysis : public MachineFunctionPass {
  97. std::unique_ptr<GISelKnownBits> Info;
  98. public:
  99. static char ID;
  100. GISelKnownBitsAnalysis() : MachineFunctionPass(ID) {
  101. initializeGISelKnownBitsAnalysisPass(*PassRegistry::getPassRegistry());
  102. }
  103. GISelKnownBits &get(MachineFunction &MF) {
  104. if (!Info)
  105. Info = std::make_unique<GISelKnownBits>(MF);
  106. return *Info.get();
  107. }
  108. void getAnalysisUsage(AnalysisUsage &AU) const override;
  109. bool runOnMachineFunction(MachineFunction &MF) override;
  110. void releaseMemory() override { Info.reset(); }
  111. };
  112. } // namespace llvm
  113. #endif // LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
  114. #ifdef __GNUC__
  115. #pragma GCC diagnostic pop
  116. #endif