[llvm] [InstCombine] Don't folder select to or if value argument is user of invalid addrspacecast inst (PR #144686)

Wenju He via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 19 21:05:56 PDT 2025


wenju-he wrote:

> > > If an invalid addrspacecast can produce poison, then this must be specified in
> > > https://github.com/llvm/llvm-project/blob/34a48941498d95ec2682f7adaeb6115b7b4d70ba/llvm/lib/Analysis/ValueTracking.cpp#L7352
> > 
> > 
> > This PR uses isValidAddrSpaceCast query from TTI which is available in InstCombine. But TTI isn't available in above canCreateUndefOrPoison function. If we unconditionally return true for addrspacecast in canCreateUndefOrPoison, there is regression in following test probably because canCreateUndefOrPoison returning true for valid addrspacecast is preventing the optimization:
> > https://github.com/llvm/llvm-project/blob/5875fafdc547889fb089c943a881a9ab6d8a23c0/llvm/test/Transforms/Attributor/reduced/aapointer_info_map_invalidation.ll#L4-L19
> > 
> > > LangRef should also be adjusted in https://llvm.org/docs/LangRef.html#addrspacecast-to-instruction to specify that addrspacecast can introduce poison.
> > 
> > 
> > done in [ffff2c3](https://github.com/llvm/llvm-project/commit/ffff2c3624239c1290e71abcce44f3317aab597a), please review
> 
> I don't think special handling of addrspacecast in `logical and/or->bitwise and/or` works. There are many places which call `isGuaranteedNotToBeUndefOrPoison`. For example, eliminating freeze from `freeze ptr (addrspacecast ptr addrspace(5) noundef %x to ptr)` is still invalid. That is, I can construct an input to bypass the workaround and cause the miscompilation. It would be better to default to returning true for addrspacecast in `canCreateUndefOrPoison`, then add an optional callback for `isValidAddrSpaceCast` queries.

thanks @dtcxzyw I've changed to return true for addrspacecast in `canCreateUndefOrPoison` in 3128b338492a977b35b7c39bc770e85a703ca8d0. Adding an optional callback looks like the same as querying isValidAddrSpaceCast right after canCreateUndefOrPoison call, so I added isValidAddrSpaceCast query in llvm/lib/Transforms/IPO/AttributorAttributes.cpp to restore previous behavior of llvm/test/Transforms/Attributor/reduced/aapointer_info_map_invalidation.ll. The test is also updated to specify triple so that TTI is available.

Does it make sense to add an optional TTI argument to canCreateUndefOrPoison/isGuaranteedNotToBePoison/isGuaranteedNotToBeUndefOrPoison? If yes, then we don't need to query isValidAddrSpaceCast right after isGuaranteedNotToBeUndefOrPoison call in AttributorAttributes and potentially some other places.

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


More information about the llvm-commits mailing list