[llvm] [DAG] SelectionDAG::canCreateUndefOrPoison - Mark AVGFLOORS and AVGCEILS as safe (PR #148191)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 14 01:17:30 PDT 2025


================
@@ -0,0 +1,16 @@
+; RUN: llc < %s -march=arm64 -mcpu=apple-m1 | FileCheck %s
+
+; CHECK-LABEL: avg:
+; CHECK:       add
+; CHECK:       lsr
+; CHECK:       ret
+
+define zeroext i8 @avg(i8 noundef zeroext %a, i8 noundef zeroext %b) {
+entry:
+  %conv = zext i8 %a to i16
+  %conv1 = zext i8 %b to i16
+  %add = add nuw nsw i16 %conv1, %conv
+  %div3 = lshr i16 %add, 1
+  %conv2 = trunc nuw i16 %div3 to i8
+  ret i8 %conv2
+}
----------------
RKSimon wrote:

The problem you have if you attempt to freeze the expanded avg pattern - is that the freeze might get folded through instructions BEFORE the avg node is created. You're going to have to use the aarch64 hadd/rhadd intrinsics and rely on a fold that uses knownbits/signbits that is blocked if the freeze it still present `and(avg(and(x,const0),and(y,const1)),const2)` might work for avgceilu/avgflooru for instance.

llvm\test\CodeGen\X86\freeze-binary.ll has some examples of frozen calls of other intrinsics to give you some ideas that you could try for @llvm.aarch64.neon.shadd.v8i16 etc.


https://github.com/llvm/llvm-project/pull/148191


More information about the llvm-commits mailing list