[PATCH] D82108: [IndVarSimplify] Don't replace IV user with unsafe loop-invariant (PR45360)

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 18 10:19:12 PDT 2020


lebedev.ri created this revision.
lebedev.ri added reviewers: mkazantsev, reames, helloqirun.
lebedev.ri added a project: LLVM.
Herald added a subscriber: hiraditya.

As PR45360 <https://bugs.llvm.org/show_bug.cgi?id=45360> reports,
with new cost-model we can sometimes end up being able to expand `udiv`/`urem` instructions.
And that exposes at least one instance of when we do that
regardless of whether or not it is safe to do.
In this particular case, it's `SimplifyIndvar::replaceIVUserWithLoopInvariant()`.

It seems to me, we simply need to check with `isSafeToExpandAt()` first,
but i may be missing something.

The test isn't great. I'm not sure how to make it only run `-indvars`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D82108

Files:
  llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
  llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll


Index: llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll
+++ llvm/test/Transforms/IndVarSimplify/X86/pr45360.ll
@@ -26,7 +26,6 @@
 ; CHECK:       bb19:
 ; CHECK-NEXT:    [[I8_LCSSA9:%.*]] = phi i32 [ [[D_PROMOTED8]], [[BB:%.*]] ], [ [[I8:%.*]], [[BB27:%.*]] ]
 ; CHECK-NEXT:    [[I8]] = and i32 [[I8_LCSSA9]], [[I6]]
-; CHECK-NEXT:    [[TMP0:%.*]] = urem i32 [[I24]], [[I8]]
 ; CHECK-NEXT:    [[I21:%.*]] = icmp eq i32 [[I8]], 0
 ; CHECK-NEXT:    br i1 [[I21]], label [[BB27_THREAD:%.*]], label [[BB27]]
 ; CHECK:       bb27.thread:
@@ -35,8 +34,9 @@
 ; CHECK-NEXT:    store i32 0, i32* @c, align 4
 ; CHECK-NEXT:    br label [[BB32:%.*]]
 ; CHECK:       bb27:
-; CHECK-NEXT:    store i32 [[TMP0]], i32* @e, align 4
-; CHECK-NEXT:    [[I30:%.*]] = icmp eq i32 [[TMP0]], 0
+; CHECK-NEXT:    [[I26:%.*]] = urem i32 [[I24]], [[I8]]
+; CHECK-NEXT:    store i32 [[I26]], i32* @e, align 4
+; CHECK-NEXT:    [[I30:%.*]] = icmp eq i32 [[I26]], 0
 ; CHECK-NEXT:    br i1 [[I30]], label [[BB32_LOOPEXIT:%.*]], label [[BB19]]
 ; CHECK:       bb32.loopexit:
 ; CHECK-NEXT:    store i32 -1, i32* @f, align 4
Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -673,6 +673,13 @@
     return false;
 
   auto *IP = GetLoopInvariantInsertPosition(L, I);
+
+  if (!isSafeToExpandAt(S, IP, *SE)) {
+    LLVM_DEBUG(dbgs() << "INDVARS: Can not replace IV user: " << *I
+                      << " with loop invariant: " << *S << '\n');
+    return false;
+  }
+
   auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
 
   I->replaceAllUsesWith(Invariant);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82108.271759.patch
Type: text/x-patch
Size: 1840 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200618/069bb7e8/attachment.bin>


More information about the llvm-commits mailing list