[PATCH] D121950: [IndVars] Teach replaceCongruentIVs to avoid scrambling induction variables
Eli Friedman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 17 14:04:28 PDT 2022
efriedma created this revision.
efriedma added reviewers: reames, mkazantsev, dmakogon, fhahn, nikic, lebedev.ri, sanjoy.
Herald added a subscriber: hiraditya.
Herald added a project: All.
efriedma requested review of this revision.
Herald added a project: LLVM.
replaceCongruentIVs analysis is based on ScalarEvolution; this makes comparing different PHIs and performing the replacement straightforward. However, it can have some side-effects: it isn't aware whether an induction variable is in canonical form, so it can perform replacements which obscure the meaning of the IR.
In test22 in widen-loop-comp.ll, the resulting loop can't be analyzed by ScalarEvolution at all.
My attempted solution is to restrict the transform: don't try to replace induction variables using PHI nodes that don't represent simple induction variables.
I'm not sure if this is the best solution; suggestions welcome.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D121950
Files:
llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
llvm/test/Transforms/IndVarSimplify/AArch64/widen-loop-comp.ll
Index: llvm/test/Transforms/IndVarSimplify/AArch64/widen-loop-comp.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/AArch64/widen-loop-comp.ll
+++ llvm/test/Transforms/IndVarSimplify/AArch64/widen-loop-comp.ll
@@ -1446,9 +1446,9 @@
; CHECK-NEXT: store i16 0, i16* [[PTR:%.*]], align 4
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
-; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
-; CHECK-NEXT: [[INDVARS:%.*]] = trunc i32 [[IV]] to i16
-; CHECK-NEXT: [[VAL_INC:%.*]] = add i16 [[INDVARS]], 1
+; CHECK-NEXT: [[VAL:%.*]] = phi i16 [ [[VAL_INC:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], [[LOOP]] ], [ 0, [[ENTRY]] ]
+; CHECK-NEXT: [[VAL_INC]] = add i16 [[VAL]], 1
; CHECK-NEXT: store i16 [[VAL_INC]], i16* [[PTR]], align 4
; CHECK-NEXT: [[IV_WIDE:%.*]] = zext i32 [[IV]] to i64
; CHECK-NEXT: call void @foo(i64 [[IV_WIDE]])
Index: llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
===================================================================
--- llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -2070,11 +2070,14 @@
OrigPhiRef = Phi;
if (Phi->getType()->isIntegerTy() && TTI &&
TTI->isTruncateFree(Phi->getType(), Phis.back()->getType())) {
- // This phi can be freely truncated to the narrowest phi type. Map the
- // truncated expression to it so it will be reused for narrow types.
- const SCEV *TruncExpr =
- SE.getTruncateExpr(SE.getSCEV(Phi), Phis.back()->getType());
- ExprToIVMap[TruncExpr] = Phi;
+ const SCEV *PhiExpr = SE.getSCEV(Phi);
+ if (isa<SCEVAddRecExpr>(PhiExpr)) {
+ // This phi can be freely truncated to the narrowest phi type. Map the
+ // truncated expression to it so it will be reused for narrow types.
+ const SCEV *TruncExpr =
+ SE.getTruncateExpr(PhiExpr, Phis.back()->getType());
+ ExprToIVMap[TruncExpr] = Phi;
+ }
}
continue;
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121950.416310.patch
Type: text/x-patch
Size: 2187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220317/279dfdb7/attachment.bin>
More information about the llvm-commits
mailing list