| 1 |
// Late-stage instruction combination pass. |
| 2 |
// Copyright (C) 2023-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 under |
| 7 |
// the terms of the GNU General Public License as published by the Free |
| 8 |
// Software Foundation; either version 3, or (at your option) any later |
| 9 |
// version. |
| 10 |
// |
| 11 |
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 |
// 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 |
// The current purpose of this pass is to substitute definitions into |
| 21 |
// all uses, so that the definition can be removed. However, it could |
| 22 |
// be extended to handle other combination-related optimizations in future. |
| 23 |
// |
| 24 |
// The pass can run before or after register allocation. When running |
| 25 |
// before register allocation, it tries to avoid cases that are likely |
| 26 |
// to increase register pressure. For the same reason, it avoids moving |
| 27 |
// instructions around, even if doing so would allow an optimization to |
| 28 |
// succeed. These limitations are removed when running after register |
| 29 |
// allocation. |
| 30 |
|
| 31 |
#define INCLUDE_ALGORITHM |
| 32 |
#define INCLUDE_FUNCTIONAL |
| 33 |
#define INCLUDE_ARRAY |
| 34 |
#include "config.h" |
| 35 |
#include "system.h" |
| 36 |
#include "coretypes.h" |
| 37 |
#include "backend.h" |
| 38 |
#include "rtl.h" |
| 39 |
#include "df.h" |
| 40 |
#include "rtl-ssa.h" |
| 41 |
#include "print-rtl.h" |
| 42 |
#include "tree-pass.h" |
| 43 |
#include "cfgcleanup.h" |
| 44 |
#include "target.h" |
| 45 |
#include "dbgcnt.h" |
| 46 |
|
| 47 |
using namespace rtl_ssa; |
| 48 |
|
| 49 |
namespace { |
| 50 |
const pass_data pass_data_late_combine = |
| 51 |
{ |
| 52 |
RTL_PASS, // type |
| 53 |
"late_combine", // name |
| 54 |
OPTGROUP_NONE, // optinfo_flags |
| 55 |
TV_LATE_COMBINE, // tv_id |
| 56 |
0, // properties_required |
| 57 |
0, // properties_provided |
| 58 |
0, // properties_destroyed |
| 59 |
0, // todo_flags_start |
| 60 |
TODO_df_finish, // todo_flags_finish |
| 61 |
}; |
| 62 |
|
| 63 |
// Represents an attempt to substitute a single-set definition into all |
| 64 |
// uses of the definition. |
| 65 |
class insn_combination |
| 66 |
{ |
| 67 |
public: |
| 68 |
insn_combination (set_info *, rtx, rtx); |
| 69 |
bool run (); |
| 70 |
array_slice<insn_change *const> use_changes () const; |
| 71 |
|
| 72 |
private: |
| 73 |
use_array get_new_uses (use_info *); |
| 74 |
bool substitute_nondebug_use (use_info *); |
| 75 |
bool substitute_nondebug_uses (set_info *); |
| 76 |
bool try_to_preserve_debug_info (insn_change &, use_info *); |
| 77 |
void substitute_debug_use (use_info *); |
| 78 |
bool substitute_note (insn_info *, rtx, bool); |
| 79 |
void substitute_notes (insn_info *, bool); |
| 80 |
void substitute_note_uses (use_info *); |
| 81 |
void substitute_optional_uses (set_info *); |
| 82 |
|
| 83 |
// Represents the state of the function's RTL at the start of this |
| 84 |
// combination attempt. |
| 85 |
insn_change_watermark m_rtl_watermark; |
| 86 |
|
| 87 |
// Represents the rtl-ssa state at the start of this combination attempt. |
| 88 |
obstack_watermark m_attempt; |
| 89 |
|
| 90 |
// The instruction that contains the definition, and that we're trying |
| 91 |
// to delete. |
| 92 |
insn_info *m_def_insn; |
| 93 |
|
| 94 |
// The definition itself. |
| 95 |
set_info *m_def; |
| 96 |
|
| 97 |
// The destination and source of the single set that defines m_def. |
| 98 |
// The destination is known to be a plain REG. |
| 99 |
rtx m_dest; |
| 100 |
rtx m_src; |
| 101 |
|
| 102 |
// Contains the full list of changes that we want to make, in reverse |
| 103 |
// postorder. |
| 104 |
auto_vec<insn_change *> m_nondebug_changes; |
| 105 |
}; |
| 106 |
|
| 107 |
// Class that represents one run of the pass. |
| 108 |
class late_combine |
| 109 |
{ |
| 110 |
public: |
| 111 |
unsigned int execute (function *); |
| 112 |
|
| 113 |
private: |
| 114 |
rtx optimizable_set (insn_info *); |
| 115 |
bool check_register_pressure (insn_info *, rtx); |
| 116 |
bool check_uses (set_info *, rtx); |
| 117 |
bool combine_into_uses (insn_info *, insn_info *); |
| 118 |
|
| 119 |
auto_vec<insn_info *> m_worklist; |
| 120 |
}; |
| 121 |
|
| 122 |
insn_combination::insn_combination (set_info *def, rtx dest, rtx src) |
| 123 |
: m_rtl_watermark (), |
| 124 |
m_attempt (crtl->ssa->new_change_attempt ()), |
| 125 |
m_def_insn (def->insn ()), |
| 126 |
m_def (def), |
| 127 |
m_dest (dest), |
| 128 |
m_src (src), |
| 129 |
m_nondebug_changes () |
| 130 |
{ |
| 131 |
} |
| 132 |
|
| 133 |
array_slice<insn_change *const> |
| 134 |
insn_combination::use_changes () const |
| 135 |
{ |
| 136 |
return { m_nondebug_changes.address () + 1, |
| 137 |
m_nondebug_changes.length () - 1 }; |
| 138 |
} |
| 139 |
|
| 140 |
// USE is a direct or indirect use of m_def. Return the list of uses |
| 141 |
// that would be needed after substituting m_def into the instruction. |
| 142 |
// The returned list is marked as invalid if USE's insn and m_def_insn |
| 143 |
// use different definitions for the same resource (register or memory). |
| 144 |
use_array |
| 145 |
insn_combination::get_new_uses (use_info *use) |
| 146 |
{ |
| 147 |
auto *def = use->def (); |
| 148 |
auto *use_insn = use->insn (); |
| 149 |
|
| 150 |
use_array new_uses = use_insn->uses (); |
| 151 |
new_uses = remove_uses_of_def (m_attempt, new_uses, def); |
| 152 |
new_uses = merge_access_arrays (m_attempt, m_def_insn->uses (), new_uses); |
| 153 |
if (new_uses.is_valid () && use->ebb () != m_def->ebb ()) |
| 154 |
new_uses = crtl->ssa->make_uses_available (m_attempt, new_uses, use->bb (), |
| 155 |
use_insn->is_debug_insn ()); |
| 156 |
return new_uses; |
| 157 |
} |
| 158 |
|
| 159 |
// Start the process of trying to replace USE by substitution, given that |
| 160 |
// USE occurs in a non-debug instruction. Check: |
| 161 |
// |
| 162 |
// - that the substitution can be represented in RTL |
| 163 |
// |
| 164 |
// - that each use of a resource (register or memory) within the new |
| 165 |
// instruction has a consistent definition |
| 166 |
// |
| 167 |
// - that the new instruction is a recognized pattern |
| 168 |
// |
| 169 |
// - that the instruction can be placed somewhere that makes all definitions |
| 170 |
// and uses valid, and that permits any new hard-register clobbers added |
| 171 |
// during the recognition process |
| 172 |
// |
| 173 |
// Return true on success. |
| 174 |
bool |
| 175 |
insn_combination::substitute_nondebug_use (use_info *use) |
| 176 |
{ |
| 177 |
insn_info *use_insn = use->insn (); |
| 178 |
rtx_insn *use_rtl = use_insn->rtl (); |
| 179 |
|
| 180 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 181 |
dump_insn_slim (dump_file, use->insn ()->rtl ()); |
| 182 |
|
| 183 |
// Reject second and subsequent uses if the target does not allow |
| 184 |
// the defining instruction to be copied. |
| 185 |
if (targetm.cannot_copy_insn_p |
| 186 |
&& m_nondebug_changes.length () >= 2 |
| 187 |
&& targetm.cannot_copy_insn_p (m_def_insn->rtl ())) |
| 188 |
{ |
| 189 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 190 |
fprintf (dump_file, "-- The target does not allow multiple" |
| 191 |
" copies of insn %d\n", m_def_insn->uid ()); |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
// Check that we can change the instruction pattern. Leave recognition |
| 196 |
// of the result till later. |
| 197 |
insn_propagation prop (use_rtl, m_dest, m_src); |
| 198 |
if (!prop.apply_to_pattern (&PATTERN (use_rtl)) |
| 199 |
|| prop.num_replacements == 0) |
| 200 |
{ |
| 201 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 202 |
fprintf (dump_file, "-- RTL substitution failed\n"); |
| 203 |
return false; |
| 204 |
} |
| 205 |
|
| 206 |
use_array new_uses = get_new_uses (use); |
| 207 |
if (!new_uses.is_valid ()) |
| 208 |
{ |
| 209 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 210 |
fprintf (dump_file, "-- could not prove that all sources" |
| 211 |
" are available\n"); |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
// Create a tentative change for the use. |
| 216 |
auto *where = XOBNEW (m_attempt, insn_change); |
| 217 |
auto *use_change = new (where) insn_change (use_insn); |
| 218 |
m_nondebug_changes.safe_push (use_change); |
| 219 |
use_change->new_uses = new_uses; |
| 220 |
|
| 221 |
struct local_ignore : ignore_nothing |
| 222 |
{ |
| 223 |
local_ignore (const set_info *def, const insn_info *use_insn) |
| 224 |
: m_def (def), m_use_insn (use_insn) {} |
| 225 |
|
| 226 |
// We don't limit the number of insns per optimization, so ignoring all |
| 227 |
// insns for all insns would lead to quadratic complexity. Just ignore |
| 228 |
// the use and definition, which should be enough for most purposes. |
| 229 |
bool |
| 230 |
should_ignore_insn (const insn_info *insn) |
| 231 |
{ |
| 232 |
return insn == m_def->insn () || insn == m_use_insn; |
| 233 |
} |
| 234 |
|
| 235 |
// Ignore the definition that we're removing, and all uses of it. |
| 236 |
bool should_ignore_def (const def_info *def) { return def == m_def; } |
| 237 |
|
| 238 |
const set_info *m_def; |
| 239 |
const insn_info *m_use_insn; |
| 240 |
}; |
| 241 |
|
| 242 |
auto ignore = local_ignore (m_def, use_insn); |
| 243 |
|
| 244 |
// Moving instructions before register allocation could increase |
| 245 |
// register pressure. Only try moving them after RA. |
| 246 |
if (reload_completed && can_move_insn_p (use_insn)) |
| 247 |
use_change->move_range = { use_insn->bb ()->head_insn (), |
| 248 |
use_insn->ebb ()->last_bb ()->end_insn () }; |
| 249 |
if (!restrict_movement (*use_change, ignore)) |
| 250 |
{ |
| 251 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 252 |
fprintf (dump_file, "-- cannot satisfy all definitions and uses" |
| 253 |
" in insn %d\n", INSN_UID (use_insn->rtl ())); |
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
if (!recog (m_attempt, *use_change, ignore)) |
| 258 |
return false; |
| 259 |
|
| 260 |
return true; |
| 261 |
} |
| 262 |
|
| 263 |
// Apply substitute_nondebug_use to all direct and indirect uses of DEF. |
| 264 |
// There will be at most one level of indirection. |
| 265 |
bool |
| 266 |
insn_combination::substitute_nondebug_uses (set_info *def) |
| 267 |
{ |
| 268 |
for (use_info *use : def->nondebug_insn_uses ()) |
| 269 |
if (!use->is_live_out_use () |
| 270 |
&& !use->only_occurs_in_notes () |
| 271 |
&& !substitute_nondebug_use (use)) |
| 272 |
return false; |
| 273 |
|
| 274 |
for (use_info *use : def->phi_uses ()) |
| 275 |
if (!substitute_nondebug_uses (use->phi ())) |
| 276 |
return false; |
| 277 |
|
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
// USE_CHANGE.insn () is a debug instruction that uses m_def. Try to |
| 282 |
// substitute the definition into the instruction and try to describe |
| 283 |
// the result in USE_CHANGE. Return true on success. Failure means that |
| 284 |
// the instruction must be reset instead. |
| 285 |
bool |
| 286 |
insn_combination::try_to_preserve_debug_info (insn_change &use_change, |
| 287 |
use_info *use) |
| 288 |
{ |
| 289 |
// Punt on unsimplified subregs of hard registers. In that case, |
| 290 |
// propagation can succeed and create a wider reg than the one we |
| 291 |
// started with. |
| 292 |
if (HARD_REGISTER_NUM_P (use->regno ()) |
| 293 |
&& use->includes_subregs ()) |
| 294 |
return false; |
| 295 |
|
| 296 |
insn_info *use_insn = use_change.insn (); |
| 297 |
rtx_insn *use_rtl = use_insn->rtl (); |
| 298 |
|
| 299 |
use_change.new_uses = get_new_uses (use); |
| 300 |
if (!use_change.new_uses.is_valid () |
| 301 |
|| !restrict_movement (use_change)) |
| 302 |
return false; |
| 303 |
|
| 304 |
insn_propagation prop (use_rtl, m_dest, m_src); |
| 305 |
return prop.apply_to_pattern (&INSN_VAR_LOCATION_LOC (use_rtl)); |
| 306 |
} |
| 307 |
|
| 308 |
// USE_INSN is a debug instruction that uses m_def. Update it to reflect |
| 309 |
// the fact that m_def is going to disappear. Try to preserve the source |
| 310 |
// value if possible, but reset the instruction if not. |
| 311 |
void |
| 312 |
insn_combination::substitute_debug_use (use_info *use) |
| 313 |
{ |
| 314 |
auto *use_insn = use->insn (); |
| 315 |
rtx_insn *use_rtl = use_insn->rtl (); |
| 316 |
|
| 317 |
auto use_change = insn_change (use_insn); |
| 318 |
if (!try_to_preserve_debug_info (use_change, use)) |
| 319 |
{ |
| 320 |
use_change.new_uses = {}; |
| 321 |
use_change.move_range = use_change.insn (); |
| 322 |
INSN_VAR_LOCATION_LOC (use_rtl) = gen_rtx_UNKNOWN_VAR_LOC (); |
| 323 |
} |
| 324 |
insn_change *changes[] = { &use_change }; |
| 325 |
crtl->ssa->change_insns (changes); |
| 326 |
} |
| 327 |
|
| 328 |
// NOTE is a reg note of USE_INSN, which previously used m_def. Update |
| 329 |
// the note to reflect the fact that m_def is going to disappear. Return |
| 330 |
// true on success, or false if the note must be deleted. |
| 331 |
// |
| 332 |
// CAN_PROPAGATE is true if m_dest can be replaced with m_use. |
| 333 |
bool |
| 334 |
insn_combination::substitute_note (insn_info *use_insn, rtx note, |
| 335 |
bool can_propagate) |
| 336 |
{ |
| 337 |
if (REG_NOTE_KIND (note) == REG_EQUAL |
| 338 |
|| REG_NOTE_KIND (note) == REG_EQUIV) |
| 339 |
{ |
| 340 |
insn_propagation prop (use_insn->rtl (), m_dest, m_src); |
| 341 |
return (prop.apply_to_note (&XEXP (note, 0)) |
| 342 |
&& (can_propagate || prop.num_replacements == 0)); |
| 343 |
} |
| 344 |
return true; |
| 345 |
} |
| 346 |
|
| 347 |
// Update USE_INSN's notes after deciding to go ahead with the optimization. |
| 348 |
// CAN_PROPAGATE is true if m_dest can be replaced with m_use. |
| 349 |
void |
| 350 |
insn_combination::substitute_notes (insn_info *use_insn, bool can_propagate) |
| 351 |
{ |
| 352 |
rtx_insn *use_rtl = use_insn->rtl (); |
| 353 |
rtx *ptr = ®_NOTES (use_rtl); |
| 354 |
while (rtx note = *ptr) |
| 355 |
{ |
| 356 |
if (substitute_note (use_insn, note, can_propagate)) |
| 357 |
ptr = &XEXP (note, 1); |
| 358 |
else |
| 359 |
*ptr = XEXP (note, 1); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// We've decided to go ahead with the substitution. Update all REG_NOTES |
| 364 |
// involving USE. |
| 365 |
void |
| 366 |
insn_combination::substitute_note_uses (use_info *use) |
| 367 |
{ |
| 368 |
insn_info *use_insn = use->insn (); |
| 369 |
|
| 370 |
bool can_propagate = true; |
| 371 |
if (use->only_occurs_in_notes ()) |
| 372 |
{ |
| 373 |
// The only uses are in notes. Try to keep the note if we can, |
| 374 |
// but removing it is better than aborting the optimization. |
| 375 |
insn_change use_change (use_insn); |
| 376 |
use_change.new_uses = get_new_uses (use); |
| 377 |
if (!use_change.new_uses.is_valid () |
| 378 |
|| !restrict_movement (use_change)) |
| 379 |
{ |
| 380 |
use_change.move_range = use_insn; |
| 381 |
use_change.new_uses = remove_uses_of_def (m_attempt, |
| 382 |
use_insn->uses (), |
| 383 |
use->def ()); |
| 384 |
can_propagate = false; |
| 385 |
} |
| 386 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 387 |
{ |
| 388 |
fprintf (dump_file, "%s notes in:\n", |
| 389 |
can_propagate ? "updating" : "removing"); |
| 390 |
dump_insn_slim (dump_file, use_insn->rtl ()); |
| 391 |
} |
| 392 |
substitute_notes (use_insn, can_propagate); |
| 393 |
insn_change *changes[] = { &use_change }; |
| 394 |
crtl->ssa->change_insns (changes); |
| 395 |
} |
| 396 |
else |
| 397 |
// We've already decided to update the insn's pattern and know that m_src |
| 398 |
// will be available at the insn's new location. Now update its notes. |
| 399 |
substitute_notes (use_insn, can_propagate); |
| 400 |
} |
| 401 |
|
| 402 |
// We've decided to go ahead with the substitution and we've dealt with |
| 403 |
// all uses that occur in the patterns of non-debug insns. Update all |
| 404 |
// other uses for the fact that m_def is about to disappear. |
| 405 |
void |
| 406 |
insn_combination::substitute_optional_uses (set_info *def) |
| 407 |
{ |
| 408 |
if (auto insn_uses = def->all_insn_uses ()) |
| 409 |
{ |
| 410 |
use_info *use = *insn_uses.begin (); |
| 411 |
while (use) |
| 412 |
{ |
| 413 |
use_info *next_use = use->next_any_insn_use (); |
| 414 |
if (use->is_in_debug_insn ()) |
| 415 |
substitute_debug_use (use); |
| 416 |
else if (!use->is_live_out_use ()) |
| 417 |
substitute_note_uses (use); |
| 418 |
use = next_use; |
| 419 |
} |
| 420 |
} |
| 421 |
for (use_info *use : def->phi_uses ()) |
| 422 |
substitute_optional_uses (use->phi ()); |
| 423 |
} |
| 424 |
|
| 425 |
// Try to perform the substitution. Return true on success. |
| 426 |
bool |
| 427 |
insn_combination::run () |
| 428 |
{ |
| 429 |
if (dump_file && (dump_flags & TDF_DETAILS)) |
| 430 |
{ |
| 431 |
fprintf (dump_file, "\ntrying to combine definition of r%d in:\n", |
| 432 |
m_def->regno ()); |
| 433 |
dump_insn_slim (dump_file, m_def_insn->rtl ()); |
| 434 |
fprintf (dump_file, "into:\n"); |
| 435 |
} |
| 436 |
|
| 437 |
auto def_change = insn_change::delete_insn (m_def_insn); |
| 438 |
m_nondebug_changes.safe_push (&def_change); |
| 439 |
|
| 440 |
if (!substitute_nondebug_uses (m_def) |
| 441 |
|| !changes_are_worthwhile (m_nondebug_changes) |
| 442 |
|| !crtl->ssa->verify_insn_changes (m_nondebug_changes)) |
| 443 |
return false; |
| 444 |
|
| 445 |
// We've now decided that the optimization is valid and profitable. |
| 446 |
// Allow it to be suppressed for bisection purposes. |
| 447 |
if (!dbg_cnt (::late_combine)) |
| 448 |
return false; |
| 449 |
|
| 450 |
substitute_optional_uses (m_def); |
| 451 |
|
| 452 |
confirm_change_group (); |
| 453 |
crtl->ssa->change_insns (m_nondebug_changes); |
| 454 |
return true; |
| 455 |
} |
| 456 |
|
| 457 |
// See whether INSN is a single_set that we can optimize. Return the |
| 458 |
// set if so, otherwise return null. |
| 459 |
rtx |
| 460 |
late_combine::optimizable_set (insn_info *insn) |
| 461 |
{ |
| 462 |
if (!insn->can_be_optimized () |
| 463 |
|| insn->is_asm () |
| 464 |
|| insn->is_call () |
| 465 |
|| insn->has_volatile_refs () |
| 466 |
|| insn->has_pre_post_modify () |
| 467 |
|| !can_move_insn_p (insn)) |
| 468 |
return NULL_RTX; |
| 469 |
|
| 470 |
return single_set (insn->rtl ()); |
| 471 |
} |
| 472 |
|
| 473 |
// Suppose that we can replace all uses of SET_DEST (SET) with SET_SRC (SET), |
| 474 |
// where SET occurs in INSN. Return true if doing so is not likely to |
| 475 |
// increase register pressure. |
| 476 |
bool |
| 477 |
late_combine::check_register_pressure (insn_info *insn, rtx set) |
| 478 |
{ |
| 479 |
// Plain register-to-register moves do not establish a register class |
| 480 |
// preference and have no well-defined effect on the register allocator. |
| 481 |
// If changes in register class are needed, the register allocator is |
| 482 |
// in the best position to place those changes. If no change in |
| 483 |
// register class is needed, then the optimization reduces register |
| 484 |
// pressure if SET_SRC (set) was already live at uses, otherwise the |
| 485 |
// optimization is pressure-neutral. |
| 486 |
rtx src = SET_SRC (set); |
| 487 |
if (REG_P (src)) |
| 488 |
return true; |
| 489 |
|
| 490 |
// On the same basis, substituting a SET_SRC that contains a single |
| 491 |
// pseudo register either reduces pressure or is pressure-neutral, |
| 492 |
// subject to the constraints below. We would need to do more |
| 493 |
// analysis for SET_SRCs that use more than one pseudo register. |
| 494 |
unsigned int nregs = 0; |
| 495 |
for (auto *use : insn->uses ()) |
| 496 |
if (use->is_reg () |
| 497 |
&& !HARD_REGISTER_NUM_P (use->regno ()) |
| 498 |
&& !use->only_occurs_in_notes ()) |
| 499 |
if (++nregs > 1) |
| 500 |
return false; |
| 501 |
|
| 502 |
// If there are no pseudo registers in SET_SRC then the optimization |
| 503 |
// should improve register pressure. |
| 504 |
if (nregs == 0) |
| 505 |
return true; |
| 506 |
|
| 507 |
// We'd be substituting (set (reg R1) SRC) where SRC is known to |
| 508 |
// contain a single pseudo register R2. Assume for simplicity that |
| 509 |
// each new use of R2 would need to be in the same class C as the |
| 510 |
// current use of R2. If, for a realistic allocation, C is a |
| 511 |
// non-strict superset of the R1's register class, the effect on |
| 512 |
// register pressure should be positive or neutral. If instead |
| 513 |
// R1 occupies a different register class from R2, or if R1 has |
| 514 |
// more allocation freedom than R2, then there's a higher risk that |
| 515 |
// the effect on register pressure could be negative. |
| 516 |
// |
| 517 |
// First use constrain_operands to get the most likely choice of |
| 518 |
// alternative. For simplicity, just handle the case where the |
| 519 |
// output operand is operand 0. |
| 520 |
extract_insn (insn->rtl ()); |
| 521 |
rtx dest = SET_DEST (set); |
| 522 |
if (recog_data.n_operands == 0 |
| 523 |
|| recog_data.operand[0] != dest) |
| 524 |
return false; |
| 525 |
|
| 526 |
if (!constrain_operands (0, get_enabled_alternatives (insn->rtl ()))) |
| 527 |
return false; |
| 528 |
|
| 529 |
preprocess_constraints (insn->rtl ()); |
| 530 |
auto *alt = which_op_alt (); |
| 531 |
auto dest_class = alt[0].cl; |
| 532 |
|
| 533 |
// Check operands 1 and above. |
| 534 |
auto check_src = [&] (unsigned int i) |
| 535 |
{ |
| 536 |
if (recog_data.is_operator[i]) |
| 537 |
return true; |
| 538 |
|
| 539 |
rtx op = recog_data.operand[i]; |
| 540 |
if (CONSTANT_P (op)) |
| 541 |
return true; |
| 542 |
|
| 543 |
if (SUBREG_P (op)) |
| 544 |
op = SUBREG_REG (op); |
| 545 |
if (REG_P (op)) |
| 546 |
{ |
| 547 |
// Ignore hard registers. We've already rejected uses of non-fixed |
| 548 |
// hard registers in the SET_SRC. |
| 549 |
if (HARD_REGISTER_P (op)) |
| 550 |
return true; |
| 551 |
|
| 552 |
// Make sure that the source operand's class is at least as |
| 553 |
// permissive as the destination operand's class. |
| 554 |
auto src_class = alternative_class (alt, i); |
| 555 |
if (dest_class != src_class) |
| 556 |
{ |
| 557 |
auto extra_dest_regs = (reg_class_contents[dest_class] |
| 558 |
& ~reg_class_contents[src_class] |
| 559 |
& ~fixed_reg_set); |
| 560 |
if (!hard_reg_set_empty_p (extra_dest_regs)) |
| 561 |
return false; |
| 562 |
} |
| 563 |
|
| 564 |
// Make sure that the source operand occupies no more hard |
| 565 |
// registers than the destination operand. This mostly matters |
| 566 |
// for subregs. |
| 567 |
if (targetm.class_max_nregs (dest_class, GET_MODE (dest)) |
| 568 |
< targetm.class_max_nregs (src_class, GET_MODE (op))) |
| 569 |
return false; |
| 570 |
|
| 571 |
return true; |
| 572 |
} |
| 573 |
return false; |
| 574 |
}; |
| 575 |
for (int i = 1; i < recog_data.n_operands; ++i) |
| 576 |
if (recog_data.operand_type[i] != OP_OUT && !check_src (i)) |
| 577 |
return false; |
| 578 |
|
| 579 |
return true; |
| 580 |
} |
| 581 |
|
| 582 |
// Check uses of DEF to see whether there is anything obvious that |
| 583 |
// prevents the substitution of SET into uses of DEF. |
| 584 |
bool |
| 585 |
late_combine::check_uses (set_info *def, rtx set) |
| 586 |
{ |
| 587 |
use_info *prev_use = nullptr; |
| 588 |
for (use_info *use : def->nondebug_insn_uses ()) |
| 589 |
{ |
| 590 |
insn_info *use_insn = use->insn (); |
| 591 |
|
| 592 |
if (use->is_live_out_use ()) |
| 593 |
continue; |
| 594 |
if (use->only_occurs_in_notes ()) |
| 595 |
continue; |
| 596 |
|
| 597 |
// We cannot replace all uses if the value is live on exit. |
| 598 |
if (use->is_artificial ()) |
| 599 |
return false; |
| 600 |
|
| 601 |
// Avoid increasing the complexity of instructions that |
| 602 |
// reference allocatable hard registers. |
| 603 |
if (!REG_P (SET_SRC (set)) |
| 604 |
&& !reload_completed |
| 605 |
&& (accesses_include_nonfixed_hard_registers (use_insn->uses ()) |
| 606 |
|| accesses_include_nonfixed_hard_registers (use_insn->defs ()))) |
| 607 |
return false; |
| 608 |
|
| 609 |
// Don't substitute into a non-local goto, since it can then be |
| 610 |
// treated as a jump to local label, e.g. in shorten_branches. |
| 611 |
// ??? But this shouldn't be necessary. |
| 612 |
if (use_insn->is_jump () |
| 613 |
&& find_reg_note (use_insn->rtl (), REG_NON_LOCAL_GOTO, NULL_RTX)) |
| 614 |
return false; |
| 615 |
|
| 616 |
// Reject cases where one of the uses is a function argument. |
| 617 |
// The combine attempt should fail anyway, but this is a common |
| 618 |
// case that is easy to check early. |
| 619 |
if (use_insn->is_call () |
| 620 |
&& HARD_REGISTER_P (SET_DEST (set)) |
| 621 |
&& find_reg_fusage (use_insn->rtl (), USE, SET_DEST (set))) |
| 622 |
return false; |
| 623 |
|
| 624 |
// We'll keep the uses in their original order, even if we move |
| 625 |
// them relative to other instructions. Make sure that non-final |
| 626 |
// uses do not change any values that occur in the SET_SRC. |
| 627 |
if (prev_use && prev_use->ebb () == use->ebb ()) |
| 628 |
{ |
| 629 |
def_info *ultimate_def = look_through_degenerate_phi (def); |
| 630 |
if (insn_clobbers_resources (prev_use->insn (), |
| 631 |
ultimate_def->insn ()->uses ())) |
| 632 |
return false; |
| 633 |
} |
| 634 |
|
| 635 |
prev_use = use; |
| 636 |
} |
| 637 |
|
| 638 |
for (use_info *use : def->phi_uses ()) |
| 639 |
if (!use->phi ()->is_degenerate () |
| 640 |
|| !check_uses (use->phi (), set)) |
| 641 |
return false; |
| 642 |
|
| 643 |
return true; |
| 644 |
} |
| 645 |
|
| 646 |
// Try to remove INSN by substituting a definition into all uses. |
| 647 |
// If the optimization moves any instructions before CURSOR, add those |
| 648 |
// instructions to the end of m_worklist. |
| 649 |
bool |
| 650 |
late_combine::combine_into_uses (insn_info *insn, insn_info *cursor) |
| 651 |
{ |
| 652 |
// For simplicity, don't try to handle sets of multiple hard registers. |
| 653 |
// And for correctness, don't remove any assignments to the stack or |
| 654 |
// frame pointers, since that would implicitly change the set of valid |
| 655 |
// memory locations between this assignment and the next. |
| 656 |
// |
| 657 |
// Removing assignments to the hard frame pointer would invalidate |
| 658 |
// backtraces. |
| 659 |
set_info *def = single_set_info (insn); |
| 660 |
if (!def |
| 661 |
|| !def->is_reg () |
| 662 |
|| def->regno () == STACK_POINTER_REGNUM |
| 663 |
|| def->regno () == FRAME_POINTER_REGNUM |
| 664 |
|| def->regno () == HARD_FRAME_POINTER_REGNUM) |
| 665 |
return false; |
| 666 |
|
| 667 |
rtx set = optimizable_set (insn); |
| 668 |
if (!set) |
| 669 |
return false; |
| 670 |
|
| 671 |
// For simplicity, don't try to handle subreg destinations. |
| 672 |
rtx dest = SET_DEST (set); |
| 673 |
if (!REG_P (dest) || def->regno () != REGNO (dest)) |
| 674 |
return false; |
| 675 |
|
| 676 |
// Don't prolong the live ranges of allocatable hard registers, or put |
| 677 |
// them into more complicated instructions. Failing to prevent this |
| 678 |
// could lead to spill failures, or at least to worst register allocation. |
| 679 |
if (!reload_completed |
| 680 |
&& accesses_include_nonfixed_hard_registers (insn->uses ())) |
| 681 |
return false; |
| 682 |
|
| 683 |
if (!reload_completed && !check_register_pressure (insn, set)) |
| 684 |
return false; |
| 685 |
|
| 686 |
if (!check_uses (def, set)) |
| 687 |
return false; |
| 688 |
|
| 689 |
insn_combination combination (def, SET_DEST (set), SET_SRC (set)); |
| 690 |
if (!combination.run ()) |
| 691 |
return false; |
| 692 |
|
| 693 |
for (auto *use_change : combination.use_changes ()) |
| 694 |
if (*use_change->insn () < *cursor) |
| 695 |
m_worklist.safe_push (use_change->insn ()); |
| 696 |
else |
| 697 |
break; |
| 698 |
return true; |
| 699 |
} |
| 700 |
|
| 701 |
// Run the pass on function FN. |
| 702 |
unsigned int |
| 703 |
late_combine::execute (function *fn) |
| 704 |
{ |
| 705 |
// Initialization. |
| 706 |
calculate_dominance_info (CDI_DOMINATORS); |
| 707 |
df_analyze (); |
| 708 |
crtl->ssa = new rtl_ssa::function_info (fn); |
| 709 |
// Don't allow memory_operand to match volatile MEMs. |
| 710 |
init_recog_no_volatile (); |
| 711 |
|
| 712 |
insn_info *insn = *crtl->ssa->nondebug_insns ().begin (); |
| 713 |
while (insn) |
| 714 |
{ |
| 715 |
if (!insn->is_artificial ()) |
| 716 |
{ |
| 717 |
insn_info *prev = insn->prev_nondebug_insn (); |
| 718 |
if (combine_into_uses (insn, prev)) |
| 719 |
{ |
| 720 |
// Any instructions that get added to the worklist were |
| 721 |
// previously after PREV. Thus if we were able to move |
| 722 |
// an instruction X before PREV during one combination, |
| 723 |
// X cannot depend on any instructions that we move before |
| 724 |
// PREV during subsequent combinations. This means that |
| 725 |
// the worklist should be free of backwards dependencies, |
| 726 |
// even if it isn't necessarily in RPO. |
| 727 |
for (unsigned int i = 0; i < m_worklist.length (); ++i) |
| 728 |
combine_into_uses (m_worklist[i], prev); |
| 729 |
m_worklist.truncate (0); |
| 730 |
insn = prev; |
| 731 |
} |
| 732 |
} |
| 733 |
insn = insn->next_nondebug_insn (); |
| 734 |
} |
| 735 |
|
| 736 |
// Finalization. |
| 737 |
if (crtl->ssa->perform_pending_updates ()) |
| 738 |
cleanup_cfg (0); |
| 739 |
|
| 740 |
delete crtl->ssa; |
| 741 |
crtl->ssa = nullptr; |
| 742 |
|
| 743 |
// Make the recognizer allow volatile MEMs again. |
| 744 |
init_recog (); |
| 745 |
free_dominance_info (CDI_DOMINATORS); |
| 746 |
return 0; |
| 747 |
} |
| 748 |
|
| 749 |
class pass_late_combine : public rtl_opt_pass |
| 750 |
{ |
| 751 |
public: |
| 752 |
pass_late_combine (gcc::context *ctxt) |
| 753 |
: rtl_opt_pass (pass_data_late_combine, ctxt) |
| 754 |
{} |
| 755 |
|
| 756 |
// opt_pass methods: |
| 757 |
opt_pass *clone () override { return new pass_late_combine (m_ctxt); } |
| 758 |
bool gate (function *) override; |
| 759 |
unsigned int execute (function *) override; |
| 760 |
}; |
| 761 |
|
| 762 |
bool |
| 763 |
pass_late_combine::gate (function *) |
| 764 |
{ |
| 765 |
return optimize > 0 && flag_late_combine_instructions; |
| 766 |
} |
| 767 |
|
| 768 |
unsigned int |
| 769 |
pass_late_combine::execute (function *fn) |
| 770 |
{ |
| 771 |
return late_combine ().execute (fn); |
| 772 |
} |
| 773 |
|
| 774 |
} // end namespace |
| 775 |
|
| 776 |
// Create a new CC fusion pass instance. |
| 777 |
|
| 778 |
rtl_opt_pass * |
| 779 |
make_pass_late_combine (gcc::context *ctxt) |
| 780 |
{ |
| 781 |
return new pass_late_combine (ctxt); |
| 782 |
} |