[GlobalISel] Incorrect MIFlags on instructions widened by the legalizer
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Consider an arithmetic instruction that is too narrow for some target, for example:
```llvm
%2:_(s8) = nsw nuw G_ADD %0(s8), %1(s8)
```
assuming the smallest add legal for the target is a 32-bit one, this would be widened by the legalizer as:
```llvm
%3:_(s32) = G_ANYEXT %0(s8)
%4:_(s32) = G_ANYEXT %1(s8)
%5:_(s32) = nsw nuw G_ADD %3(s32), %4(s32)
%2:_(s8) = G_TRUNC %5(s32)
```
However, the legalizer doesn't drop the `nsw`/`nuw` flags from the instruction, even though it's not guaranteed that the arithmetic will not overflow with the newly inserted `G_ANYEXT` instructions as the upper 24 bits of the operands could be anything.
https://godbolt.org/z/4rr53sYbG
Contributor guide
Research direction
Start with the GlobalISel legalizer's instruction-widening path and reproduce the example using the linked Compiler Explorer case. Done means widened arithmetic no longer retains nsw/nuw when inserted extensions can permit overflow, with regression coverage for the reported case.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100