Skip to content

Commit

Permalink
[DPWBS-1388] Add legality rules for G_INTRINSIC_TRUNC
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar Solymosi authored and konstantinschwarz committed Mar 17, 2020
1 parent 290d0bf commit f97d2c6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
38 changes: 38 additions & 0 deletions llvm/lib/Target/TriCore/TriCoreLegalizerInfo.cpp
Expand Up @@ -310,6 +310,10 @@ TriCoreLegalizerInfo::TriCoreLegalizerInfo(const TriCoreSubtarget &ST) {
.clampScalar(0, s32, s64)
.widenScalarToNextPow2(0);

// G_INTRINSIC_TRUNC should use custom legalization since we cannot model all
// semantics of this instruction on TriCore yet
getActionDefinitionsBuilder(G_INTRINSIC_TRUNC).customFor({s32, s64});

// Floating Point Unary Ops

// G_FPTOSI and G_FPTOUI are always legal for f32->s32 conversions.
Expand Down Expand Up @@ -529,6 +533,8 @@ bool TriCoreLegalizerInfo::legalizeCustom(MachineInstr &MI,
return false;
case TargetOpcode::G_FCMP:
return legalizeFCmp(MI, MRI, MIRBuilder);
case TargetOpcode::G_INTRINSIC_TRUNC:
return legalizeIntrinsicTrunc(MI, MRI, MIRBuilder);
case TargetOpcode::G_VAARG:
return legalizeVaArg(MI, MRI, MIRBuilder);
}
Expand All @@ -553,6 +559,38 @@ bool TriCoreLegalizerInfo::legalizeIntrinsic(
return true;
}

bool TriCoreLegalizerInfo::legalizeIntrinsicTrunc(
MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &MIRBuilder) const {
// Since G_INTRINSIC_TRUNC cannot be modeled in hardware, and because a
// libcall action is not supported, we need to create a libcall here
assert(MI.getOpcode() == TargetOpcode::G_INTRINSIC_TRUNC);

const Register DstReg = MI.getOperand(0).getReg();
const Register SrcReg = MI.getOperand(1).getReg();

LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();

const LLT SrcTy = MRI.getType(SrcReg);
const unsigned Size = SrcTy.getSizeInBits();

RTLIB::Libcall RTLibOpc = Size == 32 ? RTLIB::TRUNC_F32 : RTLIB::TRUNC_F64;
const auto Type = Size == 32 ? Type::getFloatTy(Ctx) : Type::getDoubleTy(Ctx);

MIRBuilder.setInstr(MI);

assert(SrcTy == MRI.getType(DstReg) &&
(SrcTy == LLT::scalar(32) || SrcTy == LLT::scalar(64)) &&
"Expected float or double float types");

if (createLibcall(MIRBuilder, RTLibOpc, {DstReg, Type}, {{SrcReg, Type}}) ==
LegalizerHelper::UnableToLegalize)
return false;

MI.eraseFromParent();
return true;
}

bool TriCoreLegalizerInfo::legalizeFCmp(MachineInstr &MI,
MachineRegisterInfo &MRI,
MachineIRBuilder &MIRBuilder) const {
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/TriCore/TriCoreLegalizerInfo.h
Expand Up @@ -37,6 +37,8 @@ class TriCoreLegalizerInfo : public LegalizerInfo {
private:
bool legalizeFCmp(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &MIRBuilder) const;
bool legalizeIntrinsicTrunc(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &MIRBuilder) const;
bool legalizeVaArg(MachineInstr &MI, MachineRegisterInfo &MRI,
MachineIRBuilder &MIRBuilder) const;
void setFCmpLibcalls();
Expand Down
41 changes: 41 additions & 0 deletions llvm/test/CodeGen/TriCore/GlobalIsel/legalize-intrinsic-trunc.mir
@@ -0,0 +1,41 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
# RUN: llc -O0 -mtriple=tricore -global-isel -run-pass=legalizer \
# RUN: -verify-machineinstrs %s -o - | FileCheck %s

---
name: intrinsic_trunc_s32
body: |
bb.0:
liveins: $d4
; CHECK-LABEL: name: intrinsic_trunc_s32
; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY $d4
; CHECK: ADJCALLSTACKDOWN 0, 0, implicit-def $a10, implicit $a10
; CHECK: $d4 = COPY [[COPY]](s32)
; CHECK: CALL &truncf, csr_tricore_uppercontext, implicit-def $a11, implicit $psw, implicit $d4, implicit-def $d2
; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY $d2
; CHECK: ADJCALLSTACKUP 0, 0, implicit-def $a10, implicit $a10
; CHECK: $d4 = COPY [[COPY1]](s32)
%0:_(s32) = COPY $d4
%1:_(s32) = G_INTRINSIC_TRUNC %0(s32)
$d4 = COPY %1(s32)
...

---
name: intrinsic_trunc_s64
body: |
bb.0:
liveins: $d4
; CHECK-LABEL: name: intrinsic_trunc_s64
; CHECK: [[COPY:%[0-9]+]]:_(s64) = COPY $e4
; CHECK: ADJCALLSTACKDOWN 0, 0, implicit-def $a10, implicit $a10
; CHECK: $e4 = COPY [[COPY]](s64)
; CHECK: CALL &trunc, csr_tricore_uppercontext, implicit-def $a11, implicit $psw, implicit $e4, implicit-def $e2
; CHECK: [[COPY1:%[0-9]+]]:_(s64) = COPY $e2
; CHECK: ADJCALLSTACKUP 0, 0, implicit-def $a10, implicit $a10
; CHECK: $e4 = COPY [[COPY1]](s64)
%0:_(s64) = COPY $e4
%1:_(s64) = G_INTRINSIC_TRUNC %0(s64)
$e4 = COPY %1(s64)
...

0 comments on commit f97d2c6

Please sign in to comment.