public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH, AArch64] Use MOVN to generate 64-bit negative immediates where sensible
@ 2014-05-08 17:36 Ian Bolton
  2014-05-16  9:17 ` Ian Bolton
  2014-05-16 12:35 ` Richard Earnshaw
  0 siblings, 2 replies; 10+ messages in thread
From: Ian Bolton @ 2014-05-08 17:36 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1030 bytes --]

Hi,

It currently takes 4 instructions to generate certain immediates on
AArch64 (unless we put them in the constant pool).

For example ...

  long long
  ffffbeefcafebabe ()
  {
    return 0xFFFFBEEFCAFEBABEll;
  }

leads to ...

  mov x0, 0x47806
  mov x0, 0xcafe, lsl 16
  mov x0, 0xbeef, lsl 32
  orr x0, x0, -281474976710656

The above case is tackled in this patch by employing MOVN
to generate the top 32-bits in a single instruction ...

  mov x0, -71536975282177
  movk x0, 0xcafe, lsl 16
  movk x0, 0xbabe, lsl 0

Note that where at least two half-words are 0xffff, existing
code that does the immediate in two instructions is still used.)

Tested on standard gcc regressions and the attached test case.

OK for commit?

Cheers,
Ian


2014-05-08  Ian Bolton  <ian.bolton@arm.com>

gcc/
	* config/aarch64/aarch64.c (aarch64_expand_mov_immediate):
	Use MOVN when top-most half-word (and only that half-word)
	is 0xffff.
gcc/testsuite/
	* gcc.target/aarch64/movn_1.c: New test.

[-- Attachment #2: aarch64-movn-exploitation-patch-v5.txt --]
[-- Type: text/plain, Size: 1576 bytes --]

diff --git a/gcc/config/aarch64/aarch64.c b/gcc/config/aarch64/aarch64.c
index 43a83566..a8e504e 100644
--- a/gcc/config/aarch64/aarch64.c
+++ b/gcc/config/aarch64/aarch64.c
@@ -1177,6 +1177,18 @@ aarch64_expand_mov_immediate (rtx dest, rtx imm)
 	}
     }
 
+  /* Look for case where upper 16 bits are set, so we can use MOVN.  */
+  if ((val & 0xffff000000000000ll) == 0xffff000000000000ll)
+    {
+      emit_insn (gen_rtx_SET (VOIDmode, dest,
+			      GEN_INT (~ (~val & (0xffffll << 32)))));
+      emit_insn (gen_insv_immdi (dest, GEN_INT (16),
+				 GEN_INT ((val >> 16) & 0xffff)));
+      emit_insn (gen_insv_immdi (dest, GEN_INT (0),
+				 GEN_INT (val & 0xffff)));
+      return;
+    }
+
  simple_sequence:
   first = true;
   mask = 0xffff;
diff --git a/gcc/testsuite/gcc.target/aarch64/movn_1.c b/gcc/testsuite/gcc.target/aarch64/movn_1.c
new file mode 100644
index 0000000..cc11ade
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/movn_1.c
@@ -0,0 +1,27 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-inline --save-temps" } */
+
+extern void abort (void);
+
+long long
+foo ()
+{
+  /* { dg-final { scan-assembler "mov\tx\[0-9\]+, -71536975282177" } } */
+  return 0xffffbeefcafebabell;
+}
+
+long long
+merge4 (int a, int b, int c, int d)
+{
+  return ((long long) a << 48 | (long long) b << 32
+	  | (long long) c << 16 | (long long) d);
+}
+
+int main ()
+{
+  if (foo () != merge4 (0xffff, 0xbeef, 0xcafe, 0xbabe))
+    abort ();
+  return 0;
+}
+
+/* { dg-final { cleanup-saved-temps } } */

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-08-14 17:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-08 17:36 [PATCH, AArch64] Use MOVN to generate 64-bit negative immediates where sensible Ian Bolton
2014-05-16  9:17 ` Ian Bolton
2014-05-16 12:35 ` Richard Earnshaw
2014-08-07 11:32   ` Kyrill Tkachov
2014-08-07 12:46     ` Richard Earnshaw
2014-08-07 12:57       ` Kyrill Tkachov
2014-08-07 13:31         ` Richard Earnshaw
2014-08-07 19:33         ` Richard Henderson
2014-08-13 15:30           ` Kyrill Tkachov
2014-08-14 17:31             ` Richard Henderson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).