# Changes

## Summary

- [InstCombine] Dropping redundant masking before left-shift [5/5] (PR42563) Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: f. `((x << MaskShAmt) a>> MaskShAmt) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: f. `(ShiftShAmt-MaskShAmt) s>= 0` (i.e. `ShiftShAmt u>= MaskShAmt`) Normally, the inner pattern is sign-extend, but for our purposes it's no different to other patterns: alive proofs: f: https://rise4fun.com/Alive/7U3 For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64524
- [InstCombine] Dropping redundant masking before left-shift [4/5] (PR42563) Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: e. `((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: e. `(ShiftShAmt-MaskShAmt) s>= 0` (i.e. `ShiftShAmt u>= MaskShAmt`) alive proofs: e: https://rise4fun.com/Alive/0FT For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64521
- [InstCombine] Dropping redundant masking before left-shift [3/5] (PR42563) Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: d. `(x & ((-1 << MaskShAmt) >> MaskShAmt)) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: d. `(ShiftShAmt-MaskShAmt) s>= 0` (i.e. `ShiftShAmt u>= MaskShAmt`) alive proofs: d: https://rise4fun.com/Alive/I5Y For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64519
- [InstCombine] Dropping redundant masking before left-shift [2/5] (PR42563) Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: c. `(x & (-1 >> MaskShAmt)) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: c. `(ShiftShAmt-MaskShAmt) s>= 0` (i.e. `ShiftShAmt u>= MaskShAmt`) alive proofs: c: https://rise4fun.com/Alive/RgJh For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64517
- [InstCombine] Dropping redundant masking before left-shift [1/5] (PR42563) Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: b. `(x & (~(-1 << maskNbits))) << shiftNbits` All these patterns can be simplified to just: `x << ShiftShAmt` iff: b. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)` alive proof: b: https://rise4fun.com/Alive/y8M For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64514
- [InstCombine] Dropping redundant masking before left-shift [0/5] (PR42563) Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: a. `(x & ((1 << MaskShAmt) - 1)) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: a. `(MaskShAmt+ShiftShAmt) u>= bitwidth(x)` alive proof: a: https://rise4fun.com/Alive/wi9 Indeed, not all of these patterns are canonical. But since this fold will only produce a single instruction i'm really interested in handling even uncanonical patterns, since i have this general kind of pattern in hotpaths, and it is not totally outlandish for bit-twiddling code. For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Reviewers: spatel, nikic, huihuiz, xbolva00 Reviewed By: xbolva00 Subscribers: efriedma, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D64512

Change Type | Path in Repository | Path in Workspace |
---|---|---|

/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (diff) | llvm.src/lib/Transforms/InstCombine/InstCombineShifts.cpp | |

/llvm/trunk/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-f.ll (diff) | llvm.src/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-f.ll | |

Change Type | Path in Repository | Path in Workspace |

/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (diff) | llvm.src/lib/Transforms/InstCombine/InstCombineShifts.cpp | |

/llvm/trunk/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-e.ll (diff) | llvm.src/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-e.ll | |

Change Type | Path in Repository | Path in Workspace |

/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (diff) | llvm.src/lib/Transforms/InstCombine/InstCombineShifts.cpp | |

/llvm/trunk/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-d.ll (diff) | llvm.src/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-d.ll | |

Change Type | Path in Repository | Path in Workspace |

/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (diff) | llvm.src/lib/Transforms/InstCombine/InstCombineShifts.cpp | |

/llvm/trunk/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-c.ll (diff) | llvm.src/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-c.ll | |

Change Type | Path in Repository | Path in Workspace |

/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (diff) | llvm.src/lib/Transforms/InstCombine/InstCombineShifts.cpp | |

/llvm/trunk/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-b.ll (diff) | llvm.src/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-b.ll | |

Change Type | Path in Repository | Path in Workspace |

/llvm/trunk/lib/Transforms/InstCombine/InstCombineShifts.cpp (diff) | llvm.src/lib/Transforms/InstCombine/InstCombineShifts.cpp | |

/llvm/trunk/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-a.ll (diff) | llvm.src/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-a.ll |