From afb1938c3955a45d90e681a0e51a8055a0f9bcf3 Mon Sep 17 00:00:00 2001 From: Tim Northover Date: Wed, 23 Jul 2014 13:59:12 +0000 Subject: [PATCH] ARM: spot SBFX-compatbile code expressed with sign_extend_inreg We were assuming all SBFX-like operations would have the shl/asr form, but often when the field being extracted is an i8 or i16, we end up with a SIGN_EXTEND_INREG acting on a shift instead. Simple enough to check for though. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213754 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/ARM/ARMISelDAGToDAG.cpp | 20 ++++++++++++++++++++ test/CodeGen/ARM/sbfx.ll | 18 ++++++++++++++++++ test/CodeGen/Thumb2/thumb2-sxt_rot.ll | 3 +-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/lib/Target/ARM/ARMISelDAGToDAG.cpp b/lib/Target/ARM/ARMISelDAGToDAG.cpp index 38547cfae2e..f41e4dfea72 100644 --- a/lib/Target/ARM/ARMISelDAGToDAG.cpp +++ b/lib/Target/ARM/ARMISelDAGToDAG.cpp @@ -2361,6 +2361,25 @@ SDNode *ARMDAGToDAGISel::SelectV6T2BitfieldExtractOp(SDNode *N, return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); } } + + if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) { + unsigned Width = cast(N->getOperand(1))->getVT().getSizeInBits(); + unsigned LSB = 0; + if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) && + !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB)) + return nullptr; + + if (LSB + Width > 32) + return nullptr; + + SDValue Reg0 = CurDAG->getRegister(0, MVT::i32); + SDValue Ops[] = { N->getOperand(0).getOperand(0), + CurDAG->getTargetConstant(LSB, MVT::i32), + CurDAG->getTargetConstant(Width - 1, MVT::i32), + getAL(CurDAG), Reg0 }; + return CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops); + } + return nullptr; } @@ -2509,6 +2528,7 @@ SDNode *ARMDAGToDAGISel::Select(SDNode *N) { if (SDNode *I = SelectV6T2BitfieldExtractOp(N, false)) return I; break; + case ISD::SIGN_EXTEND_INREG: case ISD::SRA: if (SDNode *I = SelectV6T2BitfieldExtractOp(N, true)) return I; diff --git a/test/CodeGen/ARM/sbfx.ll b/test/CodeGen/ARM/sbfx.ll index 3c25edcaa75..5b77c59bca9 100644 --- a/test/CodeGen/ARM/sbfx.ll +++ b/test/CodeGen/ARM/sbfx.ll @@ -45,3 +45,21 @@ entry: %tmp2 = ashr i32 %tmp, 1 ret i32 %tmp2 } + +define signext i8 @f6(i32 %a) { +; CHECK-LABEL: f6: +; CHECK: sbfx r0, r0, #23, #8 + + %tmp = lshr i32 %a, 23 + %res = trunc i32 %tmp to i8 + ret i8 %res +} + +define signext i8 @f7(i32 %a) { +; CHECK-LABEL: f7: +; CHECK-NOT: sbfx + + %tmp = lshr i32 %a, 25 + %res = trunc i32 %tmp to i8 + ret i8 %res +} diff --git a/test/CodeGen/Thumb2/thumb2-sxt_rot.ll b/test/CodeGen/Thumb2/thumb2-sxt_rot.ll index 03acee259d5..02a8c47ea48 100644 --- a/test/CodeGen/Thumb2/thumb2-sxt_rot.ll +++ b/test/CodeGen/Thumb2/thumb2-sxt_rot.ll @@ -10,8 +10,7 @@ define i32 @test0(i8 %A) { define signext i8 @test1(i32 %A) { ; CHECK-LABEL: test1: -; CHECK: lsrs r0, r0, #8 -; CHECK: sxtb r0, r0 +; CHECK: sbfx r0, r0, #8, #8 %B = lshr i32 %A, 8 %C = shl i32 %A, 24 %D = or i32 %B, %C