From 611eb829532da35c19f6ac85818eae06e5534b5e Mon Sep 17 00:00:00 2001 From: Daniel Sanders Date: Mon, 29 Feb 2016 15:26:54 +0000 Subject: [PATCH] [mips] Make isel select the correct DEXT variant up front. Summary: Previously, it would always select DEXT and substitute any invalid matches for DEXTU/DEXTM during MipsMCCodeEmitter::encodeInstruction(). This works but causes problems when adding range checked immediates to IAS. Now isel selects the correct variant up front. Reviewers: vkalintiris Subscribers: dsanders, llvm-commits Differential Revision: http://reviews.llvm.org/D16810 llvm-svn: 262229 --- .../Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp | 29 ++++++++-------------- llvm/lib/Target/Mips/MicroMips64r6InstrInfo.td | 2 +- llvm/lib/Target/Mips/MicroMipsInstrInfo.td | 6 ++--- llvm/lib/Target/Mips/Mips64InstrInfo.td | 15 ++++++----- llvm/lib/Target/Mips/MipsInstrInfo.td | 26 ++++++++++++++++--- llvm/test/CodeGen/Mips/fcopysign-f32-f64.ll | 2 +- llvm/test/CodeGen/Mips/fcopysign.ll | 2 +- llvm/test/CodeGen/Mips/mips64extins.ll | 10 ++++++-- llvm/test/MC/Mips/micromips64r6/invalid.s | 4 +-- llvm/test/MC/Mips/mips64r2/invalid.s | 4 +-- 10 files changed, 58 insertions(+), 42 deletions(-) diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp index 4b030eb..d043bdf 100644 --- a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp +++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp @@ -81,16 +81,10 @@ static void LowerLargeShift(MCInst& Inst) { } } -// Pick a DEXT or DINS instruction variant based on the pos and size operands -static void LowerDextDins(MCInst& InstIn) { - int Opcode = InstIn.getOpcode(); - - if (Opcode == Mips::DEXT) - assert(InstIn.getNumOperands() == 4 && - "Invalid no. of machine operands for DEXT!"); - else // Only DEXT and DINS are possible - assert(InstIn.getNumOperands() == 5 && - "Invalid no. of machine operands for DINS!"); +// Pick a DINS instruction variant based on the pos and size operands +static void LowerDins(MCInst& InstIn) { + assert(InstIn.getNumOperands() == 5 && + "Invalid no. of machine operands for DINS!"); assert(InstIn.getOperand(2).isImm()); int64_t pos = InstIn.getOperand(2).getImm(); @@ -98,17 +92,17 @@ static void LowerDextDins(MCInst& InstIn) { int64_t size = InstIn.getOperand(3).getImm(); if (size <= 32) { - if (pos < 32) // DEXT/DINS, do nothing + if (pos < 32) // DINS, do nothing return; - // DEXTU/DINSU + // DINSU InstIn.getOperand(2).setImm(pos - 32); - InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU); + InstIn.setOpcode(Mips::DINSU); return; } - // DEXTM/DINSM - assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32"); + // DINSM + assert(pos < 32 && "DINS cannot have both size and pos > 32"); InstIn.getOperand(3).setImm(size - 32); - InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM); + InstIn.setOpcode(Mips::DINSM); return; } @@ -164,9 +158,8 @@ encodeInstruction(const MCInst &MI, raw_ostream &OS, LowerLargeShift(TmpInst); break; // Double extract instruction is chosen by pos and size operands - case Mips::DEXT: case Mips::DINS: - LowerDextDins(TmpInst); + LowerDins(TmpInst); } unsigned long N = Fixups.size(); diff --git a/llvm/lib/Target/Mips/MicroMips64r6InstrInfo.td b/llvm/lib/Target/Mips/MicroMips64r6InstrInfo.td index ffbcf40..96cbd10 100644 --- a/llvm/lib/Target/Mips/MicroMips64r6InstrInfo.td +++ b/llvm/lib/Target/Mips/MicroMips64r6InstrInfo.td @@ -71,7 +71,7 @@ class EXTBITS_DESC_BASE; class DEXTM_MMR6_DESC : EXTBITS_DESC_BASE<"dextm", GPR64Opnd, uimm5, uimm5_plus33, MipsExt>; diff --git a/llvm/lib/Target/Mips/MicroMipsInstrInfo.td b/llvm/lib/Target/Mips/MicroMipsInstrInfo.td index e441e11..6c0de02 100644 --- a/llvm/lib/Target/Mips/MicroMipsInstrInfo.td +++ b/llvm/lib/Target/Mips/MicroMipsInstrInfo.td @@ -845,10 +845,10 @@ let DecoderNamespace = "MicroMips", Predicates = [InMicroMips] in { def WSBH_MM : MMRel, SubwordSwap<"wsbh", GPR32Opnd, II_WSBH>, SEB_FM_MM<0x1ec>, ISA_MIPS32R2; // TODO: Add '0 < pos+size <= 32' constraint check to ext instruction - def EXT_MM : MMRel, ExtBase<"ext", GPR32Opnd, uimm5, uimm5_plus1, - MipsExt>, EXT_FM_MM<0x2c>; + def EXT_MM : MMRel, ExtBase<"ext", GPR32Opnd, uimm5, uimm5_plus1, immZExt5, + immZExt5Plus1, MipsExt>, EXT_FM_MM<0x2c>; def INS_MM : MMRel, InsBase<"ins", GPR32Opnd, uimm5, uimm5_inssize_plus1, - MipsIns>, EXT_FM_MM<0x0c>; + MipsIns>, EXT_FM_MM<0x0c>; /// Jump Instructions let DecoderMethod = "DecodeJumpTargetMM" in { diff --git a/llvm/lib/Target/Mips/Mips64InstrInfo.td b/llvm/lib/Target/Mips/Mips64InstrInfo.td index d5d794d..e0a31c8 100644 --- a/llvm/lib/Target/Mips/Mips64InstrInfo.td +++ b/llvm/lib/Target/Mips/Mips64InstrInfo.td @@ -268,15 +268,14 @@ let isCodeGenOnly = 1 in def RDHWR64 : ReadHardware, RDHWR_FM; let AdditionalPredicates = [NotInMicroMips] in { - // TODO: Add 'pos + size' constraint check to dext* instructions - // DEXT: 0 < pos + size <= 63 - // DEXTM, DEXTU: 32 < pos + size <= 64 - def DEXT : ExtBase<"dext", GPR64Opnd, uimm5, uimm5_plus1, MipsExt>, - EXT_FM<3>; - def DEXTM : ExtBase<"dextm", GPR64Opnd, uimm5, uimm5_plus33, MipsExt>, - EXT_FM<1>; + // The 'pos + size' constraints are enforced by the code that lowers into + // MipsISD::Ext. + def DEXT : ExtBase<"dext", GPR64Opnd, uimm5_report_uimm6, uimm5_plus1, + immZExt5, immZExt5Plus1, MipsExt>, EXT_FM<3>; + def DEXTM : ExtBase<"dextm", GPR64Opnd, uimm5, uimm5_plus33, immZExt5, + immZExt5Plus33, MipsExt>, EXT_FM<1>; def DEXTU : ExtBase<"dextu", GPR64Opnd, uimm5_plus32, uimm5_plus1, - MipsExt>, EXT_FM<2>; + immZExt5Plus32, immZExt5Plus1, MipsExt>, EXT_FM<2>; def DINS : InsBase<"dins", GPR64Opnd, uimm6, uimm5_inssize_plus1, MipsIns>, EXT_FM<7>; def DINSU : InsBase<"dinsu", GPR64Opnd, uimm5_plus32, uimm5_inssize_plus1>, diff --git a/llvm/lib/Target/Mips/MipsInstrInfo.td b/llvm/lib/Target/Mips/MipsInstrInfo.td index d120250e..ea4fa50 100644 --- a/llvm/lib/Target/Mips/MipsInstrInfo.td +++ b/llvm/lib/Target/Mips/MipsInstrInfo.td @@ -653,6 +653,13 @@ def uimm16_64_relaxed : Operand { !cast("UImm16RelaxedAsmOperandClass"); } +// Like uimm5 but reports a less confusing error for 32-63 when +// an instruction alias permits that. +def uimm5_report_uimm6 : Operand { + let PrintMethod = "printUnsignedImm"; + let ParserMatchClass = ConstantUImm5ReportUImm6AsmOperandClass; +} + // Like uimm5_64 but reports a less confusing error for 32-63 when // an instruction alias permits that. def uimm5_64_report_uimm6 : Operand { @@ -825,6 +832,16 @@ def immLow16Zero : PatLeaf<(imm), [{ // shamt field must fit in 5 bits. def immZExt5 : ImmLeaf; +def immZExt5Plus1 : PatLeaf<(imm), [{ + return isUInt<5>(N->getZExtValue() - 1); +}]>; +def immZExt5Plus32 : PatLeaf<(imm), [{ + return isUInt<5>(N->getZExtValue() - 32); +}]>; +def immZExt5Plus33 : PatLeaf<(imm), [{ + return isUInt<5>(N->getZExtValue() - 33); +}]>; + // True if (N + 1) fits in 16-bit field. def immSExt16Plus1 : PatLeaf<(imm), [{ return isInt<17>(N->getSExtValue()) && isInt<16>(N->getSExtValue() + 1); @@ -1273,10 +1290,11 @@ class ReadHardware : // Ext and Ins class ExtBase : + Operand SizeOpnd, PatFrag PosImm, PatFrag SizeImm, + SDPatternOperator Op = null_frag> : InstSE<(outs RO:$rt), (ins RO:$rs, PosOpnd:$pos, SizeOpnd:$size), !strconcat(opstr, " $rt, $rs, $pos, $size"), - [(set RO:$rt, (Op RO:$rs, imm:$pos, imm:$size))], II_EXT, + [(set RO:$rt, (Op RO:$rs, PosImm:$pos, SizeImm:$size))], II_EXT, FrmR, opstr>, ISA_MIPS32R2; class InsBase, RDHWR_FM; } // TODO: Add '0 < pos+size <= 32' constraint check to ext instruction -def EXT : MMRel, ExtBase<"ext", GPR32Opnd, uimm5, uimm5_plus1, MipsExt>, - EXT_FM<0>; +def EXT : MMRel, ExtBase<"ext", GPR32Opnd, uimm5, uimm5_plus1, immZExt5, + immZExt5Plus1, MipsExt>, EXT_FM<0>; def INS : MMRel, InsBase<"ins", GPR32Opnd, uimm5, uimm5_inssize_plus1, MipsIns>, EXT_FM<4>; diff --git a/llvm/test/CodeGen/Mips/fcopysign-f32-f64.ll b/llvm/test/CodeGen/Mips/fcopysign-f32-f64.ll index 860bc79..511487e 100644 --- a/llvm/test/CodeGen/Mips/fcopysign-f32-f64.ll +++ b/llvm/test/CodeGen/Mips/fcopysign-f32-f64.ll @@ -18,7 +18,7 @@ entry: ; 64: or $[[OR:[0-9]+]], $[[AND0]], $[[SLL1]] ; 64: mtc1 $[[OR]], $f0 -; 64R2: dext ${{[0-9]+}}, ${{[0-9]+}}, 63, 1 +; 64R2: dextu ${{[0-9]+}}, ${{[0-9]+}}, 63, 1 ; 64R2: ins $[[INS:[0-9]+]], ${{[0-9]+}}, 31, 1 ; 64R2: mtc1 $[[INS]], $f0 diff --git a/llvm/test/CodeGen/Mips/fcopysign.ll b/llvm/test/CodeGen/Mips/fcopysign.ll index 6928f2f..ffc72a1 100644 --- a/llvm/test/CodeGen/Mips/fcopysign.ll +++ b/llvm/test/CodeGen/Mips/fcopysign.ll @@ -27,7 +27,7 @@ entry: ; 64: or $[[OR:[0-9]+]], $[[AND0]], $[[AND1]] ; 64: dmtc1 $[[OR]], $f0 -; 64R2: dext $[[EXT:[0-9]+]], ${{[0-9]+}}, 63, 1 +; 64R2: dextu $[[EXT:[0-9]+]], ${{[0-9]+}}, 63, 1 ; 64R2: dins $[[INS:[0-9]+]], $[[EXT]], 63, 1 ; 64R2: dmtc1 $[[INS]], $f0 diff --git a/llvm/test/CodeGen/Mips/mips64extins.ll b/llvm/test/CodeGen/Mips/mips64extins.ll index 211cd5f..bf68bbd 100644 --- a/llvm/test/CodeGen/Mips/mips64extins.ll +++ b/llvm/test/CodeGen/Mips/mips64extins.ll @@ -2,6 +2,7 @@ define i64 @dext(i64 %i) nounwind readnone { entry: +; CHECK-LABEL: dext: ; CHECK: dext ${{[0-9]+}}, ${{[0-9]+}}, 5, 10 %shr = lshr i64 %i, 5 %and = and i64 %shr, 1023 @@ -10,7 +11,8 @@ entry: define i64 @dextm(i64 %i) nounwind readnone { entry: -; CHECK: dext ${{[0-9]+}}, ${{[0-9]+}}, 5, 34 +; CHECK-LABEL: dextm: +; CHECK: dextm ${{[0-9]+}}, ${{[0-9]+}}, 5, 34 %shr = lshr i64 %i, 5 %and = and i64 %shr, 17179869183 ret i64 %and @@ -18,7 +20,8 @@ entry: define i64 @dextu(i64 %i) nounwind readnone { entry: -; CHECK: dext ${{[0-9]+}}, ${{[0-9]+}}, 34, 6 +; CHECK-LABEL: dextu: +; CHECK: dextu ${{[0-9]+}}, ${{[0-9]+}}, 34, 6 %shr = lshr i64 %i, 34 %and = and i64 %shr, 63 ret i64 %and @@ -26,6 +29,7 @@ entry: define i64 @dins(i64 %i, i64 %j) nounwind readnone { entry: +; CHECK-LABEL: dins: ; CHECK: dins ${{[0-9]+}}, ${{[0-9]+}}, 8, 10 %shl2 = shl i64 %j, 8 %and = and i64 %shl2, 261888 @@ -36,6 +40,7 @@ entry: define i64 @dinsm(i64 %i, i64 %j) nounwind readnone { entry: +; CHECK-LABEL: dinsm: ; CHECK: dins ${{[0-9]+}}, ${{[0-9]+}}, 10, 33 %shl4 = shl i64 %j, 10 %and = and i64 %shl4, 8796093021184 @@ -46,6 +51,7 @@ entry: define i64 @dinsu(i64 %i, i64 %j) nounwind readnone { entry: +; CHECK-LABEL: dinsu: ; CHECK: dins ${{[0-9]+}}, ${{[0-9]+}}, 40, 13 %shl4 = shl i64 %j, 40 %and = and i64 %shl4, 9006099743113216 diff --git a/llvm/test/MC/Mips/micromips64r6/invalid.s b/llvm/test/MC/Mips/micromips64r6/invalid.s index c9f5d85..c42d204 100644 --- a/llvm/test/MC/Mips/micromips64r6/invalid.s +++ b/llvm/test/MC/Mips/micromips64r6/invalid.s @@ -19,8 +19,8 @@ cache -1, 255($7) # CHECK: :[[@LINE]]:9: error: expected 5-bit unsigned immediate cache 32, 255($7) # CHECK: :[[@LINE]]:9: error: expected 5-bit unsigned immediate # FIXME: Check various 'pos + size' constraints on dext* - dext $2, $3, -1, 1 # CHECK: :[[@LINE]]:16: error: expected 5-bit unsigned immediate - dext $2, $3, 32, 1 # CHECK: :[[@LINE]]:16: error: expected 5-bit unsigned immediate + dext $2, $3, -1, 1 # CHECK: :[[@LINE]]:16: error: expected 6-bit unsigned immediate + dext $2, $3, 64, 1 # CHECK: :[[@LINE]]:16: error: expected 6-bit unsigned immediate dext $2, $3, 1, 0 # CHECK: :[[@LINE]]:19: error: expected immediate in range 1 .. 32 dext $2, $3, 1, 33 # CHECK: :[[@LINE]]:19: error: expected immediate in range 1 .. 32 dextm $2, $3, -1, 1 # CHECK: :[[@LINE]]:17: error: expected 5-bit unsigned immediate diff --git a/llvm/test/MC/Mips/mips64r2/invalid.s b/llvm/test/MC/Mips/mips64r2/invalid.s index 86bfc95..8e29029 100644 --- a/llvm/test/MC/Mips/mips64r2/invalid.s +++ b/llvm/test/MC/Mips/mips64r2/invalid.s @@ -9,8 +9,8 @@ cache -1, 255($7) # CHECK: :[[@LINE]]:15: error: expected 5-bit unsigned immediate cache 32, 255($7) # CHECK: :[[@LINE]]:15: error: expected 5-bit unsigned immediate # FIXME: Check various 'pos + size' constraints on dext* - dext $2, $3, -1, 1 # CHECK: :[[@LINE]]:22: error: expected 5-bit unsigned immediate - dext $2, $3, 32, 1 # CHECK: :[[@LINE]]:22: error: expected 5-bit unsigned immediate + dext $2, $3, -1, 1 # CHECK: :[[@LINE]]:22: error: expected 6-bit unsigned immediate + dext $2, $3, 64, 1 # CHECK: :[[@LINE]]:22: error: expected 6-bit unsigned immediate dext $2, $3, 1, 0 # CHECK: :[[@LINE]]:25: error: expected immediate in range 1 .. 32 dext $2, $3, 1, 33 # CHECK: :[[@LINE]]:25: error: expected immediate in range 1 .. 32 dextm $2, $3, -1, 1 # CHECK: :[[@LINE]]:23: error: expected 5-bit unsigned immediate -- 2.7.4