LLVM 19.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"
31#include <deque>
32
33namespace llvm {
34
35class APInt;
36class BasicBlock;
37class BinaryOperator;
38class Function;
39class Instruction;
40class IRBuilderBase;
41class Value;
42
43/// A private "module" namespace for types and utilities used by Reassociate.
44/// These are implementation details and should not be used by clients.
45namespace reassociate {
46
47struct ValueEntry {
48 unsigned Rank;
50
51 ValueEntry(unsigned R, Value *O) : Rank(R), Op(O) {}
52};
53
54inline bool operator<(const ValueEntry &LHS, const ValueEntry &RHS) {
55 return LHS.Rank > RHS.Rank; // Sort so that highest rank goes to start.
56}
57
58/// Utility class representing a base and exponent pair which form one
59/// factor of some product.
60struct Factor {
62 unsigned Power;
63
64 Factor(Value *Base, unsigned Power) : Base(Base), Power(Power) {}
65};
66
68 bool HasNUW;
69 bool HasNSW;
71 // Note: AllKnownNonNegative can be true in a case where one of the operands
72 // is negative, but one the operators is not NSW. AllKnownNonNegative should
73 // not be used independently of HasNSW
75};
76
77class XorOpnd;
78
79} // end namespace reassociate
80
81/// Reassociate commutative expressions.
82class ReassociatePass : public PassInfoMixin<ReassociatePass> {
83public:
84 using OrderedSet =
85 SetVector<AssertingVH<Instruction>, std::deque<AssertingVH<Instruction>>>;
86
87protected:
91
92 // Arbitrary, but prevents quadratic behavior.
93 static const unsigned GlobalReassociateLimit = 10;
94 static const unsigned NumBinaryOps =
95 Instruction::BinaryOpsEnd - Instruction::BinaryOpsBegin;
96
97 struct PairMapValue {
100 unsigned Score;
101 bool isValid() const { return Value1 && Value2; }
102 };
104
106
107public:
109
110private:
111 void BuildRankMap(Function &F, ReversePostOrderTraversal<Function *> &RPOT);
112 unsigned getRank(Value *V);
113 void canonicalizeOperands(Instruction *I);
114 void ReassociateExpression(BinaryOperator *I);
115 void RewriteExprTree(BinaryOperator *I,
118 Value *OptimizeExpression(BinaryOperator *I,
120 Value *OptimizeAdd(Instruction *I,
122 Value *OptimizeXor(Instruction *I,
124 bool CombineXorOpnd(BasicBlock::iterator It, reassociate::XorOpnd *Opnd1,
125 APInt &ConstOpnd, Value *&Res);
126 bool CombineXorOpnd(BasicBlock::iterator It, reassociate::XorOpnd *Opnd1,
127 reassociate::XorOpnd *Opnd2, APInt &ConstOpnd,
128 Value *&Res);
129 Value *buildMinimalMultiplyDAG(IRBuilderBase &Builder,
131 Value *OptimizeMul(BinaryOperator *I,
133 Value *RemoveFactorFromExpression(Value *V, Value *Factor);
134 void EraseInst(Instruction *I);
135 void RecursivelyEraseDeadInsts(Instruction *I, OrderedSet &Insts);
136 void OptimizeInst(Instruction *I);
137 Instruction *canonicalizeNegFPConstantsForOp(Instruction *I, Instruction *Op,
138 Value *OtherOp);
139 Instruction *canonicalizeNegFPConstants(Instruction *I);
140 void BuildPairMap(ReversePostOrderTraversal<Function *> &RPOT);
141};
142
143} // end namespace llvm
144
145#endif // LLVM_TRANSFORMS_SCALAR_REASSOCIATE_H
basic Basic Alias true
This file defines the DenseMap class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
nary reassociate
This header defines various interfaces for pass management in LLVM.
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:77
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:167
This class represents an Operation in the Expression.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:92
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
Reassociate commutative expressions.
Definition: Reassociate.h:82
DenseMap< BasicBlock *, unsigned > RankMap
Definition: Reassociate.h:88
DenseMap< AssertingVH< Value >, unsigned > ValueRankMap
Definition: Reassociate.h:89
static const unsigned GlobalReassociateLimit
Definition: Reassociate.h:93
OrderedSet RedoInsts
Definition: Reassociate.h:90
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
static const unsigned NumBinaryOps
Definition: Reassociate.h:94
DenseMap< std::pair< Value *, Value * >, PairMapValue > PairMap[NumBinaryOps]
Definition: Reassociate.h:103
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
LLVM Value Representation.
Definition: Value.h:74
A nullable Value handle that is nullable.
Definition: ValueHandle.h:144
Utility class representing a non-constant Xor-operand.
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
bool operator<(const ValueEntry &LHS, const ValueEntry &RHS)
Definition: Reassociate.h:54
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:69
Utility class representing a base and exponent pair which form one factor of some product.
Definition: Reassociate.h:60
Factor(Value *Base, unsigned Power)
Definition: Reassociate.h:64
ValueEntry(unsigned R, Value *O)
Definition: Reassociate.h:51