21217 Commits

Author SHA1 Message Date
Prabhu Rajasekaran
1531dfcb3a
[llvm] Linker flags for UEFI (#137878)
Use appropriate linker flag format to match PE spec for UEFI.
2025-04-30 12:14:58 -07:00
Nikita Popov
6feb4a8ef4
[IR] Don't allow values of opaque type (#137625)
Consider opaque types as non-first-class types, i.e. do not allow SSA
values to have opaque type.
2025-04-30 15:01:00 +02:00
Akshat Oke
e91cbd4f29
[CodeGen][NPM] Port VirtRegRewriter to NPM (#130564) 2025-04-30 14:10:46 +05:30
Vikram Hegde
53a8b89003
[CodeGen][NewPM] Port "ShrinkWrap" pass to NPM (#129880) 2025-04-30 13:11:17 +05:30
Nikita Popov
38cb7d5e75
[IR] Don't allow label arguments (#137799)
We currently accept label arguments to inline asm calls. This support
predates both blockaddresses and callbr and is only covered by one X86
test. Remove it in favor of callbr (or at least blockaddress, though
that cannot guarantee correct codegen, just like using block labels
directly can't).

I didn't bother implementing bitcode upgrade support for this, but I can
add it if desired.
2025-04-30 09:11:36 +02:00
Peter Collingbourne
c88b537771
Target: Stop assigning RELRO sections to .ldata.rel.ro.
Linkers do not currently support PT_GNU_RELRO for SHF_X86_64_LARGE
sections; that would require the linker to emit more than one
PT_GNU_RELRO because large sections are discontiguous by design,
and most ELF dynamic loaders do not support that (bionic appears to
support it but glibc/musl/FreeBSD/NetBSD/OpenBSD appear not to). With
current linkers these sections will end up in .ldata which results
in silently disabling RELRO. Therefore, disable SHF_X86_64_LARGE for
RELRO sections. If this ever gets supported by downstream components
in the future we could add an opt-in flag for moving these sections
to .ldata.rel.ro which would trigger the creation of a second
PT_GNU_RELRO.

Reviewers: MaskRay, aeubanks

Reviewed By: aeubanks

Pull Request: https://github.com/llvm/llvm-project/pull/137742
2025-04-29 12:40:43 -07:00
Sergei Barannikov
becd418626
[CGP] Despeculate ctlz/cttz with "illegal" integer types (#137197)
The code below the removed check looks generic enough to support
arbitrary integer widths. This change helps 32-bit targets avoid
expensive expansion/libcalls in the case of zero input.

Pull Request: https://github.com/llvm/llvm-project/pull/137197
2025-04-29 22:33:40 +03:00
Feng Zou
f02c93d707
[X86] Remove LLD command in LIT test (#137794)
The test introduced in #136660, may be failed as ld.lld command not
found.
2025-04-29 21:27:36 +08:00
Rohit Aggarwal
2466100128
[X86] Add new #134979 test cases for gather scalar (#137416)
We are adding new test cases to see the transformation impact due to #134979.

These are similar to previous commit #688c3ffb057a87b86c6c1e77040418adf511efbb.

Handling struct with two member and different vector factor.

---------

Co-authored-by: Rohit Aggarwal <Rohit.Aggarwal@amd.com>
2025-04-29 13:52:43 +01:00
Feng Zou
bd6addc032
[X86][APX] Suppress EGPR/NDD instructions for relocations (#136660)
Suppress EGPR/NDD instructions for relocations to avoid APX relocation
types emitted. This is to keep backward compatibility with old version
of linkers without APX support. The use case is to try APX features with
LLVM + old built-in linker on RHEL9 OS which is expected to be EOL in
2032.
If there are APX relocation types, the old version of linkers would
raise "unsupported relocation type" error. Example:
```
$ llvm-mc -filetype=obj -o got.o -triple=x86_64-unknown-linux got.s
$ ld got.o -o got.exe
ld: got.o: unsupported relocation type 0x2b
...

$ cat got.s
...
movq foo@GOTPCREL(%rip), %r16

$ llvm-objdump -dr got.o
...
1: d5 48 8b 05 00 00 00 00       movq    (%rip), %r16
0000000000000005:  R_X86_64_CODE_4_GOTPCRELX    foo-0x4
```
2025-04-29 19:12:59 +08:00
Simon Pilgrim
8961f3ee75
[X86] shouldReduceLoadWidth - don't split loads if we can freely reuse full width legal binop (#129695)
Currently shouldReduceLoadWidth is very relaxed about when loads can be
split to avoid extractions from the original full width load - resulting
in many cases where the number of memory operations notably increases,
replacing the cost of a extract_subvector for additional loads.

This patch adjusts the 256/512-bit vector load splitting metric to
detect cases where ANY use of the full width load is be used directly -
in which case we will now reuse that load for smaller types, unless we'd
need to extract an upper subvector / integer element - i.e. we now
correctly treat (extract_subvector cst, 0) as free.

We retain the existing logic of never splitting loads if all uses are
extract+stores but we improve this by peeking through bitcasts while
looking for extract_subvector/store chains.

This required a number of fixes - shouldReduceLoadWidth now needs to
peek through bitcasts UP the use-chain to find final users (limited to
hasOneUse cases to reduce complexity). It also exposed an issue in
isTargetCanonicalConstantNode which assumed that a load of vector
constant data would always extract, which is no longer the case.
2025-04-29 10:35:15 +01:00
weiguozhi
b25b51eb63
[InlineSpiller] Check rematerialization before folding operand (#134015)
Current implementation tries to fold the operand before
rematerialization because it can reduce one register usage. But if there
is a physical register available we can still rematerialize it without
causing high register pressure.

This patch do this check to find the better choice. Then we can produce

    xorps %xmm1, %xmm1
    ucomiss %xmm1, %xmm0

instead of 

    ucomiss LCPI0_1(%rip), %xmm0
2025-04-28 09:52:03 -07:00
Haohai Wen
3579fc003d
[COFF] Preserve UniqueID used to create MCSectionCOFF (#123869)
This UniqueID can be used later to create associative section.
e.g. A .pseudo_probe associated to the section of the corresponding
function.
2025-04-28 13:39:40 +08:00
Phoebe Wang
a87d8e9442
[X86][AVX512FP16] Decouple AVX512VL and AVX512DQ from AVX512FP16 (#137450)
Fixes: #136209
2025-04-27 14:01:37 +08:00
Phoebe Wang
af223bc696
[X86] Avoid zero extend i16 when inserting fp16 (#126194) 2025-04-27 09:51:13 +08:00
Simon Pilgrim
5d91d12d91
[X86] getFauxShuffleMask - generalise logical shifts to work with non-uniform shift amounts (#137349)
Still doesn't help pre-AVX2 targets which might have lowered SHL to a MUL by that point.
2025-04-26 11:05:30 +01:00
Simon Pilgrim
9f94e36247 [X86] vector-shuffle-combining-ssse3.ll - add tests showing the failure to merge logical shifts with non-uniform shift amounts into shuffles 2025-04-25 15:10:33 +01:00
Pawan Nirpal
1ff931ea5b
[X86][NFC] Add sitofp and fptosi tests for x87 mode (#136860)
Add SDAG and GlobalISel runs as precommit testcases for sitofp and 
fptosi in x87 mode.
2025-04-25 15:46:26 +02:00
Feng Zou
7a424276de
Revert "[X86][APX] Support peephole optimization with CCMP instruction (#129994)" (#136796)
This reverts commit 7ae75851b2e1570662261c97c13cfc65357c283d.

There is a problem with peephole optimization for CCMP instruction. See
the example below:
C source code:
```
  if (a > 2 || (b && (a == 2))) { … }
```
MIR before peephole optimization:
```
  TEST8rr %21:gr8, %21:gr8, implicit-def $eflags // b
  CCMP32ri %30:gr32, 2, 0, 5, implicit-def $eflags, implicit $eflags // a == 2
  CCMP32ri %30:gr32, 3, 0, 5, implicit-def $eflags, implicit $eflags // a > 2 (transformed to a < 3)
  JCC_1 %bb.6, 2, implicit $eflags
  JMP_1 %bb.3
```
Inputs:
```
  a = 1, b = 0.
```
With the inputs above, the expected behavior is to jump to %bb.6 BB.
After TEST8rr instruction being executed with b(%21) == 0, the ZF bit is
set to 1 in eflags, so the eflags doesn't satisfy SCC condition in the
following CCMP32ri instruction (for a==2 condition) which skips compare
a(%30) with 2 and set flags in its payload to 0x202 (ZF = 0). The eflags
satisfies the SCC condition in the 2nd CCMP32ri instruction which
compares a(%30) with 3. It sets CF to 1 in eflags and the JCC
instruction jumps to %bb.6 BB.

But after adding CCMP support, peephole optimization eliminates the 2nd
CCMP32ri instruction and updates the condition of JCC instruction to
"BE" from "B". With the same inputs, JCC instruction falls through to
the next instruction. It's not expected and the 2nd CCMP32ri should not
be eliminated.
```
  TEST8rr %21:gr8, %21:gr8, implicit-def $eflags // b
  CCMP32ri %30:gr32, 2, 0, 5, implicit-def $eflags, implicit $eflags  // a == 2
  JCC_1 %bb.6, 6, implicit $eflags
  JMP_1 %bb.3
```
2025-04-25 10:55:31 +08:00
Simon Pilgrim
10f6c3e270
[DAG] visitCONCAT_VECTORS - relax legality checks (#137210)
We can fold combineConcatVectorOfConcatVectors/combineConcatVectorOfExtracts until after vector legalization
2025-04-24 19:08:06 +01:00
Evgenii Kudriashov
ed866d994c
[X86][Combine] Ensure single use chain in extract-load combine (#136520)
The problem is that `SrcBC = peekThroughBitcasts(Src)` doesn't ensure
single use chain. It results in the situation when a cast may have
multiple users and instead of replacing a load we introduce a new one.
The situation is worsened by the fact that we've replaced the token from
the original load and its correct memory order now is not guaranteed.
2025-04-24 16:16:50 +02:00
Simon Pilgrim
e3eee9e81e [X86] vector-trunc.ll - replace stores to ptr undef with real ptr values
Prevents some unnecessary codegen changes in an upcoming patch
2025-04-24 15:09:17 +01:00
Simon Pilgrim
10ea5eedb1 [X86] pr40891.ll - add X64 test coverage 2025-04-24 14:41:58 +01:00
Simon Pilgrim
8b2d269db2 [X86] Add extended test coverage for #135010 2025-04-24 13:38:20 +01:00
Simon Pilgrim
acc335bfa7 [X86] Add build vector test patterns with only 2 unique scalars
Based off test coverage for #135753 - these should be lowered to BLEND(BROADCAST(X),BROADCAST(Y))
2025-04-24 11:47:09 +01:00
Simon Pilgrim
88083a0c42
[X86] SimplifyDemandedVectorEltsForTargetNode - handle 512-bit X86ISD::VPERMI with lower half demanded elts (#137139)
512-bit X86ISD::VPERMI nodes handle the lower/upper 256-bits separately - so if we don't demand the upper half elements, we can just use the 256-bit variant.
2025-04-24 10:41:31 +01:00
Evgenii Kudriashov
9ad219372a
[X86][NFC] Precommit test for #136520 2025-04-24 02:57:59 +02:00
Sergei Barannikov
5080a0251f
[CodeGenPrepare] Unfold slow ctpop when used in power-of-two test (#102731)
DAG combiner already does this transformation, but in some cases it does
not have a chance because either CodeGenPrepare or SelectionDAGBuilder
move icmp to a different basic block.

https://alive2.llvm.org/ce/z/ARzh99

Fixes #94829

Pull Request: https://github.com/llvm/llvm-project/pull/102731
2025-04-23 08:54:10 +03:00
zhijian lin
afda4c295b
Reland [SelectionDAG] Folding ZERO-EXTEND/SIGN_EXTEND poison to Poison value in getNode (#136701)
This patch addresses the signed/zero extension of poison by using a
poison value of the extended type instead of a constant zero of the
extended type.
2025-04-22 17:36:41 -04:00
Iris
2b71269388
[SelectionDAG][X86] Fold sub(x, mul(divrem(x,y)[0], y)) to divrem(x, y)[1] (#136565)
Closes #51823.
2025-04-22 20:57:03 +08:00
Simon Pilgrim
788b50a438 [X86] Add test coverage for #136368 2025-04-21 13:24:20 +01:00
Phoebe Wang
ddee2d8559
[X86][APX] Combine (X86Sub 0, AND(X, Y)) to (X86And X, Y) for CLOAD/CSTORE (#136429)
https://godbolt.org/z/TsWochrbf
2025-04-21 10:07:45 +08:00
Evgenii Kudriashov
e8245d5324
[X86][GlobalISel] Support addr matching in SDAG patterns (#130445)
addr matching was the only gatekeeper for starting selecting G_LOAD
and G_STORE using SDAG patterns.

* Introduce a complex renderer gi_addr for addr. In this patch only
the existing functionality has been implemented. The renderer's name is
the same as in SDAG: selectAddr. Apparently the type of
GIComplexOperandMatcher doesn't matter as RISCV also uses s32 for
both 64 and 32 bit pointers.
* X86SelectAddress now is used for both: pattern matching and manual
selection. As a result it accumulates all the code that previously was
distributed among different selection functions.
* Replace getLoadStoreOp with getPtrLoadStoreOp in Load/Store
selector as GlobalISel matcher or emitter can't map the pointer type
into i32/i64 types used in SDAG patterns for pointers. So the load and
store selection of pointers is still manual. getLoadStoreOp is still
present because it is used in G_FCONSTANT lowering that requires extra
efforts to select it using SDAG patterns.
* Since truncating stores are not supported, we custom legalize them by
matching types of store and MMO.
* Introduce a constant pool flag in X86AddressMode because otherwise
we need to introduce a GlobalISel copy for X86ISelAddressMode.
* Also please notice in the tests that GlobalISel prefers to fold memory
operands immediately comparing to SDAG. The reason is that GlobalISel
doesn't have target hooks in GIM_CheckIsSafeToFold. Or maybe another
check on profitability is required along with safety check that is
currently not present.
2025-04-19 22:59:18 +02:00
Evgenii Kudriashov
db97d56c97
[X86][APX] Handle AND_NF instruction for compare peephole (#136233) 2025-04-19 01:48:40 +02:00
Alex MacLean
aed4ecc456
[DAGCombiner] Fold and/or of NaN SETCC - tests follow up (#136168)
Follow up to #135645 to address test cleanup review.
2025-04-18 14:32:20 -07:00
zhijian lin
3dfdb4dad5
[SelectionDAG] Propagate poison in getNode with two operands if the input is poison. (#135387)
Propagation to poison in function `SDValue
SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,SDValue
N1, SDValue N2, const SDNodeFlags Flags) ` if one of the input is
poison.
 
 The patch also revert the test cases
 llvm/test/CodeGen/X86/pr119158.ll
 llvm/test/CodeGen/X86/half.ll
 
which are mentioned in
https://github.com/llvm/llvm-project/pull/125883#discussion_r2021390919

---------

Co-authored-by: Amy Kwan <amy.kwan1@ibm.com>
2025-04-17 09:23:14 -04:00
Alex MacLean
1bfd444628
[DAGCombiner] Fold and/or of NaN SETCC (#135645)
Fold an AND or OR of two NaN SETCC nodes into a single SETCC where
possible. This optimization already exists in InstCombine but adding in
here as well can allow for additional folding if more logical operations
are exposed.
2025-04-16 06:48:42 -07:00
Simon Pilgrim
bb5f53c727
[DAG] isSplatValue - only treat binop splats with repeated undef elements as undef (#135945)
#135597 didn't correctly fix the issue of binops with an undef element
from only one operand - only reporting the common undef elements could
incorrectly recognise splats where the (binop X, undef) fold might
actually be different - we need to ensure both operands have the same
demanded undefs for certainty.

Fixes #135917
2025-04-16 12:34:11 +01:00
Simon Pilgrim
1f96aea037 [X86] Add test coverage for #135917 2025-04-16 10:44:46 +01:00
Feng Zou
bed03ae366
[X86] Fix the issue of creating index reg negations (#135632)
The 8 and 16 bit LEA instruction support was added by PR #122102, and we
have to update creating index register negations accordingly. The issue
is exposed with APX NDD instructions.
2025-04-16 10:47:05 +08:00
Phoebe Wang
17ac7e0e84
[X86][APX] Fix a typo in LEA suffix (#135734)
Found during reviewing #135632
2025-04-15 17:00:25 +08:00
Simon Pilgrim
17b4cacbd4
[DAG] isSplatValue - only treat binop splats shared undef elements as undef (#135597)
#134602 demonstrated an issue where an AND node always had at least one demanded UNDEF element in either operand, and incorrectly reported this an all-undef result - despite the other element being 0 (so would correctly fold to 0).

This fix only assumes a binops splats element is undefined if both operands are undef.

Fixes #134602
2025-04-15 08:33:42 +01:00
Michael Maitland
74e8f29f31
[RegAlloc] Sort CopyHint by IsCSR (#131046)
`weightCalcHelper` is responsible for adding hints to MRI. Prior to this
PR, we fell back on register ID as the last tie breaker for sorting
hints. However, there is an opportunity to add an additional sorting
characteristic: whether or not a register is a callee-saved-register.

I thought of this idea because I saw that `AllocationOrder::create`
calls `RegisterClassInfo::getOrder`, which returns a list of registers
such that the registers which alias callee-saved-registers come last.
From this, I conclude that the register allocator prefers an order such
that callee-saved-registers are allocated after
non-callee-saved-registers to avoid having to spill the CSR.

This sorting characteristic occurs only as a tie breaker to the Weight
calculation. This is a good idea since the weight calculation is pretty
complex and I'm sure it is a pretty stable metric. I think its pretty
reasonable to agree that whether a register is callee-saved or not is a
better tie breaker than register ID. I think this is evident by the test
diff, since the changes all seem to have no impact or improve the
register allocation.
2025-04-14 09:58:46 -04:00
Akshat Oke
e29f986838
[CodeGen][NPM] Port RemoveLoadsIntoFakeUses to NPM (#130068) 2025-04-14 12:58:03 +05:30
Simon Pilgrim
150e7b14f9 [X86] Add test coverage for #134602 2025-04-14 07:50:00 +01:00
Simon Pilgrim
07b439605a
[X86] combineConcatVectorOps - require free concatenation of at least one operand of UNPCKL\H (#135366)
Stop just replacing 2*UNPCK+INSERT_SUBVECTOR with 2*INSERT_SUBVECTOR+UNPCK

Currently limited to sub-64-bit element cases until we've accounted for the remaining regressions from some build_vector style patterns.
2025-04-11 15:41:09 +01:00
Simon Pilgrim
937cfdc7be
[X86] combineGatherScatter - split non-constant (add v, (splat b)) indices patterns and add the splat into the (scalar) base address (#135201)
We already did this for constant cases, this patch generalizes the existing fold to attempt to extract the splat from either operand of a ADD node for the gather/scatter index value

This cleanup should also make it easier to add support for splitting vXi32 indices on x86_64 64-bit pointer targets in the future as well.

Noticed while reviewing #134979
2025-04-11 10:53:53 +01:00
Simon Pilgrim
750d009bb2
[X86] combineConcatVectorOps - add concatenation handling for BITCAST nodes (#133913)
These nodes are effectively free, so we should only concatenate if the
inner nodes will concatenate together.

This also exposed a regression in canonicalizeShuffleWithOp that failed
to realize it could potentially merge shuffles with a CONCAT_VECTORS
node.
2025-04-10 17:44:39 +01:00
zhijian lin
378ac572ac
Reland "[SelectionDAG] Introducing a new ISD::POISON SDNode to represent the poison value in the IR." (#135056)
A new ISD::POISON SDNode is introduced to represent the poison value in
the IR, replacing the previous use of ISD::UNDEF
2025-04-10 11:29:14 -04:00
Simon Pilgrim
9ff27d5fc2 [X86] add additional avgfloor 'add(shift(x,1),shift(y,1),and(x,y,1))' patterns
Add mixture of pattern combos to match against the SDPatternMatch::m_Reassociatable* matchers
2025-04-10 13:32:54 +01:00