4297 Commits

Author SHA1 Message Date
Gabriel Baraldi
5e0a06b34d
Move ExpandMemCmp and MergeIcmp to the middle end (#77370)
Moving these into the middle-end pipeline will allow for additional
optimization of the expansion result, such as CSE of redundant loads
(c.f. https://godbolt.org/z/bEna4Md9r). For now, we conservatively place
the passes at the end of the middle-end pipeline, so we mostly don't
benefit from additional optimizations yet. The pipeline position will be
moved in a future change.

This builds on work done by legrosbuffle in
https://reviews.llvm.org/D60318.

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 09:57:00 +02:00
Lei Huang
697ff31fc7
[PowerPC] Enhance vec_rl() to generate xvrlw (#189023)
Enhance existing builtin vec_rl() to generate the new xvrlw, VSX version
of vrlw, for cpu=future.
2026-04-01 11:38:43 -04:00
LU-JOHN
c245d764b8
[CodeGen] Do not remove IMPLICIT_DEF unless all uses have undef flag added (#188133)
Do not remove IMPLICIT_DEF of a physreg unless all uses have an undef
flag added. Previously, only the first use instruction had undef flags
added. This will cause a failure in machine instruction verification.
Multi-instruction uses tested in AMDGPU/multi-use-implicit-def.mir and
X86/multi-use-implicit-def.mir.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
2026-04-01 10:11:42 -05:00
natanelh-mobileye
46dd9d6f52
[SDAG][abd] Combine abd of small types (#181538)
It is beneficial to combine abd of illegal, small types (types that get promoted to wider scalar size).
2026-03-31 13:40:51 +00:00
Luke Lau
598f3535fa
[SelectionDAG] Expand CTTZ_ELTS[_ZERO_POISON] and handle legalization (#188691)
This is a second attempt at "[SelectionDAG] Expand
CTTZ_ELTS[_ZERO_POISON] and handle splitting" (#188220)

That PR had to be reverted in 7d39664a6ae8daaf186b65578492244d96a50bf2
because we had crashes on AMDGPU since we didn't have scalarization
support, and other crashes on PowerPC because we didn't handle the case
when a vector needed widened. Tests for these are added in
AMDGPU/cttz-elts.ll, RISCV/rvv/cttz-elts-scalarize.ll and
PowerPC/cttz-elts.ll.

The former crash has been fixed by adding
DAGTypeLegalizer::ScalarizeVecOp_CTTZ_ELTS.

The second crash has been fixed by reworking
TargetLowering::expandCttzElts. The expansion for CTTZ_ELTS is nearly
identical to VECTOR_FIND_LAST_ACTIVE, except it uses a reverse step
vector and subtracts the result from VF. The easiest way to fix these
crashes without introducing regressions is to reuse the
VECTOR_FIND_LAST_ACTIVE expansion which already handles the case where
the vector needs widened.

This means that the node now needs to take in a boolean vector argument
and uses VSELECT instead of an AND to zero out inactive lanes, so the op
promotion code has also been shared.
2026-03-31 07:25:57 +00:00
Mingjie Xu
227edfb2f4
[CodeGenPrepare][NFC] Reland: Update the dominator tree instead of rebuilding it (#179040)
The original differential revision is https://reviews.llvm.org/D153638

Reverted in
f5b5a30858
because of causing a clang crash.

This patch relands it with the crash fixed. Call `DTU->flush()` in each
iteration of `while (MadeChange)`
loop, flush all awaiting BasicBlocks deletion, and prevent iterator
invalidation.
2026-03-31 09:01:11 +08:00
Folkert de Vries
73cddef788
optimize is_finite assembly (#169402)
Fixes https://github.com/llvm/llvm-project/issues/169270

Changes the implementation of `is_finite` to emit fewer instructions,
e.g.

X86_64

```asm
old: # 18 bytes
        movd    %xmm0, %eax
        andl    $2147483647, %eax
        cmpl    $2139095040, %eax
        setl    %al
        retq
new: # 15 bytes
        movd    %xmm0, %eax
        addl    %eax, %eax
        cmpl    $-16777216, %eax
        setb    %al
        retq
```

Aarch64

```asm
old:
        fmov    w9, s0
        mov     w8, #2139095040
        and     w9, w9, #0x7fffffff
        cmp     w9, w8
        cset    w0, lt
        ret
new:
        fmov    w8, s0
        ubfx    w8, w8, #23, #8
        cmp     w8, #255
        cset    w0, lo
        ret
```

See the issue for more information.
2026-03-29 14:28:07 +00:00
Marina Taylor
55322f2d43
[ObjCARC] Run ObjCARCContract before PreISelIntrinsicLowering (#184149)
74e4694 moved ObjCARCContract from running before the codegen pipeline
into addISelPrepare(), which runs after PreISelIntrinsicLowering.

This broke ObjCARCContract's retainRV-to-claimRV optimization because
ObjCARCContract identifies ARC calls via intrinsics, not their lowered
counterparts.

This patch restores the pre-74e4694 ordering by moving ObjCARCContract
to addISelPasses.

The IntrinsicInst.cpp change looks extraneous but is required here:
ObjCARCContract may now rewrite the bundle operand from retainRV to
claimRV. When PreISelIntrinsicLowering then encounters this new
intrinsic use, lowerObjCCall asserts mayLowerToFunctionCall.

Assisted-by: claude

rdar://137997453
2026-03-27 15:37:47 +00:00
134ARG
331c1c0b84
[ValueTracking] Refine SIToFP/UIToFP FPClass inference with KnownBits (#187185)
This patch propagates the KnownBits of the source integer to improve
floating-point class inference for sitofp and uitofp instructions.

Specifically,
1. The result is never -0.0.
2. The result is not +0.0 if the source integer is known non-zero.
3. The result is not negative if the source integer is known
non-negative (or for uitofp).
4. The result is not Infinity if the largest possible integer magnitude
fits within the target FP type's exponent limits.

alive2 results for added testcases:
testcase 1: https://alive2.llvm.org/ce/z/eM34LB
testcase 2: https://alive2.llvm.org/ce/z/ext7XF 
testcase 3: https://alive2.llvm.org/ce/z/g8yb6q
testcase 4: https://alive2.llvm.org/ce/z/cyFYRy
testcase 5: https://alive2.llvm.org/ce/z/LePFrm

alive2 for updated testcase in binop-itofp:

updated 1: https://alive2.llvm.org/ce/z/KPQ5bZ
udpated 2: https://alive2.llvm.org/ce/z/bGf43t
updated 3: https://alive2.llvm.org/ce/z/YKnCwU
updaetd 4: https://alive2.llvm.org/ce/z/mqKaq-
updated 5: https://alive2.llvm.org/ce/z/jYSAB5

Fix #186952
2026-03-26 18:14:58 +01:00
Nikita Popov
5eab5fd69a
[PowerPC] Fix memory attributes on larx/stcx (#186395)
Modelling these as ll/sc style instructions as `memory(argmem: read)`
and `memory(argmem: write)` is not correct. The read/write additional
microarchitectural state, so they should at least read/write
`inaccessiblemem` as well.

Treating these are purely reading/writing can result in miscompilations
-- this came up in https://github.com/llvm/llvm-project/pull/169379
where the stcx ended up being hoisted outside the atomicrmw loop.
2026-03-26 15:25:09 +01:00
Simon Pilgrim
bec0aee0ba
[PowerPC] Use add_like pattern for ADDI/ADDIS add-immediate matching (#187326)
Allow or_disjoint nodes with sext-immediates to make use of the ADD instructions instead of OR (which use zext-immediates) to potentially allow further folding
2026-03-25 12:33:53 +00:00
Xinlong Chen
b670265b58
[DAG] ComputeKnownBits - set low bit to zero for ADD(X,X) (#186461)
ADD(X,X) is equivalent to SHL(X,1), so bit[0] is always zero.

This allows downstream folds like `and(add(x,x), 1) -> 0`.

Fixes #186091
2026-03-23 16:10:36 +00:00
Xinlong Chen
af9bc2ab1e
[PowerPC] Skip tryBitfieldInsert when an operand is a constant (#187663)
When either operand of an OR node is a constant, bail out of
`tryBitfieldInsert` and let `ORI`/`ORIS`/`ADDI`/`ADDIS` tablegen
patterns handle it.

These patterns produce a single instruction without the tied-register
constraint that RLWIMI requires, avoiding unnecessary LI + RLWIMI
sequences.

Prep work to help with regressions identified on
https://github.com/llvm/llvm-project/pull/186461
2026-03-23 13:55:09 +00:00
Shivam Gupta
796b218edd
[LegalizeTypes] Expand UDIV/UREM by constant via chunk summation (#146238)
This patch improves the lowering of 128-bit unsigned division and
remainder by constants (UDIV/UREM) by avoiding a fallback to libcall
(__udivti3/uremti3) for specific divisors.

When a divisor D satisfies the condition (1 << ChunkWidth) % D == 1, the
128-bit value is split into fixed-width chunks (e.g., 30-bit) and summed
before applying a smaller UDIV/UREM. This transformation is based on the
"remainder by summing digits" trick described in Hacker’s Delight.

This fixes #137514 for some constants.
2026-03-19 17:58:54 +05:30
Nikita Popov
7404a5dbe0
[PowerPC] Preserve load output chain in vcmpequb combine (#187010)
Replace uses of the old load output chain with the new load output
chain. A plain replacement here is fine because the transform verifies
the load is one-use.

Fixes https://github.com/llvm/llvm-project/issues/186549.
2026-03-18 11:38:51 +01:00
Himadhith
76d5704633
[NFC][PowerPC] Update check lines to include power 9 label (#187193)
The current check lines do not provide a clear distinction between
`power 9` and `power 8` as power 8 label was introduced recently through
#181776. Added `power-9` label to the RUN lines to make it more readable
and understandable.

Co-authored-by: himadhith <himadhith.v@ibm.com>
2026-03-18 13:12:47 +05:30
Himadhith
709ef15d74
[NFC][PowerPC] Pre-commit to optimize bswap64 builtin for power8 (#181776)
The current codegen (for power 8 targets specifically) does not make use
of the parallelism and does most of the operations sequentially.
This will be optimized in a future patch which will follow this NFC PR.
It will enhance the performance and also save us instructions.

---------

Co-authored-by: himadhith <himadhith.v@ibm.com>
2026-03-18 10:09:18 +05:30
Lei Huang
f489565643
[PowerPC] Use lxvp/stxvp for mcpu=future v256i1 types (#184447)
For `-mcpu=future`, add patterns to use paired vector instructions
(lxvp/lxvpx/stxvp/stxvpx)
for v256i1 operations instead of splitting into two separate vector
operations.

Assistend by AI.
2026-03-16 11:20:32 -04:00
Alexis Engelke
211279d11c
[CodeGenPrepare][NFC] Get BPI/BFI from pass/analysis manager (#186651)
BranchProbabilityInfo will compute it's own dominator tree and
post-dominator tree if none is specified; avoid this by using the
analysis manager/pass manager to get the analysis, which will reuse the
previously computed DomTree.
2026-03-15 17:41:22 +01:00
paperchalice
26ac669101
[LLVM] Remove "no-nans-fp-math" attribute support (#186285)
Now all `NoNaNsFPMath` uses have been removed, remove this attribute.
2026-03-13 09:29:28 +00:00
Lei Huang
bf85f52fbd
[PowerPC] Update dmr builtin names (#183160)
Remove `_mma` from the following built-ins as they are not related to
MMA:

* __builtin_mma_dmsetdmrz
* __builtin_mma_dmmr
* __builtin_mma_dmxor
* __builtin_mma_build_dmr
* __builtin_mma_disassemble_dmr

AI Assisted.
2026-03-12 12:54:07 -04:00
Fangrui Song
c889454f1d
[MC] Rename PrivateGlobalPrefix to InternalSymbolPrefix. NFC (#185164)
The "private global" terminology, likely came from
llvm/lib/IR/Mangler.cpp, is misleading: "private" is the opposite of
"global", and these prefixed symbols are not global in the object file
format sense (e.g. ELF has STB_GLOBAL while these symbols are always
STB_LOCAL). The term "internal symbol" better describes their purpose:
symbols for internal use by compilers and assemblers, not meant to be
visible externally.

This rename is a step toward adopting the "internal symbol prefix"
terminology agreed with GNU as
(https://sourceware.org/pipermail/binutils/2026-March/148448.html).
2026-03-10 01:03:27 -07:00
paperchalice
a266f60ddf
[SelectionDAG] Remove NoNaNsFPMath uses (#183448)
This pr removes the rest uses in LLVMCodeGen.
2026-03-10 14:06:42 +08:00
Maryam Moghadas
f7a48fbefa
[PowerPC] Add AMO load with Compare and Swap Not Equal (#178061)
This commit adds support for lwat/ldat atomic operations with function
code 16 (Compare and Swap Not Equal) via 4 clang builtins:

__builtin_amo_lwat_csne for 32-bit unsigned operations 
__builtin_amo_ldat_csne for 64-bit unsigned operations
 __builtin_amo_lwat_csne_s for 32-bit signed operations 
__builtin_amo_ldat_csne_s for 64-bit signed operations
2026-03-09 13:50:08 -04:00
Sean Fertile
c2db12daa1
[AIX] Sort relocations in XCOFF object writer. (#180807)
Some relocations (like R_REF) are emitted to an offset 0 within the CSECT. If other relocations have already been emitted then the relocations are not in increasing order and the linker will emit an error. Sort the relocations before emitting to fix the problem.
2026-03-04 12:25:11 -05:00
Craig Topper
98c46261d9
[TargetLowering][PowerPC] Don't unroll vector CLMUL when MUL is not supported. (#184238)
We can use the bittest lowering instead.
2026-03-03 17:25:54 -08:00
Craig Topper
d20395cfa3
[LegalizeVectorOps][RISCV][PowerPC][AArch64][X86] Enable the clmul/clmulr/clmulh expansion code. (#184257)
These opcodes weren't added to the master switch statement that
determines if they should be considered vector ops.
2026-03-02 21:50:40 -08:00
zhijian lin
67a51ea34d
[NFC][POWER] add Pre-Commit test case for Inefficient std::bit_floor(x) (#183363)
add a pre-commit test case for Inefficient asm of std::bit_floor(x) for
powerpc.
2026-02-27 13:24:34 -05:00
paperchalice
dc66b51a70
[PowerPC] Remove NoNaNsFPMath uses (#183449) 2026-02-27 04:45:54 +08:00
zhijian lin
da851db4bb
[PowerPC] using milicode call for memccpy instead of lib call (#182563)
AIX has "millicode" routines, which are functions loaded at boot time
into fixed addresses in kernel memory. This allows them to be customized
for the processor. The __memccpy routine is a millicode implementation;
we use millicode for the memccpy function instead of a library call to
improve performance

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2026-02-26 13:09:22 -05:00
paperchalice
d46a400767
[PowerPC] Remove NoSignedZerosFPMath uses (#180087)
Users should use `nsz` flag only.
2026-02-26 11:13:42 +00:00
Simon Pilgrim
efcf64e898
[DAG] visitOR - attempt to fold (or buildvector(), buildvector()) -> buildvector() (#183032)
See if we can fold all elements of an OR of buildvectors: OR(-1,X) ->
-1, OR(0,X) -> X, etc.
2026-02-25 10:03:15 +00:00
zhijian lin
ce2a9cbf15
[PowerPC] Fix inefficient code for __builtin_ppc_test_data_class (#181420)
emit a more efficient asm for  llvm.ppc.test.data.class.f64
2026-02-23 10:44:38 -05:00
Jay Foad
594e889b7f
[PowerPC] Fix duplicate RUN lines in tests (#182275) 2026-02-23 10:32:01 +00:00
Peter Collingbourne
ebe9c6eabd
CodeGen: Introduce MachineFunction::getPreferredAlignment().
MachineFunction can now be queried for the preferred alignment which
comes from the function attributes (optsize, minsize, prefalign) and
TargetLowering.

Part of this RFC:
https://discourse.llvm.org/t/rfc-enhancing-function-alignment-attributes/88019

Reviewers: vitalybuka, nikic, efriedma-quic, MaskRay

Reviewed By: vitalybuka

Pull Request: https://github.com/llvm/llvm-project/pull/158368
2026-02-20 11:16:45 -08:00
Simon Pilgrim
9b3470d56f
[DAG] expandCLMUL - unroll vector clmul if vector multiplies are not supported (#182041)
Fixes powerpc cases reported on #182039

I'm hoping #177566 can be adapted to improve upon this.
2026-02-18 19:03:36 +00:00
Bill Wendling
9a0d65cdfd
[NFC][CodeGen] Rename CallBrPrepare pass to InlineAsmPrepare (#181547)
This is an NFC change to make room for a more generalized "prepare" pass
for inline assembly beyond CallBrInsts. In particular, changing how we
generate code for inline assembly with "rm" constraints.
2026-02-17 15:37:35 -08:00
Nikita Popov
c4721872af Revert "[Clang][inlineasm] Add special support for "rm" output constraints (#92040)"
This change landed without approval.

This reverts commit 45e666a8531c1148bdb170b9a120f99e1500c427.
This reverts commit a636dd4c37f12594275de2fe180ca35bc04d76ea.
2026-02-14 15:59:04 +01:00
Bill Wendling
45e666a853
[Clang][inlineasm] Add special support for "rm" output constraints (#92040)
Clang isn't able to support multiple constraints on inputs and outputs,
like "rm". Instead, it picks the "safest" one to use, i.e. the memory
constraint for "rm". This leads to obviously horrible code:

  asm __volatile__ ("pushf\n\t"
                    "popq %0"
                    : "=rm" (x));

is compiled to:

        pushf
	popq -8(%rsp)
	movq	-8(%rsp), %rax

It gets worse when inlined into other functions, because it may
introduce
a stack where none is needed.

With this change, Clang now generates IR for the more optimistic choice
("r"). All but the fast register allocator are able to fold registers if
it turns out that register pressure is too high.

This leaves the fast register allocator. The fast register allocator, as
the name suggests, is built for execution speed, not code quality. Thus,
we add special processing to convert the "optimistic" IR into the
"conservative" choice (again at the IR level), which we know it can
handle.

We focus on "rm" for the initial commit, but that can be expanded in the
future for other constraints where Clang generates ++ungood code (like
"g").

Fixes: https://github.com/llvm/llvm-project/issues/20571
2026-02-14 05:02:24 -08:00
zhijian lin
343103a680
[NFC] Pre-Commit test case for __builtin_ppc_test_data_class (#181181)
add a pre-commit test case for __builtin_ppc_test_data_class
2026-02-13 15:07:01 -05:00
paperchalice
c53acf0443
[SelectionDAGBuilder] Remove NoNaNsFPMath uses (#169904)
Replaced by checking fast-math flags or value tracking results.
2026-02-09 09:48:07 +08:00
Wael Yehia
13c9276daa [AIX] fix aix-ifunc-toc-restore-query-neg.ll (#153049) 2026-02-05 19:00:23 +00:00
Matt Arsenault
2502e3b7ba
IR: Promote "denormal-fp-math" to a first class attribute (#174293)
Convert "denormal-fp-math" and "denormal-fp-math-f32" into a first
class denormal_fpenv attribute. Previously the query for the effective
denormal mode involved two string attribute queries with parsing. I'm
introducing more uses of this, so it makes sense to convert this
to a more efficient encoding. The old representation was also awkward
since it was split across two separate attributes. The new encoding
just stores the default and float modes as bitfields, largely avoiding
the need to consider if the other mode is set.

The syntax in the common cases looks like this:
  `denormal_fpenv(preservesign,preservesign)`
  `denormal_fpenv(float: preservesign,preservesign)`
  `denormal_fpenv(dynamic,dynamic float: preservesign,preservesign)`

I wasn't sure about reusing the float type name instead of adding a
new keyword. It's parsed as a type but only accepts float. I'm also
debating switching the name to subnormal to match the current
preferred IEEE terminology (also used by nofpclass and other
contexts).

This has a behavior change when using the command flag debug
options to set the denormal mode. The behavior of the flag
ignored functions with an explicit attribute set, per
the default and f32 version. Now that these are one attribute,
the flag logic can't distinguish which of the two components
were explicitly set on the function. Only one test appeared to
rely on this behavior, so I just avoided using the flags in it.

This also does not perform all the code cleanups this enables.
In particular the attributor handling could be cleaned up.

I also guessed at how to support this in MLIR. I followed
MemoryEffects as a reference; it appears bitfields are expanded
into arguments to attributes, so the representation there is
a bit uglier with the 2 2-element fields flattened into 4 arguments.
2026-02-05 13:31:26 +00:00
Jay Foad
7ea33e6848
[CodeGen] Remove unused first operand of SUBREG_TO_REG (#179690)
The first input operand of SUBREG_TO_REG was an immediate that most
targets set to 0. In practice it had no effect on codegen. Remove it.
2026-02-04 17:35:21 +00:00
Nikita Popov
90c632ab48
[PowerPC] Only set QualName symbol on first section switch (#179253)
We were setting it every time when switching to the section. This caused
problems when the debug_aranges emission performed a switch at the end
of the section, resulting in symbols incorrectly pointing to the end
instead of the start of the function.
2026-02-04 10:21:07 +01:00
Wael Yehia
e2061328a8 [AIX] fix aix-ifunc-toc-restore-query.ll (#153049) 2026-02-04 03:53:59 +00:00
Wael Yehia
cc5859671d [AIX] disable aix-ifunc-toc-restore-query-neg.ll on all platforms except ppc for now (#153049) 2026-02-04 03:44:28 +00:00
paperchalice
1ffe78811e
[PowerPC] Remove NoInfsFPMath uses (#163029)
Only `ninf` should be used.
This is the PowerPC part.
2026-02-04 00:35:15 +00:00
Wael Yehia
e1f69ee8e8
[AIX] Implement the ifunc attribute. (#153049)
Currently, the AIX linker and loader do not provide a mechanism to
implement ifuncs similar to GNU_ifunc on ELF Linux.
On AIX, we will lower `__attribute__((ifunc("resolver"))` to the llvm
`ifunc` as other platforms do. The llvm `ifunc` in turn will get lowered
at late stages of the optimization pipeline to an AIX-specific
implementation. No special linkage or relocations are needed when
generating assembly/object output.

On AIX, a function `foo` has two symbols associated with it: a function
descriptor (`foo`) residing in the `.data` section, and an entry point
(`.foo`) residing in the `.text` section. The first field of the
descriptor is the address of the entry point. Typically, the address
field in the descriptor is initialized once: statically, at load time
(?), or at runtime if runtime linking is enabled.

Here we would like to use the address field in the descriptor to
implement the `ifunc` semantics. Specifically, the ifunc function will
become a stub that jumps to the entry point in the address field. A
constructor function is linked into every linkage module. The
constructor walks an array of `{descriptor, resolver}` pairs, calling
the resolver and saving the result in the address field in the
descriptor (thus setting `foo`'s descriptor to point to the resolved
version early during program runtime).

Known limitations:
- Due to bug #161576, which affects object generation path, you will
need either `-ffunction-sections` or `-fno-integrated-as` to generate a
correct/linkable object file.
- aliases to ifuncs are not supported, a testcase has been added and
marked XFAIL. I'm planning to address in a follow-up PR because it's not
important enough, IMHO, for this PR
- dead ifuncs in a CU that contains at least one live ifunc, will result
in all ifuncs being kept by the linker. The fix for this is common with
a similar problem we have with PGO. PR #159435 is trying to provide a
mechanism that will allow the ifunc and PGO implementations to avoid the
dead code retention at the link step.
- the resolver must return a function that is in the same DSO as the
ifunc; the compiler will try to detect if this condition is violated and
report it, but it cannot detect it in general. To be safe, all candidate
functions (returned by a particular resolver) must either be static or
have hidden/protected visibility. This is so that the ifunc stub doesn't
have to save and restore the TOC register r2. In future work, this case
will be supported and the requirement will be lifted.

---------

Co-authored-by: Wael Yehia <wyehia@ca.ibm.com>
2026-02-03 14:15:16 -05:00
zhijian lin
dc520ea4af
[PowerPC] using milicode call for strcmp instead of lib call (#177009)
1. AIX has "millicode" routines, which are functions loaded at boot time
into fixed addresses in kernel memory. This allows them to be customized
for the processor. The __strcmp routine is a millicode implementation;
we use millicode for the strcmp function instead of a library call to
improve performance.
2026-02-02 09:34:53 -05:00