[PATCH] D141114: [LoopFlattening] Check for extra uses on Mul

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 6 01:04:53 PST 2023


dmgreen created this revision.
dmgreen added reviewers: SjoerdMeijer, fhahn, ostannard.
Herald added a subscriber: hiraditya.
Herald added a project: All.
dmgreen requested review of this revision.
Herald added a project: LLVM.

Similar to D138404 <https://reviews.llvm.org/D138404>, we were not guarding against extra uses of the Mul. In most cases other checks would catch the issue due to unsupported instructions in the outer loop, but certain non-canonical loop forms could still get through.

Fixes #59339


https://reviews.llvm.org/D141114

Files:
  llvm/lib/Transforms/Scalar/LoopFlatten.cpp
  llvm/test/Transforms/LoopFlatten/pr59339.ll


Index: llvm/test/Transforms/LoopFlatten/pr59339.ll
===================================================================
--- llvm/test/Transforms/LoopFlatten/pr59339.ll
+++ llvm/test/Transforms/LoopFlatten/pr59339.ll
@@ -7,7 +7,6 @@
 
 define void @test0(i16* %0, i16* %1) {
 ; CHECK-LABEL: @test0(
-; CHECK-NEXT:    [[FLATTEN_TRIPCOUNT:%.*]] = mul i64 3, 2
 ; CHECK-NEXT:    br label [[DOTPREHEADER:%.*]]
 ; CHECK:       .preheader:
 ; CHECK-NEXT:    [[TMP3:%.*]] = phi i64 [ 0, [[TMP2:%.*]] ], [ [[TMP7:%.*]], [[TMP6:%.*]] ]
@@ -16,17 +15,17 @@
 ; CHECK-NEXT:    br label [[TMP9:%.*]]
 ; CHECK:       6:
 ; CHECK-NEXT:    [[TMP7]] = add i64 [[TMP3]], 1
-; CHECK-NEXT:    [[TMP8:%.*]] = icmp slt i64 [[TMP7]], [[FLATTEN_TRIPCOUNT]]
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp slt i64 [[TMP7]], 2
 ; CHECK-NEXT:    br i1 [[TMP8]], label [[DOTPREHEADER]], label [[TMP16:%.*]]
 ; CHECK:       9:
-; CHECK-NEXT:    [[TMP10:%.*]] = phi i64 [ 0, [[DOTPREHEADER]] ]
+; CHECK-NEXT:    [[TMP10:%.*]] = phi i64 [ 0, [[DOTPREHEADER]] ], [ [[TMP14:%.*]], [[TMP9]] ]
 ; CHECK-NEXT:    [[TMP11:%.*]] = load i16, ptr [[TMP5]], align 2
 ; CHECK-NEXT:    [[TMP12:%.*]] = add i64 [[TMP10]], [[TMP4]]
-; CHECK-NEXT:    [[TMP13:%.*]] = getelementptr i16, ptr [[TMP0:%.*]], i64 [[TMP3]]
+; CHECK-NEXT:    [[TMP13:%.*]] = getelementptr i16, ptr [[TMP0:%.*]], i64 [[TMP12]]
 ; CHECK-NEXT:    store i16 [[TMP11]], ptr [[TMP13]], align 2
-; CHECK-NEXT:    [[TMP14:%.*]] = add nuw nsw i64 [[TMP10]], 1
+; CHECK-NEXT:    [[TMP14]] = add nuw nsw i64 [[TMP10]], 1
 ; CHECK-NEXT:    [[TMP15:%.*]] = icmp ult i64 [[TMP14]], 3
-; CHECK-NEXT:    br label [[TMP6]]
+; CHECK-NEXT:    br i1 [[TMP15]], label [[TMP9]], label [[TMP6]]
 ; CHECK:       16:
 ; CHECK-NEXT:    ret void
 ;
Index: llvm/lib/Transforms/Scalar/LoopFlatten.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopFlatten.cpp
+++ llvm/lib/Transforms/Scalar/LoopFlatten.cpp
@@ -215,6 +215,15 @@
     LLVM_DEBUG(dbgs() << "Matched multiplication: "; MatchedMul->dump());
     LLVM_DEBUG(dbgs() << "Matched iteration count: "; MatchedItCount->dump());
 
+    // The mul should not have any other uses. Widening may leave trivially dead
+    // uses, which can be ignored.
+    if (count_if(MatchedMul->users(), [](User *U) {
+          return !isInstructionTriviallyDead(cast<Instruction>(U));
+        }) > 1) {
+      LLVM_DEBUG(dbgs() << "Multiply has more than one use\n");
+      return false;
+    }
+
     // Look through extends if the IV has been widened. Don't look through
     // extends if we already looked through a trunc.
     if (Widened && IsAdd &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D141114.486767.patch
Type: text/x-patch
Size: 2643 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230106/6b886e7a/attachment.bin>


More information about the llvm-commits mailing list