LLVM  4.0.0
Reassociate.h
Go to the documentation of this file.
1 //===- Reassociate.h - Reassociate binary expressions -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass reassociates commutative expressions in an order that is designed
11 // to promote better constant propagation, GCSE, LICM, PRE, etc.
12 //
13 // For example: 4 + (x + 5) -> x + (4 + 5)
14 //
15 // In the implementation of this algorithm, constants are assigned rank = 0,
16 // function arguments are rank = 1, and other values are assigned ranks
17 // corresponding to the reverse post order traversal of current function
18 // (starting at 2), which effectively gives values in deep loops higher rank
19 // than values not in loops.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
24 #define LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
25 
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/IR/IRBuilder.h"
29 #include "llvm/IR/Operator.h"
30 #include "llvm/IR/PassManager.h"
31 
32 namespace llvm {
33 
34 /// A private "module" namespace for types and utilities used by Reassociate.
35 /// These are implementation details and should not be used by clients.
36 namespace reassociate {
37 struct ValueEntry {
38  unsigned Rank;
40  ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {}
41 };
42 inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) {
43  return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start.
44 }
45 
46 /// \brief Utility class representing a base and exponent pair which form one
47 /// factor of some product.
48 struct Factor {
50  unsigned Power;
51  Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
52 };
53 
54 class XorOpnd;
55 }
56 
57 /// Reassociate commutative expressions.
58 class ReassociatePass : public PassInfoMixin<ReassociatePass> {
60  DenseMap<AssertingVH<Value>, unsigned> ValueRankMap;
62  bool MadeChange;
63 
64 public:
66 
67 private:
68  void BuildRankMap(Function &F, ReversePostOrderTraversal<Function *> &RPOT);
69  unsigned getRank(Value *V);
70  void canonicalizeOperands(Instruction *I);
71  void ReassociateExpression(BinaryOperator *I);
72  void RewriteExprTree(BinaryOperator *I,
74  Value *OptimizeExpression(BinaryOperator *I,
76  Value *OptimizeAdd(Instruction *I,
78  Value *OptimizeXor(Instruction *I,
80  bool CombineXorOpnd(Instruction *I, reassociate::XorOpnd *Opnd1,
81  APInt &ConstOpnd, Value *&Res);
82  bool CombineXorOpnd(Instruction *I, reassociate::XorOpnd *Opnd1,
83  reassociate::XorOpnd *Opnd2, APInt &ConstOpnd,
84  Value *&Res);
85  bool collectMultiplyFactors(SmallVectorImpl<reassociate::ValueEntry> &Ops,
87  Value *buildMinimalMultiplyDAG(IRBuilder<> &Builder,
89  Value *OptimizeMul(BinaryOperator *I,
91  Value *RemoveFactorFromExpression(Value *V, Value *Factor);
92  void EraseInst(Instruction *I);
93  void RecursivelyEraseDeadInsts(Instruction *I,
95  void OptimizeInst(Instruction *I);
96  Instruction *canonicalizeNegConstExpr(Instruction *I);
97 };
98 }
99 
100 #endif // LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
Utility class representing a non-constant Xor-operand.
Definition: Reassociate.cpp:77
nary reassociate
Utility class representing a base and exponent pair which form one factor of some product...
Definition: Reassociate.h:48
Reassociate commutative expressions.
Definition: Reassociate.h:58
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:588
bool operator<(const ValueEntry &LHS, const ValueEntry &RHS)
Definition: Reassociate.h:42
#define F(x, y, z)
Definition: MD5.cpp:51
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
ValueEntry(unsigned R, Value *O)
Definition: Reassociate.h:40
Factor(Value *Base, unsigned Power)
Definition: Reassociate.h:51
Value handle that asserts if the Value is deleted.
Definition: ValueHandle.h:182
Class for arbitrary precision integers.
Definition: APInt.h:77
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:71
A vector that has set insertion semantics.
Definition: SetVector.h:41
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)