DemandedBits.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_DEMANDED_BITS_H
  26. #define LLVM_ANALYSIS_DEMANDED_BITS_H
  27. #include "llvm/ADT/APInt.h"
  28. #include "llvm/ADT/DenseMap.h"
  29. #include "llvm/ADT/Optional.h"
  30. #include "llvm/ADT/SmallPtrSet.h"
  31. #include "llvm/IR/PassManager.h"
  32. #include "llvm/Pass.h"
  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 true if, during analysis, I could not be reached.
  55. bool isInstructionDead(Instruction *I);
  56. /// Return whether this use is dead by means of not having any demanded bits.
  57. bool isUseDead(Use *U);
  58. void print(raw_ostream &OS);
  59. /// Compute alive bits of one addition operand from alive output and known
  60. /// operand bits
  61. static APInt determineLiveOperandBitsAdd(unsigned OperandNo,
  62. const APInt &AOut,
  63. const KnownBits &LHS,
  64. const KnownBits &RHS);
  65. /// Compute alive bits of one subtraction operand from alive output and known
  66. /// operand bits
  67. static APInt determineLiveOperandBitsSub(unsigned OperandNo,
  68. const APInt &AOut,
  69. const KnownBits &LHS,
  70. const KnownBits &RHS);
  71. private:
  72. void performAnalysis();
  73. void determineLiveOperandBits(const Instruction *UserI,
  74. const Value *Val, unsigned OperandNo,
  75. const APInt &AOut, APInt &AB,
  76. KnownBits &Known, KnownBits &Known2, bool &KnownBitsComputed);
  77. Function &F;
  78. AssumptionCache ∾
  79. DominatorTree &DT;
  80. bool Analyzed = false;
  81. // The set of visited instructions (non-integer-typed only).
  82. SmallPtrSet<Instruction*, 32> Visited;
  83. DenseMap<Instruction *, APInt> AliveBits;
  84. // Uses with no demanded bits. If the user also has no demanded bits, the use
  85. // might not be stored explicitly in this map, to save memory during analysis.
  86. SmallPtrSet<Use *, 16> DeadUses;
  87. };
  88. class DemandedBitsWrapperPass : public FunctionPass {
  89. private:
  90. mutable Optional<DemandedBits> DB;
  91. public:
  92. static char ID; // Pass identification, replacement for typeid
  93. DemandedBitsWrapperPass();
  94. bool runOnFunction(Function &F) override;
  95. void getAnalysisUsage(AnalysisUsage &AU) const override;
  96. /// Clean up memory in between runs
  97. void releaseMemory() override;
  98. DemandedBits &getDemandedBits() { return *DB; }
  99. void print(raw_ostream &OS, const Module *M) const override;
  100. };
  101. /// An analysis that produces \c DemandedBits for a function.
  102. class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
  103. friend AnalysisInfoMixin<DemandedBitsAnalysis>;
  104. static AnalysisKey Key;
  105. public:
  106. /// Provide the result type for this analysis pass.
  107. using Result = DemandedBits;
  108. /// Run the analysis pass over a function and produce demanded bits
  109. /// information.
  110. DemandedBits run(Function &F, FunctionAnalysisManager &AM);
  111. };
  112. /// Printer pass for DemandedBits
  113. class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
  114. raw_ostream &OS;
  115. public:
  116. explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
  117. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  118. };
  119. /// Create a demanded bits analysis pass.
  120. FunctionPass *createDemandedBitsWrapperPass();
  121. } // end namespace llvm
  122. #endif // LLVM_ANALYSIS_DEMANDED_BITS_H
  123. #ifdef __GNUC__
  124. #pragma GCC diagnostic pop
  125. #endif