Lines 1664-1696
Link Here
|
1664 |
} |
1664 |
} |
1665 |
} |
1665 |
} |
1666 |
|
1666 |
|
1667 |
/* If A > B, returns -1. If A == B, returns 0. If A < B, returns 1. |
|
|
1668 |
If neither of these relations can be proved, returns 2. */ |
1669 |
|
1670 |
static int |
1671 |
compare_trees (tree a, tree b) |
1672 |
{ |
1673 |
tree typea = TREE_TYPE (a), typeb = TREE_TYPE (b); |
1674 |
tree type; |
1675 |
|
1676 |
if (TYPE_PRECISION (typea) > TYPE_PRECISION (typeb)) |
1677 |
type = typea; |
1678 |
else |
1679 |
type = typeb; |
1680 |
|
1681 |
a = fold_convert (type, a); |
1682 |
b = fold_convert (type, b); |
1683 |
|
1684 |
if (nonzero_p (fold_binary (EQ_EXPR, boolean_type_node, a, b))) |
1685 |
return 0; |
1686 |
if (nonzero_p (fold_binary (LT_EXPR, boolean_type_node, a, b))) |
1687 |
return 1; |
1688 |
if (nonzero_p (fold_binary (GT_EXPR, boolean_type_node, a, b))) |
1689 |
return -1; |
1690 |
|
1691 |
return 2; |
1692 |
} |
1693 |
|
1694 |
/* Returns true if statement S1 dominates statement S2. */ |
1667 |
/* Returns true if statement S1 dominates statement S2. */ |
1695 |
|
1668 |
|
1696 |
static bool |
1669 |
static bool |
Lines 1785-1907
Link Here
|
1785 |
return false; |
1758 |
return false; |
1786 |
} |
1759 |
} |
1787 |
|
1760 |
|
1788 |
/* Checks whether it is correct to count the induction variable BASE + |
1761 |
/* Returns true if the arithmetics in TYPE can be assumed not to wrap. */ |
1789 |
STEP * I at AT_STMT in a wider type NEW_TYPE, using the bounds on |
|
|
1790 |
numbers of iterations of a LOOP. If it is possible, return the |
1791 |
value of step of the induction variable in the NEW_TYPE, otherwise |
1792 |
return NULL_TREE. */ |
1793 |
|
1762 |
|
1794 |
static tree |
1763 |
bool |
1795 |
convert_step_widening (struct loop *loop, tree new_type, tree base, tree step, |
1764 |
nowrap_type_p (tree type) |
1796 |
tree at_stmt) |
|
|
1797 |
{ |
1765 |
{ |
1798 |
struct nb_iter_bound *bound; |
1766 |
if (!flag_wrapv |
1799 |
tree base_in_new_type, base_plus_step_in_new_type, step_in_new_type; |
1767 |
&& INTEGRAL_TYPE_P (type) |
1800 |
tree delta, step_abs; |
1768 |
&& !TYPE_UNSIGNED (type)) |
1801 |
tree unsigned_type, valid_niter; |
1769 |
return true; |
1802 |
|
1770 |
|
1803 |
/* Compute the new step. For example, {(uchar) 100, +, (uchar) 240} |
1771 |
if (POINTER_TYPE_P (type)) |
1804 |
is converted to {(uint) 100, +, (uint) 0xfffffff0} in order to |
1772 |
return true; |
1805 |
keep the values of the induction variable unchanged: 100, 84, 68, |
|
|
1806 |
... |
1807 |
|
1773 |
|
1808 |
Another example is: (uint) {(uchar)100, +, (uchar)3} is converted |
|
|
1809 |
to {(uint)100, +, (uint)3}. |
1810 |
|
1811 |
Before returning the new step, verify that the number of |
1812 |
iterations is less than DELTA / STEP_ABS (i.e. in the previous |
1813 |
example (256 - 100) / 3) such that the iv does not wrap (in which |
1814 |
case the operations are too difficult to be represented and |
1815 |
handled: the values of the iv should be taken modulo 256 in the |
1816 |
wider type; this is not implemented). */ |
1817 |
base_in_new_type = fold_convert (new_type, base); |
1818 |
base_plus_step_in_new_type = |
1819 |
fold_convert (new_type, |
1820 |
fold_build2 (PLUS_EXPR, TREE_TYPE (base), base, step)); |
1821 |
step_in_new_type = fold_build2 (MINUS_EXPR, new_type, |
1822 |
base_plus_step_in_new_type, |
1823 |
base_in_new_type); |
1824 |
|
1825 |
if (TREE_CODE (step_in_new_type) != INTEGER_CST) |
1826 |
return NULL_TREE; |
1827 |
|
1828 |
switch (compare_trees (base_plus_step_in_new_type, base_in_new_type)) |
1829 |
{ |
1830 |
case -1: |
1831 |
{ |
1832 |
tree extreme = upper_bound_in_type (new_type, TREE_TYPE (base)); |
1833 |
delta = fold_build2 (MINUS_EXPR, new_type, extreme, |
1834 |
base_in_new_type); |
1835 |
step_abs = step_in_new_type; |
1836 |
break; |
1837 |
} |
1838 |
|
1839 |
case 1: |
1840 |
{ |
1841 |
tree extreme = lower_bound_in_type (new_type, TREE_TYPE (base)); |
1842 |
delta = fold_build2 (MINUS_EXPR, new_type, base_in_new_type, |
1843 |
extreme); |
1844 |
step_abs = fold_build1 (NEGATE_EXPR, new_type, step_in_new_type); |
1845 |
break; |
1846 |
} |
1847 |
|
1848 |
case 0: |
1849 |
return step_in_new_type; |
1850 |
|
1851 |
default: |
1852 |
return NULL_TREE; |
1853 |
} |
1854 |
|
1855 |
unsigned_type = unsigned_type_for (new_type); |
1856 |
delta = fold_convert (unsigned_type, delta); |
1857 |
step_abs = fold_convert (unsigned_type, step_abs); |
1858 |
valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, |
1859 |
delta, step_abs); |
1860 |
|
1861 |
estimate_numbers_of_iterations_loop (loop); |
1862 |
for (bound = loop->bounds; bound; bound = bound->next) |
1863 |
if (proved_non_wrapping_p (at_stmt, bound, new_type, valid_niter)) |
1864 |
return step_in_new_type; |
1865 |
|
1866 |
/* Fail when the loop has no bound estimations, or when no bound can |
1867 |
be used for verifying the conversion. */ |
1868 |
return NULL_TREE; |
1869 |
} |
1870 |
|
1871 |
/* Returns true when VAR is used in pointer arithmetics. DEPTH is |
1872 |
used for limiting the search. */ |
1873 |
|
1874 |
static bool |
1875 |
used_in_pointer_arithmetic_p (tree var, int depth) |
1876 |
{ |
1877 |
use_operand_p use_p; |
1878 |
imm_use_iterator iter; |
1879 |
|
1880 |
if (depth == 0 |
1881 |
|| TREE_CODE (var) != SSA_NAME |
1882 |
|| !has_single_use (var)) |
1883 |
return false; |
1884 |
|
1885 |
FOR_EACH_IMM_USE_FAST (use_p, iter, var) |
1886 |
{ |
1887 |
tree stmt = USE_STMT (use_p); |
1888 |
|
1889 |
if (stmt && TREE_CODE (stmt) == MODIFY_EXPR) |
1890 |
{ |
1891 |
tree rhs = TREE_OPERAND (stmt, 1); |
1892 |
|
1893 |
if (TREE_CODE (rhs) == NOP_EXPR |
1894 |
|| TREE_CODE (rhs) == CONVERT_EXPR) |
1895 |
{ |
1896 |
if (POINTER_TYPE_P (TREE_TYPE (rhs))) |
1897 |
return true; |
1898 |
return false; |
1899 |
} |
1900 |
else |
1901 |
return used_in_pointer_arithmetic_p (TREE_OPERAND (stmt, 0), |
1902 |
depth - 1); |
1903 |
} |
1904 |
} |
1905 |
return false; |
1774 |
return false; |
1906 |
} |
1775 |
} |
1907 |
|
1776 |
|
Lines 1910-2065
Link Here
|
1910 |
enough with respect to the step and initial condition in order to |
1779 |
enough with respect to the step and initial condition in order to |
1911 |
keep the evolution confined in TYPEs bounds. Return true when the |
1780 |
keep the evolution confined in TYPEs bounds. Return true when the |
1912 |
iv is known to overflow or when the property is not computable. |
1781 |
iv is known to overflow or when the property is not computable. |
|
|
1782 |
|
1783 |
USE_OVERFLOW_SEMANTICS is true if this function should assume that |
1784 |
the rules for overflow of the given language apply (e.g., that signed |
1785 |
arithmetics in C does not overflow). */ |
1913 |
|
1786 |
|
1914 |
Initialize INIT_IS_MAX to true when the evolution goes from |
|
|
1915 |
INIT_IS_MAX to LOWER_BOUND_IN_TYPE, false in the contrary case. |
1916 |
When this property cannot be determined, UNKNOWN_MAX is set to |
1917 |
true. */ |
1918 |
|
1919 |
bool |
1787 |
bool |
1920 |
scev_probably_wraps_p (tree type, tree base, tree step, |
1788 |
scev_probably_wraps_p (tree base, tree step, |
1921 |
tree at_stmt, struct loop *loop, |
1789 |
tree at_stmt, struct loop *loop, |
1922 |
bool *init_is_max, bool *unknown_max) |
1790 |
bool use_oveflow_semantics) |
1923 |
{ |
1791 |
{ |
1924 |
struct nb_iter_bound *bound; |
1792 |
struct nb_iter_bound *bound; |
1925 |
tree delta, step_abs; |
1793 |
tree delta, step_abs; |
1926 |
tree unsigned_type, valid_niter; |
1794 |
tree unsigned_type, valid_niter; |
1927 |
tree base_plus_step, bpsps; |
1795 |
tree type = TREE_TYPE (step); |
1928 |
int cps, cpsps; |
|
|
1929 |
|
1796 |
|
1930 |
/* FIXME: The following code will not be used anymore once |
1797 |
/* FIXME: We really need something like |
1931 |
http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html is |
1798 |
http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html. |
1932 |
committed. |
|
|
1933 |
|
1799 |
|
1934 |
If AT_STMT is a cast to unsigned that is later used for |
1800 |
We used to test for the following situation that frequently appears |
1935 |
referencing a memory location, it is followed by a pointer |
1801 |
during address arithmetics: |
1936 |
conversion just after. Because pointers do not wrap, the |
|
|
1937 |
sequences that reference the memory do not wrap either. In the |
1938 |
following example, sequences corresponding to D_13 and to D_14 |
1939 |
can be proved to not wrap because they are used for computing a |
1940 |
memory access: |
1941 |
|
1802 |
|
1942 |
D.1621_13 = (long unsigned intD.4) D.1620_12; |
1803 |
D.1621_13 = (long unsigned intD.4) D.1620_12; |
1943 |
D.1622_14 = D.1621_13 * 8; |
1804 |
D.1622_14 = D.1621_13 * 8; |
1944 |
D.1623_15 = (doubleD.29 *) D.1622_14; |
1805 |
D.1623_15 = (doubleD.29 *) D.1622_14; |
1945 |
*/ |
|
|
1946 |
if (at_stmt && TREE_CODE (at_stmt) == MODIFY_EXPR) |
1947 |
{ |
1948 |
tree op0 = TREE_OPERAND (at_stmt, 0); |
1949 |
tree op1 = TREE_OPERAND (at_stmt, 1); |
1950 |
tree type_op1 = TREE_TYPE (op1); |
1951 |
|
1806 |
|
1952 |
if ((TYPE_UNSIGNED (type_op1) |
1807 |
And derived that the sequence corresponding to D_14 |
1953 |
&& used_in_pointer_arithmetic_p (op0, 2)) |
1808 |
can be proved to not wrap because it is used for computing a |
1954 |
|| POINTER_TYPE_P (type_op1)) |
1809 |
memory access; however, this is not really the case -- for example, |
1955 |
{ |
1810 |
if D_12 = (unsigned char) [254,+,1], then D_14 has values |
1956 |
*unknown_max = true; |
1811 |
2032, 2040, 0, 8, ..., but the code is still legal. */ |
1957 |
return false; |
|
|
1958 |
} |
1959 |
} |
1960 |
|
1812 |
|
1961 |
if (chrec_contains_undetermined (base) |
1813 |
if (chrec_contains_undetermined (base) |
1962 |
|| chrec_contains_undetermined (step) |
1814 |
|| chrec_contains_undetermined (step) |
1963 |
|| TREE_CODE (base) == REAL_CST |
1815 |
|| TREE_CODE (step) != INTEGER_CST) |
1964 |
|| TREE_CODE (step) == REAL_CST) |
|
|
1965 |
{ |
1966 |
*unknown_max = true; |
1967 |
return true; |
1968 |
} |
1969 |
|
1970 |
*unknown_max = false; |
1971 |
base_plus_step = fold_build2 (PLUS_EXPR, type, base, step); |
1972 |
bpsps = fold_build2 (PLUS_EXPR, type, base_plus_step, step); |
1973 |
cps = compare_trees (base_plus_step, base); |
1974 |
cpsps = compare_trees (bpsps, base_plus_step); |
1975 |
|
1976 |
/* Check that the sequence is not wrapping in the first step: it |
1977 |
should have the same monotonicity for the first two steps. See |
1978 |
PR23410. */ |
1979 |
if (cps != cpsps) |
1980 |
return true; |
1816 |
return true; |
1981 |
|
1817 |
|
1982 |
switch (cps) |
1818 |
if (zero_p (step)) |
1983 |
{ |
1819 |
return false; |
1984 |
case -1: |
|
|
1985 |
{ |
1986 |
tree extreme = upper_bound_in_type (type, TREE_TYPE (base)); |
1987 |
delta = fold_build2 (MINUS_EXPR, type, extreme, base); |
1988 |
step_abs = step; |
1989 |
*init_is_max = false; |
1990 |
break; |
1991 |
} |
1992 |
|
1820 |
|
1993 |
case 1: |
1821 |
/* If we can use the fact that signed and pointer arithmetics does not |
1994 |
{ |
1822 |
wrap, we are done. */ |
1995 |
tree extreme = lower_bound_in_type (type, TREE_TYPE (base)); |
1823 |
if (use_oveflow_semantics && nowrap_type_p (type)) |
1996 |
delta = fold_build2 (MINUS_EXPR, type, base, extreme); |
1824 |
return false; |
1997 |
step_abs = fold_build1 (NEGATE_EXPR, type, step); |
|
|
1998 |
*init_is_max = true; |
1999 |
break; |
2000 |
} |
2001 |
|
1825 |
|
2002 |
case 0: |
1826 |
/* Otherwise, compute the number of iterations before we reach the |
2003 |
/* This means step is equal to 0. This should not happen. It |
1827 |
bound of the type, and verify that the loop is exited before this |
2004 |
could happen in convert step, but not here. Safely answer |
1828 |
occurs. */ |
2005 |
don't know as in the default case. */ |
1829 |
unsigned_type = unsigned_type_for (type); |
|
|
1830 |
base = fold_convert (unsigned_type, base); |
2006 |
|
1831 |
|
2007 |
default: |
1832 |
if (tree_int_cst_sign_bit (step)) |
2008 |
*unknown_max = true; |
1833 |
{ |
2009 |
return true; |
1834 |
tree extreme = fold_convert (unsigned_type, |
|
|
1835 |
lower_bound_in_type (type, type)); |
1836 |
delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme); |
1837 |
step_abs = fold_build1 (NEGATE_EXPR, unsigned_type, |
1838 |
fold_convert (unsigned_type, step)); |
2010 |
} |
1839 |
} |
2011 |
|
1840 |
else |
2012 |
/* If AT_STMT represents a cast operation, we may not be able to |
|
|
2013 |
take advantage of the undefinedness of signed type evolutions. |
2014 |
|
2015 |
implement-c.texi states: "For conversion to a type of width |
2016 |
N, the value is reduced modulo 2^N to be within range of the |
2017 |
type;" |
2018 |
|
2019 |
See PR 21959 for a test case. Essentially, given a cast |
2020 |
operation |
2021 |
unsigned char uc; |
2022 |
signed char sc; |
2023 |
... |
2024 |
sc = (signed char) uc; |
2025 |
if (sc < 0) |
2026 |
... |
2027 |
|
2028 |
where uc and sc have the scev {0, +, 1}, we would consider uc to |
2029 |
wrap around, but not sc, because it is of a signed type. This |
2030 |
causes VRP to erroneously fold the predicate above because it |
2031 |
thinks that sc cannot be negative. */ |
2032 |
if (at_stmt && TREE_CODE (at_stmt) == MODIFY_EXPR) |
2033 |
{ |
1841 |
{ |
2034 |
tree rhs = TREE_OPERAND (at_stmt, 1); |
1842 |
tree extreme = fold_convert (unsigned_type, |
2035 |
tree outer_t = TREE_TYPE (rhs); |
1843 |
upper_bound_in_type (type, type)); |
2036 |
|
1844 |
delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base); |
2037 |
if (!TYPE_UNSIGNED (outer_t) |
1845 |
step_abs = fold_convert (unsigned_type, step); |
2038 |
&& (TREE_CODE (rhs) == NOP_EXPR || TREE_CODE (rhs) == CONVERT_EXPR)) |
|
|
2039 |
{ |
2040 |
tree inner_t = TREE_TYPE (TREE_OPERAND (rhs, 0)); |
2041 |
|
2042 |
/* If the inner type is unsigned and its size and/or |
2043 |
precision are smaller to that of the outer type, then the |
2044 |
expression may wrap around. */ |
2045 |
if (TYPE_UNSIGNED (inner_t) |
2046 |
&& (TYPE_SIZE (inner_t) <= TYPE_SIZE (outer_t) |
2047 |
|| TYPE_PRECISION (inner_t) <= TYPE_PRECISION (outer_t))) |
2048 |
{ |
2049 |
*unknown_max = true; |
2050 |
return true; |
2051 |
} |
2052 |
} |
2053 |
} |
1846 |
} |
2054 |
|
1847 |
|
2055 |
/* After having set INIT_IS_MAX, we can return false: when not using |
|
|
2056 |
wrapping arithmetic, signed types don't wrap. */ |
2057 |
if (!flag_wrapv && !TYPE_UNSIGNED (type)) |
2058 |
return false; |
2059 |
|
2060 |
unsigned_type = unsigned_type_for (type); |
2061 |
delta = fold_convert (unsigned_type, delta); |
2062 |
step_abs = fold_convert (unsigned_type, step_abs); |
2063 |
valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs); |
1848 |
valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs); |
2064 |
|
1849 |
|
2065 |
estimate_numbers_of_iterations_loop (loop); |
1850 |
estimate_numbers_of_iterations_loop (loop); |
Lines 2069-2104
Link Here
|
2069 |
|
1854 |
|
2070 |
/* At this point we still don't have a proof that the iv does not |
1855 |
/* At this point we still don't have a proof that the iv does not |
2071 |
overflow: give up. */ |
1856 |
overflow: give up. */ |
2072 |
*unknown_max = true; |
|
|
2073 |
return true; |
1857 |
return true; |
2074 |
} |
1858 |
} |
2075 |
|
1859 |
|
2076 |
/* Return the conversion to NEW_TYPE of the STEP of an induction |
|
|
2077 |
variable BASE + STEP * I at AT_STMT. When it fails, return |
2078 |
NULL_TREE. */ |
2079 |
|
2080 |
tree |
2081 |
convert_step (struct loop *loop, tree new_type, tree base, tree step, |
2082 |
tree at_stmt) |
2083 |
{ |
2084 |
tree base_type; |
2085 |
|
2086 |
if (chrec_contains_undetermined (base) |
2087 |
|| chrec_contains_undetermined (step)) |
2088 |
return NULL_TREE; |
2089 |
|
2090 |
base_type = TREE_TYPE (base); |
2091 |
|
2092 |
/* When not using wrapping arithmetic, signed types don't wrap. */ |
2093 |
if (!flag_wrapv && !TYPE_UNSIGNED (base_type)) |
2094 |
return fold_convert (new_type, step); |
2095 |
|
2096 |
if (TYPE_PRECISION (new_type) > TYPE_PRECISION (base_type)) |
2097 |
return convert_step_widening (loop, new_type, base, step, at_stmt); |
2098 |
|
2099 |
return fold_convert (new_type, step); |
2100 |
} |
2101 |
|
2102 |
/* Frees the information on upper bounds on numbers of iterations of LOOP. */ |
1860 |
/* Frees the information on upper bounds on numbers of iterations of LOOP. */ |
2103 |
|
1861 |
|
2104 |
void |
1862 |
void |