Skip to content

Commit e498902

Browse files
committed
[M68k] Implement SWAP instruction encoding and basic patterns
This implements an Instruction class for SWAP in order to enable its use during DAG selection. As of now, it is being used for mul/div patterns which were previously getting the high-word output by shifting it 16 bits to the right.
1 parent 7b19681 commit e498902

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

llvm/lib/Target/M68k/M68kInstrArithmetic.td

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,12 @@ def : Pat<(udiv i8:$dst, i8:$opd),
655655

656656
def : Pat<(srem i8:$dst, i8:$opd),
657657
(EXTRACT_SUBREG
658-
(ASR32di (ASR32di (SDIVd32d16 (MOVSXd32d8 $dst), (MOVSXd16d8 $opd)), 8), 8),
658+
(SWAP (SDIVd32d16 (MOVSXd32d8 $dst), (MOVSXd16d8 $opd))),
659659
MxSubRegIndex8Lo)>;
660660

661661
def : Pat<(urem i8:$dst, i8:$opd),
662662
(EXTRACT_SUBREG
663-
(LSR32di (LSR32di (UDIVd32d16 (MOVZXd32d8 $dst), (MOVZXd16d8 $opd)), 8), 8),
663+
(SWAP (UDIVd32d16 (MOVZXd32d8 $dst), (MOVZXd16d8 $opd))),
664664
MxSubRegIndex8Lo)>;
665665

666666
// RR i16
@@ -676,12 +676,12 @@ def : Pat<(udiv i16:$dst, i16:$opd),
676676

677677
def : Pat<(srem i16:$dst, i16:$opd),
678678
(EXTRACT_SUBREG
679-
(ASR32di (ASR32di (SDIVd32d16 (MOVSXd32d16 $dst), $opd), 8), 8),
679+
(SWAP (SDIVd32d16 (MOVSXd32d16 $dst), $opd)),
680680
MxSubRegIndex16Lo)>;
681681

682682
def : Pat<(urem i16:$dst, i16:$opd),
683683
(EXTRACT_SUBREG
684-
(LSR32di (LSR32di (UDIVd32d16 (MOVZXd32d16 $dst), $opd), 8), 8),
684+
(SWAP (UDIVd32d16 (MOVZXd32d16 $dst), $opd)),
685685
MxSubRegIndex16Lo)>;
686686

687687
// RI i8
@@ -697,12 +697,12 @@ def : Pat<(udiv i8:$dst, Mxi8immSExt8:$opd),
697697

698698
def : Pat<(srem i8:$dst, Mxi8immSExt8:$opd),
699699
(EXTRACT_SUBREG
700-
(ASR32di (ASR32di (SDIVd32i16 (MOVSXd32d8 $dst), (as_i16imm $opd)), 8), 8),
700+
(SWAP (SDIVd32i16 (MOVSXd32d8 $dst), (as_i16imm $opd))),
701701
MxSubRegIndex8Lo)>;
702702

703703
def : Pat<(urem i8:$dst, Mxi8immSExt8:$opd),
704704
(EXTRACT_SUBREG
705-
(LSR32di (LSR32di (UDIVd32i16 (MOVZXd32d8 $dst), (as_i16imm $opd)), 8), 8),
705+
(SWAP (UDIVd32i16 (MOVZXd32d8 $dst), (as_i16imm $opd))),
706706
MxSubRegIndex8Lo)>;
707707

708708
// RI i16
@@ -718,12 +718,12 @@ def : Pat<(udiv i16:$dst, Mxi16immSExt16:$opd),
718718

719719
def : Pat<(srem i16:$dst, Mxi16immSExt16:$opd),
720720
(EXTRACT_SUBREG
721-
(ASR32di (ASR32di (SDIVd32i16 (MOVSXd32d16 $dst), imm:$opd), 8), 8),
721+
(SWAP (SDIVd32i16 (MOVSXd32d16 $dst), imm:$opd)),
722722
MxSubRegIndex16Lo)>;
723723

724724
def : Pat<(urem i16:$dst, Mxi16immSExt16:$opd),
725725
(EXTRACT_SUBREG
726-
(LSR32di (LSR32di (UDIVd32i16 (MOVZXd32d16 $dst), imm:$opd), 8), 8),
726+
(SWAP (UDIVd32i16 (MOVZXd32d16 $dst), imm:$opd)),
727727
MxSubRegIndex16Lo)>;
728728

729729

@@ -740,12 +740,12 @@ def : Pat<(mul i16:$dst, i16:$opd),
740740

741741
def : Pat<(mulhs i16:$dst, i16:$opd),
742742
(EXTRACT_SUBREG
743-
(ASR32di (ASR32di (SMULd32d16 (MOVXd32d16 $dst), $opd), 8), 8),
743+
(SWAP (SMULd32d16 (MOVXd32d16 $dst), $opd)),
744744
MxSubRegIndex16Lo)>;
745745

746746
def : Pat<(mulhu i16:$dst, i16:$opd),
747747
(EXTRACT_SUBREG
748-
(LSR32di (LSR32di (UMULd32d16 (MOVXd32d16 $dst), $opd), 8), 8),
748+
(SWAP (UMULd32d16 (MOVXd32d16 $dst), $opd)),
749749
MxSubRegIndex16Lo)>;
750750

751751
def : Pat<(mul i32:$dst, i32:$opd), (SMULd32d32 $dst, $opd)>;
@@ -759,12 +759,12 @@ def : Pat<(mul i16:$dst, Mxi16immSExt16:$opd),
759759

760760
def : Pat<(mulhs i16:$dst, Mxi16immSExt16:$opd),
761761
(EXTRACT_SUBREG
762-
(ASR32di (ASR32di (SMULd32i16 (MOVXd32d16 $dst), imm:$opd), 8), 8),
762+
(SWAP (SMULd32i16 (MOVXd32d16 $dst), imm:$opd)),
763763
MxSubRegIndex16Lo)>;
764764

765765
def : Pat<(mulhu i16:$dst, Mxi16immSExt16:$opd),
766766
(EXTRACT_SUBREG
767-
(LSR32di (LSR32di (UMULd32i16 (MOVXd32d16 $dst), imm:$opd), 8), 8),
767+
(SWAP (UMULd32i16 (MOVXd32d16 $dst), imm:$opd)),
768768
MxSubRegIndex16Lo)>;
769769

770770

llvm/lib/Target/M68k/M68kInstrShiftRotate.td

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,23 @@ defm ASR : MxSROp<"asr", sra, MxRODI_R, MxROOP_AS>;
9696

9797
defm ROL : MxSROp<"rol", rotl, MxRODI_L, MxROOP_RO>;
9898
defm ROR : MxSROp<"ror", rotr, MxRODI_R, MxROOP_RO>;
99+
100+
101+
/// ---------------------------------------+---------
102+
/// F E D C B A 9 8 7 6 5 4 3 | 2 1 0
103+
/// ---------------------------------------+---------
104+
/// 0 1 0 0 1 0 0 0 0 1 0 0 0 | REG
105+
/// ---------------------------------------+---------
106+
107+
let isAsCheapAsAMove = 1, Defs = [CCR] in {
108+
let Constraints = "$src = $dst" in {
109+
def SWAP
110+
: MxInst<(outs MxDRD32:$dst), (ins MxDRD32:$src), "swap\t$src",
111+
[]> {
112+
let Inst = (descend 0b0100, 0b1000, 0b0100, 0b0, (operand "$src", 3));
113+
}
114+
} // Constraints
115+
} // Defs = [CCR]
116+
117+
def : Pat<(rotl i32:$src, (i32 16)), (SWAP MxDRD32:$src)>;
118+
def : Pat<(rotr i32:$src, (i32 16)), (SWAP MxDRD32:$src)>;

0 commit comments

Comments
 (0)