[llvm] [PowerPC] Use zext instead of anyext in custom and combine (PR #68784)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 11 03:21:03 PDT 2023


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/68784

This custom combine currently converts `and(anyext(x),c)` into `anyext(and(x,c))`. This is not correct, because the original expression guaranteed that the high bits are zero, while the new one sets them to undef.

Emit `zext(and(x,c))` instead.

Fixes https://github.com/llvm/llvm-project/issues/68783.

>From c3b6edd1d816b7f0ba5aa2dc1ab44088027f69e5 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 11 Oct 2023 12:16:04 +0200
Subject: [PATCH] [PowerPC] Use zext instead of anyext in custom and combine

This custom combine currently converts an `and(anyext(x),c)`
into `anyext(and(x,c))`. This is not correct, because the original
expression guaranteed that the high bits are zero, while the new
one sets them to undef.

Emit `zext(and(x,c))` instead.

Fixes https://github.com/llvm/llvm-project/issues/68783.
---
 llvm/lib/Target/PowerPC/PPCISelLowering.cpp     | 2 +-
 llvm/test/CodeGen/PowerPC/and-extend-combine.ll | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
index c6257dcdf76f633..5e0c2d62f5a9cb5 100644
--- a/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCISelLowering.cpp
@@ -15588,7 +15588,7 @@ SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
       break;
     SDValue ConstOp = DAG.getConstant(Imm, dl, MVT::i32);
     SDValue NarrowAnd = DAG.getNode(ISD::AND, dl, MVT::i32, NarrowOp, ConstOp);
-    return DAG.getAnyExtOrTrunc(NarrowAnd, dl, N->getValueType(0));
+    return DAG.getZExtOrTrunc(NarrowAnd, dl, N->getValueType(0));
   }
   case ISD::SHL:
     return combineSHL(N, DCI);
diff --git a/llvm/test/CodeGen/PowerPC/and-extend-combine.ll b/llvm/test/CodeGen/PowerPC/and-extend-combine.ll
index 11f83ccc6806d0d..0b30d48912dc374 100644
--- a/llvm/test/CodeGen/PowerPC/and-extend-combine.ll
+++ b/llvm/test/CodeGen/PowerPC/and-extend-combine.ll
@@ -23,11 +23,12 @@ bb:
   ret ptr %i8
 }
 
-; FIXME: This is a miscompile.
 define void @pr68783(i32 %x, ptr %p) {
 ; CHECK-LABEL: pr68783:
 ; CHECK:       # %bb.0:
 ; CHECK-NEXT:    rlwinm r3, r3, 31, 24, 31
+; CHECK-NEXT:    li r5, 0
+; CHECK-NEXT:    sth r5, 4(r4)
 ; CHECK-NEXT:    stw r3, 0(r4)
 ; CHECK-NEXT:    blr
   %lshr = lshr i32 %x, 1



More information about the llvm-commits mailing list