422 Commits

Author SHA1 Message Date
YunQiang Su
8988148003
Intrinsic: introduce minimumnum and maximumnum (#93841)
Currently, on different platform, the behaivor of llvm.minnum is
different if one operand is sNaN:

When we compare sNaN vs NUM:

ARM/AArch64/PowerPC: follow the IEEE754-2008's minNUM: return qNaN.
RISC-V/Hexagon follow the IEEE754-2019's minimumNumber: return NUM. X86:
Returns NUM but not same with IEEE754-2019's minimumNumber as
     +0.0 is not always greater than -0.0.
MIPS/LoongArch/Generic: return NUM.
LIBCALL: returns qNaN.

So, let's introduce llvm.minmumnum/llvm.maximumnum, which always follow
IEEE754-2019's minimumNumber/maximumNumber.

Half-fix: #93033
2024-06-21 11:53:08 +08:00
Christudasan Devadasan
27bebc1161
[GISel] Unify multiple instances of getTypeForLLT (NFC) (#95577)
Multiple static instances of this utility function have been found in
different GlobalISel files.
Unifying them by adding an instance in utils.cpp.
2024-06-15 18:11:32 +05:30
Dhruv Chawla
e12bf36d23
[GISel][CombinerHelper] Combine op(trunc(x), trunc(y)) -> trunc(op(x, y)) (#89023) 2024-06-03 10:42:10 +05:30
Yingwei Zheng
0864501b97
[GISel] Convert zext nneg to sext if it is cheaper (#93856)
This patch converts `zext nneg` to `sext` on RISCV to use free sext.

---------

Co-authored-by: Thorsten Schütt <schuett@gmail.com>
2024-06-01 15:02:43 +08:00
Him188
8bce40b1eb
[AArch64][GISel] Support SVE with 128-bit min-size for G_LOAD and G_STORE (#92130)
This patch adds basic support for scalable vector types in load & store
instructions for AArch64 with GISel.

Only scalable vector types with a 128-bit base size are supported, e.g.
`<vscale x 4 x i32>`, `<vscale x 16 x i8>`.

This patch adapted some ideas from a similar abandoned patch
[https://github.com/llvm/llvm-project/pull/72976](https://github.com/llvm/llvm-project/pull/72976).
2024-05-30 09:10:43 +01:00
Thorsten Schütt
6d90ac1e06
[GlobalIsel] Combine freeze (#93239) 2024-05-29 18:05:33 +02:00
Dhruv Chawla
4c48b3cb5c
[GISel][CombinerHelper] Push freeze through non-poison-producing operands (#90618)
This combine matches the existing fold in InstCombine, i.e.
InstCombinerImpl::pushFreezeToPreventPoisonFromPropagating.

It tries to push freeze through an operand if the operand has only one
maybe-poison operand and all other operands are guaranteed non-poison,
and if the operation itself cannot generate poison (eg. add with nsw can
generate poison, even with non-poison operands).

This is beneficial because it can potentially enable other optimizations
to occur that would otherwise be blocked because of the freeze.
2024-05-23 13:45:52 +05:30
Thorsten Schütt
778826f0b8
[GlobalIsel] Combine select to integer min max more (#92570) 2024-05-18 13:43:10 +02:00
Thorsten Schütt
9bffe79049
[GlobalIsel] Speedup select to integer min/max (#92378)
https://github.com/llvm/llvm-project/issues/92309
2024-05-17 07:32:18 +02:00
Pierre van Houtryve
0bc1ec5cda
[GlobalISel] Reduce KnownBits usage in matcher combines (#92381)
Two icmp/and combines forced computation of KnownBits on all operands
everytime. We can avoid computing KnownBits on the LHS by exploiting a
couple of properties:
- Constants are always on the RHS for those instructions. If we have no
KnownBits on the RHS, we can bail out early and avoid computing LHS
knownbits.
- For icmp uge/ult 0, we don't need to know the KBs of the LHS to infer
the result

This allows to save some KnownBits calls, which are very expensive,
without affecting codegen.
2024-05-16 13:34:22 +02:00
Thorsten Schütt
737e0bcfe3
[GlobalIsel] combine ext of trunc with flags (#87115)
https://github.com/llvm/llvm-project/pull/85592

https://discourse.llvm.org/t/rfc-add-nowrap-flags-to-trunc/77453

https://github.com/llvm/llvm-project/pull/88609
2024-05-08 14:27:02 +02:00
AtariDreams
13188bcd9f
[GlobalISel]: Simplify udiv lowering by determining known zeros (#89678) 2024-04-24 22:14:02 +02:00
Xu Zhang
f6d431f208
[CodeGen] Make the parameter TRI required in some functions. (#85968)
Fixes #82659

There are some functions, such as `findRegisterDefOperandIdx` and  `findRegisterDefOperand`, that have too many default parameters. As a result, we have encountered some issues due to the lack of TRI  parameters, as shown in issue #82411.

Following @RKSimon 's suggestion, this patch refactors 9 functions, including `{reads, kills, defines, modifies}Register`,  `registerDefIsDead`, and `findRegister{UseOperandIdx, UseOperand, DefOperandIdx, DefOperand}`, adjusting the order of the TRI parameter and making it required. In addition, all the places that call these functions have also been updated correctly to ensure no additional impact.

After this, the caller of these functions should explicitly know whether to pass the `TargetRegisterInfo` or just a `nullptr`.
2024-04-24 14:24:14 +01:00
AtariDreams
5fef5e68dc
[GlobalISel] matchSDivByConst should use isNullValue() (#89666)
It has been using isZeroValue(), which is for floats, not integers.
2024-04-22 15:47:18 -07:00
chuongg3
821935bb55
[AArch64][GlobalISel] Combine Shuffles of G_CONCAT_VECTORS (#87489)
Combines G_SHUFFLE_VECTOR whose sources comes from G_CONCAT_VECTORS into
a single G_CONCAT_VECTORS instruction.

a = G_CONCAT_VECTORS x, y, undef, undef
b = G_CONCAT_VECTORS z, undef, undef, undef
c = G_SHUFFLE_VECTORS a, b, <0, 1, 4, undef>
===>
c = G_CONCAT_VECTORS x, y, z, undef`
2024-04-22 10:47:09 +01:00
darkbuck
e2c91091e5
[GlobalISel] Handle more commutable instructions in commute_constant_to_rhs
Reviewers:
rupprecht, aartbik, cyndyishida, Pierre-vh, aemerson, ftynse, hanhanW, banach-space, jayfoad, nicolasvasilache, daniel-grumberg, arsenm, PeimingLiu, JDevlieghere, matthias-springer

Reviewed By: arsenm, Pierre-vh

Pull Request: https://github.com/llvm/llvm-project/pull/87424
2024-04-16 02:36:12 -04:00
darkbuck
8e98435ae9
[GISel][Combine] Enhance combining on G_BUILD_VECTOR
Reviewers: aemerson, arsenm

Reviewed By: arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/87831
2024-04-06 18:33:01 -04:00
Amara Emerson
60fc4ac67a [GlobalISel] Don't form anyextending atomic loads.
Until we can reliably check the legality and improve our selection of these,
don't form them at all.
2024-04-05 13:34:59 -07:00
Michael Liao
a1b2f0cc44 Reland "[GlobalISel] Fix the infinite loop issue in commute_int_constant_to_rhs"
- That test needs to disable combine rules by name and hence requires `asserts`.
2024-04-05 10:34:12 -04:00
Gulfem Savrun Yeniceri
be8fd86f6a Revert "[GlobalISel] Fix the infinite loop issue in commute_int_constant_to_rhs"
This reverts commit 1f01c580444ea2daef67f95ffc5fde2de5a37cec
because combine-commute-int-const-lhs.mir test failed in
multiple builders.
https://lab.llvm.org/buildbot/#/builders/124/builds/10375
https://luci-milo.appspot.com/ui/p/fuchsia/builders/prod/clang-linux-x64/b8751607530180046481/overview
2024-04-04 16:39:31 +00:00
Jay Foad
1b761205f2
[APInt] Add a simpler overload of multiplicativeInverse (#87610)
The current APInt::multiplicativeInverse takes a modulus which can be
any value, but all in-tree callers use a power of two. Moreover, most
callers want to use two to the power of the width of an existing APInt,
which is awkward because 2^N is not representable as an N-bit APInt.

Add a new overload of multiplicativeInverse which implicitly uses
2^BitWidth as the modulus.
2024-04-04 16:11:06 +01:00
darkbuck
1f01c58044
[GlobalISel] Fix the infinite loop issue in commute_int_constant_to_rhs
- When both operands are constant, the matcher runs into an infinite
  loop as the commutation should be applied only when LHS is a constant
  and RHS is not.

Reviewers: arsenm

Reviewed By: arsenm

Pull Request: https://github.com/llvm/llvm-project/pull/87426
2024-04-03 20:52:21 -04:00
Thorsten Schütt
8bb9443333
[GlobalIsel] Combine G_EXTRACT_VECTOR_ELT (#85321)
preliminary steps
2024-04-02 09:01:24 +02:00
Shilei Tian
360f7f5674
[GlobalISel] Call setInstrAndDebugLoc before tryCombineAll (#86993)
This can remove all unnecessary redundant calls in each combiner.
2024-03-29 15:27:28 -04:00
Shilei Tian
661bb9daae
[GlobalISel] Handle div-by-pow2 (#83155)
This patch adds similar handling of div-by-pow2 as in `SelectionDAG`.
2024-03-29 12:41:47 -04:00
Thorsten Schütt
deefe3fbc9
[GlobalIsel] Post-review combine ADDO (#85961)
https://github.com/llvm/llvm-project/pull/82927
2024-03-21 03:56:40 +01:00
David Green
601e102bdb
[CodeGen] Use LocationSize for MMO getSize (#84751)
This is part of #70452 that changes the type used for the external
interface of MMO to LocationSize as opposed to uint64_t. This means the
constructors take LocationSize, and convert ~UINT64_C(0) to
LocationSize::beforeOrAfter(). The getSize methods return a
LocationSize.

This allows us to be more precise with unknown sizes, not accidentally
treating them as unsigned values, and in the future should allow us to
add proper scalable vector support but none of that is included in this
patch. It should mostly be an NFC.

Global ISel is still expected to use the underlying LLT as it needs, and
are not expected to see unknown sizes for generic operations. Most of
the changes are hopefully fairly mechanical, adding a lot of getValue()
calls and protecting them with hasValue() where needed.
2024-03-17 18:15:56 +00:00
Thorsten Schütt
5f774619ea
[GlobalIsel] Combine ADDO (#82927)
Perform the requested arithmetic and produce a carry output in addition
to the normal result.

Clang has them as builtins (__builtin_add_overflow_p). The middle end
has intrinsics for them (sadd_with_overflow).

AArch64: ADDS Add and set flags

On Neoverse V2, they run at half the throughput of basic arithmetic and
have a limited set of pipelines.
2024-03-14 12:45:19 +01:00
Shilei Tian
55d4816393 [NFC] Remove trailing white spaces 2024-03-13 20:18:40 -04:00
Amara Emerson
26fa440957 [GlobalISel] Fix yet another pointer type invalid combining issue, this time in tryFoldSelectOfConstants() 2024-03-07 15:58:28 -08:00
Amara Emerson
641b98a0d1 [GlobalISel] Fix crash in tryFoldAndOrOrICmpsUsingRanges() with pointer types. 2024-03-07 12:56:40 -08:00
Owen Anderson
ebb64d8370
[GlobalISel] Make the Combiner insert G_FREEZE when converting G_SELECT to binary operations. (#82733)
This is needed because the binary operators (G_OR and G_AND) do
not have the poison-suppressing semantics of G_SELECT.

Fixes https://github.com/llvm/llvm-project/issues/72475
2024-02-26 10:50:37 -05:00
Owen Anderson
c02b0d008c
[GlobalISel] Make sure to check for load barriers when merging G_EXTRACT_VECTOR_ELT into G_LOAD. (#82306)
Fixes https://github.com/llvm/llvm-project/issues/78477
2024-02-20 22:42:14 -05:00
David Green
1b12974ccb
[AArch64][AMDGPU][GlobalISel] Remove vector handling from unmerge_dead_to_trunc (#82224)
This combine transforms an unmerge where only the first element is used
into a truncate. That works OK for scalar but for vector needs to insert
a bitcast to integers, perform the truncate then bitcast back to
vectors. This generates more awkward code than using an Unmerge.
2024-02-20 10:54:44 +00:00
Thorsten Schütt
63a4b4f610
[GlobalIsel] Combine logic of floating point compares (#81886)
It is purely based on symmetry. Registers can be scalars, vectors, and
non-constants.

X < 5.0 || X > 5.0
  ->
X != 5.0

X < Y && X > Y
  ->
  FCMP_FALSE

X < Y && X < Y
  ->
  FCMP_TRUE

see InstCombinerImpl::foldLogicOfFCmps
2024-02-20 09:56:33 +01:00
chuongg3
f6f8e202f5
[AArch64][GlobalISel] Refactor Combine G_CONCAT_VECTOR (#80866)
The combine now works using tablegen and checks if new instruction is
legal before creating it.
2024-02-15 10:09:20 +00:00
Thorsten Schütt
364f781344
[GlobalIsel] Combine logic of icmps (#77855)
Inspired by InstCombinerImpl::foldAndOrOfICmpsUsingRanges with some
adaptations to MIR.
2024-02-06 15:58:02 +01:00
Dávid Ferenc Szabó
55172b7005
[GlobalISel] Improve combines for extend operation by taking hint ins… (#74125)
…tructions into account

Hint instructions like G_ASSERT_ZEXT cann be viewed as a copy. Including
this fact into the combiner allows the match more patterns involving
such instructions.
2024-01-17 15:21:02 +07:00
Amara Emerson
eb009ed249 [GlobalISel] Fix the select->minmax combine from trying to operate on pointer types. 2024-01-15 18:20:18 -08:00
Amara Emerson
a946934a12 [GlobalISel][NFC] Use GPhi wrapper in more places instead of iterating over operands. 2024-01-11 22:25:53 -08:00
Emil J
3baedb4111
[GISel] Fix #77762: extend correct source registers in combiner helper rule extend_through_phis (#77765)
Since we already know which register we want to extend, we don't have to
ask its defining MI about it

---------

Co-authored-by: Emil Tywoniak <Emil.Tywoniak@hightec-rt.com>
2024-01-12 12:09:58 +08:00
Thorsten Schütt
d7642b2200
[GlobalIsel] Combine select to integer minmax (second attempt). (#77520)
Instcombine canonicalizes selects to floating point and integer minmax.
This and the dag combiner canonicalize to floating point minmax. None of
them canonicalizes to integer minmax. On Neoverse V2 basic integer
arithmetic and integer minmax have the same costs.
2024-01-11 09:50:33 +01:00
Thorsten Schütt
a085402ef5 Revert "[GlobalIsel] Combine select of binops (#76763)"
This reverts commit 1687555572ee4fb435da400dde02e7a1e60b742c.
2024-01-06 17:04:24 +01:00
Thorsten Schütt
1687555572
[GlobalIsel] Combine select of binops (#76763) 2024-01-06 11:28:10 +01:00
Thorsten Schütt
4b9194952d
[GlobalIsel] Combine selects with constants (#76089)
A first small step at combining selects.
2024-01-02 17:26:39 +01:00
Jie Fu
a8d5f731d6 [GlobalISel] Remove unused variable 'ResultTy' in CombinerHelper.cpp (NFC)
llvm-project/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp:1179:7:
 error: unused variable 'ResultTy' [-Werror,-Wunused-variable]
  LLT ResultTy = MRI.getType(MI.getOperand(0).getReg());
      ^
1 error generated.
2023-12-06 14:08:07 +08:00
Pranav Taneja
41507fe595
[GISel] Combine (Scalarize) vector load followed by an element extract. 2023-12-06 11:23:23 +05:30
Antonio Frighetto
0ff5281c94 [GlobalISel] Treat shift amounts as unsigned in matchShiftImmedChain
A miscompilation issue in the GISel pre-legalization
phase has been addressed with improved routines.

Fixes: https://github.com/llvm/llvm-project/issues/71440.
2023-11-24 18:14:52 +01:00
Pierre van Houtryve
b26e6a8eb5
[GlobalISel] Add GITypeOf special type (#66079)
Allows creating a register/immediate that uses the same type as a
matched operand.
2023-10-31 09:57:10 +01:00
Amara Emerson
93659947d2
[AArch64][GlobalISel] Add support for pre-indexed loads/stores. (#70185)
The pre-index matcher just needs some small heuristics to make sure it
doesn't cause regressions. Apart from that it's a simple change, since
the only difference is an immediate operand of '1' vs '0' in the
instruction.
2023-10-26 10:29:12 -07:00