<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJztV01z2zYQ_TXUBSMNRYr6OOggW07GnTbJNJ5-nDIgCVKoQUADgFbsX9-3IGNRjpLGcXKrhxYlYHfxsAu83c1Neb--9swJ0Tjmd9IxPGUrmDds5_3eRekmSl7hseJOioObKHXXTIytMbSdThezbMGiJImyi-3mdZRt2dVHL3TJ3gtui90rYze6_NXw0rGD9DvG9f0H0UnsuC6V1DVpJ5eMO8ZZLp0oPFOCl4QgWyzjeLlgh50sduzACaJgxspaaq5YYZpGelLOW8-kJ-xcOcMqLskyM5p52-rbbm3S5WUpSmZFp8oq-ZFJXagWo5Mo3kbxpvv8XZRtAUkvnGcFd4L8MJhXqmDjtykbN8W-jdLtw3TGxoaN2QR-OVij6xie6jWyCzJDViae20n9AD9FyfLUvzUgtvkEwPCDnNy_xntr_oFT8LOSSji8F6vlNIlX-PbUbrIagrwQlbGCITCXpsmlhusq5rMVArb0B0PBQCyYXI6j9EqmCVMhUDSbzcitPstgknEYefs7kIoyRArBgy81E1UFYPJOqHuKxR5yJcVRzmeOBPuIs82bLWu4u2WAgzUOLL_3grCe-jSax_0TfrLBn8_mEGaEEc7GCWADhE-kfbYIsvNZkB2cOLJyIgxfnIqWQRt2L412nmuPacxG6WWChdKroboH0AEmJ3xRBIvn1KekjAlIKRotdkdTfhpQ4IQHOx4xgwaiEL5SOFyaUBykxp2IZnEefHcFxWXwAYDg9VqZnKtNWVrh3OPCklQ3nRZUgmSrS1H1Ej0KP0t6T0CgR0KnoQNSCisq_OtC8FyJMDPAVVnTPOIKm8SpCoO0egC6ejlCBLxz97MQLocAeTi-StaaJT3W_g50aJcBbPYVrMseKv861OzFUIsfBrX4MtQnN67_TLekMWCNQBlhdCh3Q9nCW64drnXDvQTbdnkkt-ZWaHKBxmUn2iW26ciFuORBWNNdSSLpfj89B30iGHBGIBzikzzw2An19HQ0YTc74iZi_jB-vO0iZBC62l32gETrMEgcRJBopiMt6YCcSI_u0SqwlkE-ganAWsA4-U-a-gIZnOeS_8ngpQg7l8-GLg9HY549dTiteZa8558rfxats8pnctG8y0Xz-ZEBviEHfXf2oW3-BCp8-PFEOF_8DCL8LqDPpcFAb1SIOuKRUNKBL0rB-iq03e-FZTgEufQuFFYYfKyC7rhCEU1kF1YDi4Krrqn6QjVrWkXl750As4GziA7HRz4MVfhA5mDsLbTxN4R3rbtauPX71n9bIQU74R0lmQ2OOPkyPRUu6p08EY6_4its7DpU40J7gjQotT9V-eCrz3sCBJtCreStOG89uegwUIEcynB2_Z7yAp7Nm78_XP11cxXyBIsWF-dUGNzvcHQAZBMq1QsKFh3D38DslHJq4TfvrrX_gwLW4UEN3mp_Y7sm4q0WPc4o7dc4huIp3MtBUCjjFMZaamioKPaUf6jJuaGego6FDSjgGYcupQgJFEfNaJTToe8gW4_l8reE-DkR9o1SJ7LJp1L6qe3uNSrXablKV3zEWyRIu_7F6HcOqXPUWrV-di8jnWtDM5OlWbIc7dYrIURcLHmeJ9WiKvlClDyZTedJvqrELJmNFM-Fcmt0U-gYg1FkTlzIGqUGNaDb0cthyHUSJ3ji6XQVp7PFZJHMkypLsfSqEBlfgkhEg4Px2AaP7DqYzNvaYVJJ54898oi7cPpEj_qhAd6mb3jxag9C1rggx5ESN76prej21I05UMqtsGNum36jXnoVbB6LNEu9N_tzQFOVxwEzey8b-UAXMNBr35KVobIZhd2vw9b_BZe6nxg>53528</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [DAGCombiner]  Wrong code after optimizing load with and mask
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:codegen
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            zmodem,
            uweigand,
            davemgreen,
            sparker-arm
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          JonPsson
      </td>
    </tr>
</table>

<pre>
    It seems this is due to https://reviews.llvm.org/D117457 "[DAG] Extend SearchForAndLoads with any_extend handling", as a bisect lead to 5780087 which was the original commit, but it is also failing on trunk with the added recommit fix included.

Reduced test case:

llc -O3 -mcpu=z14 -o - ./wrong0.ll
[testcase.tar.gz](https://github.com/llvm/llvm-project/files/7981209/testcase.tar.gz)

Before DAGCombine of t59 (two anyext i8->i32 loads (t54, t55) are OR:ed, and then effectively compared as i64s, with an AND mask for low byte):

```
            t56: i32 = or t54, t55
          t57: i64 = any_extend t56
        t59: i64 = and t57, Constant:i64<255>
      t60: i32 = setcc t59, Constant:i64<1>, setlt:ch
    t19: ch = store<(store (s32) into @b)> t18, t60, GlobalAddress:i64<i32* @b> 0, undef:i64
  t42: i64,ch = load<(dereferenceable load (s32) from @b), sext from i32> t19, GlobalAddress:i64<i32* @b> 0, undef:i64
  t54: i32,ch = load<(dereferenceable load (s8) from @a, align 2), anyext from i8> t5, GlobalAddress:i64<i8* @a> 0, undef:i64
  t55: i32,ch = load<(dereferenceable load (s8) from @c, align 2), anyext from i8> t5, GlobalAddress:i64<i8* @c> 0, undef:i64
```

=> DAGCombine t59 =>

This transformation seems broken: now the two loads are zeroextending from i8->i32, and AND:ed as before, and then OR:ed. The OR is then any_extended to i64 which is used for the i64 comparison (t60) without any masking.

```
      t60: i32 = setcc t57, Constant:i64<1>, setlt:ch
    t19: ch = store<(store (s32) into @b)> t18, t60, GlobalAddress:i64<i32* @b> 0, undef:i64
  t42: i64,ch = load<(dereferenceable load (s32) from @b), sext from i32> t19, GlobalAddress:i64<i32* @b> 0, undef:i64
      t64: i32 = and t65, Constant:i32<255>
      t66: i32 = and t67, Constant:i32<255>
    t56: i32 = or t64, t66
  t57: i64 = any_extend t56
  t59: i64 = and t57, Constant:i64<255>
  t65: i32,ch = load<(dereferenceable load (s8) from @a, align 2), zext from i8> t5, GlobalAddress:i64<i8* @a> 0, undef:i64
  t67: i32,ch = load<(dereferenceable load (s8) from @c, align 2), zext from i8> t5, GlobalAddress:i64<i8* @c> 0, undef:i64
```

This leads to wrong code as the upper 32 bits of the compared value are undefined. If t57 would have been zero-extending this would have worked....

In the output:

```
        ork     %r0, %r0, %r1
        cghi    %r0, 0
```

If I comment out the added handling in SearchForAndLoads() like
```
+    // case ISD::ANY_EXTEND: {
+    //   unsigned ActiveBits = Mask->getAPIntValue().countTrailingOnes();
...
```
, the output is correct as it uses a Test under Mask instruction to only test the low byte:

```
       ork     %r0, %r0, %r1
       tmll    %r0, 255
```
 

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV02T2jgQ_TXmooICG4M5cGCGSWq2dpNUMrUfp5Rsy0Y7skRJ8pCZX5_XsjOYCclm8nFbymAjdbeeu6XX3bkp79fXnjkhGsf8TjqGq2wF84btvN-7KNlE8QtcVtxJcXATpe6aibE1hraz2XKeLlkUx1F6sd28jNItu_rghS7ZO8FtsXth7EaXvxteOnaQfse4vn8vOokd16WSuibt-JJxxzjLpROFZ0rwkhCky2w6zZbssJPFjh04QRTMWFlLzRUrTNNIT8p565n0hJ0rZ1jFJVlmRjNvW33brU26vCxFyazoVFklPzCpC9VidBJNt9F00_2-FWVbQNIL51nBnSA_DOaVKtj4dcLGTbFvo2T7MJuzsWFjNoFfDtboegpP9RrpBZkhKxPP7aR-gJ-iODv1bw2IbT4BMPwhJ_e38d6af-EU_K2kEg735SqbxdMVnp7ajVdDkBeiMlYwBObSNLnUcF3FfLpCwDJ_MBQMxILJbBwlVzKJmQqBotl0Tm71aQqTjMPI67dAKsoQKQQPvtRMVBWAyTuh7ikWe8iVFEe5mDsS7CPONq-2rOHulgEO1jiw_N4Lwnrq02gx7a_wlw0-Pl1AmBFGOBs7gA0QPpH26TLILuZBdrDjyMqJMHxxKloGbdi9NNp5rj2mMRsllzEWSq6G6h5AB5ic8EURLJ5Tn5EyJiClaLTYHU35WUCBHR7seMQMGohCeKRwuCSmOEiNMxHNp3nw3RUUs-ADAMHtpTI5V5uytMK5x4UlqW46LagEyVaXouolehR-HveegECPhHZDB6QUVlT46kLwXIkwM8BVWdM84goviV0VBmn1AHT14wgR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