haifa-sched: fix autopref_rank_for_schedule comparator [PR109187]
authorAlexander Monakov <amonakov@ispras.ru>
Tue, 28 Mar 2023 13:00:37 +0000 (16:00 +0300)
committerAlexander Monakov <amonakov@ispras.ru>
Tue, 28 Mar 2023 13:44:25 +0000 (16:44 +0300)
Do not attempt to use a plain subtraction for generating a three-way
comparison result in autopref_rank_for_schedule qsort comparator, as
offsets are not restricted and subtraction may overflow.  Open-code
a safe three-way comparison instead.

gcc/ChangeLog:

PR rtl-optimization/109187
* haifa-sched.cc (autopref_rank_for_schedule): Avoid use of overflowing
subtraction in three-way comparison.

gcc/testsuite/ChangeLog:

PR rtl-optimization/109187
* gcc.dg/pr109187.c: New test.

gcc/haifa-sched.cc
gcc/testsuite/gcc.dg/pr109187.c [new file with mode: 0644]

index 48b5377..2c881ed 100644 (file)
@@ -5686,7 +5686,7 @@ autopref_rank_for_schedule (const rtx_insn *insn1, const rtx_insn *insn2)
 
       if (!irrel1 && !irrel2)
        /* Sort memory references from lowest offset to the largest.  */
-       r = data1->offset - data2->offset;
+       r = (data1->offset > data2->offset) - (data1->offset < data2->offset);
       else if (write)
        /* Schedule "irrelevant" insns before memory stores to resolve
           as many producer dependencies of stores as possible.  */
diff --git a/gcc/testsuite/gcc.dg/pr109187.c b/gcc/testsuite/gcc.dg/pr109187.c
new file mode 100644 (file)
index 0000000..1ef14a7
--- /dev/null
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 --param sched-autopref-queue-depth=1" } */
+
+void f(int *a)
+{
+  for (;;)
+    asm("" :: "r"(a[-0x10000000]), "r"(a[0x10000000]), "r"(a[0]) : "memory");
+}