From 37741dd6f8e3f1283001242b25f82b6a7600411d Mon Sep 17 00:00:00 2001
From: David Holsgrove <david.holsgrove@xilinx.com>
Date: Thu, 23 May 2013 10:51:32 +1000
Subject: [PATCH] [Patch, microblaze]: Fix bswaphi2 implementation

MicroBlaze insn swaph swaps the contents of register rA
as two halfwords placing result in rD;

(rD)[0:15]  <- (rA)[16:31]
(rD)[16:31] <- (rA)[0:15]

gcc bswaphi2 is intended to reverse the order of the bytes
in the half integer in rA

(rD)[8:15]  <- (rA)[0:7]
(rD)[7:0]   <- (rA)[8:15]
(rD)[24:31] <- (rA)[16:23]
(rD)[16:23] <- (rA)[24:31]

Correct microblaze bswaphi2 insn pattern to be a
swapb followed by swaph.

Correct bswapsi2 insn pattern to use tab and no space between
registers for better formatting.

Changelog

2013-11-26  David Holsgrove <david.holsgrove@xilinx.com>

 * gcc/config/microblaze/microblaze.md: Correct bswaphi2 / bswapsi2 insn

ChangeLog/testsuite

2013-11-26  David Holsgrove <david.holsgrove@xilinx.com>

 * gcc/testsuite/gcc.target/microblaze/others/bswap16.c: New test.

Reported-by: Nathan Rossi <nathan.rossi@xilinx.com>
Signed-off-by: David Holsgrove <david.holsgrove@xilinx.com>
---
 gcc/config/microblaze/microblaze.md                |    4 ++--
 .../gcc.target/microblaze/others/bswap16.c         |   14 ++++++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/microblaze/others/bswap16.c

diff --git a/gcc/config/microblaze/microblaze.md b/gcc/config/microblaze/microblaze.md
index 8d2a362..4b0ca1a 100644
--- a/gcc/config/microblaze/microblaze.md
+++ b/gcc/config/microblaze/microblaze.md
@@ -358,14 +358,14 @@
   [(set (match_operand:SI 0 "register_operand" "=r")
         (bswap:SI (match_operand:SI 1 "register_operand" "r")))]
   "TARGET_REORDER"
-  "swapb %0, %1"
+  "swapb\t%0,%1"
 )
 
 (define_insn "bswaphi2"
   [(set (match_operand:HI 0 "register_operand" "=r")
         (bswap:HI (match_operand:HI 1 "register_operand" "r")))]
   "TARGET_REORDER"
-  "swaph %0, %1"
+  "swapb\t%0,%1\;swaph\t%0,%0"
 )
 
 ;;----------------------------------------------------------------
diff --git a/gcc/testsuite/gcc.target/microblaze/others/bswap16.c b/gcc/testsuite/gcc.target/microblaze/others/bswap16.c
new file mode 100644
index 0000000..c1364d9
--- /dev/null
+++ b/gcc/testsuite/gcc.target/microblaze/others/bswap16.c
@@ -0,0 +1,14 @@
+/* { dg-options "-O3 -mcpu=v8.30.b" } */
+
+unsigned short swapu16_1 (unsigned short x)
+{
+  return (x << 8) | (x >> 8);
+}
+
+unsigned short swapu16_2 (unsigned short x)
+{
+  return (x >> 8) | (x << 8);
+}
+
+/* { dg-final { scan-assembler "swapb\t" } } */
+/* { dg-final { scan-assembler "swaph\t" } } */
\ No newline at end of file
-- 
1.7.9.5

