[llvm] [InstCombine] Simplify zext(sub(0, trunc(x))) -> and(sub(0, x), mask) (Fixes #165306) (PR #167101)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 8 04:41:16 PST 2025


dtcxzyw wrote:

> > > In my test case, the types are i6 (Source) and i64 (Destination). It seems shouldChangeType returns false for this
> > 
> > 
> > It is a bit strange to me. What is the datalayout you use?
> 
> @dtcxzyw, Sorry, I'm not sure what you mean by datalayout. Could you clarify what you're looking for?

I added the datalayout to the motivating case. It got folded by InstCombine without your patch:
```
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i64 @neg_trunc_zext(i64 %a) {
  %t = trunc i64 %a to i6
  %n = sub i6 0, %t
  %z = zext i6 %n to i64
  ret i64 %z
}
```
```
define i64 @neg_trunc_zext(i64 %a) {
  %n = sub i64 0, %a
  %z = and i64 %n, 63
  ret i64 %z
}
```
So looks like it is not a real issue?


https://github.com/llvm/llvm-project/pull/167101


More information about the llvm-commits mailing list