| 1 |
/* Global, SSA-based optimizations using mathematical identities. |
| 2 |
Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GCC. |
| 5 |
|
| 6 |
GCC is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
GCC is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 |
for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with GCC; see the file COPYING3. If not see |
| 18 |
<http://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
/* Currently, the only mini-pass in this file tries to CSE reciprocal |
| 21 |
operations. These are common in sequences such as this one: |
| 22 |
|
| 23 |
modulus = sqrt(x*x + y*y + z*z); |
| 24 |
x = x / modulus; |
| 25 |
y = y / modulus; |
| 26 |
z = z / modulus; |
| 27 |
|
| 28 |
that can be optimized to |
| 29 |
|
| 30 |
modulus = sqrt(x*x + y*y + z*z); |
| 31 |
rmodulus = 1.0 / modulus; |
| 32 |
x = x * rmodulus; |
| 33 |
y = y * rmodulus; |
| 34 |
z = z * rmodulus; |
| 35 |
|
| 36 |
We do this for loop invariant divisors, and with this pass whenever |
| 37 |
we notice that a division has the same divisor multiple times. |
| 38 |
|
| 39 |
Of course, like in PRE, we don't insert a division if a dominator |
| 40 |
already has one. However, this cannot be done as an extension of |
| 41 |
PRE for several reasons. |
| 42 |
|
| 43 |
First of all, with some experiments it was found out that the |
| 44 |
transformation is not always useful if there are only two divisions |
| 45 |
by the same divisor. This is probably because modern processors |
| 46 |
can pipeline the divisions; on older, in-order processors it should |
| 47 |
still be effective to optimize two divisions by the same number. |
| 48 |
We make this a param, and it shall be called N in the remainder of |
| 49 |
this comment. |
| 50 |
|
| 51 |
Second, if trapping math is active, we have less freedom on where |
| 52 |
to insert divisions: we can only do so in basic blocks that already |
| 53 |
contain one. (If divisions don't trap, instead, we can insert |
| 54 |
divisions elsewhere, which will be in blocks that are common dominators |
| 55 |
of those that have the division). |
| 56 |
|
| 57 |
We really don't want to compute the reciprocal unless a division will |
| 58 |
be found. To do this, we won't insert the division in a basic block |
| 59 |
that has less than N divisions *post-dominating* it. |
| 60 |
|
| 61 |
The algorithm constructs a subset of the dominator tree, holding the |
| 62 |
blocks containing the divisions and the common dominators to them, |
| 63 |
and walk it twice. The first walk is in post-order, and it annotates |
| 64 |
each block with the number of divisions that post-dominate it: this |
| 65 |
gives information on where divisions can be inserted profitably. |
| 66 |
The second walk is in pre-order, and it inserts divisions as explained |
| 67 |
above, and replaces divisions by multiplications. |
| 68 |
|
| 69 |
In the best case, the cost of the pass is O(n_statements). In the |
| 70 |
worst-case, the cost is due to creating the dominator tree subset, |
| 71 |
with a cost of O(n_basic_blocks ^ 2); however this can only happen |
| 72 |
for n_statements / n_basic_blocks statements. So, the amortized cost |
| 73 |
of creating the dominator tree subset is O(n_basic_blocks) and the |
| 74 |
worst-case cost of the pass is O(n_statements * n_basic_blocks). |
| 75 |
|
| 76 |
More practically, the cost will be small because there are few |
| 77 |
divisions, and they tend to be in the same basic block, so insert_bb |
| 78 |
is called very few times. |
| 79 |
|
| 80 |
If we did this using domwalk.cc, an efficient implementation would have |
| 81 |
to work on all the variables in a single pass, because we could not |
| 82 |
work on just a subset of the dominator tree, as we do now, and the |
| 83 |
cost would also be something like O(n_statements * n_basic_blocks). |
| 84 |
The data structures would be more complex in order to work on all the |
| 85 |
variables in a single pass. */ |
| 86 |
|
| 87 |
#include "config.h" |
| 88 |
#include "system.h" |
| 89 |
#include "coretypes.h" |
| 90 |
#include "backend.h" |
| 91 |
#include "target.h" |
| 92 |
#include "rtl.h" |
| 93 |
#include "tree.h" |
| 94 |
#include "gimple.h" |
| 95 |
#include "predict.h" |
| 96 |
#include "alloc-pool.h" |
| 97 |
#include "tree-pass.h" |
| 98 |
#include "ssa.h" |
| 99 |
#include "optabs-tree.h" |
| 100 |
#include "gimple-pretty-print.h" |
| 101 |
#include "alias.h" |
| 102 |
#include "fold-const.h" |
| 103 |
#include "gimple-iterator.h" |
| 104 |
#include "gimple-fold.h" |
| 105 |
#include "gimplify.h" |
| 106 |
#include "gimplify-me.h" |
| 107 |
#include "stor-layout.h" |
| 108 |
#include "tree-cfg.h" |
| 109 |
#include "tree-dfa.h" |
| 110 |
#include "tree-ssa.h" |
| 111 |
#include "builtins.h" |
| 112 |
#include "internal-fn.h" |
| 113 |
#include "case-cfn-macros.h" |
| 114 |
#include "optabs-libfuncs.h" |
| 115 |
#include "tree-eh.h" |
| 116 |
#include "targhooks.h" |
| 117 |
#include "domwalk.h" |
| 118 |
#include "tree-ssa-math-opts.h" |
| 119 |
#include "dbgcnt.h" |
| 120 |
#include "cfghooks.h" |
| 121 |
|
| 122 |
/* This structure represents one basic block that either computes a |
| 123 |
division, or is a common dominator for basic block that compute a |
| 124 |
division. */ |
| 125 |
struct occurrence { |
| 126 |
/* The basic block represented by this structure. */ |
| 127 |
basic_block bb = basic_block(); |
| 128 |
|
| 129 |
/* If non-NULL, the SSA_NAME holding the definition for a reciprocal |
| 130 |
inserted in BB. */ |
| 131 |
tree recip_def = tree(); |
| 132 |
|
| 133 |
/* If non-NULL, the SSA_NAME holding the definition for a squared |
| 134 |
reciprocal inserted in BB. */ |
| 135 |
tree square_recip_def = tree(); |
| 136 |
|
| 137 |
/* If non-NULL, the GIMPLE_ASSIGN for a reciprocal computation that |
| 138 |
was inserted in BB. */ |
| 139 |
gimple *recip_def_stmt = nullptr; |
| 140 |
|
| 141 |
/* Pointer to a list of "struct occurrence"s for blocks dominated |
| 142 |
by BB. */ |
| 143 |
struct occurrence *children = nullptr; |
| 144 |
|
| 145 |
/* Pointer to the next "struct occurrence"s in the list of blocks |
| 146 |
sharing a common dominator. */ |
| 147 |
struct occurrence *next = nullptr; |
| 148 |
|
| 149 |
/* The number of divisions that are in BB before compute_merit. The |
| 150 |
number of divisions that are in BB or post-dominate it after |
| 151 |
compute_merit. */ |
| 152 |
int num_divisions = 0; |
| 153 |
|
| 154 |
/* True if the basic block has a division, false if it is a common |
| 155 |
dominator for basic blocks that do. If it is false and trapping |
| 156 |
math is active, BB is not a candidate for inserting a reciprocal. */ |
| 157 |
bool bb_has_division = false; |
| 158 |
|
| 159 |
/* Construct a struct occurrence for basic block BB, and whose |
| 160 |
children list is headed by CHILDREN. */ |
| 161 |
occurrence (basic_block bb, struct occurrence *children) |
| 162 |
: bb (bb), children (children) |
| 163 |
{ |
| 164 |
bb->aux = this; |
| 165 |
} |
| 166 |
|
| 167 |
/* Destroy a struct occurrence and remove it from its basic block. */ |
| 168 |
~occurrence () |
| 169 |
{ |
| 170 |
bb->aux = nullptr; |
| 171 |
} |
| 172 |
|
| 173 |
/* Allocate memory for a struct occurrence from OCC_POOL. */ |
| 174 |
static void* operator new (size_t); |
| 175 |
|
| 176 |
/* Return memory for a struct occurrence to OCC_POOL. */ |
| 177 |
static void operator delete (void*, size_t); |
| 178 |
}; |
| 179 |
|
| 180 |
static struct |
| 181 |
{ |
| 182 |
/* Number of 1.0/X ops inserted. */ |
| 183 |
int rdivs_inserted; |
| 184 |
|
| 185 |
/* Number of 1.0/FUNC ops inserted. */ |
| 186 |
int rfuncs_inserted; |
| 187 |
} reciprocal_stats; |
| 188 |
|
| 189 |
static struct |
| 190 |
{ |
| 191 |
/* Number of cexpi calls inserted. */ |
| 192 |
int inserted; |
| 193 |
|
| 194 |
/* Number of conversions removed. */ |
| 195 |
int conv_removed; |
| 196 |
|
| 197 |
} sincos_stats; |
| 198 |
|
| 199 |
static struct |
| 200 |
{ |
| 201 |
/* Number of widening multiplication ops inserted. */ |
| 202 |
int widen_mults_inserted; |
| 203 |
|
| 204 |
/* Number of integer multiply-and-accumulate ops inserted. */ |
| 205 |
int maccs_inserted; |
| 206 |
|
| 207 |
/* Number of fp fused multiply-add ops inserted. */ |
| 208 |
int fmas_inserted; |
| 209 |
|
| 210 |
/* Number of divmod calls inserted. */ |
| 211 |
int divmod_calls_inserted; |
| 212 |
|
| 213 |
/* Number of highpart multiplication ops inserted. */ |
| 214 |
int highpart_mults_inserted; |
| 215 |
} widen_mul_stats; |
| 216 |
|
| 217 |
/* The instance of "struct occurrence" representing the highest |
| 218 |
interesting block in the dominator tree. */ |
| 219 |
static struct occurrence *occ_head; |
| 220 |
|
| 221 |
/* Allocation pool for getting instances of "struct occurrence". */ |
| 222 |
static object_allocator<occurrence> *occ_pool; |
| 223 |
|
| 224 |
void* occurrence::operator new (size_t n) |
| 225 |
{ |
| 226 |
gcc_assert (n == sizeof(occurrence)); |
| 227 |
return occ_pool->allocate_raw (); |
| 228 |
} |
| 229 |
|
| 230 |
void occurrence::operator delete (void *occ, size_t n) |
| 231 |
{ |
| 232 |
gcc_assert (n == sizeof(occurrence)); |
| 233 |
occ_pool->remove_raw (occ); |
| 234 |
} |
| 235 |
|
| 236 |
/* Insert NEW_OCC into our subset of the dominator tree. P_HEAD points to a |
| 237 |
list of "struct occurrence"s, one per basic block, having IDOM as |
| 238 |
their common dominator. |
| 239 |
|
| 240 |
We try to insert NEW_OCC as deep as possible in the tree, and we also |
| 241 |
insert any other block that is a common dominator for BB and one |
| 242 |
block already in the tree. */ |
| 243 |
|
| 244 |
static void |
| 245 |
insert_bb (struct occurrence *new_occ, basic_block idom, |
| 246 |
struct occurrence **p_head) |
| 247 |
{ |
| 248 |
struct occurrence *occ, **p_occ; |
| 249 |
|
| 250 |
for (p_occ = p_head; (occ = *p_occ) != NULL; ) |
| 251 |
{ |
| 252 |
basic_block bb = new_occ->bb, occ_bb = occ->bb; |
| 253 |
basic_block dom = nearest_common_dominator (CDI_DOMINATORS, occ_bb, bb); |
| 254 |
if (dom == bb) |
| 255 |
{ |
| 256 |
/* BB dominates OCC_BB. OCC becomes NEW_OCC's child: remove OCC |
| 257 |
from its list. */ |
| 258 |
*p_occ = occ->next; |
| 259 |
occ->next = new_occ->children; |
| 260 |
new_occ->children = occ; |
| 261 |
|
| 262 |
/* Try the next block (it may as well be dominated by BB). */ |
| 263 |
} |
| 264 |
|
| 265 |
else if (dom == occ_bb) |
| 266 |
{ |
| 267 |
/* OCC_BB dominates BB. Tail recurse to look deeper. */ |
| 268 |
insert_bb (new_occ, dom, &occ->children); |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
else if (dom != idom) |
| 273 |
{ |
| 274 |
gcc_assert (!dom->aux); |
| 275 |
|
| 276 |
/* There is a dominator between IDOM and BB, add it and make |
| 277 |
two children out of NEW_OCC and OCC. First, remove OCC from |
| 278 |
its list. */ |
| 279 |
*p_occ = occ->next; |
| 280 |
new_occ->next = occ; |
| 281 |
occ->next = NULL; |
| 282 |
|
| 283 |
/* None of the previous blocks has DOM as a dominator: if we tail |
| 284 |
recursed, we would reexamine them uselessly. Just switch BB with |
| 285 |
DOM, and go on looking for blocks dominated by DOM. */ |
| 286 |
new_occ = new occurrence (dom, new_occ); |
| 287 |
} |
| 288 |
|
| 289 |
else |
| 290 |
{ |
| 291 |
/* Nothing special, go on with the next element. */ |
| 292 |
p_occ = &occ->next; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
/* No place was found as a child of IDOM. Make BB a sibling of IDOM. */ |
| 297 |
new_occ->next = *p_head; |
| 298 |
*p_head = new_occ; |
| 299 |
} |
| 300 |
|
| 301 |
/* Register that we found a division in BB. |
| 302 |
IMPORTANCE is a measure of how much weighting to give |
| 303 |
that division. Use IMPORTANCE = 2 to register a single |
| 304 |
division. If the division is going to be found multiple |
| 305 |
times use 1 (as it is with squares). */ |
| 306 |
|
| 307 |
static inline void |
| 308 |
register_division_in (basic_block bb, int importance) |
| 309 |
{ |
| 310 |
struct occurrence *occ; |
| 311 |
|
| 312 |
occ = (struct occurrence *) bb->aux; |
| 313 |
if (!occ) |
| 314 |
{ |
| 315 |
occ = new occurrence (bb, NULL); |
| 316 |
insert_bb (occ, ENTRY_BLOCK_PTR_FOR_FN (cfun), &occ_head); |
| 317 |
} |
| 318 |
|
| 319 |
occ->bb_has_division = true; |
| 320 |
occ->num_divisions += importance; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
/* Compute the number of divisions that postdominate each block in OCC and |
| 325 |
its children. */ |
| 326 |
|
| 327 |
static void |
| 328 |
compute_merit (struct occurrence *occ) |
| 329 |
{ |
| 330 |
struct occurrence *occ_child; |
| 331 |
basic_block dom = occ->bb; |
| 332 |
|
| 333 |
for (occ_child = occ->children; occ_child; occ_child = occ_child->next) |
| 334 |
{ |
| 335 |
basic_block bb; |
| 336 |
if (occ_child->children) |
| 337 |
compute_merit (occ_child); |
| 338 |
|
| 339 |
if (flag_exceptions) |
| 340 |
bb = single_noncomplex_succ (dom); |
| 341 |
else |
| 342 |
bb = dom; |
| 343 |
|
| 344 |
if (dominated_by_p (CDI_POST_DOMINATORS, bb, occ_child->bb)) |
| 345 |
occ->num_divisions += occ_child->num_divisions; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
/* Return whether USE_STMT is a floating-point division by DEF. */ |
| 351 |
static inline bool |
| 352 |
is_division_by (gimple *use_stmt, tree def) |
| 353 |
{ |
| 354 |
return is_gimple_assign (use_stmt) |
| 355 |
&& gimple_assign_rhs_code (use_stmt) == RDIV_EXPR |
| 356 |
&& gimple_assign_rhs2 (use_stmt) == def |
| 357 |
/* Do not recognize x / x as valid division, as we are getting |
| 358 |
confused later by replacing all immediate uses x in such |
| 359 |
a stmt. */ |
| 360 |
&& gimple_assign_rhs1 (use_stmt) != def |
| 361 |
&& !stmt_can_throw_internal (cfun, use_stmt); |
| 362 |
} |
| 363 |
|
| 364 |
/* Return TRUE if USE_STMT is a multiplication of DEF by A. */ |
| 365 |
static inline bool |
| 366 |
is_mult_by (gimple *use_stmt, tree def, tree a) |
| 367 |
{ |
| 368 |
if (gimple_code (use_stmt) == GIMPLE_ASSIGN |
| 369 |
&& gimple_assign_rhs_code (use_stmt) == MULT_EXPR) |
| 370 |
{ |
| 371 |
tree op0 = gimple_assign_rhs1 (use_stmt); |
| 372 |
tree op1 = gimple_assign_rhs2 (use_stmt); |
| 373 |
|
| 374 |
return (op0 == def && op1 == a) |
| 375 |
|| (op0 == a && op1 == def); |
| 376 |
} |
| 377 |
return 0; |
| 378 |
} |
| 379 |
|
| 380 |
/* Return whether USE_STMT is DEF * DEF. */ |
| 381 |
static inline bool |
| 382 |
is_square_of (gimple *use_stmt, tree def) |
| 383 |
{ |
| 384 |
return is_mult_by (use_stmt, def, def); |
| 385 |
} |
| 386 |
|
| 387 |
/* Return whether USE_STMT is a floating-point division by |
| 388 |
DEF * DEF. */ |
| 389 |
static inline bool |
| 390 |
is_division_by_square (gimple *use_stmt, tree def) |
| 391 |
{ |
| 392 |
if (gimple_code (use_stmt) == GIMPLE_ASSIGN |
| 393 |
&& gimple_assign_rhs_code (use_stmt) == RDIV_EXPR |
| 394 |
&& gimple_assign_rhs1 (use_stmt) != gimple_assign_rhs2 (use_stmt) |
| 395 |
&& !stmt_can_throw_internal (cfun, use_stmt)) |
| 396 |
{ |
| 397 |
tree denominator = gimple_assign_rhs2 (use_stmt); |
| 398 |
if (TREE_CODE (denominator) == SSA_NAME) |
| 399 |
return is_square_of (SSA_NAME_DEF_STMT (denominator), def); |
| 400 |
} |
| 401 |
return 0; |
| 402 |
} |
| 403 |
|
| 404 |
/* Walk the subset of the dominator tree rooted at OCC, setting the |
| 405 |
RECIP_DEF field to a definition of 1.0 / DEF that can be used in |
| 406 |
the given basic block. The field may be left NULL, of course, |
| 407 |
if it is not possible or profitable to do the optimization. |
| 408 |
|
| 409 |
DEF_BSI is an iterator pointing at the statement defining DEF. |
| 410 |
If RECIP_DEF is set, a dominator already has a computation that can |
| 411 |
be used. |
| 412 |
|
| 413 |
If should_insert_square_recip is set, then this also inserts |
| 414 |
the square of the reciprocal immediately after the definition |
| 415 |
of the reciprocal. */ |
| 416 |
|
| 417 |
static void |
| 418 |
insert_reciprocals (gimple_stmt_iterator *def_gsi, struct occurrence *occ, |
| 419 |
tree def, tree recip_def, tree square_recip_def, |
| 420 |
int should_insert_square_recip, int threshold) |
| 421 |
{ |
| 422 |
tree type; |
| 423 |
gassign *new_stmt, *new_square_stmt; |
| 424 |
gimple_stmt_iterator gsi; |
| 425 |
struct occurrence *occ_child; |
| 426 |
|
| 427 |
if (!recip_def |
| 428 |
&& (occ->bb_has_division || !flag_trapping_math) |
| 429 |
/* Divide by two as all divisions are counted twice in |
| 430 |
the costing loop. */ |
| 431 |
&& occ->num_divisions / 2 >= threshold) |
| 432 |
{ |
| 433 |
/* Make a variable with the replacement and substitute it. */ |
| 434 |
type = TREE_TYPE (def); |
| 435 |
recip_def = create_tmp_reg (type, "reciptmp"); |
| 436 |
new_stmt = gimple_build_assign (recip_def, RDIV_EXPR, |
| 437 |
build_one_cst (type), def); |
| 438 |
|
| 439 |
if (should_insert_square_recip) |
| 440 |
{ |
| 441 |
square_recip_def = create_tmp_reg (type, "powmult_reciptmp"); |
| 442 |
new_square_stmt = gimple_build_assign (square_recip_def, MULT_EXPR, |
| 443 |
recip_def, recip_def); |
| 444 |
} |
| 445 |
|
| 446 |
if (occ->bb_has_division) |
| 447 |
{ |
| 448 |
/* Case 1: insert before an existing division. */ |
| 449 |
gsi = gsi_after_labels (occ->bb); |
| 450 |
while (!gsi_end_p (gsi) |
| 451 |
&& (!is_division_by (gsi_stmt (gsi), def)) |
| 452 |
&& (!is_division_by_square (gsi_stmt (gsi), def))) |
| 453 |
gsi_next (&gsi); |
| 454 |
|
| 455 |
gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT); |
| 456 |
if (should_insert_square_recip) |
| 457 |
gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT); |
| 458 |
} |
| 459 |
else if (def_gsi && occ->bb == gsi_bb (*def_gsi)) |
| 460 |
{ |
| 461 |
/* Case 2: insert right after the definition. Note that this will |
| 462 |
never happen if the definition statement can throw, because in |
| 463 |
that case the sole successor of the statement's basic block will |
| 464 |
dominate all the uses as well. */ |
| 465 |
gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT); |
| 466 |
if (should_insert_square_recip) |
| 467 |
gsi_insert_after (def_gsi, new_square_stmt, GSI_NEW_STMT); |
| 468 |
} |
| 469 |
else |
| 470 |
{ |
| 471 |
/* Case 3: insert in a basic block not containing defs/uses. */ |
| 472 |
gsi = gsi_after_labels (occ->bb); |
| 473 |
gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT); |
| 474 |
if (should_insert_square_recip) |
| 475 |
gsi_insert_before (&gsi, new_square_stmt, GSI_SAME_STMT); |
| 476 |
} |
| 477 |
|
| 478 |
reciprocal_stats.rdivs_inserted++; |
| 479 |
|
| 480 |
occ->recip_def_stmt = new_stmt; |
| 481 |
} |
| 482 |
|
| 483 |
occ->recip_def = recip_def; |
| 484 |
occ->square_recip_def = square_recip_def; |
| 485 |
for (occ_child = occ->children; occ_child; occ_child = occ_child->next) |
| 486 |
insert_reciprocals (def_gsi, occ_child, def, recip_def, |
| 487 |
square_recip_def, should_insert_square_recip, |
| 488 |
threshold); |
| 489 |
} |
| 490 |
|
| 491 |
/* Replace occurrences of expr / (x * x) with expr * ((1 / x) * (1 / x)). |
| 492 |
Take as argument the use for (x * x). */ |
| 493 |
static inline void |
| 494 |
replace_reciprocal_squares (use_operand_p use_p) |
| 495 |
{ |
| 496 |
gimple *use_stmt = USE_STMT (use_p); |
| 497 |
basic_block bb = gimple_bb (use_stmt); |
| 498 |
struct occurrence *occ = (struct occurrence *) bb->aux; |
| 499 |
|
| 500 |
if (optimize_bb_for_speed_p (bb) && occ->square_recip_def |
| 501 |
&& occ->recip_def) |
| 502 |
{ |
| 503 |
gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
| 504 |
gimple_assign_set_rhs_code (use_stmt, MULT_EXPR); |
| 505 |
gimple_assign_set_rhs2 (use_stmt, occ->square_recip_def); |
| 506 |
SET_USE (use_p, occ->square_recip_def); |
| 507 |
fold_stmt_inplace (&gsi); |
| 508 |
update_stmt (use_stmt); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
/* Replace the division at USE_P with a multiplication by the reciprocal, if |
| 514 |
possible. */ |
| 515 |
|
| 516 |
static inline void |
| 517 |
replace_reciprocal (use_operand_p use_p) |
| 518 |
{ |
| 519 |
gimple *use_stmt = USE_STMT (use_p); |
| 520 |
basic_block bb = gimple_bb (use_stmt); |
| 521 |
struct occurrence *occ = (struct occurrence *) bb->aux; |
| 522 |
|
| 523 |
if (optimize_bb_for_speed_p (bb) |
| 524 |
&& occ->recip_def && use_stmt != occ->recip_def_stmt) |
| 525 |
{ |
| 526 |
gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
| 527 |
gimple_assign_set_rhs_code (use_stmt, MULT_EXPR); |
| 528 |
SET_USE (use_p, occ->recip_def); |
| 529 |
fold_stmt_inplace (&gsi); |
| 530 |
update_stmt (use_stmt); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
/* Free OCC and return one more "struct occurrence" to be freed. */ |
| 536 |
|
| 537 |
static struct occurrence * |
| 538 |
free_bb (struct occurrence *occ) |
| 539 |
{ |
| 540 |
struct occurrence *child, *next; |
| 541 |
|
| 542 |
/* First get the two pointers hanging off OCC. */ |
| 543 |
next = occ->next; |
| 544 |
child = occ->children; |
| 545 |
delete occ; |
| 546 |
|
| 547 |
/* Now ensure that we don't recurse unless it is necessary. */ |
| 548 |
if (!child) |
| 549 |
return next; |
| 550 |
else |
| 551 |
{ |
| 552 |
while (next) |
| 553 |
next = free_bb (next); |
| 554 |
|
| 555 |
return child; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
/* Transform sequences like |
| 560 |
t = sqrt (a) |
| 561 |
x = 1.0 / t; |
| 562 |
r1 = x * x; |
| 563 |
r2 = a * x; |
| 564 |
into: |
| 565 |
t = sqrt (a) |
| 566 |
r1 = 1.0 / a; |
| 567 |
r2 = t; |
| 568 |
x = r1 * r2; |
| 569 |
depending on the uses of x, r1, r2. This removes one multiplication and |
| 570 |
allows the sqrt and division operations to execute in parallel. |
| 571 |
DEF_GSI is the gsi of the initial division by sqrt that defines |
| 572 |
DEF (x in the example above). */ |
| 573 |
|
| 574 |
static void |
| 575 |
optimize_recip_sqrt (gimple_stmt_iterator *def_gsi, tree def) |
| 576 |
{ |
| 577 |
gimple *use_stmt; |
| 578 |
imm_use_iterator use_iter; |
| 579 |
gimple *stmt = gsi_stmt (*def_gsi); |
| 580 |
tree x = def; |
| 581 |
tree orig_sqrt_ssa_name = gimple_assign_rhs2 (stmt); |
| 582 |
tree div_rhs1 = gimple_assign_rhs1 (stmt); |
| 583 |
|
| 584 |
if (TREE_CODE (orig_sqrt_ssa_name) != SSA_NAME |
| 585 |
|| TREE_CODE (div_rhs1) != REAL_CST |
| 586 |
|| !real_equal (&TREE_REAL_CST (div_rhs1), &dconst1)) |
| 587 |
return; |
| 588 |
|
| 589 |
gcall *sqrt_stmt |
| 590 |
= dyn_cast <gcall *> (SSA_NAME_DEF_STMT (orig_sqrt_ssa_name)); |
| 591 |
|
| 592 |
if (!sqrt_stmt || !gimple_call_lhs (sqrt_stmt)) |
| 593 |
return; |
| 594 |
|
| 595 |
switch (gimple_call_combined_fn (sqrt_stmt)) |
| 596 |
{ |
| 597 |
CASE_CFN_SQRT: |
| 598 |
CASE_CFN_SQRT_FN: |
| 599 |
break; |
| 600 |
|
| 601 |
default: |
| 602 |
return; |
| 603 |
} |
| 604 |
tree a = gimple_call_arg (sqrt_stmt, 0); |
| 605 |
|
| 606 |
/* We have 'a' and 'x'. Now analyze the uses of 'x'. */ |
| 607 |
|
| 608 |
/* Statements that use x in x * x. */ |
| 609 |
auto_vec<gimple *> sqr_stmts; |
| 610 |
/* Statements that use x in a * x. */ |
| 611 |
auto_vec<gimple *> mult_stmts; |
| 612 |
bool has_other_use = false; |
| 613 |
bool mult_on_main_path = false; |
| 614 |
|
| 615 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, x) |
| 616 |
{ |
| 617 |
if (is_gimple_debug (use_stmt)) |
| 618 |
continue; |
| 619 |
if (is_square_of (use_stmt, x)) |
| 620 |
{ |
| 621 |
sqr_stmts.safe_push (use_stmt); |
| 622 |
if (gimple_bb (use_stmt) == gimple_bb (stmt)) |
| 623 |
mult_on_main_path = true; |
| 624 |
} |
| 625 |
else if (is_mult_by (use_stmt, x, a)) |
| 626 |
{ |
| 627 |
mult_stmts.safe_push (use_stmt); |
| 628 |
if (gimple_bb (use_stmt) == gimple_bb (stmt)) |
| 629 |
mult_on_main_path = true; |
| 630 |
} |
| 631 |
else |
| 632 |
has_other_use = true; |
| 633 |
} |
| 634 |
|
| 635 |
/* In the x * x and a * x cases we just rewire stmt operands or |
| 636 |
remove multiplications. In the has_other_use case we introduce |
| 637 |
a multiplication so make sure we don't introduce a multiplication |
| 638 |
on a path where there was none. */ |
| 639 |
if (has_other_use && !mult_on_main_path) |
| 640 |
return; |
| 641 |
|
| 642 |
if (sqr_stmts.is_empty () && mult_stmts.is_empty ()) |
| 643 |
return; |
| 644 |
|
| 645 |
/* If x = 1.0 / sqrt (a) has uses other than those optimized here we want |
| 646 |
to be able to compose it from the sqr and mult cases. */ |
| 647 |
if (has_other_use && (sqr_stmts.is_empty () || mult_stmts.is_empty ())) |
| 648 |
return; |
| 649 |
|
| 650 |
if (dump_file) |
| 651 |
{ |
| 652 |
fprintf (dump_file, "Optimizing reciprocal sqrt multiplications of\n"); |
| 653 |
print_gimple_stmt (dump_file, sqrt_stmt, 0, TDF_NONE); |
| 654 |
print_gimple_stmt (dump_file, stmt, 0, TDF_NONE); |
| 655 |
fprintf (dump_file, "\n"); |
| 656 |
} |
| 657 |
|
| 658 |
bool delete_div = !has_other_use; |
| 659 |
tree sqr_ssa_name = NULL_TREE; |
| 660 |
if (!sqr_stmts.is_empty ()) |
| 661 |
{ |
| 662 |
/* r1 = x * x. Transform the original |
| 663 |
x = 1.0 / t |
| 664 |
into |
| 665 |
tmp1 = 1.0 / a |
| 666 |
r1 = tmp1. */ |
| 667 |
|
| 668 |
sqr_ssa_name |
| 669 |
= make_temp_ssa_name (TREE_TYPE (a), NULL, "recip_sqrt_sqr"); |
| 670 |
|
| 671 |
if (dump_file) |
| 672 |
{ |
| 673 |
fprintf (dump_file, "Replacing original division\n"); |
| 674 |
print_gimple_stmt (dump_file, stmt, 0, TDF_NONE); |
| 675 |
fprintf (dump_file, "with new division\n"); |
| 676 |
} |
| 677 |
stmt |
| 678 |
= gimple_build_assign (sqr_ssa_name, gimple_assign_rhs_code (stmt), |
| 679 |
gimple_assign_rhs1 (stmt), a); |
| 680 |
gsi_insert_before (def_gsi, stmt, GSI_SAME_STMT); |
| 681 |
gsi_remove (def_gsi, true); |
| 682 |
*def_gsi = gsi_for_stmt (stmt); |
| 683 |
fold_stmt_inplace (def_gsi); |
| 684 |
update_stmt (stmt); |
| 685 |
|
| 686 |
if (dump_file) |
| 687 |
print_gimple_stmt (dump_file, stmt, 0, TDF_NONE); |
| 688 |
|
| 689 |
delete_div = false; |
| 690 |
gimple *sqr_stmt; |
| 691 |
unsigned int i; |
| 692 |
FOR_EACH_VEC_ELT (sqr_stmts, i, sqr_stmt) |
| 693 |
{ |
| 694 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (sqr_stmt); |
| 695 |
gimple_assign_set_rhs_from_tree (&gsi2, sqr_ssa_name); |
| 696 |
update_stmt (sqr_stmt); |
| 697 |
} |
| 698 |
} |
| 699 |
if (!mult_stmts.is_empty ()) |
| 700 |
{ |
| 701 |
/* r2 = a * x. Transform this into: |
| 702 |
r2 = t (The original sqrt (a)). */ |
| 703 |
unsigned int i; |
| 704 |
gimple *mult_stmt = NULL; |
| 705 |
FOR_EACH_VEC_ELT (mult_stmts, i, mult_stmt) |
| 706 |
{ |
| 707 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (mult_stmt); |
| 708 |
|
| 709 |
if (dump_file) |
| 710 |
{ |
| 711 |
fprintf (dump_file, "Replacing squaring multiplication\n"); |
| 712 |
print_gimple_stmt (dump_file, mult_stmt, 0, TDF_NONE); |
| 713 |
fprintf (dump_file, "with assignment\n"); |
| 714 |
} |
| 715 |
gimple_assign_set_rhs_from_tree (&gsi2, orig_sqrt_ssa_name); |
| 716 |
fold_stmt_inplace (&gsi2); |
| 717 |
update_stmt (mult_stmt); |
| 718 |
if (dump_file) |
| 719 |
print_gimple_stmt (dump_file, mult_stmt, 0, TDF_NONE); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
if (has_other_use) |
| 724 |
{ |
| 725 |
/* Using the two temporaries tmp1, tmp2 from above |
| 726 |
the original x is now: |
| 727 |
x = tmp1 * tmp2. */ |
| 728 |
gcc_assert (orig_sqrt_ssa_name); |
| 729 |
gcc_assert (sqr_ssa_name); |
| 730 |
|
| 731 |
gimple *new_stmt |
| 732 |
= gimple_build_assign (x, MULT_EXPR, |
| 733 |
orig_sqrt_ssa_name, sqr_ssa_name); |
| 734 |
gsi_insert_after (def_gsi, new_stmt, GSI_NEW_STMT); |
| 735 |
update_stmt (stmt); |
| 736 |
} |
| 737 |
else if (delete_div) |
| 738 |
{ |
| 739 |
/* Remove the original division. */ |
| 740 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (stmt); |
| 741 |
gsi_remove (&gsi2, true); |
| 742 |
release_defs (stmt); |
| 743 |
} |
| 744 |
else |
| 745 |
release_ssa_name (x); |
| 746 |
} |
| 747 |
|
| 748 |
/* Look for floating-point divisions among DEF's uses, and try to |
| 749 |
replace them by multiplications with the reciprocal. Add |
| 750 |
as many statements computing the reciprocal as needed. |
| 751 |
|
| 752 |
DEF must be a GIMPLE register of a floating-point type. */ |
| 753 |
|
| 754 |
static void |
| 755 |
execute_cse_reciprocals_1 (gimple_stmt_iterator *def_gsi, tree def) |
| 756 |
{ |
| 757 |
use_operand_p use_p, square_use_p; |
| 758 |
imm_use_iterator use_iter, square_use_iter; |
| 759 |
tree square_def; |
| 760 |
struct occurrence *occ; |
| 761 |
int count = 0; |
| 762 |
int threshold; |
| 763 |
int square_recip_count = 0; |
| 764 |
int sqrt_recip_count = 0; |
| 765 |
|
| 766 |
gcc_assert (FLOAT_TYPE_P (TREE_TYPE (def)) && TREE_CODE (def) == SSA_NAME); |
| 767 |
threshold = targetm.min_divisions_for_recip_mul (TYPE_MODE (TREE_TYPE (def))); |
| 768 |
|
| 769 |
/* If DEF is a square (x * x), count the number of divisions by x. |
| 770 |
If there are more divisions by x than by (DEF * DEF), prefer to optimize |
| 771 |
the reciprocal of x instead of DEF. This improves cases like: |
| 772 |
def = x * x |
| 773 |
t0 = a / def |
| 774 |
t1 = b / def |
| 775 |
t2 = c / x |
| 776 |
Reciprocal optimization of x results in 1 division rather than 2 or 3. */ |
| 777 |
gimple *def_stmt = SSA_NAME_DEF_STMT (def); |
| 778 |
|
| 779 |
if (is_gimple_assign (def_stmt) |
| 780 |
&& gimple_assign_rhs_code (def_stmt) == MULT_EXPR |
| 781 |
&& TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME |
| 782 |
&& gimple_assign_rhs1 (def_stmt) == gimple_assign_rhs2 (def_stmt)) |
| 783 |
{ |
| 784 |
tree op0 = gimple_assign_rhs1 (def_stmt); |
| 785 |
|
| 786 |
FOR_EACH_IMM_USE_FAST (use_p, use_iter, op0) |
| 787 |
{ |
| 788 |
gimple *use_stmt = USE_STMT (use_p); |
| 789 |
if (is_division_by (use_stmt, op0)) |
| 790 |
sqrt_recip_count++; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
FOR_EACH_IMM_USE_FAST (use_p, use_iter, def) |
| 795 |
{ |
| 796 |
gimple *use_stmt = USE_STMT (use_p); |
| 797 |
if (is_division_by (use_stmt, def)) |
| 798 |
{ |
| 799 |
register_division_in (gimple_bb (use_stmt), 2); |
| 800 |
count++; |
| 801 |
} |
| 802 |
|
| 803 |
if (is_square_of (use_stmt, def)) |
| 804 |
{ |
| 805 |
square_def = gimple_assign_lhs (use_stmt); |
| 806 |
FOR_EACH_IMM_USE_FAST (square_use_p, square_use_iter, square_def) |
| 807 |
{ |
| 808 |
gimple *square_use_stmt = USE_STMT (square_use_p); |
| 809 |
if (is_division_by (square_use_stmt, square_def)) |
| 810 |
{ |
| 811 |
/* This is executed twice for each division by a square. */ |
| 812 |
register_division_in (gimple_bb (square_use_stmt), 1); |
| 813 |
square_recip_count++; |
| 814 |
} |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
/* Square reciprocals were counted twice above. */ |
| 820 |
square_recip_count /= 2; |
| 821 |
|
| 822 |
/* If it is more profitable to optimize 1 / x, don't optimize 1 / (x * x). */ |
| 823 |
if (sqrt_recip_count > square_recip_count) |
| 824 |
goto out; |
| 825 |
|
| 826 |
/* Do the expensive part only if we can hope to optimize something. */ |
| 827 |
if (count + square_recip_count >= threshold && count >= 1) |
| 828 |
{ |
| 829 |
gimple *use_stmt; |
| 830 |
for (occ = occ_head; occ; occ = occ->next) |
| 831 |
{ |
| 832 |
compute_merit (occ); |
| 833 |
insert_reciprocals (def_gsi, occ, def, NULL, NULL, |
| 834 |
square_recip_count, threshold); |
| 835 |
} |
| 836 |
|
| 837 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def) |
| 838 |
{ |
| 839 |
if (is_division_by (use_stmt, def)) |
| 840 |
{ |
| 841 |
FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter) |
| 842 |
replace_reciprocal (use_p); |
| 843 |
} |
| 844 |
else if (square_recip_count > 0 && is_square_of (use_stmt, def)) |
| 845 |
{ |
| 846 |
FOR_EACH_IMM_USE_ON_STMT (use_p, use_iter) |
| 847 |
{ |
| 848 |
/* Find all uses of the square that are divisions and |
| 849 |
* replace them by multiplications with the inverse. */ |
| 850 |
imm_use_iterator square_iterator; |
| 851 |
gimple *powmult_use_stmt = USE_STMT (use_p); |
| 852 |
tree powmult_def_name = gimple_assign_lhs (powmult_use_stmt); |
| 853 |
|
| 854 |
FOR_EACH_IMM_USE_STMT (powmult_use_stmt, |
| 855 |
square_iterator, powmult_def_name) |
| 856 |
FOR_EACH_IMM_USE_ON_STMT (square_use_p, square_iterator) |
| 857 |
{ |
| 858 |
gimple *powmult_use_stmt = USE_STMT (square_use_p); |
| 859 |
if (is_division_by (powmult_use_stmt, powmult_def_name)) |
| 860 |
replace_reciprocal_squares (square_use_p); |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
out: |
| 868 |
for (occ = occ_head; occ; ) |
| 869 |
occ = free_bb (occ); |
| 870 |
|
| 871 |
occ_head = NULL; |
| 872 |
} |
| 873 |
|
| 874 |
/* Return an internal function that implements the reciprocal of CALL, |
| 875 |
or IFN_LAST if there is no such function that the target supports. */ |
| 876 |
|
| 877 |
internal_fn |
| 878 |
internal_fn_reciprocal (gcall *call) |
| 879 |
{ |
| 880 |
internal_fn ifn; |
| 881 |
|
| 882 |
switch (gimple_call_combined_fn (call)) |
| 883 |
{ |
| 884 |
CASE_CFN_SQRT: |
| 885 |
CASE_CFN_SQRT_FN: |
| 886 |
ifn = IFN_RSQRT; |
| 887 |
break; |
| 888 |
|
| 889 |
default: |
| 890 |
return IFN_LAST; |
| 891 |
} |
| 892 |
|
| 893 |
tree_pair types = direct_internal_fn_types (ifn, call); |
| 894 |
if (!direct_internal_fn_supported_p (ifn, types, OPTIMIZE_FOR_SPEED)) |
| 895 |
return IFN_LAST; |
| 896 |
|
| 897 |
return ifn; |
| 898 |
} |
| 899 |
|
| 900 |
/* Go through all the floating-point SSA_NAMEs, and call |
| 901 |
execute_cse_reciprocals_1 on each of them. */ |
| 902 |
namespace { |
| 903 |
|
| 904 |
const pass_data pass_data_cse_reciprocals = |
| 905 |
{ |
| 906 |
GIMPLE_PASS, /* type */ |
| 907 |
"recip", /* name */ |
| 908 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 909 |
TV_TREE_RECIP, /* tv_id */ |
| 910 |
PROP_ssa, /* properties_required */ |
| 911 |
0, /* properties_provided */ |
| 912 |
0, /* properties_destroyed */ |
| 913 |
0, /* todo_flags_start */ |
| 914 |
TODO_update_ssa, /* todo_flags_finish */ |
| 915 |
}; |
| 916 |
|
| 917 |
class pass_cse_reciprocals : public gimple_opt_pass |
| 918 |
{ |
| 919 |
public: |
| 920 |
pass_cse_reciprocals (gcc::context *ctxt) |
| 921 |
: gimple_opt_pass (pass_data_cse_reciprocals, ctxt) |
| 922 |
{} |
| 923 |
|
| 924 |
/* opt_pass methods: */ |
| 925 |
bool gate (function *) final override |
| 926 |
{ |
| 927 |
return optimize && flag_reciprocal_math; |
| 928 |
} |
| 929 |
unsigned int execute (function *) final override; |
| 930 |
|
| 931 |
}; // class pass_cse_reciprocals |
| 932 |
|
| 933 |
unsigned int |
| 934 |
pass_cse_reciprocals::execute (function *fun) |
| 935 |
{ |
| 936 |
basic_block bb; |
| 937 |
tree arg; |
| 938 |
|
| 939 |
occ_pool = new object_allocator<occurrence> ("dominators for recip"); |
| 940 |
|
| 941 |
memset (&reciprocal_stats, 0, sizeof (reciprocal_stats)); |
| 942 |
calculate_dominance_info (CDI_DOMINATORS); |
| 943 |
calculate_dominance_info (CDI_POST_DOMINATORS); |
| 944 |
|
| 945 |
if (flag_checking) |
| 946 |
FOR_EACH_BB_FN (bb, fun) |
| 947 |
gcc_assert (!bb->aux); |
| 948 |
|
| 949 |
for (arg = DECL_ARGUMENTS (fun->decl); arg; arg = DECL_CHAIN (arg)) |
| 950 |
if (FLOAT_TYPE_P (TREE_TYPE (arg)) |
| 951 |
&& is_gimple_reg (arg)) |
| 952 |
{ |
| 953 |
tree name = ssa_default_def (fun, arg); |
| 954 |
if (name) |
| 955 |
execute_cse_reciprocals_1 (NULL, name); |
| 956 |
} |
| 957 |
|
| 958 |
FOR_EACH_BB_FN (bb, fun) |
| 959 |
{ |
| 960 |
tree def; |
| 961 |
|
| 962 |
for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
| 963 |
gsi_next (&gsi)) |
| 964 |
{ |
| 965 |
gphi *phi = gsi.phi (); |
| 966 |
def = PHI_RESULT (phi); |
| 967 |
if (! virtual_operand_p (def) |
| 968 |
&& FLOAT_TYPE_P (TREE_TYPE (def))) |
| 969 |
execute_cse_reciprocals_1 (NULL, def); |
| 970 |
} |
| 971 |
|
| 972 |
for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi); |
| 973 |
gsi_next (&gsi)) |
| 974 |
{ |
| 975 |
gimple *stmt = gsi_stmt (gsi); |
| 976 |
|
| 977 |
if (gimple_has_lhs (stmt) |
| 978 |
&& (def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF)) != NULL |
| 979 |
&& FLOAT_TYPE_P (TREE_TYPE (def)) |
| 980 |
&& TREE_CODE (def) == SSA_NAME) |
| 981 |
{ |
| 982 |
execute_cse_reciprocals_1 (&gsi, def); |
| 983 |
stmt = gsi_stmt (gsi); |
| 984 |
if (flag_unsafe_math_optimizations |
| 985 |
&& is_gimple_assign (stmt) |
| 986 |
&& gimple_assign_lhs (stmt) == def |
| 987 |
&& !stmt_can_throw_internal (cfun, stmt) |
| 988 |
&& gimple_assign_rhs_code (stmt) == RDIV_EXPR) |
| 989 |
optimize_recip_sqrt (&gsi, def); |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
if (optimize_bb_for_size_p (bb)) |
| 994 |
continue; |
| 995 |
|
| 996 |
/* Scan for a/func(b) and convert it to reciprocal a*rfunc(b). */ |
| 997 |
for (gimple_stmt_iterator gsi = gsi_after_labels (bb); !gsi_end_p (gsi); |
| 998 |
gsi_next (&gsi)) |
| 999 |
{ |
| 1000 |
gimple *stmt = gsi_stmt (gsi); |
| 1001 |
|
| 1002 |
if (is_gimple_assign (stmt) |
| 1003 |
&& gimple_assign_rhs_code (stmt) == RDIV_EXPR) |
| 1004 |
{ |
| 1005 |
tree arg1 = gimple_assign_rhs2 (stmt); |
| 1006 |
gimple *stmt1; |
| 1007 |
|
| 1008 |
if (TREE_CODE (arg1) != SSA_NAME) |
| 1009 |
continue; |
| 1010 |
|
| 1011 |
stmt1 = SSA_NAME_DEF_STMT (arg1); |
| 1012 |
|
| 1013 |
if (is_gimple_call (stmt1) |
| 1014 |
&& gimple_call_lhs (stmt1)) |
| 1015 |
{ |
| 1016 |
bool fail; |
| 1017 |
imm_use_iterator ui; |
| 1018 |
use_operand_p use_p; |
| 1019 |
tree fndecl = NULL_TREE; |
| 1020 |
|
| 1021 |
gcall *call = as_a <gcall *> (stmt1); |
| 1022 |
internal_fn ifn = internal_fn_reciprocal (call); |
| 1023 |
if (ifn == IFN_LAST) |
| 1024 |
{ |
| 1025 |
fndecl = gimple_call_fndecl (call); |
| 1026 |
if (!fndecl |
| 1027 |
|| !fndecl_built_in_p (fndecl, BUILT_IN_MD)) |
| 1028 |
continue; |
| 1029 |
fndecl = targetm.builtin_reciprocal (fndecl); |
| 1030 |
if (!fndecl) |
| 1031 |
continue; |
| 1032 |
} |
| 1033 |
|
| 1034 |
/* Check that all uses of the SSA name are divisions, |
| 1035 |
otherwise replacing the defining statement will do |
| 1036 |
the wrong thing. */ |
| 1037 |
fail = false; |
| 1038 |
FOR_EACH_IMM_USE_FAST (use_p, ui, arg1) |
| 1039 |
{ |
| 1040 |
gimple *stmt2 = USE_STMT (use_p); |
| 1041 |
if (is_gimple_debug (stmt2)) |
| 1042 |
continue; |
| 1043 |
if (!is_gimple_assign (stmt2) |
| 1044 |
|| gimple_assign_rhs_code (stmt2) != RDIV_EXPR |
| 1045 |
|| gimple_assign_rhs1 (stmt2) == arg1 |
| 1046 |
|| gimple_assign_rhs2 (stmt2) != arg1) |
| 1047 |
{ |
| 1048 |
fail = true; |
| 1049 |
break; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
if (fail) |
| 1053 |
continue; |
| 1054 |
|
| 1055 |
gimple_replace_ssa_lhs (call, arg1); |
| 1056 |
reset_flow_sensitive_info (arg1); |
| 1057 |
if (gimple_call_internal_p (call) != (ifn != IFN_LAST)) |
| 1058 |
{ |
| 1059 |
auto_vec<tree, 4> args; |
| 1060 |
for (unsigned int i = 0; |
| 1061 |
i < gimple_call_num_args (call); i++) |
| 1062 |
args.safe_push (gimple_call_arg (call, i)); |
| 1063 |
gcall *stmt2; |
| 1064 |
if (ifn == IFN_LAST) |
| 1065 |
stmt2 = gimple_build_call_vec (fndecl, args); |
| 1066 |
else |
| 1067 |
stmt2 = gimple_build_call_internal_vec (ifn, args); |
| 1068 |
gimple_call_set_lhs (stmt2, arg1); |
| 1069 |
gimple_move_vops (stmt2, call); |
| 1070 |
gimple_call_set_nothrow (stmt2, |
| 1071 |
gimple_call_nothrow_p (call)); |
| 1072 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (call); |
| 1073 |
gsi_replace (&gsi2, stmt2, true); |
| 1074 |
} |
| 1075 |
else |
| 1076 |
{ |
| 1077 |
if (ifn == IFN_LAST) |
| 1078 |
gimple_call_set_fndecl (call, fndecl); |
| 1079 |
else |
| 1080 |
gimple_call_set_internal_fn (call, ifn); |
| 1081 |
update_stmt (call); |
| 1082 |
} |
| 1083 |
reciprocal_stats.rfuncs_inserted++; |
| 1084 |
|
| 1085 |
FOR_EACH_IMM_USE_STMT (stmt, ui, arg1) |
| 1086 |
{ |
| 1087 |
gimple_stmt_iterator gsi = gsi_for_stmt (stmt); |
| 1088 |
gimple_assign_set_rhs_code (stmt, MULT_EXPR); |
| 1089 |
fold_stmt_inplace (&gsi); |
| 1090 |
update_stmt (stmt); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
statistics_counter_event (fun, "reciprocal divs inserted", |
| 1098 |
reciprocal_stats.rdivs_inserted); |
| 1099 |
statistics_counter_event (fun, "reciprocal functions inserted", |
| 1100 |
reciprocal_stats.rfuncs_inserted); |
| 1101 |
|
| 1102 |
free_dominance_info (CDI_DOMINATORS); |
| 1103 |
free_dominance_info (CDI_POST_DOMINATORS); |
| 1104 |
delete occ_pool; |
| 1105 |
return 0; |
| 1106 |
} |
| 1107 |
|
| 1108 |
} // anon namespace |
| 1109 |
|
| 1110 |
gimple_opt_pass * |
| 1111 |
make_pass_cse_reciprocals (gcc::context *ctxt) |
| 1112 |
{ |
| 1113 |
return new pass_cse_reciprocals (ctxt); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* If NAME is the result of a type conversion, look for other |
| 1117 |
equivalent dominating or dominated conversions, and replace all |
| 1118 |
uses with the earliest dominating name, removing the redundant |
| 1119 |
conversions. Return the prevailing name. */ |
| 1120 |
|
| 1121 |
static tree |
| 1122 |
execute_cse_conv_1 (tree name, bool *cfg_changed) |
| 1123 |
{ |
| 1124 |
if (SSA_NAME_IS_DEFAULT_DEF (name) |
| 1125 |
|| SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)) |
| 1126 |
return name; |
| 1127 |
|
| 1128 |
gimple *def_stmt = SSA_NAME_DEF_STMT (name); |
| 1129 |
|
| 1130 |
if (!gimple_assign_cast_p (def_stmt)) |
| 1131 |
return name; |
| 1132 |
|
| 1133 |
tree src = gimple_assign_rhs1 (def_stmt); |
| 1134 |
|
| 1135 |
if (TREE_CODE (src) != SSA_NAME) |
| 1136 |
return name; |
| 1137 |
|
| 1138 |
imm_use_iterator use_iter; |
| 1139 |
gimple *use_stmt; |
| 1140 |
|
| 1141 |
/* Find the earliest dominating def. */ |
| 1142 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, src) |
| 1143 |
{ |
| 1144 |
if (use_stmt == def_stmt |
| 1145 |
|| !gimple_assign_cast_p (use_stmt)) |
| 1146 |
continue; |
| 1147 |
|
| 1148 |
tree lhs = gimple_assign_lhs (use_stmt); |
| 1149 |
|
| 1150 |
if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) |
| 1151 |
|| (gimple_assign_rhs1 (use_stmt) |
| 1152 |
!= gimple_assign_rhs1 (def_stmt)) |
| 1153 |
|| !types_compatible_p (TREE_TYPE (name), TREE_TYPE (lhs))) |
| 1154 |
continue; |
| 1155 |
|
| 1156 |
bool use_dominates; |
| 1157 |
if (gimple_bb (def_stmt) == gimple_bb (use_stmt)) |
| 1158 |
{ |
| 1159 |
gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
| 1160 |
while (!gsi_end_p (gsi) && gsi_stmt (gsi) != def_stmt) |
| 1161 |
gsi_next (&gsi); |
| 1162 |
use_dominates = !gsi_end_p (gsi); |
| 1163 |
} |
| 1164 |
else if (dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt), |
| 1165 |
gimple_bb (def_stmt))) |
| 1166 |
use_dominates = false; |
| 1167 |
else if (dominated_by_p (CDI_DOMINATORS, gimple_bb (def_stmt), |
| 1168 |
gimple_bb (use_stmt))) |
| 1169 |
use_dominates = true; |
| 1170 |
else |
| 1171 |
continue; |
| 1172 |
|
| 1173 |
if (use_dominates) |
| 1174 |
{ |
| 1175 |
std::swap (name, lhs); |
| 1176 |
std::swap (def_stmt, use_stmt); |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
/* Now go through all uses of SRC again, replacing the equivalent |
| 1181 |
dominated conversions. We may replace defs that were not |
| 1182 |
dominated by the then-prevailing defs when we first visited |
| 1183 |
them. */ |
| 1184 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, src) |
| 1185 |
{ |
| 1186 |
if (use_stmt == def_stmt |
| 1187 |
|| !gimple_assign_cast_p (use_stmt)) |
| 1188 |
continue; |
| 1189 |
|
| 1190 |
tree lhs = gimple_assign_lhs (use_stmt); |
| 1191 |
|
| 1192 |
if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) |
| 1193 |
|| (gimple_assign_rhs1 (use_stmt) |
| 1194 |
!= gimple_assign_rhs1 (def_stmt)) |
| 1195 |
|| !types_compatible_p (TREE_TYPE (name), TREE_TYPE (lhs))) |
| 1196 |
continue; |
| 1197 |
|
| 1198 |
basic_block use_bb = gimple_bb (use_stmt); |
| 1199 |
if (gimple_bb (def_stmt) == use_bb |
| 1200 |
|| dominated_by_p (CDI_DOMINATORS, use_bb, gimple_bb (def_stmt))) |
| 1201 |
{ |
| 1202 |
sincos_stats.conv_removed++; |
| 1203 |
|
| 1204 |
gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
| 1205 |
replace_uses_by (lhs, name); |
| 1206 |
if (gsi_remove (&gsi, true) |
| 1207 |
&& gimple_purge_dead_eh_edges (use_bb)) |
| 1208 |
*cfg_changed = true; |
| 1209 |
release_defs (use_stmt); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
return name; |
| 1214 |
} |
| 1215 |
|
| 1216 |
/* Records an occurrence at statement USE_STMT in the vector of trees |
| 1217 |
STMTS if it is dominated by *TOP_BB or dominates it or this basic block |
| 1218 |
is not yet initialized. Returns true if the occurrence was pushed on |
| 1219 |
the vector. Adjusts *TOP_BB to be the basic block dominating all |
| 1220 |
statements in the vector. */ |
| 1221 |
|
| 1222 |
static bool |
| 1223 |
maybe_record_sincos (vec<gimple *> *stmts, |
| 1224 |
basic_block *top_bb, gimple *use_stmt) |
| 1225 |
{ |
| 1226 |
basic_block use_bb = gimple_bb (use_stmt); |
| 1227 |
if (*top_bb |
| 1228 |
&& (*top_bb == use_bb |
| 1229 |
|| dominated_by_p (CDI_DOMINATORS, use_bb, *top_bb))) |
| 1230 |
stmts->safe_push (use_stmt); |
| 1231 |
else if (!*top_bb |
| 1232 |
|| dominated_by_p (CDI_DOMINATORS, *top_bb, use_bb)) |
| 1233 |
{ |
| 1234 |
stmts->safe_push (use_stmt); |
| 1235 |
*top_bb = use_bb; |
| 1236 |
} |
| 1237 |
else |
| 1238 |
return false; |
| 1239 |
|
| 1240 |
return true; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/* Look for sin, cos and cexpi calls with the same argument NAME and |
| 1244 |
create a single call to cexpi CSEing the result in this case. |
| 1245 |
We first walk over all immediate uses of the argument collecting |
| 1246 |
statements that we can CSE in a vector and in a second pass replace |
| 1247 |
the statement rhs with a REALPART or IMAGPART expression on the |
| 1248 |
result of the cexpi call we insert before the use statement that |
| 1249 |
dominates all other candidates. */ |
| 1250 |
|
| 1251 |
static bool |
| 1252 |
execute_cse_sincos_1 (tree name) |
| 1253 |
{ |
| 1254 |
gimple_stmt_iterator gsi; |
| 1255 |
imm_use_iterator use_iter; |
| 1256 |
tree fndecl, res, type = NULL_TREE; |
| 1257 |
gimple *def_stmt, *use_stmt, *stmt; |
| 1258 |
int seen_cos = 0, seen_sin = 0, seen_cexpi = 0; |
| 1259 |
auto_vec<gimple *> stmts; |
| 1260 |
basic_block top_bb = NULL; |
| 1261 |
int i; |
| 1262 |
bool cfg_changed = false; |
| 1263 |
|
| 1264 |
name = execute_cse_conv_1 (name, &cfg_changed); |
| 1265 |
|
| 1266 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, name) |
| 1267 |
{ |
| 1268 |
if (gimple_code (use_stmt) != GIMPLE_CALL |
| 1269 |
|| !gimple_call_lhs (use_stmt)) |
| 1270 |
continue; |
| 1271 |
|
| 1272 |
switch (gimple_call_combined_fn (use_stmt)) |
| 1273 |
{ |
| 1274 |
CASE_CFN_COS: |
| 1275 |
seen_cos |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0; |
| 1276 |
break; |
| 1277 |
|
| 1278 |
CASE_CFN_SIN: |
| 1279 |
seen_sin |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0; |
| 1280 |
break; |
| 1281 |
|
| 1282 |
CASE_CFN_CEXPI: |
| 1283 |
seen_cexpi |= maybe_record_sincos (&stmts, &top_bb, use_stmt) ? 1 : 0; |
| 1284 |
break; |
| 1285 |
|
| 1286 |
default:; |
| 1287 |
continue; |
| 1288 |
} |
| 1289 |
|
| 1290 |
tree t = mathfn_built_in_type (gimple_call_combined_fn (use_stmt)); |
| 1291 |
if (!type) |
| 1292 |
{ |
| 1293 |
type = t; |
| 1294 |
t = TREE_TYPE (name); |
| 1295 |
} |
| 1296 |
/* This checks that NAME has the right type in the first round, |
| 1297 |
and, in subsequent rounds, that the built_in type is the same |
| 1298 |
type, or a compatible type. */ |
| 1299 |
if (type != t && !types_compatible_p (type, t)) |
| 1300 |
return false; |
| 1301 |
} |
| 1302 |
if (seen_cos + seen_sin + seen_cexpi <= 1) |
| 1303 |
return false; |
| 1304 |
|
| 1305 |
/* Simply insert cexpi at the beginning of top_bb but not earlier than |
| 1306 |
the name def statement. */ |
| 1307 |
fndecl = mathfn_built_in (type, BUILT_IN_CEXPI); |
| 1308 |
if (!fndecl) |
| 1309 |
return false; |
| 1310 |
stmt = gimple_build_call (fndecl, 1, name); |
| 1311 |
res = make_temp_ssa_name (TREE_TYPE (TREE_TYPE (fndecl)), stmt, "sincostmp"); |
| 1312 |
gimple_call_set_lhs (stmt, res); |
| 1313 |
|
| 1314 |
def_stmt = SSA_NAME_DEF_STMT (name); |
| 1315 |
if (!SSA_NAME_IS_DEFAULT_DEF (name) |
| 1316 |
&& gimple_code (def_stmt) != GIMPLE_PHI |
| 1317 |
&& gimple_bb (def_stmt) == top_bb) |
| 1318 |
{ |
| 1319 |
gsi = gsi_for_stmt (def_stmt); |
| 1320 |
gsi_insert_after (&gsi, stmt, GSI_SAME_STMT); |
| 1321 |
} |
| 1322 |
else |
| 1323 |
{ |
| 1324 |
gsi = gsi_after_labels (top_bb); |
| 1325 |
gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
| 1326 |
} |
| 1327 |
sincos_stats.inserted++; |
| 1328 |
|
| 1329 |
/* And adjust the recorded old call sites. */ |
| 1330 |
for (i = 0; stmts.iterate (i, &use_stmt); ++i) |
| 1331 |
{ |
| 1332 |
tree rhs = NULL; |
| 1333 |
|
| 1334 |
switch (gimple_call_combined_fn (use_stmt)) |
| 1335 |
{ |
| 1336 |
CASE_CFN_COS: |
| 1337 |
rhs = fold_build1 (REALPART_EXPR, type, res); |
| 1338 |
break; |
| 1339 |
|
| 1340 |
CASE_CFN_SIN: |
| 1341 |
rhs = fold_build1 (IMAGPART_EXPR, type, res); |
| 1342 |
break; |
| 1343 |
|
| 1344 |
CASE_CFN_CEXPI: |
| 1345 |
rhs = res; |
| 1346 |
break; |
| 1347 |
|
| 1348 |
default:; |
| 1349 |
gcc_unreachable (); |
| 1350 |
} |
| 1351 |
|
| 1352 |
/* Replace call with a copy. */ |
| 1353 |
stmt = gimple_build_assign (gimple_call_lhs (use_stmt), rhs); |
| 1354 |
|
| 1355 |
gsi = gsi_for_stmt (use_stmt); |
| 1356 |
gsi_replace (&gsi, stmt, true); |
| 1357 |
if (gimple_purge_dead_eh_edges (gimple_bb (stmt))) |
| 1358 |
cfg_changed = true; |
| 1359 |
} |
| 1360 |
|
| 1361 |
return cfg_changed; |
| 1362 |
} |
| 1363 |
|
| 1364 |
/* To evaluate powi(x,n), the floating point value x raised to the |
| 1365 |
constant integer exponent n, we use a hybrid algorithm that |
| 1366 |
combines the "window method" with look-up tables. For an |
| 1367 |
introduction to exponentiation algorithms and "addition chains", |
| 1368 |
see section 4.6.3, "Evaluation of Powers" of Donald E. Knuth, |
| 1369 |
"Seminumerical Algorithms", Vol. 2, "The Art of Computer Programming", |
| 1370 |
3rd Edition, 1998, and Daniel M. Gordon, "A Survey of Fast Exponentiation |
| 1371 |
Methods", Journal of Algorithms, Vol. 27, pp. 129-146, 1998. */ |
| 1372 |
|
| 1373 |
/* Provide a default value for POWI_MAX_MULTS, the maximum number of |
| 1374 |
multiplications to inline before calling the system library's pow |
| 1375 |
function. powi(x,n) requires at worst 2*bits(n)-2 multiplications, |
| 1376 |
so this default never requires calling pow, powf or powl. */ |
| 1377 |
|
| 1378 |
#ifndef POWI_MAX_MULTS |
| 1379 |
#define POWI_MAX_MULTS (2*HOST_BITS_PER_WIDE_INT-2) |
| 1380 |
#endif |
| 1381 |
|
| 1382 |
/* The size of the "optimal power tree" lookup table. All |
| 1383 |
exponents less than this value are simply looked up in the |
| 1384 |
powi_table below. This threshold is also used to size the |
| 1385 |
cache of pseudo registers that hold intermediate results. */ |
| 1386 |
#define POWI_TABLE_SIZE 256 |
| 1387 |
|
| 1388 |
/* The size, in bits of the window, used in the "window method" |
| 1389 |
exponentiation algorithm. This is equivalent to a radix of |
| 1390 |
(1<<POWI_WINDOW_SIZE) in the corresponding "m-ary method". */ |
| 1391 |
#define POWI_WINDOW_SIZE 3 |
| 1392 |
|
| 1393 |
/* The following table is an efficient representation of an |
| 1394 |
"optimal power tree". For each value, i, the corresponding |
| 1395 |
value, j, in the table states than an optimal evaluation |
| 1396 |
sequence for calculating pow(x,i) can be found by evaluating |
| 1397 |
pow(x,j)*pow(x,i-j). An optimal power tree for the first |
| 1398 |
100 integers is given in Knuth's "Seminumerical algorithms". */ |
| 1399 |
|
| 1400 |
static const unsigned char powi_table[POWI_TABLE_SIZE] = |
| 1401 |
{ |
| 1402 |
0, 1, 1, 2, 2, 3, 3, 4, /* 0 - 7 */ |
| 1403 |
4, 6, 5, 6, 6, 10, 7, 9, /* 8 - 15 */ |
| 1404 |
8, 16, 9, 16, 10, 12, 11, 13, /* 16 - 23 */ |
| 1405 |
12, 17, 13, 18, 14, 24, 15, 26, /* 24 - 31 */ |
| 1406 |
16, 17, 17, 19, 18, 33, 19, 26, /* 32 - 39 */ |
| 1407 |
20, 25, 21, 40, 22, 27, 23, 44, /* 40 - 47 */ |
| 1408 |
24, 32, 25, 34, 26, 29, 27, 44, /* 48 - 55 */ |
| 1409 |
28, 31, 29, 34, 30, 60, 31, 36, /* 56 - 63 */ |
| 1410 |
32, 64, 33, 34, 34, 46, 35, 37, /* 64 - 71 */ |
| 1411 |
36, 65, 37, 50, 38, 48, 39, 69, /* 72 - 79 */ |
| 1412 |
40, 49, 41, 43, 42, 51, 43, 58, /* 80 - 87 */ |
| 1413 |
44, 64, 45, 47, 46, 59, 47, 76, /* 88 - 95 */ |
| 1414 |
48, 65, 49, 66, 50, 67, 51, 66, /* 96 - 103 */ |
| 1415 |
52, 70, 53, 74, 54, 104, 55, 74, /* 104 - 111 */ |
| 1416 |
56, 64, 57, 69, 58, 78, 59, 68, /* 112 - 119 */ |
| 1417 |
60, 61, 61, 80, 62, 75, 63, 68, /* 120 - 127 */ |
| 1418 |
64, 65, 65, 128, 66, 129, 67, 90, /* 128 - 135 */ |
| 1419 |
68, 73, 69, 131, 70, 94, 71, 88, /* 136 - 143 */ |
| 1420 |
72, 128, 73, 98, 74, 132, 75, 121, /* 144 - 151 */ |
| 1421 |
76, 102, 77, 124, 78, 132, 79, 106, /* 152 - 159 */ |
| 1422 |
80, 97, 81, 160, 82, 99, 83, 134, /* 160 - 167 */ |
| 1423 |
84, 86, 85, 95, 86, 160, 87, 100, /* 168 - 175 */ |
| 1424 |
88, 113, 89, 98, 90, 107, 91, 122, /* 176 - 183 */ |
| 1425 |
92, 111, 93, 102, 94, 126, 95, 150, /* 184 - 191 */ |
| 1426 |
96, 128, 97, 130, 98, 133, 99, 195, /* 192 - 199 */ |
| 1427 |
100, 128, 101, 123, 102, 164, 103, 138, /* 200 - 207 */ |
| 1428 |
104, 145, 105, 146, 106, 109, 107, 149, /* 208 - 215 */ |
| 1429 |
108, 200, 109, 146, 110, 170, 111, 157, /* 216 - 223 */ |
| 1430 |
112, 128, 113, 130, 114, 182, 115, 132, /* 224 - 231 */ |
| 1431 |
116, 200, 117, 132, 118, 158, 119, 206, /* 232 - 239 */ |
| 1432 |
120, 240, 121, 162, 122, 147, 123, 152, /* 240 - 247 */ |
| 1433 |
124, 166, 125, 214, 126, 138, 127, 153, /* 248 - 255 */ |
| 1434 |
}; |
| 1435 |
|
| 1436 |
|
| 1437 |
/* Return the number of multiplications required to calculate |
| 1438 |
powi(x,n) where n is less than POWI_TABLE_SIZE. This is a |
| 1439 |
subroutine of powi_cost. CACHE is an array indicating |
| 1440 |
which exponents have already been calculated. */ |
| 1441 |
|
| 1442 |
static int |
| 1443 |
powi_lookup_cost (unsigned HOST_WIDE_INT n, bool *cache) |
| 1444 |
{ |
| 1445 |
/* If we've already calculated this exponent, then this evaluation |
| 1446 |
doesn't require any additional multiplications. */ |
| 1447 |
if (cache[n]) |
| 1448 |
return 0; |
| 1449 |
|
| 1450 |
cache[n] = true; |
| 1451 |
return powi_lookup_cost (n - powi_table[n], cache) |
| 1452 |
+ powi_lookup_cost (powi_table[n], cache) + 1; |
| 1453 |
} |
| 1454 |
|
| 1455 |
/* Return the number of multiplications required to calculate |
| 1456 |
powi(x,n) for an arbitrary x, given the exponent N. This |
| 1457 |
function needs to be kept in sync with powi_as_mults below. */ |
| 1458 |
|
| 1459 |
static int |
| 1460 |
powi_cost (HOST_WIDE_INT n) |
| 1461 |
{ |
| 1462 |
bool cache[POWI_TABLE_SIZE]; |
| 1463 |
unsigned HOST_WIDE_INT digit; |
| 1464 |
unsigned HOST_WIDE_INT val; |
| 1465 |
int result; |
| 1466 |
|
| 1467 |
if (n == 0) |
| 1468 |
return 0; |
| 1469 |
|
| 1470 |
/* Ignore the reciprocal when calculating the cost. */ |
| 1471 |
val = absu_hwi (n); |
| 1472 |
|
| 1473 |
/* Initialize the exponent cache. */ |
| 1474 |
memset (cache, 0, POWI_TABLE_SIZE * sizeof (bool)); |
| 1475 |
cache[1] = true; |
| 1476 |
|
| 1477 |
result = 0; |
| 1478 |
|
| 1479 |
while (val >= POWI_TABLE_SIZE) |
| 1480 |
{ |
| 1481 |
if (val & 1) |
| 1482 |
{ |
| 1483 |
digit = val & ((1 << POWI_WINDOW_SIZE) - 1); |
| 1484 |
result += powi_lookup_cost (digit, cache) |
| 1485 |
+ POWI_WINDOW_SIZE + 1; |
| 1486 |
val >>= POWI_WINDOW_SIZE; |
| 1487 |
} |
| 1488 |
else |
| 1489 |
{ |
| 1490 |
val >>= 1; |
| 1491 |
result++; |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
return result + powi_lookup_cost (val, cache); |
| 1496 |
} |
| 1497 |
|
| 1498 |
/* Recursive subroutine of powi_as_mults. This function takes the |
| 1499 |
array, CACHE, of already calculated exponents and an exponent N and |
| 1500 |
returns a tree that corresponds to CACHE[1]**N, with type TYPE. */ |
| 1501 |
|
| 1502 |
static tree |
| 1503 |
powi_as_mults_1 (gimple_stmt_iterator *gsi, location_t loc, tree type, |
| 1504 |
unsigned HOST_WIDE_INT n, tree *cache) |
| 1505 |
{ |
| 1506 |
tree op0, op1, ssa_target; |
| 1507 |
unsigned HOST_WIDE_INT digit; |
| 1508 |
gassign *mult_stmt; |
| 1509 |
|
| 1510 |
if (n < POWI_TABLE_SIZE && cache[n]) |
| 1511 |
return cache[n]; |
| 1512 |
|
| 1513 |
ssa_target = make_temp_ssa_name (type, NULL, "powmult"); |
| 1514 |
|
| 1515 |
if (n < POWI_TABLE_SIZE) |
| 1516 |
{ |
| 1517 |
cache[n] = ssa_target; |
| 1518 |
op0 = powi_as_mults_1 (gsi, loc, type, n - powi_table[n], cache); |
| 1519 |
op1 = powi_as_mults_1 (gsi, loc, type, powi_table[n], cache); |
| 1520 |
} |
| 1521 |
else if (n & 1) |
| 1522 |
{ |
| 1523 |
digit = n & ((1 << POWI_WINDOW_SIZE) - 1); |
| 1524 |
op0 = powi_as_mults_1 (gsi, loc, type, n - digit, cache); |
| 1525 |
op1 = powi_as_mults_1 (gsi, loc, type, digit, cache); |
| 1526 |
} |
| 1527 |
else |
| 1528 |
{ |
| 1529 |
op0 = powi_as_mults_1 (gsi, loc, type, n >> 1, cache); |
| 1530 |
op1 = op0; |
| 1531 |
} |
| 1532 |
|
| 1533 |
mult_stmt = gimple_build_assign (ssa_target, MULT_EXPR, op0, op1); |
| 1534 |
gimple_set_location (mult_stmt, loc); |
| 1535 |
gsi_insert_before (gsi, mult_stmt, GSI_SAME_STMT); |
| 1536 |
|
| 1537 |
return ssa_target; |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* Convert ARG0**N to a tree of multiplications of ARG0 with itself. |
| 1541 |
This function needs to be kept in sync with powi_cost above. */ |
| 1542 |
|
| 1543 |
tree |
| 1544 |
powi_as_mults (gimple_stmt_iterator *gsi, location_t loc, |
| 1545 |
tree arg0, HOST_WIDE_INT n) |
| 1546 |
{ |
| 1547 |
tree cache[POWI_TABLE_SIZE], result, type = TREE_TYPE (arg0); |
| 1548 |
gassign *div_stmt; |
| 1549 |
tree target; |
| 1550 |
|
| 1551 |
if (n == 0) |
| 1552 |
return build_one_cst (type); |
| 1553 |
|
| 1554 |
memset (cache, 0, sizeof (cache)); |
| 1555 |
cache[1] = arg0; |
| 1556 |
|
| 1557 |
result = powi_as_mults_1 (gsi, loc, type, absu_hwi (n), cache); |
| 1558 |
if (n >= 0) |
| 1559 |
return result; |
| 1560 |
|
| 1561 |
/* If the original exponent was negative, reciprocate the result. */ |
| 1562 |
target = make_temp_ssa_name (type, NULL, "powmult"); |
| 1563 |
div_stmt = gimple_build_assign (target, RDIV_EXPR, |
| 1564 |
build_real (type, dconst1), result); |
| 1565 |
gimple_set_location (div_stmt, loc); |
| 1566 |
gsi_insert_before (gsi, div_stmt, GSI_SAME_STMT); |
| 1567 |
|
| 1568 |
return target; |
| 1569 |
} |
| 1570 |
|
| 1571 |
/* ARG0 and N are the two arguments to a powi builtin in GSI with |
| 1572 |
location info LOC. If the arguments are appropriate, create an |
| 1573 |
equivalent sequence of statements prior to GSI using an optimal |
| 1574 |
number of multiplications, and return an expession holding the |
| 1575 |
result. */ |
| 1576 |
|
| 1577 |
static tree |
| 1578 |
gimple_expand_builtin_powi (gimple_stmt_iterator *gsi, location_t loc, |
| 1579 |
tree arg0, HOST_WIDE_INT n) |
| 1580 |
{ |
| 1581 |
if ((n >= -1 && n <= 2) |
| 1582 |
|| (optimize_function_for_speed_p (cfun) |
| 1583 |
&& powi_cost (n) <= POWI_MAX_MULTS)) |
| 1584 |
return powi_as_mults (gsi, loc, arg0, n); |
| 1585 |
|
| 1586 |
return NULL_TREE; |
| 1587 |
} |
| 1588 |
|
| 1589 |
/* Build a gimple call statement that calls FN with argument ARG. |
| 1590 |
Set the lhs of the call statement to a fresh SSA name. Insert the |
| 1591 |
statement prior to GSI's current position, and return the fresh |
| 1592 |
SSA name. */ |
| 1593 |
|
| 1594 |
static tree |
| 1595 |
build_and_insert_call (gimple_stmt_iterator *gsi, location_t loc, |
| 1596 |
tree fn, tree arg) |
| 1597 |
{ |
| 1598 |
gcall *call_stmt; |
| 1599 |
tree ssa_target; |
| 1600 |
|
| 1601 |
call_stmt = gimple_build_call (fn, 1, arg); |
| 1602 |
ssa_target = make_temp_ssa_name (TREE_TYPE (arg), NULL, "powroot"); |
| 1603 |
gimple_set_lhs (call_stmt, ssa_target); |
| 1604 |
gimple_set_location (call_stmt, loc); |
| 1605 |
gsi_insert_before (gsi, call_stmt, GSI_SAME_STMT); |
| 1606 |
|
| 1607 |
return ssa_target; |
| 1608 |
} |
| 1609 |
|
| 1610 |
/* Build a gimple binary operation with the given CODE and arguments |
| 1611 |
ARG0, ARG1, assigning the result to a new SSA name for variable |
| 1612 |
TARGET. Insert the statement prior to GSI's current position, and |
| 1613 |
return the fresh SSA name.*/ |
| 1614 |
|
| 1615 |
static tree |
| 1616 |
build_and_insert_binop (gimple_stmt_iterator *gsi, location_t loc, |
| 1617 |
const char *name, enum tree_code code, |
| 1618 |
tree arg0, tree arg1) |
| 1619 |
{ |
| 1620 |
tree result = make_temp_ssa_name (TREE_TYPE (arg0), NULL, name); |
| 1621 |
gassign *stmt = gimple_build_assign (result, code, arg0, arg1); |
| 1622 |
gimple_set_location (stmt, loc); |
| 1623 |
gsi_insert_before (gsi, stmt, GSI_SAME_STMT); |
| 1624 |
return result; |
| 1625 |
} |
| 1626 |
|
| 1627 |
/* Build a gimple assignment to cast VAL to TYPE. Insert the statement |
| 1628 |
prior to GSI's current position, and return the fresh SSA name. */ |
| 1629 |
|
| 1630 |
static tree |
| 1631 |
build_and_insert_cast (gimple_stmt_iterator *gsi, location_t loc, |
| 1632 |
tree type, tree val) |
| 1633 |
{ |
| 1634 |
tree result = make_ssa_name (type); |
| 1635 |
gassign *stmt = gimple_build_assign (result, NOP_EXPR, val); |
| 1636 |
gimple_set_location (stmt, loc); |
| 1637 |
gsi_insert_before (gsi, stmt, GSI_SAME_STMT); |
| 1638 |
return result; |
| 1639 |
} |
| 1640 |
|
| 1641 |
struct pow_synth_sqrt_info |
| 1642 |
{ |
| 1643 |
bool *factors; |
| 1644 |
unsigned int deepest; |
| 1645 |
unsigned int num_mults; |
| 1646 |
}; |
| 1647 |
|
| 1648 |
/* Return true iff the real value C can be represented as a |
| 1649 |
sum of powers of 0.5 up to N. That is: |
| 1650 |
C == SUM<i from 1..N> (a[i]*(0.5**i)) where a[i] is either 0 or 1. |
| 1651 |
Record in INFO the various parameters of the synthesis algorithm such |
| 1652 |
as the factors a[i], the maximum 0.5 power and the number of |
| 1653 |
multiplications that will be required. */ |
| 1654 |
|
| 1655 |
bool |
| 1656 |
representable_as_half_series_p (REAL_VALUE_TYPE c, unsigned n, |
| 1657 |
struct pow_synth_sqrt_info *info) |
| 1658 |
{ |
| 1659 |
REAL_VALUE_TYPE factor = dconsthalf; |
| 1660 |
REAL_VALUE_TYPE remainder = c; |
| 1661 |
|
| 1662 |
info->deepest = 0; |
| 1663 |
info->num_mults = 0; |
| 1664 |
memset (info->factors, 0, n * sizeof (bool)); |
| 1665 |
|
| 1666 |
for (unsigned i = 0; i < n; i++) |
| 1667 |
{ |
| 1668 |
REAL_VALUE_TYPE res; |
| 1669 |
|
| 1670 |
/* If something inexact happened bail out now. */ |
| 1671 |
if (real_arithmetic (&res, MINUS_EXPR, &remainder, &factor)) |
| 1672 |
return false; |
| 1673 |
|
| 1674 |
/* We have hit zero. The number is representable as a sum |
| 1675 |
of powers of 0.5. */ |
| 1676 |
if (real_equal (&res, &dconst0)) |
| 1677 |
{ |
| 1678 |
info->factors[i] = true; |
| 1679 |
info->deepest = i + 1; |
| 1680 |
return true; |
| 1681 |
} |
| 1682 |
else if (!REAL_VALUE_NEGATIVE (res)) |
| 1683 |
{ |
| 1684 |
remainder = res; |
| 1685 |
info->factors[i] = true; |
| 1686 |
info->num_mults++; |
| 1687 |
} |
| 1688 |
else |
| 1689 |
info->factors[i] = false; |
| 1690 |
|
| 1691 |
real_arithmetic (&factor, MULT_EXPR, &factor, &dconsthalf); |
| 1692 |
} |
| 1693 |
return false; |
| 1694 |
} |
| 1695 |
|
| 1696 |
/* Return the tree corresponding to FN being applied |
| 1697 |
to ARG N times at GSI and LOC. |
| 1698 |
Look up previous results from CACHE if need be. |
| 1699 |
cache[0] should contain just plain ARG i.e. FN applied to ARG 0 times. */ |
| 1700 |
|
| 1701 |
static tree |
| 1702 |
get_fn_chain (tree arg, unsigned int n, gimple_stmt_iterator *gsi, |
| 1703 |
tree fn, location_t loc, tree *cache) |
| 1704 |
{ |
| 1705 |
tree res = cache[n]; |
| 1706 |
if (!res) |
| 1707 |
{ |
| 1708 |
tree prev = get_fn_chain (arg, n - 1, gsi, fn, loc, cache); |
| 1709 |
res = build_and_insert_call (gsi, loc, fn, prev); |
| 1710 |
cache[n] = res; |
| 1711 |
} |
| 1712 |
|
| 1713 |
return res; |
| 1714 |
} |
| 1715 |
|
| 1716 |
/* Print to STREAM the repeated application of function FNAME to ARG |
| 1717 |
N times. So, for FNAME = "foo", ARG = "x", N = 2 it would print: |
| 1718 |
"foo (foo (x))". */ |
| 1719 |
|
| 1720 |
static void |
| 1721 |
print_nested_fn (FILE* stream, const char *fname, const char* arg, |
| 1722 |
unsigned int n) |
| 1723 |
{ |
| 1724 |
if (n == 0) |
| 1725 |
fprintf (stream, "%s", arg); |
| 1726 |
else |
| 1727 |
{ |
| 1728 |
fprintf (stream, "%s (", fname); |
| 1729 |
print_nested_fn (stream, fname, arg, n - 1); |
| 1730 |
fprintf (stream, ")"); |
| 1731 |
} |
| 1732 |
} |
| 1733 |
|
| 1734 |
/* Print to STREAM the fractional sequence of sqrt chains |
| 1735 |
applied to ARG, described by INFO. Used for the dump file. */ |
| 1736 |
|
| 1737 |
static void |
| 1738 |
dump_fractional_sqrt_sequence (FILE *stream, const char *arg, |
| 1739 |
struct pow_synth_sqrt_info *info) |
| 1740 |
{ |
| 1741 |
for (unsigned int i = 0; i < info->deepest; i++) |
| 1742 |
{ |
| 1743 |
bool is_set = info->factors[i]; |
| 1744 |
if (is_set) |
| 1745 |
{ |
| 1746 |
print_nested_fn (stream, "sqrt", arg, i + 1); |
| 1747 |
if (i != info->deepest - 1) |
| 1748 |
fprintf (stream, " * "); |
| 1749 |
} |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
/* Print to STREAM a representation of raising ARG to an integer |
| 1754 |
power N. Used for the dump file. */ |
| 1755 |
|
| 1756 |
static void |
| 1757 |
dump_integer_part (FILE *stream, const char* arg, HOST_WIDE_INT n) |
| 1758 |
{ |
| 1759 |
if (n > 1) |
| 1760 |
fprintf (stream, "powi (%s, " HOST_WIDE_INT_PRINT_DEC ")", arg, n); |
| 1761 |
else if (n == 1) |
| 1762 |
fprintf (stream, "%s", arg); |
| 1763 |
} |
| 1764 |
|
| 1765 |
/* Attempt to synthesize a POW[F] (ARG0, ARG1) call using chains of |
| 1766 |
square roots. Place at GSI and LOC. Limit the maximum depth |
| 1767 |
of the sqrt chains to MAX_DEPTH. Return the tree holding the |
| 1768 |
result of the expanded sequence or NULL_TREE if the expansion failed. |
| 1769 |
|
| 1770 |
This routine assumes that ARG1 is a real number with a fractional part |
| 1771 |
(the integer exponent case will have been handled earlier in |
| 1772 |
gimple_expand_builtin_pow). |
| 1773 |
|
| 1774 |
For ARG1 > 0.0: |
| 1775 |
* For ARG1 composed of a whole part WHOLE_PART and a fractional part |
| 1776 |
FRAC_PART i.e. WHOLE_PART == floor (ARG1) and |
| 1777 |
FRAC_PART == ARG1 - WHOLE_PART: |
| 1778 |
Produce POWI (ARG0, WHOLE_PART) * POW (ARG0, FRAC_PART) where |
| 1779 |
POW (ARG0, FRAC_PART) is expanded as a product of square root chains |
| 1780 |
if it can be expressed as such, that is if FRAC_PART satisfies: |
| 1781 |
FRAC_PART == <SUM from i = 1 until MAX_DEPTH> (a[i] * (0.5**i)) |
| 1782 |
where integer a[i] is either 0 or 1. |
| 1783 |
|
| 1784 |
Example: |
| 1785 |
POW (x, 3.625) == POWI (x, 3) * POW (x, 0.625) |
| 1786 |
--> POWI (x, 3) * SQRT (x) * SQRT (SQRT (SQRT (x))) |
| 1787 |
|
| 1788 |
For ARG1 < 0.0 there are two approaches: |
| 1789 |
* (A) Expand to 1.0 / POW (ARG0, -ARG1) where POW (ARG0, -ARG1) |
| 1790 |
is calculated as above. |
| 1791 |
|
| 1792 |
Example: |
| 1793 |
POW (x, -5.625) == 1.0 / POW (x, 5.625) |
| 1794 |
--> 1.0 / (POWI (x, 5) * SQRT (x) * SQRT (SQRT (SQRT (x)))) |
| 1795 |
|
| 1796 |
* (B) : WHOLE_PART := - ceil (abs (ARG1)) |
| 1797 |
FRAC_PART := ARG1 - WHOLE_PART |
| 1798 |
and expand to POW (x, FRAC_PART) / POWI (x, WHOLE_PART). |
| 1799 |
Example: |
| 1800 |
POW (x, -5.875) == POW (x, 0.125) / POWI (X, 6) |
| 1801 |
--> SQRT (SQRT (SQRT (x))) / (POWI (x, 6)) |
| 1802 |
|
| 1803 |
For ARG1 < 0.0 we choose between (A) and (B) depending on |
| 1804 |
how many multiplications we'd have to do. |
| 1805 |
So, for the example in (B): POW (x, -5.875), if we were to |
| 1806 |
follow algorithm (A) we would produce: |
| 1807 |
1.0 / POWI (X, 5) * SQRT (X) * SQRT (SQRT (X)) * SQRT (SQRT (SQRT (X))) |
| 1808 |
which contains more multiplications than approach (B). |
| 1809 |
|
| 1810 |
Hopefully, this approach will eliminate potentially expensive POW library |
| 1811 |
calls when unsafe floating point math is enabled and allow the compiler to |
| 1812 |
further optimise the multiplies, square roots and divides produced by this |
| 1813 |
function. */ |
| 1814 |
|
| 1815 |
static tree |
| 1816 |
expand_pow_as_sqrts (gimple_stmt_iterator *gsi, location_t loc, |
| 1817 |
tree arg0, tree arg1, HOST_WIDE_INT max_depth) |
| 1818 |
{ |
| 1819 |
tree type = TREE_TYPE (arg0); |
| 1820 |
machine_mode mode = TYPE_MODE (type); |
| 1821 |
tree sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); |
| 1822 |
bool one_over = true; |
| 1823 |
|
| 1824 |
if (!sqrtfn) |
| 1825 |
return NULL_TREE; |
| 1826 |
|
| 1827 |
if (TREE_CODE (arg1) != REAL_CST) |
| 1828 |
return NULL_TREE; |
| 1829 |
|
| 1830 |
REAL_VALUE_TYPE exp_init = TREE_REAL_CST (arg1); |
| 1831 |
|
| 1832 |
gcc_assert (max_depth > 0); |
| 1833 |
tree *cache = XALLOCAVEC (tree, max_depth + 1); |
| 1834 |
|
| 1835 |
struct pow_synth_sqrt_info synth_info; |
| 1836 |
synth_info.factors = XALLOCAVEC (bool, max_depth + 1); |
| 1837 |
synth_info.deepest = 0; |
| 1838 |
synth_info.num_mults = 0; |
| 1839 |
|
| 1840 |
bool neg_exp = REAL_VALUE_NEGATIVE (exp_init); |
| 1841 |
REAL_VALUE_TYPE exp = real_value_abs (&exp_init); |
| 1842 |
|
| 1843 |
/* The whole and fractional parts of exp. */ |
| 1844 |
REAL_VALUE_TYPE whole_part; |
| 1845 |
REAL_VALUE_TYPE frac_part; |
| 1846 |
|
| 1847 |
real_floor (&whole_part, mode, &exp); |
| 1848 |
real_arithmetic (&frac_part, MINUS_EXPR, &exp, &whole_part); |
| 1849 |
|
| 1850 |
|
| 1851 |
REAL_VALUE_TYPE ceil_whole = dconst0; |
| 1852 |
REAL_VALUE_TYPE ceil_fract = dconst0; |
| 1853 |
|
| 1854 |
if (neg_exp) |
| 1855 |
{ |
| 1856 |
real_ceil (&ceil_whole, mode, &exp); |
| 1857 |
real_arithmetic (&ceil_fract, MINUS_EXPR, &ceil_whole, &exp); |
| 1858 |
} |
| 1859 |
|
| 1860 |
if (!representable_as_half_series_p (frac_part, max_depth, &synth_info)) |
| 1861 |
return NULL_TREE; |
| 1862 |
|
| 1863 |
/* Check whether it's more profitable to not use 1.0 / ... */ |
| 1864 |
if (neg_exp) |
| 1865 |
{ |
| 1866 |
struct pow_synth_sqrt_info alt_synth_info; |
| 1867 |
alt_synth_info.factors = XALLOCAVEC (bool, max_depth + 1); |
| 1868 |
alt_synth_info.deepest = 0; |
| 1869 |
alt_synth_info.num_mults = 0; |
| 1870 |
|
| 1871 |
if (representable_as_half_series_p (ceil_fract, max_depth, |
| 1872 |
&alt_synth_info) |
| 1873 |
&& alt_synth_info.deepest <= synth_info.deepest |
| 1874 |
&& alt_synth_info.num_mults < synth_info.num_mults) |
| 1875 |
{ |
| 1876 |
whole_part = ceil_whole; |
| 1877 |
frac_part = ceil_fract; |
| 1878 |
synth_info.deepest = alt_synth_info.deepest; |
| 1879 |
synth_info.num_mults = alt_synth_info.num_mults; |
| 1880 |
memcpy (synth_info.factors, alt_synth_info.factors, |
| 1881 |
(max_depth + 1) * sizeof (bool)); |
| 1882 |
one_over = false; |
| 1883 |
} |
| 1884 |
} |
| 1885 |
|
| 1886 |
HOST_WIDE_INT n = real_to_integer (&whole_part); |
| 1887 |
REAL_VALUE_TYPE cint; |
| 1888 |
real_from_integer (&cint, VOIDmode, n, SIGNED); |
| 1889 |
|
| 1890 |
if (!real_identical (&whole_part, &cint)) |
| 1891 |
return NULL_TREE; |
| 1892 |
|
| 1893 |
if (powi_cost (n) + synth_info.num_mults > POWI_MAX_MULTS) |
| 1894 |
return NULL_TREE; |
| 1895 |
|
| 1896 |
memset (cache, 0, (max_depth + 1) * sizeof (tree)); |
| 1897 |
|
| 1898 |
tree integer_res = n == 0 ? build_real (type, dconst1) : arg0; |
| 1899 |
|
| 1900 |
/* Calculate the integer part of the exponent. */ |
| 1901 |
if (n > 1) |
| 1902 |
{ |
| 1903 |
integer_res = gimple_expand_builtin_powi (gsi, loc, arg0, n); |
| 1904 |
if (!integer_res) |
| 1905 |
return NULL_TREE; |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (dump_file) |
| 1909 |
{ |
| 1910 |
char string[64]; |
| 1911 |
|
| 1912 |
real_to_decimal (string, &exp_init, sizeof (string), 0, 1); |
| 1913 |
fprintf (dump_file, "synthesizing pow (x, %s) as:\n", string); |
| 1914 |
|
| 1915 |
if (neg_exp) |
| 1916 |
{ |
| 1917 |
if (one_over) |
| 1918 |
{ |
| 1919 |
fprintf (dump_file, "1.0 / ("); |
| 1920 |
dump_integer_part (dump_file, "x", n); |
| 1921 |
if (n > 0) |
| 1922 |
fprintf (dump_file, " * "); |
| 1923 |
dump_fractional_sqrt_sequence (dump_file, "x", &synth_info); |
| 1924 |
fprintf (dump_file, ")"); |
| 1925 |
} |
| 1926 |
else |
| 1927 |
{ |
| 1928 |
dump_fractional_sqrt_sequence (dump_file, "x", &synth_info); |
| 1929 |
fprintf (dump_file, " / ("); |
| 1930 |
dump_integer_part (dump_file, "x", n); |
| 1931 |
fprintf (dump_file, ")"); |
| 1932 |
} |
| 1933 |
} |
| 1934 |
else |
| 1935 |
{ |
| 1936 |
dump_fractional_sqrt_sequence (dump_file, "x", &synth_info); |
| 1937 |
if (n > 0) |
| 1938 |
fprintf (dump_file, " * "); |
| 1939 |
dump_integer_part (dump_file, "x", n); |
| 1940 |
} |
| 1941 |
|
| 1942 |
fprintf (dump_file, "\ndeepest sqrt chain: %d\n", synth_info.deepest); |
| 1943 |
} |
| 1944 |
|
| 1945 |
|
| 1946 |
tree fract_res = NULL_TREE; |
| 1947 |
cache[0] = arg0; |
| 1948 |
|
| 1949 |
/* Calculate the fractional part of the exponent. */ |
| 1950 |
for (unsigned i = 0; i < synth_info.deepest; i++) |
| 1951 |
{ |
| 1952 |
if (synth_info.factors[i]) |
| 1953 |
{ |
| 1954 |
tree sqrt_chain = get_fn_chain (arg0, i + 1, gsi, sqrtfn, loc, cache); |
| 1955 |
|
| 1956 |
if (!fract_res) |
| 1957 |
fract_res = sqrt_chain; |
| 1958 |
|
| 1959 |
else |
| 1960 |
fract_res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR, |
| 1961 |
fract_res, sqrt_chain); |
| 1962 |
} |
| 1963 |
} |
| 1964 |
|
| 1965 |
tree res = NULL_TREE; |
| 1966 |
|
| 1967 |
if (neg_exp) |
| 1968 |
{ |
| 1969 |
if (one_over) |
| 1970 |
{ |
| 1971 |
if (n > 0) |
| 1972 |
res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR, |
| 1973 |
fract_res, integer_res); |
| 1974 |
else |
| 1975 |
res = fract_res; |
| 1976 |
|
| 1977 |
res = build_and_insert_binop (gsi, loc, "powrootrecip", RDIV_EXPR, |
| 1978 |
build_real (type, dconst1), res); |
| 1979 |
} |
| 1980 |
else |
| 1981 |
{ |
| 1982 |
res = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR, |
| 1983 |
fract_res, integer_res); |
| 1984 |
} |
| 1985 |
} |
| 1986 |
else |
| 1987 |
res = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR, |
| 1988 |
fract_res, integer_res); |
| 1989 |
return res; |
| 1990 |
} |
| 1991 |
|
| 1992 |
/* ARG0 and ARG1 are the two arguments to a pow builtin call in GSI |
| 1993 |
with location info LOC. If possible, create an equivalent and |
| 1994 |
less expensive sequence of statements prior to GSI, and return an |
| 1995 |
expession holding the result. */ |
| 1996 |
|
| 1997 |
static tree |
| 1998 |
gimple_expand_builtin_pow (gimple_stmt_iterator *gsi, location_t loc, |
| 1999 |
tree arg0, tree arg1) |
| 2000 |
{ |
| 2001 |
REAL_VALUE_TYPE c, cint, dconst1_3, dconst1_4, dconst1_6; |
| 2002 |
REAL_VALUE_TYPE c2, dconst3; |
| 2003 |
HOST_WIDE_INT n; |
| 2004 |
tree type, sqrtfn, cbrtfn, sqrt_arg0, result, cbrt_x, powi_cbrt_x; |
| 2005 |
machine_mode mode; |
| 2006 |
bool speed_p = optimize_bb_for_speed_p (gsi_bb (*gsi)); |
| 2007 |
bool hw_sqrt_exists, c_is_int, c2_is_int; |
| 2008 |
|
| 2009 |
dconst1_4 = dconst1; |
| 2010 |
SET_REAL_EXP (&dconst1_4, REAL_EXP (&dconst1_4) - 2); |
| 2011 |
|
| 2012 |
/* If the exponent isn't a constant, there's nothing of interest |
| 2013 |
to be done. */ |
| 2014 |
if (TREE_CODE (arg1) != REAL_CST) |
| 2015 |
return NULL_TREE; |
| 2016 |
|
| 2017 |
/* Don't perform the operation if flag_signaling_nans is on |
| 2018 |
and the operand is a signaling NaN. */ |
| 2019 |
if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))) |
| 2020 |
&& ((TREE_CODE (arg0) == REAL_CST |
| 2021 |
&& REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))) |
| 2022 |
|| REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))) |
| 2023 |
return NULL_TREE; |
| 2024 |
|
| 2025 |
/* If the exponent is equivalent to an integer, expand to an optimal |
| 2026 |
multiplication sequence when profitable. */ |
| 2027 |
c = TREE_REAL_CST (arg1); |
| 2028 |
n = real_to_integer (&c); |
| 2029 |
real_from_integer (&cint, VOIDmode, n, SIGNED); |
| 2030 |
c_is_int = real_identical (&c, &cint); |
| 2031 |
|
| 2032 |
if (c_is_int |
| 2033 |
&& ((n >= -1 && n <= 2) |
| 2034 |
|| (flag_unsafe_math_optimizations |
| 2035 |
&& speed_p |
| 2036 |
&& powi_cost (n) <= POWI_MAX_MULTS))) |
| 2037 |
return gimple_expand_builtin_powi (gsi, loc, arg0, n); |
| 2038 |
|
| 2039 |
/* Attempt various optimizations using sqrt and cbrt. */ |
| 2040 |
type = TREE_TYPE (arg0); |
| 2041 |
mode = TYPE_MODE (type); |
| 2042 |
sqrtfn = mathfn_built_in (type, BUILT_IN_SQRT); |
| 2043 |
|
| 2044 |
/* Optimize pow(x,0.5) = sqrt(x). This replacement is always safe |
| 2045 |
unless signed zeros must be maintained. pow(-0,0.5) = +0, while |
| 2046 |
sqrt(-0) = -0. */ |
| 2047 |
if (sqrtfn |
| 2048 |
&& real_equal (&c, &dconsthalf) |
| 2049 |
&& !HONOR_SIGNED_ZEROS (mode)) |
| 2050 |
return build_and_insert_call (gsi, loc, sqrtfn, arg0); |
| 2051 |
|
| 2052 |
hw_sqrt_exists = optab_handler (sqrt_optab, mode) != CODE_FOR_nothing; |
| 2053 |
|
| 2054 |
/* Optimize pow(x,1./3.) = cbrt(x). This requires unsafe math |
| 2055 |
optimizations since 1./3. is not exactly representable. If x |
| 2056 |
is negative and finite, the correct value of pow(x,1./3.) is |
| 2057 |
a NaN with the "invalid" exception raised, because the value |
| 2058 |
of 1./3. actually has an even denominator. The correct value |
| 2059 |
of cbrt(x) is a negative real value. */ |
| 2060 |
cbrtfn = mathfn_built_in (type, BUILT_IN_CBRT); |
| 2061 |
dconst1_3 = real_value_truncate (mode, dconst_third ()); |
| 2062 |
|
| 2063 |
if (flag_unsafe_math_optimizations |
| 2064 |
&& cbrtfn |
| 2065 |
&& (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0)) |
| 2066 |
&& real_equal (&c, &dconst1_3)) |
| 2067 |
return build_and_insert_call (gsi, loc, cbrtfn, arg0); |
| 2068 |
|
| 2069 |
/* Optimize pow(x,1./6.) = cbrt(sqrt(x)). Don't do this optimization |
| 2070 |
if we don't have a hardware sqrt insn. */ |
| 2071 |
dconst1_6 = dconst1_3; |
| 2072 |
SET_REAL_EXP (&dconst1_6, REAL_EXP (&dconst1_6) - 1); |
| 2073 |
|
| 2074 |
if (flag_unsafe_math_optimizations |
| 2075 |
&& sqrtfn |
| 2076 |
&& cbrtfn |
| 2077 |
&& (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0)) |
| 2078 |
&& speed_p |
| 2079 |
&& hw_sqrt_exists |
| 2080 |
&& real_equal (&c, &dconst1_6)) |
| 2081 |
{ |
| 2082 |
/* sqrt(x) */ |
| 2083 |
sqrt_arg0 = build_and_insert_call (gsi, loc, sqrtfn, arg0); |
| 2084 |
|
| 2085 |
/* cbrt(sqrt(x)) */ |
| 2086 |
return build_and_insert_call (gsi, loc, cbrtfn, sqrt_arg0); |
| 2087 |
} |
| 2088 |
|
| 2089 |
|
| 2090 |
/* Attempt to expand the POW as a product of square root chains. |
| 2091 |
Expand the 0.25 case even when otpimising for size. */ |
| 2092 |
if (flag_unsafe_math_optimizations |
| 2093 |
&& sqrtfn |
| 2094 |
&& hw_sqrt_exists |
| 2095 |
&& (speed_p || real_equal (&c, &dconst1_4)) |
| 2096 |
&& !HONOR_SIGNED_ZEROS (mode)) |
| 2097 |
{ |
| 2098 |
unsigned int max_depth = speed_p |
| 2099 |
? param_max_pow_sqrt_depth |
| 2100 |
: 2; |
| 2101 |
|
| 2102 |
tree expand_with_sqrts |
| 2103 |
= expand_pow_as_sqrts (gsi, loc, arg0, arg1, max_depth); |
| 2104 |
|
| 2105 |
if (expand_with_sqrts) |
| 2106 |
return expand_with_sqrts; |
| 2107 |
} |
| 2108 |
|
| 2109 |
real_arithmetic (&c2, MULT_EXPR, &c, &dconst2); |
| 2110 |
n = real_to_integer (&c2); |
| 2111 |
real_from_integer (&cint, VOIDmode, n, SIGNED); |
| 2112 |
c2_is_int = real_identical (&c2, &cint); |
| 2113 |
|
| 2114 |
/* Optimize pow(x,c), where 3c = n for some nonzero integer n, into |
| 2115 |
|
| 2116 |
powi(x, n/3) * powi(cbrt(x), n%3), n > 0; |
| 2117 |
1.0 / (powi(x, abs(n)/3) * powi(cbrt(x), abs(n)%3)), n < 0. |
| 2118 |
|
| 2119 |
Do not calculate the first factor when n/3 = 0. As cbrt(x) is |
| 2120 |
different from pow(x, 1./3.) due to rounding and behavior with |
| 2121 |
negative x, we need to constrain this transformation to unsafe |
| 2122 |
math and positive x or finite math. */ |
| 2123 |
real_from_integer (&dconst3, VOIDmode, 3, SIGNED); |
| 2124 |
real_arithmetic (&c2, MULT_EXPR, &c, &dconst3); |
| 2125 |
real_round (&c2, mode, &c2); |
| 2126 |
n = real_to_integer (&c2); |
| 2127 |
real_from_integer (&cint, VOIDmode, n, SIGNED); |
| 2128 |
real_arithmetic (&c2, RDIV_EXPR, &cint, &dconst3); |
| 2129 |
real_convert (&c2, mode, &c2); |
| 2130 |
|
| 2131 |
if (flag_unsafe_math_optimizations |
| 2132 |
&& cbrtfn |
| 2133 |
&& (!HONOR_NANS (mode) || tree_expr_nonnegative_p (arg0)) |
| 2134 |
&& real_identical (&c2, &c) |
| 2135 |
&& !c2_is_int |
| 2136 |
&& optimize_function_for_speed_p (cfun) |
| 2137 |
&& powi_cost (n / 3) <= POWI_MAX_MULTS) |
| 2138 |
{ |
| 2139 |
tree powi_x_ndiv3 = NULL_TREE; |
| 2140 |
|
| 2141 |
/* Attempt to fold powi(arg0, abs(n/3)) into multiplies. If not |
| 2142 |
possible or profitable, give up. Skip the degenerate case when |
| 2143 |
abs(n) < 3, where the result is always 1. */ |
| 2144 |
if (absu_hwi (n) >= 3) |
| 2145 |
{ |
| 2146 |
powi_x_ndiv3 = gimple_expand_builtin_powi (gsi, loc, arg0, |
| 2147 |
abs_hwi (n / 3)); |
| 2148 |
if (!powi_x_ndiv3) |
| 2149 |
return NULL_TREE; |
| 2150 |
} |
| 2151 |
|
| 2152 |
/* Calculate powi(cbrt(x), n%3). Don't use gimple_expand_builtin_powi |
| 2153 |
as that creates an unnecessary variable. Instead, just produce |
| 2154 |
either cbrt(x) or cbrt(x) * cbrt(x). */ |
| 2155 |
cbrt_x = build_and_insert_call (gsi, loc, cbrtfn, arg0); |
| 2156 |
|
| 2157 |
if (absu_hwi (n) % 3 == 1) |
| 2158 |
powi_cbrt_x = cbrt_x; |
| 2159 |
else |
| 2160 |
powi_cbrt_x = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR, |
| 2161 |
cbrt_x, cbrt_x); |
| 2162 |
|
| 2163 |
/* Multiply the two subexpressions, unless powi(x,abs(n)/3) = 1. */ |
| 2164 |
if (absu_hwi (n) < 3) |
| 2165 |
result = powi_cbrt_x; |
| 2166 |
else |
| 2167 |
result = build_and_insert_binop (gsi, loc, "powroot", MULT_EXPR, |
| 2168 |
powi_x_ndiv3, powi_cbrt_x); |
| 2169 |
|
| 2170 |
/* If n is negative, reciprocate the result. */ |
| 2171 |
if (n < 0) |
| 2172 |
result = build_and_insert_binop (gsi, loc, "powroot", RDIV_EXPR, |
| 2173 |
build_real (type, dconst1), result); |
| 2174 |
|
| 2175 |
return result; |
| 2176 |
} |
| 2177 |
|
| 2178 |
/* No optimizations succeeded. */ |
| 2179 |
return NULL_TREE; |
| 2180 |
} |
| 2181 |
|
| 2182 |
/* Go through all calls to sin, cos and cexpi and call execute_cse_sincos_1 |
| 2183 |
on the SSA_NAME argument of each of them. */ |
| 2184 |
|
| 2185 |
namespace { |
| 2186 |
|
| 2187 |
const pass_data pass_data_cse_sincos = |
| 2188 |
{ |
| 2189 |
GIMPLE_PASS, /* type */ |
| 2190 |
"sincos", /* name */ |
| 2191 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 2192 |
TV_TREE_SINCOS, /* tv_id */ |
| 2193 |
PROP_ssa, /* properties_required */ |
| 2194 |
0, /* properties_provided */ |
| 2195 |
0, /* properties_destroyed */ |
| 2196 |
0, /* todo_flags_start */ |
| 2197 |
TODO_update_ssa, /* todo_flags_finish */ |
| 2198 |
}; |
| 2199 |
|
| 2200 |
class pass_cse_sincos : public gimple_opt_pass |
| 2201 |
{ |
| 2202 |
public: |
| 2203 |
pass_cse_sincos (gcc::context *ctxt) |
| 2204 |
: gimple_opt_pass (pass_data_cse_sincos, ctxt) |
| 2205 |
{} |
| 2206 |
|
| 2207 |
/* opt_pass methods: */ |
| 2208 |
bool gate (function *) final override |
| 2209 |
{ |
| 2210 |
return optimize; |
| 2211 |
} |
| 2212 |
|
| 2213 |
unsigned int execute (function *) final override; |
| 2214 |
|
| 2215 |
}; // class pass_cse_sincos |
| 2216 |
|
| 2217 |
unsigned int |
| 2218 |
pass_cse_sincos::execute (function *fun) |
| 2219 |
{ |
| 2220 |
basic_block bb; |
| 2221 |
bool cfg_changed = false; |
| 2222 |
|
| 2223 |
calculate_dominance_info (CDI_DOMINATORS); |
| 2224 |
memset (&sincos_stats, 0, sizeof (sincos_stats)); |
| 2225 |
|
| 2226 |
FOR_EACH_BB_FN (bb, fun) |
| 2227 |
{ |
| 2228 |
gimple_stmt_iterator gsi; |
| 2229 |
|
| 2230 |
for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
| 2231 |
{ |
| 2232 |
gimple *stmt = gsi_stmt (gsi); |
| 2233 |
|
| 2234 |
if (is_gimple_call (stmt) |
| 2235 |
&& gimple_call_lhs (stmt)) |
| 2236 |
{ |
| 2237 |
tree arg; |
| 2238 |
switch (gimple_call_combined_fn (stmt)) |
| 2239 |
{ |
| 2240 |
CASE_CFN_COS: |
| 2241 |
CASE_CFN_SIN: |
| 2242 |
CASE_CFN_CEXPI: |
| 2243 |
arg = gimple_call_arg (stmt, 0); |
| 2244 |
/* Make sure we have either sincos or cexp. */ |
| 2245 |
if (!targetm.libc_has_function (function_c99_math_complex, |
| 2246 |
TREE_TYPE (arg)) |
| 2247 |
&& !targetm.libc_has_function (function_sincos, |
| 2248 |
TREE_TYPE (arg))) |
| 2249 |
break; |
| 2250 |
|
| 2251 |
if (TREE_CODE (arg) == SSA_NAME) |
| 2252 |
cfg_changed |= execute_cse_sincos_1 (arg); |
| 2253 |
break; |
| 2254 |
default: |
| 2255 |
break; |
| 2256 |
} |
| 2257 |
} |
| 2258 |
} |
| 2259 |
} |
| 2260 |
|
| 2261 |
statistics_counter_event (fun, "sincos statements inserted", |
| 2262 |
sincos_stats.inserted); |
| 2263 |
statistics_counter_event (fun, "conv statements removed", |
| 2264 |
sincos_stats.conv_removed); |
| 2265 |
|
| 2266 |
return cfg_changed ? TODO_cleanup_cfg : 0; |
| 2267 |
} |
| 2268 |
|
| 2269 |
} // anon namespace |
| 2270 |
|
| 2271 |
gimple_opt_pass * |
| 2272 |
make_pass_cse_sincos (gcc::context *ctxt) |
| 2273 |
{ |
| 2274 |
return new pass_cse_sincos (ctxt); |
| 2275 |
} |
| 2276 |
|
| 2277 |
/* Expand powi(x,n) into an optimal number of multiplies, when n is a |
| 2278 |
constant. */ |
| 2279 |
namespace { |
| 2280 |
|
| 2281 |
const pass_data pass_data_expand_pow = |
| 2282 |
{ |
| 2283 |
GIMPLE_PASS, /* type */ |
| 2284 |
"pow", /* name */ |
| 2285 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 2286 |
TV_TREE_POW, /* tv_id */ |
| 2287 |
PROP_ssa, /* properties_required */ |
| 2288 |
PROP_gimple_opt_math, /* properties_provided */ |
| 2289 |
0, /* properties_destroyed */ |
| 2290 |
0, /* todo_flags_start */ |
| 2291 |
TODO_update_ssa, /* todo_flags_finish */ |
| 2292 |
}; |
| 2293 |
|
| 2294 |
class pass_expand_pow : public gimple_opt_pass |
| 2295 |
{ |
| 2296 |
public: |
| 2297 |
pass_expand_pow (gcc::context *ctxt) |
| 2298 |
: gimple_opt_pass (pass_data_expand_pow, ctxt) |
| 2299 |
{} |
| 2300 |
|
| 2301 |
/* opt_pass methods: */ |
| 2302 |
bool gate (function *) final override |
| 2303 |
{ |
| 2304 |
return optimize; |
| 2305 |
} |
| 2306 |
|
| 2307 |
unsigned int execute (function *) final override; |
| 2308 |
|
| 2309 |
}; // class pass_expand_pow |
| 2310 |
|
| 2311 |
unsigned int |
| 2312 |
pass_expand_pow::execute (function *fun) |
| 2313 |
{ |
| 2314 |
basic_block bb; |
| 2315 |
bool cfg_changed = false; |
| 2316 |
|
| 2317 |
calculate_dominance_info (CDI_DOMINATORS); |
| 2318 |
|
| 2319 |
FOR_EACH_BB_FN (bb, fun) |
| 2320 |
{ |
| 2321 |
gimple_stmt_iterator gsi; |
| 2322 |
bool cleanup_eh = false; |
| 2323 |
|
| 2324 |
for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
| 2325 |
{ |
| 2326 |
gimple *stmt = gsi_stmt (gsi); |
| 2327 |
|
| 2328 |
/* Only the last stmt in a bb could throw, no need to call |
| 2329 |
gimple_purge_dead_eh_edges if we change something in the middle |
| 2330 |
of a basic block. */ |
| 2331 |
cleanup_eh = false; |
| 2332 |
|
| 2333 |
if (is_gimple_call (stmt) |
| 2334 |
&& gimple_call_lhs (stmt)) |
| 2335 |
{ |
| 2336 |
tree arg0, arg1, result; |
| 2337 |
HOST_WIDE_INT n; |
| 2338 |
location_t loc; |
| 2339 |
|
| 2340 |
switch (gimple_call_combined_fn (stmt)) |
| 2341 |
{ |
| 2342 |
CASE_CFN_POW: |
| 2343 |
arg0 = gimple_call_arg (stmt, 0); |
| 2344 |
arg1 = gimple_call_arg (stmt, 1); |
| 2345 |
|
| 2346 |
loc = gimple_location (stmt); |
| 2347 |
result = gimple_expand_builtin_pow (&gsi, loc, arg0, arg1); |
| 2348 |
|
| 2349 |
if (result) |
| 2350 |
{ |
| 2351 |
tree lhs = gimple_get_lhs (stmt); |
| 2352 |
gassign *new_stmt = gimple_build_assign (lhs, result); |
| 2353 |
gimple_set_location (new_stmt, loc); |
| 2354 |
unlink_stmt_vdef (stmt); |
| 2355 |
gsi_replace (&gsi, new_stmt, true); |
| 2356 |
cleanup_eh = true; |
| 2357 |
if (gimple_vdef (stmt)) |
| 2358 |
release_ssa_name (gimple_vdef (stmt)); |
| 2359 |
} |
| 2360 |
break; |
| 2361 |
|
| 2362 |
CASE_CFN_POWI: |
| 2363 |
arg0 = gimple_call_arg (stmt, 0); |
| 2364 |
arg1 = gimple_call_arg (stmt, 1); |
| 2365 |
loc = gimple_location (stmt); |
| 2366 |
|
| 2367 |
if (real_minus_onep (arg0)) |
| 2368 |
{ |
| 2369 |
tree t0, t1, cond, one, minus_one; |
| 2370 |
gassign *stmt; |
| 2371 |
|
| 2372 |
t0 = TREE_TYPE (arg0); |
| 2373 |
t1 = TREE_TYPE (arg1); |
| 2374 |
one = build_real (t0, dconst1); |
| 2375 |
minus_one = build_real (t0, dconstm1); |
| 2376 |
|
| 2377 |
cond = make_temp_ssa_name (t1, NULL, "powi_cond"); |
| 2378 |
stmt = gimple_build_assign (cond, BIT_AND_EXPR, |
| 2379 |
arg1, build_int_cst (t1, 1)); |
| 2380 |
gimple_set_location (stmt, loc); |
| 2381 |
gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
| 2382 |
|
| 2383 |
result = make_temp_ssa_name (t0, NULL, "powi"); |
| 2384 |
stmt = gimple_build_assign (result, COND_EXPR, cond, |
| 2385 |
minus_one, one); |
| 2386 |
gimple_set_location (stmt, loc); |
| 2387 |
gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); |
| 2388 |
} |
| 2389 |
else |
| 2390 |
{ |
| 2391 |
if (!tree_fits_shwi_p (arg1)) |
| 2392 |
break; |
| 2393 |
|
| 2394 |
n = tree_to_shwi (arg1); |
| 2395 |
result = gimple_expand_builtin_powi (&gsi, loc, arg0, n); |
| 2396 |
} |
| 2397 |
|
| 2398 |
if (result) |
| 2399 |
{ |
| 2400 |
tree lhs = gimple_get_lhs (stmt); |
| 2401 |
gassign *new_stmt = gimple_build_assign (lhs, result); |
| 2402 |
gimple_set_location (new_stmt, loc); |
| 2403 |
unlink_stmt_vdef (stmt); |
| 2404 |
gsi_replace (&gsi, new_stmt, true); |
| 2405 |
cleanup_eh = true; |
| 2406 |
if (gimple_vdef (stmt)) |
| 2407 |
release_ssa_name (gimple_vdef (stmt)); |
| 2408 |
} |
| 2409 |
break; |
| 2410 |
|
| 2411 |
default:; |
| 2412 |
} |
| 2413 |
} |
| 2414 |
} |
| 2415 |
if (cleanup_eh) |
| 2416 |
cfg_changed |= gimple_purge_dead_eh_edges (bb); |
| 2417 |
} |
| 2418 |
|
| 2419 |
return cfg_changed ? TODO_cleanup_cfg : 0; |
| 2420 |
} |
| 2421 |
|
| 2422 |
} // anon namespace |
| 2423 |
|
| 2424 |
gimple_opt_pass * |
| 2425 |
make_pass_expand_pow (gcc::context *ctxt) |
| 2426 |
{ |
| 2427 |
return new pass_expand_pow (ctxt); |
| 2428 |
} |
| 2429 |
|
| 2430 |
/* Return true if stmt is a type conversion operation that can be stripped |
| 2431 |
when used in a widening multiply operation. */ |
| 2432 |
static bool |
| 2433 |
widening_mult_conversion_strippable_p (tree result_type, gimple *stmt) |
| 2434 |
{ |
| 2435 |
enum tree_code rhs_code = gimple_assign_rhs_code (stmt); |
| 2436 |
|
| 2437 |
if (TREE_CODE (result_type) == INTEGER_TYPE) |
| 2438 |
{ |
| 2439 |
tree op_type; |
| 2440 |
tree inner_op_type; |
| 2441 |
|
| 2442 |
if (!CONVERT_EXPR_CODE_P (rhs_code)) |
| 2443 |
return false; |
| 2444 |
|
| 2445 |
op_type = TREE_TYPE (gimple_assign_lhs (stmt)); |
| 2446 |
|
| 2447 |
/* If the type of OP has the same precision as the result, then |
| 2448 |
we can strip this conversion. The multiply operation will be |
| 2449 |
selected to create the correct extension as a by-product. */ |
| 2450 |
if (TYPE_PRECISION (result_type) == TYPE_PRECISION (op_type)) |
| 2451 |
return true; |
| 2452 |
|
| 2453 |
/* We can also strip a conversion if it preserves the signed-ness of |
| 2454 |
the operation and doesn't narrow the range. */ |
| 2455 |
inner_op_type = TREE_TYPE (gimple_assign_rhs1 (stmt)); |
| 2456 |
|
| 2457 |
/* If the inner-most type is unsigned, then we can strip any |
| 2458 |
intermediate widening operation. If it's signed, then the |
| 2459 |
intermediate widening operation must also be signed. */ |
| 2460 |
if ((TYPE_UNSIGNED (inner_op_type) |
| 2461 |
|| TYPE_UNSIGNED (op_type) == TYPE_UNSIGNED (inner_op_type)) |
| 2462 |
&& TYPE_PRECISION (op_type) > TYPE_PRECISION (inner_op_type)) |
| 2463 |
return true; |
| 2464 |
|
| 2465 |
return false; |
| 2466 |
} |
| 2467 |
|
| 2468 |
return rhs_code == FIXED_CONVERT_EXPR; |
| 2469 |
} |
| 2470 |
|
| 2471 |
/* Return true if RHS is a suitable operand for a widening multiplication, |
| 2472 |
assuming a target type of TYPE. |
| 2473 |
There are two cases: |
| 2474 |
|
| 2475 |
- RHS makes some value at least twice as wide. Store that value |
| 2476 |
in *NEW_RHS_OUT if so, and store its type in *TYPE_OUT. |
| 2477 |
|
| 2478 |
- RHS is an integer constant. Store that value in *NEW_RHS_OUT if so, |
| 2479 |
but leave *TYPE_OUT untouched. */ |
| 2480 |
|
| 2481 |
static bool |
| 2482 |
is_widening_mult_rhs_p (tree type, tree rhs, tree *type_out, |
| 2483 |
tree *new_rhs_out) |
| 2484 |
{ |
| 2485 |
gimple *stmt; |
| 2486 |
tree type1, rhs1; |
| 2487 |
|
| 2488 |
if (TREE_CODE (rhs) == SSA_NAME) |
| 2489 |
{ |
| 2490 |
/* Use tree_non_zero_bits to see if this operand is zero_extended |
| 2491 |
for unsigned widening multiplications or non-negative for |
| 2492 |
signed widening multiplications. */ |
| 2493 |
if (TREE_CODE (type) == INTEGER_TYPE |
| 2494 |
&& (TYPE_PRECISION (type) & 1) == 0 |
| 2495 |
&& int_mode_for_size (TYPE_PRECISION (type) / 2, 1).exists ()) |
| 2496 |
{ |
| 2497 |
unsigned int prec = TYPE_PRECISION (type); |
| 2498 |
unsigned int hprec = prec / 2; |
| 2499 |
wide_int bits = wide_int::from (tree_nonzero_bits (rhs), prec, |
| 2500 |
TYPE_SIGN (TREE_TYPE (rhs))); |
| 2501 |
if (TYPE_UNSIGNED (type) |
| 2502 |
&& wi::bit_and (bits, wi::mask (hprec, true, prec)) == 0) |
| 2503 |
{ |
| 2504 |
*type_out = build_nonstandard_integer_type (hprec, true); |
| 2505 |
/* X & MODE_MASK can be simplified to (T)X. */ |
| 2506 |
stmt = SSA_NAME_DEF_STMT (rhs); |
| 2507 |
if (is_gimple_assign (stmt) |
| 2508 |
&& gimple_assign_rhs_code (stmt) == BIT_AND_EXPR |
| 2509 |
&& TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST |
| 2510 |
&& wide_int::from (wi::to_wide (gimple_assign_rhs2 (stmt)), |
| 2511 |
prec, TYPE_SIGN (TREE_TYPE (rhs))) |
| 2512 |
== wi::mask (hprec, false, prec)) |
| 2513 |
*new_rhs_out = gimple_assign_rhs1 (stmt); |
| 2514 |
else |
| 2515 |
*new_rhs_out = rhs; |
| 2516 |
return true; |
| 2517 |
} |
| 2518 |
else if (!TYPE_UNSIGNED (type) |
| 2519 |
&& wi::bit_and (bits, wi::mask (hprec - 1, true, prec)) == 0) |
| 2520 |
{ |
| 2521 |
*type_out = build_nonstandard_integer_type (hprec, false); |
| 2522 |
*new_rhs_out = rhs; |
| 2523 |
return true; |
| 2524 |
} |
| 2525 |
} |
| 2526 |
|
| 2527 |
stmt = SSA_NAME_DEF_STMT (rhs); |
| 2528 |
if (is_gimple_assign (stmt)) |
| 2529 |
{ |
| 2530 |
|
| 2531 |
if (widening_mult_conversion_strippable_p (type, stmt)) |
| 2532 |
{ |
| 2533 |
rhs1 = gimple_assign_rhs1 (stmt); |
| 2534 |
|
| 2535 |
if (TREE_CODE (rhs1) == INTEGER_CST) |
| 2536 |
{ |
| 2537 |
*new_rhs_out = rhs1; |
| 2538 |
*type_out = NULL; |
| 2539 |
return true; |
| 2540 |
} |
| 2541 |
} |
| 2542 |
else |
| 2543 |
rhs1 = rhs; |
| 2544 |
} |
| 2545 |
else |
| 2546 |
rhs1 = rhs; |
| 2547 |
|
| 2548 |
type1 = TREE_TYPE (rhs1); |
| 2549 |
|
| 2550 |
if (TREE_CODE (type1) != TREE_CODE (type) |
| 2551 |
|| TYPE_PRECISION (type1) * 2 > TYPE_PRECISION (type)) |
| 2552 |
return false; |
| 2553 |
|
| 2554 |
*new_rhs_out = rhs1; |
| 2555 |
*type_out = type1; |
| 2556 |
return true; |
| 2557 |
} |
| 2558 |
|
| 2559 |
if (TREE_CODE (rhs) == INTEGER_CST) |
| 2560 |
{ |
| 2561 |
*new_rhs_out = rhs; |
| 2562 |
*type_out = NULL; |
| 2563 |
return true; |
| 2564 |
} |
| 2565 |
|
| 2566 |
return false; |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* Return true if STMT performs a widening multiplication, assuming the |
| 2570 |
output type is TYPE. If so, store the unwidened types of the operands |
| 2571 |
in *TYPE1_OUT and *TYPE2_OUT respectively. Also fill *RHS1_OUT and |
| 2572 |
*RHS2_OUT such that converting those operands to types *TYPE1_OUT |
| 2573 |
and *TYPE2_OUT would give the operands of the multiplication. */ |
| 2574 |
|
| 2575 |
static bool |
| 2576 |
is_widening_mult_p (gimple *stmt, |
| 2577 |
tree *type1_out, tree *rhs1_out, |
| 2578 |
tree *type2_out, tree *rhs2_out) |
| 2579 |
{ |
| 2580 |
tree type = TREE_TYPE (gimple_assign_lhs (stmt)); |
| 2581 |
|
| 2582 |
if (TREE_CODE (type) == INTEGER_TYPE) |
| 2583 |
{ |
| 2584 |
if (TYPE_OVERFLOW_TRAPS (type)) |
| 2585 |
return false; |
| 2586 |
} |
| 2587 |
else if (TREE_CODE (type) != FIXED_POINT_TYPE) |
| 2588 |
return false; |
| 2589 |
|
| 2590 |
if (!is_widening_mult_rhs_p (type, gimple_assign_rhs1 (stmt), type1_out, |
| 2591 |
rhs1_out)) |
| 2592 |
return false; |
| 2593 |
|
| 2594 |
if (!is_widening_mult_rhs_p (type, gimple_assign_rhs2 (stmt), type2_out, |
| 2595 |
rhs2_out)) |
| 2596 |
return false; |
| 2597 |
|
| 2598 |
if (*type1_out == NULL) |
| 2599 |
{ |
| 2600 |
if (*type2_out == NULL || !int_fits_type_p (*rhs1_out, *type2_out)) |
| 2601 |
return false; |
| 2602 |
*type1_out = *type2_out; |
| 2603 |
} |
| 2604 |
|
| 2605 |
if (*type2_out == NULL) |
| 2606 |
{ |
| 2607 |
if (!int_fits_type_p (*rhs2_out, *type1_out)) |
| 2608 |
return false; |
| 2609 |
*type2_out = *type1_out; |
| 2610 |
} |
| 2611 |
|
| 2612 |
/* Ensure that the larger of the two operands comes first. */ |
| 2613 |
if (TYPE_PRECISION (*type1_out) < TYPE_PRECISION (*type2_out)) |
| 2614 |
{ |
| 2615 |
std::swap (*type1_out, *type2_out); |
| 2616 |
std::swap (*rhs1_out, *rhs2_out); |
| 2617 |
} |
| 2618 |
|
| 2619 |
return true; |
| 2620 |
} |
| 2621 |
|
| 2622 |
/* Check to see if the CALL statement is an invocation of copysign |
| 2623 |
with 1. being the first argument. */ |
| 2624 |
static bool |
| 2625 |
is_copysign_call_with_1 (gimple *call) |
| 2626 |
{ |
| 2627 |
gcall *c = dyn_cast <gcall *> (call); |
| 2628 |
if (! c) |
| 2629 |
return false; |
| 2630 |
|
| 2631 |
enum combined_fn code = gimple_call_combined_fn (c); |
| 2632 |
|
| 2633 |
if (code == CFN_LAST) |
| 2634 |
return false; |
| 2635 |
|
| 2636 |
if (builtin_fn_p (code)) |
| 2637 |
{ |
| 2638 |
switch (as_builtin_fn (code)) |
| 2639 |
{ |
| 2640 |
CASE_FLT_FN (BUILT_IN_COPYSIGN): |
| 2641 |
CASE_FLT_FN_FLOATN_NX (BUILT_IN_COPYSIGN): |
| 2642 |
return real_onep (gimple_call_arg (c, 0)); |
| 2643 |
default: |
| 2644 |
return false; |
| 2645 |
} |
| 2646 |
} |
| 2647 |
|
| 2648 |
if (internal_fn_p (code)) |
| 2649 |
{ |
| 2650 |
switch (as_internal_fn (code)) |
| 2651 |
{ |
| 2652 |
case IFN_COPYSIGN: |
| 2653 |
return real_onep (gimple_call_arg (c, 0)); |
| 2654 |
default: |
| 2655 |
return false; |
| 2656 |
} |
| 2657 |
} |
| 2658 |
|
| 2659 |
return false; |
| 2660 |
} |
| 2661 |
|
| 2662 |
/* Try to expand the pattern x * copysign (1, y) into xorsign (x, y). |
| 2663 |
This only happens when the xorsign optab is defined, if the |
| 2664 |
pattern is not a xorsign pattern or if expansion fails FALSE is |
| 2665 |
returned, otherwise TRUE is returned. */ |
| 2666 |
static bool |
| 2667 |
convert_expand_mult_copysign (gimple *stmt, gimple_stmt_iterator *gsi) |
| 2668 |
{ |
| 2669 |
tree treeop0, treeop1, lhs, type; |
| 2670 |
location_t loc = gimple_location (stmt); |
| 2671 |
lhs = gimple_assign_lhs (stmt); |
| 2672 |
treeop0 = gimple_assign_rhs1 (stmt); |
| 2673 |
treeop1 = gimple_assign_rhs2 (stmt); |
| 2674 |
type = TREE_TYPE (lhs); |
| 2675 |
machine_mode mode = TYPE_MODE (type); |
| 2676 |
|
| 2677 |
if (HONOR_SNANS (type)) |
| 2678 |
return false; |
| 2679 |
|
| 2680 |
if (TREE_CODE (treeop0) == SSA_NAME && TREE_CODE (treeop1) == SSA_NAME) |
| 2681 |
{ |
| 2682 |
gimple *call0 = SSA_NAME_DEF_STMT (treeop0); |
| 2683 |
if (!has_single_use (treeop0) || !is_copysign_call_with_1 (call0)) |
| 2684 |
{ |
| 2685 |
call0 = SSA_NAME_DEF_STMT (treeop1); |
| 2686 |
if (!has_single_use (treeop1) || !is_copysign_call_with_1 (call0)) |
| 2687 |
return false; |
| 2688 |
|
| 2689 |
treeop1 = treeop0; |
| 2690 |
} |
| 2691 |
if (optab_handler (xorsign_optab, mode) == CODE_FOR_nothing) |
| 2692 |
return false; |
| 2693 |
|
| 2694 |
gcall *c = as_a<gcall*> (call0); |
| 2695 |
treeop0 = gimple_call_arg (c, 1); |
| 2696 |
|
| 2697 |
gcall *call_stmt |
| 2698 |
= gimple_build_call_internal (IFN_XORSIGN, 2, treeop1, treeop0); |
| 2699 |
gimple_set_lhs (call_stmt, lhs); |
| 2700 |
gimple_set_location (call_stmt, loc); |
| 2701 |
gsi_replace (gsi, call_stmt, true); |
| 2702 |
return true; |
| 2703 |
} |
| 2704 |
|
| 2705 |
return false; |
| 2706 |
} |
| 2707 |
|
| 2708 |
/* Process a single gimple statement STMT, which has a MULT_EXPR as |
| 2709 |
its rhs, and try to convert it into a WIDEN_MULT_EXPR. The return |
| 2710 |
value is true iff we converted the statement. */ |
| 2711 |
|
| 2712 |
static bool |
| 2713 |
convert_mult_to_widen (gimple *stmt, gimple_stmt_iterator *gsi) |
| 2714 |
{ |
| 2715 |
tree lhs, rhs1, rhs2, type, type1, type2; |
| 2716 |
enum insn_code handler; |
| 2717 |
scalar_int_mode to_mode, from_mode, actual_mode; |
| 2718 |
optab op; |
| 2719 |
int actual_precision; |
| 2720 |
location_t loc = gimple_location (stmt); |
| 2721 |
bool from_unsigned1, from_unsigned2; |
| 2722 |
|
| 2723 |
lhs = gimple_assign_lhs (stmt); |
| 2724 |
type = TREE_TYPE (lhs); |
| 2725 |
if (TREE_CODE (type) != INTEGER_TYPE) |
| 2726 |
return false; |
| 2727 |
|
| 2728 |
if (!is_widening_mult_p (stmt, &type1, &rhs1, &type2, &rhs2)) |
| 2729 |
return false; |
| 2730 |
|
| 2731 |
/* if any one of rhs1 and rhs2 is subject to abnormal coalescing, |
| 2732 |
avoid the tranform. */ |
| 2733 |
if ((TREE_CODE (rhs1) == SSA_NAME |
| 2734 |
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)) |
| 2735 |
|| (TREE_CODE (rhs2) == SSA_NAME |
| 2736 |
&& SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2))) |
| 2737 |
return false; |
| 2738 |
|
| 2739 |
to_mode = SCALAR_INT_TYPE_MODE (type); |
| 2740 |
from_mode = SCALAR_INT_TYPE_MODE (type1); |
| 2741 |
if (to_mode == from_mode) |
| 2742 |
return false; |
| 2743 |
|
| 2744 |
from_unsigned1 = TYPE_UNSIGNED (type1); |
| 2745 |
from_unsigned2 = TYPE_UNSIGNED (type2); |
| 2746 |
|
| 2747 |
if (from_unsigned1 && from_unsigned2) |
| 2748 |
op = umul_widen_optab; |
| 2749 |
else if (!from_unsigned1 && !from_unsigned2) |
| 2750 |
op = smul_widen_optab; |
| 2751 |
else |
| 2752 |
op = usmul_widen_optab; |
| 2753 |
|
| 2754 |
handler = find_widening_optab_handler_and_mode (op, to_mode, from_mode, |
| 2755 |
&actual_mode); |
| 2756 |
|
| 2757 |
if (handler == CODE_FOR_nothing) |
| 2758 |
{ |
| 2759 |
if (op != smul_widen_optab) |
| 2760 |
{ |
| 2761 |
/* We can use a signed multiply with unsigned types as long as |
| 2762 |
there is a wider mode to use, or it is the smaller of the two |
| 2763 |
types that is unsigned. Note that type1 >= type2, always. */ |
| 2764 |
if ((TYPE_UNSIGNED (type1) |
| 2765 |
&& TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode)) |
| 2766 |
|| (TYPE_UNSIGNED (type2) |
| 2767 |
&& TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode))) |
| 2768 |
{ |
| 2769 |
if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode) |
| 2770 |
|| GET_MODE_SIZE (to_mode) <= GET_MODE_SIZE (from_mode)) |
| 2771 |
return false; |
| 2772 |
} |
| 2773 |
|
| 2774 |
op = smul_widen_optab; |
| 2775 |
handler = find_widening_optab_handler_and_mode (op, to_mode, |
| 2776 |
from_mode, |
| 2777 |
&actual_mode); |
| 2778 |
|
| 2779 |
if (handler == CODE_FOR_nothing) |
| 2780 |
return false; |
| 2781 |
|
| 2782 |
from_unsigned1 = from_unsigned2 = false; |
| 2783 |
} |
| 2784 |
else |
| 2785 |
{ |
| 2786 |
/* Expand can synthesize smul_widen_optab if the target |
| 2787 |
supports umul_widen_optab. */ |
| 2788 |
op = umul_widen_optab; |
| 2789 |
handler = find_widening_optab_handler_and_mode (op, to_mode, |
| 2790 |
from_mode, |
| 2791 |
&actual_mode); |
| 2792 |
if (handler == CODE_FOR_nothing) |
| 2793 |
return false; |
| 2794 |
} |
| 2795 |
} |
| 2796 |
|
| 2797 |
/* Ensure that the inputs to the handler are in the correct precison |
| 2798 |
for the opcode. This will be the full mode size. */ |
| 2799 |
actual_precision = GET_MODE_PRECISION (actual_mode); |
| 2800 |
if (2 * actual_precision > TYPE_PRECISION (type)) |
| 2801 |
return false; |
| 2802 |
if (actual_precision != TYPE_PRECISION (type1) |
| 2803 |
|| from_unsigned1 != TYPE_UNSIGNED (type1)) |
| 2804 |
{ |
| 2805 |
if (!useless_type_conversion_p (type1, TREE_TYPE (rhs1))) |
| 2806 |
{ |
| 2807 |
if (TREE_CODE (rhs1) == INTEGER_CST) |
| 2808 |
rhs1 = fold_convert (type1, rhs1); |
| 2809 |
else |
| 2810 |
rhs1 = build_and_insert_cast (gsi, loc, type1, rhs1); |
| 2811 |
} |
| 2812 |
type1 = build_nonstandard_integer_type (actual_precision, |
| 2813 |
from_unsigned1); |
| 2814 |
} |
| 2815 |
if (!useless_type_conversion_p (type1, TREE_TYPE (rhs1))) |
| 2816 |
{ |
| 2817 |
if (TREE_CODE (rhs1) == INTEGER_CST) |
| 2818 |
rhs1 = fold_convert (type1, rhs1); |
| 2819 |
else |
| 2820 |
rhs1 = build_and_insert_cast (gsi, loc, type1, rhs1); |
| 2821 |
} |
| 2822 |
if (actual_precision != TYPE_PRECISION (type2) |
| 2823 |
|| from_unsigned2 != TYPE_UNSIGNED (type2)) |
| 2824 |
{ |
| 2825 |
if (!useless_type_conversion_p (type2, TREE_TYPE (rhs2))) |
| 2826 |
{ |
| 2827 |
if (TREE_CODE (rhs2) == INTEGER_CST) |
| 2828 |
rhs2 = fold_convert (type2, rhs2); |
| 2829 |
else |
| 2830 |
rhs2 = build_and_insert_cast (gsi, loc, type2, rhs2); |
| 2831 |
} |
| 2832 |
type2 = build_nonstandard_integer_type (actual_precision, |
| 2833 |
from_unsigned2); |
| 2834 |
} |
| 2835 |
if (!useless_type_conversion_p (type2, TREE_TYPE (rhs2))) |
| 2836 |
{ |
| 2837 |
if (TREE_CODE (rhs2) == INTEGER_CST) |
| 2838 |
rhs2 = fold_convert (type2, rhs2); |
| 2839 |
else |
| 2840 |
rhs2 = build_and_insert_cast (gsi, loc, type2, rhs2); |
| 2841 |
} |
| 2842 |
|
| 2843 |
gimple_assign_set_rhs1 (stmt, rhs1); |
| 2844 |
gimple_assign_set_rhs2 (stmt, rhs2); |
| 2845 |
gimple_assign_set_rhs_code (stmt, WIDEN_MULT_EXPR); |
| 2846 |
update_stmt (stmt); |
| 2847 |
widen_mul_stats.widen_mults_inserted++; |
| 2848 |
return true; |
| 2849 |
} |
| 2850 |
|
| 2851 |
/* Process a single gimple statement STMT, which is found at the |
| 2852 |
iterator GSI and has a either a PLUS_EXPR or a MINUS_EXPR as its |
| 2853 |
rhs (given by CODE), and try to convert it into a |
| 2854 |
WIDEN_MULT_PLUS_EXPR or a WIDEN_MULT_MINUS_EXPR. The return value |
| 2855 |
is true iff we converted the statement. */ |
| 2856 |
|
| 2857 |
static bool |
| 2858 |
convert_plusminus_to_widen (gimple_stmt_iterator *gsi, gimple *stmt, |
| 2859 |
enum tree_code code) |
| 2860 |
{ |
| 2861 |
gimple *rhs1_stmt = NULL, *rhs2_stmt = NULL; |
| 2862 |
gimple *conv1_stmt = NULL, *conv2_stmt = NULL, *conv_stmt; |
| 2863 |
tree type, type1, type2, optype; |
| 2864 |
tree lhs, rhs1, rhs2, mult_rhs1, mult_rhs2, add_rhs; |
| 2865 |
enum tree_code rhs1_code = ERROR_MARK, rhs2_code = ERROR_MARK; |
| 2866 |
optab this_optab; |
| 2867 |
enum tree_code wmult_code; |
| 2868 |
enum insn_code handler; |
| 2869 |
scalar_mode to_mode, from_mode, actual_mode; |
| 2870 |
location_t loc = gimple_location (stmt); |
| 2871 |
int actual_precision; |
| 2872 |
bool from_unsigned1, from_unsigned2; |
| 2873 |
|
| 2874 |
lhs = gimple_assign_lhs (stmt); |
| 2875 |
type = TREE_TYPE (lhs); |
| 2876 |
if ((TREE_CODE (type) != INTEGER_TYPE |
| 2877 |
&& TREE_CODE (type) != FIXED_POINT_TYPE) |
| 2878 |
|| !type_has_mode_precision_p (type)) |
| 2879 |
return false; |
| 2880 |
|
| 2881 |
if (code == MINUS_EXPR) |
| 2882 |
wmult_code = WIDEN_MULT_MINUS_EXPR; |
| 2883 |
else |
| 2884 |
wmult_code = WIDEN_MULT_PLUS_EXPR; |
| 2885 |
|
| 2886 |
rhs1 = gimple_assign_rhs1 (stmt); |
| 2887 |
rhs2 = gimple_assign_rhs2 (stmt); |
| 2888 |
|
| 2889 |
if (TREE_CODE (rhs1) == SSA_NAME) |
| 2890 |
{ |
| 2891 |
rhs1_stmt = SSA_NAME_DEF_STMT (rhs1); |
| 2892 |
if (is_gimple_assign (rhs1_stmt)) |
| 2893 |
rhs1_code = gimple_assign_rhs_code (rhs1_stmt); |
| 2894 |
} |
| 2895 |
|
| 2896 |
if (TREE_CODE (rhs2) == SSA_NAME) |
| 2897 |
{ |
| 2898 |
rhs2_stmt = SSA_NAME_DEF_STMT (rhs2); |
| 2899 |
if (is_gimple_assign (rhs2_stmt)) |
| 2900 |
rhs2_code = gimple_assign_rhs_code (rhs2_stmt); |
| 2901 |
} |
| 2902 |
|
| 2903 |
/* Allow for one conversion statement between the multiply |
| 2904 |
and addition/subtraction statement. If there are more than |
| 2905 |
one conversions then we assume they would invalidate this |
| 2906 |
transformation. If that's not the case then they should have |
| 2907 |
been folded before now. */ |
| 2908 |
if (CONVERT_EXPR_CODE_P (rhs1_code)) |
| 2909 |
{ |
| 2910 |
conv1_stmt = rhs1_stmt; |
| 2911 |
rhs1 = gimple_assign_rhs1 (rhs1_stmt); |
| 2912 |
if (TREE_CODE (rhs1) == SSA_NAME) |
| 2913 |
{ |
| 2914 |
rhs1_stmt = SSA_NAME_DEF_STMT (rhs1); |
| 2915 |
if (is_gimple_assign (rhs1_stmt)) |
| 2916 |
rhs1_code = gimple_assign_rhs_code (rhs1_stmt); |
| 2917 |
} |
| 2918 |
else |
| 2919 |
return false; |
| 2920 |
} |
| 2921 |
if (CONVERT_EXPR_CODE_P (rhs2_code)) |
| 2922 |
{ |
| 2923 |
conv2_stmt = rhs2_stmt; |
| 2924 |
rhs2 = gimple_assign_rhs1 (rhs2_stmt); |
| 2925 |
if (TREE_CODE (rhs2) == SSA_NAME) |
| 2926 |
{ |
| 2927 |
rhs2_stmt = SSA_NAME_DEF_STMT (rhs2); |
| 2928 |
if (is_gimple_assign (rhs2_stmt)) |
| 2929 |
rhs2_code = gimple_assign_rhs_code (rhs2_stmt); |
| 2930 |
} |
| 2931 |
else |
| 2932 |
return false; |
| 2933 |
} |
| 2934 |
|
| 2935 |
/* If code is WIDEN_MULT_EXPR then it would seem unnecessary to call |
| 2936 |
is_widening_mult_p, but we still need the rhs returns. |
| 2937 |
|
| 2938 |
It might also appear that it would be sufficient to use the existing |
| 2939 |
operands of the widening multiply, but that would limit the choice of |
| 2940 |
multiply-and-accumulate instructions. |
| 2941 |
|
| 2942 |
If the widened-multiplication result has more than one uses, it is |
| 2943 |
probably wiser not to do the conversion. Also restrict this operation |
| 2944 |
to single basic block to avoid moving the multiply to a different block |
| 2945 |
with a higher execution frequency. */ |
| 2946 |
if (code == PLUS_EXPR |
| 2947 |
&& (rhs1_code == MULT_EXPR || rhs1_code == WIDEN_MULT_EXPR)) |
| 2948 |
{ |
| 2949 |
if (!has_single_use (rhs1) |
| 2950 |
|| gimple_bb (rhs1_stmt) != gimple_bb (stmt) |
| 2951 |
|| !is_widening_mult_p (rhs1_stmt, &type1, &mult_rhs1, |
| 2952 |
&type2, &mult_rhs2)) |
| 2953 |
return false; |
| 2954 |
add_rhs = rhs2; |
| 2955 |
conv_stmt = conv1_stmt; |
| 2956 |
} |
| 2957 |
else if (rhs2_code == MULT_EXPR || rhs2_code == WIDEN_MULT_EXPR) |
| 2958 |
{ |
| 2959 |
if (!has_single_use (rhs2) |
| 2960 |
|| gimple_bb (rhs2_stmt) != gimple_bb (stmt) |
| 2961 |
|| !is_widening_mult_p (rhs2_stmt, &type1, &mult_rhs1, |
| 2962 |
&type2, &mult_rhs2)) |
| 2963 |
return false; |
| 2964 |
add_rhs = rhs1; |
| 2965 |
conv_stmt = conv2_stmt; |
| 2966 |
} |
| 2967 |
else |
| 2968 |
return false; |
| 2969 |
|
| 2970 |
to_mode = SCALAR_TYPE_MODE (type); |
| 2971 |
from_mode = SCALAR_TYPE_MODE (type1); |
| 2972 |
if (to_mode == from_mode) |
| 2973 |
return false; |
| 2974 |
|
| 2975 |
/* For fixed point types, the mode classes could be different |
| 2976 |
so reject that case. */ |
| 2977 |
if (GET_MODE_CLASS (from_mode) != GET_MODE_CLASS (to_mode)) |
| 2978 |
return false; |
| 2979 |
|
| 2980 |
from_unsigned1 = TYPE_UNSIGNED (type1); |
| 2981 |
from_unsigned2 = TYPE_UNSIGNED (type2); |
| 2982 |
optype = type1; |
| 2983 |
|
| 2984 |
/* There's no such thing as a mixed sign madd yet, so use a wider mode. */ |
| 2985 |
if (from_unsigned1 != from_unsigned2) |
| 2986 |
{ |
| 2987 |
if (!INTEGRAL_TYPE_P (type)) |
| 2988 |
return false; |
| 2989 |
/* We can use a signed multiply with unsigned types as long as |
| 2990 |
there is a wider mode to use, or it is the smaller of the two |
| 2991 |
types that is unsigned. Note that type1 >= type2, always. */ |
| 2992 |
if ((from_unsigned1 |
| 2993 |
&& TYPE_PRECISION (type1) == GET_MODE_PRECISION (from_mode)) |
| 2994 |
|| (from_unsigned2 |
| 2995 |
&& TYPE_PRECISION (type2) == GET_MODE_PRECISION (from_mode))) |
| 2996 |
{ |
| 2997 |
if (!GET_MODE_WIDER_MODE (from_mode).exists (&from_mode) |
| 2998 |
|| GET_MODE_SIZE (from_mode) >= GET_MODE_SIZE (to_mode)) |
| 2999 |
return false; |
| 3000 |
} |
| 3001 |
|
| 3002 |
from_unsigned1 = from_unsigned2 = false; |
| 3003 |
optype = build_nonstandard_integer_type (GET_MODE_PRECISION (from_mode), |
| 3004 |
false); |
| 3005 |
} |
| 3006 |
|
| 3007 |
/* If there was a conversion between the multiply and addition |
| 3008 |
then we need to make sure it fits a multiply-and-accumulate. |
| 3009 |
The should be a single mode change which does not change the |
| 3010 |
value. */ |
| 3011 |
if (conv_stmt) |
| 3012 |
{ |
| 3013 |
/* We use the original, unmodified data types for this. */ |
| 3014 |
tree from_type = TREE_TYPE (gimple_assign_rhs1 (conv_stmt)); |
| 3015 |
tree to_type = TREE_TYPE (gimple_assign_lhs (conv_stmt)); |
| 3016 |
int data_size = TYPE_PRECISION (type1) + TYPE_PRECISION (type2); |
| 3017 |
bool is_unsigned = TYPE_UNSIGNED (type1) && TYPE_UNSIGNED (type2); |
| 3018 |
|
| 3019 |
if (TYPE_PRECISION (from_type) > TYPE_PRECISION (to_type)) |
| 3020 |
{ |
| 3021 |
/* Conversion is a truncate. */ |
| 3022 |
if (TYPE_PRECISION (to_type) < data_size) |
| 3023 |
return false; |
| 3024 |
} |
| 3025 |
else if (TYPE_PRECISION (from_type) < TYPE_PRECISION (to_type)) |
| 3026 |
{ |
| 3027 |
/* Conversion is an extend. Check it's the right sort. */ |
| 3028 |
if (TYPE_UNSIGNED (from_type) != is_unsigned |
| 3029 |
&& !(is_unsigned && TYPE_PRECISION (from_type) > data_size)) |
| 3030 |
return false; |
| 3031 |
} |
| 3032 |
/* else convert is a no-op for our purposes. */ |
| 3033 |
} |
| 3034 |
|
| 3035 |
/* Verify that the machine can perform a widening multiply |
| 3036 |
accumulate in this mode/signedness combination, otherwise |
| 3037 |
this transformation is likely to pessimize code. */ |
| 3038 |
this_optab = optab_for_tree_code (wmult_code, optype, optab_default); |
| 3039 |
handler = find_widening_optab_handler_and_mode (this_optab, to_mode, |
| 3040 |
from_mode, &actual_mode); |
| 3041 |
|
| 3042 |
if (handler == CODE_FOR_nothing) |
| 3043 |
return false; |
| 3044 |
|
| 3045 |
/* Ensure that the inputs to the handler are in the correct precison |
| 3046 |
for the opcode. This will be the full mode size. */ |
| 3047 |
actual_precision = GET_MODE_PRECISION (actual_mode); |
| 3048 |
if (actual_precision != TYPE_PRECISION (type1) |
| 3049 |
|| from_unsigned1 != TYPE_UNSIGNED (type1)) |
| 3050 |
{ |
| 3051 |
if (!useless_type_conversion_p (type1, TREE_TYPE (mult_rhs1))) |
| 3052 |
{ |
| 3053 |
if (TREE_CODE (mult_rhs1) == INTEGER_CST) |
| 3054 |
mult_rhs1 = fold_convert (type1, mult_rhs1); |
| 3055 |
else |
| 3056 |
mult_rhs1 = build_and_insert_cast (gsi, loc, type1, mult_rhs1); |
| 3057 |
} |
| 3058 |
type1 = build_nonstandard_integer_type (actual_precision, |
| 3059 |
from_unsigned1); |
| 3060 |
} |
| 3061 |
if (!useless_type_conversion_p (type1, TREE_TYPE (mult_rhs1))) |
| 3062 |
{ |
| 3063 |
if (TREE_CODE (mult_rhs1) == INTEGER_CST) |
| 3064 |
mult_rhs1 = fold_convert (type1, mult_rhs1); |
| 3065 |
else |
| 3066 |
mult_rhs1 = build_and_insert_cast (gsi, loc, type1, mult_rhs1); |
| 3067 |
} |
| 3068 |
if (actual_precision != TYPE_PRECISION (type2) |
| 3069 |
|| from_unsigned2 != TYPE_UNSIGNED (type2)) |
| 3070 |
{ |
| 3071 |
if (!useless_type_conversion_p (type2, TREE_TYPE (mult_rhs2))) |
| 3072 |
{ |
| 3073 |
if (TREE_CODE (mult_rhs2) == INTEGER_CST) |
| 3074 |
mult_rhs2 = fold_convert (type2, mult_rhs2); |
| 3075 |
else |
| 3076 |
mult_rhs2 = build_and_insert_cast (gsi, loc, type2, mult_rhs2); |
| 3077 |
} |
| 3078 |
type2 = build_nonstandard_integer_type (actual_precision, |
| 3079 |
from_unsigned2); |
| 3080 |
} |
| 3081 |
if (!useless_type_conversion_p (type2, TREE_TYPE (mult_rhs2))) |
| 3082 |
{ |
| 3083 |
if (TREE_CODE (mult_rhs2) == INTEGER_CST) |
| 3084 |
mult_rhs2 = fold_convert (type2, mult_rhs2); |
| 3085 |
else |
| 3086 |
mult_rhs2 = build_and_insert_cast (gsi, loc, type2, mult_rhs2); |
| 3087 |
} |
| 3088 |
|
| 3089 |
if (!useless_type_conversion_p (type, TREE_TYPE (add_rhs))) |
| 3090 |
add_rhs = build_and_insert_cast (gsi, loc, type, add_rhs); |
| 3091 |
|
| 3092 |
gimple_assign_set_rhs_with_ops (gsi, wmult_code, mult_rhs1, mult_rhs2, |
| 3093 |
add_rhs); |
| 3094 |
update_stmt (gsi_stmt (*gsi)); |
| 3095 |
widen_mul_stats.maccs_inserted++; |
| 3096 |
return true; |
| 3097 |
} |
| 3098 |
|
| 3099 |
/* Given a result MUL_RESULT which is a result of a multiplication of OP1 and |
| 3100 |
OP2 and which we know is used in statements that can be, together with the |
| 3101 |
multiplication, converted to FMAs, perform the transformation. */ |
| 3102 |
|
| 3103 |
static void |
| 3104 |
convert_mult_to_fma_1 (tree mul_result, tree op1, tree op2) |
| 3105 |
{ |
| 3106 |
tree type = TREE_TYPE (mul_result); |
| 3107 |
gimple *use_stmt; |
| 3108 |
imm_use_iterator imm_iter; |
| 3109 |
gcall *fma_stmt; |
| 3110 |
|
| 3111 |
FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, mul_result) |
| 3112 |
{ |
| 3113 |
gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
| 3114 |
tree addop, mulop1 = op1, result = mul_result; |
| 3115 |
bool negate_p = false; |
| 3116 |
gimple_seq seq = NULL; |
| 3117 |
|
| 3118 |
if (is_gimple_debug (use_stmt)) |
| 3119 |
continue; |
| 3120 |
|
| 3121 |
if (is_gimple_assign (use_stmt) |
| 3122 |
&& gimple_assign_rhs_code (use_stmt) == NEGATE_EXPR) |
| 3123 |
{ |
| 3124 |
result = gimple_assign_lhs (use_stmt); |
| 3125 |
use_operand_p use_p; |
| 3126 |
gimple *neguse_stmt; |
| 3127 |
single_imm_use (gimple_assign_lhs (use_stmt), &use_p, &neguse_stmt); |
| 3128 |
gsi_remove (&gsi, true); |
| 3129 |
release_defs (use_stmt); |
| 3130 |
|
| 3131 |
use_stmt = neguse_stmt; |
| 3132 |
gsi = gsi_for_stmt (use_stmt); |
| 3133 |
negate_p = true; |
| 3134 |
} |
| 3135 |
|
| 3136 |
tree cond, else_value, ops[3], len, bias; |
| 3137 |
tree_code code; |
| 3138 |
if (!can_interpret_as_conditional_op_p (use_stmt, &cond, &code, |
| 3139 |
ops, &else_value, |
| 3140 |
&len, &bias)) |
| 3141 |
gcc_unreachable (); |
| 3142 |
addop = ops[0] == result ? ops[1] : ops[0]; |
| 3143 |
|
| 3144 |
if (code == MINUS_EXPR) |
| 3145 |
{ |
| 3146 |
if (ops[0] == result) |
| 3147 |
/* a * b - c -> a * b + (-c) */ |
| 3148 |
addop = gimple_build (&seq, NEGATE_EXPR, type, addop); |
| 3149 |
else |
| 3150 |
/* a - b * c -> (-b) * c + a */ |
| 3151 |
negate_p = !negate_p; |
| 3152 |
} |
| 3153 |
|
| 3154 |
if (negate_p) |
| 3155 |
mulop1 = gimple_build (&seq, NEGATE_EXPR, type, mulop1); |
| 3156 |
|
| 3157 |
if (seq) |
| 3158 |
gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT); |
| 3159 |
|
| 3160 |
if (len) |
| 3161 |
fma_stmt |
| 3162 |
= gimple_build_call_internal (IFN_COND_LEN_FMA, 7, cond, mulop1, op2, |
| 3163 |
addop, else_value, len, bias); |
| 3164 |
else if (cond) |
| 3165 |
fma_stmt = gimple_build_call_internal (IFN_COND_FMA, 5, cond, mulop1, |
| 3166 |
op2, addop, else_value); |
| 3167 |
else |
| 3168 |
fma_stmt = gimple_build_call_internal (IFN_FMA, 3, mulop1, op2, addop); |
| 3169 |
gimple_set_lhs (fma_stmt, gimple_get_lhs (use_stmt)); |
| 3170 |
gimple_call_set_nothrow (fma_stmt, !stmt_can_throw_internal (cfun, |
| 3171 |
use_stmt)); |
| 3172 |
gsi_replace (&gsi, fma_stmt, true); |
| 3173 |
/* Follow all SSA edges so that we generate FMS, FNMA and FNMS |
| 3174 |
regardless of where the negation occurs. */ |
| 3175 |
gimple *orig_stmt = gsi_stmt (gsi); |
| 3176 |
if (fold_stmt (&gsi, follow_all_ssa_edges)) |
| 3177 |
{ |
| 3178 |
if (maybe_clean_or_replace_eh_stmt (orig_stmt, gsi_stmt (gsi))) |
| 3179 |
gcc_unreachable (); |
| 3180 |
update_stmt (gsi_stmt (gsi)); |
| 3181 |
} |
| 3182 |
|
| 3183 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3184 |
{ |
| 3185 |
fprintf (dump_file, "Generated FMA "); |
| 3186 |
print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, TDF_NONE); |
| 3187 |
fprintf (dump_file, "\n"); |
| 3188 |
} |
| 3189 |
|
| 3190 |
/* If the FMA result is negated in a single use, fold the negation |
| 3191 |
too. */ |
| 3192 |
orig_stmt = gsi_stmt (gsi); |
| 3193 |
use_operand_p use_p; |
| 3194 |
gimple *neg_stmt; |
| 3195 |
if (is_gimple_call (orig_stmt) |
| 3196 |
&& gimple_call_internal_p (orig_stmt) |
| 3197 |
&& gimple_call_lhs (orig_stmt) |
| 3198 |
&& TREE_CODE (gimple_call_lhs (orig_stmt)) == SSA_NAME |
| 3199 |
&& single_imm_use (gimple_call_lhs (orig_stmt), &use_p, &neg_stmt) |
| 3200 |
&& is_gimple_assign (neg_stmt) |
| 3201 |
&& gimple_assign_rhs_code (neg_stmt) == NEGATE_EXPR |
| 3202 |
&& !stmt_could_throw_p (cfun, neg_stmt)) |
| 3203 |
{ |
| 3204 |
gsi = gsi_for_stmt (neg_stmt); |
| 3205 |
if (fold_stmt (&gsi, follow_all_ssa_edges)) |
| 3206 |
{ |
| 3207 |
if (maybe_clean_or_replace_eh_stmt (neg_stmt, gsi_stmt (gsi))) |
| 3208 |
gcc_unreachable (); |
| 3209 |
update_stmt (gsi_stmt (gsi)); |
| 3210 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3211 |
{ |
| 3212 |
fprintf (dump_file, "Folded FMA negation "); |
| 3213 |
print_gimple_stmt (dump_file, gsi_stmt (gsi), 0, TDF_NONE); |
| 3214 |
fprintf (dump_file, "\n"); |
| 3215 |
} |
| 3216 |
} |
| 3217 |
} |
| 3218 |
|
| 3219 |
widen_mul_stats.fmas_inserted++; |
| 3220 |
} |
| 3221 |
} |
| 3222 |
|
| 3223 |
/* Data necessary to perform the actual transformation from a multiplication |
| 3224 |
and an addition to an FMA after decision is taken it should be done and to |
| 3225 |
then delete the multiplication statement from the function IL. */ |
| 3226 |
|
| 3227 |
struct fma_transformation_info |
| 3228 |
{ |
| 3229 |
gimple *mul_stmt; |
| 3230 |
tree mul_result; |
| 3231 |
tree op1; |
| 3232 |
tree op2; |
| 3233 |
}; |
| 3234 |
|
| 3235 |
/* Structure containing the current state of FMA deferring, i.e. whether we are |
| 3236 |
deferring, whether to continue deferring, and all data necessary to come |
| 3237 |
back and perform all deferred transformations. */ |
| 3238 |
|
| 3239 |
class fma_deferring_state |
| 3240 |
{ |
| 3241 |
public: |
| 3242 |
/* Class constructor. Pass true as PERFORM_DEFERRING in order to actually |
| 3243 |
do any deferring. */ |
| 3244 |
|
| 3245 |
fma_deferring_state (bool perform_deferring) |
| 3246 |
: m_candidates (), m_mul_result_set (), m_initial_phi (NULL), |
| 3247 |
m_last_result (NULL_TREE), m_deferring_p (perform_deferring) {} |
| 3248 |
|
| 3249 |
/* List of FMA candidates for which we the transformation has been determined |
| 3250 |
possible but we at this point in BB analysis we do not consider them |
| 3251 |
beneficial. */ |
| 3252 |
auto_vec<fma_transformation_info, 8> m_candidates; |
| 3253 |
|
| 3254 |
/* Set of results of multiplication that are part of an already deferred FMA |
| 3255 |
candidates. */ |
| 3256 |
hash_set<tree> m_mul_result_set; |
| 3257 |
|
| 3258 |
/* The PHI that supposedly feeds back result of a FMA to another over loop |
| 3259 |
boundary. */ |
| 3260 |
gphi *m_initial_phi; |
| 3261 |
|
| 3262 |
/* Result of the last produced FMA candidate or NULL if there has not been |
| 3263 |
one. */ |
| 3264 |
tree m_last_result; |
| 3265 |
|
| 3266 |
/* If true, deferring might still be profitable. If false, transform all |
| 3267 |
candidates and no longer defer. */ |
| 3268 |
bool m_deferring_p; |
| 3269 |
}; |
| 3270 |
|
| 3271 |
/* Transform all deferred FMA candidates and mark STATE as no longer |
| 3272 |
deferring. */ |
| 3273 |
|
| 3274 |
static void |
| 3275 |
cancel_fma_deferring (fma_deferring_state *state) |
| 3276 |
{ |
| 3277 |
if (!state->m_deferring_p) |
| 3278 |
return; |
| 3279 |
|
| 3280 |
for (unsigned i = 0; i < state->m_candidates.length (); i++) |
| 3281 |
{ |
| 3282 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3283 |
fprintf (dump_file, "Generating deferred FMA\n"); |
| 3284 |
|
| 3285 |
const fma_transformation_info &fti = state->m_candidates[i]; |
| 3286 |
convert_mult_to_fma_1 (fti.mul_result, fti.op1, fti.op2); |
| 3287 |
|
| 3288 |
gimple_stmt_iterator gsi = gsi_for_stmt (fti.mul_stmt); |
| 3289 |
gsi_remove (&gsi, true); |
| 3290 |
release_defs (fti.mul_stmt); |
| 3291 |
} |
| 3292 |
state->m_deferring_p = false; |
| 3293 |
} |
| 3294 |
|
| 3295 |
/* If OP is an SSA name defined by a PHI node, return the PHI statement. |
| 3296 |
Otherwise return NULL. */ |
| 3297 |
|
| 3298 |
static gphi * |
| 3299 |
result_of_phi (tree op) |
| 3300 |
{ |
| 3301 |
if (TREE_CODE (op) != SSA_NAME) |
| 3302 |
return NULL; |
| 3303 |
|
| 3304 |
return dyn_cast <gphi *> (SSA_NAME_DEF_STMT (op)); |
| 3305 |
} |
| 3306 |
|
| 3307 |
/* After processing statements of a BB and recording STATE, return true if the |
| 3308 |
initial phi is fed by the last FMA candidate result ore one such result from |
| 3309 |
previously processed BBs marked in LAST_RESULT_SET. */ |
| 3310 |
|
| 3311 |
static bool |
| 3312 |
last_fma_candidate_feeds_initial_phi (fma_deferring_state *state, |
| 3313 |
hash_set<tree> *last_result_set) |
| 3314 |
{ |
| 3315 |
ssa_op_iter iter; |
| 3316 |
use_operand_p use; |
| 3317 |
FOR_EACH_PHI_ARG (use, state->m_initial_phi, iter, SSA_OP_USE) |
| 3318 |
{ |
| 3319 |
tree t = USE_FROM_PTR (use); |
| 3320 |
if (t == state->m_last_result |
| 3321 |
|| last_result_set->contains (t)) |
| 3322 |
return true; |
| 3323 |
} |
| 3324 |
|
| 3325 |
return false; |
| 3326 |
} |
| 3327 |
|
| 3328 |
/* Combine the multiplication at MUL_STMT with operands MULOP1 and MULOP2 |
| 3329 |
with uses in additions and subtractions to form fused multiply-add |
| 3330 |
operations. Returns true if successful and MUL_STMT should be removed. |
| 3331 |
If MUL_COND is nonnull, the multiplication in MUL_STMT is conditional |
| 3332 |
on MUL_COND, otherwise it is unconditional. |
| 3333 |
|
| 3334 |
If STATE indicates that we are deferring FMA transformation, that means |
| 3335 |
that we do not produce FMAs for basic blocks which look like: |
| 3336 |
|
| 3337 |
<bb 6> |
| 3338 |
# accumulator_111 = PHI <0.0(5), accumulator_66(6)> |
| 3339 |
_65 = _14 * _16; |
| 3340 |
accumulator_66 = _65 + accumulator_111; |
| 3341 |
|
| 3342 |
or its unrolled version, i.e. with several FMA candidates that feed result |
| 3343 |
of one into the addend of another. Instead, we add them to a list in STATE |
| 3344 |
and if we later discover an FMA candidate that is not part of such a chain, |
| 3345 |
we go back and perform all deferred past candidates. */ |
| 3346 |
|
| 3347 |
static bool |
| 3348 |
convert_mult_to_fma (gimple *mul_stmt, tree op1, tree op2, |
| 3349 |
fma_deferring_state *state, tree mul_cond = NULL_TREE, |
| 3350 |
tree mul_len = NULL_TREE, tree mul_bias = NULL_TREE) |
| 3351 |
{ |
| 3352 |
tree mul_result = gimple_get_lhs (mul_stmt); |
| 3353 |
/* If there isn't a LHS then this can't be an FMA. There can be no LHS |
| 3354 |
if the statement was left just for the side-effects. */ |
| 3355 |
if (!mul_result) |
| 3356 |
return false; |
| 3357 |
tree type = TREE_TYPE (mul_result); |
| 3358 |
gimple *use_stmt, *neguse_stmt; |
| 3359 |
use_operand_p use_p; |
| 3360 |
imm_use_iterator imm_iter; |
| 3361 |
|
| 3362 |
if (FLOAT_TYPE_P (type) |
| 3363 |
&& flag_fp_contract_mode != FP_CONTRACT_FAST) |
| 3364 |
return false; |
| 3365 |
|
| 3366 |
/* We don't want to do bitfield reduction ops. */ |
| 3367 |
if (INTEGRAL_TYPE_P (type) |
| 3368 |
&& (!type_has_mode_precision_p (type) || TYPE_OVERFLOW_TRAPS (type))) |
| 3369 |
return false; |
| 3370 |
|
| 3371 |
/* If the target doesn't support it, don't generate it. We assume that |
| 3372 |
if fma isn't available then fms, fnma or fnms are not either. */ |
| 3373 |
optimization_type opt_type = bb_optimization_type (gimple_bb (mul_stmt)); |
| 3374 |
if (!direct_internal_fn_supported_p (IFN_FMA, type, opt_type)) |
| 3375 |
return false; |
| 3376 |
|
| 3377 |
/* If the multiplication has zero uses, it is kept around probably because |
| 3378 |
of -fnon-call-exceptions. Don't optimize it away in that case, |
| 3379 |
it is DCE job. */ |
| 3380 |
if (has_zero_uses (mul_result)) |
| 3381 |
return false; |
| 3382 |
|
| 3383 |
bool check_defer |
| 3384 |
= (state->m_deferring_p |
| 3385 |
&& maybe_le (tree_to_poly_int64 (TYPE_SIZE (type)), |
| 3386 |
param_avoid_fma_max_bits)); |
| 3387 |
bool defer = check_defer; |
| 3388 |
bool seen_negate_p = false; |
| 3389 |
|
| 3390 |
/* There is no numerical difference between fused and unfused integer FMAs, |
| 3391 |
and the assumption below that FMA is as cheap as addition is unlikely |
| 3392 |
to be true, especially if the multiplication occurs multiple times on |
| 3393 |
the same chain. E.g., for something like: |
| 3394 |
|
| 3395 |
(((a * b) + c) >> 1) + (a * b) |
| 3396 |
|
| 3397 |
we do not want to duplicate the a * b into two additions, not least |
| 3398 |
because the result is not a natural FMA chain. */ |
| 3399 |
if (ANY_INTEGRAL_TYPE_P (type) |
| 3400 |
&& !has_single_use (mul_result)) |
| 3401 |
return false; |
| 3402 |
|
| 3403 |
if (!dbg_cnt (form_fma)) |
| 3404 |
return false; |
| 3405 |
|
| 3406 |
/* Make sure that the multiplication statement becomes dead after |
| 3407 |
the transformation, thus that all uses are transformed to FMAs. |
| 3408 |
This means we assume that an FMA operation has the same cost |
| 3409 |
as an addition. */ |
| 3410 |
FOR_EACH_IMM_USE_FAST (use_p, imm_iter, mul_result) |
| 3411 |
{ |
| 3412 |
tree result = mul_result; |
| 3413 |
bool negate_p = false; |
| 3414 |
|
| 3415 |
use_stmt = USE_STMT (use_p); |
| 3416 |
|
| 3417 |
if (is_gimple_debug (use_stmt)) |
| 3418 |
continue; |
| 3419 |
|
| 3420 |
/* For now restrict this operations to single basic blocks. In theory |
| 3421 |
we would want to support sinking the multiplication in |
| 3422 |
m = a*b; |
| 3423 |
if () |
| 3424 |
ma = m + c; |
| 3425 |
else |
| 3426 |
d = m; |
| 3427 |
to form a fma in the then block and sink the multiplication to the |
| 3428 |
else block. */ |
| 3429 |
if (gimple_bb (use_stmt) != gimple_bb (mul_stmt)) |
| 3430 |
return false; |
| 3431 |
|
| 3432 |
/* A negate on the multiplication leads to FNMA. */ |
| 3433 |
if (is_gimple_assign (use_stmt) |
| 3434 |
&& gimple_assign_rhs_code (use_stmt) == NEGATE_EXPR) |
| 3435 |
{ |
| 3436 |
ssa_op_iter iter; |
| 3437 |
use_operand_p usep; |
| 3438 |
|
| 3439 |
/* If (due to earlier missed optimizations) we have two |
| 3440 |
negates of the same value, treat them as equivalent |
| 3441 |
to a single negate with multiple uses. */ |
| 3442 |
if (seen_negate_p) |
| 3443 |
return false; |
| 3444 |
|
| 3445 |
result = gimple_assign_lhs (use_stmt); |
| 3446 |
|
| 3447 |
/* Make sure the negate statement becomes dead with this |
| 3448 |
single transformation. */ |
| 3449 |
if (!single_imm_use (gimple_assign_lhs (use_stmt), |
| 3450 |
&use_p, &neguse_stmt)) |
| 3451 |
return false; |
| 3452 |
|
| 3453 |
/* Make sure the multiplication isn't also used on that stmt. */ |
| 3454 |
FOR_EACH_PHI_OR_STMT_USE (usep, neguse_stmt, iter, SSA_OP_USE) |
| 3455 |
if (USE_FROM_PTR (usep) == mul_result) |
| 3456 |
return false; |
| 3457 |
|
| 3458 |
/* Re-validate. */ |
| 3459 |
use_stmt = neguse_stmt; |
| 3460 |
if (gimple_bb (use_stmt) != gimple_bb (mul_stmt)) |
| 3461 |
return false; |
| 3462 |
|
| 3463 |
negate_p = seen_negate_p = true; |
| 3464 |
} |
| 3465 |
|
| 3466 |
tree cond, else_value, ops[3], len, bias; |
| 3467 |
tree_code code; |
| 3468 |
if (!can_interpret_as_conditional_op_p (use_stmt, &cond, &code, ops, |
| 3469 |
&else_value, &len, &bias)) |
| 3470 |
return false; |
| 3471 |
|
| 3472 |
/* The multiplication result must be one of the addition operands. */ |
| 3473 |
if (ops[0] != result && ops[1] != result) |
| 3474 |
return false; |
| 3475 |
|
| 3476 |
switch (code) |
| 3477 |
{ |
| 3478 |
case MINUS_EXPR: |
| 3479 |
if (ops[1] == result) |
| 3480 |
negate_p = !negate_p; |
| 3481 |
break; |
| 3482 |
case PLUS_EXPR: |
| 3483 |
break; |
| 3484 |
default: |
| 3485 |
/* FMA can only be formed from PLUS and MINUS. */ |
| 3486 |
return false; |
| 3487 |
} |
| 3488 |
|
| 3489 |
if (len) |
| 3490 |
{ |
| 3491 |
/* For COND_LEN_* operations, we may have dummpy mask which is |
| 3492 |
the all true mask. Such TREE type may be mul_cond != cond |
| 3493 |
but we still consider they are equal. */ |
| 3494 |
if (mul_cond && cond != mul_cond |
| 3495 |
&& !(integer_truep (mul_cond) && integer_truep (cond))) |
| 3496 |
return false; |
| 3497 |
|
| 3498 |
if (else_value == result) |
| 3499 |
return false; |
| 3500 |
|
| 3501 |
if (!direct_internal_fn_supported_p (IFN_COND_LEN_FMA, type, |
| 3502 |
opt_type)) |
| 3503 |
return false; |
| 3504 |
|
| 3505 |
if (mul_len) |
| 3506 |
{ |
| 3507 |
poly_int64 mul_value, value; |
| 3508 |
if (poly_int_tree_p (mul_len, &mul_value) |
| 3509 |
&& poly_int_tree_p (len, &value) |
| 3510 |
&& maybe_ne (mul_value, value)) |
| 3511 |
return false; |
| 3512 |
else if (mul_len != len) |
| 3513 |
return false; |
| 3514 |
|
| 3515 |
if (wi::to_widest (mul_bias) != wi::to_widest (bias)) |
| 3516 |
return false; |
| 3517 |
} |
| 3518 |
} |
| 3519 |
else |
| 3520 |
{ |
| 3521 |
if (mul_cond && cond != mul_cond) |
| 3522 |
return false; |
| 3523 |
|
| 3524 |
if (cond) |
| 3525 |
{ |
| 3526 |
if (cond == result || else_value == result) |
| 3527 |
return false; |
| 3528 |
if (!direct_internal_fn_supported_p (IFN_COND_FMA, type, |
| 3529 |
opt_type)) |
| 3530 |
return false; |
| 3531 |
} |
| 3532 |
} |
| 3533 |
|
| 3534 |
/* If the subtrahend (OPS[1]) is computed by a MULT_EXPR that |
| 3535 |
we'll visit later, we might be able to get a more profitable |
| 3536 |
match with fnma. |
| 3537 |
OTOH, if we don't, a negate / fma pair has likely lower latency |
| 3538 |
that a mult / subtract pair. */ |
| 3539 |
if (code == MINUS_EXPR |
| 3540 |
&& !negate_p |
| 3541 |
&& ops[0] == result |
| 3542 |
&& !direct_internal_fn_supported_p (IFN_FMS, type, opt_type) |
| 3543 |
&& direct_internal_fn_supported_p (IFN_FNMA, type, opt_type) |
| 3544 |
&& TREE_CODE (ops[1]) == SSA_NAME |
| 3545 |
&& has_single_use (ops[1])) |
| 3546 |
{ |
| 3547 |
gimple *stmt2 = SSA_NAME_DEF_STMT (ops[1]); |
| 3548 |
if (is_gimple_assign (stmt2) |
| 3549 |
&& gimple_assign_rhs_code (stmt2) == MULT_EXPR) |
| 3550 |
return false; |
| 3551 |
} |
| 3552 |
|
| 3553 |
/* We can't handle a * b + a * b. */ |
| 3554 |
if (ops[0] == ops[1]) |
| 3555 |
return false; |
| 3556 |
/* If deferring, make sure we are not looking at an instruction that |
| 3557 |
wouldn't have existed if we were not. */ |
| 3558 |
if (state->m_deferring_p |
| 3559 |
&& (state->m_mul_result_set.contains (ops[0]) |
| 3560 |
|| state->m_mul_result_set.contains (ops[1]))) |
| 3561 |
return false; |
| 3562 |
|
| 3563 |
if (check_defer) |
| 3564 |
{ |
| 3565 |
tree use_lhs = gimple_get_lhs (use_stmt); |
| 3566 |
if (state->m_last_result) |
| 3567 |
{ |
| 3568 |
if (ops[1] == state->m_last_result |
| 3569 |
|| ops[0] == state->m_last_result) |
| 3570 |
defer = true; |
| 3571 |
else |
| 3572 |
defer = false; |
| 3573 |
} |
| 3574 |
else |
| 3575 |
{ |
| 3576 |
gcc_checking_assert (!state->m_initial_phi); |
| 3577 |
gphi *phi; |
| 3578 |
if (ops[0] == result) |
| 3579 |
phi = result_of_phi (ops[1]); |
| 3580 |
else |
| 3581 |
{ |
| 3582 |
gcc_assert (ops[1] == result); |
| 3583 |
phi = result_of_phi (ops[0]); |
| 3584 |
} |
| 3585 |
|
| 3586 |
if (phi) |
| 3587 |
{ |
| 3588 |
state->m_initial_phi = phi; |
| 3589 |
defer = true; |
| 3590 |
} |
| 3591 |
else |
| 3592 |
defer = false; |
| 3593 |
} |
| 3594 |
|
| 3595 |
state->m_last_result = use_lhs; |
| 3596 |
check_defer = false; |
| 3597 |
} |
| 3598 |
else |
| 3599 |
defer = false; |
| 3600 |
|
| 3601 |
/* While it is possible to validate whether or not the exact form that |
| 3602 |
we've recognized is available in the backend, the assumption is that |
| 3603 |
if the deferring logic above did not trigger, the transformation is |
| 3604 |
never a loss. For instance, suppose the target only has the plain FMA |
| 3605 |
pattern available. Consider a*b-c -> fma(a,b,-c): we've exchanged |
| 3606 |
MUL+SUB for FMA+NEG, which is still two operations. Consider |
| 3607 |
-(a*b)-c -> fma(-a,b,-c): we still have 3 operations, but in the FMA |
| 3608 |
form the two NEGs are independent and could be run in parallel. */ |
| 3609 |
} |
| 3610 |
|
| 3611 |
if (defer) |
| 3612 |
{ |
| 3613 |
fma_transformation_info fti; |
| 3614 |
fti.mul_stmt = mul_stmt; |
| 3615 |
fti.mul_result = mul_result; |
| 3616 |
fti.op1 = op1; |
| 3617 |
fti.op2 = op2; |
| 3618 |
state->m_candidates.safe_push (fti); |
| 3619 |
state->m_mul_result_set.add (mul_result); |
| 3620 |
|
| 3621 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 3622 |
{ |
| 3623 |
fprintf (dump_file, "Deferred generating FMA for multiplication "); |
| 3624 |
print_gimple_stmt (dump_file, mul_stmt, 0, TDF_NONE); |
| 3625 |
fprintf (dump_file, "\n"); |
| 3626 |
} |
| 3627 |
|
| 3628 |
return false; |
| 3629 |
} |
| 3630 |
else |
| 3631 |
{ |
| 3632 |
if (state->m_deferring_p) |
| 3633 |
cancel_fma_deferring (state); |
| 3634 |
convert_mult_to_fma_1 (mul_result, op1, op2); |
| 3635 |
return true; |
| 3636 |
} |
| 3637 |
} |
| 3638 |
|
| 3639 |
|
| 3640 |
/* Helper function of match_arith_overflow. For MUL_OVERFLOW, if we have |
| 3641 |
a check for non-zero like: |
| 3642 |
_1 = x_4(D) * y_5(D); |
| 3643 |
*res_7(D) = _1; |
| 3644 |
if (x_4(D) != 0) |
| 3645 |
goto <bb 3>; [50.00%] |
| 3646 |
else |
| 3647 |
goto <bb 4>; [50.00%] |
| 3648 |
|
| 3649 |
<bb 3> [local count: 536870913]: |
| 3650 |
_2 = _1 / x_4(D); |
| 3651 |
_9 = _2 != y_5(D); |
| 3652 |
_10 = (int) _9; |
| 3653 |
|
| 3654 |
<bb 4> [local count: 1073741824]: |
| 3655 |
# iftmp.0_3 = PHI <_10(3), 0(2)> |
| 3656 |
then in addition to using .MUL_OVERFLOW (x_4(D), y_5(D)) we can also |
| 3657 |
optimize the x_4(D) != 0 condition to 1. */ |
| 3658 |
|
| 3659 |
static void |
| 3660 |
maybe_optimize_guarding_check (vec<gimple *> &mul_stmts, gimple *cond_stmt, |
| 3661 |
gimple *div_stmt, bool *cfg_changed) |
| 3662 |
{ |
| 3663 |
basic_block bb = gimple_bb (cond_stmt); |
| 3664 |
if (gimple_bb (div_stmt) != bb || !single_pred_p (bb)) |
| 3665 |
return; |
| 3666 |
edge pred_edge = single_pred_edge (bb); |
| 3667 |
basic_block pred_bb = pred_edge->src; |
| 3668 |
if (EDGE_COUNT (pred_bb->succs) != 2) |
| 3669 |
return; |
| 3670 |
edge other_edge = EDGE_SUCC (pred_bb, EDGE_SUCC (pred_bb, 0) == pred_edge); |
| 3671 |
edge other_succ_edge = NULL; |
| 3672 |
if (gimple_code (cond_stmt) == GIMPLE_COND) |
| 3673 |
{ |
| 3674 |
if (EDGE_COUNT (bb->succs) != 2) |
| 3675 |
return; |
| 3676 |
other_succ_edge = EDGE_SUCC (bb, 0); |
| 3677 |
if (gimple_cond_code (cond_stmt) == NE_EXPR) |
| 3678 |
{ |
| 3679 |
if (other_succ_edge->flags & EDGE_TRUE_VALUE) |
| 3680 |
other_succ_edge = EDGE_SUCC (bb, 1); |
| 3681 |
} |
| 3682 |
else if (other_succ_edge->flags & EDGE_FALSE_VALUE) |
| 3683 |
other_succ_edge = EDGE_SUCC (bb, 0); |
| 3684 |
if (other_edge->dest != other_succ_edge->dest) |
| 3685 |
return; |
| 3686 |
} |
| 3687 |
else if (!single_succ_p (bb) || other_edge->dest != single_succ (bb)) |
| 3688 |
return; |
| 3689 |
gcond *zero_cond = safe_dyn_cast <gcond *> (*gsi_last_bb (pred_bb)); |
| 3690 |
if (zero_cond == NULL |
| 3691 |
|| (gimple_cond_code (zero_cond) |
| 3692 |
!= ((pred_edge->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR)) |
| 3693 |
|| !integer_zerop (gimple_cond_rhs (zero_cond))) |
| 3694 |
return; |
| 3695 |
tree zero_cond_lhs = gimple_cond_lhs (zero_cond); |
| 3696 |
if (TREE_CODE (zero_cond_lhs) != SSA_NAME) |
| 3697 |
return; |
| 3698 |
if (gimple_assign_rhs2 (div_stmt) != zero_cond_lhs) |
| 3699 |
{ |
| 3700 |
/* Allow the divisor to be result of a same precision cast |
| 3701 |
from zero_cond_lhs. */ |
| 3702 |
tree rhs2 = gimple_assign_rhs2 (div_stmt); |
| 3703 |
if (TREE_CODE (rhs2) != SSA_NAME) |
| 3704 |
return; |
| 3705 |
gimple *g = SSA_NAME_DEF_STMT (rhs2); |
| 3706 |
if (!gimple_assign_cast_p (g) |
| 3707 |
|| gimple_assign_rhs1 (g) != gimple_cond_lhs (zero_cond) |
| 3708 |
|| !INTEGRAL_TYPE_P (TREE_TYPE (zero_cond_lhs)) |
| 3709 |
|| (TYPE_PRECISION (TREE_TYPE (zero_cond_lhs)) |
| 3710 |
!= TYPE_PRECISION (TREE_TYPE (rhs2)))) |
| 3711 |
return; |
| 3712 |
} |
| 3713 |
gimple_stmt_iterator gsi = gsi_after_labels (bb); |
| 3714 |
mul_stmts.quick_push (div_stmt); |
| 3715 |
if (is_gimple_debug (gsi_stmt (gsi))) |
| 3716 |
gsi_next_nondebug (&gsi); |
| 3717 |
unsigned cast_count = 0; |
| 3718 |
while (gsi_stmt (gsi) != cond_stmt) |
| 3719 |
{ |
| 3720 |
/* If original mul_stmt has a single use, allow it in the same bb, |
| 3721 |
we are looking then just at __builtin_mul_overflow_p. |
| 3722 |
Though, in that case the original mul_stmt will be replaced |
| 3723 |
by .MUL_OVERFLOW, REALPART_EXPR and IMAGPART_EXPR stmts. */ |
| 3724 |
gimple *mul_stmt; |
| 3725 |
unsigned int i; |
| 3726 |
bool ok = false; |
| 3727 |
FOR_EACH_VEC_ELT (mul_stmts, i, mul_stmt) |
| 3728 |
{ |
| 3729 |
if (gsi_stmt (gsi) == mul_stmt) |
| 3730 |
{ |
| 3731 |
ok = true; |
| 3732 |
break; |
| 3733 |
} |
| 3734 |
} |
| 3735 |
if (!ok && gimple_assign_cast_p (gsi_stmt (gsi)) && ++cast_count < 4) |
| 3736 |
ok = true; |
| 3737 |
if (!ok) |
| 3738 |
return; |
| 3739 |
gsi_next_nondebug (&gsi); |
| 3740 |
} |
| 3741 |
if (gimple_code (cond_stmt) == GIMPLE_COND) |
| 3742 |
{ |
| 3743 |
basic_block succ_bb = other_edge->dest; |
| 3744 |
for (gphi_iterator gpi = gsi_start_phis (succ_bb); !gsi_end_p (gpi); |
| 3745 |
gsi_next (&gpi)) |
| 3746 |
{ |
| 3747 |
gphi *phi = gpi.phi (); |
| 3748 |
tree v1 = gimple_phi_arg_def (phi, other_edge->dest_idx); |
| 3749 |
tree v2 = gimple_phi_arg_def (phi, other_succ_edge->dest_idx); |
| 3750 |
if (!operand_equal_p (v1, v2, 0)) |
| 3751 |
return; |
| 3752 |
} |
| 3753 |
} |
| 3754 |
else |
| 3755 |
{ |
| 3756 |
tree lhs = gimple_assign_lhs (cond_stmt); |
| 3757 |
if (!lhs || !INTEGRAL_TYPE_P (TREE_TYPE (lhs))) |
| 3758 |
return; |
| 3759 |
gsi_next_nondebug (&gsi); |
| 3760 |
if (!gsi_end_p (gsi)) |
| 3761 |
{ |
| 3762 |
if (gimple_assign_rhs_code (cond_stmt) == COND_EXPR) |
| 3763 |
return; |
| 3764 |
gimple *cast_stmt = gsi_stmt (gsi); |
| 3765 |
if (!gimple_assign_cast_p (cast_stmt)) |
| 3766 |
return; |
| 3767 |
tree new_lhs = gimple_assign_lhs (cast_stmt); |
| 3768 |
gsi_next_nondebug (&gsi); |
| 3769 |
if (!gsi_end_p (gsi) |
| 3770 |
|| !new_lhs |
| 3771 |
|| !INTEGRAL_TYPE_P (TREE_TYPE (new_lhs)) |
| 3772 |
|| TYPE_PRECISION (TREE_TYPE (new_lhs)) <= 1) |
| 3773 |
return; |
| 3774 |
lhs = new_lhs; |
| 3775 |
} |
| 3776 |
edge succ_edge = single_succ_edge (bb); |
| 3777 |
basic_block succ_bb = succ_edge->dest; |
| 3778 |
gsi = gsi_start_phis (succ_bb); |
| 3779 |
if (gsi_end_p (gsi)) |
| 3780 |
return; |
| 3781 |
gphi *phi = as_a <gphi *> (gsi_stmt (gsi)); |
| 3782 |
gsi_next (&gsi); |
| 3783 |
if (!gsi_end_p (gsi)) |
| 3784 |
return; |
| 3785 |
if (gimple_phi_arg_def (phi, succ_edge->dest_idx) != lhs) |
| 3786 |
return; |
| 3787 |
tree other_val = gimple_phi_arg_def (phi, other_edge->dest_idx); |
| 3788 |
if (gimple_assign_rhs_code (cond_stmt) == COND_EXPR) |
| 3789 |
{ |
| 3790 |
tree cond = gimple_assign_rhs1 (cond_stmt); |
| 3791 |
if (TREE_CODE (cond) == NE_EXPR) |
| 3792 |
{ |
| 3793 |
if (!operand_equal_p (other_val, |
| 3794 |
gimple_assign_rhs3 (cond_stmt), 0)) |
| 3795 |
return; |
| 3796 |
} |
| 3797 |
else if (!operand_equal_p (other_val, |
| 3798 |
gimple_assign_rhs2 (cond_stmt), 0)) |
| 3799 |
return; |
| 3800 |
} |
| 3801 |
else if (gimple_assign_rhs_code (cond_stmt) == NE_EXPR) |
| 3802 |
{ |
| 3803 |
if (!integer_zerop (other_val)) |
| 3804 |
return; |
| 3805 |
} |
| 3806 |
else if (!integer_onep (other_val)) |
| 3807 |
return; |
| 3808 |
} |
| 3809 |
if (pred_edge->flags & EDGE_TRUE_VALUE) |
| 3810 |
gimple_cond_make_true (zero_cond); |
| 3811 |
else |
| 3812 |
gimple_cond_make_false (zero_cond); |
| 3813 |
update_stmt (zero_cond); |
| 3814 |
reset_flow_sensitive_info_in_bb (bb); |
| 3815 |
*cfg_changed = true; |
| 3816 |
} |
| 3817 |
|
| 3818 |
/* Helper function for arith_overflow_check_p. Return true |
| 3819 |
if VAL1 is equal to VAL2 cast to corresponding integral type |
| 3820 |
with other signedness or vice versa. */ |
| 3821 |
|
| 3822 |
static bool |
| 3823 |
arith_cast_equal_p (tree val1, tree val2) |
| 3824 |
{ |
| 3825 |
if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST) |
| 3826 |
return wi::eq_p (wi::to_wide (val1), wi::to_wide (val2)); |
| 3827 |
else if (TREE_CODE (val1) != SSA_NAME || TREE_CODE (val2) != SSA_NAME) |
| 3828 |
return false; |
| 3829 |
if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (val1)) |
| 3830 |
&& gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val1)) == val2) |
| 3831 |
return true; |
| 3832 |
if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (val2)) |
| 3833 |
&& gimple_assign_rhs1 (SSA_NAME_DEF_STMT (val2)) == val1) |
| 3834 |
return true; |
| 3835 |
return false; |
| 3836 |
} |
| 3837 |
|
| 3838 |
/* Helper function of match_arith_overflow. Return 1 |
| 3839 |
if USE_STMT is unsigned overflow check ovf != 0 for |
| 3840 |
STMT, -1 if USE_STMT is unsigned overflow check ovf == 0 |
| 3841 |
and 0 otherwise. */ |
| 3842 |
|
| 3843 |
static int |
| 3844 |
arith_overflow_check_p (gimple *stmt, gimple *cast_stmt, gimple *&use_stmt, |
| 3845 |
tree maxval, tree *other) |
| 3846 |
{ |
| 3847 |
enum tree_code ccode = ERROR_MARK; |
| 3848 |
tree crhs1 = NULL_TREE, crhs2 = NULL_TREE; |
| 3849 |
enum tree_code code = gimple_assign_rhs_code (stmt); |
| 3850 |
tree lhs = gimple_assign_lhs (cast_stmt ? cast_stmt : stmt); |
| 3851 |
tree rhs1 = gimple_assign_rhs1 (stmt); |
| 3852 |
tree rhs2 = gimple_assign_rhs2 (stmt); |
| 3853 |
tree multop = NULL_TREE, divlhs = NULL_TREE; |
| 3854 |
gimple *cur_use_stmt = use_stmt; |
| 3855 |
|
| 3856 |
if (code == MULT_EXPR) |
| 3857 |
{ |
| 3858 |
if (!is_gimple_assign (use_stmt)) |
| 3859 |
return 0; |
| 3860 |
if (gimple_assign_rhs_code (use_stmt) != TRUNC_DIV_EXPR) |
| 3861 |
return 0; |
| 3862 |
if (gimple_assign_rhs1 (use_stmt) != lhs) |
| 3863 |
return 0; |
| 3864 |
if (cast_stmt) |
| 3865 |
{ |
| 3866 |
if (arith_cast_equal_p (gimple_assign_rhs2 (use_stmt), rhs1)) |
| 3867 |
multop = rhs2; |
| 3868 |
else if (arith_cast_equal_p (gimple_assign_rhs2 (use_stmt), rhs2)) |
| 3869 |
multop = rhs1; |
| 3870 |
else |
| 3871 |
return 0; |
| 3872 |
} |
| 3873 |
else if (gimple_assign_rhs2 (use_stmt) == rhs1) |
| 3874 |
multop = rhs2; |
| 3875 |
else if (operand_equal_p (gimple_assign_rhs2 (use_stmt), rhs2, 0)) |
| 3876 |
multop = rhs1; |
| 3877 |
else |
| 3878 |
return 0; |
| 3879 |
if (stmt_ends_bb_p (use_stmt)) |
| 3880 |
return 0; |
| 3881 |
divlhs = gimple_assign_lhs (use_stmt); |
| 3882 |
if (!divlhs) |
| 3883 |
return 0; |
| 3884 |
use_operand_p use; |
| 3885 |
if (!single_imm_use (divlhs, &use, &cur_use_stmt)) |
| 3886 |
return 0; |
| 3887 |
if (cast_stmt && gimple_assign_cast_p (cur_use_stmt)) |
| 3888 |
{ |
| 3889 |
tree cast_lhs = gimple_assign_lhs (cur_use_stmt); |
| 3890 |
if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs)) |
| 3891 |
&& TYPE_UNSIGNED (TREE_TYPE (cast_lhs)) |
| 3892 |
&& (TYPE_PRECISION (TREE_TYPE (cast_lhs)) |
| 3893 |
== TYPE_PRECISION (TREE_TYPE (divlhs))) |
| 3894 |
&& single_imm_use (cast_lhs, &use, &cur_use_stmt)) |
| 3895 |
{ |
| 3896 |
cast_stmt = NULL; |
| 3897 |
divlhs = cast_lhs; |
| 3898 |
} |
| 3899 |
else |
| 3900 |
return 0; |
| 3901 |
} |
| 3902 |
} |
| 3903 |
if (gimple_code (cur_use_stmt) == GIMPLE_COND) |
| 3904 |
{ |
| 3905 |
ccode = gimple_cond_code (cur_use_stmt); |
| 3906 |
crhs1 = gimple_cond_lhs (cur_use_stmt); |
| 3907 |
crhs2 = gimple_cond_rhs (cur_use_stmt); |
| 3908 |
} |
| 3909 |
else if (is_gimple_assign (cur_use_stmt)) |
| 3910 |
{ |
| 3911 |
if (gimple_assign_rhs_class (cur_use_stmt) == GIMPLE_BINARY_RHS) |
| 3912 |
{ |
| 3913 |
ccode = gimple_assign_rhs_code (cur_use_stmt); |
| 3914 |
crhs1 = gimple_assign_rhs1 (cur_use_stmt); |
| 3915 |
crhs2 = gimple_assign_rhs2 (cur_use_stmt); |
| 3916 |
} |
| 3917 |
else if (gimple_assign_rhs_code (cur_use_stmt) == COND_EXPR) |
| 3918 |
{ |
| 3919 |
tree cond = gimple_assign_rhs1 (cur_use_stmt); |
| 3920 |
if (COMPARISON_CLASS_P (cond)) |
| 3921 |
{ |
| 3922 |
ccode = TREE_CODE (cond); |
| 3923 |
crhs1 = TREE_OPERAND (cond, 0); |
| 3924 |
crhs2 = TREE_OPERAND (cond, 1); |
| 3925 |
} |
| 3926 |
else |
| 3927 |
return 0; |
| 3928 |
} |
| 3929 |
else |
| 3930 |
return 0; |
| 3931 |
} |
| 3932 |
else |
| 3933 |
return 0; |
| 3934 |
|
| 3935 |
if (maxval |
| 3936 |
&& ccode == RSHIFT_EXPR |
| 3937 |
&& crhs1 == lhs |
| 3938 |
&& TREE_CODE (crhs2) == INTEGER_CST |
| 3939 |
&& wi::to_widest (crhs2) == TYPE_PRECISION (TREE_TYPE (maxval))) |
| 3940 |
{ |
| 3941 |
tree shiftlhs = gimple_assign_lhs (use_stmt); |
| 3942 |
if (!shiftlhs) |
| 3943 |
return 0; |
| 3944 |
use_operand_p use; |
| 3945 |
if (!single_imm_use (shiftlhs, &use, &cur_use_stmt)) |
| 3946 |
return 0; |
| 3947 |
if (gimple_code (cur_use_stmt) == GIMPLE_COND) |
| 3948 |
{ |
| 3949 |
ccode = gimple_cond_code (cur_use_stmt); |
| 3950 |
crhs1 = gimple_cond_lhs (cur_use_stmt); |
| 3951 |
crhs2 = gimple_cond_rhs (cur_use_stmt); |
| 3952 |
} |
| 3953 |
else if (is_gimple_assign (cur_use_stmt)) |
| 3954 |
{ |
| 3955 |
if (gimple_assign_rhs_class (cur_use_stmt) == GIMPLE_BINARY_RHS) |
| 3956 |
{ |
| 3957 |
ccode = gimple_assign_rhs_code (cur_use_stmt); |
| 3958 |
crhs1 = gimple_assign_rhs1 (cur_use_stmt); |
| 3959 |
crhs2 = gimple_assign_rhs2 (cur_use_stmt); |
| 3960 |
} |
| 3961 |
else if (gimple_assign_rhs_code (cur_use_stmt) == COND_EXPR) |
| 3962 |
{ |
| 3963 |
tree cond = gimple_assign_rhs1 (cur_use_stmt); |
| 3964 |
if (COMPARISON_CLASS_P (cond)) |
| 3965 |
{ |
| 3966 |
ccode = TREE_CODE (cond); |
| 3967 |
crhs1 = TREE_OPERAND (cond, 0); |
| 3968 |
crhs2 = TREE_OPERAND (cond, 1); |
| 3969 |
} |
| 3970 |
else |
| 3971 |
return 0; |
| 3972 |
} |
| 3973 |
else |
| 3974 |
{ |
| 3975 |
enum tree_code sc = gimple_assign_rhs_code (cur_use_stmt); |
| 3976 |
tree castlhs = gimple_assign_lhs (cur_use_stmt); |
| 3977 |
if (!CONVERT_EXPR_CODE_P (sc) |
| 3978 |
|| !castlhs |
| 3979 |
|| !INTEGRAL_TYPE_P (TREE_TYPE (castlhs)) |
| 3980 |
|| (TYPE_PRECISION (TREE_TYPE (castlhs)) |
| 3981 |
> TYPE_PRECISION (TREE_TYPE (maxval)))) |
| 3982 |
return 0; |
| 3983 |
return 1; |
| 3984 |
} |
| 3985 |
} |
| 3986 |
else |
| 3987 |
return 0; |
| 3988 |
if ((ccode != EQ_EXPR && ccode != NE_EXPR) |
| 3989 |
|| crhs1 != shiftlhs |
| 3990 |
|| !integer_zerop (crhs2)) |
| 3991 |
return 0; |
| 3992 |
return 1; |
| 3993 |
} |
| 3994 |
|
| 3995 |
if (TREE_CODE_CLASS (ccode) != tcc_comparison) |
| 3996 |
return 0; |
| 3997 |
|
| 3998 |
switch (ccode) |
| 3999 |
{ |
| 4000 |
case GT_EXPR: |
| 4001 |
case LE_EXPR: |
| 4002 |
if (maxval) |
| 4003 |
{ |
| 4004 |
/* r = a + b; r > maxval or r <= maxval */ |
| 4005 |
if (crhs1 == lhs |
| 4006 |
&& TREE_CODE (crhs2) == INTEGER_CST |
| 4007 |
&& tree_int_cst_equal (crhs2, maxval)) |
| 4008 |
return ccode == GT_EXPR ? 1 : -1; |
| 4009 |
break; |
| 4010 |
} |
| 4011 |
/* r = a - b; r > a or r <= a |
| 4012 |
r = a + b; a > r or a <= r or b > r or b <= r. */ |
| 4013 |
if ((code == MINUS_EXPR && crhs1 == lhs && crhs2 == rhs1) |
| 4014 |
|| (code == PLUS_EXPR && (crhs1 == rhs1 || crhs1 == rhs2) |
| 4015 |
&& crhs2 == lhs)) |
| 4016 |
return ccode == GT_EXPR ? 1 : -1; |
| 4017 |
/* r = ~a; b > r or b <= r. */ |
| 4018 |
if (code == BIT_NOT_EXPR && crhs2 == lhs) |
| 4019 |
{ |
| 4020 |
if (other) |
| 4021 |
*other = crhs1; |
| 4022 |
return ccode == GT_EXPR ? 1 : -1; |
| 4023 |
} |
| 4024 |
break; |
| 4025 |
case LT_EXPR: |
| 4026 |
case GE_EXPR: |
| 4027 |
if (maxval) |
| 4028 |
break; |
| 4029 |
/* r = a - b; a < r or a >= r |
| 4030 |
r = a + b; r < a or r >= a or r < b or r >= b. */ |
| 4031 |
if ((code == MINUS_EXPR && crhs1 == rhs1 && crhs2 == lhs) |
| 4032 |
|| (code == PLUS_EXPR && crhs1 == lhs |
| 4033 |
&& (crhs2 == rhs1 || crhs2 == rhs2))) |
| 4034 |
return ccode == LT_EXPR ? 1 : -1; |
| 4035 |
/* r = ~a; r < b or r >= b. */ |
| 4036 |
if (code == BIT_NOT_EXPR && crhs1 == lhs) |
| 4037 |
{ |
| 4038 |
if (other) |
| 4039 |
*other = crhs2; |
| 4040 |
return ccode == LT_EXPR ? 1 : -1; |
| 4041 |
} |
| 4042 |
break; |
| 4043 |
case EQ_EXPR: |
| 4044 |
case NE_EXPR: |
| 4045 |
/* r = a * b; _1 = r / a; _1 == b |
| 4046 |
r = a * b; _1 = r / b; _1 == a |
| 4047 |
r = a * b; _1 = r / a; _1 != b |
| 4048 |
r = a * b; _1 = r / b; _1 != a. */ |
| 4049 |
if (code == MULT_EXPR) |
| 4050 |
{ |
| 4051 |
if (cast_stmt) |
| 4052 |
{ |
| 4053 |
if ((crhs1 == divlhs && arith_cast_equal_p (crhs2, multop)) |
| 4054 |
|| (crhs2 == divlhs && arith_cast_equal_p (crhs1, multop))) |
| 4055 |
{ |
| 4056 |
use_stmt = cur_use_stmt; |
| 4057 |
return ccode == NE_EXPR ? 1 : -1; |
| 4058 |
} |
| 4059 |
} |
| 4060 |
else if ((crhs1 == divlhs && operand_equal_p (crhs2, multop, 0)) |
| 4061 |
|| (crhs2 == divlhs && crhs1 == multop)) |
| 4062 |
{ |
| 4063 |
use_stmt = cur_use_stmt; |
| 4064 |
return ccode == NE_EXPR ? 1 : -1; |
| 4065 |
} |
| 4066 |
} |
| 4067 |
break; |
| 4068 |
default: |
| 4069 |
break; |
| 4070 |
} |
| 4071 |
return 0; |
| 4072 |
} |
| 4073 |
|
| 4074 |
extern bool gimple_unsigned_integer_sat_add (tree, tree*, tree (*)(tree)); |
| 4075 |
extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree)); |
| 4076 |
extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree)); |
| 4077 |
|
| 4078 |
extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree)); |
| 4079 |
extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree)); |
| 4080 |
extern bool gimple_signed_integer_sat_trunc (tree, tree*, tree (*)(tree)); |
| 4081 |
|
| 4082 |
static void |
| 4083 |
build_saturation_binary_arith_call_and_replace (gimple_stmt_iterator *gsi, |
| 4084 |
internal_fn fn, tree lhs, |
| 4085 |
tree op_0, tree op_1) |
| 4086 |
{ |
| 4087 |
if (direct_internal_fn_supported_p (fn, TREE_TYPE (op_0), OPTIMIZE_FOR_BOTH)) |
| 4088 |
{ |
| 4089 |
gcall *call = gimple_build_call_internal (fn, 2, op_0, op_1); |
| 4090 |
gimple_call_set_lhs (call, lhs); |
| 4091 |
gsi_replace (gsi, call, /* update_eh_info */ true); |
| 4092 |
} |
| 4093 |
} |
| 4094 |
|
| 4095 |
static bool |
| 4096 |
build_saturation_binary_arith_call_and_insert (gimple_stmt_iterator *gsi, |
| 4097 |
internal_fn fn, tree lhs, |
| 4098 |
tree op_0, tree op_1) |
| 4099 |
{ |
| 4100 |
if (!direct_internal_fn_supported_p (fn, TREE_TYPE (op_0), OPTIMIZE_FOR_BOTH)) |
| 4101 |
return false; |
| 4102 |
|
| 4103 |
gcall *call = gimple_build_call_internal (fn, 2, op_0, op_1); |
| 4104 |
gimple_call_set_lhs (call, lhs); |
| 4105 |
gsi_insert_before (gsi, call, GSI_SAME_STMT); |
| 4106 |
|
| 4107 |
return true; |
| 4108 |
} |
| 4109 |
|
| 4110 |
/* |
| 4111 |
* Try to match saturation unsigned add with assign. |
| 4112 |
* _7 = _4 + _6; |
| 4113 |
* _8 = _4 > _7; |
| 4114 |
* _9 = (long unsigned int) _8; |
| 4115 |
* _10 = -_9; |
| 4116 |
* _12 = _7 | _10; |
| 4117 |
* => |
| 4118 |
* _12 = .SAT_ADD (_4, _6); */ |
| 4119 |
|
| 4120 |
static void |
| 4121 |
match_unsigned_saturation_add (gimple_stmt_iterator *gsi, gassign *stmt) |
| 4122 |
{ |
| 4123 |
tree ops[2]; |
| 4124 |
tree lhs = gimple_assign_lhs (stmt); |
| 4125 |
|
| 4126 |
if (gimple_unsigned_integer_sat_add (lhs, ops, NULL)) |
| 4127 |
build_saturation_binary_arith_call_and_replace (gsi, IFN_SAT_ADD, lhs, |
| 4128 |
ops[0], ops[1]); |
| 4129 |
} |
| 4130 |
|
| 4131 |
/* |
| 4132 |
* Try to match saturation add with PHI. |
| 4133 |
* For unsigned integer: |
| 4134 |
* <bb 2> : |
| 4135 |
* _1 = x_3(D) + y_4(D); |
| 4136 |
* if (_1 >= x_3(D)) |
| 4137 |
* goto <bb 3>; [INV] |
| 4138 |
* else |
| 4139 |
* goto <bb 4>; [INV] |
| 4140 |
* |
| 4141 |
* <bb 3> : |
| 4142 |
* |
| 4143 |
* <bb 4> : |
| 4144 |
* # _2 = PHI <255(2), _1(3)> |
| 4145 |
* => |
| 4146 |
* <bb 4> [local count: 1073741824]: |
| 4147 |
* _2 = .SAT_ADD (x_4(D), y_5(D)); |
| 4148 |
* |
| 4149 |
* For signed integer: |
| 4150 |
* x.0_1 = (long unsigned int) x_7(D); |
| 4151 |
* y.1_2 = (long unsigned int) y_8(D); |
| 4152 |
* _3 = x.0_1 + y.1_2; |
| 4153 |
* sum_9 = (int64_t) _3; |
| 4154 |
* _4 = x_7(D) ^ y_8(D); |
| 4155 |
* _5 = x_7(D) ^ sum_9; |
| 4156 |
* _15 = ~_4; |
| 4157 |
* _16 = _5 & _15; |
| 4158 |
* if (_16 < 0) |
| 4159 |
* goto <bb 3>; [41.00%] |
| 4160 |
* else |
| 4161 |
* goto <bb 4>; [59.00%] |
| 4162 |
* _11 = x_7(D) < 0; |
| 4163 |
* _12 = (long int) _11; |
| 4164 |
* _13 = -_12; |
| 4165 |
* _14 = _13 ^ 9223372036854775807; |
| 4166 |
* # _6 = PHI <_14(3), sum_9(2)> |
| 4167 |
* => |
| 4168 |
* _6 = .SAT_ADD (x_5(D), y_6(D)); [tail call] */ |
| 4169 |
|
| 4170 |
static bool |
| 4171 |
match_saturation_add (gimple_stmt_iterator *gsi, gphi *phi) |
| 4172 |
{ |
| 4173 |
if (gimple_phi_num_args (phi) != 2) |
| 4174 |
return false; |
| 4175 |
|
| 4176 |
tree ops[2]; |
| 4177 |
tree phi_result = gimple_phi_result (phi); |
| 4178 |
|
| 4179 |
if (!gimple_unsigned_integer_sat_add (phi_result, ops, NULL) |
| 4180 |
&& !gimple_signed_integer_sat_add (phi_result, ops, NULL)) |
| 4181 |
return false; |
| 4182 |
|
| 4183 |
if (!TYPE_UNSIGNED (TREE_TYPE (ops[0])) && TREE_CODE (ops[1]) == INTEGER_CST) |
| 4184 |
ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]); |
| 4185 |
|
| 4186 |
return build_saturation_binary_arith_call_and_insert (gsi, IFN_SAT_ADD, |
| 4187 |
phi_result, ops[0], |
| 4188 |
ops[1]); |
| 4189 |
} |
| 4190 |
|
| 4191 |
/* |
| 4192 |
* Try to match saturation unsigned sub. |
| 4193 |
* _1 = _4 >= _5; |
| 4194 |
* _3 = _4 - _5; |
| 4195 |
* _6 = _1 ? _3 : 0; |
| 4196 |
* => |
| 4197 |
* _6 = .SAT_SUB (_4, _5); */ |
| 4198 |
|
| 4199 |
static void |
| 4200 |
match_unsigned_saturation_sub (gimple_stmt_iterator *gsi, gassign *stmt) |
| 4201 |
{ |
| 4202 |
tree ops[2]; |
| 4203 |
tree lhs = gimple_assign_lhs (stmt); |
| 4204 |
|
| 4205 |
if (gimple_unsigned_integer_sat_sub (lhs, ops, NULL)) |
| 4206 |
build_saturation_binary_arith_call_and_replace (gsi, IFN_SAT_SUB, lhs, |
| 4207 |
ops[0], ops[1]); |
| 4208 |
} |
| 4209 |
|
| 4210 |
/* |
| 4211 |
* Try to match saturation unsigned sub. |
| 4212 |
* <bb 2> [local count: 1073741824]: |
| 4213 |
* if (x_2(D) > y_3(D)) |
| 4214 |
* goto <bb 3>; [50.00%] |
| 4215 |
* else |
| 4216 |
* goto <bb 4>; [50.00%] |
| 4217 |
* |
| 4218 |
* <bb 3> [local count: 536870912]: |
| 4219 |
* _4 = x_2(D) - y_3(D); |
| 4220 |
* |
| 4221 |
* <bb 4> [local count: 1073741824]: |
| 4222 |
* # _1 = PHI <0(2), _4(3)> |
| 4223 |
* => |
| 4224 |
* <bb 4> [local count: 1073741824]: |
| 4225 |
* _1 = .SAT_SUB (x_2(D), y_3(D)); */ |
| 4226 |
static bool |
| 4227 |
match_saturation_sub (gimple_stmt_iterator *gsi, gphi *phi) |
| 4228 |
{ |
| 4229 |
if (gimple_phi_num_args (phi) != 2) |
| 4230 |
return false; |
| 4231 |
|
| 4232 |
tree ops[2]; |
| 4233 |
tree phi_result = gimple_phi_result (phi); |
| 4234 |
|
| 4235 |
if (!gimple_unsigned_integer_sat_sub (phi_result, ops, NULL) |
| 4236 |
&& !gimple_signed_integer_sat_sub (phi_result, ops, NULL)) |
| 4237 |
return false; |
| 4238 |
|
| 4239 |
return build_saturation_binary_arith_call_and_insert (gsi, IFN_SAT_SUB, |
| 4240 |
phi_result, ops[0], |
| 4241 |
ops[1]); |
| 4242 |
} |
| 4243 |
|
| 4244 |
/* |
| 4245 |
* Try to match saturation unsigned sub. |
| 4246 |
* uint16_t x_4(D); |
| 4247 |
* uint8_t _6; |
| 4248 |
* overflow_5 = x_4(D) > 255; |
| 4249 |
* _1 = (unsigned char) x_4(D); |
| 4250 |
* _2 = (unsigned char) overflow_5; |
| 4251 |
* _3 = -_2; |
| 4252 |
* _6 = _1 | _3; |
| 4253 |
* => |
| 4254 |
* _6 = .SAT_TRUNC (x_4(D)); |
| 4255 |
* */ |
| 4256 |
static void |
| 4257 |
match_unsigned_saturation_trunc (gimple_stmt_iterator *gsi, gassign *stmt) |
| 4258 |
{ |
| 4259 |
tree ops[1]; |
| 4260 |
tree lhs = gimple_assign_lhs (stmt); |
| 4261 |
tree type = TREE_TYPE (lhs); |
| 4262 |
|
| 4263 |
if (gimple_unsigned_integer_sat_trunc (lhs, ops, NULL) |
| 4264 |
&& direct_internal_fn_supported_p (IFN_SAT_TRUNC, |
| 4265 |
tree_pair (type, TREE_TYPE (ops[0])), |
| 4266 |
OPTIMIZE_FOR_BOTH)) |
| 4267 |
{ |
| 4268 |
gcall *call = gimple_build_call_internal (IFN_SAT_TRUNC, 1, ops[0]); |
| 4269 |
gimple_call_set_lhs (call, lhs); |
| 4270 |
gsi_replace (gsi, call, /* update_eh_info */ true); |
| 4271 |
} |
| 4272 |
} |
| 4273 |
|
| 4274 |
/* |
| 4275 |
* Try to match saturation truncate. |
| 4276 |
* Aka: |
| 4277 |
* x.0_1 = (unsigned long) x_4(D); |
| 4278 |
* _2 = x.0_1 + 2147483648; |
| 4279 |
* if (_2 > 4294967295) |
| 4280 |
* goto <bb 4>; [50.00%] |
| 4281 |
* else |
| 4282 |
* goto <bb 3>; [50.00%] |
| 4283 |
* ;; succ: 4 |
| 4284 |
* ;; 3 |
| 4285 |
* |
| 4286 |
* ;; basic block 3, loop depth 0 |
| 4287 |
* ;; pred: 2 |
| 4288 |
* trunc_5 = (int32_t) x_4(D); |
| 4289 |
* goto <bb 5>; [100.00%] |
| 4290 |
* ;; succ: 5 |
| 4291 |
* |
| 4292 |
* ;; basic block 4, loop depth 0 |
| 4293 |
* ;; pred: 2 |
| 4294 |
* _7 = x_4(D) < 0; |
| 4295 |
* _8 = (int) _7; |
| 4296 |
* _9 = -_8; |
| 4297 |
* _10 = _9 ^ 2147483647; |
| 4298 |
* ;; succ: 5 |
| 4299 |
* |
| 4300 |
* ;; basic block 5, loop depth 0 |
| 4301 |
* ;; pred: 3 |
| 4302 |
* ;; 4 |
| 4303 |
* # _3 = PHI <trunc_5(3), _10(4)> |
| 4304 |
* => |
| 4305 |
* _6 = .SAT_TRUNC (x_4(D)); |
| 4306 |
*/ |
| 4307 |
|
| 4308 |
static bool |
| 4309 |
match_saturation_trunc (gimple_stmt_iterator *gsi, gphi *phi) |
| 4310 |
{ |
| 4311 |
if (gimple_phi_num_args (phi) != 2) |
| 4312 |
return false; |
| 4313 |
|
| 4314 |
tree ops[1]; |
| 4315 |
tree phi_result = gimple_phi_result (phi); |
| 4316 |
tree type = TREE_TYPE (phi_result); |
| 4317 |
|
| 4318 |
if (!gimple_unsigned_integer_sat_trunc (phi_result, ops, NULL) |
| 4319 |
&& !gimple_signed_integer_sat_trunc (phi_result, ops, NULL)) |
| 4320 |
return false; |
| 4321 |
|
| 4322 |
if (!direct_internal_fn_supported_p (IFN_SAT_TRUNC, |
| 4323 |
tree_pair (type, TREE_TYPE (ops[0])), |
| 4324 |
OPTIMIZE_FOR_BOTH)) |
| 4325 |
return false; |
| 4326 |
|
| 4327 |
gcall *call = gimple_build_call_internal (IFN_SAT_TRUNC, 1, ops[0]); |
| 4328 |
gimple_call_set_lhs (call, phi_result); |
| 4329 |
gsi_insert_before (gsi, call, GSI_SAME_STMT); |
| 4330 |
|
| 4331 |
return true; |
| 4332 |
} |
| 4333 |
|
| 4334 |
/* Recognize for unsigned x |
| 4335 |
x = y - z; |
| 4336 |
if (x > y) |
| 4337 |
where there are other uses of x and replace it with |
| 4338 |
_7 = .SUB_OVERFLOW (y, z); |
| 4339 |
x = REALPART_EXPR <_7>; |
| 4340 |
_8 = IMAGPART_EXPR <_7>; |
| 4341 |
if (_8) |
| 4342 |
and similarly for addition. |
| 4343 |
|
| 4344 |
Also recognize: |
| 4345 |
yc = (type) y; |
| 4346 |
zc = (type) z; |
| 4347 |
x = yc + zc; |
| 4348 |
if (x > max) |
| 4349 |
where y and z have unsigned types with maximum max |
| 4350 |
and there are other uses of x and all of those cast x |
| 4351 |
back to that unsigned type and again replace it with |
| 4352 |
_7 = .ADD_OVERFLOW (y, z); |
| 4353 |
_9 = REALPART_EXPR <_7>; |
| 4354 |
_8 = IMAGPART_EXPR <_7>; |
| 4355 |
if (_8) |
| 4356 |
and replace (utype) x with _9. |
| 4357 |
Or with x >> popcount (max) instead of x > max. |
| 4358 |
|
| 4359 |
Also recognize: |
| 4360 |
x = ~z; |
| 4361 |
if (y > x) |
| 4362 |
and replace it with |
| 4363 |
_7 = .ADD_OVERFLOW (y, z); |
| 4364 |
_8 = IMAGPART_EXPR <_7>; |
| 4365 |
if (_8) |
| 4366 |
|
| 4367 |
And also recognize: |
| 4368 |
z = x * y; |
| 4369 |
if (x != 0) |
| 4370 |
goto <bb 3>; [50.00%] |
| 4371 |
else |
| 4372 |
goto <bb 4>; [50.00%] |
| 4373 |
|
| 4374 |
<bb 3> [local count: 536870913]: |
| 4375 |
_2 = z / x; |
| 4376 |
_9 = _2 != y; |
| 4377 |
_10 = (int) _9; |
| 4378 |
|
| 4379 |
<bb 4> [local count: 1073741824]: |
| 4380 |
# iftmp.0_3 = PHI <_10(3), 0(2)> |
| 4381 |
and replace it with |
| 4382 |
_7 = .MUL_OVERFLOW (x, y); |
| 4383 |
z = IMAGPART_EXPR <_7>; |
| 4384 |
_8 = IMAGPART_EXPR <_7>; |
| 4385 |
_9 = _8 != 0; |
| 4386 |
iftmp.0_3 = (int) _9; */ |
| 4387 |
|
| 4388 |
static bool |
| 4389 |
match_arith_overflow (gimple_stmt_iterator *gsi, gimple *stmt, |
| 4390 |
enum tree_code code, bool *cfg_changed) |
| 4391 |
{ |
| 4392 |
tree lhs = gimple_assign_lhs (stmt); |
| 4393 |
tree type = TREE_TYPE (lhs); |
| 4394 |
use_operand_p use_p; |
| 4395 |
imm_use_iterator iter; |
| 4396 |
bool use_seen = false; |
| 4397 |
bool ovf_use_seen = false; |
| 4398 |
gimple *use_stmt; |
| 4399 |
gimple *add_stmt = NULL; |
| 4400 |
bool add_first = false; |
| 4401 |
gimple *cond_stmt = NULL; |
| 4402 |
gimple *cast_stmt = NULL; |
| 4403 |
tree cast_lhs = NULL_TREE; |
| 4404 |
|
| 4405 |
gcc_checking_assert (code == PLUS_EXPR |
| 4406 |
|| code == MINUS_EXPR |
| 4407 |
|| code == MULT_EXPR |
| 4408 |
|| code == BIT_NOT_EXPR); |
| 4409 |
if (!INTEGRAL_TYPE_P (type) |
| 4410 |
|| !TYPE_UNSIGNED (type) |
| 4411 |
|| has_zero_uses (lhs) |
| 4412 |
|| (code != PLUS_EXPR |
| 4413 |
&& code != MULT_EXPR |
| 4414 |
&& optab_handler (code == MINUS_EXPR ? usubv4_optab : uaddv4_optab, |
| 4415 |
TYPE_MODE (type)) == CODE_FOR_nothing)) |
| 4416 |
return false; |
| 4417 |
|
| 4418 |
tree rhs1 = gimple_assign_rhs1 (stmt); |
| 4419 |
tree rhs2 = gimple_assign_rhs2 (stmt); |
| 4420 |
FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) |
| 4421 |
{ |
| 4422 |
use_stmt = USE_STMT (use_p); |
| 4423 |
if (is_gimple_debug (use_stmt)) |
| 4424 |
continue; |
| 4425 |
|
| 4426 |
tree other = NULL_TREE; |
| 4427 |
if (arith_overflow_check_p (stmt, NULL, use_stmt, NULL_TREE, &other)) |
| 4428 |
{ |
| 4429 |
if (code == BIT_NOT_EXPR) |
| 4430 |
{ |
| 4431 |
gcc_assert (other); |
| 4432 |
if (TREE_CODE (other) != SSA_NAME) |
| 4433 |
return false; |
| 4434 |
if (rhs2 == NULL) |
| 4435 |
rhs2 = other; |
| 4436 |
else |
| 4437 |
return false; |
| 4438 |
cond_stmt = use_stmt; |
| 4439 |
} |
| 4440 |
ovf_use_seen = true; |
| 4441 |
} |
| 4442 |
else |
| 4443 |
{ |
| 4444 |
use_seen = true; |
| 4445 |
if (code == MULT_EXPR |
| 4446 |
&& cast_stmt == NULL |
| 4447 |
&& gimple_assign_cast_p (use_stmt)) |
| 4448 |
{ |
| 4449 |
cast_lhs = gimple_assign_lhs (use_stmt); |
| 4450 |
if (INTEGRAL_TYPE_P (TREE_TYPE (cast_lhs)) |
| 4451 |
&& !TYPE_UNSIGNED (TREE_TYPE (cast_lhs)) |
| 4452 |
&& (TYPE_PRECISION (TREE_TYPE (cast_lhs)) |
| 4453 |
== TYPE_PRECISION (TREE_TYPE (lhs)))) |
| 4454 |
cast_stmt = use_stmt; |
| 4455 |
else |
| 4456 |
cast_lhs = NULL_TREE; |
| 4457 |
} |
| 4458 |
} |
| 4459 |
if (ovf_use_seen && use_seen) |
| 4460 |
break; |
| 4461 |
} |
| 4462 |
|
| 4463 |
if (!ovf_use_seen |
| 4464 |
&& code == MULT_EXPR |
| 4465 |
&& cast_stmt) |
| 4466 |
{ |
| 4467 |
if (TREE_CODE (rhs1) != SSA_NAME |
| 4468 |
|| (TREE_CODE (rhs2) != SSA_NAME && TREE_CODE (rhs2) != INTEGER_CST)) |
| 4469 |
return false; |
| 4470 |
FOR_EACH_IMM_USE_FAST (use_p, iter, cast_lhs) |
| 4471 |
{ |
| 4472 |
use_stmt = USE_STMT (use_p); |
| 4473 |
if (is_gimple_debug (use_stmt)) |
| 4474 |
continue; |
| 4475 |
|
| 4476 |
if (arith_overflow_check_p (stmt, cast_stmt, use_stmt, |
| 4477 |
NULL_TREE, NULL)) |
| 4478 |
ovf_use_seen = true; |
| 4479 |
} |
| 4480 |
} |
| 4481 |
else |
| 4482 |
{ |
| 4483 |
cast_stmt = NULL; |
| 4484 |
cast_lhs = NULL_TREE; |
| 4485 |
} |
| 4486 |
|
| 4487 |
tree maxval = NULL_TREE; |
| 4488 |
if (!ovf_use_seen |
| 4489 |
|| (code != MULT_EXPR && (code == BIT_NOT_EXPR ? use_seen : !use_seen)) |
| 4490 |
|| (code == PLUS_EXPR |
| 4491 |
&& optab_handler (uaddv4_optab, |
| 4492 |
TYPE_MODE (type)) == CODE_FOR_nothing) |
| 4493 |
|| (code == MULT_EXPR |
| 4494 |
&& optab_handler (cast_stmt ? mulv4_optab : umulv4_optab, |
| 4495 |
TYPE_MODE (type)) == CODE_FOR_nothing |
| 4496 |
&& (use_seen |
| 4497 |
|| cast_stmt |
| 4498 |
|| !can_mult_highpart_p (TYPE_MODE (type), true)))) |
| 4499 |
{ |
| 4500 |
if (code != PLUS_EXPR) |
| 4501 |
return false; |
| 4502 |
if (TREE_CODE (rhs1) != SSA_NAME |
| 4503 |
|| !gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs1))) |
| 4504 |
return false; |
| 4505 |
rhs1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs1)); |
| 4506 |
tree type1 = TREE_TYPE (rhs1); |
| 4507 |
if (!INTEGRAL_TYPE_P (type1) |
| 4508 |
|| !TYPE_UNSIGNED (type1) |
| 4509 |
|| TYPE_PRECISION (type1) >= TYPE_PRECISION (type) |
| 4510 |
|| (TYPE_PRECISION (type1) |
| 4511 |
!= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type1)))) |
| 4512 |
return false; |
| 4513 |
if (TREE_CODE (rhs2) == INTEGER_CST) |
| 4514 |
{ |
| 4515 |
if (wi::ne_p (wi::rshift (wi::to_wide (rhs2), |
| 4516 |
TYPE_PRECISION (type1), |
| 4517 |
UNSIGNED), 0)) |
| 4518 |
return false; |
| 4519 |
rhs2 = fold_convert (type1, rhs2); |
| 4520 |
} |
| 4521 |
else |
| 4522 |
{ |
| 4523 |
if (TREE_CODE (rhs2) != SSA_NAME |
| 4524 |
|| !gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs2))) |
| 4525 |
return false; |
| 4526 |
rhs2 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs2)); |
| 4527 |
tree type2 = TREE_TYPE (rhs2); |
| 4528 |
if (!INTEGRAL_TYPE_P (type2) |
| 4529 |
|| !TYPE_UNSIGNED (type2) |
| 4530 |
|| TYPE_PRECISION (type2) >= TYPE_PRECISION (type) |
| 4531 |
|| (TYPE_PRECISION (type2) |
| 4532 |
!= GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type2)))) |
| 4533 |
return false; |
| 4534 |
} |
| 4535 |
if (TYPE_PRECISION (type1) >= TYPE_PRECISION (TREE_TYPE (rhs2))) |
| 4536 |
type = type1; |
| 4537 |
else |
| 4538 |
type = TREE_TYPE (rhs2); |
| 4539 |
|
| 4540 |
if (TREE_CODE (type) != INTEGER_TYPE |
| 4541 |
|| optab_handler (uaddv4_optab, |
| 4542 |
TYPE_MODE (type)) == CODE_FOR_nothing) |
| 4543 |
return false; |
| 4544 |
|
| 4545 |
maxval = wide_int_to_tree (type, wi::max_value (TYPE_PRECISION (type), |
| 4546 |
UNSIGNED)); |
| 4547 |
ovf_use_seen = false; |
| 4548 |
use_seen = false; |
| 4549 |
basic_block use_bb = NULL; |
| 4550 |
FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) |
| 4551 |
{ |
| 4552 |
use_stmt = USE_STMT (use_p); |
| 4553 |
if (is_gimple_debug (use_stmt)) |
| 4554 |
continue; |
| 4555 |
|
| 4556 |
if (arith_overflow_check_p (stmt, NULL, use_stmt, maxval, NULL)) |
| 4557 |
{ |
| 4558 |
ovf_use_seen = true; |
| 4559 |
use_bb = gimple_bb (use_stmt); |
| 4560 |
} |
| 4561 |
else |
| 4562 |
{ |
| 4563 |
if (!gimple_assign_cast_p (use_stmt) |
| 4564 |
|| gimple_assign_rhs_code (use_stmt) == VIEW_CONVERT_EXPR) |
| 4565 |
return false; |
| 4566 |
tree use_lhs = gimple_assign_lhs (use_stmt); |
| 4567 |
if (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs)) |
| 4568 |
|| (TYPE_PRECISION (TREE_TYPE (use_lhs)) |
| 4569 |
> TYPE_PRECISION (type))) |
| 4570 |
return false; |
| 4571 |
use_seen = true; |
| 4572 |
} |
| 4573 |
} |
| 4574 |
if (!ovf_use_seen) |
| 4575 |
return false; |
| 4576 |
if (!useless_type_conversion_p (type, TREE_TYPE (rhs1))) |
| 4577 |
{ |
| 4578 |
if (!use_seen) |
| 4579 |
return false; |
| 4580 |
tree new_rhs1 = make_ssa_name (type); |
| 4581 |
gimple *g = gimple_build_assign (new_rhs1, NOP_EXPR, rhs1); |
| 4582 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 4583 |
rhs1 = new_rhs1; |
| 4584 |
} |
| 4585 |
else if (!useless_type_conversion_p (type, TREE_TYPE (rhs2))) |
| 4586 |
{ |
| 4587 |
if (!use_seen) |
| 4588 |
return false; |
| 4589 |
tree new_rhs2 = make_ssa_name (type); |
| 4590 |
gimple *g = gimple_build_assign (new_rhs2, NOP_EXPR, rhs2); |
| 4591 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 4592 |
rhs2 = new_rhs2; |
| 4593 |
} |
| 4594 |
else if (!use_seen) |
| 4595 |
{ |
| 4596 |
/* If there are no uses of the wider addition, check if |
| 4597 |
forwprop has not created a narrower addition. |
| 4598 |
Require it to be in the same bb as the overflow check. */ |
| 4599 |
FOR_EACH_IMM_USE_FAST (use_p, iter, rhs1) |
| 4600 |
{ |
| 4601 |
use_stmt = USE_STMT (use_p); |
| 4602 |
if (is_gimple_debug (use_stmt)) |
| 4603 |
continue; |
| 4604 |
|
| 4605 |
if (use_stmt == stmt) |
| 4606 |
continue; |
| 4607 |
|
| 4608 |
if (!is_gimple_assign (use_stmt) |
| 4609 |
|| gimple_bb (use_stmt) != use_bb |
| 4610 |
|| gimple_assign_rhs_code (use_stmt) != PLUS_EXPR) |
| 4611 |
continue; |
| 4612 |
|
| 4613 |
if (gimple_assign_rhs1 (use_stmt) == rhs1) |
| 4614 |
{ |
| 4615 |
if (!operand_equal_p (gimple_assign_rhs2 (use_stmt), |
| 4616 |
rhs2, 0)) |
| 4617 |
continue; |
| 4618 |
} |
| 4619 |
else if (gimple_assign_rhs2 (use_stmt) == rhs1) |
| 4620 |
{ |
| 4621 |
if (gimple_assign_rhs1 (use_stmt) != rhs2) |
| 4622 |
continue; |
| 4623 |
} |
| 4624 |
else |
| 4625 |
continue; |
| 4626 |
|
| 4627 |
add_stmt = use_stmt; |
| 4628 |
break; |
| 4629 |
} |
| 4630 |
if (add_stmt == NULL) |
| 4631 |
return false; |
| 4632 |
|
| 4633 |
/* If stmt and add_stmt are in the same bb, we need to find out |
| 4634 |
which one is earlier. If they are in different bbs, we've |
| 4635 |
checked add_stmt is in the same bb as one of the uses of the |
| 4636 |
stmt lhs, so stmt needs to dominate add_stmt too. */ |
| 4637 |
if (gimple_bb (stmt) == gimple_bb (add_stmt)) |
| 4638 |
{ |
| 4639 |
gimple_stmt_iterator gsif = *gsi; |
| 4640 |
gimple_stmt_iterator gsib = *gsi; |
| 4641 |
int i; |
| 4642 |
/* Search both forward and backward from stmt and have a small |
| 4643 |
upper bound. */ |
| 4644 |
for (i = 0; i < 128; i++) |
| 4645 |
{ |
| 4646 |
if (!gsi_end_p (gsib)) |
| 4647 |
{ |
| 4648 |
gsi_prev_nondebug (&gsib); |
| 4649 |
if (gsi_stmt (gsib) == add_stmt) |
| 4650 |
{ |
| 4651 |
add_first = true; |
| 4652 |
break; |
| 4653 |
} |
| 4654 |
} |
| 4655 |
else if (gsi_end_p (gsif)) |
| 4656 |
break; |
| 4657 |
if (!gsi_end_p (gsif)) |
| 4658 |
{ |
| 4659 |
gsi_next_nondebug (&gsif); |
| 4660 |
if (gsi_stmt (gsif) == add_stmt) |
| 4661 |
break; |
| 4662 |
} |
| 4663 |
} |
| 4664 |
if (i == 128) |
| 4665 |
return false; |
| 4666 |
if (add_first) |
| 4667 |
*gsi = gsi_for_stmt (add_stmt); |
| 4668 |
} |
| 4669 |
} |
| 4670 |
} |
| 4671 |
|
| 4672 |
if (code == BIT_NOT_EXPR) |
| 4673 |
*gsi = gsi_for_stmt (cond_stmt); |
| 4674 |
|
| 4675 |
auto_vec<gimple *, 8> mul_stmts; |
| 4676 |
if (code == MULT_EXPR && cast_stmt) |
| 4677 |
{ |
| 4678 |
type = TREE_TYPE (cast_lhs); |
| 4679 |
gimple *g = SSA_NAME_DEF_STMT (rhs1); |
| 4680 |
if (gimple_assign_cast_p (g) |
| 4681 |
&& useless_type_conversion_p (type, |
| 4682 |
TREE_TYPE (gimple_assign_rhs1 (g))) |
| 4683 |
&& !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g))) |
| 4684 |
rhs1 = gimple_assign_rhs1 (g); |
| 4685 |
else |
| 4686 |
{ |
| 4687 |
g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, rhs1); |
| 4688 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 4689 |
rhs1 = gimple_assign_lhs (g); |
| 4690 |
mul_stmts.quick_push (g); |
| 4691 |
} |
| 4692 |
if (TREE_CODE (rhs2) == INTEGER_CST) |
| 4693 |
rhs2 = fold_convert (type, rhs2); |
| 4694 |
else |
| 4695 |
{ |
| 4696 |
g = SSA_NAME_DEF_STMT (rhs2); |
| 4697 |
if (gimple_assign_cast_p (g) |
| 4698 |
&& useless_type_conversion_p (type, |
| 4699 |
TREE_TYPE (gimple_assign_rhs1 (g))) |
| 4700 |
&& !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (g))) |
| 4701 |
rhs2 = gimple_assign_rhs1 (g); |
| 4702 |
else |
| 4703 |
{ |
| 4704 |
g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, rhs2); |
| 4705 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 4706 |
rhs2 = gimple_assign_lhs (g); |
| 4707 |
mul_stmts.quick_push (g); |
| 4708 |
} |
| 4709 |
} |
| 4710 |
} |
| 4711 |
tree ctype = build_complex_type (type); |
| 4712 |
gcall *g = gimple_build_call_internal (code == MULT_EXPR |
| 4713 |
? IFN_MUL_OVERFLOW |
| 4714 |
: code != MINUS_EXPR |
| 4715 |
? IFN_ADD_OVERFLOW : IFN_SUB_OVERFLOW, |
| 4716 |
2, rhs1, rhs2); |
| 4717 |
tree ctmp = make_ssa_name (ctype); |
| 4718 |
gimple_call_set_lhs (g, ctmp); |
| 4719 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 4720 |
tree new_lhs = (maxval || cast_stmt) ? make_ssa_name (type) : lhs; |
| 4721 |
gassign *g2; |
| 4722 |
if (code != BIT_NOT_EXPR) |
| 4723 |
{ |
| 4724 |
g2 = gimple_build_assign (new_lhs, REALPART_EXPR, |
| 4725 |
build1 (REALPART_EXPR, type, ctmp)); |
| 4726 |
if (maxval || cast_stmt) |
| 4727 |
{ |
| 4728 |
gsi_insert_before (gsi, g2, GSI_SAME_STMT); |
| 4729 |
if (add_first) |
| 4730 |
*gsi = gsi_for_stmt (stmt); |
| 4731 |
} |
| 4732 |
else |
| 4733 |
gsi_replace (gsi, g2, true); |
| 4734 |
if (code == MULT_EXPR) |
| 4735 |
{ |
| 4736 |
mul_stmts.quick_push (g); |
| 4737 |
mul_stmts.quick_push (g2); |
| 4738 |
if (cast_stmt) |
| 4739 |
{ |
| 4740 |
g2 = gimple_build_assign (lhs, NOP_EXPR, new_lhs); |
| 4741 |
gsi_replace (gsi, g2, true); |
| 4742 |
mul_stmts.quick_push (g2); |
| 4743 |
} |
| 4744 |
} |
| 4745 |
} |
| 4746 |
tree ovf = make_ssa_name (type); |
| 4747 |
g2 = gimple_build_assign (ovf, IMAGPART_EXPR, |
| 4748 |
build1 (IMAGPART_EXPR, type, ctmp)); |
| 4749 |
if (code != BIT_NOT_EXPR) |
| 4750 |
gsi_insert_after (gsi, g2, GSI_NEW_STMT); |
| 4751 |
else |
| 4752 |
gsi_insert_before (gsi, g2, GSI_SAME_STMT); |
| 4753 |
if (code == MULT_EXPR) |
| 4754 |
mul_stmts.quick_push (g2); |
| 4755 |
|
| 4756 |
FOR_EACH_IMM_USE_STMT (use_stmt, iter, cast_lhs ? cast_lhs : lhs) |
| 4757 |
{ |
| 4758 |
if (is_gimple_debug (use_stmt)) |
| 4759 |
continue; |
| 4760 |
|
| 4761 |
gimple *orig_use_stmt = use_stmt; |
| 4762 |
int ovf_use = arith_overflow_check_p (stmt, cast_stmt, use_stmt, |
| 4763 |
maxval, NULL); |
| 4764 |
if (ovf_use == 0) |
| 4765 |
{ |
| 4766 |
gcc_assert (code != BIT_NOT_EXPR); |
| 4767 |
if (maxval) |
| 4768 |
{ |
| 4769 |
tree use_lhs = gimple_assign_lhs (use_stmt); |
| 4770 |
gimple_assign_set_rhs1 (use_stmt, new_lhs); |
| 4771 |
if (useless_type_conversion_p (TREE_TYPE (use_lhs), |
| 4772 |
TREE_TYPE (new_lhs))) |
| 4773 |
gimple_assign_set_rhs_code (use_stmt, SSA_NAME); |
| 4774 |
update_stmt (use_stmt); |
| 4775 |
} |
| 4776 |
continue; |
| 4777 |
} |
| 4778 |
if (gimple_code (use_stmt) == GIMPLE_COND) |
| 4779 |
{ |
| 4780 |
gcond *cond_stmt = as_a <gcond *> (use_stmt); |
| 4781 |
gimple_cond_set_lhs (cond_stmt, ovf); |
| 4782 |
gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0)); |
| 4783 |
gimple_cond_set_code (cond_stmt, ovf_use == 1 ? NE_EXPR : EQ_EXPR); |
| 4784 |
} |
| 4785 |
else |
| 4786 |
{ |
| 4787 |
gcc_checking_assert (is_gimple_assign (use_stmt)); |
| 4788 |
if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS) |
| 4789 |
{ |
| 4790 |
if (gimple_assign_rhs_code (use_stmt) == RSHIFT_EXPR) |
| 4791 |
{ |
| 4792 |
g2 = gimple_build_assign (make_ssa_name (boolean_type_node), |
| 4793 |
ovf_use == 1 ? NE_EXPR : EQ_EXPR, |
| 4794 |
ovf, build_int_cst (type, 0)); |
| 4795 |
gimple_stmt_iterator gsiu = gsi_for_stmt (use_stmt); |
| 4796 |
gsi_insert_before (&gsiu, g2, GSI_SAME_STMT); |
| 4797 |
gimple_assign_set_rhs_with_ops (&gsiu, NOP_EXPR, |
| 4798 |
gimple_assign_lhs (g2)); |
| 4799 |
update_stmt (use_stmt); |
| 4800 |
use_operand_p use; |
| 4801 |
single_imm_use (gimple_assign_lhs (use_stmt), &use, |
| 4802 |
&use_stmt); |
| 4803 |
if (gimple_code (use_stmt) == GIMPLE_COND) |
| 4804 |
{ |
| 4805 |
gcond *cond_stmt = as_a <gcond *> (use_stmt); |
| 4806 |
gimple_cond_set_lhs (cond_stmt, ovf); |
| 4807 |
gimple_cond_set_rhs (cond_stmt, build_int_cst (type, 0)); |
| 4808 |
} |
| 4809 |
else |
| 4810 |
{ |
| 4811 |
gcc_checking_assert (is_gimple_assign (use_stmt)); |
| 4812 |
if (gimple_assign_rhs_class (use_stmt) |
| 4813 |
== GIMPLE_BINARY_RHS) |
| 4814 |
{ |
| 4815 |
gimple_assign_set_rhs1 (use_stmt, ovf); |
| 4816 |
gimple_assign_set_rhs2 (use_stmt, |
| 4817 |
build_int_cst (type, 0)); |
| 4818 |
} |
| 4819 |
else if (gimple_assign_cast_p (use_stmt)) |
| 4820 |
gimple_assign_set_rhs1 (use_stmt, ovf); |
| 4821 |
else |
| 4822 |
{ |
| 4823 |
tree_code sc = gimple_assign_rhs_code (use_stmt); |
| 4824 |
gcc_checking_assert (sc == COND_EXPR); |
| 4825 |
tree cond = gimple_assign_rhs1 (use_stmt); |
| 4826 |
cond = build2 (TREE_CODE (cond), |
| 4827 |
boolean_type_node, ovf, |
| 4828 |
build_int_cst (type, 0)); |
| 4829 |
gimple_assign_set_rhs1 (use_stmt, cond); |
| 4830 |
} |
| 4831 |
} |
| 4832 |
update_stmt (use_stmt); |
| 4833 |
gsi_remove (&gsiu, true); |
| 4834 |
gsiu = gsi_for_stmt (g2); |
| 4835 |
gsi_remove (&gsiu, true); |
| 4836 |
continue; |
| 4837 |
} |
| 4838 |
else |
| 4839 |
{ |
| 4840 |
gimple_assign_set_rhs1 (use_stmt, ovf); |
| 4841 |
gimple_assign_set_rhs2 (use_stmt, build_int_cst (type, 0)); |
| 4842 |
gimple_assign_set_rhs_code (use_stmt, |
| 4843 |
ovf_use == 1 |
| 4844 |
? NE_EXPR : EQ_EXPR); |
| 4845 |
} |
| 4846 |
} |
| 4847 |
else |
| 4848 |
{ |
| 4849 |
gcc_checking_assert (gimple_assign_rhs_code (use_stmt) |
| 4850 |
== COND_EXPR); |
| 4851 |
tree cond = build2 (ovf_use == 1 ? NE_EXPR : EQ_EXPR, |
| 4852 |
boolean_type_node, ovf, |
| 4853 |
build_int_cst (type, 0)); |
| 4854 |
gimple_assign_set_rhs1 (use_stmt, cond); |
| 4855 |
} |
| 4856 |
} |
| 4857 |
update_stmt (use_stmt); |
| 4858 |
if (code == MULT_EXPR && use_stmt != orig_use_stmt) |
| 4859 |
{ |
| 4860 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (orig_use_stmt); |
| 4861 |
maybe_optimize_guarding_check (mul_stmts, use_stmt, orig_use_stmt, |
| 4862 |
cfg_changed); |
| 4863 |
use_operand_p use; |
| 4864 |
gimple *cast_stmt; |
| 4865 |
if (single_imm_use (gimple_assign_lhs (orig_use_stmt), &use, |
| 4866 |
&cast_stmt) |
| 4867 |
&& gimple_assign_cast_p (cast_stmt)) |
| 4868 |
{ |
| 4869 |
gimple_stmt_iterator gsi3 = gsi_for_stmt (cast_stmt); |
| 4870 |
gsi_remove (&gsi3, true); |
| 4871 |
release_ssa_name (gimple_assign_lhs (cast_stmt)); |
| 4872 |
} |
| 4873 |
gsi_remove (&gsi2, true); |
| 4874 |
release_ssa_name (gimple_assign_lhs (orig_use_stmt)); |
| 4875 |
} |
| 4876 |
} |
| 4877 |
if (maxval) |
| 4878 |
{ |
| 4879 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (stmt); |
| 4880 |
gsi_remove (&gsi2, true); |
| 4881 |
if (add_stmt) |
| 4882 |
{ |
| 4883 |
gimple *g = gimple_build_assign (gimple_assign_lhs (add_stmt), |
| 4884 |
new_lhs); |
| 4885 |
gsi2 = gsi_for_stmt (add_stmt); |
| 4886 |
gsi_replace (&gsi2, g, true); |
| 4887 |
} |
| 4888 |
} |
| 4889 |
else if (code == BIT_NOT_EXPR) |
| 4890 |
{ |
| 4891 |
*gsi = gsi_for_stmt (stmt); |
| 4892 |
gsi_remove (gsi, true); |
| 4893 |
release_ssa_name (lhs); |
| 4894 |
return true; |
| 4895 |
} |
| 4896 |
return false; |
| 4897 |
} |
| 4898 |
|
| 4899 |
/* Helper of match_uaddc_usubc. Look through an integral cast |
| 4900 |
which should preserve [0, 1] range value (unless source has |
| 4901 |
1-bit signed type) and the cast has single use. */ |
| 4902 |
|
| 4903 |
static gimple * |
| 4904 |
uaddc_cast (gimple *g) |
| 4905 |
{ |
| 4906 |
if (!gimple_assign_cast_p (g)) |
| 4907 |
return g; |
| 4908 |
tree op = gimple_assign_rhs1 (g); |
| 4909 |
if (TREE_CODE (op) == SSA_NAME |
| 4910 |
&& INTEGRAL_TYPE_P (TREE_TYPE (op)) |
| 4911 |
&& (TYPE_PRECISION (TREE_TYPE (op)) > 1 |
| 4912 |
|| TYPE_UNSIGNED (TREE_TYPE (op))) |
| 4913 |
&& has_single_use (gimple_assign_lhs (g))) |
| 4914 |
return SSA_NAME_DEF_STMT (op); |
| 4915 |
return g; |
| 4916 |
} |
| 4917 |
|
| 4918 |
/* Helper of match_uaddc_usubc. Look through a NE_EXPR |
| 4919 |
comparison with 0 which also preserves [0, 1] value range. */ |
| 4920 |
|
| 4921 |
static gimple * |
| 4922 |
uaddc_ne0 (gimple *g) |
| 4923 |
{ |
| 4924 |
if (is_gimple_assign (g) |
| 4925 |
&& gimple_assign_rhs_code (g) == NE_EXPR |
| 4926 |
&& integer_zerop (gimple_assign_rhs2 (g)) |
| 4927 |
&& TREE_CODE (gimple_assign_rhs1 (g)) == SSA_NAME |
| 4928 |
&& has_single_use (gimple_assign_lhs (g))) |
| 4929 |
return SSA_NAME_DEF_STMT (gimple_assign_rhs1 (g)); |
| 4930 |
return g; |
| 4931 |
} |
| 4932 |
|
| 4933 |
/* Return true if G is {REAL,IMAG}PART_EXPR PART with SSA_NAME |
| 4934 |
operand. */ |
| 4935 |
|
| 4936 |
static bool |
| 4937 |
uaddc_is_cplxpart (gimple *g, tree_code part) |
| 4938 |
{ |
| 4939 |
return (is_gimple_assign (g) |
| 4940 |
&& gimple_assign_rhs_code (g) == part |
| 4941 |
&& TREE_CODE (TREE_OPERAND (gimple_assign_rhs1 (g), 0)) == SSA_NAME); |
| 4942 |
} |
| 4943 |
|
| 4944 |
/* Try to match e.g. |
| 4945 |
_29 = .ADD_OVERFLOW (_3, _4); |
| 4946 |
_30 = REALPART_EXPR <_29>; |
| 4947 |
_31 = IMAGPART_EXPR <_29>; |
| 4948 |
_32 = .ADD_OVERFLOW (_30, _38); |
| 4949 |
_33 = REALPART_EXPR <_32>; |
| 4950 |
_34 = IMAGPART_EXPR <_32>; |
| 4951 |
_35 = _31 + _34; |
| 4952 |
as |
| 4953 |
_36 = .UADDC (_3, _4, _38); |
| 4954 |
_33 = REALPART_EXPR <_36>; |
| 4955 |
_35 = IMAGPART_EXPR <_36>; |
| 4956 |
or |
| 4957 |
_22 = .SUB_OVERFLOW (_6, _5); |
| 4958 |
_23 = REALPART_EXPR <_22>; |
| 4959 |
_24 = IMAGPART_EXPR <_22>; |
| 4960 |
_25 = .SUB_OVERFLOW (_23, _37); |
| 4961 |
_26 = REALPART_EXPR <_25>; |
| 4962 |
_27 = IMAGPART_EXPR <_25>; |
| 4963 |
_28 = _24 | _27; |
| 4964 |
as |
| 4965 |
_29 = .USUBC (_6, _5, _37); |
| 4966 |
_26 = REALPART_EXPR <_29>; |
| 4967 |
_288 = IMAGPART_EXPR <_29>; |
| 4968 |
provided _38 or _37 above have [0, 1] range |
| 4969 |
and _3, _4 and _30 or _6, _5 and _23 are unsigned |
| 4970 |
integral types with the same precision. Whether + or | or ^ is |
| 4971 |
used on the IMAGPART_EXPR results doesn't matter, with one of |
| 4972 |
added or subtracted operands in [0, 1] range at most one |
| 4973 |
.ADD_OVERFLOW or .SUB_OVERFLOW will indicate overflow. */ |
| 4974 |
|
| 4975 |
static bool |
| 4976 |
match_uaddc_usubc (gimple_stmt_iterator *gsi, gimple *stmt, tree_code code) |
| 4977 |
{ |
| 4978 |
tree rhs[4]; |
| 4979 |
rhs[0] = gimple_assign_rhs1 (stmt); |
| 4980 |
rhs[1] = gimple_assign_rhs2 (stmt); |
| 4981 |
rhs[2] = NULL_TREE; |
| 4982 |
rhs[3] = NULL_TREE; |
| 4983 |
tree type = TREE_TYPE (rhs[0]); |
| 4984 |
if (!INTEGRAL_TYPE_P (type) || !TYPE_UNSIGNED (type)) |
| 4985 |
return false; |
| 4986 |
|
| 4987 |
auto_vec<gimple *, 2> temp_stmts; |
| 4988 |
if (code != BIT_IOR_EXPR && code != BIT_XOR_EXPR) |
| 4989 |
{ |
| 4990 |
/* If overflow flag is ignored on the MSB limb, we can end up with |
| 4991 |
the most significant limb handled as r = op1 + op2 + ovf1 + ovf2; |
| 4992 |
or r = op1 - op2 - ovf1 - ovf2; or various equivalent expressions |
| 4993 |
thereof. Handle those like the ovf = ovf1 + ovf2; case to recognize |
| 4994 |
the limb below the MSB, but also create another .UADDC/.USUBC call |
| 4995 |
for the last limb. |
| 4996 |
|
| 4997 |
First look through assignments with the same rhs code as CODE, |
| 4998 |
with the exception that subtraction of a constant is canonicalized |
| 4999 |
into addition of its negation. rhs[0] will be minuend for |
| 5000 |
subtractions and one of addends for addition, all other assigned |
| 5001 |
rhs[i] operands will be subtrahends or other addends. */ |
| 5002 |
while (TREE_CODE (rhs[0]) == SSA_NAME && !rhs[3]) |
| 5003 |
{ |
| 5004 |
gimple *g = SSA_NAME_DEF_STMT (rhs[0]); |
| 5005 |
if (has_single_use (rhs[0]) |
| 5006 |
&& is_gimple_assign (g) |
| 5007 |
&& (gimple_assign_rhs_code (g) == code |
| 5008 |
|| (code == MINUS_EXPR |
| 5009 |
&& gimple_assign_rhs_code (g) == PLUS_EXPR |
| 5010 |
&& TREE_CODE (gimple_assign_rhs2 (g)) == INTEGER_CST))) |
| 5011 |
{ |
| 5012 |
tree r2 = gimple_assign_rhs2 (g); |
| 5013 |
if (gimple_assign_rhs_code (g) != code) |
| 5014 |
{ |
| 5015 |
r2 = const_unop (NEGATE_EXPR, TREE_TYPE (r2), r2); |
| 5016 |
if (!r2) |
| 5017 |
break; |
| 5018 |
} |
| 5019 |
rhs[0] = gimple_assign_rhs1 (g); |
| 5020 |
tree &r = rhs[2] ? rhs[3] : rhs[2]; |
| 5021 |
r = r2; |
| 5022 |
temp_stmts.quick_push (g); |
| 5023 |
} |
| 5024 |
else |
| 5025 |
break; |
| 5026 |
} |
| 5027 |
for (int i = 1; i <= 2; ++i) |
| 5028 |
while (rhs[i] && TREE_CODE (rhs[i]) == SSA_NAME && !rhs[3]) |
| 5029 |
{ |
| 5030 |
gimple *g = SSA_NAME_DEF_STMT (rhs[i]); |
| 5031 |
if (has_single_use (rhs[i]) |
| 5032 |
&& is_gimple_assign (g) |
| 5033 |
&& gimple_assign_rhs_code (g) == PLUS_EXPR) |
| 5034 |
{ |
| 5035 |
rhs[i] = gimple_assign_rhs1 (g); |
| 5036 |
if (rhs[2]) |
| 5037 |
rhs[3] = gimple_assign_rhs2 (g); |
| 5038 |
else |
| 5039 |
rhs[2] = gimple_assign_rhs2 (g); |
| 5040 |
temp_stmts.quick_push (g); |
| 5041 |
} |
| 5042 |
else |
| 5043 |
break; |
| 5044 |
} |
| 5045 |
/* If there are just 3 addends or one minuend and two subtrahends, |
| 5046 |
check for UADDC or USUBC being pattern recognized earlier. |
| 5047 |
Say r = op1 + op2 + ovf1 + ovf2; where the (ovf1 + ovf2) part |
| 5048 |
got pattern matched earlier as __imag__ .UADDC (arg1, arg2, arg3) |
| 5049 |
etc. */ |
| 5050 |
if (rhs[2] && !rhs[3]) |
| 5051 |
{ |
| 5052 |
for (int i = (code == MINUS_EXPR ? 1 : 0); i < 3; ++i) |
| 5053 |
if (TREE_CODE (rhs[i]) == SSA_NAME) |
| 5054 |
{ |
| 5055 |
gimple *im = uaddc_cast (SSA_NAME_DEF_STMT (rhs[i])); |
| 5056 |
im = uaddc_ne0 (im); |
| 5057 |
if (uaddc_is_cplxpart (im, IMAGPART_EXPR)) |
| 5058 |
{ |
| 5059 |
/* We found one of the 3 addends or 2 subtrahends to be |
| 5060 |
__imag__ of something, verify it is .UADDC/.USUBC. */ |
| 5061 |
tree rhs1 = gimple_assign_rhs1 (im); |
| 5062 |
gimple *ovf = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs1, 0)); |
| 5063 |
tree ovf_lhs = NULL_TREE; |
| 5064 |
tree ovf_arg1 = NULL_TREE, ovf_arg2 = NULL_TREE; |
| 5065 |
if (gimple_call_internal_p (ovf, code == PLUS_EXPR |
| 5066 |
? IFN_ADD_OVERFLOW |
| 5067 |
: IFN_SUB_OVERFLOW)) |
| 5068 |
{ |
| 5069 |
/* Or verify it is .ADD_OVERFLOW/.SUB_OVERFLOW. |
| 5070 |
This is for the case of 2 chained .UADDC/.USUBC, |
| 5071 |
where the first one uses 0 carry-in and the second |
| 5072 |
one ignores the carry-out. |
| 5073 |
So, something like: |
| 5074 |
_16 = .ADD_OVERFLOW (_1, _2); |
| 5075 |
_17 = REALPART_EXPR <_16>; |
| 5076 |
_18 = IMAGPART_EXPR <_16>; |
| 5077 |
_15 = _3 + _4; |
| 5078 |
_12 = _15 + _18; |
| 5079 |
where the first 3 statements come from the lower |
| 5080 |
limb addition and the last 2 from the higher limb |
| 5081 |
which ignores carry-out. */ |
| 5082 |
ovf_lhs = gimple_call_lhs (ovf); |
| 5083 |
tree ovf_lhs_type = TREE_TYPE (TREE_TYPE (ovf_lhs)); |
| 5084 |
ovf_arg1 = gimple_call_arg (ovf, 0); |
| 5085 |
ovf_arg2 = gimple_call_arg (ovf, 1); |
| 5086 |
/* In that case we need to punt if the types don't |
| 5087 |
mismatch. */ |
| 5088 |
if (!types_compatible_p (type, ovf_lhs_type) |
| 5089 |
|| !types_compatible_p (type, TREE_TYPE (ovf_arg1)) |
| 5090 |
|| !types_compatible_p (type, |
| 5091 |
TREE_TYPE (ovf_arg2))) |
| 5092 |
ovf_lhs = NULL_TREE; |
| 5093 |
else |
| 5094 |
{ |
| 5095 |
for (int i = (code == PLUS_EXPR ? 1 : 0); |
| 5096 |
i >= 0; --i) |
| 5097 |
{ |
| 5098 |
tree r = gimple_call_arg (ovf, i); |
| 5099 |
if (TREE_CODE (r) != SSA_NAME) |
| 5100 |
continue; |
| 5101 |
if (uaddc_is_cplxpart (SSA_NAME_DEF_STMT (r), |
| 5102 |
REALPART_EXPR)) |
| 5103 |
{ |
| 5104 |
/* Punt if one of the args which isn't |
| 5105 |
subtracted isn't __real__; that could |
| 5106 |
then prevent better match later. |
| 5107 |
Consider: |
| 5108 |
_3 = .ADD_OVERFLOW (_1, _2); |
| 5109 |
_4 = REALPART_EXPR <_3>; |
| 5110 |
_5 = IMAGPART_EXPR <_3>; |
| 5111 |
_7 = .ADD_OVERFLOW (_4, _6); |
| 5112 |
_8 = REALPART_EXPR <_7>; |
| 5113 |
_9 = IMAGPART_EXPR <_7>; |
| 5114 |
_12 = _10 + _11; |
| 5115 |
_13 = _12 + _9; |
| 5116 |
_14 = _13 + _5; |
| 5117 |
We want to match this when called on |
| 5118 |
the last stmt as a pair of .UADDC calls, |
| 5119 |
but without this check we could turn |
| 5120 |
that prematurely on _13 = _12 + _9; |
| 5121 |
stmt into .UADDC with 0 carry-in just |
| 5122 |
on the second .ADD_OVERFLOW call and |
| 5123 |
another replacing the _12 and _13 |
| 5124 |
additions. */ |
| 5125 |
ovf_lhs = NULL_TREE; |
| 5126 |
break; |
| 5127 |
} |
| 5128 |
} |
| 5129 |
} |
| 5130 |
if (ovf_lhs) |
| 5131 |
{ |
| 5132 |
use_operand_p use_p; |
| 5133 |
imm_use_iterator iter; |
| 5134 |
tree re_lhs = NULL_TREE; |
| 5135 |
FOR_EACH_IMM_USE_FAST (use_p, iter, ovf_lhs) |
| 5136 |
{ |
| 5137 |
gimple *use_stmt = USE_STMT (use_p); |
| 5138 |
if (is_gimple_debug (use_stmt)) |
| 5139 |
continue; |
| 5140 |
if (use_stmt == im) |
| 5141 |
continue; |
| 5142 |
if (!uaddc_is_cplxpart (use_stmt, |
| 5143 |
REALPART_EXPR)) |
| 5144 |
{ |
| 5145 |
ovf_lhs = NULL_TREE; |
| 5146 |
break; |
| 5147 |
} |
| 5148 |
re_lhs = gimple_assign_lhs (use_stmt); |
| 5149 |
} |
| 5150 |
if (ovf_lhs && re_lhs) |
| 5151 |
{ |
| 5152 |
FOR_EACH_IMM_USE_FAST (use_p, iter, re_lhs) |
| 5153 |
{ |
| 5154 |
gimple *use_stmt = USE_STMT (use_p); |
| 5155 |
if (is_gimple_debug (use_stmt)) |
| 5156 |
continue; |
| 5157 |
internal_fn ifn |
| 5158 |
= gimple_call_internal_fn (ovf); |
| 5159 |
/* Punt if the __real__ of lhs is used |
| 5160 |
in the same .*_OVERFLOW call. |
| 5161 |
Consider: |
| 5162 |
_3 = .ADD_OVERFLOW (_1, _2); |
| 5163 |
_4 = REALPART_EXPR <_3>; |
| 5164 |
_5 = IMAGPART_EXPR <_3>; |
| 5165 |
_7 = .ADD_OVERFLOW (_4, _6); |
| 5166 |
_8 = REALPART_EXPR <_7>; |
| 5167 |
_9 = IMAGPART_EXPR <_7>; |
| 5168 |
_12 = _10 + _11; |
| 5169 |
_13 = _12 + _5; |
| 5170 |
_14 = _13 + _9; |
| 5171 |
We want to match this when called on |
| 5172 |
the last stmt as a pair of .UADDC calls, |
| 5173 |
but without this check we could turn |
| 5174 |
that prematurely on _13 = _12 + _5; |
| 5175 |
stmt into .UADDC with 0 carry-in just |
| 5176 |
on the first .ADD_OVERFLOW call and |
| 5177 |
another replacing the _12 and _13 |
| 5178 |
additions. */ |
| 5179 |
if (gimple_call_internal_p (use_stmt, ifn)) |
| 5180 |
{ |
| 5181 |
ovf_lhs = NULL_TREE; |
| 5182 |
break; |
| 5183 |
} |
| 5184 |
} |
| 5185 |
} |
| 5186 |
} |
| 5187 |
} |
| 5188 |
if ((ovf_lhs |
| 5189 |
|| gimple_call_internal_p (ovf, |
| 5190 |
code == PLUS_EXPR |
| 5191 |
? IFN_UADDC : IFN_USUBC)) |
| 5192 |
&& (optab_handler (code == PLUS_EXPR |
| 5193 |
? uaddc5_optab : usubc5_optab, |
| 5194 |
TYPE_MODE (type)) |
| 5195 |
!= CODE_FOR_nothing)) |
| 5196 |
{ |
| 5197 |
/* And in that case build another .UADDC/.USUBC |
| 5198 |
call for the most significand limb addition. |
| 5199 |
Overflow bit is ignored here. */ |
| 5200 |
if (i != 2) |
| 5201 |
std::swap (rhs[i], rhs[2]); |
| 5202 |
gimple *g |
| 5203 |
= gimple_build_call_internal (code == PLUS_EXPR |
| 5204 |
? IFN_UADDC |
| 5205 |
: IFN_USUBC, |
| 5206 |
3, rhs[0], rhs[1], |
| 5207 |
rhs[2]); |
| 5208 |
tree nlhs = make_ssa_name (build_complex_type (type)); |
| 5209 |
gimple_call_set_lhs (g, nlhs); |
| 5210 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 5211 |
tree ilhs = gimple_assign_lhs (stmt); |
| 5212 |
g = gimple_build_assign (ilhs, REALPART_EXPR, |
| 5213 |
build1 (REALPART_EXPR, |
| 5214 |
TREE_TYPE (ilhs), |
| 5215 |
nlhs)); |
| 5216 |
gsi_replace (gsi, g, true); |
| 5217 |
/* And if it is initialized from result of __imag__ |
| 5218 |
of .{ADD,SUB}_OVERFLOW call, replace that |
| 5219 |
call with .U{ADD,SUB}C call with the same arguments, |
| 5220 |
just 0 added as third argument. This isn't strictly |
| 5221 |
necessary, .ADD_OVERFLOW (x, y) and .UADDC (x, y, 0) |
| 5222 |
produce the same result, but may result in better |
| 5223 |
generated code on some targets where the backend can |
| 5224 |
better prepare in how the result will be used. */ |
| 5225 |
if (ovf_lhs) |
| 5226 |
{ |
| 5227 |
tree zero = build_zero_cst (type); |
| 5228 |
g = gimple_build_call_internal (code == PLUS_EXPR |
| 5229 |
? IFN_UADDC |
| 5230 |
: IFN_USUBC, |
| 5231 |
3, ovf_arg1, |
| 5232 |
ovf_arg2, zero); |
| 5233 |
gimple_call_set_lhs (g, ovf_lhs); |
| 5234 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (ovf); |
| 5235 |
gsi_replace (&gsi2, g, true); |
| 5236 |
} |
| 5237 |
return true; |
| 5238 |
} |
| 5239 |
} |
| 5240 |
} |
| 5241 |
return false; |
| 5242 |
} |
| 5243 |
if (code == MINUS_EXPR && !rhs[2]) |
| 5244 |
return false; |
| 5245 |
if (code == MINUS_EXPR) |
| 5246 |
/* Code below expects rhs[0] and rhs[1] to have the IMAGPART_EXPRs. |
| 5247 |
So, for MINUS_EXPR swap the single added rhs operand (others are |
| 5248 |
subtracted) to rhs[3]. */ |
| 5249 |
std::swap (rhs[0], rhs[3]); |
| 5250 |
} |
| 5251 |
/* Walk from both operands of STMT (for +/- even sometimes from |
| 5252 |
all the 4 addends or 3 subtrahends), see through casts and != 0 |
| 5253 |
statements which would preserve [0, 1] range of values and |
| 5254 |
check which is initialized from __imag__. */ |
| 5255 |
gimple *im1 = NULL, *im2 = NULL; |
| 5256 |
for (int i = 0; i < (code == MINUS_EXPR ? 3 : 4); i++) |
| 5257 |
if (rhs[i] && TREE_CODE (rhs[i]) == SSA_NAME) |
| 5258 |
{ |
| 5259 |
gimple *im = uaddc_cast (SSA_NAME_DEF_STMT (rhs[i])); |
| 5260 |
im = uaddc_ne0 (im); |
| 5261 |
if (uaddc_is_cplxpart (im, IMAGPART_EXPR)) |
| 5262 |
{ |
| 5263 |
if (im1 == NULL) |
| 5264 |
{ |
| 5265 |
im1 = im; |
| 5266 |
if (i != 0) |
| 5267 |
std::swap (rhs[0], rhs[i]); |
| 5268 |
} |
| 5269 |
else |
| 5270 |
{ |
| 5271 |
im2 = im; |
| 5272 |
if (i != 1) |
| 5273 |
std::swap (rhs[1], rhs[i]); |
| 5274 |
break; |
| 5275 |
} |
| 5276 |
} |
| 5277 |
} |
| 5278 |
/* If we don't find at least two, punt. */ |
| 5279 |
if (!im2) |
| 5280 |
return false; |
| 5281 |
/* Check they are __imag__ of .ADD_OVERFLOW or .SUB_OVERFLOW call results, |
| 5282 |
either both .ADD_OVERFLOW or both .SUB_OVERFLOW and that we have |
| 5283 |
uaddc5/usubc5 named pattern for the corresponding mode. */ |
| 5284 |
gimple *ovf1 |
| 5285 |
= SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im1), 0)); |
| 5286 |
gimple *ovf2 |
| 5287 |
= SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im2), 0)); |
| 5288 |
internal_fn ifn; |
| 5289 |
if (!is_gimple_call (ovf1) |
| 5290 |
|| !gimple_call_internal_p (ovf1) |
| 5291 |
|| ((ifn = gimple_call_internal_fn (ovf1)) != IFN_ADD_OVERFLOW |
| 5292 |
&& ifn != IFN_SUB_OVERFLOW) |
| 5293 |
|| !gimple_call_internal_p (ovf2, ifn) |
| 5294 |
|| optab_handler (ifn == IFN_ADD_OVERFLOW ? uaddc5_optab : usubc5_optab, |
| 5295 |
TYPE_MODE (type)) == CODE_FOR_nothing |
| 5296 |
|| (rhs[2] |
| 5297 |
&& optab_handler (code == PLUS_EXPR ? uaddc5_optab : usubc5_optab, |
| 5298 |
TYPE_MODE (type)) == CODE_FOR_nothing) |
| 5299 |
|| !types_compatible_p (type, |
| 5300 |
TREE_TYPE (TREE_TYPE (gimple_call_lhs (ovf1)))) |
| 5301 |
|| !types_compatible_p (type, |
| 5302 |
TREE_TYPE (TREE_TYPE (gimple_call_lhs (ovf2))))) |
| 5303 |
return false; |
| 5304 |
tree arg1, arg2, arg3 = NULL_TREE; |
| 5305 |
gimple *re1 = NULL, *re2 = NULL; |
| 5306 |
/* On one of the two calls, one of the .ADD_OVERFLOW/.SUB_OVERFLOW arguments |
| 5307 |
should be initialized from __real__ of the other of the two calls. |
| 5308 |
Though, for .SUB_OVERFLOW, it has to be the first argument, not the |
| 5309 |
second one. */ |
| 5310 |
for (int i = (ifn == IFN_ADD_OVERFLOW ? 1 : 0); i >= 0; --i) |
| 5311 |
for (gimple *ovf = ovf1; ovf; ovf = (ovf == ovf1 ? ovf2 : NULL)) |
| 5312 |
{ |
| 5313 |
tree arg = gimple_call_arg (ovf, i); |
| 5314 |
if (TREE_CODE (arg) != SSA_NAME) |
| 5315 |
continue; |
| 5316 |
re1 = SSA_NAME_DEF_STMT (arg); |
| 5317 |
if (uaddc_is_cplxpart (re1, REALPART_EXPR) |
| 5318 |
&& (SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (re1), 0)) |
| 5319 |
== (ovf == ovf1 ? ovf2 : ovf1))) |
| 5320 |
{ |
| 5321 |
if (ovf == ovf1) |
| 5322 |
{ |
| 5323 |
/* Make sure ovf2 is the .*_OVERFLOW call with argument |
| 5324 |
initialized from __real__ of ovf1. */ |
| 5325 |
std::swap (rhs[0], rhs[1]); |
| 5326 |
std::swap (im1, im2); |
| 5327 |
std::swap (ovf1, ovf2); |
| 5328 |
} |
| 5329 |
arg3 = gimple_call_arg (ovf, 1 - i); |
| 5330 |
i = -1; |
| 5331 |
break; |
| 5332 |
} |
| 5333 |
} |
| 5334 |
if (!arg3) |
| 5335 |
return false; |
| 5336 |
arg1 = gimple_call_arg (ovf1, 0); |
| 5337 |
arg2 = gimple_call_arg (ovf1, 1); |
| 5338 |
if (!types_compatible_p (type, TREE_TYPE (arg1))) |
| 5339 |
return false; |
| 5340 |
int kind[2] = { 0, 0 }; |
| 5341 |
tree arg_im[2] = { NULL_TREE, NULL_TREE }; |
| 5342 |
/* At least one of arg2 and arg3 should have type compatible |
| 5343 |
with arg1/rhs[0], and the other one should have value in [0, 1] |
| 5344 |
range. If both are in [0, 1] range and type compatible with |
| 5345 |
arg1/rhs[0], try harder to find after looking through casts, |
| 5346 |
!= 0 comparisons which one is initialized to __imag__ of |
| 5347 |
.{ADD,SUB}_OVERFLOW or .U{ADD,SUB}C call results. */ |
| 5348 |
for (int i = 0; i < 2; ++i) |
| 5349 |
{ |
| 5350 |
tree arg = i == 0 ? arg2 : arg3; |
| 5351 |
if (types_compatible_p (type, TREE_TYPE (arg))) |
| 5352 |
kind[i] = 1; |
| 5353 |
if (!INTEGRAL_TYPE_P (TREE_TYPE (arg)) |
| 5354 |
|| (TYPE_PRECISION (TREE_TYPE (arg)) == 1 |
| 5355 |
&& !TYPE_UNSIGNED (TREE_TYPE (arg)))) |
| 5356 |
continue; |
| 5357 |
if (tree_zero_one_valued_p (arg)) |
| 5358 |
kind[i] |= 2; |
| 5359 |
if (TREE_CODE (arg) == SSA_NAME) |
| 5360 |
{ |
| 5361 |
gimple *g = SSA_NAME_DEF_STMT (arg); |
| 5362 |
if (gimple_assign_cast_p (g)) |
| 5363 |
{ |
| 5364 |
tree op = gimple_assign_rhs1 (g); |
| 5365 |
if (TREE_CODE (op) == SSA_NAME |
| 5366 |
&& INTEGRAL_TYPE_P (TREE_TYPE (op))) |
| 5367 |
g = SSA_NAME_DEF_STMT (op); |
| 5368 |
} |
| 5369 |
g = uaddc_ne0 (g); |
| 5370 |
if (!uaddc_is_cplxpart (g, IMAGPART_EXPR)) |
| 5371 |
continue; |
| 5372 |
arg_im[i] = gimple_assign_lhs (g); |
| 5373 |
g = SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (g), 0)); |
| 5374 |
if (!is_gimple_call (g) || !gimple_call_internal_p (g)) |
| 5375 |
continue; |
| 5376 |
switch (gimple_call_internal_fn (g)) |
| 5377 |
{ |
| 5378 |
case IFN_ADD_OVERFLOW: |
| 5379 |
case IFN_SUB_OVERFLOW: |
| 5380 |
case IFN_UADDC: |
| 5381 |
case IFN_USUBC: |
| 5382 |
break; |
| 5383 |
default: |
| 5384 |
continue; |
| 5385 |
} |
| 5386 |
kind[i] |= 4; |
| 5387 |
} |
| 5388 |
} |
| 5389 |
/* Make arg2 the one with compatible type and arg3 the one |
| 5390 |
with [0, 1] range. If both is true for both operands, |
| 5391 |
prefer as arg3 result of __imag__ of some ifn. */ |
| 5392 |
if ((kind[0] & 1) == 0 || ((kind[1] & 1) != 0 && kind[0] > kind[1])) |
| 5393 |
{ |
| 5394 |
std::swap (arg2, arg3); |
| 5395 |
std::swap (kind[0], kind[1]); |
| 5396 |
std::swap (arg_im[0], arg_im[1]); |
| 5397 |
} |
| 5398 |
if ((kind[0] & 1) == 0 || (kind[1] & 6) == 0) |
| 5399 |
return false; |
| 5400 |
if (!has_single_use (gimple_assign_lhs (im1)) |
| 5401 |
|| !has_single_use (gimple_assign_lhs (im2)) |
| 5402 |
|| !has_single_use (gimple_assign_lhs (re1)) |
| 5403 |
|| num_imm_uses (gimple_call_lhs (ovf1)) != 2) |
| 5404 |
return false; |
| 5405 |
/* Check that ovf2's result is used in __real__ and set re2 |
| 5406 |
to that statement. */ |
| 5407 |
use_operand_p use_p; |
| 5408 |
imm_use_iterator iter; |
| 5409 |
tree lhs = gimple_call_lhs (ovf2); |
| 5410 |
FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) |
| 5411 |
{ |
| 5412 |
gimple *use_stmt = USE_STMT (use_p); |
| 5413 |
if (is_gimple_debug (use_stmt)) |
| 5414 |
continue; |
| 5415 |
if (use_stmt == im2) |
| 5416 |
continue; |
| 5417 |
if (re2) |
| 5418 |
return false; |
| 5419 |
if (!uaddc_is_cplxpart (use_stmt, REALPART_EXPR)) |
| 5420 |
return false; |
| 5421 |
re2 = use_stmt; |
| 5422 |
} |
| 5423 |
/* Build .UADDC/.USUBC call which will be placed before the stmt. */ |
| 5424 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (ovf2); |
| 5425 |
gimple *g; |
| 5426 |
if ((kind[1] & 4) != 0 && types_compatible_p (type, TREE_TYPE (arg_im[1]))) |
| 5427 |
arg3 = arg_im[1]; |
| 5428 |
if ((kind[1] & 1) == 0) |
| 5429 |
{ |
| 5430 |
if (TREE_CODE (arg3) == INTEGER_CST) |
| 5431 |
arg3 = fold_convert (type, arg3); |
| 5432 |
else |
| 5433 |
{ |
| 5434 |
g = gimple_build_assign (make_ssa_name (type), NOP_EXPR, arg3); |
| 5435 |
gsi_insert_before (&gsi2, g, GSI_SAME_STMT); |
| 5436 |
arg3 = gimple_assign_lhs (g); |
| 5437 |
} |
| 5438 |
} |
| 5439 |
g = gimple_build_call_internal (ifn == IFN_ADD_OVERFLOW |
| 5440 |
? IFN_UADDC : IFN_USUBC, |
| 5441 |
3, arg1, arg2, arg3); |
| 5442 |
tree nlhs = make_ssa_name (TREE_TYPE (lhs)); |
| 5443 |
gimple_call_set_lhs (g, nlhs); |
| 5444 |
gsi_insert_before (&gsi2, g, GSI_SAME_STMT); |
| 5445 |
/* In the case where stmt is | or ^ of two overflow flags |
| 5446 |
or addition of those, replace stmt with __imag__ of the above |
| 5447 |
added call. In case of arg1 + arg2 + (ovf1 + ovf2) or |
| 5448 |
arg1 - arg2 - (ovf1 + ovf2) just emit it before stmt. */ |
| 5449 |
tree ilhs = rhs[2] ? make_ssa_name (type) : gimple_assign_lhs (stmt); |
| 5450 |
g = gimple_build_assign (ilhs, IMAGPART_EXPR, |
| 5451 |
build1 (IMAGPART_EXPR, TREE_TYPE (ilhs), nlhs)); |
| 5452 |
if (rhs[2]) |
| 5453 |
{ |
| 5454 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 5455 |
/* Remove some further statements which can't be kept in the IL because |
| 5456 |
they can use SSA_NAMEs whose setter is going to be removed too. */ |
| 5457 |
for (gimple *g2 : temp_stmts) |
| 5458 |
{ |
| 5459 |
gsi2 = gsi_for_stmt (g2); |
| 5460 |
gsi_remove (&gsi2, true); |
| 5461 |
release_defs (g2); |
| 5462 |
} |
| 5463 |
} |
| 5464 |
else |
| 5465 |
gsi_replace (gsi, g, true); |
| 5466 |
/* Remove some statements which can't be kept in the IL because they |
| 5467 |
use SSA_NAME whose setter is going to be removed too. */ |
| 5468 |
tree rhs1 = rhs[1]; |
| 5469 |
for (int i = 0; i < 2; i++) |
| 5470 |
if (rhs1 == gimple_assign_lhs (im2)) |
| 5471 |
break; |
| 5472 |
else |
| 5473 |
{ |
| 5474 |
g = SSA_NAME_DEF_STMT (rhs1); |
| 5475 |
rhs1 = gimple_assign_rhs1 (g); |
| 5476 |
gsi2 = gsi_for_stmt (g); |
| 5477 |
gsi_remove (&gsi2, true); |
| 5478 |
release_defs (g); |
| 5479 |
} |
| 5480 |
gcc_checking_assert (rhs1 == gimple_assign_lhs (im2)); |
| 5481 |
gsi2 = gsi_for_stmt (im2); |
| 5482 |
gsi_remove (&gsi2, true); |
| 5483 |
release_defs (im2); |
| 5484 |
/* Replace the re2 statement with __real__ of the newly added |
| 5485 |
.UADDC/.USUBC call. */ |
| 5486 |
if (re2) |
| 5487 |
{ |
| 5488 |
gsi2 = gsi_for_stmt (re2); |
| 5489 |
tree rlhs = gimple_assign_lhs (re2); |
| 5490 |
g = gimple_build_assign (rlhs, REALPART_EXPR, |
| 5491 |
build1 (REALPART_EXPR, TREE_TYPE (rlhs), nlhs)); |
| 5492 |
gsi_replace (&gsi2, g, true); |
| 5493 |
} |
| 5494 |
if (rhs[2]) |
| 5495 |
{ |
| 5496 |
/* If this is the arg1 + arg2 + (ovf1 + ovf2) or |
| 5497 |
arg1 - arg2 - (ovf1 + ovf2) case for the most significant limb, |
| 5498 |
replace stmt with __real__ of another .UADDC/.USUBC call which |
| 5499 |
handles the most significant limb. Overflow flag from this is |
| 5500 |
ignored. */ |
| 5501 |
g = gimple_build_call_internal (code == PLUS_EXPR |
| 5502 |
? IFN_UADDC : IFN_USUBC, |
| 5503 |
3, rhs[3], rhs[2], ilhs); |
| 5504 |
nlhs = make_ssa_name (TREE_TYPE (lhs)); |
| 5505 |
gimple_call_set_lhs (g, nlhs); |
| 5506 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 5507 |
ilhs = gimple_assign_lhs (stmt); |
| 5508 |
g = gimple_build_assign (ilhs, REALPART_EXPR, |
| 5509 |
build1 (REALPART_EXPR, TREE_TYPE (ilhs), nlhs)); |
| 5510 |
gsi_replace (gsi, g, true); |
| 5511 |
} |
| 5512 |
if (TREE_CODE (arg3) == SSA_NAME) |
| 5513 |
{ |
| 5514 |
/* When pattern recognizing the second least significant limb |
| 5515 |
above (i.e. first pair of .{ADD,SUB}_OVERFLOW calls for one limb), |
| 5516 |
check if the [0, 1] range argument (i.e. carry in) isn't the |
| 5517 |
result of another .{ADD,SUB}_OVERFLOW call (one handling the |
| 5518 |
least significant limb). Again look through casts and != 0. */ |
| 5519 |
gimple *im3 = SSA_NAME_DEF_STMT (arg3); |
| 5520 |
for (int i = 0; i < 2; ++i) |
| 5521 |
{ |
| 5522 |
gimple *im4 = uaddc_cast (im3); |
| 5523 |
if (im4 == im3) |
| 5524 |
break; |
| 5525 |
else |
| 5526 |
im3 = im4; |
| 5527 |
} |
| 5528 |
im3 = uaddc_ne0 (im3); |
| 5529 |
if (uaddc_is_cplxpart (im3, IMAGPART_EXPR)) |
| 5530 |
{ |
| 5531 |
gimple *ovf3 |
| 5532 |
= SSA_NAME_DEF_STMT (TREE_OPERAND (gimple_assign_rhs1 (im3), 0)); |
| 5533 |
if (gimple_call_internal_p (ovf3, ifn)) |
| 5534 |
{ |
| 5535 |
lhs = gimple_call_lhs (ovf3); |
| 5536 |
arg1 = gimple_call_arg (ovf3, 0); |
| 5537 |
arg2 = gimple_call_arg (ovf3, 1); |
| 5538 |
if (types_compatible_p (type, TREE_TYPE (TREE_TYPE (lhs))) |
| 5539 |
&& types_compatible_p (type, TREE_TYPE (arg1)) |
| 5540 |
&& types_compatible_p (type, TREE_TYPE (arg2))) |
| 5541 |
{ |
| 5542 |
/* And if it is initialized from result of __imag__ |
| 5543 |
of .{ADD,SUB}_OVERFLOW call, replace that |
| 5544 |
call with .U{ADD,SUB}C call with the same arguments, |
| 5545 |
just 0 added as third argument. This isn't strictly |
| 5546 |
necessary, .ADD_OVERFLOW (x, y) and .UADDC (x, y, 0) |
| 5547 |
produce the same result, but may result in better |
| 5548 |
generated code on some targets where the backend can |
| 5549 |
better prepare in how the result will be used. */ |
| 5550 |
g = gimple_build_call_internal (ifn == IFN_ADD_OVERFLOW |
| 5551 |
? IFN_UADDC : IFN_USUBC, |
| 5552 |
3, arg1, arg2, |
| 5553 |
build_zero_cst (type)); |
| 5554 |
gimple_call_set_lhs (g, lhs); |
| 5555 |
gsi2 = gsi_for_stmt (ovf3); |
| 5556 |
gsi_replace (&gsi2, g, true); |
| 5557 |
} |
| 5558 |
} |
| 5559 |
} |
| 5560 |
} |
| 5561 |
return true; |
| 5562 |
} |
| 5563 |
|
| 5564 |
/* Replace .POPCOUNT (x) == 1 or .POPCOUNT (x) != 1 with |
| 5565 |
(x & (x - 1)) > x - 1 or (x & (x - 1)) <= x - 1 if .POPCOUNT |
| 5566 |
isn't a direct optab. Also handle `<=`/`>` to be |
| 5567 |
`x & (x - 1) !=/== x`. */ |
| 5568 |
|
| 5569 |
static void |
| 5570 |
match_single_bit_test (gimple_stmt_iterator *gsi, gimple *stmt) |
| 5571 |
{ |
| 5572 |
tree clhs, crhs; |
| 5573 |
enum tree_code code; |
| 5574 |
bool was_le = false; |
| 5575 |
if (gimple_code (stmt) == GIMPLE_COND) |
| 5576 |
{ |
| 5577 |
clhs = gimple_cond_lhs (stmt); |
| 5578 |
crhs = gimple_cond_rhs (stmt); |
| 5579 |
code = gimple_cond_code (stmt); |
| 5580 |
} |
| 5581 |
else |
| 5582 |
{ |
| 5583 |
clhs = gimple_assign_rhs1 (stmt); |
| 5584 |
crhs = gimple_assign_rhs2 (stmt); |
| 5585 |
code = gimple_assign_rhs_code (stmt); |
| 5586 |
} |
| 5587 |
if (code != LE_EXPR && code != GT_EXPR |
| 5588 |
&& code != EQ_EXPR && code != NE_EXPR) |
| 5589 |
return; |
| 5590 |
if (code == LE_EXPR || code == GT_EXPR) |
| 5591 |
was_le = true; |
| 5592 |
if (TREE_CODE (clhs) != SSA_NAME || !integer_onep (crhs)) |
| 5593 |
return; |
| 5594 |
gimple *call = SSA_NAME_DEF_STMT (clhs); |
| 5595 |
combined_fn cfn = gimple_call_combined_fn (call); |
| 5596 |
switch (cfn) |
| 5597 |
{ |
| 5598 |
CASE_CFN_POPCOUNT: |
| 5599 |
break; |
| 5600 |
default: |
| 5601 |
return; |
| 5602 |
} |
| 5603 |
if (!has_single_use (clhs)) |
| 5604 |
return; |
| 5605 |
tree arg = gimple_call_arg (call, 0); |
| 5606 |
tree type = TREE_TYPE (arg); |
| 5607 |
if (!INTEGRAL_TYPE_P (type)) |
| 5608 |
return; |
| 5609 |
bool nonzero_arg = tree_expr_nonzero_p (arg); |
| 5610 |
if (direct_internal_fn_supported_p (IFN_POPCOUNT, type, OPTIMIZE_FOR_BOTH)) |
| 5611 |
{ |
| 5612 |
/* Tell expand_POPCOUNT the popcount result is only used in equality |
| 5613 |
comparison with one, so that it can decide based on rtx costs. */ |
| 5614 |
gimple *g = gimple_build_call_internal (IFN_POPCOUNT, 2, arg, |
| 5615 |
was_le ? integer_minus_one_node |
| 5616 |
: (nonzero_arg ? integer_zero_node |
| 5617 |
: integer_one_node)); |
| 5618 |
gimple_call_set_lhs (g, gimple_call_lhs (call)); |
| 5619 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (call); |
| 5620 |
gsi_replace (&gsi2, g, true); |
| 5621 |
return; |
| 5622 |
} |
| 5623 |
tree argm1 = make_ssa_name (type); |
| 5624 |
gimple *g = gimple_build_assign (argm1, PLUS_EXPR, arg, |
| 5625 |
build_int_cst (type, -1)); |
| 5626 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 5627 |
g = gimple_build_assign (make_ssa_name (type), |
| 5628 |
(nonzero_arg || was_le) ? BIT_AND_EXPR : BIT_XOR_EXPR, |
| 5629 |
arg, argm1); |
| 5630 |
gsi_insert_before (gsi, g, GSI_SAME_STMT); |
| 5631 |
tree_code cmpcode; |
| 5632 |
if (was_le) |
| 5633 |
{ |
| 5634 |
argm1 = build_zero_cst (type); |
| 5635 |
cmpcode = code == LE_EXPR ? EQ_EXPR : NE_EXPR; |
| 5636 |
} |
| 5637 |
else if (nonzero_arg) |
| 5638 |
{ |
| 5639 |
argm1 = build_zero_cst (type); |
| 5640 |
cmpcode = code; |
| 5641 |
} |
| 5642 |
else |
| 5643 |
cmpcode = code == EQ_EXPR ? GT_EXPR : LE_EXPR; |
| 5644 |
if (gcond *cond = dyn_cast <gcond *> (stmt)) |
| 5645 |
{ |
| 5646 |
gimple_cond_set_lhs (cond, gimple_assign_lhs (g)); |
| 5647 |
gimple_cond_set_rhs (cond, argm1); |
| 5648 |
gimple_cond_set_code (cond, cmpcode); |
| 5649 |
} |
| 5650 |
else |
| 5651 |
{ |
| 5652 |
gimple_assign_set_rhs1 (stmt, gimple_assign_lhs (g)); |
| 5653 |
gimple_assign_set_rhs2 (stmt, argm1); |
| 5654 |
gimple_assign_set_rhs_code (stmt, cmpcode); |
| 5655 |
} |
| 5656 |
update_stmt (stmt); |
| 5657 |
gimple_stmt_iterator gsi2 = gsi_for_stmt (call); |
| 5658 |
gsi_remove (&gsi2, true); |
| 5659 |
release_defs (call); |
| 5660 |
} |
| 5661 |
|
| 5662 |
/* Return true if target has support for divmod. */ |
| 5663 |
|
| 5664 |
static bool |
| 5665 |
target_supports_divmod_p (optab divmod_optab, optab div_optab, machine_mode mode) |
| 5666 |
{ |
| 5667 |
/* If target supports hardware divmod insn, use it for divmod. */ |
| 5668 |
if (optab_handler (divmod_optab, mode) != CODE_FOR_nothing) |
| 5669 |
return true; |
| 5670 |
|
| 5671 |
/* Check if libfunc for divmod is available. */ |
| 5672 |
rtx libfunc = optab_libfunc (divmod_optab, mode); |
| 5673 |
if (libfunc != NULL_RTX) |
| 5674 |
{ |
| 5675 |
/* If optab_handler exists for div_optab, perhaps in a wider mode, |
| 5676 |
we don't want to use the libfunc even if it exists for given mode. */ |
| 5677 |
machine_mode div_mode; |
| 5678 |
FOR_EACH_MODE_FROM (div_mode, mode) |
| 5679 |
if (optab_handler (div_optab, div_mode) != CODE_FOR_nothing) |
| 5680 |
return false; |
| 5681 |
|
| 5682 |
return targetm.expand_divmod_libfunc != NULL; |
| 5683 |
} |
| 5684 |
|
| 5685 |
return false; |
| 5686 |
} |
| 5687 |
|
| 5688 |
/* Check if stmt is candidate for divmod transform. */ |
| 5689 |
|
| 5690 |
static bool |
| 5691 |
divmod_candidate_p (gassign *stmt) |
| 5692 |
{ |
| 5693 |
tree type = TREE_TYPE (gimple_assign_lhs (stmt)); |
| 5694 |
machine_mode mode = TYPE_MODE (type); |
| 5695 |
optab divmod_optab, div_optab; |
| 5696 |
|
| 5697 |
if (TYPE_UNSIGNED (type)) |
| 5698 |
{ |
| 5699 |
divmod_optab = udivmod_optab; |
| 5700 |
div_optab = udiv_optab; |
| 5701 |
} |
| 5702 |
else |
| 5703 |
{ |
| 5704 |
divmod_optab = sdivmod_optab; |
| 5705 |
div_optab = sdiv_optab; |
| 5706 |
} |
| 5707 |
|
| 5708 |
tree op1 = gimple_assign_rhs1 (stmt); |
| 5709 |
tree op2 = gimple_assign_rhs2 (stmt); |
| 5710 |
|
| 5711 |
/* Disable the transform if either is a constant, since division-by-constant |
| 5712 |
may have specialized expansion. */ |
| 5713 |
if (CONSTANT_CLASS_P (op1)) |
| 5714 |
return false; |
| 5715 |
|
| 5716 |
if (CONSTANT_CLASS_P (op2)) |
| 5717 |
{ |
| 5718 |
if (integer_pow2p (op2)) |
| 5719 |
return false; |
| 5720 |
|
| 5721 |
if (element_precision (type) <= HOST_BITS_PER_WIDE_INT |
| 5722 |
&& element_precision (type) <= BITS_PER_WORD) |
| 5723 |
return false; |
| 5724 |
|
| 5725 |
/* If the divisor is not power of 2 and the precision wider than |
| 5726 |
HWI, expand_divmod punts on that, so in that case it is better |
| 5727 |
to use divmod optab or libfunc. Similarly if choose_multiplier |
| 5728 |
might need pre/post shifts of BITS_PER_WORD or more. */ |
| 5729 |
} |
| 5730 |
|
| 5731 |
/* Exclude the case where TYPE_OVERFLOW_TRAPS (type) as that should |
| 5732 |
expand using the [su]divv optabs. */ |
| 5733 |
if (TYPE_OVERFLOW_TRAPS (type)) |
| 5734 |
return false; |
| 5735 |
|
| 5736 |
if (!target_supports_divmod_p (divmod_optab, div_optab, mode)) |
| 5737 |
return false; |
| 5738 |
|
| 5739 |
return true; |
| 5740 |
} |
| 5741 |
|
| 5742 |
/* This function looks for: |
| 5743 |
t1 = a TRUNC_DIV_EXPR b; |
| 5744 |
t2 = a TRUNC_MOD_EXPR b; |
| 5745 |
and transforms it to the following sequence: |
| 5746 |
complex_tmp = DIVMOD (a, b); |
| 5747 |
t1 = REALPART_EXPR(a); |
| 5748 |
t2 = IMAGPART_EXPR(b); |
| 5749 |
For conditions enabling the transform see divmod_candidate_p(). |
| 5750 |
|
| 5751 |
The pass has three parts: |
| 5752 |
1) Find top_stmt which is trunc_div or trunc_mod stmt and dominates all |
| 5753 |
other trunc_div_expr and trunc_mod_expr stmts. |
| 5754 |
2) Add top_stmt and all trunc_div and trunc_mod stmts dominated by top_stmt |
| 5755 |
to stmts vector. |
| 5756 |
3) Insert DIVMOD call just before top_stmt and update entries in |
| 5757 |
stmts vector to use return value of DIMOVD (REALEXPR_PART for div, |
| 5758 |
IMAGPART_EXPR for mod). */ |
| 5759 |
|
| 5760 |
static bool |
| 5761 |
convert_to_divmod (gassign *stmt) |
| 5762 |
{ |
| 5763 |
if (stmt_can_throw_internal (cfun, stmt) |
| 5764 |
|| !divmod_candidate_p (stmt)) |
| 5765 |
return false; |
| 5766 |
|
| 5767 |
tree op1 = gimple_assign_rhs1 (stmt); |
| 5768 |
tree op2 = gimple_assign_rhs2 (stmt); |
| 5769 |
|
| 5770 |
imm_use_iterator use_iter; |
| 5771 |
gimple *use_stmt; |
| 5772 |
auto_vec<gimple *> stmts; |
| 5773 |
|
| 5774 |
gimple *top_stmt = stmt; |
| 5775 |
basic_block top_bb = gimple_bb (stmt); |
| 5776 |
|
| 5777 |
/* Part 1: Try to set top_stmt to "topmost" stmt that dominates |
| 5778 |
at-least stmt and possibly other trunc_div/trunc_mod stmts |
| 5779 |
having same operands as stmt. */ |
| 5780 |
|
| 5781 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, op1) |
| 5782 |
{ |
| 5783 |
if (is_gimple_assign (use_stmt) |
| 5784 |
&& (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR |
| 5785 |
|| gimple_assign_rhs_code (use_stmt) == TRUNC_MOD_EXPR) |
| 5786 |
&& operand_equal_p (op1, gimple_assign_rhs1 (use_stmt), 0) |
| 5787 |
&& operand_equal_p (op2, gimple_assign_rhs2 (use_stmt), 0)) |
| 5788 |
{ |
| 5789 |
if (stmt_can_throw_internal (cfun, use_stmt)) |
| 5790 |
continue; |
| 5791 |
|
| 5792 |
basic_block bb = gimple_bb (use_stmt); |
| 5793 |
|
| 5794 |
if (bb == top_bb) |
| 5795 |
{ |
| 5796 |
if (gimple_uid (use_stmt) < gimple_uid (top_stmt)) |
| 5797 |
top_stmt = use_stmt; |
| 5798 |
} |
| 5799 |
else if (dominated_by_p (CDI_DOMINATORS, top_bb, bb)) |
| 5800 |
{ |
| 5801 |
top_bb = bb; |
| 5802 |
top_stmt = use_stmt; |
| 5803 |
} |
| 5804 |
} |
| 5805 |
} |
| 5806 |
|
| 5807 |
tree top_op1 = gimple_assign_rhs1 (top_stmt); |
| 5808 |
tree top_op2 = gimple_assign_rhs2 (top_stmt); |
| 5809 |
|
| 5810 |
stmts.safe_push (top_stmt); |
| 5811 |
bool div_seen = (gimple_assign_rhs_code (top_stmt) == TRUNC_DIV_EXPR); |
| 5812 |
|
| 5813 |
/* Part 2: Add all trunc_div/trunc_mod statements domianted by top_bb |
| 5814 |
to stmts vector. The 2nd loop will always add stmt to stmts vector, since |
| 5815 |
gimple_bb (top_stmt) dominates gimple_bb (stmt), so the |
| 5816 |
2nd loop ends up adding at-least single trunc_mod_expr stmt. */ |
| 5817 |
|
| 5818 |
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, top_op1) |
| 5819 |
{ |
| 5820 |
if (is_gimple_assign (use_stmt) |
| 5821 |
&& (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR |
| 5822 |
|| gimple_assign_rhs_code (use_stmt) == TRUNC_MOD_EXPR) |
| 5823 |
&& operand_equal_p (top_op1, gimple_assign_rhs1 (use_stmt), 0) |
| 5824 |
&& operand_equal_p (top_op2, gimple_assign_rhs2 (use_stmt), 0)) |
| 5825 |
{ |
| 5826 |
if (use_stmt == top_stmt |
| 5827 |
|| stmt_can_throw_internal (cfun, use_stmt) |
| 5828 |
|| !dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt), top_bb)) |
| 5829 |
continue; |
| 5830 |
|
| 5831 |
stmts.safe_push (use_stmt); |
| 5832 |
if (gimple_assign_rhs_code (use_stmt) == TRUNC_DIV_EXPR) |
| 5833 |
div_seen = true; |
| 5834 |
} |
| 5835 |
} |
| 5836 |
|
| 5837 |
if (!div_seen) |
| 5838 |
return false; |
| 5839 |
|
| 5840 |
/* Part 3: Create libcall to internal fn DIVMOD: |
| 5841 |
divmod_tmp = DIVMOD (op1, op2). */ |
| 5842 |
|
| 5843 |
gcall *call_stmt = gimple_build_call_internal (IFN_DIVMOD, 2, op1, op2); |
| 5844 |
tree res = make_temp_ssa_name (build_complex_type (TREE_TYPE (op1)), |
| 5845 |
call_stmt, "divmod_tmp"); |
| 5846 |
gimple_call_set_lhs (call_stmt, res); |
| 5847 |
/* We rejected throwing statements above. */ |
| 5848 |
gimple_call_set_nothrow (call_stmt, true); |
| 5849 |
|
| 5850 |
/* Insert the call before top_stmt. */ |
| 5851 |
gimple_stmt_iterator top_stmt_gsi = gsi_for_stmt (top_stmt); |
| 5852 |
gsi_insert_before (&top_stmt_gsi, call_stmt, GSI_SAME_STMT); |
| 5853 |
|
| 5854 |
widen_mul_stats.divmod_calls_inserted++; |
| 5855 |
|
| 5856 |
/* Update all statements in stmts vector: |
| 5857 |
lhs = op1 TRUNC_DIV_EXPR op2 -> lhs = REALPART_EXPR<divmod_tmp> |
| 5858 |
lhs = op1 TRUNC_MOD_EXPR op2 -> lhs = IMAGPART_EXPR<divmod_tmp>. */ |
| 5859 |
|
| 5860 |
for (unsigned i = 0; stmts.iterate (i, &use_stmt); ++i) |
| 5861 |
{ |
| 5862 |
tree new_rhs; |
| 5863 |
|
| 5864 |
switch (gimple_assign_rhs_code (use_stmt)) |
| 5865 |
{ |
| 5866 |
case TRUNC_DIV_EXPR: |
| 5867 |
new_rhs = fold_build1 (REALPART_EXPR, TREE_TYPE (op1), res); |
| 5868 |
break; |
| 5869 |
|
| 5870 |
case TRUNC_MOD_EXPR: |
| 5871 |
new_rhs = fold_build1 (IMAGPART_EXPR, TREE_TYPE (op1), res); |
| 5872 |
break; |
| 5873 |
|
| 5874 |
default: |
| 5875 |
gcc_unreachable (); |
| 5876 |
} |
| 5877 |
|
| 5878 |
gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt); |
| 5879 |
gimple_assign_set_rhs_from_tree (&gsi, new_rhs); |
| 5880 |
update_stmt (use_stmt); |
| 5881 |
} |
| 5882 |
|
| 5883 |
return true; |
| 5884 |
} |
| 5885 |
|
| 5886 |
/* Process a single gimple assignment STMT, which has a RSHIFT_EXPR as |
| 5887 |
its rhs, and try to convert it into a MULT_HIGHPART_EXPR. The return |
| 5888 |
value is true iff we converted the statement. */ |
| 5889 |
|
| 5890 |
static bool |
| 5891 |
convert_mult_to_highpart (gassign *stmt, gimple_stmt_iterator *gsi) |
| 5892 |
{ |
| 5893 |
tree lhs = gimple_assign_lhs (stmt); |
| 5894 |
tree stype = TREE_TYPE (lhs); |
| 5895 |
tree sarg0 = gimple_assign_rhs1 (stmt); |
| 5896 |
tree sarg1 = gimple_assign_rhs2 (stmt); |
| 5897 |
|
| 5898 |
if (TREE_CODE (stype) != INTEGER_TYPE |
| 5899 |
|| TREE_CODE (sarg1) != INTEGER_CST |
| 5900 |
|| TREE_CODE (sarg0) != SSA_NAME |
| 5901 |
|| !tree_fits_uhwi_p (sarg1) |
| 5902 |
|| !has_single_use (sarg0)) |
| 5903 |
return false; |
| 5904 |
|
| 5905 |
gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (sarg0)); |
| 5906 |
if (!def) |
| 5907 |
return false; |
| 5908 |
|
| 5909 |
enum tree_code mcode = gimple_assign_rhs_code (def); |
| 5910 |
if (mcode == NOP_EXPR) |
| 5911 |
{ |
| 5912 |
tree tmp = gimple_assign_rhs1 (def); |
| 5913 |
if (TREE_CODE (tmp) != SSA_NAME || !has_single_use (tmp)) |
| 5914 |
return false; |
| 5915 |
def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (tmp)); |
| 5916 |
if (!def) |
| 5917 |
return false; |
| 5918 |
mcode = gimple_assign_rhs_code (def); |
| 5919 |
} |
| 5920 |
|
| 5921 |
if (mcode != WIDEN_MULT_EXPR |
| 5922 |
|| gimple_bb (def) != gimple_bb (stmt)) |
| 5923 |
return false; |
| 5924 |
tree mtype = TREE_TYPE (gimple_assign_lhs (def)); |
| 5925 |
if (TREE_CODE (mtype) != INTEGER_TYPE |
| 5926 |
|| TYPE_PRECISION (mtype) != TYPE_PRECISION (stype)) |
| 5927 |
return false; |
| 5928 |
|
| 5929 |
tree mop1 = gimple_assign_rhs1 (def); |
| 5930 |
tree mop2 = gimple_assign_rhs2 (def); |
| 5931 |
tree optype = TREE_TYPE (mop1); |
| 5932 |
bool unsignedp = TYPE_UNSIGNED (optype); |
| 5933 |
unsigned int prec = TYPE_PRECISION (optype); |
| 5934 |
|
| 5935 |
if (unsignedp != TYPE_UNSIGNED (mtype) |
| 5936 |
|| TYPE_PRECISION (mtype) != 2 * prec) |
| 5937 |
return false; |
| 5938 |
|
| 5939 |
unsigned HOST_WIDE_INT bits = tree_to_uhwi (sarg1); |
| 5940 |
if (bits < prec || bits >= 2 * prec) |
| 5941 |
return false; |
| 5942 |
|
| 5943 |
/* For the time being, require operands to have the same sign. */ |
| 5944 |
if (unsignedp != TYPE_UNSIGNED (TREE_TYPE (mop2))) |
| 5945 |
return false; |
| 5946 |
|
| 5947 |
machine_mode mode = TYPE_MODE (optype); |
| 5948 |
optab tab = unsignedp ? umul_highpart_optab : smul_highpart_optab; |
| 5949 |
if (optab_handler (tab, mode) == CODE_FOR_nothing) |
| 5950 |
return false; |
| 5951 |
|
| 5952 |
location_t loc = gimple_location (stmt); |
| 5953 |
tree highpart1 = build_and_insert_binop (gsi, loc, "highparttmp", |
| 5954 |
MULT_HIGHPART_EXPR, mop1, mop2); |
| 5955 |
tree highpart2 = highpart1; |
| 5956 |
tree ntype = optype; |
| 5957 |
|
| 5958 |
if (TYPE_UNSIGNED (stype) != TYPE_UNSIGNED (optype)) |
| 5959 |
{ |
| 5960 |
ntype = TYPE_UNSIGNED (stype) ? unsigned_type_for (optype) |
| 5961 |
: signed_type_for (optype); |
| 5962 |
highpart2 = build_and_insert_cast (gsi, loc, ntype, highpart1); |
| 5963 |
} |
| 5964 |
if (bits > prec) |
| 5965 |
highpart2 = build_and_insert_binop (gsi, loc, "highparttmp", |
| 5966 |
RSHIFT_EXPR, highpart2, |
| 5967 |
build_int_cst (ntype, bits - prec)); |
| 5968 |
|
| 5969 |
gassign *new_stmt = gimple_build_assign (lhs, NOP_EXPR, highpart2); |
| 5970 |
gsi_replace (gsi, new_stmt, true); |
| 5971 |
|
| 5972 |
widen_mul_stats.highpart_mults_inserted++; |
| 5973 |
return true; |
| 5974 |
} |
| 5975 |
|
| 5976 |
/* If target has spaceship<MODE>3 expander, pattern recognize |
| 5977 |
<bb 2> [local count: 1073741824]: |
| 5978 |
if (a_2(D) == b_3(D)) |
| 5979 |
goto <bb 6>; [34.00%] |
| 5980 |
else |
| 5981 |
goto <bb 3>; [66.00%] |
| 5982 |
|
| 5983 |
<bb 3> [local count: 708669601]: |
| 5984 |
if (a_2(D) < b_3(D)) |
| 5985 |
goto <bb 6>; [1.04%] |
| 5986 |
else |
| 5987 |
goto <bb 4>; [98.96%] |
| 5988 |
|
| 5989 |
<bb 4> [local count: 701299439]: |
| 5990 |
if (a_2(D) > b_3(D)) |
| 5991 |
goto <bb 5>; [48.89%] |
| 5992 |
else |
| 5993 |
goto <bb 6>; [51.11%] |
| 5994 |
|
| 5995 |
<bb 5> [local count: 342865295]: |
| 5996 |
|
| 5997 |
<bb 6> [local count: 1073741824]: |
| 5998 |
and turn it into: |
| 5999 |
<bb 2> [local count: 1073741824]: |
| 6000 |
_1 = .SPACESHIP (a_2(D), b_3(D), 0); |
| 6001 |
if (_1 == 0) |
| 6002 |
goto <bb 6>; [34.00%] |
| 6003 |
else |
| 6004 |
goto <bb 3>; [66.00%] |
| 6005 |
|
| 6006 |
<bb 3> [local count: 708669601]: |
| 6007 |
if (_1 == -1) |
| 6008 |
goto <bb 6>; [1.04%] |
| 6009 |
else |
| 6010 |
goto <bb 4>; [98.96%] |
| 6011 |
|
| 6012 |
<bb 4> [local count: 701299439]: |
| 6013 |
if (_1 == 1) |
| 6014 |
goto <bb 5>; [48.89%] |
| 6015 |
else |
| 6016 |
goto <bb 6>; [51.11%] |
| 6017 |
|
| 6018 |
<bb 5> [local count: 342865295]: |
| 6019 |
|
| 6020 |
<bb 6> [local count: 1073741824]: |
| 6021 |
so that the backend can emit optimal comparison and |
| 6022 |
conditional jump sequence. If the |
| 6023 |
<bb 6> [local count: 1073741824]: |
| 6024 |
above has a single PHI like: |
| 6025 |
# _27 = PHI<0(2), -1(3), 2(4), 1(5)> |
| 6026 |
then replace it with effectively |
| 6027 |
_1 = .SPACESHIP (a_2(D), b_3(D), 1); |
| 6028 |
_27 = _1; */ |
| 6029 |
|
| 6030 |
static void |
| 6031 |
optimize_spaceship (gcond *stmt) |
| 6032 |
{ |
| 6033 |
enum tree_code code = gimple_cond_code (stmt); |
| 6034 |
if (code != EQ_EXPR && code != NE_EXPR) |
| 6035 |
return; |
| 6036 |
tree arg1 = gimple_cond_lhs (stmt); |
| 6037 |
tree arg2 = gimple_cond_rhs (stmt); |
| 6038 |
if ((!SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1)) |
| 6039 |
&& !INTEGRAL_TYPE_P (TREE_TYPE (arg1))) |
| 6040 |
|| optab_handler (spaceship_optab, |
| 6041 |
TYPE_MODE (TREE_TYPE (arg1))) == CODE_FOR_nothing |
| 6042 |
|| operand_equal_p (arg1, arg2, 0)) |
| 6043 |
return; |
| 6044 |
|
| 6045 |
basic_block bb0 = gimple_bb (stmt), bb1, bb2 = NULL; |
| 6046 |
edge em1 = NULL, e1 = NULL, e2 = NULL; |
| 6047 |
bb1 = EDGE_SUCC (bb0, 1)->dest; |
| 6048 |
if (((EDGE_SUCC (bb0, 0)->flags & EDGE_TRUE_VALUE) != 0) ^ (code == EQ_EXPR)) |
| 6049 |
bb1 = EDGE_SUCC (bb0, 0)->dest; |
| 6050 |
|
| 6051 |
gcond *g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb1)); |
| 6052 |
if (g == NULL |
| 6053 |
|| !single_pred_p (bb1) |
| 6054 |
|| (operand_equal_p (gimple_cond_lhs (g), arg1, 0) |
| 6055 |
? !operand_equal_p (gimple_cond_rhs (g), arg2, 0) |
| 6056 |
: (!operand_equal_p (gimple_cond_lhs (g), arg2, 0) |
| 6057 |
|| !operand_equal_p (gimple_cond_rhs (g), arg1, 0))) |
| 6058 |
|| !cond_only_block_p (bb1)) |
| 6059 |
return; |
| 6060 |
|
| 6061 |
enum tree_code ccode = (operand_equal_p (gimple_cond_lhs (g), arg1, 0) |
| 6062 |
? LT_EXPR : GT_EXPR); |
| 6063 |
switch (gimple_cond_code (g)) |
| 6064 |
{ |
| 6065 |
case LT_EXPR: |
| 6066 |
case LE_EXPR: |
| 6067 |
break; |
| 6068 |
case GT_EXPR: |
| 6069 |
case GE_EXPR: |
| 6070 |
ccode = ccode == LT_EXPR ? GT_EXPR : LT_EXPR; |
| 6071 |
break; |
| 6072 |
default: |
| 6073 |
return; |
| 6074 |
} |
| 6075 |
|
| 6076 |
for (int i = 0; i < 2; ++i) |
| 6077 |
{ |
| 6078 |
/* With NaNs, </<=/>/>= are false, so we need to look for the |
| 6079 |
third comparison on the false edge from whatever non-equality |
| 6080 |
comparison the second comparison is. */ |
| 6081 |
if (HONOR_NANS (TREE_TYPE (arg1)) |
| 6082 |
&& (EDGE_SUCC (bb1, i)->flags & EDGE_TRUE_VALUE) != 0) |
| 6083 |
continue; |
| 6084 |
|
| 6085 |
bb2 = EDGE_SUCC (bb1, i)->dest; |
| 6086 |
g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb2)); |
| 6087 |
if (g == NULL |
| 6088 |
|| !single_pred_p (bb2) |
| 6089 |
|| (operand_equal_p (gimple_cond_lhs (g), arg1, 0) |
| 6090 |
? !operand_equal_p (gimple_cond_rhs (g), arg2, 0) |
| 6091 |
: (!operand_equal_p (gimple_cond_lhs (g), arg2, 0) |
| 6092 |
|| !operand_equal_p (gimple_cond_rhs (g), arg1, 0))) |
| 6093 |
|| !cond_only_block_p (bb2) |
| 6094 |
|| EDGE_SUCC (bb2, 0)->dest == EDGE_SUCC (bb2, 1)->dest) |
| 6095 |
continue; |
| 6096 |
|
| 6097 |
enum tree_code ccode2 |
| 6098 |
= (operand_equal_p (gimple_cond_lhs (g), arg1, 0) ? LT_EXPR : GT_EXPR); |
| 6099 |
switch (gimple_cond_code (g)) |
| 6100 |
{ |
| 6101 |
case LT_EXPR: |
| 6102 |
case LE_EXPR: |
| 6103 |
break; |
| 6104 |
case GT_EXPR: |
| 6105 |
case GE_EXPR: |
| 6106 |
ccode2 = ccode2 == LT_EXPR ? GT_EXPR : LT_EXPR; |
| 6107 |
break; |
| 6108 |
default: |
| 6109 |
continue; |
| 6110 |
} |
| 6111 |
if (HONOR_NANS (TREE_TYPE (arg1)) && ccode == ccode2) |
| 6112 |
continue; |
| 6113 |
|
| 6114 |
if ((ccode == LT_EXPR) |
| 6115 |
^ ((EDGE_SUCC (bb1, i)->flags & EDGE_TRUE_VALUE) != 0)) |
| 6116 |
{ |
| 6117 |
em1 = EDGE_SUCC (bb1, 1 - i); |
| 6118 |
e1 = EDGE_SUCC (bb2, 0); |
| 6119 |
e2 = EDGE_SUCC (bb2, 1); |
| 6120 |
if ((ccode2 == LT_EXPR) ^ ((e1->flags & EDGE_TRUE_VALUE) == 0)) |
| 6121 |
std::swap (e1, e2); |
| 6122 |
} |
| 6123 |
else |
| 6124 |
{ |
| 6125 |
e1 = EDGE_SUCC (bb1, 1 - i); |
| 6126 |
em1 = EDGE_SUCC (bb2, 0); |
| 6127 |
e2 = EDGE_SUCC (bb2, 1); |
| 6128 |
if ((ccode2 != LT_EXPR) ^ ((em1->flags & EDGE_TRUE_VALUE) == 0)) |
| 6129 |
std::swap (em1, e2); |
| 6130 |
} |
| 6131 |
break; |
| 6132 |
} |
| 6133 |
|
| 6134 |
if (em1 == NULL) |
| 6135 |
{ |
| 6136 |
if ((ccode == LT_EXPR) |
| 6137 |
^ ((EDGE_SUCC (bb1, 0)->flags & EDGE_TRUE_VALUE) != 0)) |
| 6138 |
{ |
| 6139 |
em1 = EDGE_SUCC (bb1, 1); |
| 6140 |
e1 = EDGE_SUCC (bb1, 0); |
| 6141 |
e2 = (e1->flags & EDGE_TRUE_VALUE) ? em1 : e1; |
| 6142 |
} |
| 6143 |
else |
| 6144 |
{ |
| 6145 |
em1 = EDGE_SUCC (bb1, 0); |
| 6146 |
e1 = EDGE_SUCC (bb1, 1); |
| 6147 |
e2 = (e1->flags & EDGE_TRUE_VALUE) ? em1 : e1; |
| 6148 |
} |
| 6149 |
} |
| 6150 |
|
| 6151 |
/* Check if there is a single bb into which all failed conditions |
| 6152 |
jump to (perhaps through an empty block) and if it results in |
| 6153 |
a single integral PHI which just sets it to -1, 0, 1, X |
| 6154 |
(or -1, 0, 1 when NaNs can't happen). In that case use 1 rather |
| 6155 |
than 0 as last .SPACESHIP argument to tell backends it might |
| 6156 |
consider different code generation and just cast the result |
| 6157 |
of .SPACESHIP to the PHI result. X above is some value |
| 6158 |
other than -1, 0, 1, for libstdc++ 2, for libc++ -127. */ |
| 6159 |
tree arg3 = integer_zero_node; |
| 6160 |
edge e = EDGE_SUCC (bb0, 0); |
| 6161 |
if (e->dest == bb1) |
| 6162 |
e = EDGE_SUCC (bb0, 1); |
| 6163 |
basic_block bbp = e->dest; |
| 6164 |
gphi *phi = NULL; |
| 6165 |
for (gphi_iterator psi = gsi_start_phis (bbp); |
| 6166 |
!gsi_end_p (psi); gsi_next (&psi)) |
| 6167 |
{ |
| 6168 |
gphi *gp = psi.phi (); |
| 6169 |
tree res = gimple_phi_result (gp); |
| 6170 |
|
| 6171 |
if (phi != NULL |
| 6172 |
|| virtual_operand_p (res) |
| 6173 |
|| !INTEGRAL_TYPE_P (TREE_TYPE (res)) |
| 6174 |
|| TYPE_PRECISION (TREE_TYPE (res)) < 2) |
| 6175 |
{ |
| 6176 |
phi = NULL; |
| 6177 |
break; |
| 6178 |
} |
| 6179 |
phi = gp; |
| 6180 |
} |
| 6181 |
if (phi |
| 6182 |
&& integer_zerop (gimple_phi_arg_def_from_edge (phi, e)) |
| 6183 |
&& EDGE_COUNT (bbp->preds) == (HONOR_NANS (TREE_TYPE (arg1)) ? 4 : 3)) |
| 6184 |
{ |
| 6185 |
HOST_WIDE_INT argval = SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1)) ? 2 : -1; |
| 6186 |
for (unsigned i = 0; phi && i < EDGE_COUNT (bbp->preds) - 1; ++i) |
| 6187 |
{ |
| 6188 |
edge e3 = i == 0 ? e1 : i == 1 ? em1 : e2; |
| 6189 |
if (e3->dest != bbp) |
| 6190 |
{ |
| 6191 |
if (!empty_block_p (e3->dest) |
| 6192 |
|| !single_succ_p (e3->dest) |
| 6193 |
|| single_succ (e3->dest) != bbp) |
| 6194 |
{ |
| 6195 |
phi = NULL; |
| 6196 |
break; |
| 6197 |
} |
| 6198 |
e3 = single_succ_edge (e3->dest); |
| 6199 |
} |
| 6200 |
tree a = gimple_phi_arg_def_from_edge (phi, e3); |
| 6201 |
if (TREE_CODE (a) != INTEGER_CST |
| 6202 |
|| (i == 0 && !integer_onep (a)) |
| 6203 |
|| (i == 1 && !integer_all_onesp (a))) |
| 6204 |
{ |
| 6205 |
phi = NULL; |
| 6206 |
break; |
| 6207 |
} |
| 6208 |
if (i == 2) |
| 6209 |
{ |
| 6210 |
tree minv = TYPE_MIN_VALUE (signed_char_type_node); |
| 6211 |
tree maxv = TYPE_MAX_VALUE (signed_char_type_node); |
| 6212 |
widest_int w = widest_int::from (wi::to_wide (a), SIGNED); |
| 6213 |
if ((w >= -1 && w <= 1) |
| 6214 |
|| w < wi::to_widest (minv) |
| 6215 |
|| w > wi::to_widest (maxv)) |
| 6216 |
{ |
| 6217 |
phi = NULL; |
| 6218 |
break; |
| 6219 |
} |
| 6220 |
argval = w.to_shwi (); |
| 6221 |
} |
| 6222 |
} |
| 6223 |
if (phi) |
| 6224 |
arg3 = build_int_cst (integer_type_node, |
| 6225 |
TYPE_UNSIGNED (TREE_TYPE (arg1)) ? 1 : argval); |
| 6226 |
} |
| 6227 |
|
| 6228 |
/* For integral <=> comparisons only use .SPACESHIP if it is turned |
| 6229 |
into an integer (-1, 0, 1). */ |
| 6230 |
if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg1)) && arg3 == integer_zero_node) |
| 6231 |
return; |
| 6232 |
|
| 6233 |
gcall *gc = gimple_build_call_internal (IFN_SPACESHIP, 3, arg1, arg2, arg3); |
| 6234 |
tree lhs = make_ssa_name (integer_type_node); |
| 6235 |
gimple_call_set_lhs (gc, lhs); |
| 6236 |
gimple_stmt_iterator gsi = gsi_for_stmt (stmt); |
| 6237 |
gsi_insert_before (&gsi, gc, GSI_SAME_STMT); |
| 6238 |
|
| 6239 |
wide_int wmin = wi::minus_one (TYPE_PRECISION (integer_type_node)); |
| 6240 |
wide_int wmax = wi::one (TYPE_PRECISION (integer_type_node)); |
| 6241 |
if (HONOR_NANS (TREE_TYPE (arg1))) |
| 6242 |
{ |
| 6243 |
if (arg3 == integer_zero_node) |
| 6244 |
wmax = wi::two (TYPE_PRECISION (integer_type_node)); |
| 6245 |
else if (tree_int_cst_sgn (arg3) < 0) |
| 6246 |
wmin = wi::to_wide (arg3); |
| 6247 |
else |
| 6248 |
wmax = wi::to_wide (arg3); |
| 6249 |
} |
| 6250 |
int_range<1> vr (TREE_TYPE (lhs), wmin, wmax); |
| 6251 |
set_range_info (lhs, vr); |
| 6252 |
|
| 6253 |
if (arg3 != integer_zero_node) |
| 6254 |
{ |
| 6255 |
tree type = TREE_TYPE (gimple_phi_result (phi)); |
| 6256 |
if (!useless_type_conversion_p (type, integer_type_node)) |
| 6257 |
{ |
| 6258 |
tree tem = make_ssa_name (type); |
| 6259 |
gimple *gcv = gimple_build_assign (tem, NOP_EXPR, lhs); |
| 6260 |
gsi_insert_before (&gsi, gcv, GSI_SAME_STMT); |
| 6261 |
lhs = tem; |
| 6262 |
} |
| 6263 |
SET_PHI_ARG_DEF_ON_EDGE (phi, e, lhs); |
| 6264 |
gimple_cond_set_lhs (stmt, boolean_false_node); |
| 6265 |
gimple_cond_set_rhs (stmt, boolean_false_node); |
| 6266 |
gimple_cond_set_code (stmt, (e->flags & EDGE_TRUE_VALUE) |
| 6267 |
? EQ_EXPR : NE_EXPR); |
| 6268 |
update_stmt (stmt); |
| 6269 |
return; |
| 6270 |
} |
| 6271 |
|
| 6272 |
gimple_cond_set_lhs (stmt, lhs); |
| 6273 |
gimple_cond_set_rhs (stmt, integer_zero_node); |
| 6274 |
update_stmt (stmt); |
| 6275 |
|
| 6276 |
gcond *cond = as_a <gcond *> (*gsi_last_bb (bb1)); |
| 6277 |
gimple_cond_set_lhs (cond, lhs); |
| 6278 |
if (em1->src == bb1 && e2 != em1) |
| 6279 |
{ |
| 6280 |
gimple_cond_set_rhs (cond, integer_minus_one_node); |
| 6281 |
gimple_cond_set_code (cond, (em1->flags & EDGE_TRUE_VALUE) |
| 6282 |
? EQ_EXPR : NE_EXPR); |
| 6283 |
} |
| 6284 |
else |
| 6285 |
{ |
| 6286 |
gcc_assert (e1->src == bb1 && e2 != e1); |
| 6287 |
gimple_cond_set_rhs (cond, integer_one_node); |
| 6288 |
gimple_cond_set_code (cond, (e1->flags & EDGE_TRUE_VALUE) |
| 6289 |
? EQ_EXPR : NE_EXPR); |
| 6290 |
} |
| 6291 |
update_stmt (cond); |
| 6292 |
|
| 6293 |
if (e2 != e1 && e2 != em1) |
| 6294 |
{ |
| 6295 |
cond = as_a <gcond *> (*gsi_last_bb (bb2)); |
| 6296 |
gimple_cond_set_lhs (cond, lhs); |
| 6297 |
if (em1->src == bb2) |
| 6298 |
gimple_cond_set_rhs (cond, integer_minus_one_node); |
| 6299 |
else |
| 6300 |
{ |
| 6301 |
gcc_assert (e1->src == bb2); |
| 6302 |
gimple_cond_set_rhs (cond, integer_one_node); |
| 6303 |
} |
| 6304 |
gimple_cond_set_code (cond, |
| 6305 |
(e2->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR); |
| 6306 |
update_stmt (cond); |
| 6307 |
} |
| 6308 |
} |
| 6309 |
|
| 6310 |
|
| 6311 |
/* Find integer multiplications where the operands are extended from |
| 6312 |
smaller types, and replace the MULT_EXPR with a WIDEN_MULT_EXPR |
| 6313 |
or MULT_HIGHPART_EXPR where appropriate. */ |
| 6314 |
|
| 6315 |
namespace { |
| 6316 |
|
| 6317 |
const pass_data pass_data_optimize_widening_mul = |
| 6318 |
{ |
| 6319 |
GIMPLE_PASS, /* type */ |
| 6320 |
"widening_mul", /* name */ |
| 6321 |
OPTGROUP_NONE, /* optinfo_flags */ |
| 6322 |
TV_TREE_WIDEN_MUL, /* tv_id */ |
| 6323 |
PROP_ssa, /* properties_required */ |
| 6324 |
0, /* properties_provided */ |
| 6325 |
0, /* properties_destroyed */ |
| 6326 |
0, /* todo_flags_start */ |
| 6327 |
TODO_update_ssa, /* todo_flags_finish */ |
| 6328 |
}; |
| 6329 |
|
| 6330 |
class pass_optimize_widening_mul : public gimple_opt_pass |
| 6331 |
{ |
| 6332 |
public: |
| 6333 |
pass_optimize_widening_mul (gcc::context *ctxt) |
| 6334 |
: gimple_opt_pass (pass_data_optimize_widening_mul, ctxt) |
| 6335 |
{} |
| 6336 |
|
| 6337 |
/* opt_pass methods: */ |
| 6338 |
bool gate (function *) final override |
| 6339 |
{ |
| 6340 |
return flag_expensive_optimizations && optimize; |
| 6341 |
} |
| 6342 |
|
| 6343 |
unsigned int execute (function *) final override; |
| 6344 |
|
| 6345 |
}; // class pass_optimize_widening_mul |
| 6346 |
|
| 6347 |
/* Walker class to perform the transformation in reverse dominance order. */ |
| 6348 |
|
| 6349 |
class math_opts_dom_walker : public dom_walker |
| 6350 |
{ |
| 6351 |
public: |
| 6352 |
/* Constructor, CFG_CHANGED is a pointer to a boolean flag that will be set |
| 6353 |
if walking modidifes the CFG. */ |
| 6354 |
|
| 6355 |
math_opts_dom_walker (bool *cfg_changed_p) |
| 6356 |
: dom_walker (CDI_DOMINATORS), m_last_result_set (), |
| 6357 |
m_cfg_changed_p (cfg_changed_p) {} |
| 6358 |
|
| 6359 |
/* The actual actions performed in the walk. */ |
| 6360 |
|
| 6361 |
void after_dom_children (basic_block) final override; |
| 6362 |
|
| 6363 |
/* Set of results of chains of multiply and add statement combinations that |
| 6364 |
were not transformed into FMAs because of active deferring. */ |
| 6365 |
hash_set<tree> m_last_result_set; |
| 6366 |
|
| 6367 |
/* Pointer to a flag of the user that needs to be set if CFG has been |
| 6368 |
modified. */ |
| 6369 |
bool *m_cfg_changed_p; |
| 6370 |
}; |
| 6371 |
|
| 6372 |
void |
| 6373 |
math_opts_dom_walker::after_dom_children (basic_block bb) |
| 6374 |
{ |
| 6375 |
gimple_stmt_iterator gsi; |
| 6376 |
|
| 6377 |
fma_deferring_state fma_state (param_avoid_fma_max_bits > 0); |
| 6378 |
|
| 6379 |
for (gphi_iterator psi_next, psi = gsi_start_phis (bb); !gsi_end_p (psi); |
| 6380 |
psi = psi_next) |
| 6381 |
{ |
| 6382 |
psi_next = psi; |
| 6383 |
gsi_next (&psi_next); |
| 6384 |
|
| 6385 |
gimple_stmt_iterator gsi = gsi_after_labels (bb); |
| 6386 |
gphi *phi = psi.phi (); |
| 6387 |
|
| 6388 |
if (match_saturation_add (&gsi, phi) |
| 6389 |
|| match_saturation_sub (&gsi, phi) |
| 6390 |
|| match_saturation_trunc (&gsi, phi)) |
| 6391 |
remove_phi_node (&psi, /* release_lhs_p */ false); |
| 6392 |
} |
| 6393 |
|
| 6394 |
for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi);) |
| 6395 |
{ |
| 6396 |
gimple *stmt = gsi_stmt (gsi); |
| 6397 |
enum tree_code code; |
| 6398 |
|
| 6399 |
if (is_gimple_assign (stmt)) |
| 6400 |
{ |
| 6401 |
code = gimple_assign_rhs_code (stmt); |
| 6402 |
switch (code) |
| 6403 |
{ |
| 6404 |
case MULT_EXPR: |
| 6405 |
if (!convert_mult_to_widen (stmt, &gsi) |
| 6406 |
&& !convert_expand_mult_copysign (stmt, &gsi) |
| 6407 |
&& convert_mult_to_fma (stmt, |
| 6408 |
gimple_assign_rhs1 (stmt), |
| 6409 |
gimple_assign_rhs2 (stmt), |
| 6410 |
&fma_state)) |
| 6411 |
{ |
| 6412 |
gsi_remove (&gsi, true); |
| 6413 |
release_defs (stmt); |
| 6414 |
continue; |
| 6415 |
} |
| 6416 |
match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p); |
| 6417 |
match_unsigned_saturation_sub (&gsi, as_a<gassign *> (stmt)); |
| 6418 |
break; |
| 6419 |
|
| 6420 |
case PLUS_EXPR: |
| 6421 |
match_unsigned_saturation_add (&gsi, as_a<gassign *> (stmt)); |
| 6422 |
match_unsigned_saturation_sub (&gsi, as_a<gassign *> (stmt)); |
| 6423 |
/* fall-through */ |
| 6424 |
case MINUS_EXPR: |
| 6425 |
if (!convert_plusminus_to_widen (&gsi, stmt, code)) |
| 6426 |
{ |
| 6427 |
match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p); |
| 6428 |
if (gsi_stmt (gsi) == stmt) |
| 6429 |
match_uaddc_usubc (&gsi, stmt, code); |
| 6430 |
} |
| 6431 |
break; |
| 6432 |
|
| 6433 |
case BIT_NOT_EXPR: |
| 6434 |
if (match_arith_overflow (&gsi, stmt, code, m_cfg_changed_p)) |
| 6435 |
continue; |
| 6436 |
break; |
| 6437 |
|
| 6438 |
case TRUNC_MOD_EXPR: |
| 6439 |
convert_to_divmod (as_a<gassign *> (stmt)); |
| 6440 |
break; |
| 6441 |
|
| 6442 |
case RSHIFT_EXPR: |
| 6443 |
convert_mult_to_highpart (as_a<gassign *> (stmt), &gsi); |
| 6444 |
break; |
| 6445 |
|
| 6446 |
case BIT_IOR_EXPR: |
| 6447 |
match_unsigned_saturation_add (&gsi, as_a<gassign *> (stmt)); |
| 6448 |
match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt)); |
| 6449 |
/* fall-through */ |
| 6450 |
case BIT_XOR_EXPR: |
| 6451 |
match_uaddc_usubc (&gsi, stmt, code); |
| 6452 |
break; |
| 6453 |
|
| 6454 |
case EQ_EXPR: |
| 6455 |
case NE_EXPR: |
| 6456 |
case LE_EXPR: |
| 6457 |
case GT_EXPR: |
| 6458 |
match_single_bit_test (&gsi, stmt); |
| 6459 |
break; |
| 6460 |
|
| 6461 |
case COND_EXPR: |
| 6462 |
case BIT_AND_EXPR: |
| 6463 |
match_unsigned_saturation_sub (&gsi, as_a<gassign *> (stmt)); |
| 6464 |
break; |
| 6465 |
|
| 6466 |
case NOP_EXPR: |
| 6467 |
match_unsigned_saturation_trunc (&gsi, as_a<gassign *> (stmt)); |
| 6468 |
break; |
| 6469 |
|
| 6470 |
default:; |
| 6471 |
} |
| 6472 |
} |
| 6473 |
else if (is_gimple_call (stmt)) |
| 6474 |
{ |
| 6475 |
switch (gimple_call_combined_fn (stmt)) |
| 6476 |
{ |
| 6477 |
CASE_CFN_POW: |
| 6478 |
if (gimple_call_lhs (stmt) |
| 6479 |
&& TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST |
| 6480 |
&& real_equal (&TREE_REAL_CST (gimple_call_arg (stmt, 1)), |
| 6481 |
&dconst2) |
| 6482 |
&& convert_mult_to_fma (stmt, |
| 6483 |
gimple_call_arg (stmt, 0), |
| 6484 |
gimple_call_arg (stmt, 0), |
| 6485 |
&fma_state)) |
| 6486 |
{ |
| 6487 |
unlink_stmt_vdef (stmt); |
| 6488 |
if (gsi_remove (&gsi, true) |
| 6489 |
&& gimple_purge_dead_eh_edges (bb)) |
| 6490 |
*m_cfg_changed_p = true; |
| 6491 |
release_defs (stmt); |
| 6492 |
continue; |
| 6493 |
} |
| 6494 |
break; |
| 6495 |
|
| 6496 |
case CFN_COND_MUL: |
| 6497 |
if (convert_mult_to_fma (stmt, |
| 6498 |
gimple_call_arg (stmt, 1), |
| 6499 |
gimple_call_arg (stmt, 2), |
| 6500 |
&fma_state, |
| 6501 |
gimple_call_arg (stmt, 0))) |
| 6502 |
|
| 6503 |
{ |
| 6504 |
gsi_remove (&gsi, true); |
| 6505 |
release_defs (stmt); |
| 6506 |
continue; |
| 6507 |
} |
| 6508 |
break; |
| 6509 |
|
| 6510 |
case CFN_COND_LEN_MUL: |
| 6511 |
if (convert_mult_to_fma (stmt, |
| 6512 |
gimple_call_arg (stmt, 1), |
| 6513 |
gimple_call_arg (stmt, 2), |
| 6514 |
&fma_state, |
| 6515 |
gimple_call_arg (stmt, 0), |
| 6516 |
gimple_call_arg (stmt, 4), |
| 6517 |
gimple_call_arg (stmt, 5))) |
| 6518 |
|
| 6519 |
{ |
| 6520 |
gsi_remove (&gsi, true); |
| 6521 |
release_defs (stmt); |
| 6522 |
continue; |
| 6523 |
} |
| 6524 |
break; |
| 6525 |
|
| 6526 |
case CFN_LAST: |
| 6527 |
cancel_fma_deferring (&fma_state); |
| 6528 |
break; |
| 6529 |
|
| 6530 |
default: |
| 6531 |
break; |
| 6532 |
} |
| 6533 |
} |
| 6534 |
else if (gimple_code (stmt) == GIMPLE_COND) |
| 6535 |
{ |
| 6536 |
match_single_bit_test (&gsi, stmt); |
| 6537 |
optimize_spaceship (as_a <gcond *> (stmt)); |
| 6538 |
} |
| 6539 |
gsi_next (&gsi); |
| 6540 |
} |
| 6541 |
if (fma_state.m_deferring_p |
| 6542 |
&& fma_state.m_initial_phi) |
| 6543 |
{ |
| 6544 |
gcc_checking_assert (fma_state.m_last_result); |
| 6545 |
if (!last_fma_candidate_feeds_initial_phi (&fma_state, |
| 6546 |
&m_last_result_set)) |
| 6547 |
cancel_fma_deferring (&fma_state); |
| 6548 |
else |
| 6549 |
m_last_result_set.add (fma_state.m_last_result); |
| 6550 |
} |
| 6551 |
} |
| 6552 |
|
| 6553 |
|
| 6554 |
unsigned int |
| 6555 |
pass_optimize_widening_mul::execute (function *fun) |
| 6556 |
{ |
| 6557 |
bool cfg_changed = false; |
| 6558 |
|
| 6559 |
memset (&widen_mul_stats, 0, sizeof (widen_mul_stats)); |
| 6560 |
calculate_dominance_info (CDI_DOMINATORS); |
| 6561 |
renumber_gimple_stmt_uids (cfun); |
| 6562 |
|
| 6563 |
math_opts_dom_walker (&cfg_changed).walk (ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
| 6564 |
|
| 6565 |
statistics_counter_event (fun, "widening multiplications inserted", |
| 6566 |
widen_mul_stats.widen_mults_inserted); |
| 6567 |
statistics_counter_event (fun, "widening maccs inserted", |
| 6568 |
widen_mul_stats.maccs_inserted); |
| 6569 |
statistics_counter_event (fun, "fused multiply-adds inserted", |
| 6570 |
widen_mul_stats.fmas_inserted); |
| 6571 |
statistics_counter_event (fun, "divmod calls inserted", |
| 6572 |
widen_mul_stats.divmod_calls_inserted); |
| 6573 |
statistics_counter_event (fun, "highpart multiplications inserted", |
| 6574 |
widen_mul_stats.highpart_mults_inserted); |
| 6575 |
|
| 6576 |
return cfg_changed ? TODO_cleanup_cfg : 0; |
| 6577 |
} |
| 6578 |
|
| 6579 |
} // anon namespace |
| 6580 |
|
| 6581 |
gimple_opt_pass * |
| 6582 |
make_pass_optimize_widening_mul (gcc::context *ctxt) |
| 6583 |
{ |
| 6584 |
return new pass_optimize_widening_mul (ctxt); |
| 6585 |
} |