Index: llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h =================================================================== --- llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h +++ llvm/include/llvm/CodeGen/GlobalISel/LegalizerHelper.h @@ -237,6 +237,7 @@ LegalizeResult lowerExtract(MachineInstr &MI); LegalizeResult lowerInsert(MachineInstr &MI); LegalizeResult lowerSADDO_SSUBO(MachineInstr &MI); + LegalizeResult lowerBswap(MachineInstr &MI); private: MachineRegisterInfo &MRI; Index: llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp =================================================================== --- llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp +++ llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp @@ -1075,6 +1075,27 @@ MI.eraseFromParent(); return Legalized; } + case TargetOpcode::G_BSWAP: { + if (SizeOp0 % NarrowSize != 0) + return UnableToLegalize; + + Observer.changingInstr(MI); + SmallVector SrcRegs, DstRegs; + unsigned NumParts = SizeOp0 / NarrowSize; + extractParts(MI.getOperand(1).getReg(), NarrowTy, NumParts, SrcRegs); + + for (unsigned i = 0; i < NumParts; ++i) { + auto DstPart = MIRBuilder.buildInstr(MI.getOpcode(), {NarrowTy}, + {SrcRegs[NumParts - 1 - i]}); + DstRegs.push_back(DstPart.getReg(0)); + } + + MIRBuilder.buildMerge(MI.getOperand(0).getReg(), DstRegs); + + Observer.changedInstr(MI); + MI.eraseFromParent(); + return Legalized; + } } } @@ -2289,6 +2310,8 @@ return lowerExtract(MI); case G_INSERT: return lowerInsert(MI); + case G_BSWAP: + return lowerBswap(MI); } } @@ -4326,3 +4349,38 @@ MI.eraseFromParent(); return Legalized; } + +LegalizerHelper::LegalizeResult +LegalizerHelper::lowerBswap(MachineInstr &MI) { + Register Dst = MI.getOperand(0).getReg(); + Register Src = MI.getOperand(1).getReg(); + const LLT Ty = MRI.getType(Src); + unsigned SizeInBytes = Ty.getSizeInBytes(); + unsigned BaseShiftAmt = (SizeInBytes - 1) * 8; + + // Swap most and least significant byte, set remaining bytes in Res to zero. + auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt); + auto LSByteShiftedLeft = MIRBuilder.buildShl(Ty, Src, ShiftAmt); + auto MSByteShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt); + auto Res = MIRBuilder.buildOr(Ty, MSByteShiftedRight, LSByteShiftedLeft); + + // Set i-th high/low byte in Res to i-th low/high byte from Src. + for (unsigned i = 1; i < SizeInBytes / 2; ++i) { + // AND with Mask leaves byte i unchanged and sets remaining bytes to 0. + APInt APMask(SizeInBytes * 8, 0xFF << (i * 8)); + auto Mask = MIRBuilder.buildConstant(Ty, APMask); + auto ShiftAmt = MIRBuilder.buildConstant(Ty, BaseShiftAmt - 16 * i); + // Low byte shifted left to place of high byte: (Src & Mask) << ShiftAmt. + auto LoByte = MIRBuilder.buildAnd(Ty, Src, Mask); + auto LoShiftedLeft = MIRBuilder.buildShl(Ty, LoByte, ShiftAmt); + Res = MIRBuilder.buildOr(Ty, Res, LoShiftedLeft); + // High byte shifted right to place of low byte: (Src >> ShiftAmt) & Mask. + auto SrcShiftedRight = MIRBuilder.buildLShr(Ty, Src, ShiftAmt); + auto HiShiftedRight = MIRBuilder.buildAnd(Ty, SrcShiftedRight, Mask); + Res = MIRBuilder.buildOr(Ty, Res, HiShiftedRight); + } + Res.getInstr()->getOperand(0).setReg(Dst); + + MI.eraseFromParent(); + return Legalized; +} Index: llvm/lib/Target/Mips/MipsLegalizerInfo.cpp =================================================================== --- llvm/lib/Target/Mips/MipsLegalizerInfo.cpp +++ llvm/lib/Target/Mips/MipsLegalizerInfo.cpp @@ -184,6 +184,19 @@ getActionDefinitionsBuilder(G_VASTART) .legalFor({p0}); + getActionDefinitionsBuilder(G_BSWAP) + .legalIf([=, &ST](const LegalityQuery &Query) { + if (ST.hasMips32r2() && CheckTyN(0, Query, {s32})) + return true; + return false; + }) + .lowerIf([=, &ST](const LegalityQuery &Query) { + if (!ST.hasMips32r2() && CheckTyN(0, Query, {s32})) + return true; + return false; + }) + .maxScalar(0, s32); + // FP instructions getActionDefinitionsBuilder(G_FCONSTANT) .legalFor({s32, s64}); Index: llvm/lib/Target/Mips/MipsRegisterBankInfo.cpp =================================================================== --- llvm/lib/Target/Mips/MipsRegisterBankInfo.cpp +++ llvm/lib/Target/Mips/MipsRegisterBankInfo.cpp @@ -451,6 +451,7 @@ case G_LSHR: case G_BRINDIRECT: case G_VASTART: + case G_BSWAP: OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx]; break; case G_ADD: Index: llvm/test/CodeGen/Mips/GlobalISel/instruction-select/bswap.mir =================================================================== --- /dev/null +++ llvm/test/CodeGen/Mips/GlobalISel/instruction-select/bswap.mir @@ -0,0 +1,30 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -mtriple=mipsel-linux-gnu -run-pass=instruction-select -mattr=+mips32r2 -verify-machineinstrs %s -o - | FileCheck %s -check-prefixes=MIPS32R2 +--- | + + define void @bswap_i32() { entry: ret void } + +... +--- +name: bswap_i32 +alignment: 4 +legalized: true +regBankSelected: true +tracksRegLiveness: true +body: | + bb.1.entry: + liveins: $a0 + + ; MIPS32R2-LABEL: name: bswap_i32 + ; MIPS32R2: liveins: $a0 + ; MIPS32R2: [[COPY:%[0-9]+]]:gpr32 = COPY $a0 + ; MIPS32R2: [[WSBH:%[0-9]+]]:gpr32 = WSBH [[COPY]] + ; MIPS32R2: [[ROTR:%[0-9]+]]:gpr32 = ROTR [[WSBH]], 16 + ; MIPS32R2: $v0 = COPY [[ROTR]] + ; MIPS32R2: RetRA implicit $v0 + %0:gprb(s32) = COPY $a0 + %1:gprb(s32) = G_BSWAP %0 + $v0 = COPY %1(s32) + RetRA implicit $v0 + +... Index: llvm/test/CodeGen/Mips/GlobalISel/legalizer/bswap.mir =================================================================== --- /dev/null +++ llvm/test/CodeGen/Mips/GlobalISel/legalizer/bswap.mir @@ -0,0 +1,101 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -mtriple=mipsel-linux-gnu -run-pass=legalizer -verify-machineinstrs %s -o - | FileCheck %s -check-prefixes=MIPS32 +# RUN: llc -mtriple=mipsel-linux-gnu -run-pass=legalizer -mattr=+mips32r2 -verify-machineinstrs %s -o - | FileCheck %s -check-prefixes=MIPS32R2 +--- | + + define void @bswap_i32() { entry: ret void } + define void @bswap_i64() { entry: ret void } + +... +--- +name: bswap_i32 +alignment: 4 +tracksRegLiveness: true +body: | + bb.1.entry: + liveins: $a0 + + ; MIPS32-LABEL: name: bswap_i32 + ; MIPS32: liveins: $a0 + ; MIPS32: [[COPY:%[0-9]+]]:_(s32) = COPY $a0 + ; MIPS32: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 24 + ; MIPS32: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[COPY]], [[C]](s32) + ; MIPS32: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[COPY]], [[C]](s32) + ; MIPS32: [[OR:%[0-9]+]]:_(s32) = G_OR [[LSHR]], [[SHL]] + ; MIPS32: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 65280 + ; MIPS32: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 8 + ; MIPS32: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C1]] + ; MIPS32: [[SHL1:%[0-9]+]]:_(s32) = G_SHL [[AND]], [[C2]](s32) + ; MIPS32: [[OR1:%[0-9]+]]:_(s32) = G_OR [[OR]], [[SHL1]] + ; MIPS32: [[LSHR1:%[0-9]+]]:_(s32) = G_LSHR [[COPY]], [[C2]](s32) + ; MIPS32: [[AND1:%[0-9]+]]:_(s32) = G_AND [[LSHR1]], [[C1]] + ; MIPS32: [[OR2:%[0-9]+]]:_(s32) = G_OR [[OR1]], [[AND1]] + ; MIPS32: $v0 = COPY [[OR2]](s32) + ; MIPS32: RetRA implicit $v0 + ; MIPS32R2-LABEL: name: bswap_i32 + ; MIPS32R2: liveins: $a0 + ; MIPS32R2: [[COPY:%[0-9]+]]:_(s32) = COPY $a0 + ; MIPS32R2: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[COPY]] + ; MIPS32R2: $v0 = COPY [[BSWAP]](s32) + ; MIPS32R2: RetRA implicit $v0 + %0:_(s32) = COPY $a0 + %1:_(s32) = G_BSWAP %0 + $v0 = COPY %1(s32) + RetRA implicit $v0 + +... +--- +name: bswap_i64 +alignment: 4 +tracksRegLiveness: true +body: | + bb.1.entry: + liveins: $a0, $a1 + + ; MIPS32-LABEL: name: bswap_i64 + ; MIPS32: liveins: $a0, $a1 + ; MIPS32: [[COPY:%[0-9]+]]:_(s32) = COPY $a0 + ; MIPS32: [[COPY1:%[0-9]+]]:_(s32) = COPY $a1 + ; MIPS32: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 24 + ; MIPS32: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[COPY1]], [[C]](s32) + ; MIPS32: [[LSHR:%[0-9]+]]:_(s32) = G_LSHR [[COPY1]], [[C]](s32) + ; MIPS32: [[OR:%[0-9]+]]:_(s32) = G_OR [[LSHR]], [[SHL]] + ; MIPS32: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 65280 + ; MIPS32: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 8 + ; MIPS32: [[AND:%[0-9]+]]:_(s32) = G_AND [[COPY1]], [[C1]] + ; MIPS32: [[SHL1:%[0-9]+]]:_(s32) = G_SHL [[AND]], [[C2]](s32) + ; MIPS32: [[OR1:%[0-9]+]]:_(s32) = G_OR [[OR]], [[SHL1]] + ; MIPS32: [[LSHR1:%[0-9]+]]:_(s32) = G_LSHR [[COPY1]], [[C2]](s32) + ; MIPS32: [[AND1:%[0-9]+]]:_(s32) = G_AND [[LSHR1]], [[C1]] + ; MIPS32: [[OR2:%[0-9]+]]:_(s32) = G_OR [[OR1]], [[AND1]] + ; MIPS32: [[SHL2:%[0-9]+]]:_(s32) = G_SHL [[COPY]], [[C]](s32) + ; MIPS32: [[LSHR2:%[0-9]+]]:_(s32) = G_LSHR [[COPY]], [[C]](s32) + ; MIPS32: [[OR3:%[0-9]+]]:_(s32) = G_OR [[LSHR2]], [[SHL2]] + ; MIPS32: [[AND2:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C1]] + ; MIPS32: [[SHL3:%[0-9]+]]:_(s32) = G_SHL [[AND2]], [[C2]](s32) + ; MIPS32: [[OR4:%[0-9]+]]:_(s32) = G_OR [[OR3]], [[SHL3]] + ; MIPS32: [[LSHR3:%[0-9]+]]:_(s32) = G_LSHR [[COPY]], [[C2]](s32) + ; MIPS32: [[AND3:%[0-9]+]]:_(s32) = G_AND [[LSHR3]], [[C1]] + ; MIPS32: [[OR5:%[0-9]+]]:_(s32) = G_OR [[OR4]], [[AND3]] + ; MIPS32: $v0 = COPY [[OR2]](s32) + ; MIPS32: $v1 = COPY [[OR5]](s32) + ; MIPS32: RetRA implicit $v0, implicit $v1 + ; MIPS32R2-LABEL: name: bswap_i64 + ; MIPS32R2: liveins: $a0, $a1 + ; MIPS32R2: [[COPY:%[0-9]+]]:_(s32) = COPY $a0 + ; MIPS32R2: [[COPY1:%[0-9]+]]:_(s32) = COPY $a1 + ; MIPS32R2: [[BSWAP:%[0-9]+]]:_(s32) = G_BSWAP [[COPY1]] + ; MIPS32R2: [[BSWAP1:%[0-9]+]]:_(s32) = G_BSWAP [[COPY]] + ; MIPS32R2: $v0 = COPY [[BSWAP]](s32) + ; MIPS32R2: $v1 = COPY [[BSWAP1]](s32) + ; MIPS32R2: RetRA implicit $v0, implicit $v1 + %1:_(s32) = COPY $a0 + %2:_(s32) = COPY $a1 + %0:_(s64) = G_MERGE_VALUES %1(s32), %2(s32) + %3:_(s64) = G_BSWAP %0 + %4:_(s32), %5:_(s32) = G_UNMERGE_VALUES %3(s64) + $v0 = COPY %4(s32) + $v1 = COPY %5(s32) + RetRA implicit $v0, implicit $v1 + +... Index: llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/bswap.ll =================================================================== --- /dev/null +++ llvm/test/CodeGen/Mips/GlobalISel/llvm-ir/bswap.ll @@ -0,0 +1,68 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -O0 -mtriple=mipsel-linux-gnu -global-isel -verify-machineinstrs %s -o -| FileCheck %s -check-prefixes=MIPS32 +; RUN: llc -O0 -mtriple=mipsel-linux-gnu -global-isel -mattr=+mips32r2 -verify-machineinstrs %s -o -| FileCheck %s -check-prefixes=MIPS32R2 + +declare i32 @llvm.bswap.i32(i32) +define i32 @bswap_i32(i32 %x) { +; MIPS32-LABEL: bswap_i32: +; MIPS32: # %bb.0: # %entry +; MIPS32-NEXT: sll $1, $4, 24 +; MIPS32-NEXT: srl $2, $4, 24 +; MIPS32-NEXT: or $1, $2, $1 +; MIPS32-NEXT: andi $2, $4, 65280 +; MIPS32-NEXT: sll $2, $2, 8 +; MIPS32-NEXT: or $1, $1, $2 +; MIPS32-NEXT: srl $2, $4, 8 +; MIPS32-NEXT: andi $2, $2, 65280 +; MIPS32-NEXT: or $2, $1, $2 +; MIPS32-NEXT: jr $ra +; MIPS32-NEXT: nop +; +; MIPS32R2-LABEL: bswap_i32: +; MIPS32R2: # %bb.0: # %entry +; MIPS32R2-NEXT: wsbh $1, $4 +; MIPS32R2-NEXT: rotr $2, $1, 16 +; MIPS32R2-NEXT: jr $ra +; MIPS32R2-NEXT: nop +entry: + %0 = tail call i32 @llvm.bswap.i32(i32 %x) + ret i32 %0 +} + +declare i64 @llvm.bswap.i64(i64) +define i64 @bswap_i64(i64 %x) { +; MIPS32-LABEL: bswap_i64: +; MIPS32: # %bb.0: # %entry +; MIPS32-NEXT: sll $1, $5, 24 +; MIPS32-NEXT: srl $2, $5, 24 +; MIPS32-NEXT: or $1, $2, $1 +; MIPS32-NEXT: andi $2, $5, 65280 +; MIPS32-NEXT: sll $2, $2, 8 +; MIPS32-NEXT: or $1, $1, $2 +; MIPS32-NEXT: srl $2, $5, 8 +; MIPS32-NEXT: andi $2, $2, 65280 +; MIPS32-NEXT: or $2, $1, $2 +; MIPS32-NEXT: sll $1, $4, 24 +; MIPS32-NEXT: srl $3, $4, 24 +; MIPS32-NEXT: or $1, $3, $1 +; MIPS32-NEXT: andi $3, $4, 65280 +; MIPS32-NEXT: sll $3, $3, 8 +; MIPS32-NEXT: or $1, $1, $3 +; MIPS32-NEXT: srl $3, $4, 8 +; MIPS32-NEXT: andi $3, $3, 65280 +; MIPS32-NEXT: or $3, $1, $3 +; MIPS32-NEXT: jr $ra +; MIPS32-NEXT: nop +; +; MIPS32R2-LABEL: bswap_i64: +; MIPS32R2: # %bb.0: # %entry +; MIPS32R2-NEXT: wsbh $1, $5 +; MIPS32R2-NEXT: rotr $2, $1, 16 +; MIPS32R2-NEXT: wsbh $1, $4 +; MIPS32R2-NEXT: rotr $3, $1, 16 +; MIPS32R2-NEXT: jr $ra +; MIPS32R2-NEXT: nop +entry: + %0 = tail call i64 @llvm.bswap.i64(i64 %x) + ret i64 %0 +} Index: llvm/test/CodeGen/Mips/GlobalISel/regbankselect/bswap.mir =================================================================== --- /dev/null +++ llvm/test/CodeGen/Mips/GlobalISel/regbankselect/bswap.mir @@ -0,0 +1,28 @@ +# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py +# RUN: llc -mtriple=mipsel-linux-gnu -run-pass=regbankselect -mattr=+mips32r2 -verify-machineinstrs %s -o - | FileCheck %s -check-prefixes=MIPS32R2 +--- | + + define void @bswap_i32() { entry: ret void } + +... +--- +name: bswap_i32 +alignment: 4 +legalized: true +tracksRegLiveness: true +body: | + bb.1.entry: + liveins: $a0 + + ; MIPS32R2-LABEL: name: bswap_i32 + ; MIPS32R2: liveins: $a0 + ; MIPS32R2: [[COPY:%[0-9]+]]:gprb(s32) = COPY $a0 + ; MIPS32R2: [[BSWAP:%[0-9]+]]:gprb(s32) = G_BSWAP [[COPY]] + ; MIPS32R2: $v0 = COPY [[BSWAP]](s32) + ; MIPS32R2: RetRA implicit $v0 + %0:_(s32) = COPY $a0 + %1:_(s32) = G_BSWAP %0 + $v0 = COPY %1(s32) + RetRA implicit $v0 + +...