Patchwork [Ada] Fix wrong number of iterations for specific loop

mail settings
Submitter Eric Botcazou
Date Aug. 13, 2013, 4:03 p.m.
Message ID <>
Download mbox | patch
Permalink /patch/266875/
State New
Headers show


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  <>

	* 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  <>

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


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.