DemandedBits.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Analysis/DemandedBits.h - Determine demanded bits ---*- 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 pass implements a demanded bits analysis. A demanded bit is one that
  15. // contributes to a result; bits that are not demanded can be either zero or
  16. // one without affecting control or data flow. For example in this sequence:
  17. //
  18. // %1 = add i32 %x, %y
  19. // %2 = trunc i32 %1 to i16
  20. //
  21. // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
  22. // trunc.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_ANALYSIS_DEMANDEDBITS_H
  26. #define LLVM_ANALYSIS_DEMANDEDBITS_H
  27. #include "llvm/ADT/APInt.h"
  28. #include "llvm/ADT/DenseMap.h"
  29. #include "llvm/ADT/SmallPtrSet.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include "llvm/Pass.h"
  32. #include <optional>
  33. namespace llvm {
  34. class AssumptionCache;
  35. class DominatorTree;
  36. class Function;
  37. class Instruction;
  38. struct KnownBits;
  39. class raw_ostream;
  40. class DemandedBits {
  41. public:
  42. DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
  43. F(F), AC(AC), DT(DT) {}
  44. /// Return the bits demanded from instruction I.
  45. ///
  46. /// For vector instructions individual vector elements are not distinguished:
  47. /// A bit is demanded if it is demanded for any of the vector elements. The
  48. /// size of the return value corresponds to the type size in bits of the
  49. /// scalar type.
  50. ///
  51. /// Instructions that do not have integer or vector of integer type are
  52. /// accepted, but will always produce a mask with all bits set.
  53. APInt getDemandedBits(Instruction *I);
  54. /// Return the bits demanded from use U.
  55. APInt getDemandedBits(Use *U);
  56. /// Return true if, during analysis, I could not be reached.
  57. bool isInstructionDead(Instruction *I);
  58. /// Return whether this use is dead by means of not having any demanded bits.
  59. bool isUseDead(Use *U);
  60. void print(raw_ostream &OS);
  61. /// Compute alive bits of one addition operand from alive output and known
  62. /// operand bits
  63. static APInt determineLiveOperandBitsAdd(unsigned OperandNo,
  64. const APInt &AOut,
  65. const KnownBits &LHS,
  66. const KnownBits &RHS);
  67. /// Compute alive bits of one subtraction operand from alive output and known
  68. /// operand bits
  69. static APInt determineLiveOperandBitsSub(unsigned OperandNo,
  70. const APInt &AOut,
  71. const KnownBits &LHS,
  72. const KnownBits &RHS);
  73. private:
  74. void performAnalysis();
  75. void determineLiveOperandBits(const Instruction *UserI,
  76. const Value *Val, unsigned OperandNo,
  77. const APInt &AOut, APInt &AB,
  78. KnownBits &Known, KnownBits &Known2, bool &KnownBitsComputed);
  79. Function &F;
  80. AssumptionCache &AC;
  81. DominatorTree &DT;
  82. bool Analyzed = false;
  83. // The set of visited instructions (non-integer-typed only).
  84. SmallPtrSet<Instruction*, 32> Visited;
  85. DenseMap<Instruction *, APInt> AliveBits;
  86. // Uses with no demanded bits. If the user also has no demanded bits, the use
  87. // might not be stored explicitly in this map, to save memory during analysis.
  88. SmallPtrSet<Use *, 16> DeadUses;
  89. };
  90. class DemandedBitsWrapperPass : public FunctionPass {
  91. private:
  92. mutable std::optional<DemandedBits> DB;
  93. public:
  94. static char ID; // Pass identification, replacement for typeid
  95. DemandedBitsWrapperPass();
  96. bool runOnFunction(Function &F) override;
  97. void getAnalysisUsage(AnalysisUsage &AU) const override;
  98. /// Clean up memory in between runs
  99. void releaseMemory() override;
  100. DemandedBits &getDemandedBits() { return *DB; }
  101. void print(raw_ostream &OS, const Module *M) const override;
  102. };
  103. /// An analysis that produces \c DemandedBits for a function.
  104. class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
  105. friend AnalysisInfoMixin<DemandedBitsAnalysis>;
  106. static AnalysisKey Key;
  107. public:
  108. /// Provide the result type for this analysis pass.
  109. using Result = DemandedBits;
  110. /// Run the analysis pass over a function and produce demanded bits
  111. /// information.
  112. DemandedBits run(Function &F, FunctionAnalysisManager &AM);
  113. };
  114. /// Printer pass for DemandedBits
  115. class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
  116. raw_ostream &OS;
  117. public:
  118. explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
  119. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  120. };
  121. /// Create a demanded bits analysis pass.
  122. FunctionPass *createDemandedBitsWrapperPass();
  123. } // end namespace llvm
  124. #endif // LLVM_ANALYSIS_DEMANDEDBITS_H
  125. #ifdef __GNUC__
  126. #pragma GCC diagnostic pop
  127. #endif