<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJztWEtv4zYQ_jXyhbAh6-HYBx8cp1nkUBTYFr0GlDiS2FCkl49kvb--M5TWiR27TXfbbVEsYJgSOc9Pw08aVkbs13fM7Yz3IJjvuGdJeW3frValKNK0yXleJ_mGJu-081vTV1JDUt4wCyLUwPqgmNmB5Vo4VnGHVoxmQQvocQrvOtl2rJLeoVKSLTvvdw4tJtkt_iw8SnhyM6Ue-5mxLU0d-c5FcVVxsSrLci64mKdFs1gu6mKRp2WSrf4Jm6zmgdJwpgfMsrXgnDTaMamZB9s7ZhpWGwEDXDvyZ4JDURcUoYhyrpONRzxA6pY5UFDTQgsagfI0xwk4L3cKUNx54GLG7lhrPOOONdzSUEmHeiTtO0CPfS99km1ZFTzTR5LKmAeSQ89c75kAz6ViGBxqyBY9UvhTzIjVFvhoUqKH0NKkQfvWMWF0kl15JsJOyZp7YNA0xvpZkt4k6Wb4vzuIoQX9QKFZwHsXPbvQtuDIX4y5N0I2ZIom0N-TNeh6OjjHiAVBKZ0LQKsfgvRDnij9JH1HNhywQylRFWGxedlLNyR1FNov_AEG0_CR9wRtY03PkkXa6Oy-wJEAytK0SBfZajqf1YMARfpuu2Uecw0WMAmM48hyrbhup-Pjw0CwtqjcBolFOv6G26x03obaz7jGNJL8hvn9Dlhydc1knuF4MwoWaRWXhTP3ytRcsVaZCocTE5_AGqnxQXIl8ZoqAK9azYqXISb5NbsNuo5Qb7y3tCGwTIJ-QpwHGQENbt4XDh-NFAwDGfDJlhQgqaAgRfGRdkOS5SlFP5gA7e3-kDuLUjMuhI2pcEV2Kc9XUTLmEF8YMIimt4Pc5tnGGSVcS6NpZbj4bPhtelUzi0oXtFvwuC97TGjnLRZGRXk7VFwe408qpzMbAq0ajbH0xcXqciyuG0CKFyMMY4yktDgWrxXwEVQtXoqjNolnaXH1QsOFoZhofBaORsYE0tfQzP8r2DxyhRRwkiymciZRlHadGnLF8TnXaOMSkNk5JAkBUpjO83mark6cgI86xp7imY1ZD5Gcre5B_18B1GLctK1HVjiwzQlLxf_38Y1FHIwLyKts-lNGLMkbfM-x8ZV5keiQcH40Iii4u4lI4UsgybfOC6mT_Ae6i3LOBFvDfSMVaN7DKJodiWZHTPZmBr3Eo_H_PmjyJ-4jO_391NoH53fWxM8DJM3GAuBgoQ7W0ZXb6_rAv_g6UwofTbD6K6j4TF5_ys7fhgUXxVt2Ou7o-UxDG4OhD8eXL4O4CZevOXPcuuJ0636OaLR5niJec2d2ydcf8sRfowmihQNHjJzxLWniDU_jEk3Ef441LvErE9xYX7Rlcfd9acmzL6OhChoCK35lj1-_37noOxd9Ay6aLs58vH0nov87Ed1SN8ipD60fYk9N8caGcujt4xFArBU9ZBz7aiQparURXecx5eUiyW4_Lhf3WEOo_v7u5-3015g4hrLZ2LpDVJ46iB3ynj1h-zz00zt0goCPtHfUf2Jzipi6HTafatp2dOPNbge2ZhOxzsUqX_EJD74zds1dNQlWrY8PRVpsp0M1QxrFGzoZGYcpwvgb1Pjsb2Mf7vCizJfZatKtUxBFdZXOU7G4mtdNVuScF2JZzRdcZCXUE8UrUG6dlNdIm88nJcShsS4yOiWZfgj4yP3-eTa6zzdDI_9paORptbyZfH3Ycp2lGf7mxXxZFkU2y1awqvJC8LSpF025Quyg51Idzocmdh1NVqF1uKik88-HRxOOKbUaIGaJEXrpFazfH5KlkoiHQWOasRywRrBU8XUSz4FwXT-CPRwiHU5-Kl4_AO6rScxhHRP4HXKi5Ak>53829</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Regression in code quality for mul=>shift conversions in multiple backends
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            regression,
            code-quality,
            llvm:optimizations
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

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

<pre>
    I spotted that [rG995d400f3a3c: [InstCombine] reduce mul operands based on undemanded high bits](https://reviews.llvm.org/rG995d400f3a3c3d47bad95551dad104f686c46305)](https://reviews.llvm.org/rG995d400f3a3c3d47bad95551dad104f686c46305) caused some regressions in terms of code that previous resulted in shifts being selected generating a multiple instead. I got as far as bisecting the commit, but not as far as looking in any detail at mitigations - so creating this bug so others don't duplicate effort.

I don't think there's any suggestion the modification is wrong - this kind of issue is quite common with these demanded bits optimisations.

Take this example from `fn2_4` in 20040629-1.c from the GCC torture suite.

clang-generated .ll:
```
%struct.anon = type { i32 }
@b = dso_local global %struct.anon zeroinitializer, align 4

; Function Attrs: nounwind
define dso_local void @fn2_4(i32 noundef %x) #0 {
entry:
  %x.addr = alloca i32, align 4
  store i32 %x, i32* %x.addr, align 4
  %0 = load i32, i32* %x.addr, align 4
  %bf.load = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i32 0, i32 0), align 4
  %bf.lshr = lshr i32 %bf.load, 6
  %bf.clear = and i32 %bf.lshr, 2047
  %sub = sub i32 %bf.clear, %0
  %bf.load1 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i32 0, i32 0), align 4
  %bf.value = and i32 %sub, 2047
  %bf.shl = shl i32 %bf.value, 6
  %bf.clear2 = and i32 %bf.load1, -131009
  %bf.set = or i32 %bf.clear2, %bf.shl
  store i32 %bf.set, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i32 0, i32 0), align 4
  ret void
}
```

Result of `opt -O2` after 995d400:
```
; ModuleID = '<stdin>'
source_filename = "<stdin>"

%struct.anon = type { i32 }

@b = dso_local local_unnamed_addr global %struct.anon zeroinitializer, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn
define dso_local void @fn2_4(i32 noundef %x) local_unnamed_addr #0 {
entry:
  %bf.load = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i64 0, i32 0), align 4
  %sub1.neg = mul i32 %x, 131008
  %bf.lshr2 = add i32 %bf.load, %sub1.neg
  %bf.shl = and i32 %bf.lshr2, 131008
  %bf.clear2 = and i32 %bf.load, -131009
  %bf.set = or i32 %bf.shl, %bf.clear2
  store i32 %bf.set, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i64 0, i32 0), align 4
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn }
```

Result of `opt -O2` before that commit:
```
; ModuleID = '<stdin>'
source_filename = "<stdin>"

%struct.anon = type { i32 }

@b = dso_local local_unnamed_addr global %struct.anon zeroinitializer, align 4

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn
define dso_local void @fn2_4(i32 noundef %x) local_unnamed_addr #0 {
entry:
  %bf.load = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i64 0, i32 0), align 4
  %sub1.neg = mul i32 %x, -64
  %bf.lshr2 = add i32 %bf.load, %sub1.neg
  %bf.shl = and i32 %bf.lshr2, 131008
  %bf.clear2 = and i32 %bf.load, -131009
  %bf.set = or i32 %bf.shl, %bf.clear2
  store i32 %bf.set, i32* getelementptr inbounds (%struct.anon, %struct.anon* @b, i64 0, i32 0), align 4
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn }
```

>From a quick look, this results in mul instructions for at least i686/x86_64, RISC-V, and AArch64 when they weren't present before.

CC @spatel-gh @topperc 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWEtv4zYQ_jXyhbAh6-FYBx8cp1nkUBTYFr0GFDmS2VCkl6SS9f76zlBaJ3bsbbptt0WxgGFK5Dw_DT9pWFu5X90xv7MhgGRhywNLymv3rqpKWaRpk_NcJPmaJu-MDxvb1cpAUt4wB7IXwLpeM7sDx430rOYerVjDeiOhwym826p2y2oVPCol2XIbws6jxSS7xZ-DRwVPfqb1YzezrqWpI9-5LK5qLquyLOeSy3laNIvlQhSLPC2TrPonbDLBe0rD2w4wy9aB98oaz5RhAVznmW2YsBIGuHbkz_YeRX2vCUWU81vVBMQDlGmZBw2CFlowCFSgOU7ABbXTgOI-AJ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