LLVM 22.0.0git
Reassociate.h
Go to the documentation of this file.
1//===- Reassociate.h - Reassociate binary expressions -----------*- 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 pass reassociates commutative expressions in an order that is designed
10// to promote better constant propagation, GCSE, LICM, PRE, etc.
11//
12// For example: 4 + (x + 5) -> x + (4 + 5)
13//
14// In the implementation of this algorithm, constants are assigned rank = 0,
15// function arguments are rank = 1, and other values are assigned ranks
16// corresponding to the reverse post order traversal of current function
17// (starting at 2), which effectively gives values in deep loops higher rank
18// than values not in loops.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
23#define LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
24
25#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/SetVector.h"
28#include "llvm/IR/BasicBlock.h"
29#include "llvm/IR/PassManager.h"
30#include "llvm/IR/ValueHandle.h"
32#include <deque>
33
34namespace llvm {
35
36class APInt;
37class BasicBlock;
38class BinaryOperator;
39class Function;
40class Instruction;
41class IRBuilderBase;
42class Value;
43struct OverflowTracking;
44
45/// A private "module" namespace for types and utilities used by Reassociate.
46/// These are implementation details and should not be used by clients.
47namespace reassociate {
48
49struct ValueEntry {
50 unsigned Rank;
52
53 ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {}
54};
55
56inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) {
57 return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start.
58}
59
60/// Utility class representing a base and exponent pair which form one
61/// factor of some product.
62struct Factor {
64 unsigned Power;
65
66 Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
67};
68
69class XorOpnd;
70
71} // end namespace reassociate
72
73/// Reassociate commutative expressions.
74class ReassociatePass : public PassInfoMixin<ReassociatePass> {
75public:
76 using OrderedSet =
77 SetVector<AssertingVH<Instruction>, std::deque<AssertingVH<Instruction>>>;
78
79protected:
83
84 // Arbitrary, but prevents quadratic behavior.
85 static const unsigned GlobalReassociateLimit = 10;
86 static const unsigned NumBinaryOps =
87 Instruction::BinaryOpsEnd - Instruction::BinaryOpsBegin;
88
89 struct PairMapValue {
92 unsigned Score;
93 bool isValid() const { return Value1 && Value2; }
94 };
96
98
99public:
101
102private:
103 void BuildRankMap(Function &F, ReversePostOrderTraversal<Function *> &RPOT);
104 unsigned getRank(Value *V);
105 void canonicalizeOperands(Instruction *I);
106 void ReassociateExpression(BinaryOperator *I);
107 void RewriteExprTree(BinaryOperator *I,
109 OverflowTracking Flags);
110 Value *OptimizeExpression(BinaryOperator *I,
112 Value *OptimizeAdd(Instruction *I,
114 Value *OptimizeXor(Instruction *I,
116 bool CombineXorOpnd(BasicBlock::iterator It, reassociate::XorOpnd *Opnd1,
117 APInt &ConstOpnd, Value *&Res);
118 bool CombineXorOpnd(BasicBlock::iterator It, reassociate::XorOpnd *Opnd1,
119 reassociate::XorOpnd *Opnd2, APInt &ConstOpnd,
120 Value *&Res);
121 Value *buildMinimalMultiplyDAG(IRBuilderBase &Builder,
123 Value *OptimizeMul(BinaryOperator *I,
125 Value *RemoveFactorFromExpression(Value *V, Value *Factor, DebugLoc DL);
126 void EraseInst(Instruction *I);
127 void RecursivelyEraseDeadInsts(Instruction *I, OrderedSet &Insts);
128 void OptimizeInst(Instruction *I);
129 Instruction *canonicalizeNegFPConstantsForOp(Instruction *I, Instruction *Op,
130 Value *OtherOp);
131 Instruction *canonicalizeNegFPConstants(Instruction *I);
132 void BuildPairMap(ReversePostOrderTraversal<Function *> &RPOT);
133};
134
135} // end namespace llvm
136
137#endif // LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
nary reassociate
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file implements a set that has insertion order iteration characteristics.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:78
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
This class represents an Operation in the Expression.
A debug info location.
Definition: DebugLoc.h:124
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
Reassociate commutative expressions.
Definition: Reassociate.h:74
DenseMap< BasicBlock *, unsigned > RankMap
Definition: Reassociate.h:80
DenseMap< AssertingVH< Value >, unsigned > ValueRankMap
Definition: Reassociate.h:81
static const unsigned GlobalReassociateLimit
Definition: Reassociate.h:85
OrderedSet RedoInsts
Definition: Reassociate.h:82
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
static const unsigned NumBinaryOps
Definition: Reassociate.h:86
DenseMap< std::pair< Value *, Value * >, PairMapValue > PairMap[NumBinaryOps]
Definition: Reassociate.h:95
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
LLVM Value Representation.
Definition: Value.h:75
A nullable Value handle that is nullable.
Definition: ValueHandle.h:145
Utility class representing a non-constant Xor-operand.
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:81
bool operator<(const ValueEntry &LHS, const ValueEntry &RHS)
Definition: Reassociate.h:56
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
Utility class representing a base and exponent pair which form one factor of some product.
Definition: Reassociate.h:62
Factor(Value *Base, unsigned Power)
Definition: Reassociate.h:66
ValueEntry(unsigned R, Value *O)
Definition: Reassociate.h:53