<div dir="ltr">thanks a lot for the fix! </div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 23, 2020 at 2:01 PM Roman Lebedev via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Roman Lebedev<br>
Date: 2020-04-23T23:59:15+03:00<br>
New Revision: 5a159ed2a8e5a9a6ced73f78e4c64b01d76d3493<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/5a159ed2a8e5a9a6ced73f78e4c64b01d76d3493" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/5a159ed2a8e5a9a6ced73f78e4c64b01d76d3493</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/5a159ed2a8e5a9a6ced73f78e4c64b01d76d3493.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/5a159ed2a8e5a9a6ced73f78e4c64b01d76d3493.diff</a><br>
<br>
LOG: [InstCombine] Negator: don't negate multi-use `sub`<br>
<br>
While we can do that, it doesn't increase instruction count,<br>
if the old `sub` sticks around then the transform is not only<br>
not a unlikely win, but a likely regression, since we likely<br>
now extended live range and use count of both of the `sub` operands,<br>
as opposed to just the result of `sub`.<br>
<br>
As Kostya Serebryany notes in post-commit review in<br>
<a href="https://reviews.llvm.org/D68408#1998112" rel="noreferrer" target="_blank">https://reviews.llvm.org/D68408#1998112</a><br>
this indeed can degrade final assembly,<br>
increase register pressure, and spilling.<br>
<br>
This isn't what we want here,<br>
so at least for now let's guard it with an use check.<br>
<br>
Added: <br>
<br>
<br>
Modified: <br>
    llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp<br>
    llvm/test/Transforms/InstCombine/sub-of-negatible.ll<br>
    llvm/test/Transforms/InstCombine/sub.ll<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp<br>
index e119a383e9db..42bb748cc287 100644<br>
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp<br>
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp<br>
@@ -152,10 +152,6 @@ LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) {<br>
<br>
   // In some cases we can give the answer without further recursion.<br>
   switch (I->getOpcode()) {<br>
-  case Instruction::Sub:<br>
-    // `sub` is always negatible.<br>
-    return Builder.CreateSub(I->getOperand(1), I->getOperand(0),<br>
-                             I->getName() + ".neg");<br>
   case Instruction::Add:<br>
     // `inc` is always negatible.<br>
     if (match(I->getOperand(1), m_One()))<br>
@@ -183,12 +179,37 @@ LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) {<br>
     }<br>
     break;<br>
   }<br>
+  case Instruction::SExt:<br>
+  case Instruction::ZExt:<br>
+    // `*ext` of i1 is always negatible<br>
+    if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1))<br>
+      return I->getOpcode() == Instruction::SExt<br>
+                 ? Builder.CreateZExt(I->getOperand(0), I->getType(),<br>
+                                      I->getName() + ".neg")<br>
+                 : Builder.CreateSExt(I->getOperand(0), I->getType(),<br>
+                                      I->getName() + ".neg");<br>
+    break;<br>
+  default:<br>
+    break; // Other instructions require recursive reasoning.<br>
+  }<br>
+<br>
+  // Some other cases, while still don't require recursion,<br>
+  // are restricted to the one-use case.<br>
+  if (!V->hasOneUse())<br>
+    return nullptr;<br>
+<br>
+  switch (I->getOpcode()) {<br>
+  case Instruction::Sub:<br>
+    // `sub` is always negatible.<br>
+    // But if the old `sub` sticks around, even thought we don't increase<br>
+    // instruction count, this is a likely regression since we increased<br>
+    // live-range of *both* of the operands, which might lead to more spilling.<br>
+    return Builder.CreateSub(I->getOperand(1), I->getOperand(0),<br>
+                             I->getName() + ".neg");<br>
   case Instruction::SDiv:<br>
     // `sdiv` is negatible if divisor is not undef/INT_MIN/1.<br>
     // While this is normally not behind a use-check,<br>
     // let's consider division to be special since it's costly.<br>
-    if (!I->hasOneUse())<br>
-      break;<br>
     if (auto *Op1C = dyn_cast<Constant>(I->getOperand(1))) {<br>
       if (!Op1C->containsUndefElement() && Op1C->isNotMinSignedValue() &&<br>
           Op1C->isNotOneValue()) {<br>
@@ -201,24 +222,9 @@ LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) {<br>
       }<br>
     }<br>
     break;<br>
-  case Instruction::SExt:<br>
-  case Instruction::ZExt:<br>
-    // `*ext` of i1 is always negatible<br>
-    if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1))<br>
-      return I->getOpcode() == Instruction::SExt<br>
-                 ? Builder.CreateZExt(I->getOperand(0), I->getType(),<br>
-                                      I->getName() + ".neg")<br>
-                 : Builder.CreateSExt(I->getOperand(0), I->getType(),<br>
-                                      I->getName() + ".neg");<br>
-    break;<br>
-  default:<br>
-    break; // Other instructions require recursive reasoning.<br>
   }<br>
<br>
-  // Rest of the logic is recursive, and if either the current instruction<br>
-  // has other uses or if it's time to give up then it's time.<br>
-  if (!V->hasOneUse())<br>
-    return nullptr;<br>
+  // Rest of the logic is recursive, so if it's time to give up then it's time.<br>
   if (Depth > NegatorMaxDepth) {<br>
     LLVM_DEBUG(dbgs() << "Negator: reached maximal allowed traversal depth in "<br>
                       << *V << ". Giving up.\n");<br>
<br>
diff  --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll<br>
index 83f86ca7a5e1..54896397f5cf 100644<br>
--- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll<br>
+++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll<br>
@@ -169,10 +169,10 @@ define i8 @t9(i8 %x, i8 %y) {<br>
 }<br>
 define i8 @n10(i8 %x, i8 %y, i8 %z) {<br>
 ; CHECK-LABEL: @n10(<br>
-; CHECK-NEXT:    [[T0_NEG:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]<br>
-; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[Y]], [[X]]<br>
+; CHECK-NEXT:    [[T0:%.*]] = sub i8 [[Y:%.*]], [[X:%.*]]<br>
 ; CHECK-NEXT:    call void @use8(i8 [[T0]])<br>
-; CHECK-NEXT:    ret i8 [[T0_NEG]]<br>
+; CHECK-NEXT:    [[T1:%.*]] = sub i8 0, [[T0]]<br>
+; CHECK-NEXT:    ret i8 [[T1]]<br>
 ;<br>
   %t0 = sub i8 %y, %x<br>
   call void @use8(i8 %t0)<br>
<br>
diff  --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll<br>
index 1b4149291951..f6fa797eb0c8 100644<br>
--- a/llvm/test/Transforms/InstCombine/sub.ll<br>
+++ b/llvm/test/Transforms/InstCombine/sub.ll<br>
@@ -555,11 +555,11 @@ define i64 @test_neg_shl_sub(i64 %a, i64 %b) {<br>
<br>
 define i64 @test_neg_shl_sub_extra_use1(i64 %a, i64 %b, i64* %p) {<br>
 ; CHECK-LABEL: @test_neg_shl_sub_extra_use1(<br>
-; CHECK-NEXT:    [[SUB_NEG:%.*]] = sub i64 [[B:%.*]], [[A:%.*]]<br>
-; CHECK-NEXT:    [[SUB:%.*]] = sub i64 [[A]], [[B]]<br>
+; CHECK-NEXT:    [[SUB:%.*]] = sub i64 [[A:%.*]], [[B:%.*]]<br>
 ; CHECK-NEXT:    store i64 [[SUB]], i64* [[P:%.*]], align 8<br>
-; CHECK-NEXT:    [[MUL_NEG:%.*]] = shl i64 [[SUB_NEG]], 2<br>
-; CHECK-NEXT:    ret i64 [[MUL_NEG]]<br>
+; CHECK-NEXT:    [[MUL:%.*]] = shl i64 [[SUB]], 2<br>
+; CHECK-NEXT:    [[NEG:%.*]] = sub i64 0, [[MUL]]<br>
+; CHECK-NEXT:    ret i64 [[NEG]]<br>
 ;<br>
   %sub = sub i64 %a, %b<br>
   store i64 %sub, i64* %p<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>