373 Commits

Author SHA1 Message Date
Jinyang He
c51ab483e6
[LoongArch] Insert nops and emit align reloc when handle alignment directive (#72962)
Refer to RISCV, we will fix up the alignment if linker relaxation
changes code size and breaks alignment. Insert enough Nops and emit
R_LARCH_ALIGN relocation type so that linker could satisfy the alignment
by removing Nops.
It does so only in sections with the SHF_EXECINSTR flag.

In LoongArch psABI v2.30, R_LARCH_ALIGN requires symbol index. The
lowest 8 bits of addend represent alignment and the other bits of addend
represent the maximum number of bytes to emit.
2024-01-24 09:17:49 +08:00
yjijd
44ba6ebc99
[CodeGen][LoongArch] Set FP_TO_SINT/FP_TO_UINT to legal for vector types (#79107)
Support the following conversions:
v4f32->v4i32, v2f64->v2i64(LSX)
v8f32->v8i32, v4f64->v4i64(LASX)
v4f32->v4i64, v4f64->v4i32(LASX)
2024-01-23 15:57:06 +08:00
yjijd
f799f93692
[CodeGen][LoongArch] Set SINT_TO_FP/UINT_TO_FP to legal for vector types (#78924)
Support the following conversions:
v4i32->v4f32, v2i64->v2f64(LSX)
v8i32->v8f32, v4i64->v4f64(LASX)
v4i32->v4f64, v4i64->v4f32(LASX)
2024-01-23 15:16:23 +08:00
Ami-zhang
fcb8342a21
[LoongArch] Add definitions and feature 'frecipe' for FP approximation intrinsics/builtins (#78962)
This PR adds definitions and 'frecipe' feature for FP approximation
intrinsics/builtins. In additions, this adds and complements relative
testcases.
2024-01-23 14:24:58 +08:00
wanglei
fcff4582f0
[LoongArch] Permit auto-vectorization using LSX/LASX with auto-vec feature (#78943)
With enough codegen complete, we can now correctly report the size of
vector registers for LSX/LASX, allowing auto vectorization (The
`auto-vec` feature needs to be enabled simultaneously).

As described, the `auto-vec` feature is an experimental one. To ensure
that automatic vectorization is not enabled by default, because the
information provided by the current `TTI` cannot yield additional
benefits for automatic vectorization.
2024-01-23 09:06:35 +08:00
Ami-zhang
84bdee2875
[LoongArch] Add LoongArch V1.1 instructions definitions and MC tests (#78238)
LoongArch V1.1 instrucions include floating-point approximate reciprocal
instructions and atomic instrucions. And add testcases for these
instrucions meanwhile.
2024-01-17 11:15:05 +08:00
Jinyang He
ed7f4edc19
[LoongArch] Add relaxDwarfLineAddr and relaxDwarfCFA to handle the mutable label diff in dwarfinfo (#77728)
When linker-relaxation is enabled, part of the label diff in dwarfinfo
cannot be computed before static link. Refer to RISCV, we add the
relaxDwarfLineAddr and relaxDwarfCFA to add relocations for these label
diffs. Calculate whether the label diff is mutable. For immutable label
diff, return false and do the other works by its parent function.
2024-01-16 13:20:13 +08:00
wanglei
98c6aa7229
[LoongArch] Implement LoongArchRegisterInfo::canRealignStack() (#76913)
This patch fixes the crash issue in the test:
CodeGen/LoongArch/can-not-realign-stack.ll

Register allocator may spill virtual registers to the stack, which    
introduces stack alignment requirements (when the size of spilled     
    registers exceeds the default alignment size of the stack). If a  
function does not have stack alignment requirements before register   
allocation, registers used for stack alignment will not be preserved. 

Therefore, we should implement `canRealignStack()` to inform the      
register allocator whether it is allowed to perform stack realignment 
operations.
2024-01-09 20:35:49 +08:00
Alex Bradbury
197214e39b
[RFC][SelectionDAG] Add and use SDNode::getAsZExtVal() helper (#76710)
This follows on from #76708, allowing
`cast<ConstantSDNode>(N)->getZExtValue()` to be replaced with just
`N->getAsZextVal();`
    
Introduced via `git grep -l "cast<ConstantSDNode>\(.*\).*getZExtValue" |
xargs sed -E -i
's/cast<ConstantSDNode>\((.*)\)->getZExtValue/\1->getAsZExtVal/'` and
then using `git clang-format` on the result.
2024-01-09 12:25:17 +00:00
Jinyang He
b57159cb19
[LoongArch] Support R_LARCH_{ADD,SUB}_ULEB128 for .uleb128 and force relocs when sym is not in section (#76433)
1, Follow RISCV 1df5ea29 to support generates relocs for .uleb128 which
can not be folded. Unlike RISCV, the located content of LoongArch should
be zero. LoongArch fixup uleb128 value by in-place addition and
subtraction reloc types named R_LARCH_{ADD,SUB}_ULEB128. The located
content can affect the result and R_LARCH_ADD_ULEB128 has enough info to
represent the first symbol value, so it needs to be set to zero.
2, Force relocs if sym is not in section so that it can emit relocs for
external symbol.

Fixes:
https://github.com/llvm/llvm-project/pull/72960#issuecomment-1866844679
2024-01-09 15:14:54 +08:00
hev
16094cb629
[llvm][LoongArch] Support per-global code model attribute for LoongArch (#72079)
This patch gets the code model from global variable attribute if it has,
otherwise the target's will be used.

---------

Signed-off-by: WANG Rui <wangrui@loongson.cn>
2024-01-06 13:36:09 +08:00
Jie Fu
52d1397e38 [LoongArch] Fix -Wunused-variable in LoongArchExpandPseudoInsts.cpp (NFC)
llvm-project/llvm/lib/Target/LoongArch/LoongArchExpandPseudoInsts.cpp:480:20:
 error: unused variable 'MF' [-Werror,-Wunused-variable]
  MachineFunction *MF = MBB.getParent();
                   ^
1 error generated.
2024-01-05 12:05:23 +08:00
wanglei
c56a5e895a [LoongArch] Reimplement the expansion of PseudoLA*_LARGE instructions (#76555)
According to the description of the psABI v2.30:
https://github.com/loongson/la-abi-specs/releases/tag/v2.30, moved the
expansion of relevant pseudo-instructions from
`LoongArchPreRAExpandPseudo` pass to `LoongArchExpandPseudo` pass, to
ensure that the code sequences of `PseudoLA*_LARGE` instructions and
Medium code model's function call are not scheduled.
2024-01-05 10:57:53 +08:00
wanglei
2cf420d5b8 [LoongArch] Emit function call code sequence as PCADDU18I+JIRL in medium code model
According to the description of the psABI v2.20:
https://github.com/loongson/la-abi-specs/releases/tag/v2.20, adjustments
are made to the function call instructions under the medium code model.

At the same time, AsmParser has already supported parsing the call36 and
tail36 macro instructions.
2024-01-05 10:56:47 +08:00
Alex Bradbury
80aeb62211
[llvm][NFC] Use SDValue::getConstantOperandVal(i) where possible (#76708)
This helper function shortens examples like
`cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();` to
`Node->getConstantOperandVal(1);`.

Implemented with:
`git grep -l
"cast<ConstantSDNode>\(.*->getOperand\(.*\)\)->getZExtValue\(\)" | xargs
sed -E -i

's/cast<ConstantSDNode>\((.*)->getOperand\((.*)\)\)->getZExtValue\(\)/\1->getConstantOperandVal(\2)/`
and `git grep -l
"cast<ConstantSDNode>\(.*\.getOperand\(.*\)\)->getZExtValue\(\)" | xargs
sed -E -i

's/cast<ConstantSDNode>\((.*)\.getOperand\((.*)\)\)->getZExtValue\(\)/\1.getConstantOperandVal(\2)/'`.
With a couple of simple manual fixes needed. Result then processed by
`git clang-format`.
2024-01-02 13:14:28 +00:00
wanglei
da5378e87e [LoongArch] Fix incorrect pattern [X]VBITSELI_B instructions
Adjusted the operand order of [X]VBITSELI_B to correctly match vselect.
2023-12-29 14:44:29 +08:00
wanglei
c7367f985e [LoongArch] Fix incorrect pattern XVREPL128VEI_{W/D} instructions
Remove the incorrect patterns for `XVREPL128VEI_{W/D}` instructions,
and add correct patterns for XVREPLVE0_{W/D} instructions
2023-12-29 14:03:53 +08:00
wanglei
47c88bcd5d [LoongArch] Fix LASX vector_extract codegen
Custom lowering `ISD::EXTRACT_VECTOR_ELT` with lasx.
2023-12-29 13:48:53 +08:00
Jinyang He
b3ef8dce98
[LoongArch] Emit R_LARCH_RELAX when expanding some LoadAddress (#72961)
Emit relax relocs when expand non-large la.pcrel and non-large la.got on
llvm-mc stage, which like what does on GAS.
1, la.pcrel -> PCALA_HI20 + RELAX + PCALA_LO12 + RELAX
2, la.got -> GOT_PC_HI20 + RELAX + GOT_PC_LO12 + RELAX
2023-12-27 08:51:48 +08:00
Jinyang He
a8081ed8ff
[LoongArch] Allow delayed decision for ADD/SUB relocations (#72960)
Refer to RISCV [1], LoongArch also need delayed decision for ADD/SUB
relocations. In handleAddSubRelocations, just return directly if SecA !=
SecB, handleFixup usually will finish the the rest of creating PCRel
relocations works. Otherwise we emit relocs depends on whether
relaxation is enabled. If not, we return true and avoid record ADD/SUB
relocations.
Now the two symbols separated by alignment directive will return without
folding symbol offset in AttemptToFoldSymbolOffsetDifference, which has
the same effect when relaxation is enabled.

[1] https://reviews.llvm.org/D155357
2023-12-20 10:54:51 +08:00
Kazu Hirata
586ecdf205
[llvm] Use StringRef::{starts,ends}_with (NFC) (#74956)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-11 21:01:36 -08:00
wanglei
af999c4be9
[LoongArch] Add codegen support for [X]VF{MSUB/NMADD/NMSUB}.{S/D} instructions (#74819)
This is similar to single and double-precision floating-point
instructions.
2023-12-11 10:37:22 +08:00
Kazu Hirata
55531e715f [Target] Remove unused forward declarations (NFC) 2023-12-10 10:38:55 -08:00
wanglei
cdc3732566 [LoongArch] Mark ISD::FNEG as legal 2023-12-08 15:07:58 +08:00
wanglei
9f70e708a7
[LoongArch] Make ISD::FSQRT a legal operation with lsx/lasx feature (#74795)
And add some patterns:
1. (fdiv 1.0, vector)
2. (fdiv 1.0, (fsqrt vector))
2023-12-08 14:16:26 +08:00
Craig Topper
e87f33d9ce
[RISCV][MC] Pass MCSubtargetInfo down to shouldForceRelocation and evaluateTargetFixup. (#73721)
Instead of using the STI stored in RISCVAsmBackend, try to get it from
the MCFragment.

This addresses the issue raised here
https://discourse.llvm.org/t/possible-problem-related-to-subtarget-usage/75283
2023-12-07 13:17:58 -08:00
wanglei
9ff7d0ebeb
[LoongArch] Add codegen support for icmp/fcmp with lsx/lasx fetaures (#74700)
Mark ISD::SETCC node as legal, and add handling for the vector types
condition codes.
2023-12-07 20:11:43 +08:00
wanglei
de21308f78 [LoongArch] Make ISD::VSELECT a legal operation with lsx/lasx 2023-12-06 16:43:38 +08:00
wanglei
e9cd197d15 [LoongArch] Support MULHS/MULHU with lsx/lasx
Mark MULHS/MULHU nodes as legal and adds the necessary patterns.
2023-12-04 10:58:05 +08:00
wanglei
a60a5421b6 Reland "[LoongArch] Support CTLZ with lsx/lasx"
This patch simultaneously adds tests for `CTPOP`.

This relands 07cec73dcd095035257eec1f213d273b10988130 with fix tests.
2023-12-02 17:22:40 +08:00
wanglei
63e6bba0c3 Revert "[LoongArch] Support CTLZ with lsx/lasx"
This reverts commit 07cec73dcd095035257eec1f213d273b10988130.
2023-12-02 17:17:48 +08:00
wanglei
07cec73dcd [LoongArch] Support CTLZ with lsx/lasx
This patch simultaneously adds tests for `CTPOP`.
2023-12-02 17:13:36 +08:00
wanglei
66a3e4fafb [LoongArch] Override TargetLowering::isShuffleMaskLegal
By default, `isShuffleMaskLegal` always returns true, which can result
 in the expansion of `BUILD_VECTOR` into a `VECTOR_SHUFFLE` node in
 certain situations. Subsequently, the `VECTOR_SHUFFLE` node is expanded
 again into a `BUILD_VECTOR`, leading to an infinite loop.
 To address this, we always return false, allowing the expansion of
 `BUILD_VECTOR` through the stack.
2023-12-02 14:25:17 +08:00
wanglei
ca66df3b02 [LoongArch] Add more and/or/xor patterns for vector types 2023-12-01 10:28:41 +08:00
wanglei
add224c0a0 [LoongArch] Custom lowering ISD::BUILD_VECTOR 2023-12-01 09:13:39 +08:00
wanglei
f2cbd1fdf7 [LoongArch] Add codegen support for insertelement 2023-12-01 09:13:39 +08:00
wanglei
b72456120f
[LoongArch] Add codegen support for extractelement (#73759)
Add codegen support for extractelement when enable `lsx` or `lasx`
feature.
2023-11-30 17:29:18 +08:00
wanglei
5e7e0d6032
[LoongArch] Fix pattern for FNMSUB_{S/D} instructions (#73742)
```
when a=c=-0.0, b=0.0:
-(a * b + (-c)) = -0.0
-a * b + c = 0.0
(fneg (fma a, b (-c))) != (fma (fneg a), b ,c)
```

See https://reviews.llvm.org/D90901 for a similar discussion on X86.
2023-11-29 15:21:21 +08:00
hev
0d9f557b6c
[LoongArch] Disable mulodi4 and muloti4 libcalls (#73199)
This library function only exists in compiler-rt not libgcc. So this
would fail to link unless we were linking with compiler-rt.

Fixes https://github.com/ClangBuiltLinux/linux/issues/1958
2023-11-23 19:34:50 +08:00
ZhaoQi
1c68c4c57a
[LoongArch][MC] Modify branch evaluation for MCInstrAnalysis (#73205)
Function evaluateBranch() is used to compute target address for a given
branch instruction and return true on success. But target address of
indirect branch cannot be simply added, so rule it out and just return
false.

This patch also add objdump tests which capture the current state of
support for printing branch targets. Without this patch, the result of
"jirl $zero, $a0, 4" is "jirl $zero, $a0, 4 <foo+0x64>". It is obviously
incorrect, because this instruction represents an indirect branch whose
target address depends on both the register value and the imm. After
this patch, it will be right despite loss of details.
2023-11-23 16:38:41 +08:00
ZhaoQi
775d2f3201
[LoongArch][MC] Support to get the FixupKind for BL (#72938)
Previously, bolt could not get FixupKind for BL correctly, because bolt
cannot get target-flags for BL. Here just add support in MCCodeEmitter.

Fixes https://github.com/llvm/llvm-project/pull/72826.
2023-11-21 19:00:29 +08:00
ZhaoQi
42a4d5e8ca
[LoongArch] Set isBarrier to true for instruction 'b' (#72339)
Instr "b offs26" represent to an unconditional branch in LoongArch. Set
isBarrier to 1 in tablegen for it, so that MCInstrAnalysis can return
correctly.

Fixes https://github.com/llvm/llvm-project/pull/71903.
2023-11-16 14:01:58 +08:00
Jinyang He
f5bfc833fc
[LoongArch] Add relax feature and keep relocations (#72191)
Add relax feature. To support linker relocation, we should make
relocation with a symbol rather than section plus offset, and keep all
relocations with non-abs symbol.
2023-11-16 11:01:26 +08:00
ZhaoQi
f7d7847096
[LoongArch][MC] Refine MCInstrAnalysis based on registers used (#71276)
MCInstrAnalysis can return properties of instructions (e.g., isCall(),
isBranch(),...) based on the informations that MCInstrDesc can get from
*InstrInfo*.td files. These infos are based on opcodes only, but JIRL
can have different properties based on different registers used.

So this patch refines several MCInstrAnalysis methods: isTerminator,
isCall,isReturn,isBranch,isUnconditionalBranch and isIndirectBranch.

This patch also allows BOLT which will be supported on LoongArch later
to get right instruction infos.
2023-11-10 15:54:33 +08:00
Kazu Hirata
bf383dca36 [llvm] Stop including llvm/Support/Endian.h (NFC)
Identified with misc-include-cleaner.
2023-11-01 23:16:33 -07:00
wanglei
f2441a06c6 [LoongArch] Set some operations action for LSX and LASX
First, expand all truncationg stores and extending loads. Second,
expand everything for `fixedlen_vector_valuetypes`.  Finally, we
selectively turn on ones that can be effectively codegen'd.

Simultaneously, this patch adds floating-point vector types to
load/store patterns. Additional test cases will be included in the IR
instruction test patchs.
2023-10-25 13:02:01 +08:00
wanglei
47826b3f14 [LoongArch] Fix td pattern for CACOP LDPTE and LDDIR
The immediate argument should be a target constant (`timm`).
2023-10-20 11:24:55 +08:00
Lu Weining
78abc45c44
[LoongArch] Improve codegen for atomic cmpxchg ops (#69339)
PR #67391 improved atomic codegen by handling memory ordering specified
by the `cmpxchg` instruction. An acquire barrier needs to be generated
when memory ordering includes an acquire operation. This PR improves the
codegen further by only handling the failure ordering.
2023-10-19 09:21:51 +08:00
wanglei
271087e3a0
[LoongArch] Implement COPY instruction between CFRs (#69300)
With this patch, all CFRs can be used for register allocation.
2023-10-19 09:20:27 +08:00
Kazu Hirata
4a0ccfa865 Use llvm::endianness::{big,little,native} (NFC)
Note that llvm::support::endianness has been renamed to
llvm::endianness while becoming an enum class as opposed to an
enum. This patch replaces support::{big,little,native} with
llvm::endianness::{big,little,native}.
2023-10-12 21:21:45 -07:00