15 Commits

Author SHA1 Message Date
wanglei
1bb7766489 [LoongArch] Optimize stack realignment using BSTRINS instruction
Prior to this change, stack realignment was achieved using the SRLI/SLLI
instructions in two steps. With this patch, stack realignment is
optimized using a single `BSTRINS` instruction.

Reviewed By: SixWeining, xen0n

Differential Revision: https://reviews.llvm.org/D158384
2023-08-23 09:21:42 +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
Lin Runze
389079c87c [LoongArch] Add GHC Calling Convention
This is modeled after [[ https://reviews.llvm.org/D89788 | the RISCV GHC calling convention]]
and matches [[ https://gitlab.haskell.org/ghc/ghc/-/merge_requests/9292 | the corresponding GHC change ]].

Reviewed By: xen0n, wangleiat

Differential Revision: https://reviews.llvm.org/D137495
2022-12-26 19:36:41 +08:00
Christudasan Devadasan
b5efec4b27 [CodeGen] Additional Register argument to storeRegToStackSlot/loadRegFromStackSlot
With D134950, targets get notified when a virtual register is created and/or
cloned. Targets can do the needful with the delegate callback. AMDGPU propagates
the virtual register flags maintained in the target file itself. They are useful
to identify a certain type of machine operands while inserting spill stores and
reloads. Since RegAllocFast spills the physical register itself, there is no way
its virtual register can be mapped back to retrieve the flags. It can be solved
by passing the virtual register as an additional argument. This argument has no
use when the spill interfaces are called during the greedy allocator or even the
PrologEpilogInserter and can pass a null register in such cases.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D138656
2022-12-17 11:55:34 +05:30
wanglei
0e4378c55e [LoongArch] Add emergency spill slot for CFR spill/reload
When all registers have been allocated and CFR needs to be saved on the
stack, an emergency spill slot is required. Because CFR's spill and
reload require a general purpose register to transfer.

The attached test case was bugpoint-reduced down from
`MultiSource/Benchmarks/mafft/Lalignmm.c` in the test-suite.
Without this patch, llc will crash and report the following errors:

```
LLVM ERROR: Error while trying to spill R4 from class GPR: Cannot scavenge register without an emergency spill slot!
```

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D138007
2022-11-19 14:35:31 +08:00
Xiaodong Liu
03d07e181d [LoongArch] Handle register spill in BranchRelaxation pass
When the range of the unconditional branch is overflow, the indirect
branch way is used. The case when there is no scavenged register for
indirect branch needs to spill register to stack.

Reviewed By: SixWeining, wangleiat

Differential Revision: https://reviews.llvm.org/D137821
2022-11-15 09:55:40 +08:00
wanglei
d20c54cbdb [LoongArch] Override TargetFrameLowering::spillCalleeSavedRegisters
When using `llvm.returnaddress` intrinsic, special handling is required
for the spill of the `RA` register. Otherwise it will cause the verifier
fail in some cases (e.g. pr17377.c of the GCC C Torture Suite).

Specifically:
```
*** Bad machine code: Using an undefined physical register ***
- function:    f
- basic block: %bb.0 entry (0xd94d18)
- instruction: ST_D killed $r1, $r22, -40 :: (store (s64) into %stack.2)
- operand 0:   killed $r1
```

Reviewed By: SixWeining

Differential Revision: https://reviews.llvm.org/D137387
2022-11-10 21:14:27 +08:00
wanglei
4e2364a285 [LoongArch] Add emergency spill slot for GPR for large frames
An emergency spill slot is created when the stack size cannot be
represented by an 11-bit signed number.

This patch also modifies how the `sp` is adjusted in the prologue.

`RegScavenger` will place the spill instruction before the prologue
if a VReg is created in the prologue. This will pollute the caller's
stack data. Therefore, until there is better way, we just use the
`addi.w/d` instruction for stack adjustment to ensure that VReg will
not be created. (RISCV has the same issue #58286)

Due to the addition of emergency spill slot, some test cases that use
exact stacksize need to be updated.

Differential Revision: https://reviews.llvm.org/D135757
2022-10-28 17:51:53 +08:00
wanglei
f589e5067f [LoongArch] Split SP adjustment
This patch split the SP adjustment to reduce the instructions in
prologue and epilogue. In this way, the offset of the callee saved
register could fit in a single store.

Similar to D68011(RISCV).

Differential Revision: https://reviews.llvm.org/D136222
2022-10-28 16:39:00 +08:00
wanglei
daf067da04 [LoongArch] Stack realignment support
This patch adds support for stack realignment while adding support for
variable sized objects.

Differential Revision: https://reviews.llvm.org/D136074
2022-10-21 17:30:29 +08:00
WANG Xuerui
c2a44b591e [LoongArch] Support lowering frames larger than 2048 bytes
Differential Revision: https://reviews.llvm.org/D134582
2022-09-27 18:58:33 +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
c2d1d9f942 [LoongArch] Implement prologue/epilogue insertion
Differential Revision: https://reviews.llvm.org/D128432
2022-07-06 15:52:26 +08:00
wanglei
5b1ec70590 [LoongArch] Support lowering FrameIndex
Ensure callee-saved registers are accessed relative to the stackpointer.

Differential Revision: https://reviews.llvm.org/D128430
2022-07-06 15:40:43 +08:00
Lu Weining
33388ae866 [LoongArch 4/6] Add basic tablegen infra for LoongArch
This patch introduces basic tablegen infra such as
LoongArch{InstrFormats,InstrInfo,RegisterInfo,CallingConv,}.td.

For now, only add instruction definitions for LoongArch basic integer
operations.
Our initial target is a working MC layer rather than codegen,
so appropriate SelectionDAG patterns will come later.

Differential revision: https://reviews.llvm.org/D115861
2022-02-10 10:23:34 +00:00