| 1 |
/* Code for range operators. |
| 2 |
Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Andrew MacLeod <[email protected]> |
| 4 |
and Aldy Hernandez <[email protected]>. |
| 5 |
|
| 6 |
This file is part of GCC. |
| 7 |
|
| 8 |
GCC is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 3, or (at your option) |
| 11 |
any later version. |
| 12 |
|
| 13 |
GCC is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with GCC; see the file COPYING3. If not see |
| 20 |
<http://www.gnu.org/licenses/>. */ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "backend.h" |
| 26 |
#include "insn-codes.h" |
| 27 |
#include "rtl.h" |
| 28 |
#include "tree.h" |
| 29 |
#include "gimple.h" |
| 30 |
#include "cfghooks.h" |
| 31 |
#include "tree-pass.h" |
| 32 |
#include "ssa.h" |
| 33 |
#include "optabs-tree.h" |
| 34 |
#include "gimple-pretty-print.h" |
| 35 |
#include "diagnostic-core.h" |
| 36 |
#include "flags.h" |
| 37 |
#include "fold-const.h" |
| 38 |
#include "stor-layout.h" |
| 39 |
#include "calls.h" |
| 40 |
#include "cfganal.h" |
| 41 |
#include "gimple-iterator.h" |
| 42 |
#include "gimple-fold.h" |
| 43 |
#include "tree-eh.h" |
| 44 |
#include "gimple-walk.h" |
| 45 |
#include "tree-cfg.h" |
| 46 |
#include "wide-int.h" |
| 47 |
#include "value-relation.h" |
| 48 |
#include "range-op.h" |
| 49 |
#include "tree-ssa-ccp.h" |
| 50 |
#include "range-op-mixed.h" |
| 51 |
|
| 52 |
// Instantiate the operators which apply to multiple types here. |
| 53 |
|
| 54 |
operator_equal op_equal; |
| 55 |
operator_not_equal op_not_equal; |
| 56 |
operator_lt op_lt; |
| 57 |
operator_le op_le; |
| 58 |
operator_gt op_gt; |
| 59 |
operator_ge op_ge; |
| 60 |
operator_identity op_ident; |
| 61 |
operator_cst op_cst; |
| 62 |
operator_cast op_cast; |
| 63 |
operator_plus op_plus; |
| 64 |
operator_abs op_abs; |
| 65 |
operator_minus op_minus; |
| 66 |
operator_negate op_negate; |
| 67 |
operator_mult op_mult; |
| 68 |
operator_addr_expr op_addr; |
| 69 |
operator_bitwise_not op_bitwise_not; |
| 70 |
operator_bitwise_xor op_bitwise_xor; |
| 71 |
operator_bitwise_and op_bitwise_and; |
| 72 |
operator_bitwise_or op_bitwise_or; |
| 73 |
operator_min op_min; |
| 74 |
operator_max op_max; |
| 75 |
|
| 76 |
// Instantaite a range operator table. |
| 77 |
range_op_table operator_table; |
| 78 |
|
| 79 |
// Invoke the initialization routines for each class of range. |
| 80 |
|
| 81 |
range_op_table::range_op_table () |
| 82 |
{ |
| 83 |
initialize_integral_ops (); |
| 84 |
initialize_pointer_ops (); |
| 85 |
initialize_float_ops (); |
| 86 |
|
| 87 |
set (EQ_EXPR, op_equal); |
| 88 |
set (NE_EXPR, op_not_equal); |
| 89 |
set (LT_EXPR, op_lt); |
| 90 |
set (LE_EXPR, op_le); |
| 91 |
set (GT_EXPR, op_gt); |
| 92 |
set (GE_EXPR, op_ge); |
| 93 |
set (SSA_NAME, op_ident); |
| 94 |
set (PAREN_EXPR, op_ident); |
| 95 |
set (OBJ_TYPE_REF, op_ident); |
| 96 |
set (REAL_CST, op_cst); |
| 97 |
set (INTEGER_CST, op_cst); |
| 98 |
set (NOP_EXPR, op_cast); |
| 99 |
set (CONVERT_EXPR, op_cast); |
| 100 |
set (PLUS_EXPR, op_plus); |
| 101 |
set (ABS_EXPR, op_abs); |
| 102 |
set (MINUS_EXPR, op_minus); |
| 103 |
set (NEGATE_EXPR, op_negate); |
| 104 |
set (MULT_EXPR, op_mult); |
| 105 |
set (ADDR_EXPR, op_addr); |
| 106 |
set (BIT_NOT_EXPR, op_bitwise_not); |
| 107 |
set (BIT_XOR_EXPR, op_bitwise_xor); |
| 108 |
set (BIT_AND_EXPR, op_bitwise_and); |
| 109 |
set (BIT_IOR_EXPR, op_bitwise_or); |
| 110 |
set (MIN_EXPR, op_min); |
| 111 |
set (MAX_EXPR, op_max); |
| 112 |
} |
| 113 |
|
| 114 |
// Instantiate a default range operator for opcodes with no entry. |
| 115 |
|
| 116 |
range_operator default_operator; |
| 117 |
|
| 118 |
// Create a default range_op_handler. |
| 119 |
|
| 120 |
range_op_handler::range_op_handler () |
| 121 |
{ |
| 122 |
m_operator = &default_operator; |
| 123 |
} |
| 124 |
|
| 125 |
// Create a range_op_handler for CODE. Use a default operatoer if CODE |
| 126 |
// does not have an entry. |
| 127 |
|
| 128 |
range_op_handler::range_op_handler (unsigned code) |
| 129 |
{ |
| 130 |
m_operator = operator_table[code]; |
| 131 |
if (!m_operator) |
| 132 |
m_operator = &default_operator; |
| 133 |
} |
| 134 |
|
| 135 |
// Return TRUE if this handler has a non-default operator. |
| 136 |
|
| 137 |
range_op_handler::operator bool () const |
| 138 |
{ |
| 139 |
return m_operator != &default_operator; |
| 140 |
} |
| 141 |
|
| 142 |
// Return a pointer to the range operator assocaited with this handler. |
| 143 |
// If it is a default operator, return NULL. |
| 144 |
// This is the equivalent of indexing the range table. |
| 145 |
|
| 146 |
range_operator * |
| 147 |
range_op_handler::range_op () const |
| 148 |
{ |
| 149 |
if (m_operator != &default_operator) |
| 150 |
return m_operator; |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
// Create a dispatch pattern for value range discriminators LHS, OP1, and OP2. |
| 155 |
// This is used to produce a unique value for each dispatch pattern. Shift |
| 156 |
// values are based on the size of the m_discriminator field in value_range.h. |
| 157 |
|
| 158 |
constexpr unsigned |
| 159 |
dispatch_trio (unsigned lhs, unsigned op1, unsigned op2) |
| 160 |
{ |
| 161 |
return ((lhs << 8) + (op1 << 4) + (op2)); |
| 162 |
} |
| 163 |
|
| 164 |
// These are the supported dispatch patterns. These map to the parameter list |
| 165 |
// of the routines in range_operator. Note the last 3 characters are |
| 166 |
// shorthand for the LHS, OP1, and OP2 range discriminator class. |
| 167 |
|
| 168 |
const unsigned RO_III = dispatch_trio (VR_IRANGE, VR_IRANGE, VR_IRANGE); |
| 169 |
const unsigned RO_IFI = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_IRANGE); |
| 170 |
const unsigned RO_IFF = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_FRANGE); |
| 171 |
const unsigned RO_FFF = dispatch_trio (VR_FRANGE, VR_FRANGE, VR_FRANGE); |
| 172 |
const unsigned RO_FIF = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_FRANGE); |
| 173 |
const unsigned RO_FII = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_IRANGE); |
| 174 |
const unsigned RO_PPP = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_PRANGE); |
| 175 |
const unsigned RO_PPI = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_IRANGE); |
| 176 |
const unsigned RO_IPP = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_PRANGE); |
| 177 |
const unsigned RO_IPI = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_IRANGE); |
| 178 |
const unsigned RO_PIP = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_PRANGE); |
| 179 |
const unsigned RO_PII = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_IRANGE); |
| 180 |
|
| 181 |
// Return a dispatch value for parameter types LHS, OP1 and OP2. |
| 182 |
|
| 183 |
unsigned |
| 184 |
range_op_handler::dispatch_kind (const vrange &lhs, const vrange &op1, |
| 185 |
const vrange& op2) const |
| 186 |
{ |
| 187 |
return dispatch_trio (lhs.m_discriminator, op1.m_discriminator, |
| 188 |
op2.m_discriminator); |
| 189 |
} |
| 190 |
|
| 191 |
void |
| 192 |
range_op_handler::discriminator_fail (const vrange &r1, |
| 193 |
const vrange &r2, |
| 194 |
const vrange &r3) const |
| 195 |
{ |
| 196 |
const char name[] = "IPF"; |
| 197 |
gcc_checking_assert (r1.m_discriminator < sizeof (name) - 1); |
| 198 |
gcc_checking_assert (r2.m_discriminator < sizeof (name) - 1); |
| 199 |
gcc_checking_assert (r3.m_discriminator < sizeof (name) - 1); |
| 200 |
fprintf (stderr, |
| 201 |
"Unsupported operand combination in dispatch: RO_%c%c%c\n", |
| 202 |
name[r1.m_discriminator], |
| 203 |
name[r2.m_discriminator], |
| 204 |
name[r3.m_discriminator]); |
| 205 |
gcc_unreachable (); |
| 206 |
} |
| 207 |
|
| 208 |
static inline bool |
| 209 |
has_pointer_operand_p (const vrange &r1, const vrange &r2, const vrange &r3) |
| 210 |
{ |
| 211 |
return is_a <prange> (r1) || is_a <prange> (r2) || is_a <prange> (r3); |
| 212 |
} |
| 213 |
|
| 214 |
// Dispatch a call to fold_range based on the types of R, LH and RH. |
| 215 |
|
| 216 |
bool |
| 217 |
range_op_handler::fold_range (vrange &r, tree type, |
| 218 |
const vrange &lh, |
| 219 |
const vrange &rh, |
| 220 |
relation_trio rel) const |
| 221 |
{ |
| 222 |
gcc_checking_assert (m_operator); |
| 223 |
#if CHECKING_P |
| 224 |
if (!lh.undefined_p () && !rh.undefined_p ()) |
| 225 |
gcc_assert (m_operator->operand_check_p (type, lh.type (), rh.type ())); |
| 226 |
#endif |
| 227 |
switch (dispatch_kind (r, lh, rh)) |
| 228 |
{ |
| 229 |
case RO_III: |
| 230 |
return m_operator->fold_range (as_a <irange> (r), type, |
| 231 |
as_a <irange> (lh), |
| 232 |
as_a <irange> (rh), rel); |
| 233 |
case RO_IFI: |
| 234 |
return m_operator->fold_range (as_a <irange> (r), type, |
| 235 |
as_a <frange> (lh), |
| 236 |
as_a <irange> (rh), rel); |
| 237 |
case RO_IFF: |
| 238 |
return m_operator->fold_range (as_a <irange> (r), type, |
| 239 |
as_a <frange> (lh), |
| 240 |
as_a <frange> (rh), rel); |
| 241 |
case RO_FFF: |
| 242 |
return m_operator->fold_range (as_a <frange> (r), type, |
| 243 |
as_a <frange> (lh), |
| 244 |
as_a <frange> (rh), rel); |
| 245 |
case RO_FII: |
| 246 |
return m_operator->fold_range (as_a <frange> (r), type, |
| 247 |
as_a <irange> (lh), |
| 248 |
as_a <irange> (rh), rel); |
| 249 |
case RO_PPP: |
| 250 |
return m_operator->fold_range (as_a <prange> (r), type, |
| 251 |
as_a <prange> (lh), |
| 252 |
as_a <prange> (rh), rel); |
| 253 |
case RO_PPI: |
| 254 |
return m_operator->fold_range (as_a <prange> (r), type, |
| 255 |
as_a <prange> (lh), |
| 256 |
as_a <irange> (rh), rel); |
| 257 |
case RO_IPP: |
| 258 |
return m_operator->fold_range (as_a <irange> (r), type, |
| 259 |
as_a <prange> (lh), |
| 260 |
as_a <prange> (rh), rel); |
| 261 |
case RO_PIP: |
| 262 |
return m_operator->fold_range (as_a <prange> (r), type, |
| 263 |
as_a <irange> (lh), |
| 264 |
as_a <prange> (rh), rel); |
| 265 |
case RO_IPI: |
| 266 |
return m_operator->fold_range (as_a <irange> (r), type, |
| 267 |
as_a <prange> (lh), |
| 268 |
as_a <irange> (rh), rel); |
| 269 |
default: |
| 270 |
return false; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// Dispatch a call to op1_range based on the types of R, LHS and OP2. |
| 275 |
|
| 276 |
bool |
| 277 |
range_op_handler::op1_range (vrange &r, tree type, |
| 278 |
const vrange &lhs, |
| 279 |
const vrange &op2, |
| 280 |
relation_trio rel) const |
| 281 |
{ |
| 282 |
gcc_checking_assert (m_operator); |
| 283 |
if (lhs.undefined_p ()) |
| 284 |
return false; |
| 285 |
#if CHECKING_P |
| 286 |
if (!op2.undefined_p ()) |
| 287 |
gcc_assert (m_operator->operand_check_p (lhs.type (), type, op2.type ())); |
| 288 |
#endif |
| 289 |
switch (dispatch_kind (r, lhs, op2)) |
| 290 |
{ |
| 291 |
case RO_III: |
| 292 |
return m_operator->op1_range (as_a <irange> (r), type, |
| 293 |
as_a <irange> (lhs), |
| 294 |
as_a <irange> (op2), rel); |
| 295 |
case RO_PPP: |
| 296 |
return m_operator->op1_range (as_a <prange> (r), type, |
| 297 |
as_a <prange> (lhs), |
| 298 |
as_a <prange> (op2), rel); |
| 299 |
case RO_PIP: |
| 300 |
return m_operator->op1_range (as_a <prange> (r), type, |
| 301 |
as_a <irange> (lhs), |
| 302 |
as_a <prange> (op2), rel); |
| 303 |
case RO_PPI: |
| 304 |
return m_operator->op1_range (as_a <prange> (r), type, |
| 305 |
as_a <prange> (lhs), |
| 306 |
as_a <irange> (op2), rel); |
| 307 |
case RO_IPI: |
| 308 |
return m_operator->op1_range (as_a <irange> (r), type, |
| 309 |
as_a <prange> (lhs), |
| 310 |
as_a <irange> (op2), rel); |
| 311 |
case RO_FIF: |
| 312 |
return m_operator->op1_range (as_a <frange> (r), type, |
| 313 |
as_a <irange> (lhs), |
| 314 |
as_a <frange> (op2), rel); |
| 315 |
case RO_FFF: |
| 316 |
return m_operator->op1_range (as_a <frange> (r), type, |
| 317 |
as_a <frange> (lhs), |
| 318 |
as_a <frange> (op2), rel); |
| 319 |
default: |
| 320 |
return false; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
// Dispatch a call to op2_range based on the types of R, LHS and OP1. |
| 325 |
|
| 326 |
bool |
| 327 |
range_op_handler::op2_range (vrange &r, tree type, |
| 328 |
const vrange &lhs, |
| 329 |
const vrange &op1, |
| 330 |
relation_trio rel) const |
| 331 |
{ |
| 332 |
gcc_checking_assert (m_operator); |
| 333 |
if (lhs.undefined_p ()) |
| 334 |
return false; |
| 335 |
#if CHECKING_P |
| 336 |
if (!op1.undefined_p ()) |
| 337 |
gcc_assert (m_operator->operand_check_p (lhs.type (), op1.type (), type)); |
| 338 |
#endif |
| 339 |
switch (dispatch_kind (r, lhs, op1)) |
| 340 |
{ |
| 341 |
case RO_III: |
| 342 |
return m_operator->op2_range (as_a <irange> (r), type, |
| 343 |
as_a <irange> (lhs), |
| 344 |
as_a <irange> (op1), rel); |
| 345 |
case RO_PIP: |
| 346 |
return m_operator->op2_range (as_a <prange> (r), type, |
| 347 |
as_a <irange> (lhs), |
| 348 |
as_a <prange> (op1), rel); |
| 349 |
case RO_IPP: |
| 350 |
return m_operator->op2_range (as_a <irange> (r), type, |
| 351 |
as_a <prange> (lhs), |
| 352 |
as_a <prange> (op1), rel); |
| 353 |
case RO_FIF: |
| 354 |
return m_operator->op2_range (as_a <frange> (r), type, |
| 355 |
as_a <irange> (lhs), |
| 356 |
as_a <frange> (op1), rel); |
| 357 |
case RO_FFF: |
| 358 |
return m_operator->op2_range (as_a <frange> (r), type, |
| 359 |
as_a <frange> (lhs), |
| 360 |
as_a <frange> (op1), rel); |
| 361 |
default: |
| 362 |
return false; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
// Dispatch a call to lhs_op1_relation based on the types of LHS, OP1 and OP2. |
| 367 |
|
| 368 |
relation_kind |
| 369 |
range_op_handler::lhs_op1_relation (const vrange &lhs, |
| 370 |
const vrange &op1, |
| 371 |
const vrange &op2, |
| 372 |
relation_kind rel) const |
| 373 |
{ |
| 374 |
gcc_checking_assert (m_operator); |
| 375 |
switch (dispatch_kind (lhs, op1, op2)) |
| 376 |
{ |
| 377 |
case RO_III: |
| 378 |
return m_operator->lhs_op1_relation (as_a <irange> (lhs), |
| 379 |
as_a <irange> (op1), |
| 380 |
as_a <irange> (op2), rel); |
| 381 |
case RO_PPP: |
| 382 |
return m_operator->lhs_op1_relation (as_a <prange> (lhs), |
| 383 |
as_a <prange> (op1), |
| 384 |
as_a <prange> (op2), rel); |
| 385 |
case RO_IPP: |
| 386 |
return m_operator->lhs_op1_relation (as_a <irange> (lhs), |
| 387 |
as_a <prange> (op1), |
| 388 |
as_a <prange> (op2), rel); |
| 389 |
case RO_PII: |
| 390 |
return m_operator->lhs_op1_relation (as_a <prange> (lhs), |
| 391 |
as_a <irange> (op1), |
| 392 |
as_a <irange> (op2), rel); |
| 393 |
case RO_IFF: |
| 394 |
return m_operator->lhs_op1_relation (as_a <irange> (lhs), |
| 395 |
as_a <frange> (op1), |
| 396 |
as_a <frange> (op2), rel); |
| 397 |
case RO_FFF: |
| 398 |
return m_operator->lhs_op1_relation (as_a <frange> (lhs), |
| 399 |
as_a <frange> (op1), |
| 400 |
as_a <frange> (op2), rel); |
| 401 |
default: |
| 402 |
return VREL_VARYING; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
// Dispatch a call to lhs_op2_relation based on the types of LHS, OP1 and OP2. |
| 407 |
|
| 408 |
relation_kind |
| 409 |
range_op_handler::lhs_op2_relation (const vrange &lhs, |
| 410 |
const vrange &op1, |
| 411 |
const vrange &op2, |
| 412 |
relation_kind rel) const |
| 413 |
{ |
| 414 |
gcc_checking_assert (m_operator); |
| 415 |
switch (dispatch_kind (lhs, op1, op2)) |
| 416 |
{ |
| 417 |
case RO_III: |
| 418 |
return m_operator->lhs_op2_relation (as_a <irange> (lhs), |
| 419 |
as_a <irange> (op1), |
| 420 |
as_a <irange> (op2), rel); |
| 421 |
case RO_IFF: |
| 422 |
return m_operator->lhs_op2_relation (as_a <irange> (lhs), |
| 423 |
as_a <frange> (op1), |
| 424 |
as_a <frange> (op2), rel); |
| 425 |
case RO_FFF: |
| 426 |
return m_operator->lhs_op2_relation (as_a <frange> (lhs), |
| 427 |
as_a <frange> (op1), |
| 428 |
as_a <frange> (op2), rel); |
| 429 |
default: |
| 430 |
return VREL_VARYING; |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
// Dispatch a call to op1_op2_relation based on the type of LHS. |
| 435 |
|
| 436 |
relation_kind |
| 437 |
range_op_handler::op1_op2_relation (const vrange &lhs, |
| 438 |
const vrange &op1, |
| 439 |
const vrange &op2) const |
| 440 |
{ |
| 441 |
gcc_checking_assert (m_operator); |
| 442 |
|
| 443 |
switch (dispatch_kind (lhs, op1, op2)) |
| 444 |
{ |
| 445 |
case RO_III: |
| 446 |
return m_operator->op1_op2_relation (as_a <irange> (lhs), |
| 447 |
as_a <irange> (op1), |
| 448 |
as_a <irange> (op2)); |
| 449 |
|
| 450 |
case RO_IPP: |
| 451 |
return m_operator->op1_op2_relation (as_a <irange> (lhs), |
| 452 |
as_a <prange> (op1), |
| 453 |
as_a <prange> (op2)); |
| 454 |
|
| 455 |
case RO_IFF: |
| 456 |
return m_operator->op1_op2_relation (as_a <irange> (lhs), |
| 457 |
as_a <frange> (op1), |
| 458 |
as_a <frange> (op2)); |
| 459 |
|
| 460 |
case RO_FFF: |
| 461 |
return m_operator->op1_op2_relation (as_a <frange> (lhs), |
| 462 |
as_a <frange> (op1), |
| 463 |
as_a <frange> (op2)); |
| 464 |
|
| 465 |
default: |
| 466 |
return VREL_VARYING; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
bool |
| 471 |
range_op_handler::overflow_free_p (const vrange &lh, |
| 472 |
const vrange &rh, |
| 473 |
relation_trio rel) const |
| 474 |
{ |
| 475 |
gcc_checking_assert (m_operator); |
| 476 |
switch (dispatch_kind (lh, lh, rh)) |
| 477 |
{ |
| 478 |
case RO_III: |
| 479 |
return m_operator->overflow_free_p(as_a <irange> (lh), |
| 480 |
as_a <irange> (rh), |
| 481 |
rel); |
| 482 |
default: |
| 483 |
return false; |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
bool |
| 488 |
range_op_handler::operand_check_p (tree t1, tree t2, tree t3) const |
| 489 |
{ |
| 490 |
gcc_checking_assert (m_operator); |
| 491 |
return m_operator->operand_check_p (t1, t2, t3); |
| 492 |
} |
| 493 |
|
| 494 |
// Update the known bitmasks in R when applying the operation CODE to |
| 495 |
// LH and RH. |
| 496 |
|
| 497 |
void |
| 498 |
update_known_bitmask (vrange &r, tree_code code, |
| 499 |
const vrange &lh, const vrange &rh) |
| 500 |
{ |
| 501 |
if (r.undefined_p () || lh.undefined_p () || rh.undefined_p () |
| 502 |
|| r.singleton_p ()) |
| 503 |
return; |
| 504 |
|
| 505 |
widest_int widest_value, widest_mask; |
| 506 |
tree type = r.type (); |
| 507 |
signop sign = TYPE_SIGN (type); |
| 508 |
int prec = TYPE_PRECISION (type); |
| 509 |
irange_bitmask lh_bits = lh.get_bitmask (); |
| 510 |
irange_bitmask rh_bits = rh.get_bitmask (); |
| 511 |
|
| 512 |
switch (get_gimple_rhs_class (code)) |
| 513 |
{ |
| 514 |
case GIMPLE_UNARY_RHS: |
| 515 |
bit_value_unop (code, sign, prec, &widest_value, &widest_mask, |
| 516 |
TYPE_SIGN (lh.type ()), |
| 517 |
TYPE_PRECISION (lh.type ()), |
| 518 |
widest_int::from (lh_bits.value (), |
| 519 |
TYPE_SIGN (lh.type ())), |
| 520 |
widest_int::from (lh_bits.mask (), |
| 521 |
TYPE_SIGN (lh.type ()))); |
| 522 |
break; |
| 523 |
case GIMPLE_BINARY_RHS: |
| 524 |
bit_value_binop (code, sign, prec, &widest_value, &widest_mask, |
| 525 |
TYPE_SIGN (lh.type ()), |
| 526 |
TYPE_PRECISION (lh.type ()), |
| 527 |
widest_int::from (lh_bits.value (), sign), |
| 528 |
widest_int::from (lh_bits.mask (), sign), |
| 529 |
TYPE_SIGN (rh.type ()), |
| 530 |
TYPE_PRECISION (rh.type ()), |
| 531 |
widest_int::from (rh_bits.value (), sign), |
| 532 |
widest_int::from (rh_bits.mask (), sign)); |
| 533 |
break; |
| 534 |
default: |
| 535 |
gcc_unreachable (); |
| 536 |
} |
| 537 |
|
| 538 |
wide_int mask = wide_int::from (widest_mask, prec, sign); |
| 539 |
wide_int value = wide_int::from (widest_value, prec, sign); |
| 540 |
// Bitmasks must have the unknown value bits cleared. |
| 541 |
value &= ~mask; |
| 542 |
irange_bitmask bm (value, mask); |
| 543 |
r.update_bitmask (bm); |
| 544 |
} |
| 545 |
|
| 546 |
// Return the upper limit for a type. |
| 547 |
|
| 548 |
static inline wide_int |
| 549 |
max_limit (const_tree type) |
| 550 |
{ |
| 551 |
return irange_val_max (type); |
| 552 |
} |
| 553 |
|
| 554 |
// Return the lower limit for a type. |
| 555 |
|
| 556 |
static inline wide_int |
| 557 |
min_limit (const_tree type) |
| 558 |
{ |
| 559 |
return irange_val_min (type); |
| 560 |
} |
| 561 |
|
| 562 |
// Return false if shifting by OP is undefined behavior. Otherwise, return |
| 563 |
// true and the range it is to be shifted by. This allows trimming out of |
| 564 |
// undefined ranges, leaving only valid ranges if there are any. |
| 565 |
|
| 566 |
static inline bool |
| 567 |
get_shift_range (irange &r, tree type, const irange &op) |
| 568 |
{ |
| 569 |
if (op.undefined_p ()) |
| 570 |
return false; |
| 571 |
|
| 572 |
// Build valid range and intersect it with the shift range. |
| 573 |
r.set (op.type (), |
| 574 |
wi::shwi (0, TYPE_PRECISION (op.type ())), |
| 575 |
wi::shwi (TYPE_PRECISION (type) - 1, TYPE_PRECISION (op.type ()))); |
| 576 |
r.intersect (op); |
| 577 |
|
| 578 |
// If there are no valid ranges in the shift range, returned false. |
| 579 |
if (r.undefined_p ()) |
| 580 |
return false; |
| 581 |
return true; |
| 582 |
} |
| 583 |
|
| 584 |
// Default wide_int fold operation returns [MIN, MAX]. |
| 585 |
|
| 586 |
void |
| 587 |
range_operator::wi_fold (irange &r, tree type, |
| 588 |
const wide_int &lh_lb ATTRIBUTE_UNUSED, |
| 589 |
const wide_int &lh_ub ATTRIBUTE_UNUSED, |
| 590 |
const wide_int &rh_lb ATTRIBUTE_UNUSED, |
| 591 |
const wide_int &rh_ub ATTRIBUTE_UNUSED) const |
| 592 |
{ |
| 593 |
gcc_checking_assert (r.supports_type_p (type)); |
| 594 |
r.set_varying (type); |
| 595 |
} |
| 596 |
|
| 597 |
// Call wi_fold when both op1 and op2 are equivalent. Further split small |
| 598 |
// subranges into constants. This can provide better precision. |
| 599 |
// For x + y, when x == y with a range of [0,4] instead of [0, 8] produce |
| 600 |
// [0,0][2, 2][4,4][6, 6][8, 8] |
| 601 |
// LIMIT is the maximum number of elements in range allowed before we |
| 602 |
// do not process them individually. |
| 603 |
|
| 604 |
void |
| 605 |
range_operator::wi_fold_in_parts_equiv (irange &r, tree type, |
| 606 |
const wide_int &lh_lb, |
| 607 |
const wide_int &lh_ub, |
| 608 |
unsigned limit) const |
| 609 |
{ |
| 610 |
int_range_max tmp; |
| 611 |
widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)), |
| 612 |
widest_int::from (lh_lb, TYPE_SIGN (type))); |
| 613 |
// if there are 1 to 8 values in the LH range, split them up. |
| 614 |
r.set_undefined (); |
| 615 |
if (lh_range >= 0 && lh_range < limit) |
| 616 |
{ |
| 617 |
for (unsigned x = 0; x <= lh_range; x++) |
| 618 |
{ |
| 619 |
wide_int val = lh_lb + x; |
| 620 |
wi_fold (tmp, type, val, val, val, val); |
| 621 |
r.union_ (tmp); |
| 622 |
} |
| 623 |
} |
| 624 |
// Otherwise just call wi_fold. |
| 625 |
else |
| 626 |
wi_fold (r, type, lh_lb, lh_ub, lh_lb, lh_ub); |
| 627 |
} |
| 628 |
|
| 629 |
// Call wi_fold, except further split small subranges into constants. |
| 630 |
// This can provide better precision. For something 8 >> [0,1] |
| 631 |
// Instead of [8, 16], we will produce [8,8][16,16] |
| 632 |
|
| 633 |
void |
| 634 |
range_operator::wi_fold_in_parts (irange &r, tree type, |
| 635 |
const wide_int &lh_lb, |
| 636 |
const wide_int &lh_ub, |
| 637 |
const wide_int &rh_lb, |
| 638 |
const wide_int &rh_ub) const |
| 639 |
{ |
| 640 |
int_range_max tmp; |
| 641 |
widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)), |
| 642 |
widest_int::from (rh_lb, TYPE_SIGN (type))); |
| 643 |
widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)), |
| 644 |
widest_int::from (lh_lb, TYPE_SIGN (type))); |
| 645 |
// If there are 2, 3, or 4 values in the RH range, do them separately. |
| 646 |
// Call wi_fold_in_parts to check the RH side. |
| 647 |
if (rh_range > 0 && rh_range < 4) |
| 648 |
{ |
| 649 |
wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb); |
| 650 |
if (rh_range > 1) |
| 651 |
{ |
| 652 |
wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1); |
| 653 |
r.union_ (tmp); |
| 654 |
if (rh_range == 3) |
| 655 |
{ |
| 656 |
wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2); |
| 657 |
r.union_ (tmp); |
| 658 |
} |
| 659 |
} |
| 660 |
wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub); |
| 661 |
r.union_ (tmp); |
| 662 |
} |
| 663 |
// Otherwise check for 2, 3, or 4 values in the LH range and split them up. |
| 664 |
// The RH side has been checked, so no recursion needed. |
| 665 |
else if (lh_range > 0 && lh_range < 4) |
| 666 |
{ |
| 667 |
wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub); |
| 668 |
if (lh_range > 1) |
| 669 |
{ |
| 670 |
wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub); |
| 671 |
r.union_ (tmp); |
| 672 |
if (lh_range == 3) |
| 673 |
{ |
| 674 |
wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub); |
| 675 |
r.union_ (tmp); |
| 676 |
} |
| 677 |
} |
| 678 |
wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub); |
| 679 |
r.union_ (tmp); |
| 680 |
} |
| 681 |
// Otherwise just call wi_fold. |
| 682 |
else |
| 683 |
wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub); |
| 684 |
} |
| 685 |
|
| 686 |
// The default for fold is to break all ranges into sub-ranges and |
| 687 |
// invoke the wi_fold method on each sub-range pair. |
| 688 |
|
| 689 |
bool |
| 690 |
range_operator::fold_range (irange &r, tree type, |
| 691 |
const irange &lh, |
| 692 |
const irange &rh, |
| 693 |
relation_trio trio) const |
| 694 |
{ |
| 695 |
gcc_checking_assert (r.supports_type_p (type)); |
| 696 |
if (empty_range_varying (r, type, lh, rh)) |
| 697 |
return true; |
| 698 |
|
| 699 |
relation_kind rel = trio.op1_op2 (); |
| 700 |
unsigned num_lh = lh.num_pairs (); |
| 701 |
unsigned num_rh = rh.num_pairs (); |
| 702 |
|
| 703 |
// If op1 and op2 are equivalences, then we don't need a complete cross |
| 704 |
// product, just pairs of matching elements. |
| 705 |
if (relation_equiv_p (rel) && lh == rh) |
| 706 |
{ |
| 707 |
int_range_max tmp; |
| 708 |
r.set_undefined (); |
| 709 |
for (unsigned x = 0; x < num_lh; ++x) |
| 710 |
{ |
| 711 |
// If the number of subranges is too high, limit subrange creation. |
| 712 |
unsigned limit = (r.num_pairs () > 32) ? 0 : 8; |
| 713 |
wide_int lh_lb = lh.lower_bound (x); |
| 714 |
wide_int lh_ub = lh.upper_bound (x); |
| 715 |
wi_fold_in_parts_equiv (tmp, type, lh_lb, lh_ub, limit); |
| 716 |
r.union_ (tmp); |
| 717 |
if (r.varying_p ()) |
| 718 |
break; |
| 719 |
} |
| 720 |
op1_op2_relation_effect (r, type, lh, rh, rel); |
| 721 |
update_bitmask (r, lh, rh); |
| 722 |
return true; |
| 723 |
} |
| 724 |
|
| 725 |
// If both ranges are single pairs, fold directly into the result range. |
| 726 |
// If the number of subranges grows too high, produce a summary result as the |
| 727 |
// loop becomes exponential with little benefit. See PR 103821. |
| 728 |
if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12) |
| 729 |
{ |
| 730 |
wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (), |
| 731 |
rh.lower_bound (), rh.upper_bound ()); |
| 732 |
op1_op2_relation_effect (r, type, lh, rh, rel); |
| 733 |
update_bitmask (r, lh, rh); |
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
int_range_max tmp; |
| 738 |
r.set_undefined (); |
| 739 |
for (unsigned x = 0; x < num_lh; ++x) |
| 740 |
for (unsigned y = 0; y < num_rh; ++y) |
| 741 |
{ |
| 742 |
wide_int lh_lb = lh.lower_bound (x); |
| 743 |
wide_int lh_ub = lh.upper_bound (x); |
| 744 |
wide_int rh_lb = rh.lower_bound (y); |
| 745 |
wide_int rh_ub = rh.upper_bound (y); |
| 746 |
wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub); |
| 747 |
r.union_ (tmp); |
| 748 |
if (r.varying_p ()) |
| 749 |
{ |
| 750 |
op1_op2_relation_effect (r, type, lh, rh, rel); |
| 751 |
update_bitmask (r, lh, rh); |
| 752 |
return true; |
| 753 |
} |
| 754 |
} |
| 755 |
op1_op2_relation_effect (r, type, lh, rh, rel); |
| 756 |
update_bitmask (r, lh, rh); |
| 757 |
return true; |
| 758 |
} |
| 759 |
|
| 760 |
// The default for op1_range is to return false. |
| 761 |
|
| 762 |
bool |
| 763 |
range_operator::op1_range (irange &r ATTRIBUTE_UNUSED, |
| 764 |
tree type ATTRIBUTE_UNUSED, |
| 765 |
const irange &lhs ATTRIBUTE_UNUSED, |
| 766 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 767 |
relation_trio) const |
| 768 |
{ |
| 769 |
return false; |
| 770 |
} |
| 771 |
|
| 772 |
// The default for op2_range is to return false. |
| 773 |
|
| 774 |
bool |
| 775 |
range_operator::op2_range (irange &r ATTRIBUTE_UNUSED, |
| 776 |
tree type ATTRIBUTE_UNUSED, |
| 777 |
const irange &lhs ATTRIBUTE_UNUSED, |
| 778 |
const irange &op1 ATTRIBUTE_UNUSED, |
| 779 |
relation_trio) const |
| 780 |
{ |
| 781 |
return false; |
| 782 |
} |
| 783 |
|
| 784 |
// The default relation routines return VREL_VARYING. |
| 785 |
|
| 786 |
relation_kind |
| 787 |
range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED, |
| 788 |
const irange &op1 ATTRIBUTE_UNUSED, |
| 789 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 790 |
relation_kind rel ATTRIBUTE_UNUSED) const |
| 791 |
{ |
| 792 |
return VREL_VARYING; |
| 793 |
} |
| 794 |
|
| 795 |
relation_kind |
| 796 |
range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED, |
| 797 |
const irange &op1 ATTRIBUTE_UNUSED, |
| 798 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 799 |
relation_kind rel ATTRIBUTE_UNUSED) const |
| 800 |
{ |
| 801 |
return VREL_VARYING; |
| 802 |
} |
| 803 |
|
| 804 |
relation_kind |
| 805 |
range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED, |
| 806 |
const irange &op1 ATTRIBUTE_UNUSED, |
| 807 |
const irange &op2 ATTRIBUTE_UNUSED) const |
| 808 |
{ |
| 809 |
return VREL_VARYING; |
| 810 |
} |
| 811 |
|
| 812 |
// Default is no relation affects the LHS. |
| 813 |
|
| 814 |
bool |
| 815 |
range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED, |
| 816 |
tree type ATTRIBUTE_UNUSED, |
| 817 |
const irange &op1_range ATTRIBUTE_UNUSED, |
| 818 |
const irange &op2_range ATTRIBUTE_UNUSED, |
| 819 |
relation_kind rel ATTRIBUTE_UNUSED) const |
| 820 |
{ |
| 821 |
return false; |
| 822 |
} |
| 823 |
|
| 824 |
bool |
| 825 |
range_operator::overflow_free_p (const irange &, const irange &, |
| 826 |
relation_trio) const |
| 827 |
{ |
| 828 |
return false; |
| 829 |
} |
| 830 |
|
| 831 |
// Apply any known bitmask updates based on this operator. |
| 832 |
|
| 833 |
void |
| 834 |
range_operator::update_bitmask (irange &, const irange &, |
| 835 |
const irange &) const |
| 836 |
{ |
| 837 |
} |
| 838 |
|
| 839 |
// Check that operand types are OK. Default to always OK. |
| 840 |
|
| 841 |
bool |
| 842 |
range_operator::operand_check_p (tree, tree, tree) const |
| 843 |
{ |
| 844 |
return true; |
| 845 |
} |
| 846 |
|
| 847 |
// Create and return a range from a pair of wide-ints that are known |
| 848 |
// to have overflowed (or underflowed). |
| 849 |
|
| 850 |
static void |
| 851 |
value_range_from_overflowed_bounds (irange &r, tree type, |
| 852 |
const wide_int &wmin, |
| 853 |
const wide_int &wmax) |
| 854 |
{ |
| 855 |
const signop sgn = TYPE_SIGN (type); |
| 856 |
const unsigned int prec = TYPE_PRECISION (type); |
| 857 |
|
| 858 |
wide_int tmin = wide_int::from (wmin, prec, sgn); |
| 859 |
wide_int tmax = wide_int::from (wmax, prec, sgn); |
| 860 |
|
| 861 |
bool covers = false; |
| 862 |
wide_int tem = tmin; |
| 863 |
tmin = tmax + 1; |
| 864 |
if (wi::cmp (tmin, tmax, sgn) < 0) |
| 865 |
covers = true; |
| 866 |
tmax = tem - 1; |
| 867 |
if (wi::cmp (tmax, tem, sgn) > 0) |
| 868 |
covers = true; |
| 869 |
|
| 870 |
// If the anti-range would cover nothing, drop to varying. |
| 871 |
// Likewise if the anti-range bounds are outside of the types |
| 872 |
// values. |
| 873 |
if (covers || wi::cmp (tmin, tmax, sgn) > 0) |
| 874 |
r.set_varying (type); |
| 875 |
else |
| 876 |
r.set (type, tmin, tmax, VR_ANTI_RANGE); |
| 877 |
} |
| 878 |
|
| 879 |
// Create and return a range from a pair of wide-ints. MIN_OVF and |
| 880 |
// MAX_OVF describe any overflow that might have occurred while |
| 881 |
// calculating WMIN and WMAX respectively. |
| 882 |
|
| 883 |
static void |
| 884 |
value_range_with_overflow (irange &r, tree type, |
| 885 |
const wide_int &wmin, const wide_int &wmax, |
| 886 |
wi::overflow_type min_ovf = wi::OVF_NONE, |
| 887 |
wi::overflow_type max_ovf = wi::OVF_NONE) |
| 888 |
{ |
| 889 |
const signop sgn = TYPE_SIGN (type); |
| 890 |
const unsigned int prec = TYPE_PRECISION (type); |
| 891 |
const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type); |
| 892 |
|
| 893 |
// For one bit precision if max != min, then the range covers all |
| 894 |
// values. |
| 895 |
if (prec == 1 && wi::ne_p (wmax, wmin)) |
| 896 |
{ |
| 897 |
r.set_varying (type); |
| 898 |
return; |
| 899 |
} |
| 900 |
|
| 901 |
if (overflow_wraps) |
| 902 |
{ |
| 903 |
// If overflow wraps, truncate the values and adjust the range, |
| 904 |
// kind, and bounds appropriately. |
| 905 |
if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE)) |
| 906 |
{ |
| 907 |
wide_int tmin = wide_int::from (wmin, prec, sgn); |
| 908 |
wide_int tmax = wide_int::from (wmax, prec, sgn); |
| 909 |
// If the limits are swapped, we wrapped around and cover |
| 910 |
// the entire range. |
| 911 |
if (wi::gt_p (tmin, tmax, sgn)) |
| 912 |
r.set_varying (type); |
| 913 |
else |
| 914 |
// No overflow or both overflow or underflow. The range |
| 915 |
// kind stays normal. |
| 916 |
r.set (type, tmin, tmax); |
| 917 |
return; |
| 918 |
} |
| 919 |
|
| 920 |
if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE) |
| 921 |
|| (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE)) |
| 922 |
value_range_from_overflowed_bounds (r, type, wmin, wmax); |
| 923 |
else |
| 924 |
// Other underflow and/or overflow, drop to VR_VARYING. |
| 925 |
r.set_varying (type); |
| 926 |
} |
| 927 |
else |
| 928 |
{ |
| 929 |
// If both bounds either underflowed or overflowed, then the result |
| 930 |
// is undefined. |
| 931 |
if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW) |
| 932 |
|| (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW)) |
| 933 |
{ |
| 934 |
r.set_undefined (); |
| 935 |
return; |
| 936 |
} |
| 937 |
|
| 938 |
// If overflow does not wrap, saturate to [MIN, MAX]. |
| 939 |
wide_int new_lb, new_ub; |
| 940 |
if (min_ovf == wi::OVF_UNDERFLOW) |
| 941 |
new_lb = wi::min_value (prec, sgn); |
| 942 |
else if (min_ovf == wi::OVF_OVERFLOW) |
| 943 |
new_lb = wi::max_value (prec, sgn); |
| 944 |
else |
| 945 |
new_lb = wmin; |
| 946 |
|
| 947 |
if (max_ovf == wi::OVF_UNDERFLOW) |
| 948 |
new_ub = wi::min_value (prec, sgn); |
| 949 |
else if (max_ovf == wi::OVF_OVERFLOW) |
| 950 |
new_ub = wi::max_value (prec, sgn); |
| 951 |
else |
| 952 |
new_ub = wmax; |
| 953 |
|
| 954 |
r.set (type, new_lb, new_ub); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
// Create and return a range from a pair of wide-ints. Canonicalize |
| 959 |
// the case where the bounds are swapped. In which case, we transform |
| 960 |
// [10,5] into [MIN,5][10,MAX]. |
| 961 |
|
| 962 |
static inline void |
| 963 |
create_possibly_reversed_range (irange &r, tree type, |
| 964 |
const wide_int &new_lb, const wide_int &new_ub) |
| 965 |
{ |
| 966 |
signop s = TYPE_SIGN (type); |
| 967 |
// If the bounds are swapped, treat the result as if an overflow occurred. |
| 968 |
if (wi::gt_p (new_lb, new_ub, s)) |
| 969 |
value_range_from_overflowed_bounds (r, type, new_lb, new_ub); |
| 970 |
else |
| 971 |
// Otherwise it's just a normal range. |
| 972 |
r.set (type, new_lb, new_ub); |
| 973 |
} |
| 974 |
|
| 975 |
// Return the summary information about boolean range LHS. If EMPTY/FULL, |
| 976 |
// return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing. |
| 977 |
|
| 978 |
bool_range_state |
| 979 |
get_bool_state (vrange &r, const vrange &lhs, tree val_type) |
| 980 |
{ |
| 981 |
// If there is no result, then this is unexecutable. |
| 982 |
if (lhs.undefined_p ()) |
| 983 |
{ |
| 984 |
r.set_undefined (); |
| 985 |
return BRS_EMPTY; |
| 986 |
} |
| 987 |
|
| 988 |
if (lhs.zero_p ()) |
| 989 |
return BRS_FALSE; |
| 990 |
|
| 991 |
// For TRUE, we can't just test for [1,1] because Ada can have |
| 992 |
// multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc. |
| 993 |
if (lhs.contains_p (build_zero_cst (lhs.type ()))) |
| 994 |
{ |
| 995 |
r.set_varying (val_type); |
| 996 |
return BRS_FULL; |
| 997 |
} |
| 998 |
|
| 999 |
return BRS_TRUE; |
| 1000 |
} |
| 1001 |
|
| 1002 |
// ------------------------------------------------------------------------ |
| 1003 |
|
| 1004 |
void |
| 1005 |
operator_equal::update_bitmask (irange &r, const irange &lh, |
| 1006 |
const irange &rh) const |
| 1007 |
{ |
| 1008 |
update_known_bitmask (r, EQ_EXPR, lh, rh); |
| 1009 |
} |
| 1010 |
|
| 1011 |
// Check if the LHS range indicates a relation between OP1 and OP2. |
| 1012 |
|
| 1013 |
relation_kind |
| 1014 |
operator_equal::op1_op2_relation (const irange &lhs, const irange &, |
| 1015 |
const irange &) const |
| 1016 |
{ |
| 1017 |
if (lhs.undefined_p ()) |
| 1018 |
return VREL_UNDEFINED; |
| 1019 |
|
| 1020 |
// FALSE = op1 == op2 indicates NE_EXPR. |
| 1021 |
if (lhs.zero_p ()) |
| 1022 |
return VREL_NE; |
| 1023 |
|
| 1024 |
// TRUE = op1 == op2 indicates EQ_EXPR. |
| 1025 |
if (!contains_zero_p (lhs)) |
| 1026 |
return VREL_EQ; |
| 1027 |
return VREL_VARYING; |
| 1028 |
} |
| 1029 |
|
| 1030 |
bool |
| 1031 |
operator_equal::fold_range (irange &r, tree type, |
| 1032 |
const irange &op1, |
| 1033 |
const irange &op2, |
| 1034 |
relation_trio rel) const |
| 1035 |
{ |
| 1036 |
if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ)) |
| 1037 |
return true; |
| 1038 |
|
| 1039 |
// We can be sure the values are always equal or not if both ranges |
| 1040 |
// consist of a single value, and then compare them. |
| 1041 |
bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ()); |
| 1042 |
bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ()); |
| 1043 |
if (op1_const && op2_const) |
| 1044 |
{ |
| 1045 |
if (wi::eq_p (op1.lower_bound (), op2.upper_bound())) |
| 1046 |
r = range_true (type); |
| 1047 |
else |
| 1048 |
r = range_false (type); |
| 1049 |
} |
| 1050 |
else |
| 1051 |
{ |
| 1052 |
// If ranges do not intersect, we know the range is not equal, |
| 1053 |
// otherwise we don't know anything for sure. |
| 1054 |
int_range_max tmp = op1; |
| 1055 |
tmp.intersect (op2); |
| 1056 |
if (tmp.undefined_p ()) |
| 1057 |
r = range_false (type); |
| 1058 |
// Check if a constant cannot satisfy the bitmask requirements. |
| 1059 |
else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ())) |
| 1060 |
r = range_false (type); |
| 1061 |
else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ())) |
| 1062 |
r = range_false (type); |
| 1063 |
else |
| 1064 |
r = range_true_and_false (type); |
| 1065 |
} |
| 1066 |
return true; |
| 1067 |
} |
| 1068 |
|
| 1069 |
bool |
| 1070 |
operator_equal::op1_range (irange &r, tree type, |
| 1071 |
const irange &lhs, |
| 1072 |
const irange &op2, |
| 1073 |
relation_trio) const |
| 1074 |
{ |
| 1075 |
switch (get_bool_state (r, lhs, type)) |
| 1076 |
{ |
| 1077 |
case BRS_TRUE: |
| 1078 |
// If it's true, the result is the same as OP2. |
| 1079 |
r = op2; |
| 1080 |
break; |
| 1081 |
|
| 1082 |
case BRS_FALSE: |
| 1083 |
// If the result is false, the only time we know anything is |
| 1084 |
// if OP2 is a constant. |
| 1085 |
if (!op2.undefined_p () |
| 1086 |
&& wi::eq_p (op2.lower_bound(), op2.upper_bound())) |
| 1087 |
{ |
| 1088 |
r = op2; |
| 1089 |
r.invert (); |
| 1090 |
} |
| 1091 |
else |
| 1092 |
r.set_varying (type); |
| 1093 |
break; |
| 1094 |
|
| 1095 |
default: |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
return true; |
| 1099 |
} |
| 1100 |
|
| 1101 |
bool |
| 1102 |
operator_equal::op2_range (irange &r, tree type, |
| 1103 |
const irange &lhs, |
| 1104 |
const irange &op1, |
| 1105 |
relation_trio rel) const |
| 1106 |
{ |
| 1107 |
return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ()); |
| 1108 |
} |
| 1109 |
|
| 1110 |
// ------------------------------------------------------------------------- |
| 1111 |
|
| 1112 |
void |
| 1113 |
operator_not_equal::update_bitmask (irange &r, const irange &lh, |
| 1114 |
const irange &rh) const |
| 1115 |
{ |
| 1116 |
update_known_bitmask (r, NE_EXPR, lh, rh); |
| 1117 |
} |
| 1118 |
|
| 1119 |
// Check if the LHS range indicates a relation between OP1 and OP2. |
| 1120 |
|
| 1121 |
relation_kind |
| 1122 |
operator_not_equal::op1_op2_relation (const irange &lhs, const irange &, |
| 1123 |
const irange &) const |
| 1124 |
{ |
| 1125 |
if (lhs.undefined_p ()) |
| 1126 |
return VREL_UNDEFINED; |
| 1127 |
|
| 1128 |
// FALSE = op1 != op2 indicates EQ_EXPR. |
| 1129 |
if (lhs.zero_p ()) |
| 1130 |
return VREL_EQ; |
| 1131 |
|
| 1132 |
// TRUE = op1 != op2 indicates NE_EXPR. |
| 1133 |
if (!contains_zero_p (lhs)) |
| 1134 |
return VREL_NE; |
| 1135 |
return VREL_VARYING; |
| 1136 |
} |
| 1137 |
|
| 1138 |
bool |
| 1139 |
operator_not_equal::fold_range (irange &r, tree type, |
| 1140 |
const irange &op1, |
| 1141 |
const irange &op2, |
| 1142 |
relation_trio rel) const |
| 1143 |
{ |
| 1144 |
if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE)) |
| 1145 |
return true; |
| 1146 |
|
| 1147 |
// We can be sure the values are always equal or not if both ranges |
| 1148 |
// consist of a single value, and then compare them. |
| 1149 |
bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ()); |
| 1150 |
bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ()); |
| 1151 |
if (op1_const && op2_const) |
| 1152 |
{ |
| 1153 |
if (wi::ne_p (op1.lower_bound (), op2.upper_bound())) |
| 1154 |
r = range_true (type); |
| 1155 |
else |
| 1156 |
r = range_false (type); |
| 1157 |
} |
| 1158 |
else |
| 1159 |
{ |
| 1160 |
// If ranges do not intersect, we know the range is not equal, |
| 1161 |
// otherwise we don't know anything for sure. |
| 1162 |
int_range_max tmp = op1; |
| 1163 |
tmp.intersect (op2); |
| 1164 |
if (tmp.undefined_p ()) |
| 1165 |
r = range_true (type); |
| 1166 |
// Check if a constant cannot satisfy the bitmask requirements. |
| 1167 |
else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ())) |
| 1168 |
r = range_true (type); |
| 1169 |
else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ())) |
| 1170 |
r = range_true (type); |
| 1171 |
else |
| 1172 |
r = range_true_and_false (type); |
| 1173 |
} |
| 1174 |
return true; |
| 1175 |
} |
| 1176 |
|
| 1177 |
bool |
| 1178 |
operator_not_equal::op1_range (irange &r, tree type, |
| 1179 |
const irange &lhs, |
| 1180 |
const irange &op2, |
| 1181 |
relation_trio) const |
| 1182 |
{ |
| 1183 |
switch (get_bool_state (r, lhs, type)) |
| 1184 |
{ |
| 1185 |
case BRS_TRUE: |
| 1186 |
// If the result is true, the only time we know anything is if |
| 1187 |
// OP2 is a constant. |
| 1188 |
if (!op2.undefined_p () |
| 1189 |
&& wi::eq_p (op2.lower_bound(), op2.upper_bound())) |
| 1190 |
{ |
| 1191 |
r = op2; |
| 1192 |
r.invert (); |
| 1193 |
} |
| 1194 |
else |
| 1195 |
r.set_varying (type); |
| 1196 |
break; |
| 1197 |
|
| 1198 |
case BRS_FALSE: |
| 1199 |
// If it's false, the result is the same as OP2. |
| 1200 |
r = op2; |
| 1201 |
break; |
| 1202 |
|
| 1203 |
default: |
| 1204 |
break; |
| 1205 |
} |
| 1206 |
return true; |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
bool |
| 1211 |
operator_not_equal::op2_range (irange &r, tree type, |
| 1212 |
const irange &lhs, |
| 1213 |
const irange &op1, |
| 1214 |
relation_trio rel) const |
| 1215 |
{ |
| 1216 |
return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ()); |
| 1217 |
} |
| 1218 |
|
| 1219 |
// (X < VAL) produces the range of [MIN, VAL - 1]. |
| 1220 |
|
| 1221 |
static void |
| 1222 |
build_lt (irange &r, tree type, const wide_int &val) |
| 1223 |
{ |
| 1224 |
wi::overflow_type ov; |
| 1225 |
wide_int lim; |
| 1226 |
signop sgn = TYPE_SIGN (type); |
| 1227 |
|
| 1228 |
// Signed 1 bit cannot represent 1 for subtraction. |
| 1229 |
if (sgn == SIGNED) |
| 1230 |
lim = wi::add (val, -1, sgn, &ov); |
| 1231 |
else |
| 1232 |
lim = wi::sub (val, 1, sgn, &ov); |
| 1233 |
|
| 1234 |
// If val - 1 underflows, check if X < MIN, which is an empty range. |
| 1235 |
if (ov) |
| 1236 |
r.set_undefined (); |
| 1237 |
else |
| 1238 |
r = int_range<1> (type, min_limit (type), lim); |
| 1239 |
} |
| 1240 |
|
| 1241 |
// (X <= VAL) produces the range of [MIN, VAL]. |
| 1242 |
|
| 1243 |
static void |
| 1244 |
build_le (irange &r, tree type, const wide_int &val) |
| 1245 |
{ |
| 1246 |
r = int_range<1> (type, min_limit (type), val); |
| 1247 |
} |
| 1248 |
|
| 1249 |
// (X > VAL) produces the range of [VAL + 1, MAX]. |
| 1250 |
|
| 1251 |
static void |
| 1252 |
build_gt (irange &r, tree type, const wide_int &val) |
| 1253 |
{ |
| 1254 |
wi::overflow_type ov; |
| 1255 |
wide_int lim; |
| 1256 |
signop sgn = TYPE_SIGN (type); |
| 1257 |
|
| 1258 |
// Signed 1 bit cannot represent 1 for addition. |
| 1259 |
if (sgn == SIGNED) |
| 1260 |
lim = wi::sub (val, -1, sgn, &ov); |
| 1261 |
else |
| 1262 |
lim = wi::add (val, 1, sgn, &ov); |
| 1263 |
// If val + 1 overflows, check is for X > MAX, which is an empty range. |
| 1264 |
if (ov) |
| 1265 |
r.set_undefined (); |
| 1266 |
else |
| 1267 |
r = int_range<1> (type, lim, max_limit (type)); |
| 1268 |
} |
| 1269 |
|
| 1270 |
// (X >= val) produces the range of [VAL, MAX]. |
| 1271 |
|
| 1272 |
static void |
| 1273 |
build_ge (irange &r, tree type, const wide_int &val) |
| 1274 |
{ |
| 1275 |
r = int_range<1> (type, val, max_limit (type)); |
| 1276 |
} |
| 1277 |
|
| 1278 |
|
| 1279 |
void |
| 1280 |
operator_lt::update_bitmask (irange &r, const irange &lh, |
| 1281 |
const irange &rh) const |
| 1282 |
{ |
| 1283 |
update_known_bitmask (r, LT_EXPR, lh, rh); |
| 1284 |
} |
| 1285 |
|
| 1286 |
// Check if the LHS range indicates a relation between OP1 and OP2. |
| 1287 |
|
| 1288 |
relation_kind |
| 1289 |
operator_lt::op1_op2_relation (const irange &lhs, const irange &, |
| 1290 |
const irange &) const |
| 1291 |
{ |
| 1292 |
if (lhs.undefined_p ()) |
| 1293 |
return VREL_UNDEFINED; |
| 1294 |
|
| 1295 |
// FALSE = op1 < op2 indicates GE_EXPR. |
| 1296 |
if (lhs.zero_p ()) |
| 1297 |
return VREL_GE; |
| 1298 |
|
| 1299 |
// TRUE = op1 < op2 indicates LT_EXPR. |
| 1300 |
if (!contains_zero_p (lhs)) |
| 1301 |
return VREL_LT; |
| 1302 |
return VREL_VARYING; |
| 1303 |
} |
| 1304 |
|
| 1305 |
bool |
| 1306 |
operator_lt::fold_range (irange &r, tree type, |
| 1307 |
const irange &op1, |
| 1308 |
const irange &op2, |
| 1309 |
relation_trio rel) const |
| 1310 |
{ |
| 1311 |
if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT)) |
| 1312 |
return true; |
| 1313 |
|
| 1314 |
signop sign = TYPE_SIGN (op1.type ()); |
| 1315 |
gcc_checking_assert (sign == TYPE_SIGN (op2.type ())); |
| 1316 |
|
| 1317 |
if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign)) |
| 1318 |
r = range_true (type); |
| 1319 |
else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign)) |
| 1320 |
r = range_false (type); |
| 1321 |
// Use nonzero bits to determine if < 0 is false. |
| 1322 |
else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign)) |
| 1323 |
r = range_false (type); |
| 1324 |
else |
| 1325 |
r = range_true_and_false (type); |
| 1326 |
return true; |
| 1327 |
} |
| 1328 |
|
| 1329 |
bool |
| 1330 |
operator_lt::op1_range (irange &r, tree type, |
| 1331 |
const irange &lhs, |
| 1332 |
const irange &op2, |
| 1333 |
relation_trio) const |
| 1334 |
{ |
| 1335 |
if (op2.undefined_p ()) |
| 1336 |
return false; |
| 1337 |
|
| 1338 |
switch (get_bool_state (r, lhs, type)) |
| 1339 |
{ |
| 1340 |
case BRS_TRUE: |
| 1341 |
build_lt (r, type, op2.upper_bound ()); |
| 1342 |
break; |
| 1343 |
|
| 1344 |
case BRS_FALSE: |
| 1345 |
build_ge (r, type, op2.lower_bound ()); |
| 1346 |
break; |
| 1347 |
|
| 1348 |
default: |
| 1349 |
break; |
| 1350 |
} |
| 1351 |
return true; |
| 1352 |
} |
| 1353 |
|
| 1354 |
bool |
| 1355 |
operator_lt::op2_range (irange &r, tree type, |
| 1356 |
const irange &lhs, |
| 1357 |
const irange &op1, |
| 1358 |
relation_trio) const |
| 1359 |
{ |
| 1360 |
if (op1.undefined_p ()) |
| 1361 |
return false; |
| 1362 |
|
| 1363 |
switch (get_bool_state (r, lhs, type)) |
| 1364 |
{ |
| 1365 |
case BRS_TRUE: |
| 1366 |
build_gt (r, type, op1.lower_bound ()); |
| 1367 |
break; |
| 1368 |
|
| 1369 |
case BRS_FALSE: |
| 1370 |
build_le (r, type, op1.upper_bound ()); |
| 1371 |
break; |
| 1372 |
|
| 1373 |
default: |
| 1374 |
break; |
| 1375 |
} |
| 1376 |
return true; |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
void |
| 1381 |
operator_le::update_bitmask (irange &r, const irange &lh, |
| 1382 |
const irange &rh) const |
| 1383 |
{ |
| 1384 |
update_known_bitmask (r, LE_EXPR, lh, rh); |
| 1385 |
} |
| 1386 |
|
| 1387 |
// Check if the LHS range indicates a relation between OP1 and OP2. |
| 1388 |
|
| 1389 |
relation_kind |
| 1390 |
operator_le::op1_op2_relation (const irange &lhs, const irange &, |
| 1391 |
const irange &) const |
| 1392 |
{ |
| 1393 |
if (lhs.undefined_p ()) |
| 1394 |
return VREL_UNDEFINED; |
| 1395 |
|
| 1396 |
// FALSE = op1 <= op2 indicates GT_EXPR. |
| 1397 |
if (lhs.zero_p ()) |
| 1398 |
return VREL_GT; |
| 1399 |
|
| 1400 |
// TRUE = op1 <= op2 indicates LE_EXPR. |
| 1401 |
if (!contains_zero_p (lhs)) |
| 1402 |
return VREL_LE; |
| 1403 |
return VREL_VARYING; |
| 1404 |
} |
| 1405 |
|
| 1406 |
bool |
| 1407 |
operator_le::fold_range (irange &r, tree type, |
| 1408 |
const irange &op1, |
| 1409 |
const irange &op2, |
| 1410 |
relation_trio rel) const |
| 1411 |
{ |
| 1412 |
if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE)) |
| 1413 |
return true; |
| 1414 |
|
| 1415 |
signop sign = TYPE_SIGN (op1.type ()); |
| 1416 |
gcc_checking_assert (sign == TYPE_SIGN (op2.type ())); |
| 1417 |
|
| 1418 |
if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign)) |
| 1419 |
r = range_true (type); |
| 1420 |
else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign)) |
| 1421 |
r = range_false (type); |
| 1422 |
else |
| 1423 |
r = range_true_and_false (type); |
| 1424 |
return true; |
| 1425 |
} |
| 1426 |
|
| 1427 |
bool |
| 1428 |
operator_le::op1_range (irange &r, tree type, |
| 1429 |
const irange &lhs, |
| 1430 |
const irange &op2, |
| 1431 |
relation_trio) const |
| 1432 |
{ |
| 1433 |
if (op2.undefined_p ()) |
| 1434 |
return false; |
| 1435 |
|
| 1436 |
switch (get_bool_state (r, lhs, type)) |
| 1437 |
{ |
| 1438 |
case BRS_TRUE: |
| 1439 |
build_le (r, type, op2.upper_bound ()); |
| 1440 |
break; |
| 1441 |
|
| 1442 |
case BRS_FALSE: |
| 1443 |
build_gt (r, type, op2.lower_bound ()); |
| 1444 |
break; |
| 1445 |
|
| 1446 |
default: |
| 1447 |
break; |
| 1448 |
} |
| 1449 |
return true; |
| 1450 |
} |
| 1451 |
|
| 1452 |
bool |
| 1453 |
operator_le::op2_range (irange &r, tree type, |
| 1454 |
const irange &lhs, |
| 1455 |
const irange &op1, |
| 1456 |
relation_trio) const |
| 1457 |
{ |
| 1458 |
if (op1.undefined_p ()) |
| 1459 |
return false; |
| 1460 |
|
| 1461 |
switch (get_bool_state (r, lhs, type)) |
| 1462 |
{ |
| 1463 |
case BRS_TRUE: |
| 1464 |
build_ge (r, type, op1.lower_bound ()); |
| 1465 |
break; |
| 1466 |
|
| 1467 |
case BRS_FALSE: |
| 1468 |
build_lt (r, type, op1.upper_bound ()); |
| 1469 |
break; |
| 1470 |
|
| 1471 |
default: |
| 1472 |
break; |
| 1473 |
} |
| 1474 |
return true; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
void |
| 1479 |
operator_gt::update_bitmask (irange &r, const irange &lh, |
| 1480 |
const irange &rh) const |
| 1481 |
{ |
| 1482 |
update_known_bitmask (r, GT_EXPR, lh, rh); |
| 1483 |
} |
| 1484 |
|
| 1485 |
// Check if the LHS range indicates a relation between OP1 and OP2. |
| 1486 |
|
| 1487 |
relation_kind |
| 1488 |
operator_gt::op1_op2_relation (const irange &lhs, const irange &, |
| 1489 |
const irange &) const |
| 1490 |
{ |
| 1491 |
if (lhs.undefined_p ()) |
| 1492 |
return VREL_UNDEFINED; |
| 1493 |
|
| 1494 |
// FALSE = op1 > op2 indicates LE_EXPR. |
| 1495 |
if (lhs.zero_p ()) |
| 1496 |
return VREL_LE; |
| 1497 |
|
| 1498 |
// TRUE = op1 > op2 indicates GT_EXPR. |
| 1499 |
if (!contains_zero_p (lhs)) |
| 1500 |
return VREL_GT; |
| 1501 |
return VREL_VARYING; |
| 1502 |
} |
| 1503 |
|
| 1504 |
bool |
| 1505 |
operator_gt::fold_range (irange &r, tree type, |
| 1506 |
const irange &op1, const irange &op2, |
| 1507 |
relation_trio rel) const |
| 1508 |
{ |
| 1509 |
if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT)) |
| 1510 |
return true; |
| 1511 |
|
| 1512 |
signop sign = TYPE_SIGN (op1.type ()); |
| 1513 |
gcc_checking_assert (sign == TYPE_SIGN (op2.type ())); |
| 1514 |
|
| 1515 |
if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign)) |
| 1516 |
r = range_true (type); |
| 1517 |
else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign)) |
| 1518 |
r = range_false (type); |
| 1519 |
else |
| 1520 |
r = range_true_and_false (type); |
| 1521 |
return true; |
| 1522 |
} |
| 1523 |
|
| 1524 |
bool |
| 1525 |
operator_gt::op1_range (irange &r, tree type, |
| 1526 |
const irange &lhs, const irange &op2, |
| 1527 |
relation_trio) const |
| 1528 |
{ |
| 1529 |
if (op2.undefined_p ()) |
| 1530 |
return false; |
| 1531 |
|
| 1532 |
switch (get_bool_state (r, lhs, type)) |
| 1533 |
{ |
| 1534 |
case BRS_TRUE: |
| 1535 |
build_gt (r, type, op2.lower_bound ()); |
| 1536 |
break; |
| 1537 |
|
| 1538 |
case BRS_FALSE: |
| 1539 |
build_le (r, type, op2.upper_bound ()); |
| 1540 |
break; |
| 1541 |
|
| 1542 |
default: |
| 1543 |
break; |
| 1544 |
} |
| 1545 |
return true; |
| 1546 |
} |
| 1547 |
|
| 1548 |
bool |
| 1549 |
operator_gt::op2_range (irange &r, tree type, |
| 1550 |
const irange &lhs, |
| 1551 |
const irange &op1, |
| 1552 |
relation_trio) const |
| 1553 |
{ |
| 1554 |
if (op1.undefined_p ()) |
| 1555 |
return false; |
| 1556 |
|
| 1557 |
switch (get_bool_state (r, lhs, type)) |
| 1558 |
{ |
| 1559 |
case BRS_TRUE: |
| 1560 |
build_lt (r, type, op1.upper_bound ()); |
| 1561 |
break; |
| 1562 |
|
| 1563 |
case BRS_FALSE: |
| 1564 |
build_ge (r, type, op1.lower_bound ()); |
| 1565 |
break; |
| 1566 |
|
| 1567 |
default: |
| 1568 |
break; |
| 1569 |
} |
| 1570 |
return true; |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
void |
| 1575 |
operator_ge::update_bitmask (irange &r, const irange &lh, |
| 1576 |
const irange &rh) const |
| 1577 |
{ |
| 1578 |
update_known_bitmask (r, GE_EXPR, lh, rh); |
| 1579 |
} |
| 1580 |
|
| 1581 |
// Check if the LHS range indicates a relation between OP1 and OP2. |
| 1582 |
|
| 1583 |
relation_kind |
| 1584 |
operator_ge::op1_op2_relation (const irange &lhs, const irange &, |
| 1585 |
const irange &) const |
| 1586 |
{ |
| 1587 |
if (lhs.undefined_p ()) |
| 1588 |
return VREL_UNDEFINED; |
| 1589 |
|
| 1590 |
// FALSE = op1 >= op2 indicates LT_EXPR. |
| 1591 |
if (lhs.zero_p ()) |
| 1592 |
return VREL_LT; |
| 1593 |
|
| 1594 |
// TRUE = op1 >= op2 indicates GE_EXPR. |
| 1595 |
if (!contains_zero_p (lhs)) |
| 1596 |
return VREL_GE; |
| 1597 |
return VREL_VARYING; |
| 1598 |
} |
| 1599 |
|
| 1600 |
bool |
| 1601 |
operator_ge::fold_range (irange &r, tree type, |
| 1602 |
const irange &op1, |
| 1603 |
const irange &op2, |
| 1604 |
relation_trio rel) const |
| 1605 |
{ |
| 1606 |
if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE)) |
| 1607 |
return true; |
| 1608 |
|
| 1609 |
signop sign = TYPE_SIGN (op1.type ()); |
| 1610 |
gcc_checking_assert (sign == TYPE_SIGN (op2.type ())); |
| 1611 |
|
| 1612 |
if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign)) |
| 1613 |
r = range_true (type); |
| 1614 |
else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign)) |
| 1615 |
r = range_false (type); |
| 1616 |
else |
| 1617 |
r = range_true_and_false (type); |
| 1618 |
return true; |
| 1619 |
} |
| 1620 |
|
| 1621 |
bool |
| 1622 |
operator_ge::op1_range (irange &r, tree type, |
| 1623 |
const irange &lhs, |
| 1624 |
const irange &op2, |
| 1625 |
relation_trio) const |
| 1626 |
{ |
| 1627 |
if (op2.undefined_p ()) |
| 1628 |
return false; |
| 1629 |
|
| 1630 |
switch (get_bool_state (r, lhs, type)) |
| 1631 |
{ |
| 1632 |
case BRS_TRUE: |
| 1633 |
build_ge (r, type, op2.lower_bound ()); |
| 1634 |
break; |
| 1635 |
|
| 1636 |
case BRS_FALSE: |
| 1637 |
build_lt (r, type, op2.upper_bound ()); |
| 1638 |
break; |
| 1639 |
|
| 1640 |
default: |
| 1641 |
break; |
| 1642 |
} |
| 1643 |
return true; |
| 1644 |
} |
| 1645 |
|
| 1646 |
bool |
| 1647 |
operator_ge::op2_range (irange &r, tree type, |
| 1648 |
const irange &lhs, |
| 1649 |
const irange &op1, |
| 1650 |
relation_trio) const |
| 1651 |
{ |
| 1652 |
if (op1.undefined_p ()) |
| 1653 |
return false; |
| 1654 |
|
| 1655 |
switch (get_bool_state (r, lhs, type)) |
| 1656 |
{ |
| 1657 |
case BRS_TRUE: |
| 1658 |
build_le (r, type, op1.upper_bound ()); |
| 1659 |
break; |
| 1660 |
|
| 1661 |
case BRS_FALSE: |
| 1662 |
build_gt (r, type, op1.lower_bound ()); |
| 1663 |
break; |
| 1664 |
|
| 1665 |
default: |
| 1666 |
break; |
| 1667 |
} |
| 1668 |
return true; |
| 1669 |
} |
| 1670 |
|
| 1671 |
|
| 1672 |
void |
| 1673 |
operator_plus::update_bitmask (irange &r, const irange &lh, |
| 1674 |
const irange &rh) const |
| 1675 |
{ |
| 1676 |
update_known_bitmask (r, PLUS_EXPR, lh, rh); |
| 1677 |
} |
| 1678 |
|
| 1679 |
// Check to see if the range of OP2 indicates anything about the relation |
| 1680 |
// between LHS and OP1. |
| 1681 |
|
| 1682 |
relation_kind |
| 1683 |
operator_plus::lhs_op1_relation (const irange &lhs, |
| 1684 |
const irange &op1, |
| 1685 |
const irange &op2, |
| 1686 |
relation_kind) const |
| 1687 |
{ |
| 1688 |
if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ()) |
| 1689 |
return VREL_VARYING; |
| 1690 |
|
| 1691 |
tree type = lhs.type (); |
| 1692 |
unsigned prec = TYPE_PRECISION (type); |
| 1693 |
wi::overflow_type ovf1, ovf2; |
| 1694 |
signop sign = TYPE_SIGN (type); |
| 1695 |
|
| 1696 |
// LHS = OP1 + 0 indicates LHS == OP1. |
| 1697 |
if (op2.zero_p ()) |
| 1698 |
return VREL_EQ; |
| 1699 |
|
| 1700 |
if (TYPE_OVERFLOW_WRAPS (type)) |
| 1701 |
{ |
| 1702 |
wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1); |
| 1703 |
wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2); |
| 1704 |
} |
| 1705 |
else |
| 1706 |
ovf1 = ovf2 = wi::OVF_NONE; |
| 1707 |
|
| 1708 |
// Never wrapping additions. |
| 1709 |
if (!ovf1 && !ovf2) |
| 1710 |
{ |
| 1711 |
// Positive op2 means lhs > op1. |
| 1712 |
if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign)) |
| 1713 |
return VREL_GT; |
| 1714 |
if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign)) |
| 1715 |
return VREL_GE; |
| 1716 |
|
| 1717 |
// Negative op2 means lhs < op1. |
| 1718 |
if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign)) |
| 1719 |
return VREL_LT; |
| 1720 |
if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign)) |
| 1721 |
return VREL_LE; |
| 1722 |
} |
| 1723 |
// Always wrapping additions. |
| 1724 |
else if (ovf1 && ovf1 == ovf2) |
| 1725 |
{ |
| 1726 |
// Positive op2 means lhs < op1. |
| 1727 |
if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign)) |
| 1728 |
return VREL_LT; |
| 1729 |
if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign)) |
| 1730 |
return VREL_LE; |
| 1731 |
|
| 1732 |
// Negative op2 means lhs > op1. |
| 1733 |
if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign)) |
| 1734 |
return VREL_GT; |
| 1735 |
if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign)) |
| 1736 |
return VREL_GE; |
| 1737 |
} |
| 1738 |
|
| 1739 |
// If op2 does not contain 0, then LHS and OP1 can never be equal. |
| 1740 |
if (!range_includes_zero_p (op2)) |
| 1741 |
return VREL_NE; |
| 1742 |
|
| 1743 |
return VREL_VARYING; |
| 1744 |
} |
| 1745 |
|
| 1746 |
// PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed |
| 1747 |
// operands. |
| 1748 |
|
| 1749 |
relation_kind |
| 1750 |
operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1, |
| 1751 |
const irange &op2, relation_kind rel) const |
| 1752 |
{ |
| 1753 |
return lhs_op1_relation (lhs, op2, op1, rel); |
| 1754 |
} |
| 1755 |
|
| 1756 |
void |
| 1757 |
operator_plus::wi_fold (irange &r, tree type, |
| 1758 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 1759 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 1760 |
{ |
| 1761 |
wi::overflow_type ov_lb, ov_ub; |
| 1762 |
signop s = TYPE_SIGN (type); |
| 1763 |
wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb); |
| 1764 |
wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub); |
| 1765 |
value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub); |
| 1766 |
} |
| 1767 |
|
| 1768 |
// Given addition or subtraction, determine the possible NORMAL ranges and |
| 1769 |
// OVERFLOW ranges given an OFFSET range. ADD_P is true for addition. |
| 1770 |
// Return the relation that exists between the LHS and OP1 in order for the |
| 1771 |
// NORMAL range to apply. |
| 1772 |
// a return value of VREL_VARYING means no ranges were applicable. |
| 1773 |
|
| 1774 |
static relation_kind |
| 1775 |
plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset, |
| 1776 |
bool add_p) |
| 1777 |
{ |
| 1778 |
relation_kind kind = VREL_VARYING; |
| 1779 |
// For now, only deal with constant adds. This could be extended to ranges |
| 1780 |
// when someone is so motivated. |
| 1781 |
if (!offset.singleton_p () || offset.zero_p ()) |
| 1782 |
return kind; |
| 1783 |
|
| 1784 |
// Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2 |
| 1785 |
wide_int off = offset.lower_bound (); |
| 1786 |
if (wi::neg_p (off, SIGNED)) |
| 1787 |
{ |
| 1788 |
add_p = !add_p; |
| 1789 |
off = wi::neg (off); |
| 1790 |
} |
| 1791 |
|
| 1792 |
wi::overflow_type ov; |
| 1793 |
tree type = offset.type (); |
| 1794 |
unsigned prec = TYPE_PRECISION (type); |
| 1795 |
wide_int ub; |
| 1796 |
wide_int lb; |
| 1797 |
// calculate the normal range and relation for the operation. |
| 1798 |
if (add_p) |
| 1799 |
{ |
| 1800 |
// [ 0 , INF - OFF] |
| 1801 |
lb = wi::zero (prec); |
| 1802 |
ub = wi::sub (irange_val_max (type), off, UNSIGNED, &ov); |
| 1803 |
kind = VREL_GT; |
| 1804 |
} |
| 1805 |
else |
| 1806 |
{ |
| 1807 |
// [ OFF, INF ] |
| 1808 |
lb = off; |
| 1809 |
ub = irange_val_max (type); |
| 1810 |
kind = VREL_LT; |
| 1811 |
} |
| 1812 |
int_range<2> normal_range (type, lb, ub); |
| 1813 |
int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE); |
| 1814 |
|
| 1815 |
r_ov = ov_range; |
| 1816 |
r_normal = normal_range; |
| 1817 |
return kind; |
| 1818 |
} |
| 1819 |
|
| 1820 |
// Once op1 has been calculated by operator_plus or operator_minus, check |
| 1821 |
// to see if the relation passed causes any part of the calculation to |
| 1822 |
// be not possible. ie |
| 1823 |
// a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF] |
| 1824 |
// and that further refines a_2 to [0, 0]. |
| 1825 |
// R is the value of op1, OP2 is the offset being added/subtracted, REL is the |
| 1826 |
// relation between LHS relation OP1 and ADD_P is true for PLUS, false for |
| 1827 |
// MINUS. IF any adjustment can be made, R will reflect it. |
| 1828 |
|
| 1829 |
static void |
| 1830 |
adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel, |
| 1831 |
bool add_p) |
| 1832 |
{ |
| 1833 |
if (r.undefined_p ()) |
| 1834 |
return; |
| 1835 |
tree type = r.type (); |
| 1836 |
// Check for unsigned overflow and calculate the overflow part. |
| 1837 |
signop s = TYPE_SIGN (type); |
| 1838 |
if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED) |
| 1839 |
return; |
| 1840 |
|
| 1841 |
// Only work with <, <=, >, >= relations. |
| 1842 |
if (!relation_lt_le_gt_ge_p (rel)) |
| 1843 |
return; |
| 1844 |
|
| 1845 |
// Get the ranges for this offset. |
| 1846 |
int_range_max normal, overflow; |
| 1847 |
relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p); |
| 1848 |
|
| 1849 |
// VREL_VARYING means there are no adjustments. |
| 1850 |
if (k == VREL_VARYING) |
| 1851 |
return; |
| 1852 |
|
| 1853 |
// If the relations match use the normal range, otherwise use overflow range. |
| 1854 |
if (relation_intersect (k, rel) == k) |
| 1855 |
r.intersect (normal); |
| 1856 |
else |
| 1857 |
r.intersect (overflow); |
| 1858 |
return; |
| 1859 |
} |
| 1860 |
|
| 1861 |
bool |
| 1862 |
operator_plus::op1_range (irange &r, tree type, |
| 1863 |
const irange &lhs, |
| 1864 |
const irange &op2, |
| 1865 |
relation_trio trio) const |
| 1866 |
{ |
| 1867 |
if (lhs.undefined_p ()) |
| 1868 |
return false; |
| 1869 |
// Start with the default operation. |
| 1870 |
range_op_handler minus (MINUS_EXPR); |
| 1871 |
if (!minus) |
| 1872 |
return false; |
| 1873 |
bool res = minus.fold_range (r, type, lhs, op2); |
| 1874 |
relation_kind rel = trio.lhs_op1 (); |
| 1875 |
// Check for a relation refinement. |
| 1876 |
if (res) |
| 1877 |
adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */); |
| 1878 |
return res; |
| 1879 |
} |
| 1880 |
|
| 1881 |
bool |
| 1882 |
operator_plus::op2_range (irange &r, tree type, |
| 1883 |
const irange &lhs, |
| 1884 |
const irange &op1, |
| 1885 |
relation_trio rel) const |
| 1886 |
{ |
| 1887 |
return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ()); |
| 1888 |
} |
| 1889 |
|
| 1890 |
class operator_widen_plus_signed : public range_operator |
| 1891 |
{ |
| 1892 |
public: |
| 1893 |
virtual void wi_fold (irange &r, tree type, |
| 1894 |
const wide_int &lh_lb, |
| 1895 |
const wide_int &lh_ub, |
| 1896 |
const wide_int &rh_lb, |
| 1897 |
const wide_int &rh_ub) const; |
| 1898 |
} op_widen_plus_signed; |
| 1899 |
|
| 1900 |
void |
| 1901 |
operator_widen_plus_signed::wi_fold (irange &r, tree type, |
| 1902 |
const wide_int &lh_lb, |
| 1903 |
const wide_int &lh_ub, |
| 1904 |
const wide_int &rh_lb, |
| 1905 |
const wide_int &rh_ub) const |
| 1906 |
{ |
| 1907 |
wi::overflow_type ov_lb, ov_ub; |
| 1908 |
signop s = TYPE_SIGN (type); |
| 1909 |
|
| 1910 |
wide_int lh_wlb |
| 1911 |
= wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED); |
| 1912 |
wide_int lh_wub |
| 1913 |
= wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED); |
| 1914 |
wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s); |
| 1915 |
wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s); |
| 1916 |
|
| 1917 |
wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb); |
| 1918 |
wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub); |
| 1919 |
|
| 1920 |
r = int_range<2> (type, new_lb, new_ub); |
| 1921 |
} |
| 1922 |
|
| 1923 |
class operator_widen_plus_unsigned : public range_operator |
| 1924 |
{ |
| 1925 |
public: |
| 1926 |
virtual void wi_fold (irange &r, tree type, |
| 1927 |
const wide_int &lh_lb, |
| 1928 |
const wide_int &lh_ub, |
| 1929 |
const wide_int &rh_lb, |
| 1930 |
const wide_int &rh_ub) const; |
| 1931 |
} op_widen_plus_unsigned; |
| 1932 |
|
| 1933 |
void |
| 1934 |
operator_widen_plus_unsigned::wi_fold (irange &r, tree type, |
| 1935 |
const wide_int &lh_lb, |
| 1936 |
const wide_int &lh_ub, |
| 1937 |
const wide_int &rh_lb, |
| 1938 |
const wide_int &rh_ub) const |
| 1939 |
{ |
| 1940 |
wi::overflow_type ov_lb, ov_ub; |
| 1941 |
signop s = TYPE_SIGN (type); |
| 1942 |
|
| 1943 |
wide_int lh_wlb |
| 1944 |
= wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED); |
| 1945 |
wide_int lh_wub |
| 1946 |
= wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED); |
| 1947 |
wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s); |
| 1948 |
wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s); |
| 1949 |
|
| 1950 |
wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb); |
| 1951 |
wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub); |
| 1952 |
|
| 1953 |
r = int_range<2> (type, new_lb, new_ub); |
| 1954 |
} |
| 1955 |
|
| 1956 |
void |
| 1957 |
operator_minus::update_bitmask (irange &r, const irange &lh, |
| 1958 |
const irange &rh) const |
| 1959 |
{ |
| 1960 |
update_known_bitmask (r, MINUS_EXPR, lh, rh); |
| 1961 |
} |
| 1962 |
|
| 1963 |
void |
| 1964 |
operator_minus::wi_fold (irange &r, tree type, |
| 1965 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 1966 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 1967 |
{ |
| 1968 |
wi::overflow_type ov_lb, ov_ub; |
| 1969 |
signop s = TYPE_SIGN (type); |
| 1970 |
wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb); |
| 1971 |
wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub); |
| 1972 |
value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub); |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
// Return the relation between LHS and OP1 based on the relation between |
| 1977 |
// OP1 and OP2. |
| 1978 |
|
| 1979 |
relation_kind |
| 1980 |
operator_minus::lhs_op1_relation (const irange &, const irange &op1, |
| 1981 |
const irange &, relation_kind rel) const |
| 1982 |
{ |
| 1983 |
if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED) |
| 1984 |
switch (rel) |
| 1985 |
{ |
| 1986 |
case VREL_GT: |
| 1987 |
case VREL_GE: |
| 1988 |
return VREL_LE; |
| 1989 |
default: |
| 1990 |
break; |
| 1991 |
} |
| 1992 |
return VREL_VARYING; |
| 1993 |
} |
| 1994 |
|
| 1995 |
// Check to see if the relation REL between OP1 and OP2 has any effect on the |
| 1996 |
// LHS of the expression. If so, apply it to LHS_RANGE. This is a helper |
| 1997 |
// function for both MINUS_EXPR and POINTER_DIFF_EXPR. |
| 1998 |
|
| 1999 |
bool |
| 2000 |
minus_op1_op2_relation_effect (irange &lhs_range, tree type, |
| 2001 |
const irange &op1_range ATTRIBUTE_UNUSED, |
| 2002 |
const irange &op2_range ATTRIBUTE_UNUSED, |
| 2003 |
relation_kind rel) |
| 2004 |
{ |
| 2005 |
if (rel == VREL_VARYING) |
| 2006 |
return false; |
| 2007 |
|
| 2008 |
int_range<2> rel_range; |
| 2009 |
unsigned prec = TYPE_PRECISION (type); |
| 2010 |
signop sgn = TYPE_SIGN (type); |
| 2011 |
|
| 2012 |
// == and != produce [0,0] and ~[0,0] regardless of wrapping. |
| 2013 |
if (rel == VREL_EQ) |
| 2014 |
rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec)); |
| 2015 |
else if (rel == VREL_NE) |
| 2016 |
rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec), |
| 2017 |
VR_ANTI_RANGE); |
| 2018 |
else if (TYPE_OVERFLOW_WRAPS (type)) |
| 2019 |
{ |
| 2020 |
switch (rel) |
| 2021 |
{ |
| 2022 |
// For wrapping signed values and unsigned, if op1 > op2 or |
| 2023 |
// op1 < op2, then op1 - op2 can be restricted to ~[0, 0]. |
| 2024 |
case VREL_GT: |
| 2025 |
case VREL_LT: |
| 2026 |
rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec), |
| 2027 |
VR_ANTI_RANGE); |
| 2028 |
break; |
| 2029 |
default: |
| 2030 |
return false; |
| 2031 |
} |
| 2032 |
} |
| 2033 |
else |
| 2034 |
{ |
| 2035 |
switch (rel) |
| 2036 |
{ |
| 2037 |
// op1 > op2, op1 - op2 can be restricted to [1, +INF] |
| 2038 |
case VREL_GT: |
| 2039 |
rel_range = int_range<2> (type, wi::one (prec), |
| 2040 |
wi::max_value (prec, sgn)); |
| 2041 |
break; |
| 2042 |
// op1 >= op2, op1 - op2 can be restricted to [0, +INF] |
| 2043 |
case VREL_GE: |
| 2044 |
rel_range = int_range<2> (type, wi::zero (prec), |
| 2045 |
wi::max_value (prec, sgn)); |
| 2046 |
break; |
| 2047 |
// op1 < op2, op1 - op2 can be restricted to [-INF, -1] |
| 2048 |
case VREL_LT: |
| 2049 |
rel_range = int_range<2> (type, wi::min_value (prec, sgn), |
| 2050 |
wi::minus_one (prec)); |
| 2051 |
break; |
| 2052 |
// op1 <= op2, op1 - op2 can be restricted to [-INF, 0] |
| 2053 |
case VREL_LE: |
| 2054 |
rel_range = int_range<2> (type, wi::min_value (prec, sgn), |
| 2055 |
wi::zero (prec)); |
| 2056 |
break; |
| 2057 |
default: |
| 2058 |
return false; |
| 2059 |
} |
| 2060 |
} |
| 2061 |
lhs_range.intersect (rel_range); |
| 2062 |
return true; |
| 2063 |
} |
| 2064 |
|
| 2065 |
bool |
| 2066 |
operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type, |
| 2067 |
const irange &op1_range, |
| 2068 |
const irange &op2_range, |
| 2069 |
relation_kind rel) const |
| 2070 |
{ |
| 2071 |
return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range, |
| 2072 |
rel); |
| 2073 |
} |
| 2074 |
|
| 2075 |
bool |
| 2076 |
operator_minus::op1_range (irange &r, tree type, |
| 2077 |
const irange &lhs, |
| 2078 |
const irange &op2, |
| 2079 |
relation_trio trio) const |
| 2080 |
{ |
| 2081 |
if (lhs.undefined_p ()) |
| 2082 |
return false; |
| 2083 |
// Start with the default operation. |
| 2084 |
range_op_handler minus (PLUS_EXPR); |
| 2085 |
if (!minus) |
| 2086 |
return false; |
| 2087 |
bool res = minus.fold_range (r, type, lhs, op2); |
| 2088 |
relation_kind rel = trio.lhs_op1 (); |
| 2089 |
if (res) |
| 2090 |
adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */); |
| 2091 |
return res; |
| 2092 |
|
| 2093 |
} |
| 2094 |
|
| 2095 |
bool |
| 2096 |
operator_minus::op2_range (irange &r, tree type, |
| 2097 |
const irange &lhs, |
| 2098 |
const irange &op1, |
| 2099 |
relation_trio) const |
| 2100 |
{ |
| 2101 |
if (lhs.undefined_p ()) |
| 2102 |
return false; |
| 2103 |
return fold_range (r, type, op1, lhs); |
| 2104 |
} |
| 2105 |
|
| 2106 |
void |
| 2107 |
operator_min::update_bitmask (irange &r, const irange &lh, |
| 2108 |
const irange &rh) const |
| 2109 |
{ |
| 2110 |
update_known_bitmask (r, MIN_EXPR, lh, rh); |
| 2111 |
} |
| 2112 |
|
| 2113 |
void |
| 2114 |
operator_min::wi_fold (irange &r, tree type, |
| 2115 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2116 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 2117 |
{ |
| 2118 |
signop s = TYPE_SIGN (type); |
| 2119 |
wide_int new_lb = wi::min (lh_lb, rh_lb, s); |
| 2120 |
wide_int new_ub = wi::min (lh_ub, rh_ub, s); |
| 2121 |
value_range_with_overflow (r, type, new_lb, new_ub); |
| 2122 |
} |
| 2123 |
|
| 2124 |
|
| 2125 |
void |
| 2126 |
operator_max::update_bitmask (irange &r, const irange &lh, |
| 2127 |
const irange &rh) const |
| 2128 |
{ |
| 2129 |
update_known_bitmask (r, MAX_EXPR, lh, rh); |
| 2130 |
} |
| 2131 |
|
| 2132 |
void |
| 2133 |
operator_max::wi_fold (irange &r, tree type, |
| 2134 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2135 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 2136 |
{ |
| 2137 |
signop s = TYPE_SIGN (type); |
| 2138 |
wide_int new_lb = wi::max (lh_lb, rh_lb, s); |
| 2139 |
wide_int new_ub = wi::max (lh_ub, rh_ub, s); |
| 2140 |
value_range_with_overflow (r, type, new_lb, new_ub); |
| 2141 |
} |
| 2142 |
|
| 2143 |
|
| 2144 |
// Calculate the cross product of two sets of ranges and return it. |
| 2145 |
// |
| 2146 |
// Multiplications, divisions and shifts are a bit tricky to handle, |
| 2147 |
// depending on the mix of signs we have in the two ranges, we need to |
| 2148 |
// operate on different values to get the minimum and maximum values |
| 2149 |
// for the new range. One approach is to figure out all the |
| 2150 |
// variations of range combinations and do the operations. |
| 2151 |
// |
| 2152 |
// However, this involves several calls to compare_values and it is |
| 2153 |
// pretty convoluted. It's simpler to do the 4 operations (MIN0 OP |
| 2154 |
// MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then |
| 2155 |
// figure the smallest and largest values to form the new range. |
| 2156 |
|
| 2157 |
void |
| 2158 |
cross_product_operator::wi_cross_product (irange &r, tree type, |
| 2159 |
const wide_int &lh_lb, |
| 2160 |
const wide_int &lh_ub, |
| 2161 |
const wide_int &rh_lb, |
| 2162 |
const wide_int &rh_ub) const |
| 2163 |
{ |
| 2164 |
wide_int cp1, cp2, cp3, cp4; |
| 2165 |
// Default to varying. |
| 2166 |
r.set_varying (type); |
| 2167 |
|
| 2168 |
// Compute the 4 cross operations, bailing if we get an overflow we |
| 2169 |
// can't handle. |
| 2170 |
if (wi_op_overflows (cp1, type, lh_lb, rh_lb)) |
| 2171 |
return; |
| 2172 |
if (wi::eq_p (lh_lb, lh_ub)) |
| 2173 |
cp3 = cp1; |
| 2174 |
else if (wi_op_overflows (cp3, type, lh_ub, rh_lb)) |
| 2175 |
return; |
| 2176 |
if (wi::eq_p (rh_lb, rh_ub)) |
| 2177 |
cp2 = cp1; |
| 2178 |
else if (wi_op_overflows (cp2, type, lh_lb, rh_ub)) |
| 2179 |
return; |
| 2180 |
if (wi::eq_p (lh_lb, lh_ub)) |
| 2181 |
cp4 = cp2; |
| 2182 |
else if (wi_op_overflows (cp4, type, lh_ub, rh_ub)) |
| 2183 |
return; |
| 2184 |
|
| 2185 |
// Order pairs. |
| 2186 |
signop sign = TYPE_SIGN (type); |
| 2187 |
if (wi::gt_p (cp1, cp2, sign)) |
| 2188 |
std::swap (cp1, cp2); |
| 2189 |
if (wi::gt_p (cp3, cp4, sign)) |
| 2190 |
std::swap (cp3, cp4); |
| 2191 |
|
| 2192 |
// Choose min and max from the ordered pairs. |
| 2193 |
wide_int res_lb = wi::min (cp1, cp3, sign); |
| 2194 |
wide_int res_ub = wi::max (cp2, cp4, sign); |
| 2195 |
value_range_with_overflow (r, type, res_lb, res_ub); |
| 2196 |
} |
| 2197 |
|
| 2198 |
|
| 2199 |
void |
| 2200 |
operator_mult::update_bitmask (irange &r, const irange &lh, |
| 2201 |
const irange &rh) const |
| 2202 |
{ |
| 2203 |
update_known_bitmask (r, MULT_EXPR, lh, rh); |
| 2204 |
} |
| 2205 |
|
| 2206 |
bool |
| 2207 |
operator_mult::op1_range (irange &r, tree type, |
| 2208 |
const irange &lhs, const irange &op2, |
| 2209 |
relation_trio) const |
| 2210 |
{ |
| 2211 |
if (lhs.undefined_p ()) |
| 2212 |
return false; |
| 2213 |
|
| 2214 |
// We can't solve 0 = OP1 * N by dividing by N with a wrapping type. |
| 2215 |
// For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas |
| 2216 |
// for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit) |
| 2217 |
if (TYPE_OVERFLOW_WRAPS (type)) |
| 2218 |
return false; |
| 2219 |
|
| 2220 |
wide_int offset; |
| 2221 |
if (op2.singleton_p (offset) && offset != 0) |
| 2222 |
return range_op_handler (TRUNC_DIV_EXPR).fold_range (r, type, lhs, op2); |
| 2223 |
|
| 2224 |
// ~[0, 0] = op1 * op2 defines op1 and op2 as non-zero. |
| 2225 |
if (!lhs.contains_p (wi::zero (TYPE_PRECISION (lhs.type ())))) |
| 2226 |
{ |
| 2227 |
r.set_nonzero (type); |
| 2228 |
return true; |
| 2229 |
} |
| 2230 |
return false; |
| 2231 |
} |
| 2232 |
|
| 2233 |
bool |
| 2234 |
operator_mult::op2_range (irange &r, tree type, |
| 2235 |
const irange &lhs, const irange &op1, |
| 2236 |
relation_trio rel) const |
| 2237 |
{ |
| 2238 |
return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ()); |
| 2239 |
} |
| 2240 |
|
| 2241 |
bool |
| 2242 |
operator_mult::wi_op_overflows (wide_int &res, tree type, |
| 2243 |
const wide_int &w0, const wide_int &w1) const |
| 2244 |
{ |
| 2245 |
wi::overflow_type overflow = wi::OVF_NONE; |
| 2246 |
signop sign = TYPE_SIGN (type); |
| 2247 |
res = wi::mul (w0, w1, sign, &overflow); |
| 2248 |
if (overflow && TYPE_OVERFLOW_UNDEFINED (type)) |
| 2249 |
{ |
| 2250 |
// For multiplication, the sign of the overflow is given |
| 2251 |
// by the comparison of the signs of the operands. |
| 2252 |
if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ()) |
| 2253 |
res = wi::max_value (w0.get_precision (), sign); |
| 2254 |
else |
| 2255 |
res = wi::min_value (w0.get_precision (), sign); |
| 2256 |
return false; |
| 2257 |
} |
| 2258 |
return overflow; |
| 2259 |
} |
| 2260 |
|
| 2261 |
void |
| 2262 |
operator_mult::wi_fold (irange &r, tree type, |
| 2263 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2264 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 2265 |
{ |
| 2266 |
if (TYPE_OVERFLOW_UNDEFINED (type)) |
| 2267 |
{ |
| 2268 |
wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub); |
| 2269 |
return; |
| 2270 |
} |
| 2271 |
|
| 2272 |
// Multiply the ranges when overflow wraps. This is basically fancy |
| 2273 |
// code so we don't drop to varying with an unsigned |
| 2274 |
// [-3,-1]*[-3,-1]. |
| 2275 |
// |
| 2276 |
// This test requires 2*prec bits if both operands are signed and |
| 2277 |
// 2*prec + 2 bits if either is not. Therefore, extend the values |
| 2278 |
// using the sign of the result to PREC2. From here on out, |
| 2279 |
// everything is just signed math no matter what the input types |
| 2280 |
// were. |
| 2281 |
|
| 2282 |
signop sign = TYPE_SIGN (type); |
| 2283 |
unsigned prec = TYPE_PRECISION (type); |
| 2284 |
widest2_int min0 = widest2_int::from (lh_lb, sign); |
| 2285 |
widest2_int max0 = widest2_int::from (lh_ub, sign); |
| 2286 |
widest2_int min1 = widest2_int::from (rh_lb, sign); |
| 2287 |
widest2_int max1 = widest2_int::from (rh_ub, sign); |
| 2288 |
widest2_int sizem1 = wi::mask <widest2_int> (prec, false); |
| 2289 |
widest2_int size = sizem1 + 1; |
| 2290 |
|
| 2291 |
// Canonicalize the intervals. |
| 2292 |
if (sign == UNSIGNED) |
| 2293 |
{ |
| 2294 |
if (wi::ltu_p (size, min0 + max0)) |
| 2295 |
{ |
| 2296 |
min0 -= size; |
| 2297 |
max0 -= size; |
| 2298 |
} |
| 2299 |
if (wi::ltu_p (size, min1 + max1)) |
| 2300 |
{ |
| 2301 |
min1 -= size; |
| 2302 |
max1 -= size; |
| 2303 |
} |
| 2304 |
} |
| 2305 |
|
| 2306 |
// Sort the 4 products so that min is in prod0 and max is in |
| 2307 |
// prod3. |
| 2308 |
widest2_int prod0 = min0 * min1; |
| 2309 |
widest2_int prod1 = min0 * max1; |
| 2310 |
widest2_int prod2 = max0 * min1; |
| 2311 |
widest2_int prod3 = max0 * max1; |
| 2312 |
|
| 2313 |
// min0min1 > max0max1 |
| 2314 |
if (prod0 > prod3) |
| 2315 |
std::swap (prod0, prod3); |
| 2316 |
|
| 2317 |
// min0max1 > max0min1 |
| 2318 |
if (prod1 > prod2) |
| 2319 |
std::swap (prod1, prod2); |
| 2320 |
|
| 2321 |
if (prod0 > prod1) |
| 2322 |
std::swap (prod0, prod1); |
| 2323 |
|
| 2324 |
if (prod2 > prod3) |
| 2325 |
std::swap (prod2, prod3); |
| 2326 |
|
| 2327 |
// diff = max - min |
| 2328 |
prod2 = prod3 - prod0; |
| 2329 |
if (wi::geu_p (prod2, sizem1)) |
| 2330 |
{ |
| 2331 |
// Multiplying by X, where X is a power of 2 is [0,0][X,+INF]. |
| 2332 |
if (TYPE_UNSIGNED (type) && rh_lb == rh_ub |
| 2333 |
&& wi::exact_log2 (rh_lb) != -1 && prec > 1) |
| 2334 |
{ |
| 2335 |
r.set (type, rh_lb, wi::max_value (prec, sign)); |
| 2336 |
int_range<2> zero; |
| 2337 |
zero.set_zero (type); |
| 2338 |
r.union_ (zero); |
| 2339 |
} |
| 2340 |
else |
| 2341 |
// The range covers all values. |
| 2342 |
r.set_varying (type); |
| 2343 |
} |
| 2344 |
else |
| 2345 |
{ |
| 2346 |
wide_int new_lb = wide_int::from (prod0, prec, sign); |
| 2347 |
wide_int new_ub = wide_int::from (prod3, prec, sign); |
| 2348 |
create_possibly_reversed_range (r, type, new_lb, new_ub); |
| 2349 |
} |
| 2350 |
} |
| 2351 |
|
| 2352 |
class operator_widen_mult_signed : public range_operator |
| 2353 |
{ |
| 2354 |
public: |
| 2355 |
virtual void wi_fold (irange &r, tree type, |
| 2356 |
const wide_int &lh_lb, |
| 2357 |
const wide_int &lh_ub, |
| 2358 |
const wide_int &rh_lb, |
| 2359 |
const wide_int &rh_ub) |
| 2360 |
const; |
| 2361 |
} op_widen_mult_signed; |
| 2362 |
|
| 2363 |
void |
| 2364 |
operator_widen_mult_signed::wi_fold (irange &r, tree type, |
| 2365 |
const wide_int &lh_lb, |
| 2366 |
const wide_int &lh_ub, |
| 2367 |
const wide_int &rh_lb, |
| 2368 |
const wide_int &rh_ub) const |
| 2369 |
{ |
| 2370 |
wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED); |
| 2371 |
wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED); |
| 2372 |
wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), SIGNED); |
| 2373 |
wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), SIGNED); |
| 2374 |
|
| 2375 |
/* We don't expect a widening multiplication to be able to overflow but range |
| 2376 |
calculations for multiplications are complicated. After widening the |
| 2377 |
operands lets call the base class. */ |
| 2378 |
return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub); |
| 2379 |
} |
| 2380 |
|
| 2381 |
class operator_widen_mult_unsigned : public range_operator |
| 2382 |
{ |
| 2383 |
public: |
| 2384 |
virtual void wi_fold (irange &r, tree type, |
| 2385 |
const wide_int &lh_lb, |
| 2386 |
const wide_int &lh_ub, |
| 2387 |
const wide_int &rh_lb, |
| 2388 |
const wide_int &rh_ub) |
| 2389 |
const; |
| 2390 |
} op_widen_mult_unsigned; |
| 2391 |
|
| 2392 |
void |
| 2393 |
operator_widen_mult_unsigned::wi_fold (irange &r, tree type, |
| 2394 |
const wide_int &lh_lb, |
| 2395 |
const wide_int &lh_ub, |
| 2396 |
const wide_int &rh_lb, |
| 2397 |
const wide_int &rh_ub) const |
| 2398 |
{ |
| 2399 |
wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), UNSIGNED); |
| 2400 |
wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), UNSIGNED); |
| 2401 |
wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED); |
| 2402 |
wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED); |
| 2403 |
|
| 2404 |
/* We don't expect a widening multiplication to be able to overflow but range |
| 2405 |
calculations for multiplications are complicated. After widening the |
| 2406 |
operands lets call the base class. */ |
| 2407 |
return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub); |
| 2408 |
} |
| 2409 |
|
| 2410 |
class operator_widen_mult_signed_unsigned : public range_operator |
| 2411 |
{ |
| 2412 |
public: |
| 2413 |
virtual void wi_fold (irange &r, tree type, |
| 2414 |
const wide_int &lh_lb, |
| 2415 |
const wide_int &lh_ub, |
| 2416 |
const wide_int &rh_lb, |
| 2417 |
const wide_int &rh_ub) |
| 2418 |
const; |
| 2419 |
} op_widen_mult_signed_unsigned; |
| 2420 |
|
| 2421 |
void |
| 2422 |
operator_widen_mult_signed_unsigned::wi_fold (irange &r, tree type, |
| 2423 |
const wide_int &lh_lb, |
| 2424 |
const wide_int &lh_ub, |
| 2425 |
const wide_int &rh_lb, |
| 2426 |
const wide_int &rh_ub) const |
| 2427 |
{ |
| 2428 |
wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED); |
| 2429 |
wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED); |
| 2430 |
wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED); |
| 2431 |
wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED); |
| 2432 |
|
| 2433 |
/* We don't expect a widening multiplication to be able to overflow but range |
| 2434 |
calculations for multiplications are complicated. After widening the |
| 2435 |
operands lets call the base class. */ |
| 2436 |
return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub); |
| 2437 |
} |
| 2438 |
|
| 2439 |
class operator_div : public cross_product_operator |
| 2440 |
{ |
| 2441 |
using range_operator::update_bitmask; |
| 2442 |
public: |
| 2443 |
operator_div (tree_code div_kind) { m_code = div_kind; } |
| 2444 |
virtual void wi_fold (irange &r, tree type, |
| 2445 |
const wide_int &lh_lb, |
| 2446 |
const wide_int &lh_ub, |
| 2447 |
const wide_int &rh_lb, |
| 2448 |
const wide_int &rh_ub) const final override; |
| 2449 |
virtual bool wi_op_overflows (wide_int &res, tree type, |
| 2450 |
const wide_int &, const wide_int &) |
| 2451 |
const final override; |
| 2452 |
void update_bitmask (irange &r, const irange &lh, const irange &rh) const |
| 2453 |
{ update_known_bitmask (r, m_code, lh, rh); } |
| 2454 |
protected: |
| 2455 |
tree_code m_code; |
| 2456 |
}; |
| 2457 |
|
| 2458 |
static operator_div op_trunc_div (TRUNC_DIV_EXPR); |
| 2459 |
static operator_div op_floor_div (FLOOR_DIV_EXPR); |
| 2460 |
static operator_div op_round_div (ROUND_DIV_EXPR); |
| 2461 |
static operator_div op_ceil_div (CEIL_DIV_EXPR); |
| 2462 |
|
| 2463 |
bool |
| 2464 |
operator_div::wi_op_overflows (wide_int &res, tree type, |
| 2465 |
const wide_int &w0, const wide_int &w1) const |
| 2466 |
{ |
| 2467 |
if (w1 == 0) |
| 2468 |
return true; |
| 2469 |
|
| 2470 |
wi::overflow_type overflow = wi::OVF_NONE; |
| 2471 |
signop sign = TYPE_SIGN (type); |
| 2472 |
|
| 2473 |
switch (m_code) |
| 2474 |
{ |
| 2475 |
case EXACT_DIV_EXPR: |
| 2476 |
case TRUNC_DIV_EXPR: |
| 2477 |
res = wi::div_trunc (w0, w1, sign, &overflow); |
| 2478 |
break; |
| 2479 |
case FLOOR_DIV_EXPR: |
| 2480 |
res = wi::div_floor (w0, w1, sign, &overflow); |
| 2481 |
break; |
| 2482 |
case ROUND_DIV_EXPR: |
| 2483 |
res = wi::div_round (w0, w1, sign, &overflow); |
| 2484 |
break; |
| 2485 |
case CEIL_DIV_EXPR: |
| 2486 |
res = wi::div_ceil (w0, w1, sign, &overflow); |
| 2487 |
break; |
| 2488 |
default: |
| 2489 |
gcc_unreachable (); |
| 2490 |
} |
| 2491 |
|
| 2492 |
if (overflow && TYPE_OVERFLOW_UNDEFINED (type)) |
| 2493 |
{ |
| 2494 |
// For division, the only case is -INF / -1 = +INF. |
| 2495 |
res = wi::max_value (w0.get_precision (), sign); |
| 2496 |
return false; |
| 2497 |
} |
| 2498 |
return overflow; |
| 2499 |
} |
| 2500 |
|
| 2501 |
void |
| 2502 |
operator_div::wi_fold (irange &r, tree type, |
| 2503 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2504 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 2505 |
{ |
| 2506 |
const wide_int dividend_min = lh_lb; |
| 2507 |
const wide_int dividend_max = lh_ub; |
| 2508 |
const wide_int divisor_min = rh_lb; |
| 2509 |
const wide_int divisor_max = rh_ub; |
| 2510 |
signop sign = TYPE_SIGN (type); |
| 2511 |
unsigned prec = TYPE_PRECISION (type); |
| 2512 |
wide_int extra_min, extra_max; |
| 2513 |
|
| 2514 |
// If we know we won't divide by zero, just do the division. |
| 2515 |
if (!wi_includes_zero_p (type, divisor_min, divisor_max)) |
| 2516 |
{ |
| 2517 |
wi_cross_product (r, type, dividend_min, dividend_max, |
| 2518 |
divisor_min, divisor_max); |
| 2519 |
return; |
| 2520 |
} |
| 2521 |
|
| 2522 |
// If we're definitely dividing by zero, there's nothing to do. |
| 2523 |
if (wi_zero_p (type, divisor_min, divisor_max)) |
| 2524 |
{ |
| 2525 |
r.set_undefined (); |
| 2526 |
return; |
| 2527 |
} |
| 2528 |
|
| 2529 |
// Perform the division in 2 parts, [LB, -1] and [1, UB], which will |
| 2530 |
// skip any division by zero. |
| 2531 |
|
| 2532 |
// First divide by the negative numbers, if any. |
| 2533 |
if (wi::neg_p (divisor_min, sign)) |
| 2534 |
wi_cross_product (r, type, dividend_min, dividend_max, |
| 2535 |
divisor_min, wi::minus_one (prec)); |
| 2536 |
else |
| 2537 |
r.set_undefined (); |
| 2538 |
|
| 2539 |
// Then divide by the non-zero positive numbers, if any. |
| 2540 |
if (wi::gt_p (divisor_max, wi::zero (prec), sign)) |
| 2541 |
{ |
| 2542 |
int_range_max tmp; |
| 2543 |
wi_cross_product (tmp, type, dividend_min, dividend_max, |
| 2544 |
wi::one (prec), divisor_max); |
| 2545 |
r.union_ (tmp); |
| 2546 |
} |
| 2547 |
// We shouldn't still have undefined here. |
| 2548 |
gcc_checking_assert (!r.undefined_p ()); |
| 2549 |
} |
| 2550 |
|
| 2551 |
|
| 2552 |
class operator_exact_divide : public operator_div |
| 2553 |
{ |
| 2554 |
using range_operator::op1_range; |
| 2555 |
public: |
| 2556 |
operator_exact_divide () : operator_div (EXACT_DIV_EXPR) { } |
| 2557 |
virtual bool op1_range (irange &r, tree type, |
| 2558 |
const irange &lhs, |
| 2559 |
const irange &op2, |
| 2560 |
relation_trio) const; |
| 2561 |
|
| 2562 |
} op_exact_div; |
| 2563 |
|
| 2564 |
bool |
| 2565 |
operator_exact_divide::op1_range (irange &r, tree type, |
| 2566 |
const irange &lhs, |
| 2567 |
const irange &op2, |
| 2568 |
relation_trio) const |
| 2569 |
{ |
| 2570 |
if (lhs.undefined_p ()) |
| 2571 |
return false; |
| 2572 |
wide_int offset; |
| 2573 |
// [2, 4] = op1 / [3,3] since its exact divide, no need to worry about |
| 2574 |
// remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12]. |
| 2575 |
// We wont bother trying to enumerate all the in between stuff :-P |
| 2576 |
// TRUE accuracy is [6,6][9,9][12,12]. This is unlikely to matter most of |
| 2577 |
// the time however. |
| 2578 |
// If op2 is a multiple of 2, we would be able to set some non-zero bits. |
| 2579 |
if (op2.singleton_p (offset) && offset != 0) |
| 2580 |
return range_op_handler (MULT_EXPR).fold_range (r, type, lhs, op2); |
| 2581 |
return false; |
| 2582 |
} |
| 2583 |
|
| 2584 |
|
| 2585 |
class operator_lshift : public cross_product_operator |
| 2586 |
{ |
| 2587 |
using range_operator::fold_range; |
| 2588 |
using range_operator::op1_range; |
| 2589 |
using range_operator::update_bitmask; |
| 2590 |
public: |
| 2591 |
virtual bool op1_range (irange &r, tree type, const irange &lhs, |
| 2592 |
const irange &op2, relation_trio rel = TRIO_VARYING) |
| 2593 |
const final override; |
| 2594 |
virtual bool fold_range (irange &r, tree type, const irange &op1, |
| 2595 |
const irange &op2, relation_trio rel = TRIO_VARYING) |
| 2596 |
const final override; |
| 2597 |
|
| 2598 |
virtual void wi_fold (irange &r, tree type, |
| 2599 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2600 |
const wide_int &rh_lb, |
| 2601 |
const wide_int &rh_ub) const final override; |
| 2602 |
virtual bool wi_op_overflows (wide_int &res, |
| 2603 |
tree type, |
| 2604 |
const wide_int &, |
| 2605 |
const wide_int &) const final override; |
| 2606 |
void update_bitmask (irange &r, const irange &lh, |
| 2607 |
const irange &rh) const final override |
| 2608 |
{ update_known_bitmask (r, LSHIFT_EXPR, lh, rh); } |
| 2609 |
// Check compatibility of LHS and op1. |
| 2610 |
bool operand_check_p (tree t1, tree t2, tree) const final override |
| 2611 |
{ return range_compatible_p (t1, t2); } |
| 2612 |
} op_lshift; |
| 2613 |
|
| 2614 |
class operator_rshift : public cross_product_operator |
| 2615 |
{ |
| 2616 |
using range_operator::fold_range; |
| 2617 |
using range_operator::op1_range; |
| 2618 |
using range_operator::lhs_op1_relation; |
| 2619 |
using range_operator::update_bitmask; |
| 2620 |
public: |
| 2621 |
virtual bool fold_range (irange &r, tree type, const irange &op1, |
| 2622 |
const irange &op2, relation_trio rel = TRIO_VARYING) |
| 2623 |
const final override; |
| 2624 |
virtual void wi_fold (irange &r, tree type, |
| 2625 |
const wide_int &lh_lb, |
| 2626 |
const wide_int &lh_ub, |
| 2627 |
const wide_int &rh_lb, |
| 2628 |
const wide_int &rh_ub) const final override; |
| 2629 |
virtual bool wi_op_overflows (wide_int &res, |
| 2630 |
tree type, |
| 2631 |
const wide_int &w0, |
| 2632 |
const wide_int &w1) const final override; |
| 2633 |
virtual bool op1_range (irange &, tree type, const irange &lhs, |
| 2634 |
const irange &op2, relation_trio rel = TRIO_VARYING) |
| 2635 |
const final override; |
| 2636 |
virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1, |
| 2637 |
const irange &op2, relation_kind rel) |
| 2638 |
const final override; |
| 2639 |
void update_bitmask (irange &r, const irange &lh, |
| 2640 |
const irange &rh) const final override |
| 2641 |
{ update_known_bitmask (r, RSHIFT_EXPR, lh, rh); } |
| 2642 |
// Check compatibility of LHS and op1. |
| 2643 |
bool operand_check_p (tree t1, tree t2, tree) const final override |
| 2644 |
{ return range_compatible_p (t1, t2); } |
| 2645 |
} op_rshift; |
| 2646 |
|
| 2647 |
|
| 2648 |
relation_kind |
| 2649 |
operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED, |
| 2650 |
const irange &op1, |
| 2651 |
const irange &op2, |
| 2652 |
relation_kind) const |
| 2653 |
{ |
| 2654 |
// If both operands range are >= 0, then the LHS <= op1. |
| 2655 |
if (!op1.undefined_p () && !op2.undefined_p () |
| 2656 |
&& wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ())) |
| 2657 |
&& wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ()))) |
| 2658 |
return VREL_LE; |
| 2659 |
return VREL_VARYING; |
| 2660 |
} |
| 2661 |
|
| 2662 |
bool |
| 2663 |
operator_lshift::fold_range (irange &r, tree type, |
| 2664 |
const irange &op1, |
| 2665 |
const irange &op2, |
| 2666 |
relation_trio rel) const |
| 2667 |
{ |
| 2668 |
int_range_max shift_range; |
| 2669 |
if (!get_shift_range (shift_range, type, op2)) |
| 2670 |
{ |
| 2671 |
if (op2.undefined_p ()) |
| 2672 |
r.set_undefined (); |
| 2673 |
else |
| 2674 |
r.set_zero (type); |
| 2675 |
return true; |
| 2676 |
} |
| 2677 |
|
| 2678 |
// Transform left shifts by constants into multiplies. |
| 2679 |
if (shift_range.singleton_p ()) |
| 2680 |
{ |
| 2681 |
unsigned shift = shift_range.lower_bound ().to_uhwi (); |
| 2682 |
wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type)); |
| 2683 |
int_range<1> mult (type, tmp, tmp); |
| 2684 |
|
| 2685 |
// Force wrapping multiplication. |
| 2686 |
bool saved_flag_wrapv = flag_wrapv; |
| 2687 |
bool saved_flag_wrapv_pointer = flag_wrapv_pointer; |
| 2688 |
flag_wrapv = 1; |
| 2689 |
flag_wrapv_pointer = 1; |
| 2690 |
bool b = op_mult.fold_range (r, type, op1, mult); |
| 2691 |
flag_wrapv = saved_flag_wrapv; |
| 2692 |
flag_wrapv_pointer = saved_flag_wrapv_pointer; |
| 2693 |
return b; |
| 2694 |
} |
| 2695 |
else |
| 2696 |
// Otherwise, invoke the generic fold routine. |
| 2697 |
return range_operator::fold_range (r, type, op1, shift_range, rel); |
| 2698 |
} |
| 2699 |
|
| 2700 |
void |
| 2701 |
operator_lshift::wi_fold (irange &r, tree type, |
| 2702 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2703 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 2704 |
{ |
| 2705 |
signop sign = TYPE_SIGN (type); |
| 2706 |
unsigned prec = TYPE_PRECISION (type); |
| 2707 |
int overflow_pos = sign == SIGNED ? prec - 1 : prec; |
| 2708 |
int bound_shift = overflow_pos - rh_ub.to_shwi (); |
| 2709 |
// If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can |
| 2710 |
// overflow. However, for that to happen, rh.max needs to be zero, |
| 2711 |
// which means rh is a singleton range of zero, which means we simply return |
| 2712 |
// [lh_lb, lh_ub] as the range. |
| 2713 |
if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0)) |
| 2714 |
{ |
| 2715 |
r = int_range<2> (type, lh_lb, lh_ub); |
| 2716 |
return; |
| 2717 |
} |
| 2718 |
|
| 2719 |
wide_int bound = wi::set_bit_in_zero (bound_shift, prec); |
| 2720 |
wide_int complement = ~(bound - 1); |
| 2721 |
wide_int low_bound, high_bound; |
| 2722 |
bool in_bounds = false; |
| 2723 |
|
| 2724 |
if (sign == UNSIGNED) |
| 2725 |
{ |
| 2726 |
low_bound = bound; |
| 2727 |
high_bound = complement; |
| 2728 |
if (wi::ltu_p (lh_ub, low_bound)) |
| 2729 |
{ |
| 2730 |
// [5, 6] << [1, 2] == [10, 24]. |
| 2731 |
// We're shifting out only zeroes, the value increases |
| 2732 |
// monotonically. |
| 2733 |
in_bounds = true; |
| 2734 |
} |
| 2735 |
else if (wi::ltu_p (high_bound, lh_lb)) |
| 2736 |
{ |
| 2737 |
// [0xffffff00, 0xffffffff] << [1, 2] |
| 2738 |
// == [0xfffffc00, 0xfffffffe]. |
| 2739 |
// We're shifting out only ones, the value decreases |
| 2740 |
// monotonically. |
| 2741 |
in_bounds = true; |
| 2742 |
} |
| 2743 |
} |
| 2744 |
else |
| 2745 |
{ |
| 2746 |
// [-1, 1] << [1, 2] == [-4, 4] |
| 2747 |
low_bound = complement; |
| 2748 |
high_bound = bound; |
| 2749 |
if (wi::lts_p (lh_ub, high_bound) |
| 2750 |
&& wi::lts_p (low_bound, lh_lb)) |
| 2751 |
{ |
| 2752 |
// For non-negative numbers, we're shifting out only zeroes, |
| 2753 |
// the value increases monotonically. For negative numbers, |
| 2754 |
// we're shifting out only ones, the value decreases |
| 2755 |
// monotonically. |
| 2756 |
in_bounds = true; |
| 2757 |
} |
| 2758 |
} |
| 2759 |
|
| 2760 |
if (in_bounds) |
| 2761 |
wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub); |
| 2762 |
else |
| 2763 |
r.set_varying (type); |
| 2764 |
} |
| 2765 |
|
| 2766 |
bool |
| 2767 |
operator_lshift::wi_op_overflows (wide_int &res, tree type, |
| 2768 |
const wide_int &w0, const wide_int &w1) const |
| 2769 |
{ |
| 2770 |
signop sign = TYPE_SIGN (type); |
| 2771 |
if (wi::neg_p (w1)) |
| 2772 |
{ |
| 2773 |
// It's unclear from the C standard whether shifts can overflow. |
| 2774 |
// The following code ignores overflow; perhaps a C standard |
| 2775 |
// interpretation ruling is needed. |
| 2776 |
res = wi::rshift (w0, -w1, sign); |
| 2777 |
} |
| 2778 |
else |
| 2779 |
res = wi::lshift (w0, w1); |
| 2780 |
return false; |
| 2781 |
} |
| 2782 |
|
| 2783 |
bool |
| 2784 |
operator_lshift::op1_range (irange &r, |
| 2785 |
tree type, |
| 2786 |
const irange &lhs, |
| 2787 |
const irange &op2, |
| 2788 |
relation_trio) const |
| 2789 |
{ |
| 2790 |
if (lhs.undefined_p ()) |
| 2791 |
return false; |
| 2792 |
|
| 2793 |
if (!contains_zero_p (lhs)) |
| 2794 |
r.set_nonzero (type); |
| 2795 |
else |
| 2796 |
r.set_varying (type); |
| 2797 |
|
| 2798 |
wide_int shift; |
| 2799 |
if (op2.singleton_p (shift)) |
| 2800 |
{ |
| 2801 |
if (wi::lt_p (shift, 0, SIGNED)) |
| 2802 |
return false; |
| 2803 |
if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type), |
| 2804 |
TYPE_PRECISION (op2.type ())), |
| 2805 |
UNSIGNED)) |
| 2806 |
return false; |
| 2807 |
if (shift == 0) |
| 2808 |
{ |
| 2809 |
r.intersect (lhs); |
| 2810 |
return true; |
| 2811 |
} |
| 2812 |
|
| 2813 |
// Work completely in unsigned mode to start. |
| 2814 |
tree utype = type; |
| 2815 |
int_range_max tmp_range; |
| 2816 |
if (TYPE_SIGN (type) == SIGNED) |
| 2817 |
{ |
| 2818 |
int_range_max tmp = lhs; |
| 2819 |
utype = unsigned_type_for (type); |
| 2820 |
range_cast (tmp, utype); |
| 2821 |
op_rshift.fold_range (tmp_range, utype, tmp, op2); |
| 2822 |
} |
| 2823 |
else |
| 2824 |
op_rshift.fold_range (tmp_range, utype, lhs, op2); |
| 2825 |
|
| 2826 |
// Start with ranges which can produce the LHS by right shifting the |
| 2827 |
// result by the shift amount. |
| 2828 |
// ie [0x08, 0xF0] = op1 << 2 will start with |
| 2829 |
// [00001000, 11110000] = op1 << 2 |
| 2830 |
// [0x02, 0x4C] aka [00000010, 00111100] |
| 2831 |
|
| 2832 |
// Then create a range from the LB with the least significant upper bit |
| 2833 |
// set, to the upper bound with all the bits set. |
| 2834 |
// This would be [0x42, 0xFC] aka [01000010, 11111100]. |
| 2835 |
|
| 2836 |
// Ideally we do this for each subrange, but just lump them all for now. |
| 2837 |
unsigned low_bits = TYPE_PRECISION (utype) - shift.to_uhwi (); |
| 2838 |
wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype)); |
| 2839 |
wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ()); |
| 2840 |
wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits); |
| 2841 |
int_range<2> fill_range (utype, new_lb, new_ub); |
| 2842 |
tmp_range.union_ (fill_range); |
| 2843 |
|
| 2844 |
if (utype != type) |
| 2845 |
range_cast (tmp_range, type); |
| 2846 |
|
| 2847 |
r.intersect (tmp_range); |
| 2848 |
return true; |
| 2849 |
} |
| 2850 |
|
| 2851 |
return !r.varying_p (); |
| 2852 |
} |
| 2853 |
|
| 2854 |
bool |
| 2855 |
operator_rshift::op1_range (irange &r, |
| 2856 |
tree type, |
| 2857 |
const irange &lhs, |
| 2858 |
const irange &op2, |
| 2859 |
relation_trio) const |
| 2860 |
{ |
| 2861 |
if (lhs.undefined_p ()) |
| 2862 |
return false; |
| 2863 |
wide_int shift; |
| 2864 |
if (op2.singleton_p (shift)) |
| 2865 |
{ |
| 2866 |
// Ignore nonsensical shifts. |
| 2867 |
unsigned prec = TYPE_PRECISION (type); |
| 2868 |
if (wi::ge_p (shift, |
| 2869 |
wi::uhwi (prec, TYPE_PRECISION (op2.type ())), |
| 2870 |
UNSIGNED)) |
| 2871 |
return false; |
| 2872 |
if (shift == 0) |
| 2873 |
{ |
| 2874 |
r = lhs; |
| 2875 |
return true; |
| 2876 |
} |
| 2877 |
|
| 2878 |
// Folding the original operation may discard some impossible |
| 2879 |
// ranges from the LHS. |
| 2880 |
int_range_max lhs_refined; |
| 2881 |
op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2); |
| 2882 |
lhs_refined.intersect (lhs); |
| 2883 |
if (lhs_refined.undefined_p ()) |
| 2884 |
{ |
| 2885 |
r.set_undefined (); |
| 2886 |
return true; |
| 2887 |
} |
| 2888 |
int_range_max shift_range (op2.type (), shift, shift); |
| 2889 |
int_range_max lb, ub; |
| 2890 |
op_lshift.fold_range (lb, type, lhs_refined, shift_range); |
| 2891 |
// LHS |
| 2892 |
// 0000 0111 = OP1 >> 3 |
| 2893 |
// |
| 2894 |
// OP1 is anything from 0011 1000 to 0011 1111. That is, a |
| 2895 |
// range from LHS<<3 plus a mask of the 3 bits we shifted on the |
| 2896 |
// right hand side (0x07). |
| 2897 |
wide_int mask = wi::mask (shift.to_uhwi (), false, prec); |
| 2898 |
int_range_max mask_range (type, |
| 2899 |
wi::zero (TYPE_PRECISION (type)), |
| 2900 |
mask); |
| 2901 |
op_plus.fold_range (ub, type, lb, mask_range); |
| 2902 |
r = lb; |
| 2903 |
r.union_ (ub); |
| 2904 |
if (!contains_zero_p (lhs_refined)) |
| 2905 |
{ |
| 2906 |
mask_range.invert (); |
| 2907 |
r.intersect (mask_range); |
| 2908 |
} |
| 2909 |
return true; |
| 2910 |
} |
| 2911 |
return false; |
| 2912 |
} |
| 2913 |
|
| 2914 |
bool |
| 2915 |
operator_rshift::wi_op_overflows (wide_int &res, |
| 2916 |
tree type, |
| 2917 |
const wide_int &w0, |
| 2918 |
const wide_int &w1) const |
| 2919 |
{ |
| 2920 |
signop sign = TYPE_SIGN (type); |
| 2921 |
if (wi::neg_p (w1)) |
| 2922 |
res = wi::lshift (w0, -w1); |
| 2923 |
else |
| 2924 |
{ |
| 2925 |
// It's unclear from the C standard whether shifts can overflow. |
| 2926 |
// The following code ignores overflow; perhaps a C standard |
| 2927 |
// interpretation ruling is needed. |
| 2928 |
res = wi::rshift (w0, w1, sign); |
| 2929 |
} |
| 2930 |
return false; |
| 2931 |
} |
| 2932 |
|
| 2933 |
bool |
| 2934 |
operator_rshift::fold_range (irange &r, tree type, |
| 2935 |
const irange &op1, |
| 2936 |
const irange &op2, |
| 2937 |
relation_trio rel) const |
| 2938 |
{ |
| 2939 |
int_range_max shift; |
| 2940 |
if (!get_shift_range (shift, type, op2)) |
| 2941 |
{ |
| 2942 |
if (op2.undefined_p ()) |
| 2943 |
r.set_undefined (); |
| 2944 |
else |
| 2945 |
r.set_zero (type); |
| 2946 |
return true; |
| 2947 |
} |
| 2948 |
|
| 2949 |
return range_operator::fold_range (r, type, op1, shift, rel); |
| 2950 |
} |
| 2951 |
|
| 2952 |
void |
| 2953 |
operator_rshift::wi_fold (irange &r, tree type, |
| 2954 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 2955 |
const wide_int &rh_lb, const wide_int &rh_ub) const |
| 2956 |
{ |
| 2957 |
wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub); |
| 2958 |
} |
| 2959 |
|
| 2960 |
|
| 2961 |
// Add a partial equivalence between the LHS and op1 for casts. |
| 2962 |
|
| 2963 |
relation_kind |
| 2964 |
operator_cast::lhs_op1_relation (const irange &lhs, |
| 2965 |
const irange &op1, |
| 2966 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 2967 |
relation_kind) const |
| 2968 |
{ |
| 2969 |
if (lhs.undefined_p () || op1.undefined_p ()) |
| 2970 |
return VREL_VARYING; |
| 2971 |
unsigned lhs_prec = TYPE_PRECISION (lhs.type ()); |
| 2972 |
unsigned op1_prec = TYPE_PRECISION (op1.type ()); |
| 2973 |
// If the result gets sign extended into a larger type check first if this |
| 2974 |
// qualifies as a partial equivalence. |
| 2975 |
if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec) |
| 2976 |
{ |
| 2977 |
// If the result is sign extended, and the LHS is larger than op1, |
| 2978 |
// check if op1's range can be negative as the sign extension will |
| 2979 |
// cause the upper bits to be 1 instead of 0, invalidating the PE. |
| 2980 |
int_range<3> negs = range_negatives (op1.type ()); |
| 2981 |
negs.intersect (op1); |
| 2982 |
if (!negs.undefined_p ()) |
| 2983 |
return VREL_VARYING; |
| 2984 |
} |
| 2985 |
|
| 2986 |
unsigned prec = MIN (lhs_prec, op1_prec); |
| 2987 |
return bits_to_pe (prec); |
| 2988 |
} |
| 2989 |
|
| 2990 |
// Return TRUE if casting from INNER to OUTER is a truncating cast. |
| 2991 |
|
| 2992 |
inline bool |
| 2993 |
operator_cast::truncating_cast_p (const irange &inner, |
| 2994 |
const irange &outer) const |
| 2995 |
{ |
| 2996 |
return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ()); |
| 2997 |
} |
| 2998 |
|
| 2999 |
// Return TRUE if [MIN,MAX] is inside the domain of RANGE's type. |
| 3000 |
|
| 3001 |
bool |
| 3002 |
operator_cast::inside_domain_p (const wide_int &min, |
| 3003 |
const wide_int &max, |
| 3004 |
const irange &range) const |
| 3005 |
{ |
| 3006 |
wide_int domain_min = irange_val_min (range.type ()); |
| 3007 |
wide_int domain_max = irange_val_max (range.type ()); |
| 3008 |
signop domain_sign = TYPE_SIGN (range.type ()); |
| 3009 |
return (wi::le_p (min, domain_max, domain_sign) |
| 3010 |
&& wi::le_p (max, domain_max, domain_sign) |
| 3011 |
&& wi::ge_p (min, domain_min, domain_sign) |
| 3012 |
&& wi::ge_p (max, domain_min, domain_sign)); |
| 3013 |
} |
| 3014 |
|
| 3015 |
|
| 3016 |
// Helper for fold_range which work on a pair at a time. |
| 3017 |
|
| 3018 |
void |
| 3019 |
operator_cast::fold_pair (irange &r, unsigned index, |
| 3020 |
const irange &inner, |
| 3021 |
const irange &outer) const |
| 3022 |
{ |
| 3023 |
tree inner_type = inner.type (); |
| 3024 |
tree outer_type = outer.type (); |
| 3025 |
signop inner_sign = TYPE_SIGN (inner_type); |
| 3026 |
unsigned outer_prec = TYPE_PRECISION (outer_type); |
| 3027 |
|
| 3028 |
// check to see if casting from INNER to OUTER is a conversion that |
| 3029 |
// fits in the resulting OUTER type. |
| 3030 |
wide_int inner_lb = inner.lower_bound (index); |
| 3031 |
wide_int inner_ub = inner.upper_bound (index); |
| 3032 |
if (truncating_cast_p (inner, outer)) |
| 3033 |
{ |
| 3034 |
// We may be able to accommodate a truncating cast if the |
| 3035 |
// resulting range can be represented in the target type... |
| 3036 |
if (wi::rshift (wi::sub (inner_ub, inner_lb), |
| 3037 |
wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())), |
| 3038 |
inner_sign) != 0) |
| 3039 |
{ |
| 3040 |
r.set_varying (outer_type); |
| 3041 |
return; |
| 3042 |
} |
| 3043 |
} |
| 3044 |
// ...but we must still verify that the final range fits in the |
| 3045 |
// domain. This catches -fstrict-enum restrictions where the domain |
| 3046 |
// range is smaller than what fits in the underlying type. |
| 3047 |
wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign); |
| 3048 |
wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign); |
| 3049 |
if (inside_domain_p (min, max, outer)) |
| 3050 |
create_possibly_reversed_range (r, outer_type, min, max); |
| 3051 |
else |
| 3052 |
r.set_varying (outer_type); |
| 3053 |
} |
| 3054 |
|
| 3055 |
|
| 3056 |
bool |
| 3057 |
operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED, |
| 3058 |
const irange &inner, |
| 3059 |
const irange &outer, |
| 3060 |
relation_trio) const |
| 3061 |
{ |
| 3062 |
if (empty_range_varying (r, type, inner, outer)) |
| 3063 |
return true; |
| 3064 |
|
| 3065 |
gcc_checking_assert (outer.varying_p ()); |
| 3066 |
gcc_checking_assert (inner.num_pairs () > 0); |
| 3067 |
|
| 3068 |
// Avoid a temporary by folding the first pair directly into the result. |
| 3069 |
fold_pair (r, 0, inner, outer); |
| 3070 |
|
| 3071 |
// Then process any additional pairs by unioning with their results. |
| 3072 |
for (unsigned x = 1; x < inner.num_pairs (); ++x) |
| 3073 |
{ |
| 3074 |
int_range_max tmp; |
| 3075 |
fold_pair (tmp, x, inner, outer); |
| 3076 |
r.union_ (tmp); |
| 3077 |
if (r.varying_p ()) |
| 3078 |
return true; |
| 3079 |
} |
| 3080 |
|
| 3081 |
update_bitmask (r, inner, outer); |
| 3082 |
return true; |
| 3083 |
} |
| 3084 |
|
| 3085 |
void |
| 3086 |
operator_cast::update_bitmask (irange &r, const irange &lh, |
| 3087 |
const irange &rh) const |
| 3088 |
{ |
| 3089 |
update_known_bitmask (r, CONVERT_EXPR, lh, rh); |
| 3090 |
} |
| 3091 |
|
| 3092 |
bool |
| 3093 |
operator_cast::op1_range (irange &r, tree type, |
| 3094 |
const irange &lhs, |
| 3095 |
const irange &op2, |
| 3096 |
relation_trio) const |
| 3097 |
{ |
| 3098 |
if (lhs.undefined_p ()) |
| 3099 |
return false; |
| 3100 |
tree lhs_type = lhs.type (); |
| 3101 |
gcc_checking_assert (types_compatible_p (op2.type(), type)); |
| 3102 |
|
| 3103 |
// If we are calculating a pointer, shortcut to what we really care about. |
| 3104 |
if (POINTER_TYPE_P (type)) |
| 3105 |
{ |
| 3106 |
// Conversion from other pointers or a constant (including 0/NULL) |
| 3107 |
// are straightforward. |
| 3108 |
if (POINTER_TYPE_P (lhs.type ()) |
| 3109 |
|| (lhs.singleton_p () |
| 3110 |
&& TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type))) |
| 3111 |
{ |
| 3112 |
r = lhs; |
| 3113 |
range_cast (r, type); |
| 3114 |
} |
| 3115 |
else |
| 3116 |
{ |
| 3117 |
// If the LHS is not a pointer nor a singleton, then it is |
| 3118 |
// either VARYING or non-zero. |
| 3119 |
if (!lhs.undefined_p () && !contains_zero_p (lhs)) |
| 3120 |
r.set_nonzero (type); |
| 3121 |
else |
| 3122 |
r.set_varying (type); |
| 3123 |
} |
| 3124 |
r.intersect (op2); |
| 3125 |
return true; |
| 3126 |
} |
| 3127 |
|
| 3128 |
if (truncating_cast_p (op2, lhs)) |
| 3129 |
{ |
| 3130 |
if (lhs.varying_p ()) |
| 3131 |
r.set_varying (type); |
| 3132 |
else |
| 3133 |
{ |
| 3134 |
// We want to insert the LHS as an unsigned value since it |
| 3135 |
// would not trigger the signed bit of the larger type. |
| 3136 |
int_range_max converted_lhs = lhs; |
| 3137 |
range_cast (converted_lhs, unsigned_type_for (lhs_type)); |
| 3138 |
range_cast (converted_lhs, type); |
| 3139 |
// Start by building the positive signed outer range for the type. |
| 3140 |
wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type), |
| 3141 |
TYPE_PRECISION (type)); |
| 3142 |
create_possibly_reversed_range (r, type, lim, |
| 3143 |
wi::max_value (TYPE_PRECISION (type), |
| 3144 |
SIGNED)); |
| 3145 |
// For the signed part, we need to simply union the 2 ranges now. |
| 3146 |
r.union_ (converted_lhs); |
| 3147 |
|
| 3148 |
// Create maximal negative number outside of LHS bits. |
| 3149 |
lim = wi::mask (TYPE_PRECISION (lhs_type), true, |
| 3150 |
TYPE_PRECISION (type)); |
| 3151 |
// Add this to the unsigned LHS range(s). |
| 3152 |
int_range_max lim_range (type, lim, lim); |
| 3153 |
int_range_max lhs_neg; |
| 3154 |
range_op_handler (PLUS_EXPR).fold_range (lhs_neg, type, |
| 3155 |
converted_lhs, lim_range); |
| 3156 |
// lhs_neg now has all the negative versions of the LHS. |
| 3157 |
// Now union in all the values from SIGNED MIN (0x80000) to |
| 3158 |
// lim-1 in order to fill in all the ranges with the upper |
| 3159 |
// bits set. |
| 3160 |
|
| 3161 |
// PR 97317. If the lhs has only 1 bit less precision than the rhs, |
| 3162 |
// we don't need to create a range from min to lim-1 |
| 3163 |
// calculate neg range traps trying to create [lim, lim - 1]. |
| 3164 |
wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED); |
| 3165 |
if (lim != min_val) |
| 3166 |
{ |
| 3167 |
int_range_max neg (type, |
| 3168 |
wi::min_value (TYPE_PRECISION (type), |
| 3169 |
SIGNED), |
| 3170 |
lim - 1); |
| 3171 |
lhs_neg.union_ (neg); |
| 3172 |
} |
| 3173 |
// And finally, munge the signed and unsigned portions. |
| 3174 |
r.union_ (lhs_neg); |
| 3175 |
} |
| 3176 |
// And intersect with any known value passed in the extra operand. |
| 3177 |
r.intersect (op2); |
| 3178 |
return true; |
| 3179 |
} |
| 3180 |
|
| 3181 |
int_range_max tmp; |
| 3182 |
if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type)) |
| 3183 |
tmp = lhs; |
| 3184 |
else |
| 3185 |
{ |
| 3186 |
// The cast is not truncating, and the range is restricted to |
| 3187 |
// the range of the RHS by this assignment. |
| 3188 |
// |
| 3189 |
// Cast the range of the RHS to the type of the LHS. |
| 3190 |
fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type)); |
| 3191 |
// Intersect this with the LHS range will produce the range, |
| 3192 |
// which will be cast to the RHS type before returning. |
| 3193 |
tmp.intersect (lhs); |
| 3194 |
} |
| 3195 |
|
| 3196 |
// Cast the calculated range to the type of the RHS. |
| 3197 |
fold_range (r, type, tmp, int_range<1> (type)); |
| 3198 |
return true; |
| 3199 |
} |
| 3200 |
|
| 3201 |
|
| 3202 |
class operator_logical_and : public range_operator |
| 3203 |
{ |
| 3204 |
using range_operator::fold_range; |
| 3205 |
using range_operator::op1_range; |
| 3206 |
using range_operator::op2_range; |
| 3207 |
public: |
| 3208 |
virtual bool fold_range (irange &r, tree type, |
| 3209 |
const irange &lh, |
| 3210 |
const irange &rh, |
| 3211 |
relation_trio rel = TRIO_VARYING) const; |
| 3212 |
virtual bool op1_range (irange &r, tree type, |
| 3213 |
const irange &lhs, |
| 3214 |
const irange &op2, |
| 3215 |
relation_trio rel = TRIO_VARYING) const; |
| 3216 |
virtual bool op2_range (irange &r, tree type, |
| 3217 |
const irange &lhs, |
| 3218 |
const irange &op1, |
| 3219 |
relation_trio rel = TRIO_VARYING) const; |
| 3220 |
// Check compatibility of all operands. |
| 3221 |
bool operand_check_p (tree t1, tree t2, tree t3) const final override |
| 3222 |
{ return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); } |
| 3223 |
} op_logical_and; |
| 3224 |
|
| 3225 |
bool |
| 3226 |
operator_logical_and::fold_range (irange &r, tree type, |
| 3227 |
const irange &lh, |
| 3228 |
const irange &rh, |
| 3229 |
relation_trio) const |
| 3230 |
{ |
| 3231 |
if (empty_range_varying (r, type, lh, rh)) |
| 3232 |
return true; |
| 3233 |
|
| 3234 |
// Precision of LHS and both operands must match. |
| 3235 |
if (TYPE_PRECISION (lh.type ()) != TYPE_PRECISION (type) |
| 3236 |
|| TYPE_PRECISION (type) != TYPE_PRECISION (rh.type ())) |
| 3237 |
return false; |
| 3238 |
|
| 3239 |
// 0 && anything is 0. |
| 3240 |
if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0)) |
| 3241 |
|| (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0))) |
| 3242 |
r = range_false (type); |
| 3243 |
else if (contains_zero_p (lh) || contains_zero_p (rh)) |
| 3244 |
// To reach this point, there must be a logical 1 on each side, and |
| 3245 |
// the only remaining question is whether there is a zero or not. |
| 3246 |
r = range_true_and_false (type); |
| 3247 |
else |
| 3248 |
r = range_true (type); |
| 3249 |
return true; |
| 3250 |
} |
| 3251 |
|
| 3252 |
bool |
| 3253 |
operator_logical_and::op1_range (irange &r, tree type, |
| 3254 |
const irange &lhs, |
| 3255 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 3256 |
relation_trio) const |
| 3257 |
{ |
| 3258 |
switch (get_bool_state (r, lhs, type)) |
| 3259 |
{ |
| 3260 |
case BRS_TRUE: |
| 3261 |
// A true result means both sides of the AND must be true. |
| 3262 |
r = range_true (type); |
| 3263 |
break; |
| 3264 |
default: |
| 3265 |
// Any other result means only one side has to be false, the |
| 3266 |
// other side can be anything. So we cannot be sure of any |
| 3267 |
// result here. |
| 3268 |
r = range_true_and_false (type); |
| 3269 |
break; |
| 3270 |
} |
| 3271 |
return true; |
| 3272 |
} |
| 3273 |
|
| 3274 |
bool |
| 3275 |
operator_logical_and::op2_range (irange &r, tree type, |
| 3276 |
const irange &lhs, |
| 3277 |
const irange &op1, |
| 3278 |
relation_trio) const |
| 3279 |
{ |
| 3280 |
return operator_logical_and::op1_range (r, type, lhs, op1); |
| 3281 |
} |
| 3282 |
|
| 3283 |
|
| 3284 |
void |
| 3285 |
operator_bitwise_and::update_bitmask (irange &r, const irange &lh, |
| 3286 |
const irange &rh) const |
| 3287 |
{ |
| 3288 |
update_known_bitmask (r, BIT_AND_EXPR, lh, rh); |
| 3289 |
} |
| 3290 |
|
| 3291 |
// Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types |
| 3292 |
// by considering the number of leading redundant sign bit copies. |
| 3293 |
// clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example |
| 3294 |
// [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help). |
| 3295 |
static bool |
| 3296 |
wi_optimize_signed_bitwise_op (irange &r, tree type, |
| 3297 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 3298 |
const wide_int &rh_lb, const wide_int &rh_ub) |
| 3299 |
{ |
| 3300 |
int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub)); |
| 3301 |
int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub)); |
| 3302 |
int new_clrsb = MIN (lh_clrsb, rh_clrsb); |
| 3303 |
if (new_clrsb == 0) |
| 3304 |
return false; |
| 3305 |
int type_prec = TYPE_PRECISION (type); |
| 3306 |
int rprec = (type_prec - new_clrsb) - 1; |
| 3307 |
value_range_with_overflow (r, type, |
| 3308 |
wi::mask (rprec, true, type_prec), |
| 3309 |
wi::mask (rprec, false, type_prec)); |
| 3310 |
return true; |
| 3311 |
} |
| 3312 |
|
| 3313 |
// An AND of 8,16, 32 or 64 bits can produce a partial equivalence between |
| 3314 |
// the LHS and op1. |
| 3315 |
|
| 3316 |
relation_kind |
| 3317 |
operator_bitwise_and::lhs_op1_relation (const irange &lhs, |
| 3318 |
const irange &op1, |
| 3319 |
const irange &op2, |
| 3320 |
relation_kind) const |
| 3321 |
{ |
| 3322 |
if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ()) |
| 3323 |
return VREL_VARYING; |
| 3324 |
if (!op2.singleton_p ()) |
| 3325 |
return VREL_VARYING; |
| 3326 |
// if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE |
| 3327 |
int prec1 = TYPE_PRECISION (op1.type ()); |
| 3328 |
int prec2 = TYPE_PRECISION (op2.type ()); |
| 3329 |
int mask_prec = 0; |
| 3330 |
wide_int mask = op2.lower_bound (); |
| 3331 |
if (wi::eq_p (mask, wi::mask (8, false, prec2))) |
| 3332 |
mask_prec = 8; |
| 3333 |
else if (wi::eq_p (mask, wi::mask (16, false, prec2))) |
| 3334 |
mask_prec = 16; |
| 3335 |
else if (wi::eq_p (mask, wi::mask (32, false, prec2))) |
| 3336 |
mask_prec = 32; |
| 3337 |
else if (wi::eq_p (mask, wi::mask (64, false, prec2))) |
| 3338 |
mask_prec = 64; |
| 3339 |
return bits_to_pe (MIN (prec1, mask_prec)); |
| 3340 |
} |
| 3341 |
|
| 3342 |
// Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if |
| 3343 |
// possible. Basically, see if we can optimize: |
| 3344 |
// |
| 3345 |
// [LB, UB] op Z |
| 3346 |
// into: |
| 3347 |
// [LB op Z, UB op Z] |
| 3348 |
// |
| 3349 |
// If the optimization was successful, accumulate the range in R and |
| 3350 |
// return TRUE. |
| 3351 |
|
| 3352 |
static bool |
| 3353 |
wi_optimize_and_or (irange &r, |
| 3354 |
enum tree_code code, |
| 3355 |
tree type, |
| 3356 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 3357 |
const wide_int &rh_lb, const wide_int &rh_ub) |
| 3358 |
{ |
| 3359 |
// Calculate the singleton mask among the ranges, if any. |
| 3360 |
wide_int lower_bound, upper_bound, mask; |
| 3361 |
if (wi::eq_p (rh_lb, rh_ub)) |
| 3362 |
{ |
| 3363 |
mask = rh_lb; |
| 3364 |
lower_bound = lh_lb; |
| 3365 |
upper_bound = lh_ub; |
| 3366 |
} |
| 3367 |
else if (wi::eq_p (lh_lb, lh_ub)) |
| 3368 |
{ |
| 3369 |
mask = lh_lb; |
| 3370 |
lower_bound = rh_lb; |
| 3371 |
upper_bound = rh_ub; |
| 3372 |
} |
| 3373 |
else |
| 3374 |
return false; |
| 3375 |
|
| 3376 |
// If Z is a constant which (for op | its bitwise not) has n |
| 3377 |
// consecutive least significant bits cleared followed by m 1 |
| 3378 |
// consecutive bits set immediately above it and either |
| 3379 |
// m + n == precision, or (x >> (m + n)) == (y >> (m + n)). |
| 3380 |
// |
| 3381 |
// The least significant n bits of all the values in the range are |
| 3382 |
// cleared or set, the m bits above it are preserved and any bits |
| 3383 |
// above these are required to be the same for all values in the |
| 3384 |
// range. |
| 3385 |
wide_int w = mask; |
| 3386 |
int m = 0, n = 0; |
| 3387 |
if (code == BIT_IOR_EXPR) |
| 3388 |
w = ~w; |
| 3389 |
if (wi::eq_p (w, 0)) |
| 3390 |
n = w.get_precision (); |
| 3391 |
else |
| 3392 |
{ |
| 3393 |
n = wi::ctz (w); |
| 3394 |
w = ~(w | wi::mask (n, false, w.get_precision ())); |
| 3395 |
if (wi::eq_p (w, 0)) |
| 3396 |
m = w.get_precision () - n; |
| 3397 |
else |
| 3398 |
m = wi::ctz (w) - n; |
| 3399 |
} |
| 3400 |
wide_int new_mask = wi::mask (m + n, true, w.get_precision ()); |
| 3401 |
if ((new_mask & lower_bound) != (new_mask & upper_bound)) |
| 3402 |
return false; |
| 3403 |
|
| 3404 |
wide_int res_lb, res_ub; |
| 3405 |
if (code == BIT_AND_EXPR) |
| 3406 |
{ |
| 3407 |
res_lb = wi::bit_and (lower_bound, mask); |
| 3408 |
res_ub = wi::bit_and (upper_bound, mask); |
| 3409 |
} |
| 3410 |
else if (code == BIT_IOR_EXPR) |
| 3411 |
{ |
| 3412 |
res_lb = wi::bit_or (lower_bound, mask); |
| 3413 |
res_ub = wi::bit_or (upper_bound, mask); |
| 3414 |
} |
| 3415 |
else |
| 3416 |
gcc_unreachable (); |
| 3417 |
value_range_with_overflow (r, type, res_lb, res_ub); |
| 3418 |
|
| 3419 |
// Furthermore, if the mask is non-zero, an IOR cannot contain zero. |
| 3420 |
if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0)) |
| 3421 |
{ |
| 3422 |
int_range<2> tmp; |
| 3423 |
tmp.set_nonzero (type); |
| 3424 |
r.intersect (tmp); |
| 3425 |
} |
| 3426 |
return true; |
| 3427 |
} |
| 3428 |
|
| 3429 |
// For range [LB, UB] compute two wide_int bit masks. |
| 3430 |
// |
| 3431 |
// In the MAYBE_NONZERO bit mask, if some bit is unset, it means that |
| 3432 |
// for all numbers in the range the bit is 0, otherwise it might be 0 |
| 3433 |
// or 1. |
| 3434 |
// |
| 3435 |
// In the MUSTBE_NONZERO bit mask, if some bit is set, it means that |
| 3436 |
// for all numbers in the range the bit is 1, otherwise it might be 0 |
| 3437 |
// or 1. |
| 3438 |
|
| 3439 |
void |
| 3440 |
wi_set_zero_nonzero_bits (tree type, |
| 3441 |
const wide_int &lb, const wide_int &ub, |
| 3442 |
wide_int &maybe_nonzero, |
| 3443 |
wide_int &mustbe_nonzero) |
| 3444 |
{ |
| 3445 |
signop sign = TYPE_SIGN (type); |
| 3446 |
|
| 3447 |
if (wi::eq_p (lb, ub)) |
| 3448 |
maybe_nonzero = mustbe_nonzero = lb; |
| 3449 |
else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign)) |
| 3450 |
{ |
| 3451 |
wide_int xor_mask = lb ^ ub; |
| 3452 |
maybe_nonzero = lb | ub; |
| 3453 |
mustbe_nonzero = lb & ub; |
| 3454 |
if (xor_mask != 0) |
| 3455 |
{ |
| 3456 |
wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false, |
| 3457 |
maybe_nonzero.get_precision ()); |
| 3458 |
maybe_nonzero = maybe_nonzero | mask; |
| 3459 |
mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask); |
| 3460 |
} |
| 3461 |
} |
| 3462 |
else |
| 3463 |
{ |
| 3464 |
maybe_nonzero = wi::minus_one (lb.get_precision ()); |
| 3465 |
mustbe_nonzero = wi::zero (lb.get_precision ()); |
| 3466 |
} |
| 3467 |
} |
| 3468 |
|
| 3469 |
void |
| 3470 |
operator_bitwise_and::wi_fold (irange &r, tree type, |
| 3471 |
const wide_int &lh_lb, |
| 3472 |
const wide_int &lh_ub, |
| 3473 |
const wide_int &rh_lb, |
| 3474 |
const wide_int &rh_ub) const |
| 3475 |
{ |
| 3476 |
if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub)) |
| 3477 |
return; |
| 3478 |
|
| 3479 |
wide_int maybe_nonzero_lh, mustbe_nonzero_lh; |
| 3480 |
wide_int maybe_nonzero_rh, mustbe_nonzero_rh; |
| 3481 |
wi_set_zero_nonzero_bits (type, lh_lb, lh_ub, |
| 3482 |
maybe_nonzero_lh, mustbe_nonzero_lh); |
| 3483 |
wi_set_zero_nonzero_bits (type, rh_lb, rh_ub, |
| 3484 |
maybe_nonzero_rh, mustbe_nonzero_rh); |
| 3485 |
|
| 3486 |
wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh; |
| 3487 |
wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh; |
| 3488 |
signop sign = TYPE_SIGN (type); |
| 3489 |
unsigned prec = TYPE_PRECISION (type); |
| 3490 |
// If both input ranges contain only negative values, we can |
| 3491 |
// truncate the result range maximum to the minimum of the |
| 3492 |
// input range maxima. |
| 3493 |
if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign)) |
| 3494 |
{ |
| 3495 |
new_ub = wi::min (new_ub, lh_ub, sign); |
| 3496 |
new_ub = wi::min (new_ub, rh_ub, sign); |
| 3497 |
} |
| 3498 |
// If either input range contains only non-negative values |
| 3499 |
// we can truncate the result range maximum to the respective |
| 3500 |
// maximum of the input range. |
| 3501 |
if (wi::ge_p (lh_lb, 0, sign)) |
| 3502 |
new_ub = wi::min (new_ub, lh_ub, sign); |
| 3503 |
if (wi::ge_p (rh_lb, 0, sign)) |
| 3504 |
new_ub = wi::min (new_ub, rh_ub, sign); |
| 3505 |
// PR68217: In case of signed & sign-bit-CST should |
| 3506 |
// result in [-INF, 0] instead of [-INF, INF]. |
| 3507 |
if (wi::gt_p (new_lb, new_ub, sign)) |
| 3508 |
{ |
| 3509 |
wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec); |
| 3510 |
if (sign == SIGNED |
| 3511 |
&& ((wi::eq_p (lh_lb, lh_ub) |
| 3512 |
&& !wi::cmps (lh_lb, sign_bit)) |
| 3513 |
|| (wi::eq_p (rh_lb, rh_ub) |
| 3514 |
&& !wi::cmps (rh_lb, sign_bit)))) |
| 3515 |
{ |
| 3516 |
new_lb = wi::min_value (prec, sign); |
| 3517 |
new_ub = wi::zero (prec); |
| 3518 |
} |
| 3519 |
} |
| 3520 |
// If the limits got swapped around, return varying. |
| 3521 |
if (wi::gt_p (new_lb, new_ub,sign)) |
| 3522 |
{ |
| 3523 |
if (sign == SIGNED |
| 3524 |
&& wi_optimize_signed_bitwise_op (r, type, |
| 3525 |
lh_lb, lh_ub, |
| 3526 |
rh_lb, rh_ub)) |
| 3527 |
return; |
| 3528 |
r.set_varying (type); |
| 3529 |
} |
| 3530 |
else |
| 3531 |
value_range_with_overflow (r, type, new_lb, new_ub); |
| 3532 |
} |
| 3533 |
|
| 3534 |
static void |
| 3535 |
set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs) |
| 3536 |
{ |
| 3537 |
if (lhs.undefined_p () || contains_zero_p (lhs)) |
| 3538 |
r.set_varying (type); |
| 3539 |
else |
| 3540 |
r.set_nonzero (type); |
| 3541 |
} |
| 3542 |
|
| 3543 |
/* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any |
| 3544 |
(otherwise return VAL). VAL and MASK must be zero-extended for |
| 3545 |
precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT |
| 3546 |
(to transform signed values into unsigned) and at the end xor |
| 3547 |
SGNBIT back. */ |
| 3548 |
|
| 3549 |
wide_int |
| 3550 |
masked_increment (const wide_int &val_in, const wide_int &mask, |
| 3551 |
const wide_int &sgnbit, unsigned int prec) |
| 3552 |
{ |
| 3553 |
wide_int bit = wi::one (prec), res; |
| 3554 |
unsigned int i; |
| 3555 |
|
| 3556 |
wide_int val = val_in ^ sgnbit; |
| 3557 |
for (i = 0; i < prec; i++, bit += bit) |
| 3558 |
{ |
| 3559 |
res = mask; |
| 3560 |
if ((res & bit) == 0) |
| 3561 |
continue; |
| 3562 |
res = bit - 1; |
| 3563 |
res = wi::bit_and_not (val + bit, res); |
| 3564 |
res &= mask; |
| 3565 |
if (wi::gtu_p (res, val)) |
| 3566 |
return res ^ sgnbit; |
| 3567 |
} |
| 3568 |
return val ^ sgnbit; |
| 3569 |
} |
| 3570 |
|
| 3571 |
// This was shamelessly stolen from register_edge_assert_for_2 and |
| 3572 |
// adjusted to work with iranges. |
| 3573 |
|
| 3574 |
void |
| 3575 |
operator_bitwise_and::simple_op1_range_solver (irange &r, tree type, |
| 3576 |
const irange &lhs, |
| 3577 |
const irange &op2) const |
| 3578 |
{ |
| 3579 |
if (!op2.singleton_p ()) |
| 3580 |
{ |
| 3581 |
set_nonzero_range_from_mask (r, type, lhs); |
| 3582 |
return; |
| 3583 |
} |
| 3584 |
unsigned int nprec = TYPE_PRECISION (type); |
| 3585 |
wide_int cst2v = op2.lower_bound (); |
| 3586 |
bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type)); |
| 3587 |
wide_int sgnbit; |
| 3588 |
if (cst2n) |
| 3589 |
sgnbit = wi::set_bit_in_zero (nprec - 1, nprec); |
| 3590 |
else |
| 3591 |
sgnbit = wi::zero (nprec); |
| 3592 |
|
| 3593 |
// Solve [lhs.lower_bound (), +INF] = x & MASK. |
| 3594 |
// |
| 3595 |
// Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and |
| 3596 |
// maximum unsigned value is ~0. For signed comparison, if CST2 |
| 3597 |
// doesn't have the most significant bit set, handle it similarly. If |
| 3598 |
// CST2 has MSB set, the minimum is the same, and maximum is ~0U/2. |
| 3599 |
wide_int valv = lhs.lower_bound (); |
| 3600 |
wide_int minv = valv & cst2v, maxv; |
| 3601 |
bool we_know_nothing = false; |
| 3602 |
if (minv != valv) |
| 3603 |
{ |
| 3604 |
// If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL. |
| 3605 |
minv = masked_increment (valv, cst2v, sgnbit, nprec); |
| 3606 |
if (minv == valv) |
| 3607 |
{ |
| 3608 |
// If we can't determine anything on this bound, fall |
| 3609 |
// through and conservatively solve for the other end point. |
| 3610 |
we_know_nothing = true; |
| 3611 |
} |
| 3612 |
} |
| 3613 |
maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec); |
| 3614 |
if (we_know_nothing) |
| 3615 |
r.set_varying (type); |
| 3616 |
else |
| 3617 |
create_possibly_reversed_range (r, type, minv, maxv); |
| 3618 |
|
| 3619 |
// Solve [-INF, lhs.upper_bound ()] = x & MASK. |
| 3620 |
// |
| 3621 |
// Minimum unsigned value for <= is 0 and maximum unsigned value is |
| 3622 |
// VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest |
| 3623 |
// VAL2 where |
| 3624 |
// VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2 |
| 3625 |
// as maximum. |
| 3626 |
// For signed comparison, if CST2 doesn't have most significant bit |
| 3627 |
// set, handle it similarly. If CST2 has MSB set, the maximum is |
| 3628 |
// the same and minimum is INT_MIN. |
| 3629 |
valv = lhs.upper_bound (); |
| 3630 |
minv = valv & cst2v; |
| 3631 |
if (minv == valv) |
| 3632 |
maxv = valv; |
| 3633 |
else |
| 3634 |
{ |
| 3635 |
maxv = masked_increment (valv, cst2v, sgnbit, nprec); |
| 3636 |
if (maxv == valv) |
| 3637 |
{ |
| 3638 |
// If we couldn't determine anything on either bound, return |
| 3639 |
// undefined. |
| 3640 |
if (we_know_nothing) |
| 3641 |
r.set_undefined (); |
| 3642 |
return; |
| 3643 |
} |
| 3644 |
maxv -= 1; |
| 3645 |
} |
| 3646 |
maxv |= ~cst2v; |
| 3647 |
minv = sgnbit; |
| 3648 |
int_range<2> upper_bits; |
| 3649 |
create_possibly_reversed_range (upper_bits, type, minv, maxv); |
| 3650 |
r.intersect (upper_bits); |
| 3651 |
} |
| 3652 |
|
| 3653 |
bool |
| 3654 |
operator_bitwise_and::op1_range (irange &r, tree type, |
| 3655 |
const irange &lhs, |
| 3656 |
const irange &op2, |
| 3657 |
relation_trio) const |
| 3658 |
{ |
| 3659 |
if (lhs.undefined_p ()) |
| 3660 |
return false; |
| 3661 |
if (types_compatible_p (type, boolean_type_node)) |
| 3662 |
return op_logical_and.op1_range (r, type, lhs, op2); |
| 3663 |
|
| 3664 |
r.set_undefined (); |
| 3665 |
for (unsigned i = 0; i < lhs.num_pairs (); ++i) |
| 3666 |
{ |
| 3667 |
int_range_max chunk (lhs.type (), |
| 3668 |
lhs.lower_bound (i), |
| 3669 |
lhs.upper_bound (i)); |
| 3670 |
int_range_max res; |
| 3671 |
simple_op1_range_solver (res, type, chunk, op2); |
| 3672 |
r.union_ (res); |
| 3673 |
} |
| 3674 |
if (r.undefined_p ()) |
| 3675 |
set_nonzero_range_from_mask (r, type, lhs); |
| 3676 |
|
| 3677 |
// For MASK == op1 & MASK, all the bits in MASK must be set in op1. |
| 3678 |
wide_int mask; |
| 3679 |
if (lhs == op2 && lhs.singleton_p (mask)) |
| 3680 |
{ |
| 3681 |
r.update_bitmask (irange_bitmask (mask, ~mask)); |
| 3682 |
return true; |
| 3683 |
} |
| 3684 |
|
| 3685 |
// For 0 = op1 & MASK, op1 is ~MASK. |
| 3686 |
if (lhs.zero_p () && op2.singleton_p ()) |
| 3687 |
{ |
| 3688 |
wide_int nz = wi::bit_not (op2.get_nonzero_bits ()); |
| 3689 |
int_range<2> tmp (type); |
| 3690 |
tmp.set_nonzero_bits (nz); |
| 3691 |
r.intersect (tmp); |
| 3692 |
} |
| 3693 |
return true; |
| 3694 |
} |
| 3695 |
|
| 3696 |
bool |
| 3697 |
operator_bitwise_and::op2_range (irange &r, tree type, |
| 3698 |
const irange &lhs, |
| 3699 |
const irange &op1, |
| 3700 |
relation_trio) const |
| 3701 |
{ |
| 3702 |
return operator_bitwise_and::op1_range (r, type, lhs, op1); |
| 3703 |
} |
| 3704 |
|
| 3705 |
|
| 3706 |
class operator_logical_or : public range_operator |
| 3707 |
{ |
| 3708 |
using range_operator::fold_range; |
| 3709 |
using range_operator::op1_range; |
| 3710 |
using range_operator::op2_range; |
| 3711 |
public: |
| 3712 |
virtual bool fold_range (irange &r, tree type, |
| 3713 |
const irange &lh, |
| 3714 |
const irange &rh, |
| 3715 |
relation_trio rel = TRIO_VARYING) const; |
| 3716 |
virtual bool op1_range (irange &r, tree type, |
| 3717 |
const irange &lhs, |
| 3718 |
const irange &op2, |
| 3719 |
relation_trio rel = TRIO_VARYING) const; |
| 3720 |
virtual bool op2_range (irange &r, tree type, |
| 3721 |
const irange &lhs, |
| 3722 |
const irange &op1, |
| 3723 |
relation_trio rel = TRIO_VARYING) const; |
| 3724 |
// Check compatibility of all operands. |
| 3725 |
bool operand_check_p (tree t1, tree t2, tree t3) const final override |
| 3726 |
{ return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); } |
| 3727 |
} op_logical_or; |
| 3728 |
|
| 3729 |
bool |
| 3730 |
operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED, |
| 3731 |
const irange &lh, |
| 3732 |
const irange &rh, |
| 3733 |
relation_trio) const |
| 3734 |
{ |
| 3735 |
if (empty_range_varying (r, type, lh, rh)) |
| 3736 |
return true; |
| 3737 |
|
| 3738 |
r = lh; |
| 3739 |
r.union_ (rh); |
| 3740 |
return true; |
| 3741 |
} |
| 3742 |
|
| 3743 |
bool |
| 3744 |
operator_logical_or::op1_range (irange &r, tree type, |
| 3745 |
const irange &lhs, |
| 3746 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 3747 |
relation_trio) const |
| 3748 |
{ |
| 3749 |
switch (get_bool_state (r, lhs, type)) |
| 3750 |
{ |
| 3751 |
case BRS_FALSE: |
| 3752 |
// A false result means both sides of the OR must be false. |
| 3753 |
r = range_false (type); |
| 3754 |
break; |
| 3755 |
default: |
| 3756 |
// Any other result means only one side has to be true, the |
| 3757 |
// other side can be anything. so we can't be sure of any result |
| 3758 |
// here. |
| 3759 |
r = range_true_and_false (type); |
| 3760 |
break; |
| 3761 |
} |
| 3762 |
return true; |
| 3763 |
} |
| 3764 |
|
| 3765 |
bool |
| 3766 |
operator_logical_or::op2_range (irange &r, tree type, |
| 3767 |
const irange &lhs, |
| 3768 |
const irange &op1, |
| 3769 |
relation_trio) const |
| 3770 |
{ |
| 3771 |
return operator_logical_or::op1_range (r, type, lhs, op1); |
| 3772 |
} |
| 3773 |
|
| 3774 |
|
| 3775 |
void |
| 3776 |
operator_bitwise_or::update_bitmask (irange &r, const irange &lh, |
| 3777 |
const irange &rh) const |
| 3778 |
{ |
| 3779 |
update_known_bitmask (r, BIT_IOR_EXPR, lh, rh); |
| 3780 |
} |
| 3781 |
|
| 3782 |
void |
| 3783 |
operator_bitwise_or::wi_fold (irange &r, tree type, |
| 3784 |
const wide_int &lh_lb, |
| 3785 |
const wide_int &lh_ub, |
| 3786 |
const wide_int &rh_lb, |
| 3787 |
const wide_int &rh_ub) const |
| 3788 |
{ |
| 3789 |
if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub)) |
| 3790 |
return; |
| 3791 |
|
| 3792 |
wide_int maybe_nonzero_lh, mustbe_nonzero_lh; |
| 3793 |
wide_int maybe_nonzero_rh, mustbe_nonzero_rh; |
| 3794 |
wi_set_zero_nonzero_bits (type, lh_lb, lh_ub, |
| 3795 |
maybe_nonzero_lh, mustbe_nonzero_lh); |
| 3796 |
wi_set_zero_nonzero_bits (type, rh_lb, rh_ub, |
| 3797 |
maybe_nonzero_rh, mustbe_nonzero_rh); |
| 3798 |
wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh; |
| 3799 |
wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh; |
| 3800 |
signop sign = TYPE_SIGN (type); |
| 3801 |
// If the input ranges contain only positive values we can |
| 3802 |
// truncate the minimum of the result range to the maximum |
| 3803 |
// of the input range minima. |
| 3804 |
if (wi::ge_p (lh_lb, 0, sign) |
| 3805 |
&& wi::ge_p (rh_lb, 0, sign)) |
| 3806 |
{ |
| 3807 |
new_lb = wi::max (new_lb, lh_lb, sign); |
| 3808 |
new_lb = wi::max (new_lb, rh_lb, sign); |
| 3809 |
} |
| 3810 |
// If either input range contains only negative values |
| 3811 |
// we can truncate the minimum of the result range to the |
| 3812 |
// respective minimum range. |
| 3813 |
if (wi::lt_p (lh_ub, 0, sign)) |
| 3814 |
new_lb = wi::max (new_lb, lh_lb, sign); |
| 3815 |
if (wi::lt_p (rh_ub, 0, sign)) |
| 3816 |
new_lb = wi::max (new_lb, rh_lb, sign); |
| 3817 |
// If the limits got swapped around, return a conservative range. |
| 3818 |
if (wi::gt_p (new_lb, new_ub, sign)) |
| 3819 |
{ |
| 3820 |
// Make sure that nonzero|X is nonzero. |
| 3821 |
if (wi::gt_p (lh_lb, 0, sign) |
| 3822 |
|| wi::gt_p (rh_lb, 0, sign) |
| 3823 |
|| wi::lt_p (lh_ub, 0, sign) |
| 3824 |
|| wi::lt_p (rh_ub, 0, sign)) |
| 3825 |
r.set_nonzero (type); |
| 3826 |
else if (sign == SIGNED |
| 3827 |
&& wi_optimize_signed_bitwise_op (r, type, |
| 3828 |
lh_lb, lh_ub, |
| 3829 |
rh_lb, rh_ub)) |
| 3830 |
return; |
| 3831 |
else |
| 3832 |
r.set_varying (type); |
| 3833 |
return; |
| 3834 |
} |
| 3835 |
value_range_with_overflow (r, type, new_lb, new_ub); |
| 3836 |
} |
| 3837 |
|
| 3838 |
bool |
| 3839 |
operator_bitwise_or::op1_range (irange &r, tree type, |
| 3840 |
const irange &lhs, |
| 3841 |
const irange &op2, |
| 3842 |
relation_trio) const |
| 3843 |
{ |
| 3844 |
if (lhs.undefined_p ()) |
| 3845 |
return false; |
| 3846 |
// If this is really a logical wi_fold, call that. |
| 3847 |
if (types_compatible_p (type, boolean_type_node)) |
| 3848 |
return op_logical_or.op1_range (r, type, lhs, op2); |
| 3849 |
|
| 3850 |
if (lhs.zero_p ()) |
| 3851 |
{ |
| 3852 |
r.set_zero (type); |
| 3853 |
return true; |
| 3854 |
} |
| 3855 |
|
| 3856 |
// if (A < 0 && B < 0) |
| 3857 |
// Sometimes gets translated to |
| 3858 |
// _1 = A | B |
| 3859 |
// if (_1 < 0)) |
| 3860 |
// It is useful for ranger to recognize a positive LHS means the RHS |
| 3861 |
// operands are also positive when dealing with the ELSE range.. |
| 3862 |
if (TYPE_SIGN (type) == SIGNED && wi::ge_p (lhs.lower_bound (), 0, SIGNED)) |
| 3863 |
{ |
| 3864 |
unsigned prec = TYPE_PRECISION (type); |
| 3865 |
r.set (type, wi::zero (prec), wi::max_value (prec, SIGNED)); |
| 3866 |
return true; |
| 3867 |
} |
| 3868 |
r.set_varying (type); |
| 3869 |
return true; |
| 3870 |
} |
| 3871 |
|
| 3872 |
bool |
| 3873 |
operator_bitwise_or::op2_range (irange &r, tree type, |
| 3874 |
const irange &lhs, |
| 3875 |
const irange &op1, |
| 3876 |
relation_trio) const |
| 3877 |
{ |
| 3878 |
return operator_bitwise_or::op1_range (r, type, lhs, op1); |
| 3879 |
} |
| 3880 |
|
| 3881 |
void |
| 3882 |
operator_bitwise_xor::update_bitmask (irange &r, const irange &lh, |
| 3883 |
const irange &rh) const |
| 3884 |
{ |
| 3885 |
update_known_bitmask (r, BIT_XOR_EXPR, lh, rh); |
| 3886 |
} |
| 3887 |
|
| 3888 |
void |
| 3889 |
operator_bitwise_xor::wi_fold (irange &r, tree type, |
| 3890 |
const wide_int &lh_lb, |
| 3891 |
const wide_int &lh_ub, |
| 3892 |
const wide_int &rh_lb, |
| 3893 |
const wide_int &rh_ub) const |
| 3894 |
{ |
| 3895 |
signop sign = TYPE_SIGN (type); |
| 3896 |
wide_int maybe_nonzero_lh, mustbe_nonzero_lh; |
| 3897 |
wide_int maybe_nonzero_rh, mustbe_nonzero_rh; |
| 3898 |
wi_set_zero_nonzero_bits (type, lh_lb, lh_ub, |
| 3899 |
maybe_nonzero_lh, mustbe_nonzero_lh); |
| 3900 |
wi_set_zero_nonzero_bits (type, rh_lb, rh_ub, |
| 3901 |
maybe_nonzero_rh, mustbe_nonzero_rh); |
| 3902 |
|
| 3903 |
wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh) |
| 3904 |
| ~(maybe_nonzero_lh | maybe_nonzero_rh)); |
| 3905 |
wide_int result_one_bits |
| 3906 |
= (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh) |
| 3907 |
| wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh)); |
| 3908 |
wide_int new_ub = ~result_zero_bits; |
| 3909 |
wide_int new_lb = result_one_bits; |
| 3910 |
|
| 3911 |
// If the range has all positive or all negative values, the result |
| 3912 |
// is better than VARYING. |
| 3913 |
if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign)) |
| 3914 |
value_range_with_overflow (r, type, new_lb, new_ub); |
| 3915 |
else if (sign == SIGNED |
| 3916 |
&& wi_optimize_signed_bitwise_op (r, type, |
| 3917 |
lh_lb, lh_ub, |
| 3918 |
rh_lb, rh_ub)) |
| 3919 |
; /* Do nothing. */ |
| 3920 |
else |
| 3921 |
r.set_varying (type); |
| 3922 |
|
| 3923 |
/* Furthermore, XOR is non-zero if its arguments can't be equal. */ |
| 3924 |
if (wi::lt_p (lh_ub, rh_lb, sign) |
| 3925 |
|| wi::lt_p (rh_ub, lh_lb, sign) |
| 3926 |
|| wi::ne_p (result_one_bits, 0)) |
| 3927 |
{ |
| 3928 |
int_range<2> tmp; |
| 3929 |
tmp.set_nonzero (type); |
| 3930 |
r.intersect (tmp); |
| 3931 |
} |
| 3932 |
} |
| 3933 |
|
| 3934 |
bool |
| 3935 |
operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range, |
| 3936 |
tree type, |
| 3937 |
const irange &, |
| 3938 |
const irange &, |
| 3939 |
relation_kind rel) const |
| 3940 |
{ |
| 3941 |
if (rel == VREL_VARYING) |
| 3942 |
return false; |
| 3943 |
|
| 3944 |
int_range<2> rel_range; |
| 3945 |
|
| 3946 |
switch (rel) |
| 3947 |
{ |
| 3948 |
case VREL_EQ: |
| 3949 |
rel_range.set_zero (type); |
| 3950 |
break; |
| 3951 |
case VREL_NE: |
| 3952 |
rel_range.set_nonzero (type); |
| 3953 |
break; |
| 3954 |
default: |
| 3955 |
return false; |
| 3956 |
} |
| 3957 |
|
| 3958 |
lhs_range.intersect (rel_range); |
| 3959 |
return true; |
| 3960 |
} |
| 3961 |
|
| 3962 |
bool |
| 3963 |
operator_bitwise_xor::op1_range (irange &r, tree type, |
| 3964 |
const irange &lhs, |
| 3965 |
const irange &op2, |
| 3966 |
relation_trio) const |
| 3967 |
{ |
| 3968 |
if (lhs.undefined_p () || lhs.varying_p ()) |
| 3969 |
{ |
| 3970 |
r = lhs; |
| 3971 |
return true; |
| 3972 |
} |
| 3973 |
if (types_compatible_p (type, boolean_type_node)) |
| 3974 |
{ |
| 3975 |
switch (get_bool_state (r, lhs, type)) |
| 3976 |
{ |
| 3977 |
case BRS_TRUE: |
| 3978 |
if (op2.varying_p ()) |
| 3979 |
r.set_varying (type); |
| 3980 |
else if (op2.zero_p ()) |
| 3981 |
r = range_true (type); |
| 3982 |
// See get_bool_state for the rationale |
| 3983 |
else if (op2.undefined_p () || contains_zero_p (op2)) |
| 3984 |
r = range_true_and_false (type); |
| 3985 |
else |
| 3986 |
r = range_false (type); |
| 3987 |
break; |
| 3988 |
case BRS_FALSE: |
| 3989 |
r = op2; |
| 3990 |
break; |
| 3991 |
default: |
| 3992 |
break; |
| 3993 |
} |
| 3994 |
return true; |
| 3995 |
} |
| 3996 |
r.set_varying (type); |
| 3997 |
return true; |
| 3998 |
} |
| 3999 |
|
| 4000 |
bool |
| 4001 |
operator_bitwise_xor::op2_range (irange &r, tree type, |
| 4002 |
const irange &lhs, |
| 4003 |
const irange &op1, |
| 4004 |
relation_trio) const |
| 4005 |
{ |
| 4006 |
return operator_bitwise_xor::op1_range (r, type, lhs, op1); |
| 4007 |
} |
| 4008 |
|
| 4009 |
class operator_trunc_mod : public range_operator |
| 4010 |
{ |
| 4011 |
using range_operator::op1_range; |
| 4012 |
using range_operator::op2_range; |
| 4013 |
using range_operator::update_bitmask; |
| 4014 |
public: |
| 4015 |
virtual void wi_fold (irange &r, tree type, |
| 4016 |
const wide_int &lh_lb, |
| 4017 |
const wide_int &lh_ub, |
| 4018 |
const wide_int &rh_lb, |
| 4019 |
const wide_int &rh_ub) const; |
| 4020 |
virtual bool op1_range (irange &r, tree type, |
| 4021 |
const irange &lhs, |
| 4022 |
const irange &op2, |
| 4023 |
relation_trio) const; |
| 4024 |
virtual bool op2_range (irange &r, tree type, |
| 4025 |
const irange &lhs, |
| 4026 |
const irange &op1, |
| 4027 |
relation_trio) const; |
| 4028 |
void update_bitmask (irange &r, const irange &lh, const irange &rh) const |
| 4029 |
{ update_known_bitmask (r, TRUNC_MOD_EXPR, lh, rh); } |
| 4030 |
} op_trunc_mod; |
| 4031 |
|
| 4032 |
void |
| 4033 |
operator_trunc_mod::wi_fold (irange &r, tree type, |
| 4034 |
const wide_int &lh_lb, |
| 4035 |
const wide_int &lh_ub, |
| 4036 |
const wide_int &rh_lb, |
| 4037 |
const wide_int &rh_ub) const |
| 4038 |
{ |
| 4039 |
wide_int new_lb, new_ub, tmp; |
| 4040 |
signop sign = TYPE_SIGN (type); |
| 4041 |
unsigned prec = TYPE_PRECISION (type); |
| 4042 |
|
| 4043 |
// Mod 0 is undefined. |
| 4044 |
if (wi_zero_p (type, rh_lb, rh_ub)) |
| 4045 |
{ |
| 4046 |
r.set_undefined (); |
| 4047 |
return; |
| 4048 |
} |
| 4049 |
|
| 4050 |
// Check for constant and try to fold. |
| 4051 |
if (lh_lb == lh_ub && rh_lb == rh_ub) |
| 4052 |
{ |
| 4053 |
wi::overflow_type ov = wi::OVF_NONE; |
| 4054 |
tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov); |
| 4055 |
if (ov == wi::OVF_NONE) |
| 4056 |
{ |
| 4057 |
r = int_range<2> (type, tmp, tmp); |
| 4058 |
return; |
| 4059 |
} |
| 4060 |
} |
| 4061 |
|
| 4062 |
// ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0. |
| 4063 |
new_ub = rh_ub - 1; |
| 4064 |
if (sign == SIGNED) |
| 4065 |
{ |
| 4066 |
tmp = -1 - rh_lb; |
| 4067 |
new_ub = wi::smax (new_ub, tmp); |
| 4068 |
} |
| 4069 |
|
| 4070 |
if (sign == UNSIGNED) |
| 4071 |
new_lb = wi::zero (prec); |
| 4072 |
else |
| 4073 |
{ |
| 4074 |
new_lb = -new_ub; |
| 4075 |
tmp = lh_lb; |
| 4076 |
if (wi::gts_p (tmp, 0)) |
| 4077 |
tmp = wi::zero (prec); |
| 4078 |
new_lb = wi::smax (new_lb, tmp); |
| 4079 |
} |
| 4080 |
tmp = lh_ub; |
| 4081 |
if (sign == SIGNED && wi::neg_p (tmp)) |
| 4082 |
tmp = wi::zero (prec); |
| 4083 |
new_ub = wi::min (new_ub, tmp, sign); |
| 4084 |
|
| 4085 |
value_range_with_overflow (r, type, new_lb, new_ub); |
| 4086 |
} |
| 4087 |
|
| 4088 |
bool |
| 4089 |
operator_trunc_mod::op1_range (irange &r, tree type, |
| 4090 |
const irange &lhs, |
| 4091 |
const irange &, |
| 4092 |
relation_trio) const |
| 4093 |
{ |
| 4094 |
if (lhs.undefined_p ()) |
| 4095 |
return false; |
| 4096 |
// PR 91029. |
| 4097 |
signop sign = TYPE_SIGN (type); |
| 4098 |
unsigned prec = TYPE_PRECISION (type); |
| 4099 |
// (a % b) >= x && x > 0 , then a >= x. |
| 4100 |
if (wi::gt_p (lhs.lower_bound (), 0, sign)) |
| 4101 |
{ |
| 4102 |
r.set (type, lhs.lower_bound (), wi::max_value (prec, sign)); |
| 4103 |
return true; |
| 4104 |
} |
| 4105 |
// (a % b) <= x && x < 0 , then a <= x. |
| 4106 |
if (wi::lt_p (lhs.upper_bound (), 0, sign)) |
| 4107 |
{ |
| 4108 |
r.set (type, wi::min_value (prec, sign), lhs.upper_bound ()); |
| 4109 |
return true; |
| 4110 |
} |
| 4111 |
return false; |
| 4112 |
} |
| 4113 |
|
| 4114 |
bool |
| 4115 |
operator_trunc_mod::op2_range (irange &r, tree type, |
| 4116 |
const irange &lhs, |
| 4117 |
const irange &, |
| 4118 |
relation_trio) const |
| 4119 |
{ |
| 4120 |
if (lhs.undefined_p ()) |
| 4121 |
return false; |
| 4122 |
// PR 91029. |
| 4123 |
signop sign = TYPE_SIGN (type); |
| 4124 |
unsigned prec = TYPE_PRECISION (type); |
| 4125 |
// (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed |
| 4126 |
// or b > x for unsigned. |
| 4127 |
if (wi::gt_p (lhs.lower_bound (), 0, sign)) |
| 4128 |
{ |
| 4129 |
if (sign == SIGNED) |
| 4130 |
r.set (type, wi::neg (lhs.lower_bound ()), |
| 4131 |
lhs.lower_bound (), VR_ANTI_RANGE); |
| 4132 |
else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign), |
| 4133 |
sign)) |
| 4134 |
r.set (type, lhs.lower_bound () + 1, wi::max_value (prec, sign)); |
| 4135 |
else |
| 4136 |
return false; |
| 4137 |
return true; |
| 4138 |
} |
| 4139 |
// (a % b) <= x && x < 0 , then b is in ~[x, -x]. |
| 4140 |
if (wi::lt_p (lhs.upper_bound (), 0, sign)) |
| 4141 |
{ |
| 4142 |
if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign)) |
| 4143 |
r.set (type, lhs.upper_bound (), |
| 4144 |
wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE); |
| 4145 |
else |
| 4146 |
return false; |
| 4147 |
return true; |
| 4148 |
} |
| 4149 |
return false; |
| 4150 |
} |
| 4151 |
|
| 4152 |
|
| 4153 |
class operator_logical_not : public range_operator |
| 4154 |
{ |
| 4155 |
using range_operator::fold_range; |
| 4156 |
using range_operator::op1_range; |
| 4157 |
public: |
| 4158 |
virtual bool fold_range (irange &r, tree type, |
| 4159 |
const irange &lh, |
| 4160 |
const irange &rh, |
| 4161 |
relation_trio rel = TRIO_VARYING) const; |
| 4162 |
virtual bool op1_range (irange &r, tree type, |
| 4163 |
const irange &lhs, |
| 4164 |
const irange &op2, |
| 4165 |
relation_trio rel = TRIO_VARYING) const; |
| 4166 |
// Check compatibility of LHS and op1. |
| 4167 |
bool operand_check_p (tree t1, tree t2, tree) const final override |
| 4168 |
{ return range_compatible_p (t1, t2); } |
| 4169 |
} op_logical_not; |
| 4170 |
|
| 4171 |
// Folding a logical NOT, oddly enough, involves doing nothing on the |
| 4172 |
// forward pass through. During the initial walk backwards, the |
| 4173 |
// logical NOT reversed the desired outcome on the way back, so on the |
| 4174 |
// way forward all we do is pass the range forward. |
| 4175 |
// |
| 4176 |
// b_2 = x_1 < 20 |
| 4177 |
// b_3 = !b_2 |
| 4178 |
// if (b_3) |
| 4179 |
// to determine the TRUE branch, walking backward |
| 4180 |
// if (b_3) if ([1,1]) |
| 4181 |
// b_3 = !b_2 [1,1] = ![0,0] |
| 4182 |
// b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255] |
| 4183 |
// which is the result we are looking for.. so.. pass it through. |
| 4184 |
|
| 4185 |
bool |
| 4186 |
operator_logical_not::fold_range (irange &r, tree type, |
| 4187 |
const irange &lh, |
| 4188 |
const irange &rh ATTRIBUTE_UNUSED, |
| 4189 |
relation_trio) const |
| 4190 |
{ |
| 4191 |
if (empty_range_varying (r, type, lh, rh)) |
| 4192 |
return true; |
| 4193 |
|
| 4194 |
r = lh; |
| 4195 |
if (!lh.varying_p () && !lh.undefined_p ()) |
| 4196 |
r.invert (); |
| 4197 |
|
| 4198 |
return true; |
| 4199 |
} |
| 4200 |
|
| 4201 |
bool |
| 4202 |
operator_logical_not::op1_range (irange &r, |
| 4203 |
tree type, |
| 4204 |
const irange &lhs, |
| 4205 |
const irange &op2, |
| 4206 |
relation_trio) const |
| 4207 |
{ |
| 4208 |
// Logical NOT is involutary...do it again. |
| 4209 |
return fold_range (r, type, lhs, op2); |
| 4210 |
} |
| 4211 |
|
| 4212 |
bool |
| 4213 |
operator_bitwise_not::fold_range (irange &r, tree type, |
| 4214 |
const irange &lh, |
| 4215 |
const irange &rh, |
| 4216 |
relation_trio) const |
| 4217 |
{ |
| 4218 |
if (empty_range_varying (r, type, lh, rh)) |
| 4219 |
return true; |
| 4220 |
|
| 4221 |
if (types_compatible_p (type, boolean_type_node)) |
| 4222 |
return op_logical_not.fold_range (r, type, lh, rh); |
| 4223 |
|
| 4224 |
// ~X is simply -1 - X. |
| 4225 |
int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)), |
| 4226 |
wi::minus_one (TYPE_PRECISION (type))); |
| 4227 |
return range_op_handler (MINUS_EXPR).fold_range (r, type, minusone, lh); |
| 4228 |
} |
| 4229 |
|
| 4230 |
bool |
| 4231 |
operator_bitwise_not::op1_range (irange &r, tree type, |
| 4232 |
const irange &lhs, |
| 4233 |
const irange &op2, |
| 4234 |
relation_trio) const |
| 4235 |
{ |
| 4236 |
if (lhs.undefined_p ()) |
| 4237 |
return false; |
| 4238 |
if (types_compatible_p (type, boolean_type_node)) |
| 4239 |
return op_logical_not.op1_range (r, type, lhs, op2); |
| 4240 |
|
| 4241 |
// ~X is -1 - X and since bitwise NOT is involutary...do it again. |
| 4242 |
return fold_range (r, type, lhs, op2); |
| 4243 |
} |
| 4244 |
|
| 4245 |
void |
| 4246 |
operator_bitwise_not::update_bitmask (irange &r, const irange &lh, |
| 4247 |
const irange &rh) const |
| 4248 |
{ |
| 4249 |
update_known_bitmask (r, BIT_NOT_EXPR, lh, rh); |
| 4250 |
} |
| 4251 |
|
| 4252 |
|
| 4253 |
bool |
| 4254 |
operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED, |
| 4255 |
const irange &lh, |
| 4256 |
const irange &rh ATTRIBUTE_UNUSED, |
| 4257 |
relation_trio) const |
| 4258 |
{ |
| 4259 |
r = lh; |
| 4260 |
return true; |
| 4261 |
} |
| 4262 |
|
| 4263 |
|
| 4264 |
// Determine if there is a relationship between LHS and OP1. |
| 4265 |
|
| 4266 |
relation_kind |
| 4267 |
operator_identity::lhs_op1_relation (const irange &lhs, |
| 4268 |
const irange &op1 ATTRIBUTE_UNUSED, |
| 4269 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 4270 |
relation_kind) const |
| 4271 |
{ |
| 4272 |
if (lhs.undefined_p ()) |
| 4273 |
return VREL_VARYING; |
| 4274 |
// Simply a copy, so they are equivalent. |
| 4275 |
return VREL_EQ; |
| 4276 |
} |
| 4277 |
|
| 4278 |
bool |
| 4279 |
operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED, |
| 4280 |
const irange &lh, |
| 4281 |
const irange &rh ATTRIBUTE_UNUSED, |
| 4282 |
relation_trio) const |
| 4283 |
{ |
| 4284 |
r = lh; |
| 4285 |
return true; |
| 4286 |
} |
| 4287 |
|
| 4288 |
bool |
| 4289 |
operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED, |
| 4290 |
const irange &lhs, |
| 4291 |
const irange &op2 ATTRIBUTE_UNUSED, |
| 4292 |
relation_trio) const |
| 4293 |
{ |
| 4294 |
r = lhs; |
| 4295 |
return true; |
| 4296 |
} |
| 4297 |
|
| 4298 |
|
| 4299 |
class operator_unknown : public range_operator |
| 4300 |
{ |
| 4301 |
using range_operator::fold_range; |
| 4302 |
public: |
| 4303 |
virtual bool fold_range (irange &r, tree type, |
| 4304 |
const irange &op1, |
| 4305 |
const irange &op2, |
| 4306 |
relation_trio rel = TRIO_VARYING) const; |
| 4307 |
} op_unknown; |
| 4308 |
|
| 4309 |
bool |
| 4310 |
operator_unknown::fold_range (irange &r, tree type, |
| 4311 |
const irange &lh ATTRIBUTE_UNUSED, |
| 4312 |
const irange &rh ATTRIBUTE_UNUSED, |
| 4313 |
relation_trio) const |
| 4314 |
{ |
| 4315 |
r.set_varying (type); |
| 4316 |
return true; |
| 4317 |
} |
| 4318 |
|
| 4319 |
|
| 4320 |
void |
| 4321 |
operator_abs::wi_fold (irange &r, tree type, |
| 4322 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 4323 |
const wide_int &rh_lb ATTRIBUTE_UNUSED, |
| 4324 |
const wide_int &rh_ub ATTRIBUTE_UNUSED) const |
| 4325 |
{ |
| 4326 |
wide_int min, max; |
| 4327 |
signop sign = TYPE_SIGN (type); |
| 4328 |
unsigned prec = TYPE_PRECISION (type); |
| 4329 |
|
| 4330 |
// Pass through LH for the easy cases. |
| 4331 |
if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign)) |
| 4332 |
{ |
| 4333 |
r = int_range<1> (type, lh_lb, lh_ub); |
| 4334 |
return; |
| 4335 |
} |
| 4336 |
|
| 4337 |
// -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get |
| 4338 |
// a useful range. |
| 4339 |
wide_int min_value = wi::min_value (prec, sign); |
| 4340 |
wide_int max_value = wi::max_value (prec, sign); |
| 4341 |
if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value)) |
| 4342 |
{ |
| 4343 |
r.set_varying (type); |
| 4344 |
return; |
| 4345 |
} |
| 4346 |
|
| 4347 |
// ABS_EXPR may flip the range around, if the original range |
| 4348 |
// included negative values. |
| 4349 |
if (wi::eq_p (lh_lb, min_value)) |
| 4350 |
{ |
| 4351 |
// ABS ([-MIN, -MIN]) isn't representable, but we have traditionally |
| 4352 |
// returned [-MIN,-MIN] so this preserves that behavior. PR37078 |
| 4353 |
if (wi::eq_p (lh_ub, min_value)) |
| 4354 |
{ |
| 4355 |
r = int_range<1> (type, min_value, min_value); |
| 4356 |
return; |
| 4357 |
} |
| 4358 |
min = max_value; |
| 4359 |
} |
| 4360 |
else |
| 4361 |
min = wi::abs (lh_lb); |
| 4362 |
|
| 4363 |
if (wi::eq_p (lh_ub, min_value)) |
| 4364 |
max = max_value; |
| 4365 |
else |
| 4366 |
max = wi::abs (lh_ub); |
| 4367 |
|
| 4368 |
// If the range contains zero then we know that the minimum value in the |
| 4369 |
// range will be zero. |
| 4370 |
if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign)) |
| 4371 |
{ |
| 4372 |
if (wi::gt_p (min, max, sign)) |
| 4373 |
max = min; |
| 4374 |
min = wi::zero (prec); |
| 4375 |
} |
| 4376 |
else |
| 4377 |
{ |
| 4378 |
// If the range was reversed, swap MIN and MAX. |
| 4379 |
if (wi::gt_p (min, max, sign)) |
| 4380 |
std::swap (min, max); |
| 4381 |
} |
| 4382 |
|
| 4383 |
// If the new range has its limits swapped around (MIN > MAX), then |
| 4384 |
// the operation caused one of them to wrap around. The only thing |
| 4385 |
// we know is that the result is positive. |
| 4386 |
if (wi::gt_p (min, max, sign)) |
| 4387 |
{ |
| 4388 |
min = wi::zero (prec); |
| 4389 |
max = max_value; |
| 4390 |
} |
| 4391 |
r = int_range<1> (type, min, max); |
| 4392 |
} |
| 4393 |
|
| 4394 |
bool |
| 4395 |
operator_abs::op1_range (irange &r, tree type, |
| 4396 |
const irange &lhs, |
| 4397 |
const irange &op2, |
| 4398 |
relation_trio) const |
| 4399 |
{ |
| 4400 |
if (empty_range_varying (r, type, lhs, op2)) |
| 4401 |
return true; |
| 4402 |
if (TYPE_UNSIGNED (type)) |
| 4403 |
{ |
| 4404 |
r = lhs; |
| 4405 |
return true; |
| 4406 |
} |
| 4407 |
// Start with the positives because negatives are an impossible result. |
| 4408 |
int_range_max positives = range_positives (type); |
| 4409 |
positives.intersect (lhs); |
| 4410 |
r = positives; |
| 4411 |
// Then add the negative of each pair: |
| 4412 |
// ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20]. |
| 4413 |
for (unsigned i = 0; i < positives.num_pairs (); ++i) |
| 4414 |
r.union_ (int_range<1> (type, |
| 4415 |
-positives.upper_bound (i), |
| 4416 |
-positives.lower_bound (i))); |
| 4417 |
// With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is |
| 4418 |
// unrepresentable. Add -TYPE_MIN_VALUE in this case. |
| 4419 |
wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type)); |
| 4420 |
wide_int lb = lhs.lower_bound (); |
| 4421 |
if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value)) |
| 4422 |
r.union_ (int_range<2> (type, lb, lb)); |
| 4423 |
return true; |
| 4424 |
} |
| 4425 |
|
| 4426 |
void |
| 4427 |
operator_abs::update_bitmask (irange &r, const irange &lh, |
| 4428 |
const irange &rh) const |
| 4429 |
{ |
| 4430 |
update_known_bitmask (r, ABS_EXPR, lh, rh); |
| 4431 |
} |
| 4432 |
|
| 4433 |
class operator_absu : public range_operator |
| 4434 |
{ |
| 4435 |
using range_operator::update_bitmask; |
| 4436 |
public: |
| 4437 |
virtual void wi_fold (irange &r, tree type, |
| 4438 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 4439 |
const wide_int &rh_lb, const wide_int &rh_ub) const; |
| 4440 |
virtual void update_bitmask (irange &r, const irange &lh, |
| 4441 |
const irange &rh) const final override; |
| 4442 |
} op_absu; |
| 4443 |
|
| 4444 |
void |
| 4445 |
operator_absu::wi_fold (irange &r, tree type, |
| 4446 |
const wide_int &lh_lb, const wide_int &lh_ub, |
| 4447 |
const wide_int &rh_lb ATTRIBUTE_UNUSED, |
| 4448 |
const wide_int &rh_ub ATTRIBUTE_UNUSED) const |
| 4449 |
{ |
| 4450 |
wide_int new_lb, new_ub; |
| 4451 |
|
| 4452 |
// Pass through VR0 the easy cases. |
| 4453 |
if (wi::ges_p (lh_lb, 0)) |
| 4454 |
{ |
| 4455 |
new_lb = lh_lb; |
| 4456 |
new_ub = lh_ub; |
| 4457 |
} |
| 4458 |
else |
| 4459 |
{ |
| 4460 |
new_lb = wi::abs (lh_lb); |
| 4461 |
new_ub = wi::abs (lh_ub); |
| 4462 |
|
| 4463 |
// If the range contains zero then we know that the minimum |
| 4464 |
// value in the range will be zero. |
| 4465 |
if (wi::ges_p (lh_ub, 0)) |
| 4466 |
{ |
| 4467 |
if (wi::gtu_p (new_lb, new_ub)) |
| 4468 |
new_ub = new_lb; |
| 4469 |
new_lb = wi::zero (TYPE_PRECISION (type)); |
| 4470 |
} |
| 4471 |
else |
| 4472 |
std::swap (new_lb, new_ub); |
| 4473 |
} |
| 4474 |
|
| 4475 |
gcc_checking_assert (TYPE_UNSIGNED (type)); |
| 4476 |
r = int_range<1> (type, new_lb, new_ub); |
| 4477 |
} |
| 4478 |
|
| 4479 |
void |
| 4480 |
operator_absu::update_bitmask (irange &r, const irange &lh, |
| 4481 |
const irange &rh) const |
| 4482 |
{ |
| 4483 |
update_known_bitmask (r, ABSU_EXPR, lh, rh); |
| 4484 |
} |
| 4485 |
|
| 4486 |
|
| 4487 |
bool |
| 4488 |
operator_negate::fold_range (irange &r, tree type, |
| 4489 |
const irange &lh, |
| 4490 |
const irange &rh, |
| 4491 |
relation_trio) const |
| 4492 |
{ |
| 4493 |
if (empty_range_varying (r, type, lh, rh)) |
| 4494 |
return true; |
| 4495 |
|
| 4496 |
// -X is simply 0 - X. |
| 4497 |
int_range<1> zero; |
| 4498 |
zero.set_zero (type); |
| 4499 |
return range_op_handler (MINUS_EXPR).fold_range (r, type, zero, lh); |
| 4500 |
} |
| 4501 |
|
| 4502 |
bool |
| 4503 |
operator_negate::op1_range (irange &r, tree type, |
| 4504 |
const irange &lhs, |
| 4505 |
const irange &op2, |
| 4506 |
relation_trio) const |
| 4507 |
{ |
| 4508 |
// NEGATE is involutory. |
| 4509 |
return fold_range (r, type, lhs, op2); |
| 4510 |
} |
| 4511 |
|
| 4512 |
|
| 4513 |
bool |
| 4514 |
operator_addr_expr::fold_range (irange &r, tree type, |
| 4515 |
const irange &lh, |
| 4516 |
const irange &rh, |
| 4517 |
relation_trio) const |
| 4518 |
{ |
| 4519 |
if (empty_range_varying (r, type, lh, rh)) |
| 4520 |
return true; |
| 4521 |
|
| 4522 |
// Return a non-null pointer of the LHS type (passed in op2). |
| 4523 |
if (lh.zero_p ()) |
| 4524 |
r.set_zero (type); |
| 4525 |
else if (lh.undefined_p () || contains_zero_p (lh)) |
| 4526 |
r.set_varying (type); |
| 4527 |
else |
| 4528 |
r.set_nonzero (type); |
| 4529 |
return true; |
| 4530 |
} |
| 4531 |
|
| 4532 |
bool |
| 4533 |
operator_addr_expr::op1_range (irange &r, tree type, |
| 4534 |
const irange &lhs, |
| 4535 |
const irange &op2, |
| 4536 |
relation_trio) const |
| 4537 |
{ |
| 4538 |
if (empty_range_varying (r, type, lhs, op2)) |
| 4539 |
return true; |
| 4540 |
|
| 4541 |
// Return a non-null pointer of the LHS type (passed in op2), but only |
| 4542 |
// if we cant overflow, eitherwise a no-zero offset could wrap to zero. |
| 4543 |
// See PR 111009. |
| 4544 |
if (!lhs.undefined_p () && !contains_zero_p (lhs) && TYPE_OVERFLOW_UNDEFINED (type)) |
| 4545 |
r.set_nonzero (type); |
| 4546 |
else |
| 4547 |
r.set_varying (type); |
| 4548 |
return true; |
| 4549 |
} |
| 4550 |
|
| 4551 |
// Initialize any integral operators to the primary table |
| 4552 |
|
| 4553 |
void |
| 4554 |
range_op_table::initialize_integral_ops () |
| 4555 |
{ |
| 4556 |
set (TRUNC_DIV_EXPR, op_trunc_div); |
| 4557 |
set (FLOOR_DIV_EXPR, op_floor_div); |
| 4558 |
set (ROUND_DIV_EXPR, op_round_div); |
| 4559 |
set (CEIL_DIV_EXPR, op_ceil_div); |
| 4560 |
set (EXACT_DIV_EXPR, op_exact_div); |
| 4561 |
set (LSHIFT_EXPR, op_lshift); |
| 4562 |
set (RSHIFT_EXPR, op_rshift); |
| 4563 |
set (TRUTH_AND_EXPR, op_logical_and); |
| 4564 |
set (TRUTH_OR_EXPR, op_logical_or); |
| 4565 |
set (TRUNC_MOD_EXPR, op_trunc_mod); |
| 4566 |
set (TRUTH_NOT_EXPR, op_logical_not); |
| 4567 |
set (IMAGPART_EXPR, op_unknown); |
| 4568 |
set (REALPART_EXPR, op_unknown); |
| 4569 |
set (ABSU_EXPR, op_absu); |
| 4570 |
set (OP_WIDEN_MULT_SIGNED, op_widen_mult_signed); |
| 4571 |
set (OP_WIDEN_MULT_UNSIGNED, op_widen_mult_unsigned); |
| 4572 |
set (OP_WIDEN_MULT_SIGNED_UNSIGNED, op_widen_mult_signed_unsigned); |
| 4573 |
set (OP_WIDEN_PLUS_SIGNED, op_widen_plus_signed); |
| 4574 |
set (OP_WIDEN_PLUS_UNSIGNED, op_widen_plus_unsigned); |
| 4575 |
|
| 4576 |
} |
| 4577 |
|
| 4578 |
bool |
| 4579 |
operator_plus::overflow_free_p (const irange &lh, const irange &rh, |
| 4580 |
relation_trio) const |
| 4581 |
{ |
| 4582 |
if (lh.undefined_p () || rh.undefined_p ()) |
| 4583 |
return false; |
| 4584 |
|
| 4585 |
tree type = lh.type (); |
| 4586 |
if (TYPE_OVERFLOW_UNDEFINED (type)) |
| 4587 |
return true; |
| 4588 |
|
| 4589 |
wi::overflow_type ovf; |
| 4590 |
signop sgn = TYPE_SIGN (type); |
| 4591 |
wide_int wmax0 = lh.upper_bound (); |
| 4592 |
wide_int wmax1 = rh.upper_bound (); |
| 4593 |
wi::add (wmax0, wmax1, sgn, &ovf); |
| 4594 |
if (ovf != wi::OVF_NONE) |
| 4595 |
return false; |
| 4596 |
|
| 4597 |
if (TYPE_UNSIGNED (type)) |
| 4598 |
return true; |
| 4599 |
|
| 4600 |
wide_int wmin0 = lh.lower_bound (); |
| 4601 |
wide_int wmin1 = rh.lower_bound (); |
| 4602 |
wi::add (wmin0, wmin1, sgn, &ovf); |
| 4603 |
if (ovf != wi::OVF_NONE) |
| 4604 |
return false; |
| 4605 |
|
| 4606 |
return true; |
| 4607 |
} |
| 4608 |
|
| 4609 |
bool |
| 4610 |
operator_minus::overflow_free_p (const irange &lh, const irange &rh, |
| 4611 |
relation_trio) const |
| 4612 |
{ |
| 4613 |
if (lh.undefined_p () || rh.undefined_p ()) |
| 4614 |
return false; |
| 4615 |
|
| 4616 |
tree type = lh.type (); |
| 4617 |
if (TYPE_OVERFLOW_UNDEFINED (type)) |
| 4618 |
return true; |
| 4619 |
|
| 4620 |
wi::overflow_type ovf; |
| 4621 |
signop sgn = TYPE_SIGN (type); |
| 4622 |
wide_int wmin0 = lh.lower_bound (); |
| 4623 |
wide_int wmax1 = rh.upper_bound (); |
| 4624 |
wi::sub (wmin0, wmax1, sgn, &ovf); |
| 4625 |
if (ovf != wi::OVF_NONE) |
| 4626 |
return false; |
| 4627 |
|
| 4628 |
if (TYPE_UNSIGNED (type)) |
| 4629 |
return true; |
| 4630 |
|
| 4631 |
wide_int wmax0 = lh.upper_bound (); |
| 4632 |
wide_int wmin1 = rh.lower_bound (); |
| 4633 |
wi::sub (wmax0, wmin1, sgn, &ovf); |
| 4634 |
if (ovf != wi::OVF_NONE) |
| 4635 |
return false; |
| 4636 |
|
| 4637 |
return true; |
| 4638 |
} |
| 4639 |
|
| 4640 |
bool |
| 4641 |
operator_mult::overflow_free_p (const irange &lh, const irange &rh, |
| 4642 |
relation_trio) const |
| 4643 |
{ |
| 4644 |
if (lh.undefined_p () || rh.undefined_p ()) |
| 4645 |
return false; |
| 4646 |
|
| 4647 |
tree type = lh.type (); |
| 4648 |
if (TYPE_OVERFLOW_UNDEFINED (type)) |
| 4649 |
return true; |
| 4650 |
|
| 4651 |
wi::overflow_type ovf; |
| 4652 |
signop sgn = TYPE_SIGN (type); |
| 4653 |
wide_int wmax0 = lh.upper_bound (); |
| 4654 |
wide_int wmax1 = rh.upper_bound (); |
| 4655 |
wi::mul (wmax0, wmax1, sgn, &ovf); |
| 4656 |
if (ovf != wi::OVF_NONE) |
| 4657 |
return false; |
| 4658 |
|
| 4659 |
if (TYPE_UNSIGNED (type)) |
| 4660 |
return true; |
| 4661 |
|
| 4662 |
wide_int wmin0 = lh.lower_bound (); |
| 4663 |
wide_int wmin1 = rh.lower_bound (); |
| 4664 |
wi::mul (wmin0, wmin1, sgn, &ovf); |
| 4665 |
if (ovf != wi::OVF_NONE) |
| 4666 |
return false; |
| 4667 |
|
| 4668 |
wi::mul (wmin0, wmax1, sgn, &ovf); |
| 4669 |
if (ovf != wi::OVF_NONE) |
| 4670 |
return false; |
| 4671 |
|
| 4672 |
wi::mul (wmax0, wmin1, sgn, &ovf); |
| 4673 |
if (ovf != wi::OVF_NONE) |
| 4674 |
return false; |
| 4675 |
|
| 4676 |
return true; |
| 4677 |
} |
| 4678 |
|
| 4679 |
#if CHECKING_P |
| 4680 |
#include "selftest.h" |
| 4681 |
|
| 4682 |
namespace selftest |
| 4683 |
{ |
| 4684 |
#define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node)) |
| 4685 |
#define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node)) |
| 4686 |
#define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node)) |
| 4687 |
#define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node)) |
| 4688 |
#define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node)) |
| 4689 |
#define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node)) |
| 4690 |
|
| 4691 |
static void |
| 4692 |
range_op_cast_tests () |
| 4693 |
{ |
| 4694 |
int_range<2> r0, r1, r2, rold; |
| 4695 |
r0.set_varying (integer_type_node); |
| 4696 |
wide_int maxint = r0.upper_bound (); |
| 4697 |
|
| 4698 |
// If a range is in any way outside of the range for the converted |
| 4699 |
// to range, default to the range for the new type. |
| 4700 |
r0.set_varying (short_integer_type_node); |
| 4701 |
wide_int minshort = r0.lower_bound (); |
| 4702 |
wide_int maxshort = r0.upper_bound (); |
| 4703 |
if (TYPE_PRECISION (integer_type_node) |
| 4704 |
> TYPE_PRECISION (short_integer_type_node)) |
| 4705 |
{ |
| 4706 |
r1 = int_range<1> (integer_type_node, |
| 4707 |
wi::zero (TYPE_PRECISION (integer_type_node)), |
| 4708 |
maxint); |
| 4709 |
range_cast (r1, short_integer_type_node); |
| 4710 |
ASSERT_TRUE (r1.lower_bound () == minshort |
| 4711 |
&& r1.upper_bound() == maxshort); |
| 4712 |
} |
| 4713 |
|
| 4714 |
// (unsigned char)[-5,-1] => [251,255]. |
| 4715 |
r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (-1)); |
| 4716 |
range_cast (r0, unsigned_char_type_node); |
| 4717 |
ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node, |
| 4718 |
UCHAR (251), UCHAR (255))); |
| 4719 |
range_cast (r0, signed_char_type_node); |
| 4720 |
ASSERT_TRUE (r0 == rold); |
| 4721 |
|
| 4722 |
// (signed char)[15, 150] => [-128,-106][15,127]. |
| 4723 |
r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (15), UCHAR (150)); |
| 4724 |
range_cast (r0, signed_char_type_node); |
| 4725 |
r1 = int_range<1> (signed_char_type_node, SCHAR (15), SCHAR (127)); |
| 4726 |
r2 = int_range<1> (signed_char_type_node, SCHAR (-128), SCHAR (-106)); |
| 4727 |
r1.union_ (r2); |
| 4728 |
ASSERT_TRUE (r1 == r0); |
| 4729 |
range_cast (r0, unsigned_char_type_node); |
| 4730 |
ASSERT_TRUE (r0 == rold); |
| 4731 |
|
| 4732 |
// (unsigned char)[-5, 5] => [0,5][251,255]. |
| 4733 |
r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (5)); |
| 4734 |
range_cast (r0, unsigned_char_type_node); |
| 4735 |
r1 = int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255)); |
| 4736 |
r2 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5)); |
| 4737 |
r1.union_ (r2); |
| 4738 |
ASSERT_TRUE (r0 == r1); |
| 4739 |
range_cast (r0, signed_char_type_node); |
| 4740 |
ASSERT_TRUE (r0 == rold); |
| 4741 |
|
| 4742 |
// (unsigned char)[-5,5] => [0,5][251,255]. |
| 4743 |
r0 = int_range<1> (integer_type_node, INT (-5), INT (5)); |
| 4744 |
range_cast (r0, unsigned_char_type_node); |
| 4745 |
r1 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5)); |
| 4746 |
r1.union_ (int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255))); |
| 4747 |
ASSERT_TRUE (r0 == r1); |
| 4748 |
|
| 4749 |
// (unsigned char)[5U,1974U] => [0,255]. |
| 4750 |
r0 = int_range<1> (unsigned_type_node, UINT (5), UINT (1974)); |
| 4751 |
range_cast (r0, unsigned_char_type_node); |
| 4752 |
ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (255))); |
| 4753 |
range_cast (r0, integer_type_node); |
| 4754 |
// Going to a wider range should not sign extend. |
| 4755 |
ASSERT_TRUE (r0 == int_range<1> (integer_type_node, INT (0), INT (255))); |
| 4756 |
|
| 4757 |
// (unsigned char)[-350,15] => [0,255]. |
| 4758 |
r0 = int_range<1> (integer_type_node, INT (-350), INT (15)); |
| 4759 |
range_cast (r0, unsigned_char_type_node); |
| 4760 |
ASSERT_TRUE (r0 == (int_range<1> |
| 4761 |
(unsigned_char_type_node, |
| 4762 |
min_limit (unsigned_char_type_node), |
| 4763 |
max_limit (unsigned_char_type_node)))); |
| 4764 |
|
| 4765 |
// Casting [-120,20] from signed char to unsigned short. |
| 4766 |
// => [0, 20][0xff88, 0xffff]. |
| 4767 |
r0 = int_range<1> (signed_char_type_node, SCHAR (-120), SCHAR (20)); |
| 4768 |
range_cast (r0, short_unsigned_type_node); |
| 4769 |
r1 = int_range<1> (short_unsigned_type_node, UINT16 (0), UINT16 (20)); |
| 4770 |
r2 = int_range<1> (short_unsigned_type_node, |
| 4771 |
UINT16 (0xff88), UINT16 (0xffff)); |
| 4772 |
r1.union_ (r2); |
| 4773 |
ASSERT_TRUE (r0 == r1); |
| 4774 |
// A truncating cast back to signed char will work because [-120, 20] |
| 4775 |
// is representable in signed char. |
| 4776 |
range_cast (r0, signed_char_type_node); |
| 4777 |
ASSERT_TRUE (r0 == int_range<1> (signed_char_type_node, |
| 4778 |
SCHAR (-120), SCHAR (20))); |
| 4779 |
|
| 4780 |
// unsigned char -> signed short |
| 4781 |
// (signed short)[(unsigned char)25, (unsigned char)250] |
| 4782 |
// => [(signed short)25, (signed short)250] |
| 4783 |
r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (25), UCHAR (250)); |
| 4784 |
range_cast (r0, short_integer_type_node); |
| 4785 |
r1 = int_range<1> (short_integer_type_node, INT16 (25), INT16 (250)); |
| 4786 |
ASSERT_TRUE (r0 == r1); |
| 4787 |
range_cast (r0, unsigned_char_type_node); |
| 4788 |
ASSERT_TRUE (r0 == rold); |
| 4789 |
|
| 4790 |
// Test casting a wider signed [-MIN,MAX] to a narrower unsigned. |
| 4791 |
r0 = int_range<1> (long_long_integer_type_node, |
| 4792 |
min_limit (long_long_integer_type_node), |
| 4793 |
max_limit (long_long_integer_type_node)); |
| 4794 |
range_cast (r0, short_unsigned_type_node); |
| 4795 |
r1 = int_range<1> (short_unsigned_type_node, |
| 4796 |
min_limit (short_unsigned_type_node), |
| 4797 |
max_limit (short_unsigned_type_node)); |
| 4798 |
ASSERT_TRUE (r0 == r1); |
| 4799 |
|
| 4800 |
// Casting NONZERO to a narrower type will wrap/overflow so |
| 4801 |
// it's just the entire range for the narrower type. |
| 4802 |
// |
| 4803 |
// "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is |
| 4804 |
// is outside of the range of a smaller range, return the full |
| 4805 |
// smaller range. |
| 4806 |
if (TYPE_PRECISION (integer_type_node) |
| 4807 |
> TYPE_PRECISION (short_integer_type_node)) |
| 4808 |
{ |
| 4809 |
r0.set_nonzero (integer_type_node); |
| 4810 |
range_cast (r0, short_integer_type_node); |
| 4811 |
r1 = int_range<1> (short_integer_type_node, |
| 4812 |
min_limit (short_integer_type_node), |
| 4813 |
max_limit (short_integer_type_node)); |
| 4814 |
ASSERT_TRUE (r0 == r1); |
| 4815 |
} |
| 4816 |
|
| 4817 |
// Casting NONZERO from a narrower signed to a wider signed. |
| 4818 |
// |
| 4819 |
// NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16]. |
| 4820 |
// Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16]. |
| 4821 |
r0.set_nonzero (short_integer_type_node); |
| 4822 |
range_cast (r0, integer_type_node); |
| 4823 |
r1 = int_range<1> (integer_type_node, INT (-32768), INT (-1)); |
| 4824 |
r2 = int_range<1> (integer_type_node, INT (1), INT (32767)); |
| 4825 |
r1.union_ (r2); |
| 4826 |
ASSERT_TRUE (r0 == r1); |
| 4827 |
} |
| 4828 |
|
| 4829 |
static void |
| 4830 |
range_op_lshift_tests () |
| 4831 |
{ |
| 4832 |
// Test that 0x808.... & 0x8.... still contains 0x8.... |
| 4833 |
// for a large set of numbers. |
| 4834 |
{ |
| 4835 |
int_range_max res; |
| 4836 |
tree big_type = long_long_unsigned_type_node; |
| 4837 |
unsigned big_prec = TYPE_PRECISION (big_type); |
| 4838 |
// big_num = 0x808,0000,0000,0000 |
| 4839 |
wide_int big_num = wi::lshift (wi::uhwi (0x808, big_prec), |
| 4840 |
wi::uhwi (48, big_prec)); |
| 4841 |
op_bitwise_and.fold_range (res, big_type, |
| 4842 |
int_range <1> (big_type), |
| 4843 |
int_range <1> (big_type, big_num, big_num)); |
| 4844 |
// val = 0x8,0000,0000,0000 |
| 4845 |
wide_int val = wi::lshift (wi::uhwi (8, big_prec), |
| 4846 |
wi::uhwi (48, big_prec)); |
| 4847 |
ASSERT_TRUE (res.contains_p (val)); |
| 4848 |
} |
| 4849 |
|
| 4850 |
if (TYPE_PRECISION (unsigned_type_node) > 31) |
| 4851 |
{ |
| 4852 |
// unsigned VARYING = op1 << 1 should be VARYING. |
| 4853 |
int_range<2> lhs (unsigned_type_node); |
| 4854 |
int_range<2> shift (unsigned_type_node, INT (1), INT (1)); |
| 4855 |
int_range_max op1; |
| 4856 |
op_lshift.op1_range (op1, unsigned_type_node, lhs, shift); |
| 4857 |
ASSERT_TRUE (op1.varying_p ()); |
| 4858 |
|
| 4859 |
// 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000]. |
| 4860 |
int_range<2> zero (unsigned_type_node, UINT (0), UINT (0)); |
| 4861 |
op_lshift.op1_range (op1, unsigned_type_node, zero, shift); |
| 4862 |
ASSERT_TRUE (op1.num_pairs () == 2); |
| 4863 |
// Remove the [0,0] range. |
| 4864 |
op1.intersect (zero); |
| 4865 |
ASSERT_TRUE (op1.num_pairs () == 1); |
| 4866 |
// op1 << 1 should be [0x8000,0x8000] << 1, |
| 4867 |
// which should result in [0,0]. |
| 4868 |
int_range_max result; |
| 4869 |
op_lshift.fold_range (result, unsigned_type_node, op1, shift); |
| 4870 |
ASSERT_TRUE (result == zero); |
| 4871 |
} |
| 4872 |
// signed VARYING = op1 << 1 should be VARYING. |
| 4873 |
if (TYPE_PRECISION (integer_type_node) > 31) |
| 4874 |
{ |
| 4875 |
// unsigned VARYING = op1 << 1 should be VARYING. |
| 4876 |
int_range<2> lhs (integer_type_node); |
| 4877 |
int_range<2> shift (integer_type_node, INT (1), INT (1)); |
| 4878 |
int_range_max op1; |
| 4879 |
op_lshift.op1_range (op1, integer_type_node, lhs, shift); |
| 4880 |
ASSERT_TRUE (op1.varying_p ()); |
| 4881 |
|
| 4882 |
// 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000]. |
| 4883 |
int_range<2> zero (integer_type_node, INT (0), INT (0)); |
| 4884 |
op_lshift.op1_range (op1, integer_type_node, zero, shift); |
| 4885 |
ASSERT_TRUE (op1.num_pairs () == 2); |
| 4886 |
// Remove the [0,0] range. |
| 4887 |
op1.intersect (zero); |
| 4888 |
ASSERT_TRUE (op1.num_pairs () == 1); |
| 4889 |
// op1 << 1 should be [0x8000,0x8000] << 1, |
| 4890 |
// which should result in [0,0]. |
| 4891 |
int_range_max result; |
| 4892 |
op_lshift.fold_range (result, unsigned_type_node, op1, shift); |
| 4893 |
ASSERT_TRUE (result == zero); |
| 4894 |
} |
| 4895 |
} |
| 4896 |
|
| 4897 |
static void |
| 4898 |
range_op_rshift_tests () |
| 4899 |
{ |
| 4900 |
// unsigned: [3, MAX] = OP1 >> 1 |
| 4901 |
{ |
| 4902 |
int_range_max lhs (unsigned_type_node, |
| 4903 |
UINT (3), max_limit (unsigned_type_node)); |
| 4904 |
int_range_max one (unsigned_type_node, |
| 4905 |
wi::one (TYPE_PRECISION (unsigned_type_node)), |
| 4906 |
wi::one (TYPE_PRECISION (unsigned_type_node))); |
| 4907 |
int_range_max op1; |
| 4908 |
op_rshift.op1_range (op1, unsigned_type_node, lhs, one); |
| 4909 |
ASSERT_FALSE (op1.contains_p (UINT (3))); |
| 4910 |
} |
| 4911 |
|
| 4912 |
// signed: [3, MAX] = OP1 >> 1 |
| 4913 |
{ |
| 4914 |
int_range_max lhs (integer_type_node, |
| 4915 |
INT (3), max_limit (integer_type_node)); |
| 4916 |
int_range_max one (integer_type_node, INT (1), INT (1)); |
| 4917 |
int_range_max op1; |
| 4918 |
op_rshift.op1_range (op1, integer_type_node, lhs, one); |
| 4919 |
ASSERT_FALSE (op1.contains_p (INT (-2))); |
| 4920 |
} |
| 4921 |
|
| 4922 |
// This is impossible, so OP1 should be []. |
| 4923 |
// signed: [MIN, MIN] = OP1 >> 1 |
| 4924 |
{ |
| 4925 |
int_range_max lhs (integer_type_node, |
| 4926 |
min_limit (integer_type_node), |
| 4927 |
min_limit (integer_type_node)); |
| 4928 |
int_range_max one (integer_type_node, INT (1), INT (1)); |
| 4929 |
int_range_max op1; |
| 4930 |
op_rshift.op1_range (op1, integer_type_node, lhs, one); |
| 4931 |
ASSERT_TRUE (op1.undefined_p ()); |
| 4932 |
} |
| 4933 |
|
| 4934 |
// signed: ~[-1] = OP1 >> 31 |
| 4935 |
if (TYPE_PRECISION (integer_type_node) > 31) |
| 4936 |
{ |
| 4937 |
int_range_max lhs (integer_type_node, INT (-1), INT (-1), VR_ANTI_RANGE); |
| 4938 |
int_range_max shift (integer_type_node, INT (31), INT (31)); |
| 4939 |
int_range_max op1; |
| 4940 |
op_rshift.op1_range (op1, integer_type_node, lhs, shift); |
| 4941 |
int_range_max negatives = range_negatives (integer_type_node); |
| 4942 |
negatives.intersect (op1); |
| 4943 |
ASSERT_TRUE (negatives.undefined_p ()); |
| 4944 |
} |
| 4945 |
} |
| 4946 |
|
| 4947 |
static void |
| 4948 |
range_op_bitwise_and_tests () |
| 4949 |
{ |
| 4950 |
int_range_max res; |
| 4951 |
wide_int min = min_limit (integer_type_node); |
| 4952 |
wide_int max = max_limit (integer_type_node); |
| 4953 |
wide_int tiny = wi::add (min, wi::one (TYPE_PRECISION (integer_type_node))); |
| 4954 |
int_range_max i1 (integer_type_node, tiny, max); |
| 4955 |
int_range_max i2 (integer_type_node, INT (255), INT (255)); |
| 4956 |
|
| 4957 |
// [MIN+1, MAX] = OP1 & 255: OP1 is VARYING |
| 4958 |
op_bitwise_and.op1_range (res, integer_type_node, i1, i2); |
| 4959 |
ASSERT_TRUE (res == int_range<1> (integer_type_node)); |
| 4960 |
|
| 4961 |
// VARYING = OP1 & 255: OP1 is VARYING |
| 4962 |
i1 = int_range<1> (integer_type_node); |
| 4963 |
op_bitwise_and.op1_range (res, integer_type_node, i1, i2); |
| 4964 |
ASSERT_TRUE (res == int_range<1> (integer_type_node)); |
| 4965 |
|
| 4966 |
// For 0 = x & MASK, x is ~MASK. |
| 4967 |
{ |
| 4968 |
int_range<2> zero (integer_type_node, INT (0), INT (0)); |
| 4969 |
int_range<2> mask = int_range<2> (integer_type_node, INT (7), INT (7)); |
| 4970 |
op_bitwise_and.op1_range (res, integer_type_node, zero, mask); |
| 4971 |
wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node)); |
| 4972 |
ASSERT_TRUE (res.get_nonzero_bits () == inv); |
| 4973 |
} |
| 4974 |
|
| 4975 |
// (NONZERO | X) is nonzero. |
| 4976 |
i1.set_nonzero (integer_type_node); |
| 4977 |
i2.set_varying (integer_type_node); |
| 4978 |
op_bitwise_or.fold_range (res, integer_type_node, i1, i2); |
| 4979 |
ASSERT_TRUE (res.nonzero_p ()); |
| 4980 |
|
| 4981 |
// (NEGATIVE | X) is nonzero. |
| 4982 |
i1 = int_range<1> (integer_type_node, INT (-5), INT (-3)); |
| 4983 |
i2.set_varying (integer_type_node); |
| 4984 |
op_bitwise_or.fold_range (res, integer_type_node, i1, i2); |
| 4985 |
ASSERT_FALSE (res.contains_p (INT (0))); |
| 4986 |
} |
| 4987 |
|
| 4988 |
static void |
| 4989 |
range_relational_tests () |
| 4990 |
{ |
| 4991 |
int_range<2> lhs (unsigned_char_type_node); |
| 4992 |
int_range<2> op1 (unsigned_char_type_node, UCHAR (8), UCHAR (10)); |
| 4993 |
int_range<2> op2 (unsigned_char_type_node, UCHAR (20), UCHAR (20)); |
| 4994 |
|
| 4995 |
// Never wrapping additions mean LHS > OP1. |
| 4996 |
relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING); |
| 4997 |
ASSERT_TRUE (code == VREL_GT); |
| 4998 |
|
| 4999 |
// Most wrapping additions mean nothing... |
| 5000 |
op1 = int_range<2> (unsigned_char_type_node, UCHAR (8), UCHAR (10)); |
| 5001 |
op2 = int_range<2> (unsigned_char_type_node, UCHAR (0), UCHAR (255)); |
| 5002 |
code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING); |
| 5003 |
ASSERT_TRUE (code == VREL_VARYING); |
| 5004 |
|
| 5005 |
// However, always wrapping additions mean LHS < OP1. |
| 5006 |
op1 = int_range<2> (unsigned_char_type_node, UCHAR (1), UCHAR (255)); |
| 5007 |
op2 = int_range<2> (unsigned_char_type_node, UCHAR (255), UCHAR (255)); |
| 5008 |
code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING); |
| 5009 |
ASSERT_TRUE (code == VREL_LT); |
| 5010 |
} |
| 5011 |
|
| 5012 |
void |
| 5013 |
range_op_tests () |
| 5014 |
{ |
| 5015 |
range_op_rshift_tests (); |
| 5016 |
range_op_lshift_tests (); |
| 5017 |
range_op_bitwise_and_tests (); |
| 5018 |
range_op_cast_tests (); |
| 5019 |
range_relational_tests (); |
| 5020 |
|
| 5021 |
extern void range_op_float_tests (); |
| 5022 |
range_op_float_tests (); |
| 5023 |
} |
| 5024 |
|
| 5025 |
} // namespace selftest |
| 5026 |
|
| 5027 |
#endif // CHECKING_P |