[Ada] Fix wrong number of iterations for specific loop

Submitted by Eric Botcazou on Aug. 13, 2013, 4:03 p.m.


Message ID 201308131803.41729.ebotcazou@adacore.com
State New
Headers show

Commit Message

Eric Botcazou Aug. 13, 2013, 4:03 p.m.
This is a regression present on all the active branches.  The compiler 
generates code that doesn't execute the expected number of iterations for a 
loop with a dynamic upper bound whose value is exactly the upper bound of the 
base index type and is obtained through a narrowing conversion.

Tested on x86_64-suse-linux, applied on all the active branches.

2013-08-13  Eric Botcazou  <ebotcazou@adacore.com>

	* gcc-interface/trans.c (can_equal_min_or_max_val_p): Be prepared for
	values outside of the range of the type.

2013-08-13  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/loop_optimization16.adb: New test.
	* gnat.dg/loop_optimization16_pkg.ad[sb]: New helper.

Patch hide | download patch | download mbox

Index: gcc-interface/trans.c
--- gcc-interface/trans.c	(revision 201622)
+++ gcc-interface/trans.c	(working copy)
@@ -2391,7 +2391,10 @@  can_equal_min_or_max_val_p (tree val, tr
   if (TREE_CODE (val) != INTEGER_CST)
     return true;
-  return tree_int_cst_equal (val, min_or_max_val) == 1;
+  if (max)
+    return tree_int_cst_lt (val, min_or_max_val) == 0;
+  else
+    return tree_int_cst_lt (min_or_max_val, val) == 0;
 /* Return true if VAL (of type TYPE) can equal the minimum value of TYPE.