AggressiveInstCombineInternal.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //===- AggressiveInstCombineInternal.h --------------------------*- 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 file implements the instruction pattern combiner classes.
  10. // Currently, it handles pattern expressions for:
  11. // * Truncate instruction
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_TRANSFORMS_AGGRESSIVEINSTCOMBINE_COMBINEINTERNAL_H
  15. #define LLVM_LIB_TRANSFORMS_AGGRESSIVEINSTCOMBINE_COMBINEINTERNAL_H
  16. #include "llvm/ADT/MapVector.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/Analysis/ValueTracking.h"
  19. #include "llvm/Support/KnownBits.h"
  20. using namespace llvm;
  21. //===----------------------------------------------------------------------===//
  22. // TruncInstCombine - looks for expression graphs dominated by trunc
  23. // instructions and for each eligible graph, it will create a reduced bit-width
  24. // expression and replace the old expression with this new one and remove the
  25. // old one. Eligible expression graph is such that:
  26. // 1. Contains only supported instructions.
  27. // 2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value.
  28. // 3. Can be evaluated into type with reduced legal bit-width (or Trunc type).
  29. // 4. All instructions in the graph must not have users outside the graph.
  30. // Only exception is for {ZExt, SExt}Inst with operand type equal to the
  31. // new reduced type chosen in (3).
  32. //
  33. // The motivation for this optimization is that evaluating and expression using
  34. // smaller bit-width is preferable, especially for vectorization where we can
  35. // fit more values in one vectorized instruction. In addition, this optimization
  36. // may decrease the number of cast instructions, but will not increase it.
  37. //===----------------------------------------------------------------------===//
  38. namespace llvm {
  39. class AssumptionCache;
  40. class DataLayout;
  41. class DominatorTree;
  42. class Function;
  43. class Instruction;
  44. class TargetLibraryInfo;
  45. class TruncInst;
  46. class Type;
  47. class Value;
  48. class TruncInstCombine {
  49. AssumptionCache ∾
  50. TargetLibraryInfo &TLI;
  51. const DataLayout &DL;
  52. const DominatorTree &DT;
  53. /// List of all TruncInst instructions to be processed.
  54. SmallVector<TruncInst *, 4> Worklist;
  55. /// Current processed TruncInst instruction.
  56. TruncInst *CurrentTruncInst = nullptr;
  57. /// Information per each instruction in the expression graph.
  58. struct Info {
  59. /// Number of LSBs that are needed to generate a valid expression.
  60. unsigned ValidBitWidth = 0;
  61. /// Minimum number of LSBs needed to generate the ValidBitWidth.
  62. unsigned MinBitWidth = 0;
  63. /// The reduced value generated to replace the old instruction.
  64. Value *NewValue = nullptr;
  65. };
  66. /// An ordered map representing expression graph post-dominated by current
  67. /// processed TruncInst. It maps each instruction in the graph to its Info
  68. /// structure. The map is ordered such that each instruction appears before
  69. /// all other instructions in the graph that uses it.
  70. MapVector<Instruction *, Info> InstInfoMap;
  71. public:
  72. TruncInstCombine(AssumptionCache &AC, TargetLibraryInfo &TLI,
  73. const DataLayout &DL, const DominatorTree &DT)
  74. : AC(AC), TLI(TLI), DL(DL), DT(DT) {}
  75. /// Perform TruncInst pattern optimization on given function.
  76. bool run(Function &F);
  77. private:
  78. /// Build expression graph dominated by the /p CurrentTruncInst and append it
  79. /// to the InstInfoMap container.
  80. ///
  81. /// \return true only if succeed to generate an eligible sub expression graph.
  82. bool buildTruncExpressionGraph();
  83. /// Calculate the minimal allowed bit-width of the chain ending with the
  84. /// currently visited truncate's operand.
  85. ///
  86. /// \return minimum number of bits to which the chain ending with the
  87. /// truncate's operand can be shrunk to.
  88. unsigned getMinBitWidth();
  89. /// Build an expression graph dominated by the current processed TruncInst and
  90. /// Check if it is eligible to be reduced to a smaller type.
  91. ///
  92. /// \return the scalar version of the new type to be used for the reduced
  93. /// expression graph, or nullptr if the expression graph is not
  94. /// eligible to be reduced.
  95. Type *getBestTruncatedType();
  96. KnownBits computeKnownBits(const Value *V) const {
  97. return llvm::computeKnownBits(V, DL, /*Depth=*/0, &AC,
  98. /*CtxI=*/cast<Instruction>(CurrentTruncInst),
  99. &DT);
  100. }
  101. unsigned ComputeNumSignBits(const Value *V) const {
  102. return llvm::ComputeNumSignBits(
  103. V, DL, /*Depth=*/0, &AC, /*CtxI=*/cast<Instruction>(CurrentTruncInst),
  104. &DT);
  105. }
  106. /// Given a \p V value and a \p SclTy scalar type return the generated reduced
  107. /// value of \p V based on the type \p SclTy.
  108. ///
  109. /// \param V value to be reduced.
  110. /// \param SclTy scalar version of new type to reduce to.
  111. /// \return the new reduced value.
  112. Value *getReducedOperand(Value *V, Type *SclTy);
  113. /// Create a new expression graph using the reduced /p SclTy type and replace
  114. /// the old expression graph with it. Also erase all instructions in the old
  115. /// graph, except those that are still needed outside the graph.
  116. ///
  117. /// \param SclTy scalar version of new type to reduce expression graph into.
  118. void ReduceExpressionGraph(Type *SclTy);
  119. };
  120. } // end namespace llvm.
  121. #endif