<div dir="auto">Thanks! That’s how I had drafted it initially, but we canonicalize that to the select form already. So I just made this go directly to the end result.</div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jan 9, 2023 at 1:35 PM Philip Reames <<a href="mailto:listmail@philipreames.com">listmail@philipreames.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I think you could replace the bitreverse with a left-shift by bitwidth-1 <br>
if you wanted.<br>
<br>
Not sure if that's better or worse than the select representation.<br>
<br>
Philip<br>
<br>
On 1/9/23 09:31, Sanjay Patel via llvm-commits wrote:<br>
> Author: Sanjay Patel<br>
> Date: 2023-01-09T12:27:37-05:00<br>
> New Revision: 0eedc9e56712938ce4c01e14ba755dbdb2a0df18<br>
><br>
> URL: <a href="https://github.com/llvm/llvm-project/commit/0eedc9e56712938ce4c01e14ba755dbdb2a0df18" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/0eedc9e56712938ce4c01e14ba755dbdb2a0df18</a><br>
> DIFF: <a href="https://github.com/llvm/llvm-project/commit/0eedc9e56712938ce4c01e14ba755dbdb2a0df18.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/0eedc9e56712938ce4c01e14ba755dbdb2a0df18.diff</a><br>
><br>
> LOG: [InstCombine] bitrev (zext i1 X) --> select X, SMinC, 0<br>
><br>
> <a href="https://alive2.llvm.org/ce/z/ZXCtgi" rel="noreferrer" target="_blank">https://alive2.llvm.org/ce/z/ZXCtgi</a><br>
><br>
> This breaks the infinite combine loop for issue #59897,<br>
> but we may still need more changes to avoid those loops.<br>
><br>
> Added:<br>
> <br>
><br>
> Modified:<br>
> llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp<br>
> llvm/test/Transforms/InstCombine/bitreverse.ll<br>
><br>
> Removed:<br>
> <br>
><br>
><br>
> ################################################################################<br>
> diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp<br>
> index 59486312448e..65248eae6acb 100644<br>
> --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp<br>
> +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp<br>
> @@ -1441,6 +1441,18 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {<br>
> <br>
> break;<br>
> }<br>
> + case Intrinsic::bitreverse: {<br>
> + // bitrev (zext i1 X to ?) --> X ? SignBitC : 0<br>
> + Value *X;<br>
> + if (match(II->getArgOperand(0), m_ZExt(m_Value(X))) &&<br>
> + X->getType()->isIntOrIntVectorTy(1)) {<br>
> + Type *Ty = II->getType();<br>
> + APInt SignBit = APInt::getSignMask(Ty->getScalarSizeInBits());<br>
> + return SelectInst::Create(X, ConstantInt::get(Ty, SignBit),<br>
> + ConstantInt::getNullValue(Ty));<br>
> + }<br>
> + break;<br>
> + }<br>
> case Intrinsic::bswap: {<br>
> Value *IIOperand = II->getArgOperand(0);<br>
> <br>
><br>
> diff --git a/llvm/test/Transforms/InstCombine/bitreverse.ll b/llvm/test/Transforms/InstCombine/bitreverse.ll<br>
> index 232d69da9a02..ab41debc65c7 100644<br>
> --- a/llvm/test/Transforms/InstCombine/bitreverse.ll<br>
> +++ b/llvm/test/Transforms/InstCombine/bitreverse.ll<br>
> @@ -323,7 +323,7 @@ define i32 @rev_i1(i1 %x) {<br>
> ; CHECK-LABEL: @rev_i1(<br>
> ; CHECK-NEXT: [[Z:%.*]] = zext i1 [[X:%.*]] to i32<br>
> ; CHECK-NEXT: call void @use_i32(i32 [[Z]])<br>
> -; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.bitreverse.i32(i32 [[Z]])<br>
> +; CHECK-NEXT: [[R:%.*]] = select i1 [[X]], i32 -2147483648, i32 0<br>
> ; CHECK-NEXT: ret i32 [[R]]<br>
> ;<br>
> %z = zext i1 %x to i32<br>
> @@ -334,8 +334,7 @@ define i32 @rev_i1(i1 %x) {<br>
> <br>
> define <2 x i8> @rev_v2i1(<2 x i1> %x) {<br>
> ; CHECK-LABEL: @rev_v2i1(<br>
> -; CHECK-NEXT: [[Z:%.*]] = zext <2 x i1> [[X:%.*]] to <2 x i8><br>
> -; CHECK-NEXT: [[R:%.*]] = call <2 x i8> @llvm.bitreverse.v2i8(<2 x i8> [[Z]])<br>
> +; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[X:%.*]], <2 x i8> <i8 -128, i8 -128>, <2 x i8> zeroinitializer<br>
> ; CHECK-NEXT: ret <2 x i8> [[R]]<br>
> ;<br>
> %z = zext <2 x i1> %x to <2 x i8><br>
> @@ -353,3 +352,19 @@ define i32 @rev_i2(i2 %x) {<br>
> %r = call i32 @llvm.bitreverse.i32(i32 %z)<br>
> ret i32 %r<br>
> }<br>
> +<br>
> +; This used to infinite loop.<br>
> +<br>
> +define i64 @PR59897(i1 %X1_2) {<br>
> +; CHECK-LABEL: @PR59897(<br>
> +; CHECK-NEXT: [[NOT_X1_2:%.*]] = xor i1 [[X1_2:%.*]], true<br>
> +; CHECK-NEXT: [[X0_3X2X5X0:%.*]] = zext i1 [[NOT_X1_2]] to i64<br>
> +; CHECK-NEXT: ret i64 [[X0_3X2X5X0]]<br>
> +;<br>
> + %X1_3 = zext i1 %X1_2 to i32<br>
> + %X8_3x2x2x0 = call i32 @llvm.bitreverse.i32(i32 %X1_3)<br>
> + %X8_4x2x3x0 = xor i32 %X8_3x2x2x0, -1<br>
> + %X0_3x2x4x0 = lshr i32 %X8_4x2x3x0, 31<br>
> + %X0_3x2x5x0 = zext i32 %X0_3x2x4x0 to i64<br>
> + ret i64 %X0_3x2x5x0<br>
> +}<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></div>