LLVM 20.0.0git
LoopUnrollAnalyzer.cpp
Go to the documentation of this file.
1//===- LoopUnrollAnalyzer.cpp - Unrolling Effect Estimation -----*- 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 file implements UnrolledInstAnalyzer class. It's used for predicting
10// potential effects that loop unrolling might have, such as enabling constant
11// propagation and other optimizations.
12//
13//===----------------------------------------------------------------------===//
14
20#include "llvm/IR/Operator.h"
21
22using namespace llvm;
23
24/// Try to simplify instruction \param I using its SCEV expression.
25///
26/// The idea is that some AddRec expressions become constants, which then
27/// could trigger folding of other instructions. However, that only happens
28/// for expressions whose start value is also constant, which isn't always the
29/// case. In another common and important case the start value is just some
30/// address (i.e. SCEVUnknown) - in this case we compute the offset and save
31/// it along with the base address instead.
32bool UnrolledInstAnalyzer::simplifyInstWithSCEV(Instruction *I) {
33 if (!SE.isSCEVable(I->getType()))
34 return false;
35
36 const SCEV *S = SE.getSCEV(I);
37 if (auto *SC = dyn_cast<SCEVConstant>(S)) {
38 SimplifiedValues[I] = SC->getValue();
39 return true;
40 }
41
42 // If we have a loop invariant computation, we only need to compute it once.
43 // Given that, all but the first occurance are free.
44 if (!IterationNumber->isZero() && SE.isLoopInvariant(S, L))
45 return true;
46
47 auto *AR = dyn_cast<SCEVAddRecExpr>(S);
48 if (!AR || AR->getLoop() != L)
49 return false;
50
51 const SCEV *ValueAtIteration = AR->evaluateAtIteration(IterationNumber, SE);
52 // Check if the AddRec expression becomes a constant.
53 if (auto *SC = dyn_cast<SCEVConstant>(ValueAtIteration)) {
54 SimplifiedValues[I] = SC->getValue();
55 return true;
56 }
57
58 // Check if the offset from the base address becomes a constant.
59 auto *Base = dyn_cast<SCEVUnknown>(SE.getPointerBase(S));
60 if (!Base)
61 return false;
62 std::optional<APInt> Offset =
63 SE.computeConstantDifference(ValueAtIteration, Base);
64 if (!Offset)
65 return false;
66 SimplifiedAddress Address;
67 Address.Base = Base->getValue();
68 Address.Offset = *Offset;
69 SimplifiedAddresses[I] = Address;
70 return false;
71}
72
73/// Try to simplify binary operator I.
74///
75/// TODO: Probably it's worth to hoist the code for estimating the
76/// simplifications effects to a separate class, since we have a very similar
77/// code in InlineCost already.
78bool UnrolledInstAnalyzer::visitBinaryOperator(BinaryOperator &I) {
79 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
80 if (!isa<Constant>(LHS))
81 if (Value *SimpleLHS = SimplifiedValues.lookup(LHS))
82 LHS = SimpleLHS;
83 if (!isa<Constant>(RHS))
84 if (Value *SimpleRHS = SimplifiedValues.lookup(RHS))
85 RHS = SimpleRHS;
86
87 Value *SimpleV = nullptr;
88 const DataLayout &DL = I.getDataLayout();
89 if (auto FI = dyn_cast<FPMathOperator>(&I))
90 SimpleV =
91 simplifyBinOp(I.getOpcode(), LHS, RHS, FI->getFastMathFlags(), DL);
92 else
93 SimpleV = simplifyBinOp(I.getOpcode(), LHS, RHS, DL);
94
95 if (SimpleV) {
96 SimplifiedValues[&I] = SimpleV;
97 return true;
98 }
100}
101
102/// Try to fold load I.
103bool UnrolledInstAnalyzer::visitLoad(LoadInst &I) {
104 Value *AddrOp = I.getPointerOperand();
105
106 auto AddressIt = SimplifiedAddresses.find(AddrOp);
107 if (AddressIt == SimplifiedAddresses.end())
108 return false;
109
110 auto *GV = dyn_cast<GlobalVariable>(AddressIt->second.Base);
111 // We're only interested in loads that can be completely folded to a
112 // constant.
113 if (!GV || !GV->hasDefinitiveInitializer() || !GV->isConstant())
114 return false;
115
116 Constant *Res =
117 ConstantFoldLoadFromConst(GV->getInitializer(), I.getType(),
118 AddressIt->second.Offset, I.getDataLayout());
119 if (!Res)
120 return false;
121
122 SimplifiedValues[&I] = Res;
123 return true;
124}
125
126/// Try to simplify cast instruction.
127bool UnrolledInstAnalyzer::visitCastInst(CastInst &I) {
128 Value *Op = I.getOperand(0);
129 if (Value *Simplified = SimplifiedValues.lookup(Op))
130 Op = Simplified;
131
132 // The cast can be invalid, because SimplifiedValues contains results of SCEV
133 // analysis, which operates on integers (and, e.g., might convert i8* null to
134 // i32 0).
135 if (CastInst::castIsValid(I.getOpcode(), Op, I.getType())) {
136 const DataLayout &DL = I.getDataLayout();
137 if (Value *V = simplifyCastInst(I.getOpcode(), Op, I.getType(), DL)) {
138 SimplifiedValues[&I] = V;
139 return true;
140 }
141 }
142
143 return Base::visitCastInst(I);
144}
145
146/// Try to simplify cmp instruction.
147bool UnrolledInstAnalyzer::visitCmpInst(CmpInst &I) {
148 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
149
150 // First try to handle simplified comparisons.
151 if (!isa<Constant>(LHS))
152 if (Value *SimpleLHS = SimplifiedValues.lookup(LHS))
153 LHS = SimpleLHS;
154 if (!isa<Constant>(RHS))
155 if (Value *SimpleRHS = SimplifiedValues.lookup(RHS))
156 RHS = SimpleRHS;
157
158 if (!isa<Constant>(LHS) && !isa<Constant>(RHS) && !I.isSigned()) {
159 auto SimplifiedLHS = SimplifiedAddresses.find(LHS);
160 if (SimplifiedLHS != SimplifiedAddresses.end()) {
161 auto SimplifiedRHS = SimplifiedAddresses.find(RHS);
162 if (SimplifiedRHS != SimplifiedAddresses.end()) {
163 SimplifiedAddress &LHSAddr = SimplifiedLHS->second;
164 SimplifiedAddress &RHSAddr = SimplifiedRHS->second;
165 if (LHSAddr.Base == RHSAddr.Base) {
166 // FIXME: This is only correct for equality predicates. For
167 // unsigned predicates, this only holds if we have nowrap flags,
168 // which we don't track (for nuw it's valid as-is, for nusw it
169 // requires converting the predicated to signed). As this is used only
170 // for cost modelling, this is not a correctness issue.
171 bool Res = ICmpInst::compare(LHSAddr.Offset, RHSAddr.Offset,
172 I.getPredicate());
173 SimplifiedValues[&I] = ConstantInt::getBool(I.getType(), Res);
174 return true;
175 }
176 }
177 }
178 }
179
180 const DataLayout &DL = I.getDataLayout();
181 if (Value *V = simplifyCmpInst(I.getPredicate(), LHS, RHS, DL)) {
182 SimplifiedValues[&I] = V;
183 return true;
184 }
185
186 return Base::visitCmpInst(I);
187}
188
189bool UnrolledInstAnalyzer::visitPHINode(PHINode &PN) {
190 // Run base visitor first. This way we can gather some useful for later
191 // analysis information.
192 if (Base::visitPHINode(PN))
193 return true;
194
195 // The loop induction PHI nodes are definitionally free.
196 return PN.getParent() == L->getHeader();
197}
198
199bool UnrolledInstAnalyzer::visitInstruction(Instruction &I) {
200 return simplifyInstWithSCEV(&I);
201}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define I(x, y, z)
Definition: MD5.cpp:58
Value * RHS
Value * LHS
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:444
static bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy)
This method can be used to determine if a cast from SrcTy to DstTy using Opcode op is valid or not.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:661
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition: Constants.cpp:880
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
An instruction for reading from memory.
Definition: Instructions.h:176
BlockT * getHeader() const
This class represents an analyzed expression in the program.
bool isZero() const
Return true if the expression is a constant zero.
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
std::optional< APInt > computeConstantDifference(const SCEV *LHS, const SCEV *RHS)
Compute LHS - RHS and returns the result as an APInt if it is a constant, and std::nullopt if it isn'...
LLVM Value Representation.
Definition: Value.h:74
const ParentTy * getParent() const
Definition: ilist_node.h:32
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
Value * simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, const SimplifyQuery &Q)
Given operands for a CastInst, fold the result or return null.
Constant * ConstantFoldLoadFromConst(Constant *C, Type *Ty, const APInt &Offset, const DataLayout &DL)
Extract value of C at the given Offset reinterpreted as Ty.
Value * simplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a BinaryOperator, fold the result or return null.
Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.