diff mbox

[ARM] PR 65694: Properly sign-extend large numbers before passing to GEN_INT in arm_canonicalize_comparison

Message ID 55278AC7.9070807@arm.com
State New
Headers show

Commit Message

Kyrylo Tkachov April 10, 2015, 8:33 a.m. UTC
Hi all,

This ICE has a similar cause to PR 64600.
The arm backend creates a const_int with a large value and doesn't properly sign-extend it.
If that rtx then happens to pass through the simplify-rtx machinery and ends up in wide-int land the asserts there catch it and ICE. In this case it was 0x7fffffff having 1 added to it to make
0x80000000 which should have been sign-extended to 0xffffffff80000000.

The code that caused this is:
class G {
public:
   void allocate(int p1) {
     if (p1 > max_size())
       operator new(sizeof(short));
   }
   unsigned max_size() { return -1 / sizeof(short); }
};


but it also needs to go through simplify-rtx to ICE, so the
testcase contains the context as well.

Similarly, the solution here is to use ARM_SIGN_EXTEND on the
SImode constants and trunc_int_for_mode for the DImode ones.

Bootstrapped and tested on arm-none-linux-gnueabihf.
Built SPEC2006 with it.
No code changes anywhere except in one instruction where a:
   cmp     r6, #2147483648

is transformed into a:
   cmp     r6, #-2147483648

Both assemble to the same thing, the disassembly is:
   cmp.w   r6, #2147483648 ; 0x80000000


Ok for trunk?

Thanks,
Kyrill

2015-04-09  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     * config/arm/arm.c (arm_canonicalize_comparison): Use ARM_SIGN_EXTEND
     when creating +1 values for SImode and trunc_int_for_mode for similar
     DImode operations.

2015-04-09  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>

     * g++.dg/torture/pr65694.C: New test.

Comments

Jakub Jelinek April 10, 2015, 8:57 a.m. UTC | #1
On Fri, Apr 10, 2015 at 09:33:11AM +0100, Kyrill Tkachov wrote:
> 2015-04-09  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
> 

Missing
	PR target/65694
line here.

>     * config/arm/arm.c (arm_canonicalize_comparison): Use ARM_SIGN_EXTEND
>     when creating +1 values for SImode and trunc_int_for_mode for similar
>     DImode operations.
> 
> 2015-04-09  Kyrylo Tkachov  <kyrylo.tkachov@arm.com>
> 

Ditto.

>     * g++.dg/torture/pr65694.C: New test.

> diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
> index 369cb67..5342b33 100644
> --- a/gcc/config/arm/arm.c
> +++ b/gcc/config/arm/arm.c
> @@ -4984,7 +4984,7 @@ arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
>  		  if (i != maxval
>  		      && arm_const_double_by_immediates (GEN_INT (i + 1)))
>  		    {
> -		      *op1 = GEN_INT (i + 1);
> +		      *op1 = GEN_INT (trunc_int_for_mode (i + 1, DImode));
>  		      *code = *code == GT ? GE : LT;
>  		      return;
>  		    }
> @@ -4994,7 +4994,7 @@ arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
>  		  if (i != ~((unsigned HOST_WIDE_INT) 0)
>  		      && arm_const_double_by_immediates (GEN_INT (i + 1)))
>  		    {
> -		      *op1 = GEN_INT (i + 1);
> +		      *op1 = GEN_INT (trunc_int_for_mode (i + 1, DImode));

The above two aren't strictly necessary, HOST_WIDE_INT is always 64-bit, so
is DImode, and GEN_INT takes HOST_WIDE_INT.
You haven't changed it in the GEN_INT (i + 1) calls passed to
arm_const_double_by_immediates anyway.
I'd think you can leave those changes to cleanup in stage1 if desirable.

> @@ -5047,7 +5047,7 @@ arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
>        if (i != maxval
>  	  && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
>  	{
> -	  *op1 = GEN_INT (i + 1);
> +	  *op1 = GEN_INT (ARM_SIGN_EXTEND (i + 1));
>  	  *code = *code == GT ? GE : LT;
>  	  return;
>  	}
> @@ -5069,7 +5069,7 @@ arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
>        if (i != ~((unsigned HOST_WIDE_INT) 0)
>  	  && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
>  	{
> -	  *op1 = GEN_INT (i + 1);
> +	  *op1 = GEN_INT (ARM_SIGN_EXTEND (i + 1));
>  	  *code = *code == GTU ? GEU : LTU;
>  	  return;
>  	}

This looks ok to me, but I'll defer the final word to ARM maintainers.
That said, the ARM_SIGN_EXTEND macro could very well use some cleanup too
now that HOST_WIDE_INT is always 64-bit and one can e.g. use
HOST_WIDE_INT_{U,}C macros to build large constants.

	Jakub
diff mbox

Patch

commit 90fbcc1f74efd4b5f077fa6caea91d24548cca34
Author: Kyrylo Tkachov <kyrylo.tkachov@arm.com>
Date:   Wed Apr 8 17:08:17 2015 +0100

    [ARM] PR target/65694: Use ARM_SIGN_EXTEND in arm_canonicalize_comparison

diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c
index 369cb67..5342b33 100644
--- a/gcc/config/arm/arm.c
+++ b/gcc/config/arm/arm.c
@@ -4984,7 +4984,7 @@  arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
 		  if (i != maxval
 		      && arm_const_double_by_immediates (GEN_INT (i + 1)))
 		    {
-		      *op1 = GEN_INT (i + 1);
+		      *op1 = GEN_INT (trunc_int_for_mode (i + 1, DImode));
 		      *code = *code == GT ? GE : LT;
 		      return;
 		    }
@@ -4994,7 +4994,7 @@  arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
 		  if (i != ~((unsigned HOST_WIDE_INT) 0)
 		      && arm_const_double_by_immediates (GEN_INT (i + 1)))
 		    {
-		      *op1 = GEN_INT (i + 1);
+		      *op1 = GEN_INT (trunc_int_for_mode (i + 1, DImode));
 		      *code = *code == GTU ? GEU : LTU;
 		      return;
 		    }
@@ -5047,7 +5047,7 @@  arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
       if (i != maxval
 	  && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
 	{
-	  *op1 = GEN_INT (i + 1);
+	  *op1 = GEN_INT (ARM_SIGN_EXTEND (i + 1));
 	  *code = *code == GT ? GE : LT;
 	  return;
 	}
@@ -5069,7 +5069,7 @@  arm_canonicalize_comparison (int *code, rtx *op0, rtx *op1,
       if (i != ~((unsigned HOST_WIDE_INT) 0)
 	  && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
 	{
-	  *op1 = GEN_INT (i + 1);
+	  *op1 = GEN_INT (ARM_SIGN_EXTEND (i + 1));
 	  *code = *code == GTU ? GEU : LTU;
 	  return;
 	}
diff --git a/gcc/testsuite/g++.dg/torture/pr65694.C b/gcc/testsuite/g++.dg/torture/pr65694.C
new file mode 100644
index 0000000..55e009a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr65694.C
@@ -0,0 +1,144 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wno-sign-compare -Wno-return-type -Wno-overflow" } */
+/* { dg-additional-options "-mthumb" { target arm_thumb2_ok } } */
+
+struct A {
+  enum { __value };
+};
+template <class _T1> struct B { _T1 first; };
+template <typename _Iterator, bool> struct C {
+  typedef typename _Iterator::iterator_type iterator_type;
+  static iterator_type _S_base(_Iterator p1) { return p1.base(); }
+};
+template <typename _RandomAccessIterator>
+typename _RandomAccessIterator::difference_type
+__distance(_RandomAccessIterator p1, _RandomAccessIterator p2, int) {
+  return p2 - p1;
+}
+
+template <typename _InputIterator>
+typename _InputIterator::difference_type distance(_InputIterator p1,
+                                                  _InputIterator p2) {
+  return __distance(p1, p2, 0);
+}
+
+template <typename _Iterator, typename> class D {
+  _Iterator _M_current;
+
+public:
+  typedef _Iterator iterator_type;
+  typedef int difference_type;
+  _Iterator base() { return _M_current; }
+};
+
+template <typename _Iterator, typename _Container>
+typename D<_Iterator, _Container>::difference_type
+operator-(D<_Iterator, _Container> p1, D<_Iterator, _Container> p2) {
+  return p1.base() - p2.base();
+}
+
+struct F {
+  static unsigned short *__copy_m(unsigned short *p1, unsigned short *p2,
+                                  unsigned short *p3) {
+    int a = p2 - p1;
+    if (a)
+      __builtin_memmove(p3, p1, a);
+    return p3 + a;
+  }
+};
+class G {
+public:
+  void allocate(int p1) {
+    if (p1 > max_size())
+      operator new(sizeof(short));
+  }
+  unsigned max_size() { return -1 / sizeof(short); }
+};
+
+template <typename> class L : public G {};
+
+struct H {
+  static unsigned short *allocate(int p1) {
+    L<short> d;
+    d.allocate(p1);
+  }
+};
+struct I {
+  template <typename _InputIterator, typename _ForwardIterator>
+  static _ForwardIterator __uninit_copy(_InputIterator p1, _InputIterator p2,
+                                        _ForwardIterator p3) {
+    return copy(p1, p2, p3);
+  }
+};
+struct J {
+  typedef unsigned short *pointer;
+  struct K {
+    unsigned short *_M_start;
+    unsigned short *_M_finish;
+  };
+  J();
+  J(int p1, int) { _M_create_storage(p1); }
+  K _M_impl;
+  pointer _M_allocate(unsigned p1) { p1 ? H::allocate(p1) : pointer(); }
+  void _M_create_storage(int p1) { _M_allocate(p1); }
+};
+
+C<D<unsigned short *, int>, 1>::iterator_type
+__miter_base(D<unsigned short *, int> p1) {
+  return C<D<unsigned short *, int>, 1>::_S_base(p1);
+}
+
+template <bool, typename _II, typename _OI>
+_OI __copy_move_a(_II p1, _II p2, _OI p3) {
+  return F::__copy_m(p1, p2, p3);
+}
+
+template <bool _IsMove, typename _II, typename _OI>
+_OI __copy_move_a2(_II p1, _II p2, _OI p3) {
+  return __copy_move_a<_IsMove>(p1, p2, p3);
+}
+
+template <typename _II, typename _OI> _OI copy(_II p1, _II p2, _OI p3) {
+  C<D<unsigned short *, int>, 1>::iterator_type b, c = __miter_base(p1);
+  b = __miter_base(p2);
+  return __copy_move_a2<A::__value>(c, b, p3);
+}
+
+template <typename _InputIterator, typename _ForwardIterator>
+_ForwardIterator uninitialized_copy(_InputIterator p1, _InputIterator p2,
+                                    _ForwardIterator p3) {
+  return I::__uninit_copy(p1, p2, p3);
+}
+
+template <typename _InputIterator, typename _ForwardIterator, typename _Tp>
+_ForwardIterator __uninitialized_copy_a(_InputIterator p1, _InputIterator p2,
+                                        _ForwardIterator p3, L<_Tp>) {
+  return uninitialized_copy(p1, p2, p3);
+}
+
+class M : J {
+  J _Base;
+
+public:
+  M();
+  M(int p1, int p2 = int()) : _Base(p1, p2) {}
+  M(D<unsigned short *, int> p1, D<unsigned short *, int> p2) {
+    _M_initialize_dispatch(p1, p2, int());
+  }
+  D<pointer, int> begin();
+  D<pointer, int> end();
+  int size() { return _M_impl._M_finish - _M_impl._M_start; }
+  void _M_initialize_dispatch(D<unsigned short *, int> p1,
+                              D<unsigned short *, int> p2, int) {
+    L<short> e;
+    int f = distance(p1, p2);
+    _M_impl._M_start = _M_allocate(f);
+    _M_impl._M_finish = __uninitialized_copy_a(p1, p2, _M_impl._M_start, e);
+  }
+};
+
+B<M> g, h;
+void twoMeans() {
+  M i(g.first.begin(), h.first.end());
+  M(i.size());
+}