195 Commits

Author SHA1 Message Date
Weining Lu
47601815ec [LoongArch] Define ual feature and override allowsMisalignedMemoryAccesses
Some CPUs do not allow memory accesses to be unaligned, e.g. 2k1000la
who uses the la264 core on which misaligned access will trigger an
exception.

In this patch, a backend feature called `ual` is defined to decribe
whether the CPU supports unaligned memroy accesses. And this feature
can be toggled by clang options `-m[no-]unaligned-access` or the
aliases `-m[no-]strict-align`. When this feature is on,
`allowsMisalignedMemoryAccesses` sets the speed number to 1 and returns
true that allows the codegen to generate unaligned memory access insns.

Clang options `-m[no-]unaligned-access` are moved from `m_arm_Features_Group`
to `m_Group` because now more than one targets use them. And a test
is added to show that they remain unused on a target that does not
support them. In addition, to keep compatible with gcc, a new alias
`-mno-strict-align` is added which is equal to `-munaligned-access`.

The feature name `ual` is consistent with linux kernel [1] and the
output of `lscpu` or `/proc/cpuinfo` [2].

There is an `LLT` variant of `allowsMisalignedMemoryAccesses`, but
seems that curently it is only used in GlobalISel which LoongArch
doesn't support yet. So this variant is not implemented in this patch.

[1]: https://github.com/torvalds/linux/blob/master/arch/loongarch/include/asm/cpu.h#L77
[2]: https://github.com/torvalds/linux/blob/master/arch/loongarch/kernel/proc.c#L75

Reviewed By: xen0n

Differential Revision: https://reviews.llvm.org/D149946
2023-06-07 13:40:58 +08:00
Nikita Popov
f7d1baa414 [KnownBits] Return zero instead of unknown for always poison shifts
For always poison shifts, any KnownBits return value is valid.
Currently we return unknown, but returning zero is generally more
profitable. We had some code in ValueTracking that tried to do this,
but was actually dead code.

Differential Revision: https://reviews.llvm.org/D150648
2023-05-23 14:41:22 +02:00
Tobias Hieta
f84bac329b
[NFC][Py Reformat] Reformat lit.local.cfg python files in llvm
This is a follow-up to b71edfaa4ec3c998aadb35255ce2f60bba2940b0
since I forgot the lit.local.cfg files in that one.

Reformatting is done with `black`.

If you end up having problems merging this commit because you
have made changes to a python file, the best way to handle that
is to run git checkout --ours <yourfile> and then reformat it
with black.

If you run into any problems, post to discourse about it and
we will try to help.

RFC Thread below:

https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Reviewed By: barannikov88, kwk

Differential Revision: https://reviews.llvm.org/D150762
2023-05-17 17:03:15 +02:00
Weining Lu
2cf0314029 [LoongArch] Move lp64s out of the unimplemented calling conv list
lp64s is same as lp64d execpt that floating point arguments and return
values are always passed via GPRs or stack which means `UseGPRForFloat`
is always `true` in `CC_LoongArch` for lp64s.

One motivation of this change is to build linux which uses
`-msoft-float` and `-mabi=lp64s` [1].

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/loongarch/Makefile?h=v6.4-rc1#n49

Reviewed By: xen0n, hev

Differential Revision: https://reviews.llvm.org/D150417
2023-05-16 21:09:31 +08:00
Zequan Wu
3977b77a6b [CodeGen] Fix nomerge attribute not working in tail calls.
In D79537, `nomerge` was made to only apply to non-tail calls. This fixes it by also applying it to tail calls.

For ARM, I only made the new MI to inherit the flag under `TCRETURNdi` and `TCRETURNri`, because that's the place tail calls got replaced. Not sure if there's any other place needed.

Fixes #61545.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D146749
2023-05-10 14:25:11 -04:00
Weining Lu
161716a713 [LoongArch] Support fcc* (condition flag) registers in inlineasm clobbers
Differential Revision: https://reviews.llvm.org/D150089
2023-05-09 14:55:50 +08:00
Fangrui Song
d14460d00e [AsmPrinter] Fix placement of function entry comments
The placement is currently wrong in the presence of function entry related
instrumentations (prefixdata, -fpatchable-function-entry=, -fsanitize=kcfi,
etc).
2023-04-18 15:01:36 -07:00
sgokhale
bb5befefc6 Revert "[CodeGen][ShrinkWrap] Split restore point"
This reverts commit 5f0bccc3d1a74111458c71f009817c9995f4bf83.

An issue has been reported here: https://github.com/ClangBuiltLinux/linux/issues/1833
2023-04-13 10:52:28 +05:30
Ben Shi
89e53b2aed [LoongArch] Optimize multiplication with immediates
Optimize (mul x, imm) to (ADD (SLLI x, s0), (SLLI x, s1)) or
(SUB (SLLI x, s0), (SLLI x, s1)) if possible.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D147692
2023-04-12 11:52:56 +08:00
Ben Shi
91212e0283 [LoongArch][NFC] Add tests of multiplication with immediates
Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D147691
2023-04-12 11:52:55 +08:00
sgokhale
5f0bccc3d1 [CodeGen][ShrinkWrap] Split restore point
This patch splits a restore point to allow it to only post-dominate blocks reachable by use
or def of CSRs(Callee Saved Registers)/FI(Frame Index).

Benchmarking this on SPEC2017, this gives around 4% improvement on povray and no significant change
for others.

Co-authored-by: junbuml

Differential Revision: https://reviews.llvm.org/D42600
2023-04-11 11:58:50 +05:30
Ben Shi
c8a2301555 [LoongArch] Optimize multiplication with immediates
Optimize "(mul r, c)" to "(SLLI (ALSL r, r, i0), i1)", in which
"c = (1 + (1 << i0)) << i1".

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D147428
2023-04-07 08:46:49 +08:00
Ben Shi
d9e884fe74 [LoongArch][NFC] Add tests of multiplication with immediates (for D147428)
Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D147427
2023-04-07 08:46:49 +08:00
Zequan Wu
321d02cc6b [NFC] Update CodeGen/*/nomerge.ll tests with utils/update_llc_test_checks.py.
Precommit this patch for better diff view on D146749.

Reviewed By: nemanjai

Differential Revision: https://reviews.llvm.org/D147454
2023-04-03 19:52:39 -04:00
Ben Shi
a703a9ae33 [LoongArch] Optimize 32-bit addition with ALSL_W on LoongArch64
Optimize 32-bit 'x+(y<<i)' to (ALSL_W y, x, i) on LoongArch64,
in which 'i' is in [1, 2, 3, 4].

Reviewed By: xen0n, SixWeining

Differential Revision: https://reviews.llvm.org/D147423
2023-04-03 14:47:23 +08:00
Ben Shi
96dcd8cb94 [LoongArch] Optimize bitwise and with immediates
Optimize bitfield extractions retaining bit positions
from 'lu12i + addi + and' to 'bstrpick + slli'.

Reviewed By: xen0n, SixWeining

Differential Revision: https://reviews.llvm.org/D147368
2023-04-03 12:10:07 +08:00
Ben Shi
d726c75388 [LoongArch][NFC] Add tests of bitwise and with immediates (for D147368)
Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D147367
2023-04-03 12:09:30 +08:00
Ben Shi
d88ffb0dc5 [LoongArch] Optimize multiplication with immediates
Optimize 'x * c' to (ALSL x, (SLLI x, i0), i1) in which
c = (1 << i0) | (1 << i1).

Reviewed By: xen0n, SixWeining

Differential Revision: https://reviews.llvm.org/D147406
2023-04-03 11:26:25 +08:00
Ben Shi
6e19f5b04f [LoongArch][NFC] Add tests of multiplication with immediates
Reviewed By: xen0n, SixWeining

Differential Revision: https://reviews.llvm.org/D147405
2023-04-03 11:25:11 +08:00
Ben Shi
4f8c1957de [LoongArch] Optimize multiplication with immediates
Optimize multiplication with some specific immediates to
a pair of `alsl`.

Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D147400
2023-04-02 22:12:49 +08:00
Ben Shi
6d0e9c269c [LoongArch][NFC] Add tests of multiplication with immediates
Reviewed By: xen0n, SixWeining

Differential Revision: https://reviews.llvm.org/D147398
2023-04-02 22:12:48 +08:00
Ben Shi
734c213004 [LoongArch] Optimize multiplication with immediates
Optimize multiplication with some specific immediates to
a pair of `alsl`.

Reviewed By: xen0n, SixWeining

Differential Revision: https://reviews.llvm.org/D147305
2023-04-01 18:11:50 +08:00
Ben Shi
918209bf85 [LoongArch][NFC] Add tests of multiplication with immediates (for D147305)
Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D147304
2023-04-01 18:11:50 +08:00
Ben Shi
ec37a35a8c [LoongArch] Optimize additions with immediates
Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D147222
2023-03-31 14:47:19 +08:00
Ben Shi
777318df1c [LoongArch][NFC] Add tests of additions with immediates (for D147222)
Reviewed By: SixWeining, xen0n, XiaodongLoong

Differential Revision: https://reviews.llvm.org/D147221
2023-03-31 14:47:19 +08:00
WANG Xuerui
19e2ebbf45 [LoongArch] Emit bytepick for picking from concatenation of two values
It seems the ISA manual's pseudo-code description for the
`BYTEPICK.[WD]` instructions is inaccurate; the behavior described here
should be correct though. The instructions' names are misleading too
(they pick full GRLen-wide words instead of bytes; they just index by
bytes) but let's stick to the official names for now.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D143880
2023-03-16 15:07:06 +08:00
WANG Xuerui
ff475a0dd9 [LoongArch] Add baseline tests for bytepick codegen. NFC
Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D143879
2023-03-16 15:07:06 +08:00
WANG Xuerui
db5dfec9d4 [Clang][LoongArch] Implement patchable function entry
Similar to D98610 for RISCV.

This is going to be required by the upcoming Linux/LoongArch
[[ https://git.kernel.org/linus/4733f09d88074 | support for dynamic ftrace ]].

Reviewed By: SixWeining, MaskRay

Differential Revision: https://reviews.llvm.org/D141785
2023-03-16 09:33:58 +08:00
Nick Desaulniers
a3a84c9e25 [llvm] add CallBrPrepare pass to pipelines
Capstone of
https://discourse.llvm.org/t/rfc-syncing-asm-goto-with-outputs-with-gcc/65453/8

Clang changes are still necessary to enable the use of outputs along
indirect edges of asm goto statements.

Link: https://github.com/llvm/llvm-project/issues/53562

Reviewed By: void

Differential Revision: https://reviews.llvm.org/D140180
2023-02-16 17:58:34 -08:00
WANG Xuerui
2b8cb7d87f [LoongArch] Make use of addu16i.d for adds with suitable immediates
Ideally `addu16i.d` could be paired with `{ld,st}ptr` for faster memory
accesses with 32-bit-aligned offsets (it was designed for this purpose),
but it would require more work and the original use case (GP-relative
accesses) does not exist any more with the current LoongArch psABI.

It could still be used for accelerating additions of certain constants
though, which is what this patch intends to do.

Reviewed By: SixWeining, gonglingqin

Differential Revision: https://reviews.llvm.org/D143710
2023-02-15 09:16:14 +08:00
WANG Xuerui
dcf9c60a53 [LoongArch] Add baseline tests for addu16i.d codegen. NFC
Reviewed By: gonglingqin

Differential Revision: https://reviews.llvm.org/D143846
2023-02-15 09:16:14 +08:00
wanglei
d507c01012 [LoongArch] Convert test to opaque pointer. NFC 2023-02-10 14:56:27 +08:00
gonglingqin
9d8a7f780c [LoongArch] Implement the convertSelectOfConstantsToMath hook
Normally, the ISD::SELECT is lowered to maskeqz + masknez. This is
inefficient when operands are constants that can be computed easily.
To avoid that, override TargetLowering::convertSelectOfConstantsToMath.

Differential Revision: https://reviews.llvm.org/D143621
2023-02-10 14:14:47 +08:00
gonglingqin
e5489f7264 [LoongArch] Add baseline tests for translating the selection of constants into mathematical operations 2023-02-09 09:45:58 +08:00
gonglingqin
653d823ad2 [LoongArch] Merge the 12bit constant address into the offset field of the instruction
There are 12bit offset fields in the ld.[b/h/w/d] and st.[b/h/w/d].
When the constant address is less than 12 bits, the address
calculation is incorporated into the offset field of the instruction.

Differential Revision: https://reviews.llvm.org/D143470
2023-02-08 16:57:11 +08:00
gonglingqin
3042419181 [LoongArch] Add baseline tests for optimizations that merge offsets into instructions 2023-02-06 19:34:42 +08:00
gonglingqin
fd68c7d223 [LoongArch] Override TargetLowering::hasAndNotCompare()
Override hasAndNotCompare() to use more `ANDN` instead of using `AND`
and `NOT`.
This patch enables the following transforms:
(X & Y) == Y ---> (~X & Y) == 0
(X & Y) != Y ---> (~X & Y) != 0.

Differential Revision: https://reviews.llvm.org/D143037
2023-02-03 14:18:50 +08:00
gonglingqin
d6f1c85edb [LoongArch] Implement TargetLowering::isLegalAddressingMode() hook
Use the exact addressing mode information instead of the default to
make better use of offsets in instructions.

Differential Revision: https://reviews.llvm.org/D142874
2023-02-02 14:01:44 +08:00
Weining Lu
4f2fde781f [LoongArch] Honor the --target-abi option when generating e_flags
Reviewed By: xen0n, MaskRay

Differential Revision: https://reviews.llvm.org/D142837
2023-02-02 11:39:32 +08:00
WANG Xuerui
695c81e8a6 [LoongArch] Implement handling of triple-implied ABIs
According to the [[ https://loongson.github.io/LoongArch-Documentation/LoongArch-toolchain-conventions-EN.html | LoongArch Toolchain Conventions ]]
it is possible to specify the ABI modifier (the "D" part of "LP64D")
via the environment field in the target triple. This is needed for
proper support for Debian-style multiarch tuples as well, so add triple
awareness to `LoongArchSubtarget` via addition of
`LoongArchABI::computeTargetABI`. Let the explicit `--target-abi`
argument intuitively take precedence over the triple-implied ABI.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D142685
2023-02-02 11:34:01 +08:00
wanglei
e9d658f72b [LoongArch] Implement isUsedByReturnOnly for tailcall more libcalls
Similar to D131087 for RISCV.

Reviewed By: xen0n

Differential Revision: https://reviews.llvm.org/D142958
2023-02-02 10:12:56 +08:00
wanglei
25ecfbf892 [LoongArch] Enable shrink wrapping when optimize the function
We set it conservatively so that it is turned on when optimization
is enabled, i.e. when the optimization level is not `O0`.

Reviewed By: xen0n

Differential Revision: https://reviews.llvm.org/D142876
2023-02-01 10:07:40 +08:00
gonglingqin
c63ba01e9b [LoongArch] Add baseline test for (X & Y) == Y and (X & Y) != Y
Add baseline testing for upcoming optimization patches according the
guideline https://reviews.llvm.org/rG1d6d1ecca7ec.
2023-01-31 19:45:42 +08:00
Matt Arsenault
778cf5431c IR: Add atomicrmw uinc_wrap and udec_wrap
These are essentially add/sub 1 with a clamping value.

AMDGPU has instructions for these. CUDA/HIP expose these as
atomicInc/atomicDec. Currently we use target intrinsics for these,
but those do no carry the ordering and syncscope. Add these to
atomicrmw so we can carry these and benefit from the regular
legalization processes.
2023-01-24 17:55:11 -04:00
OCHyams
99c12afeb4 [Assignment Tracking] Fix tests for buildbot failure (2)
Follow-up for 4ece50737d5385fb80cfa23f5297d1111f8eed39 (D142027).

Assignment Tracking Analysis now always runs and is skipped internally if
assignment tracking is disabled. Update these tests to expect to see the
pass run.

Buildbot failure: https://lab.llvm.org/buildbot/#/builders/57/builds/24094
2023-01-20 15:58:35 +00:00
wanglei
7fa0a3c923 [LoongArch] Add an option for MCInstPrinter to print numeric reg names
`-loongarch-numeric-reg` for llvm-mc and llc.
`-M numeric` (which matches GNU objdump) for llvm-objdump and llvm-mc.

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D141743
2023-01-19 16:29:22 +08:00
Paul Kirth
557a5bc336 [codegen] Add StackFrameLayoutAnalysisPass
Issue #58168 describes the difficulty diagnosing stack size issues
identified by -Wframe-larger-than. For simple code, its easy to
understand the stack layout and where space is being allocated, but in
more complex programs, where code may be heavily inlined, unrolled, and
have duplicated code paths, it is no longer easy to manually inspect the
source program and understand where stack space can be attributed.

This patch implements a machine function pass that emits remarks with a
textual representation of stack slots, and also outputs any available
debug information to map source variables to those slots.

The new behavior can be used by adding `-Rpass-analysis=stack-frame-layout`
to the compiler invocation. Like other remarks the diagnostic
information can be saved to a file in a machine readable format by
adding -fsave-optimzation-record.

Fixes: #58168

Reviewed By: nickdesaulniers, thegameg

Differential Revision: https://reviews.llvm.org/D135488
2023-01-19 01:51:14 +00:00
Nikita Popov
779df38242 [LoongArch] Convert tests to opaque pointers (NFC) 2023-01-16 10:20:46 +01:00
Paul Kirth
64a553a2b5 Revert "[LoongArch][M68k] Add 'Stack Frame Layout Analysis' to pipeline tests. NFC"
I missed that a forward fix was out when reverting
0a652c540556a118bbd9386ed3ab7fd9e60a9754.

This reverts commit 488bea797e167e6bf5ddab5f7eea78031b575ba0.
2023-01-13 23:07:23 +00:00
Craig Topper
488bea797e [LoongArch][M68k] Add 'Stack Frame Layout Analysis' to pipeline tests. NFC
These targets were missed in D135488.
2023-01-13 14:50:48 -08:00