[PATCH] D90456: [IndVars] Use more precise context when eliminating narrowing

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 18 22:42:45 PST 2020


mkazantsev updated this revision to Diff 306317.
mkazantsev added a comment.

Rebased, with dom tree logic factored out into separate patch (added dependency).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D90456/new/

https://reviews.llvm.org/D90456

Files:
  llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
  llvm/test/Transforms/IndVarSimplify/widen-loop-comp.ll


Index: llvm/test/Transforms/IndVarSimplify/widen-loop-comp.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/widen-loop-comp.ll
+++ llvm/test/Transforms/IndVarSimplify/widen-loop-comp.ll
@@ -554,16 +554,13 @@
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
 ; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[BACKEDGE:%.*]] ], [ [[TMP0]], [[ENTRY:%.*]] ]
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i64 [[INDVARS_IV]] to i32
-; CHECK-NEXT:    [[IV_NEXT:%.*]] = add i32 [[TMP1]], -1
+; CHECK-NEXT:    [[TMP1:%.*]] = add nsw i64 [[INDVARS_IV]], -1
 ; CHECK-NEXT:    [[COND:%.*]] = icmp eq i64 [[INDVARS_IV]], 0
 ; CHECK-NEXT:    br i1 [[COND]], label [[EXIT:%.*]], label [[BACKEDGE]]
 ; CHECK:       backedge:
-; CHECK-NEXT:    [[INDEX:%.*]] = zext i32 [[IV_NEXT]] to i64
-; CHECK-NEXT:    [[STORE_ADDR:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[INDEX]]
+; CHECK-NEXT:    [[STORE_ADDR:%.*]] = getelementptr i32, i32* [[P:%.*]], i64 [[TMP1]]
 ; CHECK-NEXT:    store i32 1, i32* [[STORE_ADDR]], align 4
-; CHECK-NEXT:    [[LOAD_ADDR:%.*]] = getelementptr i32, i32* [[Q:%.*]], i64 [[INDEX]]
-; CHECK-NEXT:    [[STOP:%.*]] = load i32, i32* [[Q]], align 4
+; CHECK-NEXT:    [[STOP:%.*]] = load i32, i32* [[Q:%.*]], align 4
 ; CHECK-NEXT:    [[LOOP_COND:%.*]] = icmp eq i32 [[STOP]], 0
 ; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add nsw i64 [[INDVARS_IV]], -1
 ; CHECK-NEXT:    br i1 [[LOOP_COND]], label [[LOOP]], label [[FAILURE:%.*]]
Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -1540,6 +1540,32 @@
   bool CanSignExtend = ExtKind == SignExtended && OBO->hasNoSignedWrap();
   bool CanZeroExtend = ExtKind == ZeroExtended && OBO->hasNoUnsignedWrap();
   auto AnotherOpExtKind = ExtKind;
+
+  // Check that all uses are either s/zext, or narrow def (in case of we are
+  // widening the IV increment).
+  SmallVector<Instruction *, 4> ExtUsers;
+  for (Use &U : NarrowUse->uses()) {
+    if (U.getUser() == NarrowDef)
+      continue;
+    Instruction *User = nullptr;
+    if (ExtKind == SignExtended)
+      User = dyn_cast<SExtInst>(U.getUser());
+    else
+      User = dyn_cast<ZExtInst>(U.getUser());
+    if (!User || User->getType() != WideType)
+      return false;
+    ExtUsers.push_back(User);
+  }
+  // We'll prove some facts that should be true in the context of ext users. IF
+  // there is no users, we are done now. If there are some, pick their common
+  // dominator as context.
+  if (ExtUsers.empty()) {
+    DeadInsts.emplace_back(NarrowUse);
+    return true;
+  }
+  const Instruction *Context = DT->findNearestCommonDominatorInst(ExtUsers);
+  assert(Context && "No common dominator?");
+
   if (!CanSignExtend && !CanZeroExtend) {
     // Because InstCombine turns 'sub nuw' to 'add' losing the no-wrap flag, we
     // will most likely not see it. Let's try to prove it.
@@ -1552,7 +1578,7 @@
     if (!SE->isKnownNegative(RHS))
       return false;
     bool ProvedSubNUW = SE->isKnownPredicateAt(
-        ICmpInst::ICMP_UGE, LHS, SE->getNegativeSCEV(RHS), NarrowUse);
+        ICmpInst::ICMP_UGE, LHS, SE->getNegativeSCEV(RHS), Context);
     if (!ProvedSubNUW)
       return false;
     // In fact, our 'add' is 'sub nuw'. We will need to widen the 2nd operand as
@@ -1566,22 +1592,6 @@
   if (!AddRecOp1 || AddRecOp1->getLoop() != L)
     return false;
 
-  // Check that all uses are either s/zext, or narrow def (in case of we are
-  // widening the IV increment).
-  SmallVector<Instruction *, 4> ExtUsers;
-  for (Use &U : NarrowUse->uses()) {
-    if (U.getUser() == NarrowDef)
-      continue;
-    Instruction *User = nullptr;
-    if (ExtKind == SignExtended)
-      User = dyn_cast<SExtInst>(U.getUser());
-    else
-      User = dyn_cast<ZExtInst>(U.getUser());
-    if (!User || User->getType() != WideType)
-      return false;
-    ExtUsers.push_back(User);
-  }
-
   LLVM_DEBUG(dbgs() << "Cloning arithmetic IVUser: " << *NarrowUse << "\n");
 
   // Generating a widening use instruction.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90456.306317.patch
Type: text/x-patch
Size: 4203 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201119/4f5f1211/attachment.bin>


More information about the llvm-commits mailing list