1303231Sdim//===- Reassociate.h - Reassociate binary expressions -----------*- C++ -*-===//
2303231Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6303231Sdim//
7303231Sdim//===----------------------------------------------------------------------===//
8303231Sdim//
9303231Sdim// This pass reassociates commutative expressions in an order that is designed
10303231Sdim// to promote better constant propagation, GCSE, LICM, PRE, etc.
11303231Sdim//
12303231Sdim// For example: 4 + (x + 5) -> x + (4 + 5)
13303231Sdim//
14303231Sdim// In the implementation of this algorithm, constants are assigned rank = 0,
15303231Sdim// function arguments are rank = 1, and other values are assigned ranks
16303231Sdim// corresponding to the reverse post order traversal of current function
17303231Sdim// (starting at 2), which effectively gives values in deep loops higher rank
18303231Sdim// than values not in loops.
19303231Sdim//
20303231Sdim//===----------------------------------------------------------------------===//
21303231Sdim
22303231Sdim#ifndef LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
23303231Sdim#define LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
24303231Sdim
25327952Sdim#include "llvm/ADT/DenseMap.h"
26303231Sdim#include "llvm/ADT/PostOrderIterator.h"
27303231Sdim#include "llvm/ADT/SetVector.h"
28303231Sdim#include "llvm/IR/IRBuilder.h"
29303231Sdim#include "llvm/IR/PassManager.h"
30327952Sdim#include "llvm/IR/ValueHandle.h"
31341825Sdim#include <deque>
32303231Sdim
33303231Sdimnamespace llvm {
34303231Sdim
35327952Sdimclass APInt;
36327952Sdimclass BasicBlock;
37327952Sdimclass BinaryOperator;
38327952Sdimclass Function;
39327952Sdimclass Instruction;
40327952Sdimclass Value;
41327952Sdim
42303231Sdim/// A private "module" namespace for types and utilities used by Reassociate.
43303231Sdim/// These are implementation details and should not be used by clients.
44303231Sdimnamespace reassociate {
45327952Sdim
46303231Sdimstruct ValueEntry {
47303231Sdim  unsigned Rank;
48303231Sdim  Value *Op;
49327952Sdim
50303231Sdim  ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {}
51303231Sdim};
52327952Sdim
53303231Sdiminline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) {
54303231Sdim  return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start.
55303231Sdim}
56303231Sdim
57341825Sdim/// Utility class representing a base and exponent pair which form one
58303231Sdim/// factor of some product.
59303231Sdimstruct Factor {
60303231Sdim  Value *Base;
61303231Sdim  unsigned Power;
62327952Sdim
63303231Sdim  Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
64303231Sdim};
65303231Sdim
66303231Sdimclass XorOpnd;
67303231Sdim
68327952Sdim} // end namespace reassociate
69327952Sdim
70303231Sdim/// Reassociate commutative expressions.
71303231Sdimclass ReassociatePass : public PassInfoMixin<ReassociatePass> {
72341825Sdimpublic:
73341825Sdim  using OrderedSet =
74341825Sdim      SetVector<AssertingVH<Instruction>, std::deque<AssertingVH<Instruction>>>;
75341825Sdim
76341825Sdimprotected:
77303231Sdim  DenseMap<BasicBlock *, unsigned> RankMap;
78303231Sdim  DenseMap<AssertingVH<Value>, unsigned> ValueRankMap;
79341825Sdim  OrderedSet RedoInsts;
80327952Sdim
81327952Sdim  // Arbitrary, but prevents quadratic behavior.
82327952Sdim  static const unsigned GlobalReassociateLimit = 10;
83327952Sdim  static const unsigned NumBinaryOps =
84327952Sdim      Instruction::BinaryOpsEnd - Instruction::BinaryOpsBegin;
85327952Sdim
86353358Sdim  struct PairMapValue {
87353358Sdim    WeakVH Value1;
88353358Sdim    WeakVH Value2;
89353358Sdim    unsigned Score;
90353358Sdim    bool isValid() const { return Value1 && Value2; }
91353358Sdim  };
92353358Sdim  DenseMap<std::pair<Value *, Value *>, PairMapValue> PairMap[NumBinaryOps];
93353358Sdim
94303231Sdim  bool MadeChange;
95303231Sdim
96303231Sdimpublic:
97303231Sdim  PreservedAnalyses run(Function &F, FunctionAnalysisManager &);
98303231Sdim
99303231Sdimprivate:
100314564Sdim  void BuildRankMap(Function &F, ReversePostOrderTraversal<Function *> &RPOT);
101303231Sdim  unsigned getRank(Value *V);
102303231Sdim  void canonicalizeOperands(Instruction *I);
103303231Sdim  void ReassociateExpression(BinaryOperator *I);
104303231Sdim  void RewriteExprTree(BinaryOperator *I,
105303231Sdim                       SmallVectorImpl<reassociate::ValueEntry> &Ops);
106303231Sdim  Value *OptimizeExpression(BinaryOperator *I,
107303231Sdim                            SmallVectorImpl<reassociate::ValueEntry> &Ops);
108303231Sdim  Value *OptimizeAdd(Instruction *I,
109303231Sdim                     SmallVectorImpl<reassociate::ValueEntry> &Ops);
110303231Sdim  Value *OptimizeXor(Instruction *I,
111303231Sdim                     SmallVectorImpl<reassociate::ValueEntry> &Ops);
112303231Sdim  bool CombineXorOpnd(Instruction *I, reassociate::XorOpnd *Opnd1,
113303231Sdim                      APInt &ConstOpnd, Value *&Res);
114303231Sdim  bool CombineXorOpnd(Instruction *I, reassociate::XorOpnd *Opnd1,
115303231Sdim                      reassociate::XorOpnd *Opnd2, APInt &ConstOpnd,
116303231Sdim                      Value *&Res);
117303231Sdim  Value *buildMinimalMultiplyDAG(IRBuilder<> &Builder,
118303231Sdim                                 SmallVectorImpl<reassociate::Factor> &Factors);
119303231Sdim  Value *OptimizeMul(BinaryOperator *I,
120303231Sdim                     SmallVectorImpl<reassociate::ValueEntry> &Ops);
121303231Sdim  Value *RemoveFactorFromExpression(Value *V, Value *Factor);
122303231Sdim  void EraseInst(Instruction *I);
123341825Sdim  void RecursivelyEraseDeadInsts(Instruction *I, OrderedSet &Insts);
124303231Sdim  void OptimizeInst(Instruction *I);
125360784Sdim  Instruction *canonicalizeNegFPConstantsForOp(Instruction *I, Instruction *Op,
126360784Sdim                                               Value *OtherOp);
127360784Sdim  Instruction *canonicalizeNegFPConstants(Instruction *I);
128327952Sdim  void BuildPairMap(ReversePostOrderTraversal<Function *> &RPOT);
129303231Sdim};
130303231Sdim
131327952Sdim} // end namespace llvm
132327952Sdim
133303231Sdim#endif // LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
134