<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Feb 1, 2018 at 1:57 PM Sanjay Patel 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: spatel<br>
Date: Thu Feb  1 13:55:53 2018<br>
New Revision: 324014<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=324014&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=324014&view=rev</a><br>
Log:<br>
[InstCombine] allow multi-use values in canEvaluate* if all uses are in 1 inst<br>
<br>
This is the enhancement suggested in D42536 to fix a shortcoming in<br>
regular InstCombine's canEvaluate* functionality.<br>
When we have multiple uses of a value, but they're all in one instruction, we can<br>
allow that expression to be narrowed or widened for the same cost as a single-use<br>
value.<br>
<br>
AFAICT, this can only matter for multiply: sub/and/or/xor/select would be simplified<br>
away if the operands are the same value; add becomes shl; shifts with a variable shift<br>
amount aren't handled.<br></blockquote><div><br></div><div>FWIW, we have at least one case which is now either inf-looping or taking dramatically longer to compile after this commit (way over 2x from what I can tell).</div><div><br></div><div>We're working on building a test case, but just wanted to send a heads-up in case others are hitting slow compiles.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Differential Revision: <a href="https://reviews.llvm.org/D42739" rel="noreferrer" target="_blank">https://reviews.llvm.org/D42739</a><br>
<br>
Modified:<br>
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp<br>
    llvm/trunk/test/Transforms/InstCombine/cast-mul-select.ll<br>
    llvm/trunk/test/Transforms/InstCombine/icmp-mul-zext.ll<br>
<br>
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=324014&r1=324013&r2=324014&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=324014&r1=324013&r2=324014&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Thu Feb  1 13:55:53 2018<br>
@@ -185,8 +185,14 @@ Value *InstCombiner::EvaluateInDifferent<br>
   case Instruction::Shl:<br>
   case Instruction::UDiv:<br>
   case Instruction::URem: {<br>
-    Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);<br>
-    Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);<br>
+    Value *LHS, *RHS;<br>
+    if (I->getOperand(0) == I->getOperand(1)) {<br>
+      // Don't create an unnecessary value if the operands are repeated.<br>
+      LHS = RHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);<br>
+    } else {<br>
+      LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);<br>
+      RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);<br>
+    }<br>
     Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);<br>
     break;<br>
   }<br>
@@ -320,10 +326,12 @@ static bool canNotEvaluateInType(Value *<br>
   assert(!isa<Constant>(V) && "Constant should already be handled.");<br>
   if (!isa<Instruction>(V))<br>
     return true;<br>
-  // We can't extend or shrink something that has multiple uses: doing so would<br>
-  // require duplicating the instruction in general, which isn't profitable.<br>
+  // We can't extend or shrink something that has multiple uses -- unless those<br>
+  // multiple uses are all in the same instruction -- doing so would require<br>
+  // duplicating the instruction which isn't profitable.<br>
   if (!V->hasOneUse())<br>
-    return true;<br>
+    if (any_of(V->users(), [&](User *U) { return U != V->user_back(); }))<br>
+      return true;<br>
<br>
   return false;<br>
 }<br>
<br>
Modified: llvm/trunk/test/Transforms/InstCombine/cast-mul-select.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast-mul-select.ll?rev=324014&r1=324013&r2=324014&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast-mul-select.ll?rev=324014&r1=324013&r2=324014&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/InstCombine/cast-mul-select.ll (original)<br>
+++ llvm/trunk/test/Transforms/InstCombine/cast-mul-select.ll Thu Feb  1 13:55:53 2018<br>
@@ -49,11 +49,9 @@ define i8 @select2(i1 %cond, i8 %x, i8 %<br>
<br>
 define i32 @eval_trunc_multi_use_in_one_inst(i32 %x) {<br>
 ; CHECK-LABEL: @eval_trunc_multi_use_in_one_inst(<br>
-; CHECK-NEXT:    [[Z:%.*]] = zext i32 [[X:%.*]] to i64<br>
-; CHECK-NEXT:    [[A:%.*]] = add nuw nsw i64 [[Z]], 15<br>
-; CHECK-NEXT:    [[M:%.*]] = mul i64 [[A]], [[A]]<br>
-; CHECK-NEXT:    [[T:%.*]] = trunc i64 [[M]] to i32<br>
-; CHECK-NEXT:    ret i32 [[T]]<br>
+; CHECK-NEXT:    [[A:%.*]] = add i32 [[X:%.*]], 15<br>
+; CHECK-NEXT:    [[M:%.*]] = mul i32 [[A]], [[A]]<br>
+; CHECK-NEXT:    ret i32 [[M]]<br>
 ;<br>
   %z = zext i32 %x to i64<br>
   %a = add nsw nuw i64 %z, 15<br>
@@ -64,11 +62,9 @@ define i32 @eval_trunc_multi_use_in_one_<br>
<br>
 define i32 @eval_zext_multi_use_in_one_inst(i32 %x) {<br>
 ; CHECK-LABEL: @eval_zext_multi_use_in_one_inst(<br>
-; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[X:%.*]] to i16<br>
-; CHECK-NEXT:    [[A:%.*]] = and i16 [[T]], 5<br>
-; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]<br>
-; CHECK-NEXT:    [[R:%.*]] = zext i16 [[M]] to i32<br>
-; CHECK-NEXT:    ret i32 [[R]]<br>
+; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], 5<br>
+; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i32 [[A]], [[A]]<br>
+; CHECK-NEXT:    ret i32 [[M]]<br>
 ;<br>
   %t = trunc i32 %x to i16<br>
   %a = and i16 %t, 5<br>
@@ -79,12 +75,10 @@ define i32 @eval_zext_multi_use_in_one_i<br>
<br>
 define i32 @eval_sext_multi_use_in_one_inst(i32 %x) {<br>
 ; CHECK-LABEL: @eval_sext_multi_use_in_one_inst(<br>
-; CHECK-NEXT:    [[T:%.*]] = trunc i32 [[X:%.*]] to i16<br>
-; CHECK-NEXT:    [[A:%.*]] = and i16 [[T]], 14<br>
-; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i16 [[A]], [[A]]<br>
-; CHECK-NEXT:    [[O:%.*]] = or i16 [[M]], -32768<br>
-; CHECK-NEXT:    [[R:%.*]] = sext i16 [[O]] to i32<br>
-; CHECK-NEXT:    ret i32 [[R]]<br>
+; CHECK-NEXT:    [[A:%.*]] = and i32 [[X:%.*]], 14<br>
+; CHECK-NEXT:    [[M:%.*]] = mul nuw nsw i32 [[A]], [[A]]<br>
+; CHECK-NEXT:    [[O:%.*]] = or i32 [[M]], -32768<br>
+; CHECK-NEXT:    ret i32 [[O]]<br>
 ;<br>
   %t = trunc i32 %x to i16<br>
   %a = and i16 %t, 14<br>
<br>
Modified: llvm/trunk/test/Transforms/InstCombine/icmp-mul-zext.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp-mul-zext.ll?rev=324014&r1=324013&r2=324014&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/icmp-mul-zext.ll?rev=324014&r1=324013&r2=324014&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/InstCombine/icmp-mul-zext.ll (original)<br>
+++ llvm/trunk/test/Transforms/InstCombine/icmp-mul-zext.ll Thu Feb  1 13:55:53 2018<br>
@@ -55,13 +55,12 @@ lor.end:<br>
<br>
 define void @PR33765(i8 %beth) {<br>
 ; CHECK-LABEL: @PR33765(<br>
-; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[BETH:%.*]] to i32<br>
+; CHECK-NEXT:    [[CONV:%.*]] = zext i8 [[BETH:%.*]] to i16<br>
 ; CHECK-NEXT:    br i1 false, label [[IF_THEN9:%.*]], label [[IF_THEN9]]<br>
 ; CHECK:       if.then9:<br>
-; CHECK-NEXT:    [[MUL:%.*]] = mul nuw nsw i32 [[CONV]], [[CONV]]<br>
+; CHECK-NEXT:    [[MUL:%.*]] = mul nuw i16 [[CONV]], [[CONV]]<br>
 ; CHECK-NEXT:    [[TINKY:%.*]] = load i16, i16* @glob, align 2<br>
-; CHECK-NEXT:    [[TMP1:%.*]] = trunc i32 [[MUL]] to i16<br>
-; CHECK-NEXT:    [[CONV14:%.*]] = and i16 [[TINKY]], [[TMP1]]<br>
+; CHECK-NEXT:    [[CONV14:%.*]] = and i16 [[TINKY]], [[MUL]]<br>
 ; CHECK-NEXT:    store i16 [[CONV14]], i16* @glob, align 2<br>
 ; CHECK-NEXT:    ret void<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="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div></div>