NaryReassociate.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- NaryReassociate.h - Reassociate n-ary expressions --------*- 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 reassociates n-ary add expressions and eliminates the redundancy
  15. // exposed by the reassociation.
  16. //
  17. // A motivating example:
  18. //
  19. // void foo(int a, int b) {
  20. // bar(a + b);
  21. // bar((a + 2) + b);
  22. // }
  23. //
  24. // An ideal compiler should reassociate (a + 2) + b to (a + b) + 2 and simplify
  25. // the above code to
  26. //
  27. // int t = a + b;
  28. // bar(t);
  29. // bar(t + 2);
  30. //
  31. // However, the Reassociate pass is unable to do that because it processes each
  32. // instruction individually and believes (a + 2) + b is the best form according
  33. // to its rank system.
  34. //
  35. // To address this limitation, NaryReassociate reassociates an expression in a
  36. // form that reuses existing instructions. As a result, NaryReassociate can
  37. // reassociate (a + 2) + b in the example to (a + b) + 2 because it detects that
  38. // (a + b) is computed before.
  39. //
  40. // NaryReassociate works as follows. For every instruction in the form of (a +
  41. // b) + c, it checks whether a + c or b + c is already computed by a dominating
  42. // instruction. If so, it then reassociates (a + b) + c into (a + c) + b or (b +
  43. // c) + a and removes the redundancy accordingly. To efficiently look up whether
  44. // an expression is computed before, we store each instruction seen and its SCEV
  45. // into an SCEV-to-instruction map.
  46. //
  47. // Although the algorithm pattern-matches only ternary additions, it
  48. // automatically handles many >3-ary expressions by walking through the function
  49. // in the depth-first order. For example, given
  50. //
  51. // (a + c) + d
  52. // ((a + b) + c) + d
  53. //
  54. // NaryReassociate first rewrites (a + b) + c to (a + c) + b, and then rewrites
  55. // ((a + c) + b) + d into ((a + c) + d) + b.
  56. //
  57. // Finally, the above dominator-based algorithm may need to be run multiple
  58. // iterations before emitting optimal code. One source of this need is that we
  59. // only split an operand when it is used only once. The above algorithm can
  60. // eliminate an instruction and decrease the usage count of its operands. As a
  61. // result, an instruction that previously had multiple uses may become a
  62. // single-use instruction and thus eligible for split consideration. For
  63. // example,
  64. //
  65. // ac = a + c
  66. // ab = a + b
  67. // abc = ab + c
  68. // ab2 = ab + b
  69. // ab2c = ab2 + c
  70. //
  71. // In the first iteration, we cannot reassociate abc to ac+b because ab is used
  72. // twice. However, we can reassociate ab2c to abc+b in the first iteration. As a
  73. // result, ab2 becomes dead and ab will be used only once in the second
  74. // iteration.
  75. //
  76. // Limitations and TODO items:
  77. //
  78. // 1) We only considers n-ary adds and muls for now. This should be extended
  79. // and generalized.
  80. //
  81. //===----------------------------------------------------------------------===//
  82. #ifndef LLVM_TRANSFORMS_SCALAR_NARYREASSOCIATE_H
  83. #define LLVM_TRANSFORMS_SCALAR_NARYREASSOCIATE_H
  84. #include "llvm/ADT/DenseMap.h"
  85. #include "llvm/ADT/SmallVector.h"
  86. #include "llvm/IR/PassManager.h"
  87. #include "llvm/IR/ValueHandle.h"
  88. namespace llvm {
  89. class AssumptionCache;
  90. class BinaryOperator;
  91. class DataLayout;
  92. class DominatorTree;
  93. class Function;
  94. class GetElementPtrInst;
  95. class Instruction;
  96. class ScalarEvolution;
  97. class SCEV;
  98. class TargetLibraryInfo;
  99. class TargetTransformInfo;
  100. class Type;
  101. class Value;
  102. class NaryReassociatePass : public PassInfoMixin<NaryReassociatePass> {
  103. public:
  104. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  105. // Glue for old PM.
  106. bool runImpl(Function &F, AssumptionCache *AC_, DominatorTree *DT_,
  107. ScalarEvolution *SE_, TargetLibraryInfo *TLI_,
  108. TargetTransformInfo *TTI_);
  109. private:
  110. // Runs only one iteration of the dominator-based algorithm. See the header
  111. // comments for why we need multiple iterations.
  112. bool doOneIteration(Function &F);
  113. // Reassociates I for better CSE.
  114. Instruction *tryReassociate(Instruction *I, const SCEV *&OrigSCEV);
  115. // Reassociate GEP for better CSE.
  116. Instruction *tryReassociateGEP(GetElementPtrInst *GEP);
  117. // Try splitting GEP at the I-th index and see whether either part can be
  118. // CSE'ed. This is a helper function for tryReassociateGEP.
  119. //
  120. // \p IndexedType The element type indexed by GEP's I-th index. This is
  121. // equivalent to
  122. // GEP->getIndexedType(GEP->getPointerOperand(), 0-th index,
  123. // ..., i-th index).
  124. GetElementPtrInst *tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
  125. unsigned I, Type *IndexedType);
  126. // Given GEP's I-th index = LHS + RHS, see whether &Base[..][LHS][..] or
  127. // &Base[..][RHS][..] can be CSE'ed and rewrite GEP accordingly.
  128. GetElementPtrInst *tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
  129. unsigned I, Value *LHS,
  130. Value *RHS, Type *IndexedType);
  131. // Reassociate binary operators for better CSE.
  132. Instruction *tryReassociateBinaryOp(BinaryOperator *I);
  133. // A helper function for tryReassociateBinaryOp. LHS and RHS are explicitly
  134. // passed.
  135. Instruction *tryReassociateBinaryOp(Value *LHS, Value *RHS,
  136. BinaryOperator *I);
  137. // Rewrites I to (LHS op RHS) if LHS is computed already.
  138. Instruction *tryReassociatedBinaryOp(const SCEV *LHS, Value *RHS,
  139. BinaryOperator *I);
  140. // Tries to match Op1 and Op2 by using V.
  141. bool matchTernaryOp(BinaryOperator *I, Value *V, Value *&Op1, Value *&Op2);
  142. // Gets SCEV for (LHS op RHS).
  143. const SCEV *getBinarySCEV(BinaryOperator *I, const SCEV *LHS,
  144. const SCEV *RHS);
  145. // Returns the closest dominator of \c Dominatee that computes
  146. // \c CandidateExpr. Returns null if not found.
  147. Instruction *findClosestMatchingDominator(const SCEV *CandidateExpr,
  148. Instruction *Dominatee);
  149. // Try to match \p I as signed/unsigned Min/Max and reassociate it. \p
  150. // OrigSCEV is set if \I matches Min/Max regardless whether resassociation is
  151. // done or not. If reassociation was successful newly generated instruction is
  152. // returned, otherwise nullptr.
  153. template <typename PredT>
  154. Instruction *matchAndReassociateMinOrMax(Instruction *I,
  155. const SCEV *&OrigSCEV);
  156. // Reassociate Min/Max.
  157. template <typename MaxMinT>
  158. Value *tryReassociateMinOrMax(Instruction *I, MaxMinT MaxMinMatch, Value *LHS,
  159. Value *RHS);
  160. // GetElementPtrInst implicitly sign-extends an index if the index is shorter
  161. // than the pointer size. This function returns whether Index is shorter than
  162. // GEP's pointer size, i.e., whether Index needs to be sign-extended in order
  163. // to be an index of GEP.
  164. bool requiresSignExtension(Value *Index, GetElementPtrInst *GEP);
  165. AssumptionCache *AC;
  166. const DataLayout *DL;
  167. DominatorTree *DT;
  168. ScalarEvolution *SE;
  169. TargetLibraryInfo *TLI;
  170. TargetTransformInfo *TTI;
  171. // A lookup table quickly telling which instructions compute the given SCEV.
  172. // Note that there can be multiple instructions at different locations
  173. // computing to the same SCEV, so we map a SCEV to an instruction list. For
  174. // example,
  175. //
  176. // if (p1)
  177. // foo(a + b);
  178. // if (p2)
  179. // bar(a + b);
  180. DenseMap<const SCEV *, SmallVector<WeakTrackingVH, 2>> SeenExprs;
  181. };
  182. } // end namespace llvm
  183. #endif // LLVM_TRANSFORMS_SCALAR_NARYREASSOCIATE_H
  184. #ifdef __GNUC__
  185. #pragma GCC diagnostic pop
  186. #endif