Commit Graph

56 Commits

Author SHA1 Message Date
gonglingqin 853a1b7236 [LoongArch] Clean up redundant code introduced by conflict resolution. NFC 2022-09-30 16:45:21 +08:00
Fangrui Song 04a65d62a0 Revert D134638 "[Clang][LoongArch] Add inline asm support for constraints k/m/ZB/ZC"
This reverts commit b7baddc755.

Broke CodeGen/X86/callbr-asm-kill.mir
We shall pay attention when adding new constraints.
2022-09-29 00:54:56 -07:00
Weining Lu b7baddc755 [Clang][LoongArch] Add inline asm support for constraints k/m/ZB/ZC
k: A memory operand whose address is formed by a base register and
(optionally scaled) index register.

m: A memory operand whose address is formed by a base register and
offset that is suitable for use in instructions with the same
addressing mode as st.w and ld.w.

ZB: An address that is held in a general-purpose register. The offset
is zero.

ZC: A memory operand whose address is formed by a base register and
offset that is suitable for use in instructions with the same
addressing mode as ll.w and sc.w.

Differential Revision: https://reviews.llvm.org/D134638
2022-09-29 15:02:08 +08:00
gonglingqin dc3c5a78f2 [LoongArch] Add fp_to_sint support for soft floating point
Differential Revision: https://reviews.llvm.org/D134692
2022-09-29 10:25:35 +08:00
WANG Xuerui 3155f6c508 [LoongArch] Expand llvm.stacksave and llvm.stackrestore
As in commit bfb00d4c1c ("[RISCV] Allow lowering of dynamic_stackalloc, stacksave, stackrestore").

Differential Revision: https://reviews.llvm.org/D134435
2022-09-29 09:07:44 +08:00
wanglei 983a0ae5cf [LoongArch] Specify registers used in DWARF exception handling
Defines LoongArch registers for getExceptionPointerRegister() and
getExceptionSelectorRegister().

Differential Revision: https://reviews.llvm.org/D134709
2022-09-28 17:53:16 +08:00
gonglingqin 95d2367647 [LoongArch] Expand FSIN/FCOS/FSINCOS/FPOW/FREM
Differential Revision: https://reviews.llvm.org/D134628
2022-09-28 09:42:41 +08:00
gonglingqin a6d699b55d [LoongArch] Add codegen support for strict_fsetccs and any_fsetcc
Differential Revision: https://reviews.llvm.org/D134274
2022-09-26 13:05:36 +08:00
wanglei 75265c7f49 [LoongArch] Lower BlockAddress/JumpTable
This patch uses a unified interface for lower GlobalAddress ConstantPool
BlockAddress and JumpTable.

This patch allows lowering addresses by using PC-relative addressing
for DSO-local symbols, and accessing the address through the global
offset table for DSO-preemptable symbols.

Remove hardcoded `MininumJumpTableEntries` for test lower JumpTable.

Also updated some test cases using ConstantPool, due to the addition of
relocation information.

Differential Revision: https://reviews.llvm.org/D134431
2022-09-26 10:52:54 +08:00
WANG Xuerui d2ac89b64e [LoongArch] Support fastcc and treat it as ccc
As explained in D68559 the `fastcc` calling convention may be requested
under certain conditions, hence the need for supporting it. But unlike
RISCV we actually treat it exactly like ccc, without actually inventing
any performance hack right here. And CSKY does the same thing.

This is going to fix a few more test cases with native LoongArch builds.

Differential Revision: https://reviews.llvm.org/D134443
2022-09-26 10:15:00 +08:00
WANG Xuerui f89f0990db [LoongArch] Support llvm.thread.pointer
For `__builtin_thread_pointer` to work, among other things.

Similar to D76828 for RISCV.

Differential Revision: https://reviews.llvm.org/D134368
2022-09-26 09:56:42 +08:00
Weining Lu 394f30919a [Clang][LoongArch] Add inline asm support for constraints f/l/I/K
This patch adds support for constraints `f`, `l`, `I`, `K` according
to [1]. The remain constraints (`k`, `m`, `ZB`, `ZC`) will be added
later as they are a little more complex than the others.
f: A floating-point register (if available).
l: A signed 16-bit constant.
I: A signed 12-bit constant (for arithmetic instructions).
K: An unsigned 12-bit constant (for logic instructions).

For now, no need to support register alias (e.g. `$a0`) in llvm as
clang will correctly decode the usage of register name aliases into
their official names. And AFAIK, the not yet upstreamed `rustc` for
LoongArch will always use official register names (e.g. `$r4`).

[1] https://gcc.gnu.org/onlinedocs/gccint/Machine-Constraints.html

Differential Revision: https://reviews.llvm.org/D134157
2022-09-26 08:49:58 +08:00
gonglingqin ac295597a8 [LoongArch] Add codegen support for atomicrmw add/sub/nand/and/or/xor operation
Differential Revision: https://reviews.llvm.org/D133755
2022-09-23 09:32:11 +08:00
gonglingqin 7328ff75ba [LoongArch] Add codegen support for fmaxnum_ieee and fminnum_ieee
Thanks for @xry111's previous bug fixes.
See https://github.com/loongson/llvm-project/pull/1 for more details.

Differential Revision: https://reviews.llvm.org/D133478
2022-09-20 19:22:32 +08:00
Sergei Barannikov c6acb4eb0f [SDAG] Add `getCALLSEQ_END` overload taking `uint64_t`s
All in-tree targets pass pointer-sized ConstantSDNodes to the
method. This overload reduced amount of boilerplate code a bit.  This
also makes getCALLSEQ_END consistent with getCALLSEQ_START, which
already takes uint64_ts.
2022-09-15 14:02:12 -04:00
gonglingqin 3de3439bd7 [LoongArch] Add codegen support for ISD::FMA
Differential Revision: https://reviews.llvm.org/D133281
2022-09-13 10:04:41 +08:00
gonglingqin da8c9521ee [LoongArch] Add codegen support for frint
According to the revised description in `LoongArch Reference Manual v1.02`,
frint.[s/d] does not judge whether floating-point inexact exceptions are
allowed indicated by FCSR, i.e. always executes roundToIntegralExact(x).
What's more, the manual also specifically defines that frint.s/d is only
necessary to be defined in LA64. So ISD::FRINT is legal for LA64.

Differential Revision: https://reviews.llvm.org/D133337
2022-09-09 14:25:34 +08:00
Joe Loser 5e96cea1db [llvm] Use std::size instead of llvm::array_lengthof
LLVM contains a helpful function for getting the size of a C-style
array: `llvm::array_lengthof`. This is useful prior to C++17, but not as
helpful for C++17 or later: `std::size` already has support for C-style
arrays.

Change call sites to use `std::size` instead.

Differential Revision: https://reviews.llvm.org/D133429
2022-09-08 09:01:53 -06:00
gonglingqin d5f7a2182d [LoongArch] Add codegen support for atomicrmw xchg operation on LA32
Depends on D131228

Differential Revision: https://reviews.llvm.org/D131229
2022-09-08 13:57:53 +08:00
gonglingqin b60f801607 [LoongArch] Add codegen support for atomicrmw xchg operation on LA64
In order to avoid the patch being too large, the atomicrmw xchg operation
on LA32 will be added later

Differential Revision: https://reviews.llvm.org/D131228
2022-09-08 13:57:26 +08:00
WANG Xuerui 2dd434c3ee [LoongArch] Support lowering br_jt
Jump tables cannot be generated yet, due to missing support for emitting
local addresses.

Differential Revision: https://reviews.llvm.org/D132653
2022-09-02 17:57:50 +08:00
gonglingqin 6e47ebdcec [LoongArch] Support ISD::BR_CC and branch according to condition flag register
Use bceqz/bcnez instead of movcf2gr + bnez/beqz for branch jumps.

Differential Revision: https://reviews.llvm.org/D132824
2022-09-01 10:43:16 +08:00
Michael Liao dda3878653 [LoongArch] Fix build due to TLI interface changes. NFC.
- isCheapToSpeculateCttz/isCheapToSpeculateCtlz have one type operand
  after https://reviews.llvm.org/D132520
2022-08-24 15:17:38 -04:00
gonglingqin 9046ef6f2f [LoongArch] Implement TargetLowering::hasAndNot() for more optimization chances
Differential Revision: https://reviews.llvm.org/D132282
2022-08-24 17:29:18 +08:00
gonglingqin e9a4b8e397 [LoongArch] Optimize the atomic store with amswap_db.[w/d]
When AtomicOrdering is release or stronger, use
    amswap_db.[w/d] $zero, $a1, $a0
instead of
    dbar 0
    st.[w/d] $a0, $a1, 0

Thanks to @xry111 for the suggestion: https://reviews.llvm.org/D128901#3626635

Differential Revision: https://reviews.llvm.org/D129838
2022-08-23 17:11:57 +08:00
wanglei c2ee21cf3f [LoongArch] Add some fixups and relocations
This patch only add %pc_hi20/%pc_lo12/%plt relocations in order
to be able to generate gnu ld linkable relocation file for the
`hello world` IR :
```
@.str = private unnamed_addr constant [14 x i8] c"Hello world!\0A\00", align 1

define dso_local signext i32 @main() nounwind {
entry:
  %call = call signext i32 (ptr, ...) @printf(ptr noundef @.str)
  ret i32 0
}

declare dso_local signext i32 @printf(ptr noundef, ...)
```

This patch also updates some test cases due to new modifiers introduced.
New test: test/MC/LoongArch/Relocations/relocations.s

Differential Revision: https://reviews.llvm.org/D132108
2022-08-20 11:19:24 +08:00
WANG Xuerui 929d201b7a [LoongArch] Add support for llvm.eh.dwarf.cfa
It's the same as D126181 for RISCV.

Differential Revision: https://reviews.llvm.org/D132012
2022-08-18 13:17:49 +08:00
gonglingqin 9e09c3186e [LoongArch] Add codegen support for ISD::CTPOP, ISD::CTTZ and ISD::CTLZ
Differential Revision: https://reviews.llvm.org/D131550
2022-08-12 14:15:30 +08:00
Weining Lu 40f1f9b357 [LoongArch] Return null SDValue by default in LowerOperation. NFC
Differential Revision: https://reviews.llvm.org/D131546
2022-08-12 12:09:08 +08:00
wanglei c437412fbc [LoongArch] Override TargetLowering::isOffsetFoldingLegal()
This patch disable GlobalAddress+Offset folding.

Differential Revision: https://reviews.llvm.org/D131491
2022-08-11 11:26:54 +08:00
WANG Xuerui 326f7aed38 [LoongArch] Add codegen support for bitreverse
Differential Revision: https://reviews.llvm.org/D131378
2022-08-11 08:55:14 +08:00
gonglingqin cf75ef460c [LoongArch] Add codegen support for ISD::ROTL and ISD::ROTR
Differential Revision: https://reviews.llvm.org/D131231
2022-08-09 19:39:17 +08:00
WANG Xuerui f35cb7ba34 [LoongArch] Add codegen support for bswap
Differential Revision: https://reviews.llvm.org/D131352
2022-08-09 13:42:03 +08:00
wanglei 0c2b738f8f [LoongArch] Support for varargs
This patch ensures the `$fp` always points to the bottom of the vararg
spill region.
Includes support for expand `ISD::DYNAMIC_STACKALLOC`.

Differential Revision: https://reviews.llvm.org/D130250
2022-08-08 14:01:24 +08:00
wanglei 57eb77d411 [LoongArch] Implement more of the ABI
According to the description of the LoongArch abi documentation,
(https://loongson.github.io/LoongArch-Documentation/LoongArch-ELF-ABI-EN.html)
the calling convention of LoongArch is almost the same as the RISCV's
(except for the vector part), so we borrow the implementation of RISCV.

This patch only guarantees the correctness of lp64d, because only the
part of lp64d is described in detail in the documentation.

Differential Revision: https://reviews.llvm.org/D130249
2022-08-05 15:14:16 +08:00
wanglei fd6545322c [LoongArch] Refactor insertDivByZeroTrap
Ensure non-terminators don't follow terminators.
This patch fixes the `sdiv-udiv-srem-urem.ll` test failure with
expensive check.

Differential Revision: https://reviews.llvm.org/D130247
2022-07-29 17:06:49 +08:00
Fangrui Song 9742166935 [LoongArch] Support load/store of dso_local PIC global values
lowerGlobalAddress added by D128427 can be used for PIC. The actual condition is
that the global value needs to be dso_local (a dso_preemptable one needs GOT
indirection).

load-store.ll has UB due to out-of-bounds load/store. Fix the UB in the variable
test and add an array test. Note: NOPIC array index is currently wrong.

Reviewed By: wangleiat

Differential Revision: https://reviews.llvm.org/D129977
2022-07-21 19:37:56 -07:00
Weining Lu 9b87ad33c1 [LoongArch] Implement OR combination to generate bstrins.w/d
Differential Revision: https://reviews.llvm.org/D129357
2022-07-14 17:20:43 +08:00
gonglingqin 47f3dc6d49 [LoongArch] Add codegen support for atomic fence, atomic load and atomic store
Differential Revision: https://reviews.llvm.org/D128901
2022-07-13 15:25:45 +08:00
gonglingqin e147a0f65a [LoongArch] Add codegen support for converting between unsigned integer and floating-point
Differential Revision: https://reviews.llvm.org/D128900
2022-07-13 15:25:44 +08:00
gonglingqin 1df96ce518 [LoongArch] Add codegen support for fpround, fpextend and converting between signed integer and floating-point
Differential Revision: https://reviews.llvm.org/D128899
2022-07-13 15:25:43 +08:00
Weining Lu 1d27f26426 [LoongArch] Add codegen support for multiplication operations
Reference:
https://llvm.org/docs/LangRef.html#mul-instruction

Differential Revision: https://reviews.llvm.org/D128194
2022-07-08 17:15:17 +08:00
gonglingqin aeaca854d1 [LoongArch] Add codegen support for handling floating point immediates
Differential Revision: https://reviews.llvm.org/D128898
2022-07-07 08:13:33 +08:00
Weining Lu d29215790f [LoongArch] Add codegen support for division operations
These operations include sdiv/udiv/srem/urem.

As the ISA [https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#_div_wudu_mod_wudu]
described, when the divisor is 0, the result can be any value, but no
exception will be triggered. Unlike gcc, which by default emit code
that checks divide-by-zero after the division or modulus instruction,
we only emit this check when the `-loongarch-check-zero-division`
option is passed.

Differential Revision: https://reviews.llvm.org/D128572
2022-07-06 17:54:57 +08:00
wanglei 3610d5f5d4 [LoongArch] Add initial support for function calls
Note that this is just enough for simple function call examples to
generate working code.

A good portion of this patch is the extra functions that needed to be
implemented to support the test case. e.g. storeRegToStackSlot,
loadRegFromStackSlot, eliminateFrameIndex.

Differential Revision: https://reviews.llvm.org/D128429
2022-07-05 12:02:14 +08:00
wanglei b940fe6fe2 [LoongArch] Add codegen support for conditional branches
Setting ISD::BR_CC to Expand makes it much easier to deal with
matching the expanded form.

Differential Revision: https://reviews.llvm.org/D128428
2022-07-05 11:59:58 +08:00
wanglei 5b4851ed91 [LoongArch] Add codegen support for load/store operations
This patch also support lowering global addresses.

Differential Revision: https://reviews.llvm.org/D128427
2022-07-05 11:58:20 +08:00
Weining Lu 20e6ada2fb [LoongArch] Combine ((x & shifted_mask) >> shamt) to bstrpick.{w/d}
This is an improvement to LoongArch codegen. In D127206 we combined
((x >> shamt) & shifted_mask) to bstrpick and here we do a similar
combination when certain conditions are met.

Thanks to @xen0n for reminding me.

Differential Revision: https://reviews.llvm.org/D128191
2022-06-23 17:10:06 +08:00
Weining Lu cf71f97bb1 [LoongArch] Add codegen support for part of conversion operations
These operations include `sext`, `zext` and `trunc`.

Reference:
https://llvm.org/docs/LangRef.html#conversion-operations

Differential Revision: https://reviews.llvm.org/D127204
2022-06-20 10:00:20 +08:00
Weining Lu 2ef7c4ce46 [LoongArch] Perform `and` combination with a shifted mask
Differential Revision: https://reviews.llvm.org/D127206
2022-06-20 09:58:17 +08:00