Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions llvm/lib/Target/X86/GISel/X86LegalizerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ X86LegalizerInfo::X86LegalizerInfo(const X86Subtarget &STI,
(UseX87 && typeInSet(0, {s80})(Query));
});

getActionDefinitionsBuilder(G_FABS)
.legalFor(UseX87 && !HasSSE2, {s32, s64, s80})
.custom();

// fp comparison
getActionDefinitionsBuilder(G_FCMP)
.legalFor(HasSSE1 || UseX87, {s8, s32})
Expand Down Expand Up @@ -669,6 +673,8 @@ bool X86LegalizerInfo::legalizeCustom(LegalizerHelper &Helper, MachineInstr &MI,
return legalizeSITOFP(MI, MRI, Helper);
case TargetOpcode::G_FPTOSI:
return legalizeFPTOSI(MI, MRI, Helper);
case TargetOpcode::G_FABS:
return legalizeFAbs(MI, MRI, Helper);
}
llvm_unreachable("expected switch to return");
}
Expand Down Expand Up @@ -835,6 +841,43 @@ bool X86LegalizerInfo::legalizeNarrowingStore(MachineInstr &MI,
return true;
}

bool X86LegalizerInfo::legalizeFAbs(MachineInstr &MI,
MachineRegisterInfo &MRI,
LegalizerHelper &Helper) const {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-format this ?


MachineIRBuilder &MIRBuilder = Helper.MIRBuilder;
Register SrcReg = MI.getOperand(1).getReg();
Register DstReg = MI.getOperand(0).getReg();
LLT Ty = MRI.getType(DstReg);
if (Subtarget.is32Bit()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand - why are you making x86_64 use a constant pool for all float types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For functionality of abs we are using mask, in 64 bit we have masks spans 64 bit. From ISA spec, we can only encode 32 bit immediate in instruction. so we need to load 64 bit into reg and then use reg.
Are you suggesting we only use it to double type?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mahesh-attarde I think SDAG uses constant pool because of working with xmm registers. When we don't have them we still do just a simple movabs: https://godbolt.org/z/966EGMWah

// Reset sign bit
MIRBuilder.buildAnd(
DstReg, SrcReg,
MIRBuilder.buildConstant(
Ty, APInt::getSignedMaxValue(Ty.getScalarSizeInBits())));
} else {
// In 64 bit mode, constant pool is used.
auto &MF = MIRBuilder.getMF();
Type *IRTy = getTypeForLLT(Ty, MF.getFunction().getContext());
Constant *ConstMask = ConstantInt::get(
IRTy, APInt::getSignedMaxValue(Ty.getScalarSizeInBits()));
LLT DstTy = MRI.getType(DstReg);
const DataLayout &DL = MIRBuilder.getDataLayout();
unsigned AddrSpace = DL.getDefaultGlobalsAddressSpace();
Align Alignment(DL.getABITypeAlign(
getTypeForLLT(DstTy, MF.getFunction().getContext())));
auto Addr = MIRBuilder.buildConstantPool(
LLT::pointer(AddrSpace, DL.getPointerSizeInBits(AddrSpace)),
MF.getConstantPool()->getConstantPoolIndex(ConstMask, Alignment));
MachineMemOperand *MMO =
MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
MachineMemOperand::MOLoad, DstTy, Alignment);
auto LoadedMask = MIRBuilder.buildLoad(DstTy, Addr, *MMO);
MIRBuilder.buildAnd(DstReg, SrcReg, LoadedMask);
}
MI.eraseFromParent();
return true;
}
bool X86LegalizerInfo::legalizeIntrinsic(LegalizerHelper &Helper,
MachineInstr &MI) const {
return true;
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/X86/GISel/X86LegalizerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class X86LegalizerInfo : public LegalizerInfo {

bool legalizeFPTOSI(MachineInstr &MI, MachineRegisterInfo &MRI,
LegalizerHelper &Helper) const;
bool legalizeFAbs(MachineInstr &MI, MachineRegisterInfo &MRI,
LegalizerHelper &Helper) const;
};
} // namespace llvm
#endif
2 changes: 2 additions & 0 deletions llvm/test/CodeGen/X86/isel-fabs-x87.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc < %s -mtriple=x86_64-- -mattr=+x87,-sse2,-sse | FileCheck %s --check-prefixes=X64
; RUN: llc < %s -mtriple=x86_64-- -mattr=+x87,-sse2,-sse -fast-isel -fast-isel-abort=1 | FileCheck %s --check-prefixes=X64
; RUN: llc < %s -mtriple=x86_64-- -mattr=+x87,-sse2,-sse -global-isel -global-isel-abort=1 | FileCheck %s --check-prefixes=X64
; RUN: llc < %s -mtriple=i686-- -mattr=+x87,-sse2,-sse | FileCheck %s --check-prefixes=X86
; RUN: llc < %s -mtriple=i686-- -mattr=+x87,-sse2,-sse -fast-isel -fast-isel-abort=1 | FileCheck %s --check-prefixes=X86
; RUN: llc < %s -mtriple=i686-- -mattr=+x87,-sse2,-sse -global-isel -global-isel-abort=1 | FileCheck %s --check-prefixes=X86
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add f32/f64 test coverage here? otherwise we've lost all x87-only fabs tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is known issue, we discussed it earlier while submitting test review.
#142558 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can store the value instead of returning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


define x86_fp80 @test_x86_fp80_abs(x86_fp80 %arg) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably would be nice to see how we handle floats and double without SSE.

; X64-LABEL: test_x86_fp80_abs:
Expand Down
42 changes: 41 additions & 1 deletion llvm/test/CodeGen/X86/isel-fabs.ll
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
; RUN: llc < %s -mtriple=x86_64-- -mattr=-x87 | FileCheck %s --check-prefixes=X64
; RUN: llc < %s -mtriple=x86_64-- -mattr=-x87 -fast-isel -fast-isel-abort=1 | FileCheck %s --check-prefixes=X64
; RUN: llc < %s -mtriple=x86_64-- -mattr=-x87 -global-isel -global-isel-abort=1 | FileCheck %s --check-prefixes=GISEL-X64
; RUN: llc < %s -mtriple=i686-- -mattr=-x87 | FileCheck %s --check-prefixes=X86
; RUN: llc < %s -mtriple=i686-- -mattr=-x87 -fast-isel -fast-isel-abort=1 | FileCheck %s --check-prefixes=FASTISEL-X86

; RUN: llc < %s -mtriple=i686-- -mattr=-x87 -global-isel -global-isel-abort=1 | FileCheck %s --check-prefixes=GISEL-X86

define float @test_float_abs(float %arg) {
; SDAG-X64-LABEL: test_float_abs:
; SDAG-X64: # %bb.0:
; SDAG-X64-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; SDAG-X64-NEXT: retq
;
; X64-LABEL: test_float_abs:
; X64: # %bb.0:
; X64-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-NEXT: retq
;
; GISEL-X64-LABEL: test_float_abs:
; GISEL-X64: # %bb.0:
; GISEL-X64-NEXT: movd %xmm0, %eax
; GISEL-X64-NEXT: andl {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %eax
; GISEL-X64-NEXT: movd %eax, %xmm0
; GISEL-X64-NEXT: retq
;
; X86-LABEL: test_float_abs:
; X86: # %bb.0:
; X86-NEXT: movl $2147483647, %eax # imm = 0x7FFFFFFF
Expand All @@ -22,16 +35,34 @@ define float @test_float_abs(float %arg) {
; FASTISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; FASTISEL-X86-NEXT: andl $2147483647, %eax # imm = 0x7FFFFFFF
; FASTISEL-X86-NEXT: retl
;
; GISEL-X86-LABEL: test_float_abs:
; GISEL-X86: # %bb.0:
; GISEL-X86-NEXT: movl $2147483647, %eax # imm = 0x7FFFFFFF
; GISEL-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
; GISEL-X86-NEXT: retl
%abs = tail call float @llvm.fabs.f32(float %arg)
ret float %abs
}

define double @test_double_abs(double %arg) {
; SDAG-X64-LABEL: test_double_abs:
; SDAG-X64: # %bb.0:
; SDAG-X64-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; SDAG-X64-NEXT: retq
;
; X64-LABEL: test_double_abs:
; X64: # %bb.0:
; X64-NEXT: andps {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
; X64-NEXT: retq
;
; GISEL-X64-LABEL: test_double_abs:
; GISEL-X64: # %bb.0:
; GISEL-X64-NEXT: movq %xmm0, %rax
; GISEL-X64-NEXT: andq {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %rax
; GISEL-X64-NEXT: movq %rax, %xmm0
; GISEL-X64-NEXT: retq
;
; X86-LABEL: test_double_abs:
; X86: # %bb.0:
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
Expand All @@ -45,6 +76,15 @@ define double @test_double_abs(double %arg) {
; FASTISEL-X86-NEXT: movl {{[0-9]+}}(%esp), %eax
; FASTISEL-X86-NEXT: andl $2147483647, %edx # imm = 0x7FFFFFFF
; FASTISEL-X86-NEXT: retl
;
; GISEL-X86-LABEL: test_double_abs:
; GISEL-X86: # %bb.0:
; GISEL-X86-NEXT: movl $-1, %eax
; GISEL-X86-NEXT: movl $2147483647, %edx # imm = 0x7FFFFFFF
; GISEL-X86-NEXT: andl {{[0-9]+}}(%esp), %eax
; GISEL-X86-NEXT: andl {{[0-9]+}}(%esp), %edx
; GISEL-X86-NEXT: retl

%abs = tail call double @llvm.fabs.f64(double %arg)
ret double %abs
}
Loading