Skip to content

Commit 061cebb

Browse files
author
Evgeniy Brevnov
committed
[NFC][NARY-REASSOCIATE] Restructure code to aviod isPotentiallyReassociatable
Currently we have to duplicate the same checks in isPotentiallyReassociatable and tryReassociate. With simple pattern like add/mul this may be not a big deal. But the situation gets much worse when I try to add support for min/max. Min/Max may be represented by several instructions and can take different forms. In order reduce complexity for upcoming min/max support we need to restructure the code a bit to avoid mentioned code duplication. Reviewed By: mkazantsev Differential Revision: https://reviews.llvm.org/D88286
1 parent f61c29b commit 061cebb

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

llvm/include/llvm/Transforms/Scalar/NaryReassociate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class NaryReassociatePass : public PassInfoMixin<NaryReassociatePass> {
114114
bool doOneIteration(Function &F);
115115

116116
// Reassociates I for better CSE.
117-
Instruction *tryReassociate(Instruction *I);
117+
Instruction *tryReassociate(Instruction *I, const SCEV *&OrigSCEV);
118118

119119
// Reassociate GEP for better CSE.
120120
Instruction *tryReassociateGEP(GetElementPtrInst *GEP);

llvm/lib/Transforms/Scalar/NaryReassociate.cpp

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,6 @@ bool NaryReassociatePass::runImpl(Function &F, AssumptionCache *AC_,
213213
return Changed;
214214
}
215215

216-
// Explicitly list the instruction types NaryReassociate handles for now.
217-
static bool isPotentiallyNaryReassociable(Instruction *I) {
218-
switch (I->getOpcode()) {
219-
case Instruction::Add:
220-
case Instruction::GetElementPtr:
221-
case Instruction::Mul:
222-
return true;
223-
default:
224-
return false;
225-
}
226-
}
227-
228216
bool NaryReassociatePass::doOneIteration(Function &F) {
229217
bool Changed = false;
230218
SeenExprs.clear();
@@ -236,13 +224,8 @@ bool NaryReassociatePass::doOneIteration(Function &F) {
236224
BasicBlock *BB = Node->getBlock();
237225
for (auto I = BB->begin(); I != BB->end(); ++I) {
238226
Instruction *OrigI = &*I;
239-
240-
if (!SE->isSCEVable(OrigI->getType()) ||
241-
!isPotentiallyNaryReassociable(OrigI))
242-
continue;
243-
244-
const SCEV *OrigSCEV = SE->getSCEV(OrigI);
245-
if (Instruction *NewI = tryReassociate(OrigI)) {
227+
const SCEV *OrigSCEV = nullptr;
228+
if (Instruction *NewI = tryReassociate(OrigI, OrigSCEV)) {
246229
Changed = true;
247230
OrigI->replaceAllUsesWith(NewI);
248231

@@ -274,7 +257,7 @@ bool NaryReassociatePass::doOneIteration(Function &F) {
274257
// nary-gep.ll.
275258
if (NewSCEV != OrigSCEV)
276259
SeenExprs[OrigSCEV].push_back(WeakTrackingVH(NewI));
277-
} else
260+
} else if (OrigSCEV)
278261
SeenExprs[OrigSCEV].push_back(WeakTrackingVH(OrigI));
279262
}
280263
}
@@ -286,16 +269,26 @@ bool NaryReassociatePass::doOneIteration(Function &F) {
286269
return Changed;
287270
}
288271

289-
Instruction *NaryReassociatePass::tryReassociate(Instruction *I) {
272+
Instruction *NaryReassociatePass::tryReassociate(Instruction * I,
273+
const SCEV *&OrigSCEV) {
274+
275+
if (!SE->isSCEVable(I->getType()))
276+
return nullptr;
277+
290278
switch (I->getOpcode()) {
291279
case Instruction::Add:
292280
case Instruction::Mul:
281+
OrigSCEV = SE->getSCEV(I);
293282
return tryReassociateBinaryOp(cast<BinaryOperator>(I));
294283
case Instruction::GetElementPtr:
284+
OrigSCEV = SE->getSCEV(I);
295285
return tryReassociateGEP(cast<GetElementPtrInst>(I));
296286
default:
297-
llvm_unreachable("should be filtered out by isPotentiallyNaryReassociable");
287+
return nullptr;
298288
}
289+
290+
llvm_unreachable("should not be reached");
291+
return nullptr;
299292
}
300293

301294
static bool isGEPFoldable(GetElementPtrInst *GEP,

0 commit comments

Comments
 (0)