LLVM 24.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"
29#include "llvm/IR/BasicBlock.h"
30#include "llvm/IR/PassManager.h"
31#include "llvm/IR/ValueHandle.h"
33#include <deque>
34
35namespace llvm {
36
37class APInt;
38class BasicBlock;
39class BinaryOperator;
40class Function;
41class Instruction;
42class IRBuilderBase;
43class Value;
44struct OverflowTracking;
45
46/// A private "module" namespace for types and utilities used by Reassociate.
47/// These are implementation details and should not be used by clients.
48namespace reassociate {
49
50struct ValueEntry {
51 unsigned Rank;
53
54 ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {}
55};
56
57inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) {
58 return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start.
59}
60
61/// Utility class representing a base and exponent pair which form one
62/// factor of some product.
63struct Factor {
65 unsigned Power;
66
67 Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
68};
69
70class XorOpnd;
71
72} // end namespace reassociate
73
74/// Reassociate commutative expressions.
75class ReassociatePass : public OptionalPassInfoMixin<ReassociatePass> {
76public:
77 using OrderedSet =
78 SetVector<AssertingVH<Instruction>, std::deque<AssertingVH<Instruction>>>;
79
80protected:
84
85 // Arbitrary, but prevents quadratic behavior.
86 static const unsigned GlobalReassociateLimit = 10;
87 static const unsigned NumBinaryOps =
88 Instruction::BinaryOpsEnd - Instruction::BinaryOpsBegin;
89
90 struct PairMapValue {
93 unsigned Score;
94 bool isValid() const { return Value1 && Value2; }
95 };
97
99 UniformityInfo *UA = nullptr;
100 bool UseCSELocal = false;
101
102public:
104
106
107private:
108 void BuildRankMap(Function &F, ReversePostOrderTraversal<Function *> &RPOT);
109 unsigned getRank(Value *V);
110 void canonicalizeOperands(Instruction *I);
111 void ReassociateExpression(BinaryOperator *I);
112 void RewriteExprTree(BinaryOperator *I,
114 OverflowTracking Flags);
115 Value *OptimizeExpression(BinaryOperator *I,
117 Value *OptimizeAdd(Instruction *I,
119 Value *OptimizeXor(Instruction *I,
121 bool CombineXorOpnd(BasicBlock::iterator It, reassociate::XorOpnd *Opnd1,
122 APInt &ConstOpnd, Value *&Res);
123 bool CombineXorOpnd(BasicBlock::iterator It, reassociate::XorOpnd *Opnd1,
124 reassociate::XorOpnd *Opnd2, APInt &ConstOpnd,
125 Value *&Res);
126 Value *buildMinimalMultiplyDAG(IRBuilderBase &Builder,
128 Value *OptimizeMul(BinaryOperator *I,
130 Value *RemoveFactorFromExpression(Value *V, Value *Factor, DebugLoc DL);
131 void EraseInst(Instruction *I);
132 void RecursivelyEraseDeadInsts(Instruction *I, OrderedSet &Insts);
133 void OptimizeInst(Instruction *I);
134 Instruction *canonicalizeNegFPConstantsForOp(Instruction *I, Instruction *Op,
135 Value *OtherOp);
136 Instruction *canonicalizeNegFPConstants(Instruction *I);
137 void BuildPairMap(ReversePostOrderTraversal<Function *> &RPOT);
138};
139
140} // end namespace llvm
141
142#endif // LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
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.
LLVM IR instance of the generic uniformity analysis.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM Basic Block Representation.
Definition BasicBlock.h:62
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
A debug info location.
Definition DebugLoc.h:126
Common base class shared among various IRBuilders.
Definition IRBuilder.h:111
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Reassociate commutative expressions.
Definition Reassociate.h:75
DenseMap< BasicBlock *, unsigned > RankMap
Definition Reassociate.h:81
DenseMap< AssertingVH< Value >, unsigned > ValueRankMap
Definition Reassociate.h:82
LLVM_ABI PreservedAnalyses runImpl(Function &F, UniformityInfo &UI)
UniformityInfo * UA
Definition Reassociate.h:99
static const unsigned GlobalReassociateLimit
Definition Reassociate.h:86
SetVector< AssertingVH< Instruction >, std::deque< AssertingVH< Instruction > > > OrderedSet
Definition Reassociate.h:77
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static const unsigned NumBinaryOps
Definition Reassociate.h:87
DenseMap< std::pair< Value *, Value * >, PairMapValue > PairMap[NumBinaryOps]
Definition Reassociate.h:96
A vector that has set insertion semantics.
Definition SetVector.h:57
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
LLVM Value Representation.
Definition Value.h:75
A nullable Value handle that is nullable.
Utility class representing a non-constant Xor-operand.
A private "module" namespace for types and utilities used by Reassociate.
Definition Reassociate.h:48
bool operator<(const ValueEntry &LHS, const ValueEntry &RHS)
Definition Reassociate.h:57
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
DWARFExpression::Operation Op
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A CRTP mix-in for passes that can be skipped.
Factor(Value *Base, unsigned Power)
Definition Reassociate.h:67
ValueEntry(unsigned R, Value *O)
Definition Reassociate.h:54