From 8922077b90a357b03206d635eea28981f27587ca Mon Sep 17 00:00:00 2001 From: Zoran Jovanovic Date: Fri, 29 Jan 2016 16:18:34 +0000 Subject: [PATCH] [mips] Absolute value macro expansion Author: obucina Reviewers: dsanders Differential Revision: http://reviews.llvm.org/D16323 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@259202 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/Mips/AsmParser/MipsAsmParser.cpp | 22 ++++++++++++++++++++++ lib/Target/Mips/MipsInstrInfo.td | 3 +++ test/MC/Mips/macro-abs.s | 12 ++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 test/MC/Mips/macro-abs.s diff --git a/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/lib/Target/Mips/AsmParser/MipsAsmParser.cpp index a13d1d794ab..192b3444936 100644 --- a/lib/Target/Mips/AsmParser/MipsAsmParser.cpp +++ b/lib/Target/Mips/AsmParser/MipsAsmParser.cpp @@ -233,6 +233,9 @@ class MipsAsmParser : public MCTargetAsmParser { bool expandDRotationImm(MCInst &Inst, SMLoc IDLoc, SmallVectorImpl &Instructions); + bool expandAbs(MCInst &Inst, SMLoc IDLoc, + SmallVectorImpl &Instructions); + void createNop(bool hasShortDelaySlot, SMLoc IDLoc, SmallVectorImpl &Instructions); @@ -2087,6 +2090,9 @@ MipsAsmParser::tryExpandInstruction(MCInst &Inst, SMLoc IDLoc, case Mips::DRORImm: return expandDRotationImm(Inst, IDLoc, Instructions) ? MER_Fail : MER_Success; + case Mips::ABSMacro: + return expandAbs(Inst, IDLoc, Instructions) ? MER_Fail + : MER_Success; } } @@ -3531,6 +3537,22 @@ bool MipsAsmParser::expandDRotationImm(MCInst &Inst, SMLoc IDLoc, return true; } +bool MipsAsmParser::expandAbs(MCInst &Inst, SMLoc IDLoc, + SmallVectorImpl &Instructions) { + + unsigned FirstRegOp = Inst.getOperand(0).getReg(); + unsigned SecondRegOp = Inst.getOperand(1).getReg(); + + emitRI(Mips::BGEZ, SecondRegOp, 8, IDLoc, Instructions); + if (FirstRegOp != SecondRegOp) + emitRRR(Mips::ADDu, FirstRegOp, SecondRegOp, Mips::ZERO, IDLoc, Instructions); + else + createNop(false, IDLoc, Instructions); + emitRRR(Mips::SUB, FirstRegOp, Mips::ZERO, SecondRegOp, IDLoc, Instructions); + + return false; +} + void MipsAsmParser::createNop(bool hasShortDelaySlot, SMLoc IDLoc, SmallVectorImpl &Instructions) { if (hasShortDelaySlot) diff --git a/lib/Target/Mips/MipsInstrInfo.td b/lib/Target/Mips/MipsInstrInfo.td index ffda491f0c8..c5a314fe2d8 100644 --- a/lib/Target/Mips/MipsInstrInfo.td +++ b/lib/Target/Mips/MipsInstrInfo.td @@ -1808,6 +1808,9 @@ def : MipsInstAlias<"dror $rd, $rs", def : MipsInstAlias<"dror $rd, $imm", (DRORImm GPR32Opnd:$rd, GPR32Opnd:$rd, simm16:$imm), 0>, ISA_MIPS64; +def ABSMacro : MipsAsmPseudoInst<(outs GPR32Opnd:$rd), (ins GPR32Opnd:$rs), + "abs\t$rd, $rs">; + //===----------------------------------------------------------------------===// // Instruction aliases //===----------------------------------------------------------------------===// diff --git a/test/MC/Mips/macro-abs.s b/test/MC/Mips/macro-abs.s new file mode 100644 index 00000000000..2fa7b48901c --- /dev/null +++ b/test/MC/Mips/macro-abs.s @@ -0,0 +1,12 @@ +# RUN: llvm-mc -triple mips-unknown-linux -show-encoding %s | FileCheck %s + +.text +# CHECK: .text + abs $4, $4 +# CHECK: bgez $4, 8 # encoding: [0x04,0x81,0x00,0x02] +# CHECK: nop # encoding: [0x00,0x00,0x00,0x00] +# CHECK: neg $4, $4 # encoding: [0x00,0x04,0x20,0x22] + abs $4, $5 +# CHECK: bgez $5, 8 # encoding: [0x04,0xa1,0x00,0x02] +# CHECK: move $4, $5 # encoding: [0x00,0xa0,0x20,0x21] +# CHECK: neg $4, $5 # encoding: [0x00,0x05,0x20,0x22] -- 2.11.0