DemandedBits.h 4.8 KB

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