<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Thank for bringing this to our notice and for providing the test case, I’ll investigate this. I apologize for the inconvenience.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Matthias</div><br class=""><div><blockquote type="cite" class=""><div class="">Am 20.07.2016 um 14:04 schrieb Tobias Grosser <<a href="mailto:tobias@grosser.es" class="">tobias@grosser.es</a>>:</div><br class="Apple-interchange-newline"><div class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Thank you Benjamin. Sorry for the breakage.</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Best,</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Tobias</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">On Wed, Jul 20, 2016, at 01:47 PM, Benjamin Kramer via llvm-commits</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">wrote:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">This sends 'opt -instcombine' into an infinite loop on the attached<br class="">test case, can you take a look? I reverted this change (and r276105)<br class="">in r276106.<br class=""><br class="">On Tue, Jul 19, 2016 at 6:39 PM, Tobias Grosser via llvm-commits<br class=""><<a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a>> wrote:<br class=""><blockquote type="cite" class="">Author: grosser<br class="">Date: Tue Jul 19 11:39:17 2016<br class="">New Revision: 275989<br class=""><br class="">URL: <a href="http://llvm.org/viewvc/llvm-project?rev=275989&view=rev" class="">http://llvm.org/viewvc/llvm-project?rev=275989&view=rev</a><br class="">Log:<br class="">[InstCombine] Enable cast-folding in logic(cast(icmp), cast(icmp))<br class=""><br class="">Summary:<br class="">Currently, InstCombine is already able to fold expressions of the form `logic(cast(A), cast(B))` to the simpler form `cast(logic(A, B))`, where logic designates one of `and`/`or`/`xor`. This transformation is implemented in `foldCastedBitwiseLogic()` in InstCombineAndOrXor.cpp. However, this optimization will not be performed if both `A` and `B` are `icmp` instructions. The decision to preclude casts of `icmp` instructions originates in r48715 in combination with r261707, and can be best understood by the title of the former one:<br class=""><br class=""><blockquote type="cite" class="">Transform (zext (or (icmp), (icmp))) to (or (zext (cimp), (zext icmp))) if at least one of the (zext icmp) can be transformed to eliminate an icmp.<br class=""></blockquote><br class="">Apparently, it introduced a transformation that is a reverse of the transformation that is done in `foldCastedBitwiseLogic()`. Its purpose is to expose pairs of `zext icmp` that would subsequently be optimized by `transformZExtICmp()` in InstCombineCasts.cpp. Therefore, in order to avoid an endless loop of switching back and forth between these two transformations, the one in `foldCastedBitwiseLogic()` has been restricted to exclude `icmp` instructions which is mirrored in the responsible check:<br class=""><br class="">`if ((!isa<ICmpInst>(Cast0Src) || !isa<ICmpInst>(Cast1Src)) && ...`<br class=""><br class="">This check seems to sort out more cases than necessary because:<br class="">- the reverse transformation is obviously done for `or` instructions only<br class="">- and also not every `zext icmp` pair is necessarily the result of this reverse transformation<br class=""><br class="">Therefore we now remove this check and replace it by a more finegrained one in `shouldOptimizeCast()` that now rejects only those `logic(zext(icmp), zext(icmp))` that would be able to be optimized by `transformZExtICmp()`, which also avoids the mentioned endless loop. That means we are now able to also simplify expressions of the form `logic(cast(icmp), cast(icmp))` to `cast(logic(icmp, icmp))` (`cast` being an arbitrary `CastInst`).<br class=""><br class="">As an example, consider the following IR snippet<br class=""><br class="">```<br class="">%1 = icmp sgt i64 %a, %b<br class="">%2 = zext i1 %1 to i8<br class="">%3 = icmp slt i64 %a, %c<br class="">%4 = zext i1 %3 to i8<br class="">%5 = and i8 %2, %4<br class="">```<br class=""><br class="">which would now be transformed to<br class=""><br class="">```<br class="">%1 = icmp sgt i64 %a, %b<br class="">%2 = icmp slt i64 %a, %c<br class="">%3 = and i1 %1, %2<br class="">%4 = zext i1 %3 to i8<br class="">```<br class=""><br class="">This issue became apparent when experimenting with the programming language Julia, which makes use of LLVM. Currently, Julia lowers its `Bool` datatype to LLVM's `i8` (also see <a href="https://github.com/JuliaLang/julia/pull/17225" class="">https://github.com/JuliaLang/julia/pull/17225</a>). In fact, the above IR example is the lowered form of the Julia snippet `(a > b) & (a < c)`. Like shown above, this may introduce `zext` operations, casting between `i1` and `i8`, which could for example hinder ScalarEvolution and Polly on certain code.<br class=""><br class="">Reviewers: grosser, vtjnash, majnemer<br class=""><br class="">Subscribers: majnemer, llvm-commits<br class=""><br class="">Differential Revision: <a href="https://reviews.llvm.org/D22511" class="">https://reviews.llvm.org/D22511</a><br class=""><br class="">Contributed-by: Matthias Reisinger<br class=""><br class="">Modified:<br class="">   llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp<br class="">   llvm/trunk/test/Transforms/InstCombine/zext.ll<br class=""><br class="">Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=275989&r1=275988&r2=275989&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=275989&r1=275988&r2=275989&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)<br class="">+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Tue Jul 19 11:39:17 2016<br class="">@@ -1212,6 +1212,13 @@ bool InstCombiner::shouldOptimizeCast(Ca<br class="">      isa<CmpInst>(CastSrc) && CI->getDestTy()->isVectorTy())<br class="">    return false;<br class=""><br class="">+  // Don't optimize the cast if it is a (zext icmp) that can already be<br class="">+  // eliminated.<br class="">+  if (auto *ZExt = dyn_cast<ZExtInst>(CI))<br class="">+    if (auto *ICmp = dyn_cast<ICmpInst>(CastSrc))<br class="">+      if (transformZExtICmp(ICmp, *ZExt, false))<br class="">+        return false;<br class="">+<br class="">  return true;<br class="">}<br class=""><br class="">@@ -1260,8 +1267,7 @@ Instruction *InstCombiner::foldCastedBit<br class="">  Value *Cast1Src = Cast1->getOperand(0);<br class=""><br class="">  // fold logic(cast(A), cast(B)) -> cast(logic(A, B))<br class="">-  if ((!isa<ICmpInst>(Cast0Src) || !isa<ICmpInst>(Cast1Src)) &&<br class="">-      shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {<br class="">+  if (shouldOptimizeCast(Cast0) && shouldOptimizeCast(Cast1)) {<br class="">    Value *NewOp = Builder->CreateBinOp(LogicOpc, Cast0Src, Cast1Src,<br class="">                                        I.getName());<br class="">    return CastInst::Create(CastOpcode, NewOp, DestTy);<br class=""><br class="">Modified: llvm/trunk/test/Transforms/InstCombine/zext.ll<br class="">URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/zext.ll?rev=275989&r1=275988&r2=275989&view=diff" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/zext.ll?rev=275989&r1=275988&r2=275989&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/test/Transforms/InstCombine/zext.ll (original)<br class="">+++ llvm/trunk/test/Transforms/InstCombine/zext.ll Tue Jul 19 11:39:17 2016<br class="">@@ -73,3 +73,41 @@ define <2 x i64> @fold_xor_zext_sandwich<br class="">  ret <2 x i64> %zext2<br class="">}<br class=""><br class="">+; Assert that zexts in logic(zext(icmp), zext(icmp)) can be folded<br class="">+; CHECK-LABEL: @fold_logic_zext_icmp(<br class="">+; CHECK-NEXT:    [[ICMP1:%.*]] = icmp sgt i64 %a, %b<br class="">+; CHECK-NEXT:    [[ICMP2:%.*]] = icmp slt i64 %a, %c<br class="">+; CHECK-NEXT:    [[AND:%.*]] = and i1 [[ICMP1]], [[ICMP2]]<br class="">+; CHECK-NEXT:    [[ZEXT:%.*]] = zext i1 [[AND]] to i8<br class="">+; CHECK-NEXT:    ret i8 [[ZEXT]]<br class="">+define i8 @fold_logic_zext_icmp(i64 %a, i64 %b, i64 %c) {<br class="">+  %1 = icmp sgt i64 %a, %b<br class="">+  %2 = zext i1 %1 to i8<br class="">+  %3 = icmp slt i64 %a, %c<br class="">+  %4 = zext i1 %3 to i8<br class="">+  %5 = and i8 %2, %4<br class="">+  ret i8 %5<br class="">+}<br class="">+<br class="">+; Assert that zexts in logic(zext(icmp), zext(icmp)) are also folded accross<br class="">+; nested logical operators.<br class="">+; CHECK-LABEL: @fold_nested_logic_zext_icmp(<br class="">+; CHECK-NEXT:    [[ICMP1:%.*]] = icmp sgt i64 %a, %b<br class="">+; CHECK-NEXT:    [[ICMP2:%.*]] = icmp slt i64 %a, %c<br class="">+; CHECK-NEXT:    [[AND:%.*]] = and i1 [[ICMP1]], [[ICMP2]]<br class="">+; CHECK-NEXT:    [[ICMP3:%.*]] = icmp eq i64 %a, %d<br class="">+; CHECK-NEXT:    [[OR:%.*]] = or i1 [[AND]], [[ICMP3]]<br class="">+; CHECK-NEXT:    [[ZEXT:%.*]] = zext i1 [[OR]] to i8<br class="">+; CHECK-NEXT:    ret i8 [[ZEXT]]<br class="">+define i8 @fold_nested_logic_zext_icmp(i64 %a, i64 %b, i64 %c, i64 %d) {<br class="">+  %1 = icmp sgt i64 %a, %b<br class="">+  %2 = zext i1 %1 to i8<br class="">+  %3 = icmp slt i64 %a, %c<br class="">+  %4 = zext i1 %3 to i8<br class="">+  %5 = and i8 %2, %4<br class="">+  %6 = icmp eq i64 %a, %d<br class="">+  %7 = zext i1 %6 to i8<br class="">+  %8 = or i8 %5, %7<br class="">+  ret i8 %8<br class="">+}<br class="">+<br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a><br class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits<br class=""></blockquote>_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@lists.llvm.org" class="">llvm-commits@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br class="">Email had 1 attachment:<br class="">+ bugpoint-reduced-simplified.ll<br class=""> 3k (application/octet-stream)</blockquote></div></blockquote></div><br class=""></body></html>