Skip to content

Commit

Permalink
[LoongArch] Support llvm.thread.pointer
Browse files Browse the repository at this point in the history
For `__builtin_thread_pointer` to work, among other things.

Similar to D76828 for RISCV.

Differential Revision: https://reviews.llvm.org/D134368
  • Loading branch information
xen0n authored and SixWeining committed Sep 26, 2022
1 parent a5676a3 commit f89f099
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
18 changes: 18 additions & 0 deletions llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
Expand Up @@ -60,6 +60,8 @@ LoongArchTargetLowering::LoongArchTargetLowering(const TargetMachine &TM,

setOperationAction({ISD::GlobalAddress, ISD::ConstantPool}, GRLenVT, Custom);

setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);

setOperationAction(ISD::EH_DWARF_CFA, MVT::i32, Custom);
if (Subtarget.is64Bit())
setOperationAction(ISD::EH_DWARF_CFA, MVT::i64, Custom);
Expand Down Expand Up @@ -176,6 +178,8 @@ SDValue LoongArchTargetLowering::LowerOperation(SDValue Op,
return lowerEH_DWARF_CFA(Op, DAG);
case ISD::GlobalAddress:
return lowerGlobalAddress(Op, DAG);
case ISD::INTRINSIC_WO_CHAIN:
return lowerINTRINSIC_WO_CHAIN(Op, DAG);
case ISD::SHL_PARTS:
return lowerShiftLeftParts(Op, DAG);
case ISD::SRA_PARTS:
Expand Down Expand Up @@ -310,6 +314,20 @@ SDValue LoongArchTargetLowering::lowerGlobalAddress(SDValue Op,
report_fatal_error("Unable to lowerGlobalAddress");
}

SDValue LoongArchTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
SelectionDAG &DAG) const {
unsigned IntNo = Op.getConstantOperandVal(0);

switch (IntNo) {
default:
return SDValue(); // Don't custom lower most intrinsics.
case Intrinsic::thread_pointer: {
EVT PtrVT = getPointerTy(DAG.getDataLayout());
return DAG.getRegister(LoongArch::R2, PtrVT);
}
}
}

SDValue LoongArchTargetLowering::lowerShiftLeftParts(SDValue Op,
SelectionDAG &DAG) const {
SDLoc DL(Op);
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Target/LoongArch/LoongArchISelLowering.h
Expand Up @@ -143,6 +143,7 @@ class LoongArchTargetLowering : public TargetLowering {
SDValue lowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
SDValue lowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;

bool isFPImmLegal(const APFloat &Imm, EVT VT,
bool ForCodeSize) const override;
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/CodeGen/LoongArch/thread-pointer.ll
@@ -0,0 +1,14 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s --mtriple=loongarch32 | FileCheck %s
; RUN: llc < %s --mtriple=loongarch64 | FileCheck %s

declare ptr @llvm.thread.pointer()

define ptr @thread_pointer() nounwind {
; CHECK-LABEL: thread_pointer:
; CHECK: # %bb.0:
; CHECK-NEXT: move $a0, $tp
; CHECK-NEXT: ret
%1 = tail call ptr @llvm.thread.pointer()
ret ptr %1
}

0 comments on commit f89f099

Please sign in to comment.