This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Fix incorrect sign extension when combining G_INTTOPTR and G_PTR_ADD
ClosedPublic

Authored by pratlucas on Jan 10 2022, 7:21 AM.

Details

Summary

The GlobalISel combiner currently uses sign extension when manipulating
the LHS constant when combining a sequence of the following sequence of
machine instructions into a single constant:

%0:_(s32) = G_CONSTANT i32 <CONSTANT>
%1:_(p0) = G_INTTOPTR %0:_(s32)
%2:_(s64) = G_CONSTANT i64 <CONSTANT>
%3:_(p0) = G_PTR_ADD %1:_, %2:_(s64)

This causes an issue when the bit width of the first contant and the
target pointer size are different, as G_INTTOPTR has no sign extension
semantics.

This patch fixes this by capture an arbitrary precision in when matching
the constant, allowing the matching function to correctly zero extend
it.

Diff Detail

Event Timeline

pratlucas created this revision.Jan 10 2022, 7:21 AM
pratlucas requested review of this revision.Jan 10 2022, 7:21 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2022, 7:21 AM
arsenm added inline comments.Jan 10 2022, 7:43 AM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
2028

Why leave this as int64_t? Why not just leave everything as APInt?

pratlucas updated this revision to Diff 398873.Jan 11 2022, 1:41 AM

Using APInt on match/apply functions' interfaces and merging mir test into existing one.

pratlucas marked an inline comment as done.Jan 11 2022, 1:42 AM
pratlucas retitled this revision from [GlobalISel] Fix incorrect sign extension when combining G_INTTOPTR and G_ADD to [GlobalISel] Fix incorrect sign extension when combining G_INTTOPTR and G_PTR_ADD.Jan 11 2022, 1:46 AM
arsenm added inline comments.Jan 12 2022, 4:10 PM
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
2038

This is a confusing way of stating that inttoptr uses zext

2040

Why sext here?

pratlucas updated this revision to Diff 399612.Jan 13 2022, 3:06 AM

Updating comment.

pratlucas marked an inline comment as done.Jan 13 2022, 3:27 AM
pratlucas added inline comments.
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
2040

Using sext here makes sure the pointer arithmetic works properly for negative offsets from G_PTR_ADD. This is also aligned with the behaviour of IRTranslator, which sign-extends the constant offset value (see https://github.com/llvm/llvm-project/blob/main/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp#L1516).
The extension here is not strictly necessary, as the IRTranslator already adjusts the constant size to match the pointer, but it should make things more future-proof without any harm.

arsenm accepted this revision.Jan 18 2022, 9:11 AM
This revision is now accepted and ready to land.Jan 18 2022, 9:11 AM