<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/147946>147946</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DAG] combineShiftToAVG - failure to match avgfloors pattern on preferIncOfAddToSubOfNot targets
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:AArch64,
crash-on-valid,
llvm:SelectionDAG
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RKSimon
</td>
</tr>
</table>
<pre>
```ll
define <vscale x 2 x i64> @rhadds_v2i64(<vscale x 2 x i64> %s0, <vscale x 2 x i64> %s1) {
; SVE-LABEL: rhadds_v2i64:
; SVE: // %bb.0: // %entry
; SVE-NEXT: eor z2.d, z0.d, z1.d
; SVE-NEXT: orr z0.d, z0.d, z1.d
; SVE-NEXT: asr z1.d, z2.d, #1
; SVE-NEXT: sub z0.d, z0.d, z1.d
; SVE-NEXT: ret
;
; SVE2-LABEL: rhadds_v2i64:
; SVE2: // %bb.0: // %entry
; SVE2-NEXT: ptrue p0.d
; SVE2-NEXT: srhadd z0.d, p0/m, z0.d, z1.d
; SVE2-NEXT: ret
%s0s = sext <vscale x 2 x i64> %s0 to <vscale x 2 x i128>
%s1s = sext <vscale x 2 x i64> %s1 to <vscale x 2 x i128>
%add = add <vscale x 2 x i128> %s0s, splat (i128 1)
%add2 = add <vscale x 2 x i128> %add, %s1s
%s = ashr <vscale x 2 x i128> %add2, splat (i128 1)
%result = trunc <vscale x 2 x i128> %s to <vscale x 2 x i64>
ret <vscale x 2 x i64> %result
}
```
folded to:
```ll
define <vscale x 2 x i64> @rhadds_v2i64(<vscale x 2 x i64> %s0, <vscale x 2 x i64> %s1) {
%s0s = sext <vscale x 2 x i64> %s0 to <vscale x 2 x i128>
%s1s = sext <vscale x 2 x i64> %s1 to <vscale x 2 x i128>
%not = xor <vscale x 2 x i128> %s0s, splat (i128 -1)
%sub = sub <vscale x 2 x i128> %not, %s1s
%s = ashr <vscale x 2 x i128> %sub, splat (i128 1)
%result = trunc <vscale x 2 x i128> %s to <vscale x 2 x i64>
ret <vscale x 2 x i64> %result
}
```
Targets such as aarch64 will fold `add (add x, y), 1` to `sub y, (xor x, -1)`, preventing the combineShiftToAVG AVGFLOOR/CEIL matching code from identifying the new pattern. This is a particular problem for the SVE/SVE2 tests as the above code will no longer compile.
Noticed while investigating regression for DAG topological sorting - which will fold the add to `sub y, (xor x, -1)` earlier than usual.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVl1v4ygU_TXk5SoRxnbiPPjBbZJqtFUrTatq31bYXNvsErAAp-38-hU4_RhN-jHz1CgKEvdwuOdwTMydk51GLEl-RvLNjI--N7b8_teN3Bs9q414LMmSTl-lCK0EtlIjkPT84BquEB6AwQPIZUbSLZCM2p4L4f45sDDFireALHeUsPM3iVjuEsLWQFZnhFYkPYObu-38sjrbXpK0gp92SasXSChOH8J2hO0CU10vaJh_mUHt7eMr3qvt37fHlWgs_GALEZr7QY9jshCn0cbaF9SHaO7sVA2o4x6EpclptBvr3-C26KfCS5l9wi_2R4ax1zsP3o4IA33d2U8AF3d_1jBQwnb7t1WxX2XBFBgHJN2Awwf_Xm4oePNrPQlZ3D5zJZ_jSj7gIiwP0gLVNJ5EHrsPUt2guAfCilCCEPGnlrgQ7DNEXBxzE0Q865lWut5-sJS93QRhuUU3Kh-5vB11866gk9ZE62JXFt9zdtopHPpqE36fLhlCq9YogQK8OQb1C9w_XzZ_AaTNdGIP5t3DPxnA-esEhgsnNhXHN4m08X8aQDfW7z8EXyKAhFa33HboHbix6YE74Nw2_TKDe6kUhIACWdL4oLIiDA9B1mMQws4hIcspA0sarHyc3CrC-UTcZPoy5m-weEDtpe7A9wiN2ddS400vW39rqrsLqO4udpfX198J251vv13CnvumD_DGCITWmj1IERjaxycSjfcwcO_R6gXc9tKBdMBh4NbLZlTcwmBNrXAPrbFxRfjjZLtw94JH512QHOZ5bQ447RSVaw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