[GlobalISel] Fix incorrect sign extension when combining G_INTTOPTR and G_PTR_ADD
authorLucas Prates <lucas.prates@arm.com>
Mon, 10 Jan 2022 10:19:27 +0000 (10:19 +0000)
committerLucas Prates <lucas.prates@arm.com>
Thu, 20 Jan 2022 17:02:52 +0000 (17:02 +0000)
commit283f5a198a0e3c9978ca426e64a3011b566c2581
treefcd22270a500f59d02aa2821becf8f7f2320f990
parentfabf1de13202030151899786c21d624b96605ca3
[GlobalISel] Fix incorrect sign extension when combining G_INTTOPTR and G_PTR_ADD

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.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D116941
llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h
llvm/include/llvm/Target/GlobalISel/Combine.td
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
llvm/test/CodeGen/AArch64/GlobalISel/combine-ptradd-int2ptr.mir
llvm/test/CodeGen/AArch64/GlobalISel/inttoptr_add.ll [new file with mode: 0644]