see.c 118 KB
Newer Older
Razya Ladelsky committed
1
/* Sign extension elimination optimization for GNU compiler.
2
   Copyright (C) 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
Razya Ladelsky committed
3 4 5 6 7 8
   Contributed by Leehod Baruch <leehod@il.ibm.com>

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
9
-Software Foundation; either version 3, or (at your option) any later
Razya Ladelsky committed
10 11 12 13 14 15 16 17
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.
Razya Ladelsky committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480

Problem description:
--------------------
In order to support 32bit computations on a 64bit machine, sign
extension instructions are generated to ensure the correctness of
the computation.
A possible policy (as currently implemented) is to generate a sign
extension right after each 32bit computation.
Depending on the instruction set of the architecture, some of these
sign extension instructions may be redundant.
There are two cases in which the extension may be redundant:

Case1:
The instruction that uses the 64bit operands that are sign
extended has a dual mode that works with 32bit operands.
For example:

  int32 a, b;

  a = ....	       -->	a = ....
  a = sign extend a    -->
  b = ....	       -->	b = ....
  b = sign extend a    -->
		       -->
  cmpd a, b	       -->	cmpw a, b  //half word compare

Case2:
The instruction that defines the 64bit operand (which is later sign
extended) has a dual mode that defines and sign-extends simultaneously
a 32bit operand.  For example:

  int32 a;

  ld a		     -->   lwa a   // load half word and sign extend
  a = sign extend a  -->
		     -->
  return a	     -->   return a


General idea for solution:
--------------------------
First, try to merge the sign extension with the instruction that
defines the source of the extension and (separately) with the
instructions that uses the extended result.  By doing this, both cases
of redundancies (as described above) will be eliminated.

Then, use partial redundancy elimination to place the non redundant
ones at optimal placements.


Implementation by example:
--------------------------
Note: The instruction stream is not changed till the last phase.

Phase 0: Initial code, as currently generated by gcc.

			 def1		def3
			 se1	 def2	 se3
			  | \	  |	/ |
			  |  \	  |    /  |
			  |   \	  |   /	  |
			  |    \  |  /	  |
			  |	\ | /	  |
			  |	 \|/	  |
			use1	use2	 use3
					 use4
def1 + se1:
set ((reg:SI 10) (..def1rhs..))
set ((reg:DI 100) (sign_extend:DI (reg:SI 10)))

def2:
set ((reg:DI 100) (const_int 7))

def3 + se3:
set ((reg:SI 20) (..def3rhs..))
set ((reg:DI 100) (sign_extend:DI (reg:SI 20)))

use1:
set ((reg:CC...) (compare:CC (reg:DI 100) (...)))

use2, use3, use4:
set ((...) (reg:DI 100))

Phase 1: Propagate extensions to uses.

			 def1		def3
			 se1	 def2	 se3
			  | \	  |	/ |
			  |  \	  |    /  |
			  |   \	  |   /	  |
			  |    \  |  /	  |
			  |	\ | /	  |
			  |	 \|/	  |
			 se	 se	 se
			use1	use2	 use3
					 se
					 use4

From here, all of the subregs are lowpart !

def1, def2, def3: No change.

use1:
set ((reg:DI 100) (sign_extend:DI ((subreg:SI (reg:DI 100)))))
set ((reg:CC...) (compare:CC (reg:DI 100) (...)))

use2, use3, use4:
set ((reg:DI 100) (sign_extend:DI ((subreg:SI (reg:DI 100)))))
set ((...) (reg:DI 100))


Phase 2: Merge and eliminate locally redundant extensions.


			*def1	 def2	*def3
		  [se removed]	  se	 se3
			  | \	  |	/ |
			  |  \	  |    /  |
			  |   \	  |   /	  |
			  |    \  |  /	  |
			  |	\ | /	  |
			  |	 \|/	  |
		  [se removed]	 se	  se
			*use1	use2	 use3
				      [se removed]
					 use4

The instructions that were changed at this phase are marked with
asterisk.

*def1: Merge failed.
       Remove the sign extension instruction, modify def1 and
       insert a move instruction to assure to correctness of the code.
set ((subreg:SI (reg:DI 100)) (..def1rhs..))
set ((reg:SI 10) (subreg:SI (reg:DI 100)))

def2 + se: There is no need for merge.
	   Def2 is not changed but a sign extension instruction is 
	   created.
set ((reg:DI 100) (const_int 7))
set ((reg:DI 100) (sign_extend:DI ((subreg:SI (reg:DI 100)))))

*def3 + se3: Merge succeeded.
set ((reg:DI 100) (sign_extend:DI (..def3rhs..)))
set ((reg:SI 20) (reg:DI 100))
set ((reg:DI 100) (sign_extend:DI (reg:SI 20)))
(The extension instruction is the original one).

*use1: Merge succeeded.  Remove the sign extension instruction.
set ((reg:CC...)
     (compare:CC (subreg:SI (reg:DI 100)) (...)))

use2, use3: Merge failed.  No change.

use4: The extension is locally redundant, therefore it is eliminated 
      at this point.


Phase 3: Eliminate globally redundant extensions.

Following the LCM output:

			 def1	 def2	 def3
				  se	 se3
			  | \	  |	/ |
			  |  \	  |    /  |
			  |   se  |   /	  |
			  |    \  |  /	  |
			  |	\ | /	  |
			  |	 \|/	  |
				[ses removed]
			 use1	use2	 use3
					 use4

se:
set ((reg:DI 100) (sign_extend:DI ((subreg:SI (reg:DI 100)))))

se3:
set ((reg:DI 100) (sign_extend:DI (reg:SI 20)))


Phase 4: Commit changes to the insn stream.


   def1		   def3			*def1	 def2	*def3
    se1	   def2	   se3		    [se removed]       [se removed]
    | \	    |	  / |			  | \	  |	/ |
    |  \    |	 /  |	   ------>	  |  \	  |    /  |
    |	\   |	/   |	   ------>	  |   se  |   /	  |
    |	 \  |  /    |			  |    \  |  /	  |
    |	  \ | /	    |			  |	\ | /	  |
    |	   \|/	    |			  |	 \|/	  |
   use1	   use2	   use3			 *use1	 use2	 use3
		   use4					 use4

The instructions that were changed during the whole optimization are
marked with asterisk.

The result:

def1 + se1:
[  set ((reg:SI 10) (..def1rhs..))		     ]	 - Deleted
[  set ((reg:DI 100) (sign_extend:DI (reg:SI 10)))   ]	 - Deleted
set ((subreg:SI (reg:DI 100)) (..def3rhs..))		 - Inserted
set ((reg:SI 10) (subreg:SI (reg:DI 100)))		 - Inserted

def2:
set ((reg:DI 100) (const_int 7))			 - No change

def3 + se3:
[  set ((reg:SI 20) (..def3rhs..))		     ]	 - Deleted
[  set ((reg:DI 100) (sign_extend:DI (reg:SI 20)))   ]	 - Deleted
set ((reg:DI 100) (sign_extend:DI (..def3rhs..)))	 - Inserted
set ((reg:SI 20) (reg:DI 100))				 - Inserted

use1:
[  set ((reg:CC...) (compare:CC (reg:DI 100) (...))) ]	 - Deleted
set ((reg:CC...)					 - Inserted
     (compare:CC (subreg:SI (reg:DI 100)) (...)))

use2, use3, use4:
set ((...) (reg:DI 100))				 - No change

se:							 - Inserted
set ((reg:DI 100) (sign_extend:DI ((subreg:SI (reg:DI 100)))))

Note: Most of the simple move instructions that were inserted will be
      trivially dead and therefore eliminated.

The implementation outline:
---------------------------
Some definitions:
   A web is RELEVANT if at the end of phase 1, his leader's
     relevancy is {ZERO, SIGN}_EXTENDED_DEF.  The source_mode of
     the web is the source_mode of his leader.
   A definition is a candidate for the optimization if it is part
     of a RELEVANT web and his local source_mode is not narrower
     then the source_mode of its web.
   A use is a candidate for the optimization if it is part of a
     RELEVANT web.
   A simple explicit extension is a single set instruction that
     extends a register (or a subregister) to a register (or
     subregister).
   A complex explicit extension is an explicit extension instruction
     that is not simple.
   A def extension is a simple explicit extension that is
     also a candidate for the optimization.  This extension is part
     of the instruction stream, it is not generated by this
     optimization.
   A use extension is a simple explicit extension that is generated
     and stored for candidate use during this optimization.  It is
     not emitted to the instruction stream till the last phase of
     the optimization.
   A reference is an instruction that satisfy at least on of these
     criteria:
     - It contains a definition with EXTENDED_DEF relevancy in a RELEVANT web.
     - It is followed by a def extension.
     - It contains a candidate use.

Phase 1: Propagate extensions to uses.
  In this phase, we find candidate extensions for the optimization
  and we generate (but not emit) proper extensions "right before the
  uses".

  a. Build a DF object.
  b. Traverse over all the instructions that contains a definition
     and set their local relevancy and local source_mode like this:
     - If the instruction is a simple explicit extension instruction,
       mark it as {ZERO, SIGN}_EXTENDED_DEF according to the extension
       type and mark its source_mode to be the mode of the quantity
       that is been extended.
     - Otherwise, If the instruction has an implicit extension,
       which means that its high part is an extension of its low part,
       or if it is a complicated explicit extension, mark it as
       EXTENDED_DEF and set its source_mode to be the narrowest
       mode that is been extended in the instruction.
  c. Traverse over all the instructions that contains a use and set
     their local relevancy to RELEVANT_USE (except for few corner
     cases).
  d. Produce the web.  During union of two entries, update the
     relevancy and source_mode of the leader.  There are two major
     guide lines for this update:
     - If one of the entries is NOT_RELEVANT, mark the leader
       NOT_RELEVANT.
     - If one is ZERO_EXTENDED_DEF and the other is SIGN_EXTENDED_DEF
       (or vice versa) mark the leader as NOT_RELEVANT.  We don't
       handle this kind of mixed webs.
     (For more details about this update process,
      see see_update_leader_extra_info ()).
  e. Generate uses extensions according to the relevancy and
     source_mode of the webs.

Phase 2: Merge and eliminate locally redundant extensions.
  In this phase, we try to merge def extensions and use
  extensions with their references, and eliminate redundant extensions
  in the same basic block.

  Traverse over all the references.  Do this in basic block number and
  luid number forward order.
  For each reference do:
    a. Peephole optimization - try to merge it with all its
       def extensions and use extensions in the following
       order:
       - Try to merge only the def extensions, one by one.
       - Try to merge only the use extensions, one by one.
       - Try to merge any couple of use extensions simultaneously.
       - Try to merge any def extension with one or two uses
	 extensions simultaneously.
    b. Handle each EXTENDED_DEF in it as if it was already merged with
       an extension.

  During the merge process we save the following data for each
  register in each basic block:
    a. The first instruction that defines the register in the basic
       block.
    b. The last instruction that defines the register in the basic
       block.
    c. The first extension of this register before the first
       instruction that defines it in the basic block.
    c. The first extension of this register after the last
       instruction that defines it in the basic block.
  This data will help us eliminate (or more precisely, not generate)
  locally redundant extensions, and will be useful in the next stage.

  While merging extensions with their reference there are 4 possible
  situations:
    a. A use extension was merged with the reference:
       Delete the extension instruction and save the merged reference
       for phase 4.  (For details, see see_use_extension_merged ())
    b. A use extension failed to be merged with the reference:
       If there is already such an extension in the same basic block
       and it is not dead at this point, delete the unmerged extension
       (it is locally redundant), otherwise properly update the above
       basic block data.
       (For details, see see_merge_one_use_extension ())
    c. A def extension was merged with the reference:
       Mark this extension as a merged_def extension and properly
       update the above basic block data.
       (For details, see see_merge_one_def_extension ())
    d. A def extension failed to be merged with the reference:
       Replace the definition of the NARROWmode register in the
       reference with the proper subreg of WIDEmode register and save
       the result as a merged reference.  Also, properly update the
       the above basic block data.
       (For details, see see_def_extension_not_merged ())

Phase 3: Eliminate globally redundant extensions.
In this phase, we set the bit vectors input of the edge based LCM
using the recorded data on the registers in each basic block.
We also save pointers for all the anticipatable and available
occurrences of the relevant extensions.  Then we run the LCM.

  a. Initialize the comp, antloc, kill bit vectors to zero and the
     transp bit vector to ones.

  b. Traverse over all the references.  Do this in basic block number
     and luid number forward order.  For each reference:
     - Go over all its use extensions.  For each such extension -
	 If it is not dead from the beginning of the basic block SET
	   the antloc bit of the current extension in the current
	   basic block bits.
	 If it is not dead till the end of the basic block SET the
	   comp bit of the current extension in the current basic
	   block bits.
     - Go over all its def extensions that were merged with
       it.  For each such extension -
	 If it is not dead till the end of the basic block SET the
  	   comp bit of the current extension in the current basic
	   block bits.
	 RESET the proper transp and kill bits.
     - Go over all its def extensions that were not merged
       with it.  For each such extension -
	 RESET the transp bit and SET the kill bit of the current
	 extension in the current basic block bits.

  c. Run the edge based LCM.

Phase 4: Commit changes to the insn stream.
This is the only phase that actually changes the instruction stream.
Up to this point the optimization could be aborted at any time.
Here we insert extensions at their best placements and delete the
redundant ones according to the output of the LCM.  We also replace
some of the instructions according to the second phase merges results.

  a. Use the pre_delete_map (from the output of the LCM) in order to
     delete redundant extensions.  This will prevent them from been
     emitted in the first place.

  b. Insert extensions on edges where needed according to
     pre_insert_map and edge_list (from the output of the LCM).

  c. For each reference do-
     - Emit all the uses extensions that were not deleted until now,
       right before the reference.
     - Delete all the merged and unmerged def extensions from
       the instruction stream.
     - Replace the reference with the merged one, if exist.

The implementation consists of four data structures:
- Data structure I
  Purpose: To handle the relevancy of the uses, definitions and webs.
  Relevant structures: web_entry (from df.h), see_entry_extra_info.
  Details: This is a disjoint-set data structure.  Most of its functions are
	   implemented in web.c.  Each definition and use in the code are
	   elements.  A web_entry structure is allocated for each element to
	   hold the element's relevancy and source_mode.  The union rules are
	   defined in see_update_leader_extra_info ().
- Data structure II
  Purpose: To store references and their extensions (uses and defs)
	   and to enable traverse over these references according to basic
	   block order.
  Relevant structure: see_ref_s.
  Details: This data structure consists of an array of splay trees.  One splay
	   tree for each basic block.  The splay tree nodes are references and
	   the keys are the luids of the references.
	   A see_ref_s structure is allocated for each reference.  It holds the
	   reference itself, its def and uses extensions and later the merged
	   version of the reference.
	   Using this data structure we can traverse over all the references of
	   a basic block and their extensions in forward order.
- Data structure III.
  Purpose: To store local properties of registers for each basic block.
	   This data will later help us build the LCM sbitmap_vectors
	   input.
  Relevant structure: see_register_properties.
  Details: This data structure consists of an array of hash tables.  One hash
	   for each basic block.  The hash node are a register properties
	   and the keys are the numbers of the registers.
	   A see_register_properties structure is allocated for each register
	   that we might be interested in its properties.
	   Using this data structure we can easily find the properties of a
	   register in a specific basic block.  This is necessary for locally
	   redundancy elimination and for setting up the LCM input.
- Data structure IV.
  Purpose: To store the extensions that are candidate for PRE and their
	   anticipatable and available occurrences.
  Relevant structure: see_occr, see_pre_extension_expr.
  Details: This data structure is a hash tables.  Its nodes are the extensions
	   that are candidate for PRE.
	   A see_pre_extension_expr structure is allocated for each candidate
	   extension.  It holds a copy of the extension and a linked list of all
	   the anticipatable and available occurrences of it.
	   We use this data structure when we read the output of the LCM.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"

#include "obstack.h"
#include "rtl.h"
#include "output.h"
#include "df.h"
#include "insn-config.h"
#include "recog.h"
#include "expr.h"
#include "splay-tree.h"
#include "hashtab.h"
#include "regs.h"
#include "timevar.h"
#include "tree-pass.h"
481
#include "dce.h"
Razya Ladelsky committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

/* Used to classify defs and uses according to relevancy.  */
enum entry_type {
  NOT_RELEVANT,
  SIGN_EXTENDED_DEF,
  ZERO_EXTENDED_DEF,
  EXTENDED_DEF,
  RELEVANT_USE
};

/* Used to classify extensions in relevant webs.  */
enum extension_type {
  DEF_EXTENSION,
  EXPLICIT_DEF_EXTENSION,
  IMPLICIT_DEF_EXTENSION,
  USE_EXTENSION
};

/* Global data structures and flags.  */

/* This structure will be assigned for each web_entry structure (defined
   in df.h).  It is placed in the extra_info field of a web_entry and holds the
   relevancy and source mode of the web_entry.  */

struct see_entry_extra_info
{
  /* The relevancy of the ref.  */
  enum entry_type relevancy;
  /* The relevancy of the ref.
     This field is updated only once - when this structure is created.  */
  enum entry_type local_relevancy;
  /* The source register mode.  */
  enum machine_mode source_mode;
  /* This field is used only if the relevancy is ZERO/SIGN_EXTENDED_DEF.
     It is updated only once when this structure is created.  */
  enum machine_mode local_source_mode;
  /* This field is used only if the relevancy is EXTENDED_DEF.
     It holds the narrowest mode that is sign extended.  */
  enum machine_mode source_mode_signed;
  /* This field is used only if the relevancy is EXTENDED_DEF.
     It holds the narrowest mode that is zero extended.  */
  enum machine_mode source_mode_unsigned;
};

/* There is one such structure for every reference.  It stores the reference
   itself as well as its extensions (uses and definitions).
   Used as the value in splay_tree see_bb_splay_ar[].  */
struct see_ref_s
{
  /* The luid of the insn.  */
  unsigned int luid;
  /* The insn of the ref.  */
  rtx insn;
  /* The merged insn that was formed from the reference's insn and extensions.
Kazu Hirata committed
536
     If all merges failed, it remains NULL.  */
Razya Ladelsky committed
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
  rtx merged_insn;
  /* The def extensions of the reference that were not merged with
     it.  */
  htab_t unmerged_def_se_hash;
  /* The def extensions of the reference that were merged with
     it.  Implicit extensions of the reference will be stored here too.  */
  htab_t merged_def_se_hash;
  /* The uses extensions of reference.  */
  htab_t use_se_hash;
};

/* There is one such structure for every relevant extended register in a
   specific basic block.  This data will help us build the LCM sbitmap_vectors
   input.  */
struct see_register_properties
{
  /* The register number.  */
  unsigned int regno;
  /* The last luid of the reference that defines this register in this basic
     block.  */
  int last_def;
  /* The luid of the reference that has the first extension of this register
     that appears before any definition in this basic block.  */
  int first_se_before_any_def;
  /* The luid of the reference that has the first extension of this register
     that appears after the last definition in this basic block.  */
  int first_se_after_last_def;
};

/* Occurrence of an expression.
567
   There must be at most one available occurrence and at most one anticipatable
Razya Ladelsky committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
   occurrence per basic block.  */
struct see_occr
{
  /* Next occurrence of this expression.  */
  struct see_occr *next;
  /* The insn that computes the expression.  */
  rtx insn;
  int block_num;
};

/* There is one such structure for every relevant extension expression.
   It holds a copy of this extension instruction as well as a linked lists of
   pointers to all the antic and avail occurrences of it.  */
struct see_pre_extension_expr
{
  /* A copy of the extension instruction.  */
  rtx se_insn;
  /* Index in the available expression bitmaps.  */
  int bitmap_index;
  /* List of anticipatable occurrences in basic blocks in the function.
     An "anticipatable occurrence" is the first occurrence in the basic block,
     the operands are not modified in the basic block prior to the occurrence
     and the output is not used between the start of the block and the
     occurrence.  */
  struct see_occr *antic_occr;
  /* List of available occurrence in basic blocks in the function.
     An "available occurrence" is the last occurrence in the basic block and
     the operands are not modified by following statements in the basic block
     [including this insn].  */
  struct see_occr *avail_occr;
};

/* Helper structure for the note_uses and see_replace_src functions.  */
struct see_replace_data
{
  rtx from;
  rtx to;
};

/* Helper structure for the note_uses and see_mentioned_reg functions.  */
struct see_mentioned_reg_data
{
  rtx reg;
  bool mentioned;
};

/* An array of web_entries.  The i'th definition in the df object is associated
   with def_entry[i]  */
static struct web_entry *def_entry = NULL;
/* An array of web_entries.  The i'th use in the df object is associated with
   use_entry[i]  */
static struct web_entry *use_entry = NULL;
/* Array of splay_trees.
   see_bb_splay_ar[i] refers to the splay tree of the i'th basic block.
   The splay tree will hold see_ref_s structures.  The key is the luid
   of the insn.  This way we can traverse over the references of each basic
   block in forward or backward order.  */
static splay_tree *see_bb_splay_ar = NULL;
/* Array of hashes.
   see_bb_hash_ar[i] refers to the hash of the i'th basic block.
   The hash will hold see_register_properties structure.  The key is regno.  */
static htab_t *see_bb_hash_ar = NULL;
/* Hash table that holds a copy of all the extensions.  The key is the right
   hand side of the se_insn field.  */
static htab_t see_pre_extension_hash = NULL;

/* Local LCM properties of expressions.  */
/* Nonzero for expressions that are transparent in the block.  */
static sbitmap *transp = NULL;
/* Nonzero for expressions that are computed (available) in the block.  */
static sbitmap *comp = NULL;
/* Nonzero for expressions that are locally anticipatable in the block.  */
static sbitmap *antloc = NULL;
/* Nonzero for expressions that are locally killed in the block.  */
static sbitmap *ae_kill = NULL;
/* Nonzero for expressions which should be inserted on a specific edge.  */
static sbitmap *pre_insert_map = NULL;
/* Nonzero for expressions which should be deleted in a specific block.  */
static sbitmap *pre_delete_map = NULL;
/* Contains the edge_list returned by pre_edge_lcm.  */
static struct edge_list *edge_list = NULL;
/* Records the last basic block at the beginning of the optimization.  */
static int last_bb;
/* Records the number of uses at the beginning of the optimization.  */
static unsigned int uses_num;
/* Records the number of definitions at the beginning of the optimization.  */
static unsigned int defs_num;

656
#define ENTRY_EI(ENTRY) ((struct see_entry_extra_info *) (ENTRY)->extra_info)
Razya Ladelsky committed
657 658 659 660 661 662 663 664 665 666 667 668 669

/* Functions implementation.  */

/*  Verifies that EXTENSION's pattern is this:

    set (reg/subreg reg1) (sign/zero_extend:WIDEmode (reg/subreg reg2))

    If it doesn't have the expected pattern return NULL.
    Otherwise, if RETURN_DEST_REG is set, return reg1 else return reg2.  */

static rtx
see_get_extension_reg (rtx extension, bool return_dest_reg)
{
670
  rtx set, rhs, lhs;
Razya Ladelsky committed
671 672 673
  rtx reg1 = NULL;
  rtx reg2 = NULL;

Mircea Namolaru committed
674 675 676 677
  /* Parallel pattern for extension not supported for the moment.  */
  if (GET_CODE (PATTERN (extension)) == PARALLEL)
    return NULL;

Razya Ladelsky committed
678 679 680 681 682 683 684 685 686 687 688 689 690
  set = single_set (extension);
  if (!set)
    return NULL;
  lhs = SET_DEST (set);
  rhs = SET_SRC (set);

  if (REG_P (lhs))
    reg1 = lhs;
  else if (REG_P (SUBREG_REG (lhs)))
    reg1 = SUBREG_REG (lhs);
  else
    return NULL;

691
  if (GET_CODE (rhs) != SIGN_EXTEND && GET_CODE (rhs) != ZERO_EXTEND)
Razya Ladelsky committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    return NULL;

  rhs = XEXP (rhs, 0);
  if (REG_P (rhs))
    reg2 = rhs;
  else if (REG_P (SUBREG_REG (rhs)))
    reg2 = SUBREG_REG (rhs);
  else
    return NULL;

  if (return_dest_reg)
    return reg1;
  return reg2;
}

/*  Verifies that EXTENSION's pattern is this:

    set (reg/subreg reg1) (sign/zero_extend: (...expr...)

    If it doesn't have the expected pattern return UNKNOWN.
    Otherwise, set SOURCE_MODE to be the mode of the extended expr and return
    the rtx code of the extension.  */

715
static enum entry_type
Razya Ladelsky committed
716 717
see_get_extension_data (rtx extension, enum machine_mode *source_mode)
{
718
  rtx rhs, lhs, set;
Razya Ladelsky committed
719 720

  if (!extension || !INSN_P (extension))
721
    return NOT_RELEVANT;
Razya Ladelsky committed
722

Mircea Namolaru committed
723 724 725 726
  /* Parallel pattern for extension not supported for the moment.  */
  if (GET_CODE (PATTERN (extension)) == PARALLEL)
    return NOT_RELEVANT;

Razya Ladelsky committed
727 728 729 730 731 732 733 734
  set = single_set (extension);
  if (!set)
    return NOT_RELEVANT;
  rhs = SET_SRC (set);
  lhs = SET_DEST (set);

  /* Don't handle extensions to something other then register or
     subregister.  */
735
  if (!REG_P (lhs) && GET_CODE (lhs) != SUBREG)
736
    return NOT_RELEVANT;
Razya Ladelsky committed
737

738
  if (GET_CODE (rhs) != SIGN_EXTEND && GET_CODE (rhs) != ZERO_EXTEND)
739
    return NOT_RELEVANT;
Razya Ladelsky committed
740 741

  if (!REG_P (XEXP (rhs, 0))
742 743
      && !(GET_CODE (XEXP (rhs, 0)) == SUBREG
	   && REG_P (SUBREG_REG (XEXP (rhs, 0)))))
744
    return NOT_RELEVANT;
Razya Ladelsky committed
745 746 747 748

  *source_mode = GET_MODE (XEXP (rhs, 0));

  if (GET_CODE (rhs) == SIGN_EXTEND)
749 750
    return SIGN_EXTENDED_DEF;
  return ZERO_EXTENDED_DEF;
Razya Ladelsky committed
751 752 753 754 755 756 757 758
}


/* Generate instruction with the pattern:
   set ((reg r) (sign/zero_extend (subreg:mode (reg r))))
   (the register r on both sides of the set is the same register).
   And recognize it.
   If the recognition failed, this is very bad, return NULL (This will abort
Kazu Hirata committed
759
   the entire optimization).
Razya Ladelsky committed
760 761 762
   Otherwise, return the generated instruction.  */

static rtx
763
see_gen_normalized_extension (rtx reg, enum entry_type extension_code,
Razya Ladelsky committed
764 765
   			      enum machine_mode mode)
{
766
  rtx subreg, insn;
Razya Ladelsky committed
767 768 769 770
  rtx extension = NULL;

  if (!reg
      || !REG_P (reg)
771 772
      || (extension_code != SIGN_EXTENDED_DEF
          && extension_code != ZERO_EXTENDED_DEF))
Razya Ladelsky committed
773 774 775
    return NULL;

  subreg = gen_lowpart_SUBREG (mode, reg);
776
  if (extension_code == SIGN_EXTENDED_DEF)
Razya Ladelsky committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
    extension = gen_rtx_SIGN_EXTEND (GET_MODE (reg), subreg);
  else
    extension = gen_rtx_ZERO_EXTEND (GET_MODE (reg), subreg);

  start_sequence ();
  emit_insn (gen_rtx_SET (VOIDmode, reg, extension));
  insn = get_insns ();
  end_sequence ();

  if (insn_invalid_p (insn))
    /* Recognition failed, this is very bad for this optimization.
       Abort the optimization.  */
    return NULL;
  return insn;
}

/* Hashes and splay_trees related functions implementation.  */

/* Helper functions for the pre_extension hash.
   This kind of hash will hold see_pre_extension_expr structures.

   The key is the right hand side of the se_insn field.
   Note that the se_insn is an expression that looks like:

   set ((reg:WIDEmode r1) (sign_extend:WIDEmode
			   (subreg:NARROWmode (reg:WIDEmode r2))))  */

/* Return TRUE if P1 has the same value in its rhs as P2.
   Otherwise, return FALSE.
   P1 and P2 are see_pre_extension_expr structures.  */

static int
eq_descriptor_pre_extension (const void *p1, const void *p2)
{
811 812 813 814
  const struct see_pre_extension_expr *const extension1 =
    (const struct see_pre_extension_expr *) p1;
  const struct see_pre_extension_expr *const extension2 =
    (const struct see_pre_extension_expr *) p2;
Razya Ladelsky committed
815 816
  rtx set1 = single_set (extension1->se_insn);
  rtx set2 = single_set (extension2->se_insn);
817
  rtx rhs1, rhs2;
Razya Ladelsky committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

  gcc_assert (set1 && set2);
  rhs1 = SET_SRC (set1);
  rhs2 = SET_SRC (set2);

  return rtx_equal_p (rhs1, rhs2);
}


/* P is a see_pre_extension_expr struct, use the RHS of the se_insn field.
   Note that the RHS is an expression that looks like this:
   (sign_extend:WIDEmode (subreg:NARROWmode (reg:WIDEmode r)))  */

static hashval_t
hash_descriptor_pre_extension (const void *p)
{
834 835
  const struct see_pre_extension_expr *const extension =
    (const struct see_pre_extension_expr *) p;
Razya Ladelsky committed
836
  rtx set = single_set (extension->se_insn);
837
  rtx rhs;
Razya Ladelsky committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852

  gcc_assert (set);
  rhs = SET_SRC (set);

  return hash_rtx (rhs, GET_MODE (rhs), 0, NULL, 0);
}


/* Free the allocated memory of the current see_pre_extension_expr struct.
   
   It frees the two linked list of the occurrences structures.  */

static void
hash_del_pre_extension (void *p)
{
853 854
  struct see_pre_extension_expr *const extension =
    (struct see_pre_extension_expr *) p;
Razya Ladelsky committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
  struct see_occr *curr_occr = extension->antic_occr;
  struct see_occr *next_occr = NULL;

  /*  Free the linked list of the anticipatable occurrences.  */
  while (curr_occr)
    {
      next_occr = curr_occr->next;
      free (curr_occr);
      curr_occr = next_occr;
    }

  /*  Free the linked list of the available occurrences.  */
  curr_occr = extension->avail_occr;
  while (curr_occr)
    {
      next_occr = curr_occr->next;
      free (curr_occr);
      curr_occr = next_occr;
    }

  /* Free the see_pre_extension_expr structure itself.  */
  free (extension);
}


/* Helper functions for the register_properties hash.
   This kind of hash will hold see_register_properties structures.

   The value of the key is the regno field of the structure.  */

/* Return TRUE if P1 has the same value in the regno field as P2.
   Otherwise, return FALSE.
   Where P1 and P2 are see_register_properties structures.  */

static int
eq_descriptor_properties (const void *p1, const void *p2)
{
892 893 894 895
  const struct see_register_properties *const curr_prop1 =
    (const struct see_register_properties *) p1;
  const struct see_register_properties *const curr_prop2 =
    (const struct see_register_properties *) p2;
Razya Ladelsky committed
896

897
  return curr_prop1->regno == curr_prop2->regno;
Razya Ladelsky committed
898 899 900 901 902 903 904 905 906
}


/* P is a see_register_properties struct, use the register number in the
   regno field.  */

static hashval_t
hash_descriptor_properties (const void *p)
{
907 908
  const struct see_register_properties *const curr_prop =
    (const struct see_register_properties *) p;
Razya Ladelsky committed
909 910 911 912 913 914 915 916
  return curr_prop->regno;
}


/* Free the allocated memory of the current see_register_properties struct.  */
static void
hash_del_properties (void *p)
{
917 918
  struct see_register_properties *const curr_prop =
    (struct see_register_properties *) p;
Razya Ladelsky committed
919 920 921 922 923 924 925 926 927 928 929 930 931
  free (curr_prop);
}


/* Helper functions for an extension hash.
   This kind of hash will hold insns that look like:

   set ((reg:WIDEmode r1) (sign_extend:WIDEmode
			   (subreg:NARROWmode (reg:WIDEmode r2))))
   or
   set ((reg:WIDEmode r1) (sign_extend:WIDEmode (reg:NARROWmode r2)))

   The value of the key is (REGNO (reg:WIDEmode r1))
932
   It is possible to search this hash in two ways:
Razya Ladelsky committed
933 934 935 936 937 938 939 940 941 942 943
   1.  By a register rtx. The Value that is been compared to the keys is the
       REGNO of it.
   2.  By an insn with the above pattern. The Value that is been compared to
       the keys is the REGNO of the reg on the lhs.  */

/* Return TRUE if P1 has the same value as P2.  Otherwise, return FALSE.
   Where P1 is an insn and P2 is an insn or a register.  */

static int
eq_descriptor_extension (const void *p1, const void *p2)
{
944 945
  const_rtx const insn = (const_rtx) p1;
  const_rtx const element = (const_rtx) p2;
Razya Ladelsky committed
946
  rtx set1 = single_set (insn);
947
  rtx dest_reg1;
Razya Ladelsky committed
948
  rtx set2 = NULL;
949
  const_rtx dest_reg2 = NULL;
Razya Ladelsky committed
950 951 952 953 954 955 956 957 958 959 960 961 962

  gcc_assert (set1 && element && (REG_P (element) || INSN_P (element)));

  dest_reg1 = SET_DEST (set1);

  if (INSN_P (element))
    {
      set2 = single_set (element);
      dest_reg2 = SET_DEST (set2);
    }
  else
    dest_reg2 = element;

963
  return REGNO (dest_reg1) == REGNO (dest_reg2);
Razya Ladelsky committed
964 965 966 967 968 969 970 971 972
}


/* If P is an insn, use the register number of its lhs
   otherwise, P is a register, use its number.  */

static hashval_t
hash_descriptor_extension (const void *p)
{
973
  const_rtx const r = (const_rtx) p;
974
  rtx set, lhs;
Razya Ladelsky committed
975 976 977

  if (r && REG_P (r))
    return REGNO (r);
978 979 980 981 982 983

  gcc_assert (r && INSN_P (r));
  set = single_set (r);
  gcc_assert (set);
  lhs = SET_DEST (set);
  return REGNO (lhs);
Razya Ladelsky committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
}


/* Helper function for a see_bb_splay_ar[i] splay tree.
   It frees all the allocated memory of a struct see_ref_s pointer.

   VALUE is the value of a splay tree node.  */

static void
see_free_ref_s (splay_tree_value value)
{
  struct see_ref_s *ref_s = (struct see_ref_s *)value;

  if (ref_s->unmerged_def_se_hash)
    htab_delete (ref_s->unmerged_def_se_hash);
  if (ref_s->merged_def_se_hash)
    htab_delete (ref_s->merged_def_se_hash);
  if (ref_s->use_se_hash)
    htab_delete (ref_s->use_se_hash);
  free (ref_s);
}


/* Rest of the implementation.  */

/* Search the extension hash for a suitable entry for EXTENSION.
   TYPE is the type of EXTENSION (USE_EXTENSION or DEF_EXTENSION).

   If TYPE is DEF_EXTENSION we need to normalize EXTENSION before searching the
   extension hash.

   If a suitable entry was found, return the slot.  Otherwise, store EXTENSION
   in the hash and return NULL.  */

static struct see_pre_extension_expr *
see_seek_pre_extension_expr (rtx extension, enum extension_type type)
{
1021
  struct see_pre_extension_expr **slot_pre_exp, temp_pre_exp;
Razya Ladelsky committed
1022
  rtx dest_extension_reg = see_get_extension_reg (extension, 1);
1023
  enum entry_type extension_code;
Razya Ladelsky committed
1024 1025 1026 1027 1028 1029
  enum machine_mode source_extension_mode;

  if (type == DEF_EXTENSION)
    {
      extension_code = see_get_extension_data (extension,
					       &source_extension_mode);
1030
      gcc_assert (extension_code != NOT_RELEVANT);
Razya Ladelsky committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
      extension =
	see_gen_normalized_extension (dest_extension_reg, extension_code,
				      source_extension_mode);
    }
  temp_pre_exp.se_insn = extension;
  slot_pre_exp =
    (struct see_pre_extension_expr **) htab_find_slot (see_pre_extension_hash,
							&temp_pre_exp, INSERT);
  if (*slot_pre_exp == NULL)
    /* This is the first time this extension instruction is encountered.  Store
       it in the hash.  */
    {
1043
      (*slot_pre_exp) = XNEW (struct see_pre_extension_expr);
Razya Ladelsky committed
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
      (*slot_pre_exp)->se_insn = extension;
      (*slot_pre_exp)->bitmap_index =
	(htab_elements (see_pre_extension_hash) - 1);
      (*slot_pre_exp)->antic_occr = NULL;
      (*slot_pre_exp)->avail_occr = NULL;
      return NULL;
    }
  return *slot_pre_exp;
}


/* This function defines how to update the extra_info of the web_entry.

   FIRST is the pointer of the extra_info of the first web_entry.
   SECOND is the pointer of the extra_info of the second web_entry.
   The first web_entry will be the predecessor (leader) of the second web_entry
   after the union.
   
   Return true if FIRST and SECOND points to the same web entry structure and 
   nothing is done.  Otherwise, return false.  */

static bool
see_update_leader_extra_info (struct web_entry *first, struct web_entry *second)
{
1068
  struct see_entry_extra_info *first_ei, *second_ei;
Razya Ladelsky committed
1069 1070 1071 1072 1073 1074 1075

  first = unionfind_root (first);
  second = unionfind_root (second);

  if (unionfind_union (first, second))
    return true;

1076 1077
  first_ei = (struct see_entry_extra_info *) first->extra_info;
  second_ei = (struct see_entry_extra_info *) second->extra_info;
Razya Ladelsky committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

  gcc_assert (first_ei && second_ei);

  if (second_ei->relevancy == NOT_RELEVANT)
    {
      first_ei->relevancy = NOT_RELEVANT;
      return false;
    }
  switch (first_ei->relevancy)
    {
    case NOT_RELEVANT:
1089
      break;
Razya Ladelsky committed
1090 1091 1092 1093
    case RELEVANT_USE:
      switch (second_ei->relevancy)
	{
	case RELEVANT_USE:
1094
	  break;
Razya Ladelsky committed
1095 1096 1097 1098
	case EXTENDED_DEF:
	  first_ei->relevancy = second_ei->relevancy;
	  first_ei->source_mode_signed = second_ei->source_mode_signed;
	  first_ei->source_mode_unsigned = second_ei->source_mode_unsigned;
1099
	  break;
Razya Ladelsky committed
1100 1101 1102 1103
	case SIGN_EXTENDED_DEF:
	case ZERO_EXTENDED_DEF:
	  first_ei->relevancy = second_ei->relevancy;
	  first_ei->source_mode = second_ei->source_mode;
1104
	  break;
Razya Ladelsky committed
1105 1106 1107
	default:
	  gcc_unreachable ();
	}
1108
      break;
Razya Ladelsky committed
1109 1110 1111 1112 1113 1114 1115 1116
    case SIGN_EXTENDED_DEF:
      switch (second_ei->relevancy)
	{
	case SIGN_EXTENDED_DEF:
	  /* The mode of the root should be the wider one in this case.  */
	  first_ei->source_mode =
	    (first_ei->source_mode > second_ei->source_mode) ?
	    first_ei->source_mode : second_ei->source_mode;
1117
	  break;
Razya Ladelsky committed
1118
	case RELEVANT_USE:
1119
	  break;
Razya Ladelsky committed
1120 1121 1122
	case ZERO_EXTENDED_DEF:
	  /* Don't mix webs with zero extend and sign extend.  */
	  first_ei->relevancy = NOT_RELEVANT;
1123
	  break;
Razya Ladelsky committed
1124 1125 1126 1127 1128 1129 1130 1131
	case EXTENDED_DEF:
	  if (second_ei->source_mode_signed == MAX_MACHINE_MODE)
	    first_ei->relevancy = NOT_RELEVANT;
	  else
	    /* The mode of the root should be the wider one in this case.  */
	    first_ei->source_mode =
	      (first_ei->source_mode > second_ei->source_mode_signed) ?
	      first_ei->source_mode : second_ei->source_mode_signed;
1132
	  break;
Razya Ladelsky committed
1133 1134 1135
	default:
	  gcc_unreachable ();
	}
1136
      break;
Razya Ladelsky committed
1137 1138 1139 1140 1141 1142 1143
    /* This case is similar to the previous one, with little changes.  */
    case ZERO_EXTENDED_DEF:
      switch (second_ei->relevancy)
	{
	case SIGN_EXTENDED_DEF:
	  /* Don't mix webs with zero extend and sign extend.  */
	  first_ei->relevancy = NOT_RELEVANT;
1144
	  break;
Razya Ladelsky committed
1145
	case RELEVANT_USE:
1146
	  break;
Razya Ladelsky committed
1147 1148 1149 1150 1151
	case ZERO_EXTENDED_DEF:
	  /* The mode of the root should be the wider one in this case.  */
	  first_ei->source_mode =
	    (first_ei->source_mode > second_ei->source_mode) ?
	    first_ei->source_mode : second_ei->source_mode;
1152
	  break;
Razya Ladelsky committed
1153 1154 1155 1156 1157 1158 1159 1160
	case EXTENDED_DEF:
	  if (second_ei->source_mode_unsigned == MAX_MACHINE_MODE)
	    first_ei->relevancy = NOT_RELEVANT;
	  else
	    /* The mode of the root should be the wider one in this case.  */
	    first_ei->source_mode =
	      (first_ei->source_mode > second_ei->source_mode_unsigned) ?
	      first_ei->source_mode : second_ei->source_mode_unsigned;
1161
	  break;
Razya Ladelsky committed
1162 1163 1164
	default:
	  gcc_unreachable ();
	}
1165
      break;
Razya Ladelsky committed
1166
    case EXTENDED_DEF:
1167 1168
      if (first_ei->source_mode_signed != MAX_MACHINE_MODE
	  && first_ei->source_mode_unsigned != MAX_MACHINE_MODE)
Razya Ladelsky committed
1169 1170 1171 1172 1173 1174 1175 1176
	{
	  switch (second_ei->relevancy)
	    {
	    case SIGN_EXTENDED_DEF:
	      first_ei->relevancy = SIGN_EXTENDED_DEF;
	      first_ei->source_mode =
		(first_ei->source_mode_signed > second_ei->source_mode) ?
		first_ei->source_mode_signed : second_ei->source_mode;
1177
	      break;
Razya Ladelsky committed
1178
	    case RELEVANT_USE:
1179
	      break;
Razya Ladelsky committed
1180 1181 1182 1183 1184
	    case ZERO_EXTENDED_DEF:
	      first_ei->relevancy = ZERO_EXTENDED_DEF;
	      first_ei->source_mode =
		(first_ei->source_mode_unsigned > second_ei->source_mode) ?
		first_ei->source_mode_unsigned : second_ei->source_mode;
1185
	      break;
Razya Ladelsky committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197
	    case EXTENDED_DEF:
	      if (second_ei->source_mode_unsigned != MAX_MACHINE_MODE)
		first_ei->source_mode_unsigned =
		  (first_ei->source_mode_unsigned >
		  second_ei->source_mode_unsigned) ?
		  first_ei->source_mode_unsigned :
		  second_ei->source_mode_unsigned;
	      if (second_ei->source_mode_signed != MAX_MACHINE_MODE)
		first_ei->source_mode_signed =
		  (first_ei->source_mode_signed >
		  second_ei->source_mode_signed) ?
		  first_ei->source_mode_signed : second_ei->source_mode_signed;
1198
	      break;
Razya Ladelsky committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
	    default:
	      gcc_unreachable ();
	    }
	}
      else if (first_ei->source_mode_signed == MAX_MACHINE_MODE)
	{
	  gcc_assert (first_ei->source_mode_unsigned != MAX_MACHINE_MODE);
	  switch (second_ei->relevancy)
	    {
	    case SIGN_EXTENDED_DEF:
	      first_ei->relevancy = NOT_RELEVANT;
1210
	      break;
Razya Ladelsky committed
1211
	    case RELEVANT_USE:
1212
	      break;
Razya Ladelsky committed
1213 1214 1215 1216 1217
	    case ZERO_EXTENDED_DEF:
	      first_ei->relevancy = ZERO_EXTENDED_DEF;
	      first_ei->source_mode =
		(first_ei->source_mode_unsigned > second_ei->source_mode) ?
		first_ei->source_mode_unsigned : second_ei->source_mode;
1218
	      break;
Razya Ladelsky committed
1219 1220 1221 1222 1223 1224 1225 1226 1227
	    case EXTENDED_DEF:
	      if (second_ei->source_mode_unsigned == MAX_MACHINE_MODE)
		first_ei->relevancy = NOT_RELEVANT;
	      else
		first_ei->source_mode_unsigned =
		  (first_ei->source_mode_unsigned >
		  second_ei->source_mode_unsigned) ?
		  first_ei->source_mode_unsigned :
		  second_ei->source_mode_unsigned;
1228
	      break;
Razya Ladelsky committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	    default:
	      gcc_unreachable ();
	    }
	}
      else
	{
	  gcc_assert (first_ei->source_mode_unsigned == MAX_MACHINE_MODE);
	  gcc_assert (first_ei->source_mode_signed != MAX_MACHINE_MODE);
	  switch (second_ei->relevancy)
	    {
	    case SIGN_EXTENDED_DEF:
	      first_ei->relevancy = SIGN_EXTENDED_DEF;
	      first_ei->source_mode =
		(first_ei->source_mode_signed > second_ei->source_mode) ?
		first_ei->source_mode_signed : second_ei->source_mode;
1244
	      break;
Razya Ladelsky committed
1245
	    case RELEVANT_USE:
1246
	      break;
Razya Ladelsky committed
1247 1248
	    case ZERO_EXTENDED_DEF:
	      first_ei->relevancy = NOT_RELEVANT;
1249
	      break;
Razya Ladelsky committed
1250 1251 1252 1253 1254 1255 1256 1257
	    case EXTENDED_DEF:
	      if (second_ei->source_mode_signed == MAX_MACHINE_MODE)
		first_ei->relevancy = NOT_RELEVANT;
	      else
		first_ei->source_mode_signed =
		  (first_ei->source_mode_signed >
		  second_ei->source_mode_signed) ?
		  first_ei->source_mode_signed : second_ei->source_mode_signed;
1258
	      break;
Razya Ladelsky committed
1259 1260 1261 1262
	    default:
	      gcc_unreachable ();
	    }
	}
1263
      break;
Razya Ladelsky committed
1264
    default:
1265
      /* Unknown pattern type.  */
Razya Ladelsky committed
1266 1267
      gcc_unreachable ();
    }
1268 1269

  return false;
Razya Ladelsky committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
}


/* Free global data structures.  */

static void
see_free_data_structures (void)
{
  int i;
  unsigned int j;

  /* Free the bitmap vectors.  */
  if (transp)
    {
      sbitmap_vector_free (transp);
      transp = NULL;
      sbitmap_vector_free (comp);
      comp = NULL;
      sbitmap_vector_free (antloc);
      antloc = NULL;
      sbitmap_vector_free (ae_kill);
      ae_kill = NULL;
    }
  if (pre_insert_map)
    {
      sbitmap_vector_free (pre_insert_map);
      pre_insert_map = NULL;
    }
  if (pre_delete_map)
    {
      sbitmap_vector_free (pre_delete_map);
      pre_delete_map = NULL;
    }
  if (edge_list)
    {
      free_edge_list (edge_list);
      edge_list = NULL;
    }

  /*  Free the extension hash.  */
  htab_delete (see_pre_extension_hash);

  /*  Free the array of hashes.  */
  for (i = 0; i < last_bb; i++)
    if (see_bb_hash_ar[i])
      htab_delete (see_bb_hash_ar[i]);
  free (see_bb_hash_ar);

  /*  Free the array of splay trees.  */
  for (i = 0; i < last_bb; i++)
    if (see_bb_splay_ar[i])
      splay_tree_delete (see_bb_splay_ar[i]);
  free (see_bb_splay_ar);

  /*  Free the array of web entries and their extra info field.  */
  for (j = 0; j < defs_num; j++)
    free (def_entry[j].extra_info);
  free (def_entry);
  for (j = 0; j < uses_num; j++)
    free (use_entry[j].extra_info);
  free (use_entry);
}


/* Initialize global data structures and variables.  */

static void
see_initialize_data_structures (void)
{
1339 1340 1341
  unsigned int max_reg = max_reg_num ();
  unsigned int i;

Razya Ladelsky committed
1342
  /* Build the df object. */
1343 1344 1345 1346
  df_set_flags (DF_EQ_NOTES);
  df_chain_add_problem (DF_DU_CHAIN + DF_UD_CHAIN);
  df_analyze ();
  df_set_flags (DF_DEFER_INSN_RESCAN);
Razya Ladelsky committed
1347 1348

  if (dump_file)
1349
    df_dump (dump_file);
Razya Ladelsky committed
1350 1351 1352

  /* Record the last basic block at the beginning of the optimization.  */
  last_bb = last_basic_block;
1353 1354 1355 1356 1357 1358 1359 1360 1361

  /* Record the number of uses and defs at the beginning of the optimization.  */
  uses_num = 0;
  defs_num = 0;
  for (i = 0; i < max_reg; i++) 
    {
      uses_num += DF_REG_USE_COUNT (i) + DF_REG_EQ_USE_COUNT (i);
      defs_num += DF_REG_DEF_COUNT (i);
    }
Razya Ladelsky committed
1362 1363

  /*  Allocate web entries array for the union-find data structure.  */
1364 1365
  def_entry = XCNEWVEC (struct web_entry, defs_num);
  use_entry = XCNEWVEC (struct web_entry, uses_num);
Razya Ladelsky committed
1366 1367 1368

  /*  Allocate an array of splay trees.
      One splay tree for each basic block.  */
1369
  see_bb_splay_ar = XCNEWVEC (splay_tree, last_bb);
Razya Ladelsky committed
1370 1371 1372

  /*  Allocate an array of hashes.
      One hash for each basic block.  */
1373
  see_bb_hash_ar = XCNEWVEC (htab_t, last_bb);
Razya Ladelsky committed
1374 1375 1376

  /*  Allocate the extension hash.  It will hold the extensions that we want
      to PRE.  */
1377 1378 1379 1380
  see_pre_extension_hash = htab_create (10, 
					hash_descriptor_pre_extension, 
					eq_descriptor_pre_extension,
					hash_del_pre_extension);
Razya Ladelsky committed
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
}


/* Function called by note_uses to check if a register is used in a
   subexpressions.

   X is a pointer to the subexpression and DATA is a pointer to a
   see_mentioned_reg_data structure that contains the register to look for and
   a place for the result.  */

static void
see_mentioned_reg (rtx *x, void *data)
{
  struct see_mentioned_reg_data *d
    = (struct see_mentioned_reg_data *) data;

  if (reg_mentioned_p (d->reg, *x))
    d->mentioned = true;
}


/* We don't want to merge a use extension with a reference if the extended
   register is used only in a simple move instruction.  We also don't want to
   merge a def extension with a reference if the source register of the
   extension is defined only in a simple move in the reference.

   REF is the reference instruction.
   EXTENSION is the use extension or def extension instruction.
   TYPE is the type of the extension (use or def).

   Return true if the reference is complicated enough, so we would like to merge
   it with the extension.  Otherwise, return false.  */

static bool
see_want_to_be_merged_with_extension (rtx ref, rtx extension,
   				      enum extension_type type)
{
1418
  rtx pat;
Razya Ladelsky committed
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
  rtx dest_extension_reg = see_get_extension_reg (extension, 1);
  rtx source_extension_reg = see_get_extension_reg (extension, 0);
  enum rtx_code code;
  struct see_mentioned_reg_data d;
  int i;

  pat = PATTERN (ref);
  code = GET_CODE (pat);

  if (code == PARALLEL)
    {
      for (i = 0; i < XVECLEN (pat, 0); i++)
	{
	  rtx sub = XVECEXP (pat, 0, i);

1434 1435 1436 1437 1438 1439 1440
	  if (GET_CODE (sub) == SET
	      && (REG_P (SET_DEST (sub))
		  || (GET_CODE (SET_DEST (sub)) == SUBREG
		      && REG_P (SUBREG_REG (SET_DEST (sub)))))
	      && (REG_P (SET_SRC (sub))
		  || (GET_CODE (SET_SRC (sub)) == SUBREG
		      && REG_P (SUBREG_REG (SET_SRC (sub))))))
Razya Ladelsky committed
1441 1442
	    {
	      /* This is a simple move SET.  */
1443
	      if (type == DEF_EXTENSION
Razya Ladelsky committed
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
		  && reg_mentioned_p (source_extension_reg, SET_DEST (sub)))
		return false;
	    }
	  else
	    {
	      /* This is not a simple move SET.
		 Check if it uses the source of the extension.  */
	      if (type == USE_EXTENSION)
		{
  		  d.reg = dest_extension_reg;
		  d.mentioned = false;
		  note_uses (&sub, see_mentioned_reg, &d);
		  if (d.mentioned)
		    return true;
		}
	    }
	}
      if (type == USE_EXTENSION)
	return false;
    }
  else
    {
1466
      if (code == SET
Razya Ladelsky committed
1467
	  && (REG_P (SET_DEST (pat))
1468 1469
	      || (GET_CODE (SET_DEST (pat)) == SUBREG
		  && REG_P (SUBREG_REG (SET_DEST (pat)))))
Razya Ladelsky committed
1470
	  && (REG_P (SET_SRC (pat))
1471 1472
	      || (GET_CODE (SET_SRC (pat)) == SUBREG
		  && REG_P (SUBREG_REG (SET_SRC (pat))))))
Razya Ladelsky committed
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
	/* This is a simple move SET.  */
	return false;
     }

  return true;
}


/* Print the register number of the current see_register_properties
   structure.

   This is a subroutine of see_main called via htab_traverse.
   SLOT contains the current see_register_properties structure pointer.  */

static int
see_print_register_properties (void **slot, void *b ATTRIBUTE_UNUSED)
{
1490 1491
  const struct see_register_properties *const prop =
    (const struct see_register_properties *) *slot;
Razya Ladelsky committed
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507

  gcc_assert (prop);
  fprintf (dump_file, "Property found for register %d\n", prop->regno);
  return 1;
}


/* Print the extension instruction of the current see_register_properties
   structure.

   This is a subroutine of see_main called via htab_traverse.
   SLOT contains the current see_pre_extension_expr structure pointer.  */

static int
see_print_pre_extension_expr (void **slot, void *b ATTRIBUTE_UNUSED)
{
1508 1509
  const struct see_pre_extension_expr *const pre_extension =
    (const struct see_pre_extension_expr *) *slot;
Razya Ladelsky committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

  gcc_assert (pre_extension
  	      && pre_extension->se_insn
	      && INSN_P (pre_extension->se_insn));

  fprintf (dump_file, "Index %d for:\n", pre_extension->bitmap_index);
  print_rtl_single (dump_file, pre_extension->se_insn);

  return 1;
}


/* Phase 4 implementation: Commit changes to the insn stream.  */

/* Delete the merged def extension.

   This is a subroutine of see_commit_ref_changes called via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_delete_merged_def_extension (void **slot, void *b ATTRIBUTE_UNUSED)
{
1534
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561

  if (dump_file)
    {
      fprintf (dump_file, "Deleting merged def extension:\n");
      print_rtl_single (dump_file, def_se);
    }

  if (INSN_DELETED_P (def_se))
    /* This def extension is an implicit one.  No need to delete it since
       it is not in the insn stream.  */
    return 1;

  delete_insn (def_se);
  return 1;
}


/* Delete the unmerged def extension.

   This is a subroutine of see_commit_ref_changes called via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_delete_unmerged_def_extension (void **slot, void *b ATTRIBUTE_UNUSED)
{
1562
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584

  if (dump_file)
    {
      fprintf (dump_file, "Deleting unmerged def extension:\n");
      print_rtl_single (dump_file, def_se);
    }

  delete_insn (def_se);
  return 1;
}


/* Emit the non-redundant use extension to the instruction stream.

   This is a subroutine of see_commit_ref_changes called via htab_traverse.

   SLOT contains the current use extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_emit_use_extension (void **slot, void *b)
{
1585
  rtx use_se = (rtx) *slot;
Razya Ladelsky committed
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;

  if (INSN_DELETED_P (use_se))
    /* This use extension was previously removed according to the lcm
       output.  */
    return 1;

  if (dump_file)
    {
      fprintf (dump_file, "Inserting use extension:\n");
      print_rtl_single (dump_file, use_se);
    }

1599
  add_insn_before (use_se, curr_ref_s->insn, NULL);
Razya Ladelsky committed
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665

  return 1;
}


/* For each relevant reference:
   a. Emit the non-redundant use extensions.
   b. Delete the def extensions.
   c. Replace the original reference with the merged one (if exists) and add the
      move instructions that were generated.

   This is a subroutine of see_commit_changes called via splay_tree_foreach.

   STN is the current node in the see_bb_splay_ar[i] splay tree.  It holds a
   see_ref_s structure.  */

static int
see_commit_ref_changes (splay_tree_node stn,
		   	void *data ATTRIBUTE_UNUSED)
{
  htab_t use_se_hash = ((struct see_ref_s *) (stn->value))->use_se_hash;
  htab_t unmerged_def_se_hash =
    ((struct see_ref_s *) (stn->value))->unmerged_def_se_hash;
  htab_t merged_def_se_hash =
    ((struct see_ref_s *) (stn->value))->merged_def_se_hash;
  rtx ref = ((struct see_ref_s *) (stn->value))->insn;
  rtx merged_ref = ((struct see_ref_s *) (stn->value))->merged_insn;

  /* Emit the non-redundant use extensions.  */
  if (use_se_hash)
    htab_traverse_noresize (use_se_hash, see_emit_use_extension,
			    (PTR) (stn->value));

  /* Delete the def extensions.  */
  if (unmerged_def_se_hash)
    htab_traverse (unmerged_def_se_hash, see_delete_unmerged_def_extension,
		   (PTR) (stn->value));

  if (merged_def_se_hash)
    htab_traverse (merged_def_se_hash, see_delete_merged_def_extension,
		   (PTR) (stn->value));

  /* Replace the original reference with the merged one (if exists) and add the
     move instructions that were generated.  */
  if (merged_ref && !INSN_DELETED_P (ref))
    {
      if (dump_file)
	{
	  fprintf (dump_file, "Replacing orig reference:\n");
	  print_rtl_single (dump_file, ref);
	  fprintf (dump_file, "With merged reference:\n");
	  print_rtl_single (dump_file, merged_ref);
	}
      emit_insn_after (merged_ref, ref);
      delete_insn (ref);
    }

  /* Continue to the next reference.  */
  return 0;
}


/* Insert partially redundant expressions on edges to make the expressions fully
   redundant.

   INDEX_MAP is a mapping of an index to an expression.
1666
   Return true if an instruction was inserted on an edge.
Razya Ladelsky committed
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
   Otherwise, return false.  */

static bool
see_pre_insert_extensions (struct see_pre_extension_expr **index_map)
{
  int num_edges = NUM_EDGES (edge_list);
  int set_size = pre_insert_map[0]->size;
  size_t pre_extension_num = htab_elements (see_pre_extension_hash);

  int did_insert = 0;
  int e;
  int i;
  int j;

  for (e = 0; e < num_edges; e++)
    {
      int indx;
      basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);

      for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
	{
	  SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];

	  for (j = indx; insert && j < (int) pre_extension_num;
	       j++, insert >>= 1)
	    if (insert & 1)
	      {
		struct see_pre_extension_expr *expr = index_map[j];
		int idx = expr->bitmap_index;
		rtx se_insn = NULL;
		edge eg = INDEX_EDGE (edge_list, e);

		start_sequence ();
1700
		emit_insn (copy_insn (PATTERN (expr->se_insn)));
Razya Ladelsky committed
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
		se_insn = get_insns ();
		end_sequence ();

		if (eg->flags & EDGE_ABNORMAL)
		  {
		    rtx new_insn = NULL;

		    new_insn = insert_insn_end_bb_new (se_insn, bb);
		    gcc_assert (new_insn && INSN_P (new_insn));

		    if (dump_file)
		      {
			fprintf (dump_file,
				 "PRE: end of bb %d, insn %d, ",
				 bb->index, INSN_UID (new_insn));
			fprintf (dump_file,
				 "inserting expression %d\n", idx);
		      }
		  }
		else
		  {
		    insert_insn_on_edge (se_insn, eg);

		    if (dump_file)
		      {
			fprintf (dump_file, "PRE: edge (%d,%d), ",
				 bb->index,
				 INDEX_EDGE_SUCC_BB (edge_list, e)->index);
			fprintf (dump_file, "inserting expression %d\n", idx);
		      }
		  }
		did_insert = true;
	      }
	}
    }
  return did_insert;
}


/* Since all the redundant extensions must be anticipatable, they must be a use
   extensions.  Mark them as deleted.  This will prevent them from been emitted
   in the first place.

   This is a subroutine of see_commit_changes called via htab_traverse.

   SLOT contains the current see_pre_extension_expr structure pointer.  */

static int
see_pre_delete_extension (void **slot, void *b ATTRIBUTE_UNUSED)
{
1751 1752
  struct see_pre_extension_expr *const expr =
    (struct see_pre_extension_expr *) *slot;
Razya Ladelsky committed
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
  struct see_occr *occr;
  int indx = expr->bitmap_index;

  for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
    {
      if (TEST_BIT (pre_delete_map[occr->block_num], indx))
	{
	  /* Mark as deleted.  */
	  INSN_DELETED_P (occr->insn) = 1;
	  if (dump_file)
	    {
	      fprintf (dump_file,"Redundant extension deleted:\n");
	      print_rtl_single (dump_file, occr->insn);
	    }
	}
    }
  return 1;
}


/* Create the index_map mapping of an index to an expression.

   This is a subroutine of see_commit_changes called via htab_traverse.

   SLOT contains the current see_pre_extension_expr structure pointer.
   B a pointer to see_pre_extension_expr structure pointer.  */

static int
see_map_extension (void **slot, void *b)
{
1783 1784 1785
  struct see_pre_extension_expr *const expr =
    (struct see_pre_extension_expr *) *slot;
  struct see_pre_extension_expr **const index_map =
Razya Ladelsky committed
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
    (struct see_pre_extension_expr **) b;

  index_map[expr->bitmap_index] = expr;

  return 1;
}


/* Phase 4 top level function.
   In this phase we finally change the instruction stream.
   Here we insert extensions at their best placements and delete the
   redundant ones according to the output of the LCM.  We also replace
   some of the instructions according to phase 2 merges results.  */

static void
see_commit_changes (void)
{
  struct see_pre_extension_expr **index_map;
  size_t pre_extension_num = htab_elements (see_pre_extension_hash);
  bool did_insert = false;
  int i;

1808
  index_map = XCNEWVEC (struct see_pre_extension_expr *, pre_extension_num);
Razya Ladelsky committed
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856

  if (dump_file)
    fprintf (dump_file,
      "* Phase 4: Commit changes to the insn stream.  *\n");

  /* Produce a mapping of all the pre_extensions.  */
  htab_traverse (see_pre_extension_hash, see_map_extension, (PTR) index_map);

  /* Delete redundant extension.  This will prevent them from been emitted in
     the first place.  */
  htab_traverse (see_pre_extension_hash, see_pre_delete_extension, NULL);

  /* Insert extensions on edges, according to the LCM result.  */
  did_insert = see_pre_insert_extensions (index_map);

  if (did_insert)
    commit_edge_insertions ();

  /* Commit the rest of the changes.  */
  for (i = 0; i < last_bb; i++)
    {
      if (see_bb_splay_ar[i])
	{
	  /* Traverse over all the references in the basic block in forward
	     order.  */
	  splay_tree_foreach (see_bb_splay_ar[i],
			      see_commit_ref_changes, NULL);
	}
    }

  free (index_map);
}


/* Phase 3 implementation: Eliminate globally redundant extensions.  */

/* Analyze the properties of a merged def extension for the LCM and record avail
   occurrences.

   This is a subroutine of see_analyze_ref_local_prop called
   via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_analyze_merged_def_local_prop (void **slot, void *b)
{
1857
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
1858 1859
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
  rtx ref = curr_ref_s->insn;
1860
  struct see_pre_extension_expr *extension_expr;
Razya Ladelsky committed
1861 1862
  int indx;
  int bb_num = BLOCK_NUM (ref);
1863 1864
  htab_t curr_bb_hash;
  struct see_register_properties *curr_prop, **slot_prop;
Razya Ladelsky committed
1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
  struct see_register_properties temp_prop;
  rtx dest_extension_reg = see_get_extension_reg (def_se, 1);
  struct see_occr *curr_occr = NULL;
  struct see_occr *tmp_occr = NULL;

  extension_expr = see_seek_pre_extension_expr (def_se, DEF_EXTENSION);
  /* The extension_expr must be found.  */
  gcc_assert (extension_expr);

  curr_bb_hash = see_bb_hash_ar[bb_num];
  gcc_assert (curr_bb_hash);
  temp_prop.regno = REGNO (dest_extension_reg);
  slot_prop =
    (struct see_register_properties **) htab_find_slot (curr_bb_hash,
							&temp_prop, INSERT);
  curr_prop = *slot_prop;
  gcc_assert (curr_prop);

  indx = extension_expr->bitmap_index;

  /* Reset the transparency bit.  */
  RESET_BIT (transp[bb_num], indx);
  /* Reset the killed bit.  */
  RESET_BIT (ae_kill[bb_num], indx);

1890
  if (curr_prop->first_se_after_last_def == DF_INSN_LUID (ref))
Razya Ladelsky committed
1891 1892 1893 1894
    {
      /* Set the available bit.  */
      SET_BIT (comp[bb_num], indx);
      /* Record the available occurrence.  */
1895
      curr_occr = XNEW (struct see_occr);
Razya Ladelsky committed
1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
      curr_occr->next = NULL;
      curr_occr->insn = def_se;
      curr_occr->block_num = bb_num;
      tmp_occr = extension_expr->avail_occr;
      if (!tmp_occr)
	extension_expr->avail_occr = curr_occr;
      else
	{
	  while (tmp_occr->next)
	    tmp_occr = tmp_occr->next;
	  tmp_occr->next = curr_occr;
	}
    }

  return 1;
}


/* Analyze the properties of a unmerged def extension for the LCM.

   This is a subroutine of see_analyze_ref_local_prop called
   via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_analyze_unmerged_def_local_prop (void **slot, void *b)
{
1925
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
1926 1927
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
  rtx ref = curr_ref_s->insn;
1928
  struct see_pre_extension_expr *extension_expr;
Razya Ladelsky committed
1929 1930
  int indx;
  int bb_num = BLOCK_NUM (ref);
1931 1932
  htab_t curr_bb_hash;
  struct see_register_properties *curr_prop, **slot_prop;
Razya Ladelsky committed
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
  struct see_register_properties temp_prop;
  rtx dest_extension_reg = see_get_extension_reg (def_se, 1);

  extension_expr = see_seek_pre_extension_expr (def_se, DEF_EXTENSION);
  /* The extension_expr must be found.  */
  gcc_assert (extension_expr);

  curr_bb_hash = see_bb_hash_ar[bb_num];
  gcc_assert (curr_bb_hash);
  temp_prop.regno = REGNO (dest_extension_reg);
  slot_prop =
    (struct see_register_properties **) htab_find_slot (curr_bb_hash,
							&temp_prop, INSERT);
  curr_prop = *slot_prop;
  gcc_assert (curr_prop);

  indx = extension_expr->bitmap_index;

  /* Reset the transparency bit.  */
  RESET_BIT (transp[bb_num], indx);
  /* Set the killed bit.  */
  SET_BIT (ae_kill[bb_num], indx);

  return 1;
}


1960
/* Analyze the properties of a use extension for the LCM and record any and
Razya Ladelsky committed
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
   avail occurrences.

   This is a subroutine of see_analyze_ref_local_prop called
   via htab_traverse.

   SLOT contains the current use extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_analyze_use_local_prop (void **slot, void *b)
{
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
1973
  rtx use_se = (rtx) *slot;
Razya Ladelsky committed
1974 1975
  rtx ref = curr_ref_s->insn;
  rtx dest_extension_reg = see_get_extension_reg (use_se, 1);
1976 1977
  struct see_pre_extension_expr *extension_expr;
  struct see_register_properties *curr_prop, **slot_prop;
Razya Ladelsky committed
1978 1979 1980
  struct see_register_properties temp_prop;
  struct see_occr *curr_occr = NULL;
  struct see_occr *tmp_occr = NULL;
1981
  htab_t curr_bb_hash;
Razya Ladelsky committed
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
  int indx;
  int bb_num = BLOCK_NUM (ref);

  extension_expr = see_seek_pre_extension_expr (use_se, USE_EXTENSION);
  /* The extension_expr must be found.  */
  gcc_assert (extension_expr);

  curr_bb_hash = see_bb_hash_ar[bb_num];
  gcc_assert (curr_bb_hash);
  temp_prop.regno = REGNO (dest_extension_reg);
  slot_prop =
    (struct see_register_properties **) htab_find_slot (curr_bb_hash,
							&temp_prop, INSERT);
  curr_prop = *slot_prop;
  gcc_assert (curr_prop);

  indx = extension_expr->bitmap_index;

2000
  if (curr_prop->first_se_before_any_def == DF_INSN_LUID (ref))
Razya Ladelsky committed
2001 2002 2003 2004
    {
      /* Set the anticipatable bit.  */
      SET_BIT (antloc[bb_num], indx);
      /* Record the anticipatable occurrence.  */
2005
      curr_occr = XNEW (struct see_occr);
Razya Ladelsky committed
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
      curr_occr->next = NULL;
      curr_occr->insn = use_se;
      curr_occr->block_num = bb_num;
      tmp_occr = extension_expr->antic_occr;
      if (!tmp_occr)
	extension_expr->antic_occr = curr_occr;
      else
	{
	  while (tmp_occr->next)
	    tmp_occr = tmp_occr->next;
	  tmp_occr->next = curr_occr;
	}
      if (curr_prop->last_def < 0)
	{
	  /* Set the available bit.  */
	  SET_BIT (comp[bb_num], indx);
	  /* Record the available occurrence.  */
2023
	  curr_occr = XNEW (struct see_occr);
Razya Ladelsky committed
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
	  curr_occr->next = NULL;
	  curr_occr->insn = use_se;
	  curr_occr->block_num = bb_num;
	  tmp_occr = extension_expr->avail_occr;
	  if (!tmp_occr)
	    extension_expr->avail_occr = curr_occr;
	  else
	    {
  	      while (tmp_occr->next)
  		tmp_occr = tmp_occr->next;
	      tmp_occr->next = curr_occr;
	    }
	}
      /* Note: there is no need to reset the killed bit since it must be zero at
	 this point.  */
    }
2040
  else if (curr_prop->first_se_after_last_def == DF_INSN_LUID (ref))
Razya Ladelsky committed
2041 2042 2043 2044 2045 2046
    {
      /* Set the available bit.  */
      SET_BIT (comp[bb_num], indx);
      /* Reset the killed bit.  */
      RESET_BIT (ae_kill[bb_num], indx);
      /* Record the available occurrence.  */
2047
      curr_occr = XNEW (struct see_occr);
Razya Ladelsky committed
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
      curr_occr->next = NULL;
      curr_occr->insn = use_se;
      curr_occr->block_num = bb_num;
      tmp_occr = extension_expr->avail_occr;
      if (!tmp_occr)
	extension_expr->avail_occr = curr_occr;
      else
	{
	  while (tmp_occr->next)
	    tmp_occr = tmp_occr->next;
	  tmp_occr->next = curr_occr;
	}
    }
  return 1;
}


/* Here we traverse over all the merged and unmerged extensions of the reference
   and analyze their properties for the LCM.

   This is a subroutine of see_execute_LCM called via splay_tree_foreach.

   STN is the current node in the see_bb_splay_ar[i] splay tree.  It holds a
   see_ref_s structure.  */

static int
see_analyze_ref_local_prop (splay_tree_node stn,
			    void *data ATTRIBUTE_UNUSED)
{
  htab_t use_se_hash = ((struct see_ref_s *) (stn->value))->use_se_hash;
  htab_t unmerged_def_se_hash =
    ((struct see_ref_s *) (stn->value))->unmerged_def_se_hash;
  htab_t merged_def_se_hash =
    ((struct see_ref_s *) (stn->value))->merged_def_se_hash;

  /* Analyze use extensions that were not merged with the reference.  */
  if (use_se_hash)
    htab_traverse_noresize (use_se_hash, see_analyze_use_local_prop,
			    (PTR) (stn->value));

  /* Analyze def extensions that were not merged with the reference.  */
  if (unmerged_def_se_hash)
    htab_traverse (unmerged_def_se_hash, see_analyze_unmerged_def_local_prop,
		   (PTR) (stn->value));

  /* Analyze def extensions that were merged with the reference.  */
  if (merged_def_se_hash)
    htab_traverse (merged_def_se_hash, see_analyze_merged_def_local_prop,
		   (PTR) (stn->value));

  /* Continue to the next definition.  */
  return 0;
}


/* Phase 3 top level function.
   In this phase, we set the input bit vectors of the LCM according to data
   gathered in phase 2.
   Then we run the edge based LCM.  */

static void
see_execute_LCM (void)
{
  size_t pre_extension_num = htab_elements (see_pre_extension_hash);
  int i = 0;

  if (dump_file)
    fprintf (dump_file,
      "* Phase 3: Eliminate globally redundant extensions.  *\n");

  /* Initialize the global sbitmap vectors.  */
  transp = sbitmap_vector_alloc (last_bb, pre_extension_num);
  comp = sbitmap_vector_alloc (last_bb, pre_extension_num);
  antloc = sbitmap_vector_alloc (last_bb, pre_extension_num);
  ae_kill = sbitmap_vector_alloc (last_bb, pre_extension_num);
  sbitmap_vector_ones (transp, last_bb);
  sbitmap_vector_zero (comp, last_bb);
  sbitmap_vector_zero (antloc, last_bb);
  sbitmap_vector_zero (ae_kill, last_bb);

  /* Traverse over all the splay trees of the basic blocks.  */
  for (i = 0; i < last_bb; i++)
    {
      if (see_bb_splay_ar[i])
	{
	  /* Traverse over all the references in the basic block in forward
	     order.  */
	  splay_tree_foreach (see_bb_splay_ar[i],
			      see_analyze_ref_local_prop, NULL);
	}
    }

  /* Add fake exit edges before running the lcm.  */
  add_noreturn_fake_exit_edges ();

  /* Run the LCM.  */
  edge_list = pre_edge_lcm (pre_extension_num, transp, comp, antloc,
  			    ae_kill, &pre_insert_map, &pre_delete_map);

  /* Remove the fake edges.  */
  remove_fake_exit_edges ();
}


/* Phase 2 implementation: Merge and eliminate locally redundant extensions.  */

/* In this function we set the register properties for the register that is
   defined and extended in the reference.
   The properties are defined in see_register_properties structure which is
Kazu Hirata committed
2157
   allocated per basic block and per register.
Razya Ladelsky committed
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169
   Later the extension is inserted into the see_pre_extension_hash for the next
   phase of the optimization.

   This is a subroutine of see_handle_extensions_for_one_ref called
   via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_set_prop_merged_def (void **slot, void *b)
{
2170
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
2171 2172 2173
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
  rtx insn = curr_ref_s->insn;
  rtx dest_extension_reg = see_get_extension_reg (def_se, 1);
2174
  htab_t curr_bb_hash;
Razya Ladelsky committed
2175
  struct see_register_properties *curr_prop = NULL;
2176
  struct see_register_properties **slot_prop;
Razya Ladelsky committed
2177
  struct see_register_properties temp_prop;
2178
  int ref_luid = DF_INSN_LUID (insn);
Razya Ladelsky committed
2179 2180 2181 2182 2183

  curr_bb_hash = see_bb_hash_ar[BLOCK_NUM (curr_ref_s->insn)];
  if (!curr_bb_hash)
    {
      /* The hash doesn't exist yet.  Create it.  */
2184 2185 2186 2187
      curr_bb_hash = htab_create (10, 
				  hash_descriptor_properties, 
				  eq_descriptor_properties,
				  hash_del_properties);
Razya Ladelsky committed
2188 2189 2190 2191 2192 2193 2194 2195 2196
      see_bb_hash_ar[BLOCK_NUM (curr_ref_s->insn)] = curr_bb_hash;
    }

  /* Find the right register properties in the right basic block.  */
  temp_prop.regno = REGNO (dest_extension_reg);
  slot_prop =
    (struct see_register_properties **) htab_find_slot (curr_bb_hash,
							&temp_prop, INSERT);

2197
  if (slot_prop && *slot_prop != NULL)
Razya Ladelsky committed
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
    {
      /* Property already exists.  */
      curr_prop = *slot_prop;
      gcc_assert (curr_prop->regno == REGNO (dest_extension_reg));

      curr_prop->last_def = ref_luid;
      curr_prop->first_se_after_last_def = ref_luid;
    }
  else
    {
      /* Property doesn't exist yet.  */
2209
      curr_prop = XNEW (struct see_register_properties);
Razya Ladelsky committed
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227
      curr_prop->regno = REGNO (dest_extension_reg);
      curr_prop->last_def = ref_luid;
      curr_prop->first_se_before_any_def = -1;
      curr_prop->first_se_after_last_def = ref_luid;
      *slot_prop = curr_prop;
    }

  /* Insert the def_se into see_pre_extension_hash if it isn't already
     there.  */
  see_seek_pre_extension_expr (def_se, DEF_EXTENSION);

  return 1;
}


/* In this function we set the register properties for the register that is
   defined but not extended in the reference.
   The properties are defined in see_register_properties structure which is
Kazu Hirata committed
2228
   allocated per basic block and per register.
Razya Ladelsky committed
2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
   Later the extension is inserted into the see_pre_extension_hash for the next
   phase of the optimization.

   This is a subroutine of see_handle_extensions_for_one_ref called
   via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_set_prop_unmerged_def (void **slot, void *b)
{
2241
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
2242 2243 2244
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
  rtx insn = curr_ref_s->insn;
  rtx dest_extension_reg = see_get_extension_reg (def_se, 1);
2245
  htab_t curr_bb_hash;
Razya Ladelsky committed
2246
  struct see_register_properties *curr_prop = NULL;
2247
  struct see_register_properties **slot_prop;
Razya Ladelsky committed
2248
  struct see_register_properties temp_prop;
2249
  int ref_luid = DF_INSN_LUID (insn);
Razya Ladelsky committed
2250 2251 2252 2253 2254

  curr_bb_hash = see_bb_hash_ar[BLOCK_NUM (curr_ref_s->insn)];
  if (!curr_bb_hash)
    {
      /* The hash doesn't exist yet.  Create it.  */
2255 2256 2257 2258
      curr_bb_hash = htab_create (10, 
				  hash_descriptor_properties, 
				  eq_descriptor_properties,
				  hash_del_properties);
Razya Ladelsky committed
2259 2260 2261 2262 2263 2264 2265 2266 2267
      see_bb_hash_ar[BLOCK_NUM (curr_ref_s->insn)] = curr_bb_hash;
    }

  /* Find the right register properties in the right basic block.  */
  temp_prop.regno = REGNO (dest_extension_reg);
  slot_prop =
    (struct see_register_properties **) htab_find_slot (curr_bb_hash,
							&temp_prop, INSERT);

2268
  if (slot_prop && *slot_prop != NULL)
Razya Ladelsky committed
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
    {
      /* Property already exists.  */
      curr_prop = *slot_prop;
      gcc_assert (curr_prop->regno == REGNO (dest_extension_reg));

      curr_prop->last_def = ref_luid;
      curr_prop->first_se_after_last_def = -1;
    }
  else
    {
      /* Property doesn't exist yet.  */
2280
      curr_prop = XNEW (struct see_register_properties);
Razya Ladelsky committed
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
      curr_prop->regno = REGNO (dest_extension_reg);
      curr_prop->last_def = ref_luid;
      curr_prop->first_se_before_any_def = -1;
      curr_prop->first_se_after_last_def = -1;
      *slot_prop = curr_prop;
    }

  /* Insert the def_se into see_pre_extension_hash if it isn't already
     there.  */
  see_seek_pre_extension_expr (def_se, DEF_EXTENSION);

  return 1;
}


/* In this function we set the register properties for the register that is used
   in the reference.
   The properties are defined in see_register_properties structure which is
Kazu Hirata committed
2299
   allocated per basic block and per register.
Razya Ladelsky committed
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
   When a redundant use extension is found it is removed from the hash of the
   reference.
   If the extension is non redundant it is inserted into the
   see_pre_extension_hash for the next phase of the optimization.

   This is a subroutine of see_handle_extensions_for_one_ref called
   via htab_traverse.

   SLOT contains the current use extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_set_prop_unmerged_use (void **slot, void *b)
{
2314
  rtx use_se = (rtx) *slot;
Razya Ladelsky committed
2315 2316 2317
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
  rtx insn = curr_ref_s->insn;
  rtx dest_extension_reg = see_get_extension_reg (use_se, 1);
2318
  htab_t curr_bb_hash;
Razya Ladelsky committed
2319
  struct see_register_properties *curr_prop = NULL;
2320
  struct see_register_properties **slot_prop;
Razya Ladelsky committed
2321 2322
  struct see_register_properties temp_prop;
  bool locally_redundant = false;
2323
  int ref_luid = DF_INSN_LUID (insn);
Razya Ladelsky committed
2324 2325 2326 2327 2328

  curr_bb_hash = see_bb_hash_ar[BLOCK_NUM (curr_ref_s->insn)];
  if (!curr_bb_hash)
    {
      /* The hash doesn't exist yet.  Create it.  */
2329 2330 2331 2332
      curr_bb_hash = htab_create (10, 
				  hash_descriptor_properties, 
				  eq_descriptor_properties,
				  hash_del_properties);
Razya Ladelsky committed
2333 2334 2335 2336 2337 2338 2339 2340 2341
      see_bb_hash_ar[BLOCK_NUM (curr_ref_s->insn)] = curr_bb_hash;
    }

  /* Find the right register properties in the right basic block.  */
  temp_prop.regno = REGNO (dest_extension_reg);
  slot_prop =
    (struct see_register_properties **) htab_find_slot (curr_bb_hash,
							&temp_prop, INSERT);

2342
  if (slot_prop && *slot_prop != NULL)
Razya Ladelsky committed
2343 2344 2345 2346 2347 2348
    {
      /* Property already exists.  */
      curr_prop = *slot_prop;
      gcc_assert (curr_prop->regno == REGNO (dest_extension_reg));


2349
      if (curr_prop->last_def < 0 && curr_prop->first_se_before_any_def < 0)
Razya Ladelsky committed
2350
	curr_prop->first_se_before_any_def = ref_luid;
2351 2352
      else if (curr_prop->last_def < 0
	       && curr_prop->first_se_before_any_def >= 0)
Razya Ladelsky committed
2353
	{
Kazu Hirata committed
2354
	  /* In this case the extension is locally redundant.  */
Razya Ladelsky committed
2355 2356 2357
	  htab_clear_slot (curr_ref_s->use_se_hash, (PTR *)slot);
	  locally_redundant = true;
	}
2358 2359
      else if (curr_prop->last_def >= 0
	       && curr_prop->first_se_after_last_def < 0)
Razya Ladelsky committed
2360
	curr_prop->first_se_after_last_def = ref_luid;
2361 2362
      else if (curr_prop->last_def >= 0
	       && curr_prop->first_se_after_last_def >= 0)
Razya Ladelsky committed
2363
	{
Kazu Hirata committed
2364
	  /* In this case the extension is locally redundant.  */
Razya Ladelsky committed
2365 2366 2367 2368 2369 2370 2371 2372 2373
	  htab_clear_slot (curr_ref_s->use_se_hash, (PTR *)slot);
	  locally_redundant = true;
	}
      else
	gcc_unreachable ();
    }
  else
    {
      /* Property doesn't exist yet.  Create a new one.  */
2374
      curr_prop = XNEW (struct see_register_properties);
Razya Ladelsky committed
2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
      curr_prop->regno = REGNO (dest_extension_reg);
      curr_prop->last_def = -1;
      curr_prop->first_se_before_any_def = ref_luid;
      curr_prop->first_se_after_last_def = -1;
      *slot_prop = curr_prop;
    }

  /* Insert the use_se into see_pre_extension_hash if it isn't already
     there.  */
  if (!locally_redundant)
    see_seek_pre_extension_expr (use_se, USE_EXTENSION);
  if (locally_redundant && dump_file)
    {
      fprintf (dump_file, "Locally redundant extension:\n");
      print_rtl_single (dump_file, use_se);
    }
  return 1;
}


/* Print an extension instruction.

   This is a subroutine of see_handle_extensions_for_one_ref called
   via htab_traverse.
   SLOT contains the extension instruction.  */

static int
see_print_one_extension (void **slot, void *b ATTRIBUTE_UNUSED)
{
2404
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426

  gcc_assert (def_se && INSN_P (def_se));
  print_rtl_single (dump_file, def_se);

  return 1;
}

/* Function called by note_uses to replace used subexpressions.

   X is a pointer to the subexpression and DATA is a pointer to a
   see_replace_data structure that contains the data for the replacement.  */

static void
see_replace_src (rtx *x, void *data)
{
  struct see_replace_data *d
    = (struct see_replace_data *) data;

  *x = replace_rtx (*x, d->from, d->to);
}


2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
static rtx
see_copy_insn (rtx insn)
{
  rtx pat = copy_insn (PATTERN (insn)), ret;

  if (NONJUMP_INSN_P (insn))
    ret = make_insn_raw (pat);
  else if (JUMP_P (insn))
    ret = make_jump_insn_raw (pat);
  else if (CALL_P (insn))
    {
      start_sequence ();
      ret = emit_call_insn (pat);
      end_sequence ();
      if (CALL_INSN_FUNCTION_USAGE (insn))
	CALL_INSN_FUNCTION_USAGE (ret)
	  = copy_rtx (CALL_INSN_FUNCTION_USAGE (insn));
      SIBLING_CALL_P (ret) = SIBLING_CALL_P (insn);
Kenneth Zadeck committed
2445 2446 2447 2448
      RTL_CONST_CALL_P (ret) = RTL_CONST_CALL_P (insn);
      RTL_PURE_CALL_P (ret) = RTL_PURE_CALL_P (insn);
      RTL_LOOPING_CONST_OR_PURE_CALL_P (ret) 
	= RTL_LOOPING_CONST_OR_PURE_CALL_P (insn);
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461
    }
  else
    gcc_unreachable ();
  if (REG_NOTES (insn))
    REG_NOTES (ret) = copy_rtx (REG_NOTES (insn));
  INSN_LOCATOR (ret) = INSN_LOCATOR (insn);
  RTX_FRAME_RELATED_P (ret) = RTX_FRAME_RELATED_P (insn);
  PREV_INSN (ret) = NULL_RTX;
  NEXT_INSN (ret) = NULL_RTX;
  return ret;
}


Razya Ladelsky committed
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
/* At this point the pattern is expected to be:

   ref:	    set (dest_reg) (rhs)
   def_se:  set (dest_extension_reg) (sign/zero_extend (source_extension_reg))

   The merge of these two instructions didn't succeed.

   We try to generate the pattern:
   set (subreg (dest_extension_reg)) (rhs)

   We do this in 4 steps:
2473
   a. Replace every use of dest_reg with a new pseudo register.
Razya Ladelsky committed
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
   b. Replace every instance of dest_reg with the subreg.
   c. Replace every use of the new pseudo register back to dest_reg.
   d. Try to recognize and simplify.

   If the manipulation failed, leave the original ref but try to generate and
   recognize a simple move instruction:
   set (subreg (dest_extension_reg)) (dest_reg)
   This move instruction will be emitted right after the ref to the instruction
   stream and assure the correctness of the code after def_se will be removed.

   CURR_REF_S is the current reference.
   DEF_SE is the extension that couldn't be merged.  */

static void
see_def_extension_not_merged (struct see_ref_s *curr_ref_s, rtx def_se)
{
  struct see_replace_data d;
  /* If the original insn was already merged with an extension before,
     take the merged one.  */
2493 2494 2495 2496 2497
  rtx ref = curr_ref_s->merged_insn
	    ? curr_ref_s->merged_insn : curr_ref_s->insn;
  rtx merged_ref_next = curr_ref_s->merged_insn
			? NEXT_INSN (curr_ref_s->merged_insn) : NULL_RTX;
  rtx ref_copy = see_copy_insn (ref);
Razya Ladelsky committed
2498 2499
  rtx source_extension_reg = see_get_extension_reg (def_se, 0);
  rtx dest_extension_reg = see_get_extension_reg (def_se, 1);
2500 2501 2502
  rtx set, rhs;
  rtx dest_reg, dest_real_reg;
  rtx new_pseudo_reg, subreg;
Razya Ladelsky committed
2503 2504 2505 2506 2507 2508
  enum machine_mode source_extension_mode = GET_MODE (source_extension_reg);
  enum machine_mode dest_mode;

  set = single_set (def_se);
  gcc_assert (set);
  rhs = SET_SRC (set);
2509 2510
  gcc_assert (GET_CODE (rhs) == SIGN_EXTEND
	      || GET_CODE (rhs) == ZERO_EXTEND);
Razya Ladelsky committed
2511 2512
  dest_reg = XEXP (rhs, 0);
  gcc_assert (REG_P (dest_reg)
2513
	      || (GET_CODE (dest_reg) == SUBREG
Razya Ladelsky committed
2514 2515 2516 2517 2518 2519 2520
		  && REG_P (SUBREG_REG (dest_reg))));
  dest_real_reg = REG_P (dest_reg) ? dest_reg : SUBREG_REG (dest_reg);
  dest_mode = GET_MODE (dest_reg);

  subreg = gen_lowpart_SUBREG (dest_mode, dest_extension_reg);
  new_pseudo_reg = gen_reg_rtx (source_extension_mode);

2521
  /* Step a: Replace every use of dest_real_reg with a new pseudo register.  */
Razya Ladelsky committed
2522 2523 2524 2525
  d.from = dest_real_reg;
  d.to = new_pseudo_reg;
  note_uses (&PATTERN (ref_copy), see_replace_src, &d);
  /* Step b: Replace every instance of dest_reg with the subreg.  */
2526
  ref_copy = replace_rtx (ref_copy, dest_reg, copy_rtx (subreg));
Razya Ladelsky committed
2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537

  /* Step c: Replace every use of the new pseudo register back to
     dest_real_reg.  */
  d.from = new_pseudo_reg;
  d.to = dest_real_reg;
  note_uses (&PATTERN (ref_copy), see_replace_src, &d);

  if (rtx_equal_p (PATTERN (ref), PATTERN (ref_copy))
      || insn_invalid_p (ref_copy))
    {
      /* The manipulation failed.  */
2538
      df_insn_delete (NULL, INSN_UID (ref_copy));
Razya Ladelsky committed
2539 2540

      /* Create a new copy.  */
2541 2542 2543 2544
      ref_copy = see_copy_insn (ref);

      if (curr_ref_s->merged_insn)
        df_insn_delete (NULL, INSN_UID (curr_ref_s->merged_insn));
Razya Ladelsky committed
2545 2546 2547

      /* Create a simple move instruction that will replace the def_se.  */
      start_sequence ();
2548
      emit_insn (ref_copy);
Razya Ladelsky committed
2549 2550
      emit_move_insn (subreg, dest_reg);
      if (merged_ref_next != NULL_RTX)
2551 2552 2553
	emit_insn (merged_ref_next);
      curr_ref_s->merged_insn = get_insns ();
      end_sequence ();
Razya Ladelsky committed
2554 2555 2556 2557 2558 2559 2560 2561

      if (dump_file)
	{
	  fprintf (dump_file, "Following def merge failure a move ");
	  fprintf (dump_file, "insn was added after the ref.\n");
	  fprintf (dump_file, "Original ref:\n");
	  print_rtl_single (dump_file, ref);
	  fprintf (dump_file, "Move insn that was added:\n");
2562
	  print_rtl_single (dump_file, NEXT_INSN (curr_ref_s->merged_insn));
Razya Ladelsky committed
2563 2564 2565 2566 2567 2568
	}
      return;
    }

  /* The manipulation succeeded.  Store the new manipulated reference.  */

2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
  /* It is possible for dest_reg to appear multiple times in ref_copy. In this
     case, ref_copy now has invalid sharing. Copying solves the problem.
     We don't use copy_rtx as an optimization for the common case (no sharing).
     We can't just use copy_rtx_if_shared since it does nothing on INSNs.
     Another possible solution would be to make validate_replace_rtx_1
     public and use it instead of replace_rtx. */
  reset_used_flags (PATTERN (ref_copy));
  reset_used_flags (REG_NOTES (ref_copy));
  PATTERN (ref_copy) = copy_rtx_if_shared (PATTERN (ref_copy));
  REG_NOTES (ref_copy) = copy_rtx_if_shared (REG_NOTES (ref_copy));

Razya Ladelsky committed
2580 2581 2582
  /* Try to simplify the new manipulated insn.  */
  validate_simplify_insn (ref_copy);

2583 2584 2585
  if (curr_ref_s->merged_insn)
    df_insn_delete (NULL, INSN_UID (curr_ref_s->merged_insn));

Razya Ladelsky committed
2586 2587
  /* Create a simple move instruction to assure the correctness of the code.  */
  start_sequence ();
2588
  emit_insn (ref_copy);
Razya Ladelsky committed
2589 2590
  emit_move_insn (dest_reg, subreg);
  if (merged_ref_next != NULL_RTX)
2591 2592 2593
    emit_insn (merged_ref_next);
  curr_ref_s->merged_insn = get_insns ();
  end_sequence ();
Razya Ladelsky committed
2594 2595 2596 2597 2598 2599 2600

  if (dump_file)
    {
      fprintf (dump_file, "Following merge failure the ref was transformed!\n");
      fprintf (dump_file, "Original ref:\n");
      print_rtl_single (dump_file, ref);
      fprintf (dump_file, "Transformed ref:\n");
2601
      print_rtl_single (dump_file, curr_ref_s->merged_insn);
Razya Ladelsky committed
2602
      fprintf (dump_file, "Move insn that was added:\n");
2603
      print_rtl_single (dump_file, NEXT_INSN (curr_ref_s->merged_insn));
Razya Ladelsky committed
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
    }
}


/* Merge the reference instruction (ref) with the current use extension.

   use_se extends a NARROWmode register to a WIDEmode register.
   ref uses the WIDEmode register.

   The pattern we try to merge is this:
   use_se: set (dest_extension_reg) (sign/zero_extend (source_extension_reg))
   ref:	   use (dest_extension_reg)

   where dest_extension_reg and source_extension_reg can be subregs.

   The merge is done by generating, simplifying and recognizing the pattern:
   use (sign/zero_extend (source_extension_reg))

   If ref is too simple (according to see_want_to_be_merged_with_extension ())
   we don't try to merge it with use_se and we continue as if the merge failed.

   This is a subroutine of see_handle_extensions_for_one_ref called
   via htab_traverse.
   SLOT contains the current use extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_merge_one_use_extension (void **slot, void *b)
{
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
2634
  rtx use_se = (rtx) *slot;
2635 2636 2637 2638 2639
  rtx ref = curr_ref_s->merged_insn
	    ? curr_ref_s->merged_insn : curr_ref_s->insn;
  rtx merged_ref_next = curr_ref_s->merged_insn
			? NEXT_INSN (curr_ref_s->merged_insn) : NULL_RTX;
  rtx ref_copy = see_copy_insn (ref);
Razya Ladelsky committed
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664
  rtx extension_set = single_set (use_se);
  rtx extension_rhs = NULL;
  rtx dest_extension_reg = see_get_extension_reg (use_se, 1);
  rtx note = NULL;
  rtx simplified_note = NULL;

  gcc_assert (use_se && curr_ref_s && extension_set);

  extension_rhs = SET_SRC (extension_set);

  /* In REG_EQUIV and REG_EQUAL notes that mention the register we need to
     replace the uses of the dest_extension_reg with the rhs of the extension
     instruction.  This is necessary since there might not be an extension in
     the path between the definition and the note when this optimization is
     over.  */
  note = find_reg_equal_equiv_note (ref_copy);
  if (note)
    {
      simplified_note = simplify_replace_rtx (XEXP (note, 0),
      					      dest_extension_reg,
					      extension_rhs);
      if (rtx_equal_p (XEXP (note, 0), simplified_note))
	/* Replacement failed.  Remove the note.  */
	remove_note (ref_copy, note);
      else
2665 2666
	set_unique_reg_note (ref_copy, REG_NOTE_KIND (note),
			     simplified_note);
Razya Ladelsky committed
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678
    }

  if (!see_want_to_be_merged_with_extension (ref, use_se, USE_EXTENSION))
    {
      /* The use in the reference is too simple.  Don't try to merge.  */
      if (dump_file)
	{
	  fprintf (dump_file, "Use merge skipped!\n");
	  fprintf (dump_file, "Original instructions:\n");
	  print_rtl_single (dump_file, use_se);
	  print_rtl_single (dump_file, ref);
	}
2679
      df_insn_delete (NULL, INSN_UID (ref_copy));
Razya Ladelsky committed
2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
      /* Don't remove the current use_se from the use_se_hash and continue to
	 the next extension.  */
      return 1;
    }

  validate_replace_src_group (dest_extension_reg, extension_rhs, ref_copy);

  if (!num_changes_pending ())
    /* In this case this is not a real use (the only use is/was in the notes
       list).  Remove the use extension from the hash.  This will prevent it
       from been emitted in the first place.  */
    {
      if (dump_file)
	{
	  fprintf (dump_file, "Use extension not necessary before:\n");
	  print_rtl_single (dump_file, ref);
	}
      htab_clear_slot (curr_ref_s->use_se_hash, (PTR *)slot);
2698 2699 2700 2701

      if (curr_ref_s->merged_insn)
	df_insn_delete (NULL, INSN_UID (curr_ref_s->merged_insn));

Razya Ladelsky committed
2702
      if (merged_ref_next != NULL_RTX)
2703 2704 2705 2706 2707 2708 2709 2710 2711
	{
	  start_sequence ();
	  emit_insn (ref_copy);
	  emit_insn (merged_ref_next);
	  curr_ref_s->merged_insn = get_insns ();
	  end_sequence ();
	}
      else
	curr_ref_s->merged_insn = ref_copy;
Razya Ladelsky committed
2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
      return 1;
    }

  if (!apply_change_group ())
    {
      /* The merge failed.  */
      if (dump_file)
	{
	  fprintf (dump_file, "Use merge failed!\n");
	  fprintf (dump_file, "Original instructions:\n");
	  print_rtl_single (dump_file, use_se);
	  print_rtl_single (dump_file, ref);
	}
2725
      df_insn_delete (NULL, INSN_UID (ref_copy));
Razya Ladelsky committed
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
      /* Don't remove the current use_se from the use_se_hash and continue to
	 the next extension.  */
      return 1;
    }

  /* The merge succeeded!  */

  /* Try to simplify the new merged insn.  */
  validate_simplify_insn (ref_copy);

2736 2737 2738
  if (curr_ref_s->merged_insn)
    df_insn_delete (NULL, INSN_UID (curr_ref_s->merged_insn));

Razya Ladelsky committed
2739
  if (merged_ref_next != NULL_RTX)
2740 2741 2742 2743 2744 2745 2746 2747 2748
    {
      start_sequence ();
      emit_insn (ref_copy);
      emit_insn (merged_ref_next);
      curr_ref_s->merged_insn = get_insns ();
      end_sequence ();
    }
  else
    curr_ref_s->merged_insn = ref_copy;
Razya Ladelsky committed
2749 2750 2751 2752 2753 2754 2755 2756

  if (dump_file)
    {
      fprintf (dump_file, "Use merge succeeded!\n");
      fprintf (dump_file, "Original instructions:\n");
      print_rtl_single (dump_file, use_se);
      print_rtl_single (dump_file, ref);
      fprintf (dump_file, "Merged instruction:\n");
2757
      print_rtl_single (dump_file, curr_ref_s->merged_insn);
Razya Ladelsky committed
2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
    }

  /* Remove the current use_se from the use_se_hash.  This will prevent it from
     been emitted in the first place.  */
  htab_clear_slot (curr_ref_s->use_se_hash, (PTR *)slot);
  return 1;
}


/* Merge the reference instruction (ref) with the extension that follows it
   in the same basic block (def_se).
   ref sets a NARROWmode register and def_se extends it to WIDEmode register.

   The pattern we try to merge is this:
   ref:	   set (dest_reg) (rhs)
   def_se: set (dest_extension_reg) (sign/zero_extend (source_extension_reg))

Kazu Hirata committed
2775
   where dest_reg and source_extension_reg can both be subregs (together)
Razya Ladelsky committed
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
   and (REGNO (dest_reg) == REGNO (source_extension_reg))

   The merge is done by generating, simplifying and recognizing the pattern:
   set (dest_extension_reg) (sign/zero_extend (rhs))
   If ref is a parallel instruction we just replace the relevant set in it.

   If ref is too simple (according to see_want_to_be_merged_with_extension ())
   we don't try to merge it with def_se and we continue as if the merge failed.

   This is a subroutine of see_handle_extensions_for_one_ref called
   via htab_traverse.

   SLOT contains the current def extension instruction.
   B is the see_ref_s structure pointer.  */

static int
see_merge_one_def_extension (void **slot, void *b)
{
  struct see_ref_s *curr_ref_s = (struct see_ref_s *) b;
2795
  rtx def_se = (rtx) *slot;
Razya Ladelsky committed
2796 2797
  /* If the original insn was already merged with an extension before,
     take the merged one.  */
2798 2799 2800 2801 2802
  rtx ref = curr_ref_s->merged_insn
	    ? curr_ref_s->merged_insn : curr_ref_s->insn;
  rtx merged_ref_next = curr_ref_s->merged_insn
			? NEXT_INSN (curr_ref_s->merged_insn) : NULL_RTX;
  rtx ref_copy = see_copy_insn (ref);
Razya Ladelsky committed
2803 2804 2805
  rtx new_set = NULL;
  rtx source_extension_reg = see_get_extension_reg (def_se, 0);
  rtx dest_extension_reg = see_get_extension_reg (def_se, 1);
2806
  rtx *rtx_slot, subreg;
Razya Ladelsky committed
2807 2808
  rtx temp_extension = NULL;
  rtx simplified_temp_extension = NULL;
2809
  rtx *pat;
Razya Ladelsky committed
2810
  enum rtx_code code;
2811
  enum entry_type extension_code;
Razya Ladelsky committed
2812
  enum machine_mode source_extension_mode;
2813
  enum machine_mode source_mode = VOIDmode;
Razya Ladelsky committed
2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834
  enum machine_mode dest_extension_mode;
  bool merge_success = false;
  int i;

  gcc_assert (def_se
  	      && INSN_P (def_se)
	      && curr_ref_s
	      && ref
	      && INSN_P (ref));

  if (!see_want_to_be_merged_with_extension (ref, def_se, DEF_EXTENSION))
    {
      /* The definition in the reference is too simple.  Don't try to merge.  */
      if (dump_file)
	{
	  fprintf (dump_file, "Def merge skipped!\n");
	  fprintf (dump_file, "Original instructions:\n");
	  print_rtl_single (dump_file, ref);
	  print_rtl_single (dump_file, def_se);
	}

2835
      df_insn_delete (NULL, INSN_UID (ref_copy));
Razya Ladelsky committed
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
      see_def_extension_not_merged (curr_ref_s, def_se);
      /* Continue to the next extension.  */
      return 1;
    }

  extension_code = see_get_extension_data (def_se, &source_mode);

  /* Try to merge and simplify the extension.  */
  source_extension_mode = GET_MODE (source_extension_reg);
  dest_extension_mode = GET_MODE (dest_extension_reg);

  pat = &PATTERN (ref_copy);
  code = GET_CODE (*pat);

  if (code == PARALLEL)
    {
      bool need_to_apply_change = false;

      for (i = 0; i < XVECLEN (*pat, 0); i++)
	{
	  rtx *sub = &XVECEXP (*pat, 0, i);

2858 2859 2860
	  if (GET_CODE (*sub) == SET
	      && GET_MODE (SET_SRC (*sub)) != VOIDmode
	      && GET_MODE (SET_DEST (*sub)) == source_mode
Razya Ladelsky committed
2861
	      && ((REG_P (SET_DEST (*sub))
2862 2863 2864 2865 2866
		   && REGNO (SET_DEST (*sub)) == REGNO (source_extension_reg))
		  || (GET_CODE (SET_DEST (*sub)) == SUBREG
		      && REG_P (SUBREG_REG (SET_DEST (*sub)))
		      && (REGNO (SUBREG_REG (SET_DEST (*sub))) ==
			  REGNO (source_extension_reg)))))
Razya Ladelsky committed
2867 2868 2869
	    {
	      rtx orig_src = SET_SRC (*sub);

2870
	      if (extension_code == SIGN_EXTENDED_DEF)
Razya Ladelsky committed
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889
		temp_extension = gen_rtx_SIGN_EXTEND (dest_extension_mode,
						      orig_src);
	      else
		temp_extension = gen_rtx_ZERO_EXTEND (dest_extension_mode,
						      orig_src);
	      simplified_temp_extension = simplify_rtx (temp_extension);
	      temp_extension =
		(simplified_temp_extension) ? simplified_temp_extension :
					      temp_extension;
	      new_set = gen_rtx_SET (VOIDmode, dest_extension_reg,
				     temp_extension);
	      validate_change (ref_copy, sub, new_set, 1);
	      need_to_apply_change = true;
	    }
	}
      if (need_to_apply_change)
	if (apply_change_group ())
	  merge_success = true;
    }
2890 2891 2892
  else if (code == SET
	   && GET_MODE (SET_SRC (*pat)) != VOIDmode
	   && GET_MODE (SET_DEST (*pat)) == source_mode
Razya Ladelsky committed
2893 2894
	   && ((REG_P (SET_DEST (*pat))
		&& REGNO (SET_DEST (*pat)) == REGNO (source_extension_reg))
2895 2896
	       || (GET_CODE (SET_DEST (*pat)) == SUBREG
		   && REG_P (SUBREG_REG (SET_DEST (*pat)))
Razya Ladelsky committed
2897 2898 2899 2900 2901
		   && (REGNO (SUBREG_REG (SET_DEST (*pat))) ==
		       REGNO (source_extension_reg)))))
    {
      rtx orig_src = SET_SRC (*pat);

2902
      if (extension_code == SIGN_EXTENDED_DEF)
Razya Ladelsky committed
2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
	temp_extension = gen_rtx_SIGN_EXTEND (dest_extension_mode, orig_src);
      else
	temp_extension = gen_rtx_ZERO_EXTEND (dest_extension_mode, orig_src);
      simplified_temp_extension = simplify_rtx (temp_extension);
      temp_extension = (simplified_temp_extension) ? simplified_temp_extension :
						     temp_extension;
      new_set = gen_rtx_SET (VOIDmode, dest_extension_reg, temp_extension);
      if (validate_change (ref_copy, pat, new_set, 0))
	merge_success = true;
    }
  if (!merge_success)
    {
      /* The merge failed.  */
      if (dump_file)
	{
	  fprintf (dump_file, "Def merge failed!\n");
	  fprintf (dump_file, "Original instructions:\n");
	  print_rtl_single (dump_file, ref);
	  print_rtl_single (dump_file, def_se);
	}

2924
      df_insn_delete (NULL, INSN_UID (ref_copy));
Razya Ladelsky committed
2925 2926 2927 2928 2929 2930
      see_def_extension_not_merged (curr_ref_s, def_se);
      /* Continue to the next extension.  */
      return 1;
    }

  /* The merge succeeded!  */
2931 2932
  if (curr_ref_s->merged_insn)
    df_insn_delete (NULL, INSN_UID (curr_ref_s->merged_insn));
Razya Ladelsky committed
2933 2934 2935 2936

  /* Create a simple move instruction to assure the correctness of the code.  */
  subreg = gen_lowpart_SUBREG (source_extension_mode, dest_extension_reg);
  start_sequence ();
2937
  emit_insn (ref_copy);
Razya Ladelsky committed
2938 2939
  emit_move_insn (source_extension_reg, subreg);
  if (merged_ref_next != NULL_RTX)
2940 2941 2942
    emit_insn (merged_ref_next);
  curr_ref_s->merged_insn = get_insns ();
  end_sequence ();
Razya Ladelsky committed
2943 2944 2945 2946 2947 2948 2949 2950

  if (dump_file)
    {
      fprintf (dump_file, "Def merge succeeded!\n");
      fprintf (dump_file, "Original instructions:\n");
      print_rtl_single (dump_file, ref);
      print_rtl_single (dump_file, def_se);
      fprintf (dump_file, "Merged instruction:\n");
2951
      print_rtl_single (dump_file, curr_ref_s->merged_insn);
Razya Ladelsky committed
2952
      fprintf (dump_file, "Move instruction that was added:\n");
2953
      print_rtl_single (dump_file, NEXT_INSN (curr_ref_s->merged_insn));
Razya Ladelsky committed
2954 2955 2956 2957 2958 2959
    }

  /* Remove the current def_se from the unmerged_def_se_hash and insert it to
     the merged_def_se_hash.  */
  htab_clear_slot (curr_ref_s->unmerged_def_se_hash, (PTR *)slot);
  if (!curr_ref_s->merged_def_se_hash)
2960 2961 2962 2963
    curr_ref_s->merged_def_se_hash = htab_create (10, 
						  hash_descriptor_extension, 
						  eq_descriptor_extension,
						  NULL);
Razya Ladelsky committed
2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996
  rtx_slot = (rtx *) htab_find_slot (curr_ref_s->merged_def_se_hash,
  				     dest_extension_reg, INSERT);
  gcc_assert (*rtx_slot == NULL);
  *rtx_slot = def_se;

  return 1;
}


/* Try to eliminate extensions in this order:
   a. Try to merge only the def extensions, one by one.
   b. Try to merge only the use extensions, one by one.

   TODO:
   Try to merge any couple of use extensions simultaneously.
   Try to merge any def extension with one or two uses extensions
   simultaneously.

   After all the merges are done, update the register properties for the basic
   block and eliminate locally redundant use extensions.

   This is a subroutine of see_merge_and_eliminate_extensions called
   via splay_tree_foreach.
   STN is the current node in the see_bb_splay_ar[i] splay tree.  It holds a
   see_ref_s structure.  */

static int
see_handle_extensions_for_one_ref (splay_tree_node stn,
				   void *data ATTRIBUTE_UNUSED)
{
  htab_t use_se_hash = ((struct see_ref_s *) (stn->value))->use_se_hash;
  htab_t unmerged_def_se_hash =
    ((struct see_ref_s *) (stn->value))->unmerged_def_se_hash;
2997
  htab_t merged_def_se_hash;
Razya Ladelsky committed
2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101
  rtx ref = ((struct see_ref_s *) (stn->value))->insn;

  if (dump_file)
    {
      fprintf (dump_file, "Handling ref:\n");
      print_rtl_single (dump_file, ref);
    }

  /* a. Try to eliminate only def extensions, one by one.  */
  if (unmerged_def_se_hash)
    htab_traverse_noresize (unmerged_def_se_hash, see_merge_one_def_extension,
    			    (PTR) (stn->value));

  if (use_se_hash)
    /* b. Try to eliminate only use extensions, one by one.  */
    htab_traverse_noresize (use_se_hash, see_merge_one_use_extension,
			    (PTR) (stn->value));

  merged_def_se_hash = ((struct see_ref_s *) (stn->value))->merged_def_se_hash;

  if (dump_file)
    {
      fprintf (dump_file, "The hashes of the current reference:\n");
      if (unmerged_def_se_hash)
	{
	  fprintf (dump_file, "unmerged_def_se_hash:\n");
	  htab_traverse (unmerged_def_se_hash, see_print_one_extension, NULL);
	}
      if (merged_def_se_hash)
	{
	  fprintf (dump_file, "merged_def_se_hash:\n");
	  htab_traverse (merged_def_se_hash, see_print_one_extension, NULL);
	}
      if (use_se_hash)
	{
	  fprintf (dump_file, "use_se_hash:\n");
	  htab_traverse (use_se_hash, see_print_one_extension, NULL);
	}
    }

  /* Now that all the merges are done, update the register properties of the
     basic block and eliminate locally redundant extensions.
     It is important that we first traverse the use extensions hash and
     afterwards the def extensions hashes.  */

  if (use_se_hash)
    htab_traverse_noresize (use_se_hash, see_set_prop_unmerged_use,
			    (PTR) (stn->value));

  if (unmerged_def_se_hash)
    htab_traverse (unmerged_def_se_hash, see_set_prop_unmerged_def,
		   (PTR) (stn->value));

  if (merged_def_se_hash)
    htab_traverse (merged_def_se_hash, see_set_prop_merged_def,
		   (PTR) (stn->value));

  /* Continue to the next definition.  */
  return 0;
}


/* Phase 2 top level function.
   In this phase, we try to merge def extensions and use extensions with their
   references, and eliminate redundant extensions in the same basic block.  
   We also gather information for the next phases.  */

static void
see_merge_and_eliminate_extensions (void)
{
  int i = 0;

  if (dump_file)
    fprintf (dump_file,
      "* Phase 2: Merge and eliminate locally redundant extensions.  *\n");

  /* Traverse over all the splay trees of the basic blocks.  */
  for (i = 0; i < last_bb; i++)
    {
      if (see_bb_splay_ar[i])
	{
	  if (dump_file)
	    fprintf (dump_file, "Handling references for bb %d\n", i);
	  /* Traverse over all the references in the basic block in forward
	     order.  */
	  splay_tree_foreach (see_bb_splay_ar[i],
			      see_handle_extensions_for_one_ref, NULL);
	}
    }
}


/* Phase 1 implementation: Propagate extensions to uses.  */

/* Insert REF_INSN into the splay tree of its basic block.
   SE_INSN is the extension to store in the proper hash according to TYPE.

   Return true if everything went well.
   Otherwise, return false (this will cause the optimization to be aborted).  */

static bool
see_store_reference_and_extension (rtx ref_insn, rtx se_insn,
				   enum extension_type type)
{
3102
  rtx *rtx_slot;
Razya Ladelsky committed
3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
  int curr_bb_num;
  splay_tree_node stn = NULL;
  htab_t se_hash = NULL;
  struct see_ref_s *ref_s = NULL;

  /* Check the arguments.  */
  gcc_assert (ref_insn && se_insn);
  if (!see_bb_splay_ar)
    return false;

  curr_bb_num = BLOCK_NUM (ref_insn);
3114
  gcc_assert (curr_bb_num < last_bb && curr_bb_num >= 0);
Razya Ladelsky committed
3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125

  /* Insert the reference to the splay tree of its basic block.  */
  if (!see_bb_splay_ar[curr_bb_num])
    /* The splay tree for this block doesn't exist yet, create it.  */
    see_bb_splay_ar[curr_bb_num] = splay_tree_new (splay_tree_compare_ints,
						    NULL, see_free_ref_s);
  else
    /* Splay tree already exists, check if the current reference is already
       in it.  */
    {
      stn = splay_tree_lookup (see_bb_splay_ar[curr_bb_num],
3126
			       DF_INSN_LUID (ref_insn));
Razya Ladelsky committed
3127
      if (stn)
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
	switch (type)
	  {
	  case EXPLICIT_DEF_EXTENSION:
	    se_hash =
	      ((struct see_ref_s *) (stn->value))->unmerged_def_se_hash;
	    if (!se_hash)
	      {
		se_hash = htab_create (10, 
				       hash_descriptor_extension,
				       eq_descriptor_extension, 
				       NULL);
		((struct see_ref_s *) (stn->value))->unmerged_def_se_hash =
		  se_hash;
	      }
	    break;
	  case IMPLICIT_DEF_EXTENSION:
	    se_hash = ((struct see_ref_s *) (stn->value))->merged_def_se_hash;
	    if (!se_hash)
	      {
		se_hash = htab_create (10, 
				       hash_descriptor_extension,
				       eq_descriptor_extension, 
				       NULL);
		((struct see_ref_s *) (stn->value))->merged_def_se_hash =
		  se_hash;
	      }
	    break;
	  case USE_EXTENSION:
	    se_hash = ((struct see_ref_s *) (stn->value))->use_se_hash;
	    if (!se_hash)
	      {
		se_hash = htab_create (10, 
				       hash_descriptor_extension,
				       eq_descriptor_extension, 
				       NULL);
		((struct see_ref_s *) (stn->value))->use_se_hash = se_hash;
	      }
	    break;
	  default:
	    gcc_unreachable ();
	  }
Razya Ladelsky committed
3169 3170 3171 3172 3173 3174
    }

  /* Initialize a new see_ref_s structure and insert it to the splay
     tree.  */
  if (!stn)
    {
3175
      ref_s = XNEW (struct see_ref_s);
3176
      ref_s->luid = DF_INSN_LUID (ref_insn);
Razya Ladelsky committed
3177 3178 3179 3180 3181 3182 3183
      ref_s->insn = ref_insn;
      ref_s->merged_insn = NULL;

      /* Initialize the hashes.  */
      switch (type)
	{
	case EXPLICIT_DEF_EXTENSION:
3184 3185 3186 3187
	  ref_s->unmerged_def_se_hash = htab_create (10, 
						     hash_descriptor_extension, 
						     eq_descriptor_extension,
						     NULL);
Razya Ladelsky committed
3188 3189 3190 3191 3192
	  se_hash = ref_s->unmerged_def_se_hash;
	  ref_s->merged_def_se_hash = NULL;
	  ref_s->use_se_hash = NULL;
	  break;
	case IMPLICIT_DEF_EXTENSION:
3193 3194 3195 3196
	  ref_s->merged_def_se_hash = htab_create (10, 
						   hash_descriptor_extension, 
						   eq_descriptor_extension,
						   NULL);
Razya Ladelsky committed
3197 3198 3199 3200 3201
	  se_hash = ref_s->merged_def_se_hash;
	  ref_s->unmerged_def_se_hash = NULL;
	  ref_s->use_se_hash = NULL;
	  break;
	case USE_EXTENSION:
3202 3203 3204 3205
	  ref_s->use_se_hash = htab_create (10, 
					    hash_descriptor_extension, 
					    eq_descriptor_extension,
					    NULL);
Razya Ladelsky committed
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228
	  se_hash = ref_s->use_se_hash;
	  ref_s->unmerged_def_se_hash = NULL;
	  ref_s->merged_def_se_hash = NULL;
	  break;
	default:
	  gcc_unreachable ();
	}
    }

  /* Insert the new extension instruction into the correct se_hash of the
     current reference.  */
  rtx_slot = (rtx *) htab_find_slot (se_hash, se_insn, INSERT);
  if (*rtx_slot != NULL)
    {
      gcc_assert (type == USE_EXTENSION);
      gcc_assert (rtx_equal_p (PATTERN (*rtx_slot), PATTERN (se_insn)));
    }
  else
    *rtx_slot = se_insn;

  /* If this is a new reference, insert it into the splay_tree.  */
  if (!stn)
    splay_tree_insert (see_bb_splay_ar[curr_bb_num],
3229
		       DF_INSN_LUID (ref_insn), (splay_tree_value) ref_s);
Razya Ladelsky committed
3230 3231 3232 3233 3234 3235 3236 3237 3238
  return true;
}


/* Go over all the defs, for each relevant definition (defined below) store its
   instruction as a reference.

   A definition is relevant if its root has
   ((entry_type == SIGN_EXTENDED_DEF) || (entry_type == ZERO_EXTENDED_DEF)) and
3239
   his source_mode is not narrower then the roots source_mode.
Razya Ladelsky committed
3240 3241 3242 3243 3244

   Return the number of relevant defs or negative number if something bad had
   happened and the optimization should be aborted.  */

static int
3245
see_handle_relevant_defs (df_ref ref, rtx insn)
Razya Ladelsky committed
3246 3247
{
  struct web_entry *root_entry = NULL;
3248
  rtx se_insn = NULL;
3249
  enum entry_type extension_code;
3250 3251 3252
  rtx reg = DF_REF_REAL_REG (ref);
  rtx ref_insn = NULL;
  unsigned int i = DF_REF_ID (ref);
Razya Ladelsky committed
3253

3254
  root_entry = unionfind_root (&def_entry[DF_REF_ID (ref)]);
Razya Ladelsky committed
3255

3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
  if (ENTRY_EI (root_entry)->relevancy != SIGN_EXTENDED_DEF
      && ENTRY_EI (root_entry)->relevancy != ZERO_EXTENDED_DEF)
    /* The current web is not relevant.  Continue to the next def.  */
    return 0;
  
  if (root_entry->reg)
    /* It isn't possible to have two different register for the same
       web.  */
    gcc_assert (rtx_equal_p (root_entry->reg, reg));
  else
    root_entry->reg = reg;
  
  /* The current definition is an EXTENDED_DEF or a definition that its
     source_mode is narrower then its web's source_mode.
     This means that we need to generate the implicit extension explicitly
     and store it in the current reference's merged_def_se_hash.  */
  if (ENTRY_EI (&def_entry[i])->local_relevancy == EXTENDED_DEF
      || (ENTRY_EI (&def_entry[i])->local_source_mode <
	  ENTRY_EI (root_entry)->source_mode))
    {
      
      if (ENTRY_EI (root_entry)->relevancy == SIGN_EXTENDED_DEF)
3278
	extension_code = SIGN_EXTENDED_DEF;
Razya Ladelsky committed
3279
      else
3280
	extension_code = ZERO_EXTENDED_DEF;
3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
      
      se_insn =
	see_gen_normalized_extension (reg, extension_code,
				      ENTRY_EI (root_entry)->source_mode);
      
      /* This is a dummy extension, mark it as deleted.  */
      INSN_DELETED_P (se_insn) = 1;
      
      if (!see_store_reference_and_extension (insn, se_insn,
					      IMPLICIT_DEF_EXTENSION))
Razya Ladelsky committed
3291 3292
	/* Something bad happened.  Abort the optimization.  */
	return -1;
3293
      return 1;
Razya Ladelsky committed
3294
    }
3295 3296 3297 3298 3299 3300 3301 3302
  
  ref_insn = PREV_INSN (insn);
  gcc_assert (BLOCK_NUM (ref_insn) == BLOCK_NUM (insn));
  
  if (!see_store_reference_and_extension (ref_insn, insn,
					  EXPLICIT_DEF_EXTENSION))
    /* Something bad happened.  Abort the optimization.  */
    return -1;
Razya Ladelsky committed
3303

3304 3305
  return 0;
}
Razya Ladelsky committed
3306 3307 3308 3309 3310 3311 3312 3313

/* Go over all the uses, for each use in relevant web store its instruction as
   a reference and generate an extension before it.

   Return the number of relevant uses or negative number if something bad had
   happened and the optimization should be aborted.  */

static int
3314
see_handle_relevant_uses (df_ref ref, rtx insn)
Razya Ladelsky committed
3315 3316 3317
{
  struct web_entry *root_entry = NULL;
  rtx se_insn = NULL;
3318
  enum entry_type extension_code;
3319
  rtx reg = DF_REF_REAL_REG (ref);
Razya Ladelsky committed
3320

3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336
  root_entry = unionfind_root (&use_entry[DF_REF_ID (ref)]);
  
  if (ENTRY_EI (root_entry)->relevancy != SIGN_EXTENDED_DEF
      && ENTRY_EI (root_entry)->relevancy != ZERO_EXTENDED_DEF)
    /* The current web is not relevant.  Continue to the next use.  */
    return 0;
  
  if (root_entry->reg)
    /* It isn't possible to have two different register for the same
       web.  */
    gcc_assert (rtx_equal_p (root_entry->reg, reg));
  else
    root_entry->reg = reg;
  
  /* Generate the use extension.  */
  if (ENTRY_EI (root_entry)->relevancy == SIGN_EXTENDED_DEF)
3337
    extension_code = SIGN_EXTENDED_DEF;
3338
  else
3339
    extension_code = ZERO_EXTENDED_DEF;
3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
  
  se_insn =
    see_gen_normalized_extension (reg, extension_code,
				  ENTRY_EI (root_entry)->source_mode);
  if (!se_insn)
    /* This is very bad, abort the transformation.  */
    return -1;
  
  if (!see_store_reference_and_extension (insn, se_insn,
					  USE_EXTENSION))
    /* Something bad happened.  Abort the optimization.  */
    return -1;
  return 1;
}
Razya Ladelsky committed
3354

3355 3356 3357 3358 3359
static int
see_handle_relevant_refs (void)
{
  int num_relevant_refs = 0;
  basic_block bb;
Razya Ladelsky committed
3360

3361 3362 3363 3364 3365 3366
  FOR_ALL_BB (bb)
    {
      rtx insn;
      FOR_BB_INSNS (bb, insn)
	{
	  unsigned int uid = INSN_UID (insn);
Razya Ladelsky committed
3367

3368 3369
	  if (INSN_P (insn))
	    {
3370 3371
	      df_ref *use_rec;
	      df_ref *def_rec;
3372 3373 3374
	      
	      for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
		{
3375
		  df_ref use = *use_rec;
3376 3377 3378 3379 3380 3381 3382
		  int result = see_handle_relevant_uses (use, insn);
		  if (result == -1)
		    return -1;
		  num_relevant_refs += result;
		}
	      for (use_rec = DF_INSN_UID_EQ_USES (uid); *use_rec; use_rec++)
		{
3383
		  df_ref use = *use_rec;
3384 3385 3386 3387 3388 3389 3390
		  int result = see_handle_relevant_uses (use, insn);
		  if (result == -1)
		    return -1;
		  num_relevant_refs += result;
		}
	      for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
		{
3391
		  df_ref def = *def_rec;
3392 3393 3394 3395 3396 3397 3398
		  int result = see_handle_relevant_defs (def, insn);
		  if (result == -1)
		    return -1;
		  num_relevant_refs += result;
		}
	    }
	}
Razya Ladelsky committed
3399
    }
3400
   return num_relevant_refs;
Razya Ladelsky committed
3401 3402 3403
}


3404
/* Initialized the use_entry field for REF in INSN at INDEX with ET.  */
Razya Ladelsky committed
3405 3406

static void
3407
see_update_uses_relevancy (rtx insn, df_ref ref, 
3408
			   enum entry_type et, unsigned int index)
Razya Ladelsky committed
3409 3410 3411
{
  struct see_entry_extra_info *curr_entry_extra_info;

3412
  if (dump_file)
Razya Ladelsky committed
3413
    {
3414 3415 3416 3417 3418
      rtx reg = DF_REF_REAL_REG (ref);
      fprintf (dump_file, "u%i insn %i reg %i ", 
	       index, (insn ? INSN_UID (insn) : -1), REGNO (reg));
      if (et == NOT_RELEVANT)
	fprintf (dump_file, "NOT RELEVANT \n");
Razya Ladelsky committed
3419
      else
3420
	fprintf (dump_file, "RELEVANT USE \n");
Razya Ladelsky committed
3421
    }
3422 3423

  DF_REF_ID (ref) = index;
3424
  curr_entry_extra_info = XNEW (struct see_entry_extra_info);
3425 3426 3427 3428 3429
  curr_entry_extra_info->relevancy = et;
  curr_entry_extra_info->local_relevancy = et;
  use_entry[index].extra_info = curr_entry_extra_info;
  use_entry[index].reg = NULL;
  use_entry[index].pred = NULL;
Razya Ladelsky committed
3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
}


/* A definition in a candidate for this optimization only if its pattern is
   recognized as relevant in this function.
   INSN is the instruction to be recognized.

-  If this is the pattern of a common sign extension after definition:
   PREV_INSN (INSN):	def (reg:NARROWmode r)
   INSN:		set ((reg:WIDEmode r')
   			     (sign_extend:WIDEmode (reg:NARROWmode r)))
   return SIGN_EXTENDED_DEF and set SOURCE_MODE to NARROWmode.

-  If this is the pattern of a common zero extension after definition:
   PREV_INSN (INSN):	def (reg:NARROWmode r)
   INSN:		set ((reg:WIDEmode r')
   			     (zero_extend:WIDEmode (reg:NARROWmode r)))
   return ZERO_EXTENDED_DEF and set SOURCE_MODE to NARROWmode.

-  Otherwise,

   For the pattern:
   INSN:  set ((reg:WIDEmode r) (sign_extend:WIDEmode (...expr...)))
   return EXTENDED_DEF and set SOURCE_MODE to the mode of expr.

   For the pattern:
   INSN:  set ((reg:WIDEmode r) (zero_extend:WIDEmode (...expr...)))
   return EXTENDED_DEF and set SOURCE_MODE_UNSIGNED to the mode of expr.

   For the pattern:
   INSN:  set ((reg:WIDEmode r) (CONST_INT (...)))
   return EXTENDED_DEF and set SOURCE_MODE(_UNSIGNED) to the narrowest mode that
   is implicitly sign(zero) extended to WIDEmode in the INSN.

-  FIXME: Extensions that are not adjacent to their definition and EXTENDED_DEF
   that is part of a PARALLEL instruction are not handled.
   These restriction can be relaxed.  */

static enum entry_type
see_analyze_one_def (rtx insn, enum machine_mode *source_mode,
		     enum machine_mode *source_mode_unsigned)
{
3472
  enum entry_type extension_code;
Razya Ladelsky committed
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490
  rtx rhs = NULL;
  rtx lhs = NULL;
  rtx set = NULL;
  rtx source_register = NULL;
  rtx prev_insn = NULL;
  rtx next_insn = NULL;
  enum machine_mode mode;
  enum machine_mode next_source_mode;
  HOST_WIDE_INT val = 0;
  HOST_WIDE_INT val2 = 0;
  int i = 0;

  *source_mode = MAX_MACHINE_MODE;
  *source_mode_unsigned = MAX_MACHINE_MODE;

  extension_code = see_get_extension_data (insn, source_mode);
  switch (extension_code)
    {
3491 3492
    case SIGN_EXTENDED_DEF:
    case ZERO_EXTENDED_DEF:
Razya Ladelsky committed
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
      source_register = see_get_extension_reg (insn, 0);
      /* FIXME: This restriction can be relaxed.  The only thing that is
	 important is that the reference would be inside the same basic block
	 as the extension.  */
      prev_insn = PREV_INSN (insn);
      if (!prev_insn || !INSN_P (prev_insn))
	return NOT_RELEVANT;

      if (!reg_set_between_p (source_register, PREV_INSN (prev_insn), insn))
	return NOT_RELEVANT;

      /* If we can't use copy_rtx on the reference it can't be a reference.  */
3505 3506
      if (GET_CODE (PATTERN (prev_insn)) == PARALLEL
	   && asm_noperands (PATTERN (prev_insn)) >= 0)
Razya Ladelsky committed
3507 3508 3509 3510 3511 3512
	return NOT_RELEVANT;

      /* Now, check if this extension is a reference itself.  If so, it is not
	 relevant.  Handling this extension as relevant would make things much
	 more complicated.  */
      next_insn = NEXT_INSN (insn);
Mircea Namolaru committed
3513 3514
      if (next_insn
	  && INSN_P (next_insn)
Razya Ladelsky committed
3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
	  && (see_get_extension_data (next_insn, &next_source_mode) !=
	      NOT_RELEVANT))
	{
	  rtx curr_dest_register = see_get_extension_reg (insn, 1);
	  rtx next_source_register = see_get_extension_reg (next_insn, 0);

	  if (REGNO (curr_dest_register) == REGNO (next_source_register))
	    return NOT_RELEVANT;
	}

3525
      return extension_code;
Razya Ladelsky committed
3526

3527
    case NOT_RELEVANT:
Razya Ladelsky committed
3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
      /* This may still be an EXTENDED_DEF.  */

      /* FIXME: This restriction can be relaxed.  It is possible to handle
	 PARALLEL insns too.  */
      set = single_set (insn);
      if (!set)
	return NOT_RELEVANT;
      rhs = SET_SRC (set);
      lhs = SET_DEST (set);

      /* Don't handle extensions to something other then register or
	 subregister.  */
3540
      if (!REG_P (lhs) && GET_CODE (lhs) != SUBREG)
Razya Ladelsky committed
3541 3542 3543 3544
	return NOT_RELEVANT;

      switch (GET_CODE (rhs))
	{
3545
	case SIGN_EXTEND:
Razya Ladelsky committed
3546 3547 3548
	  *source_mode = GET_MODE (XEXP (rhs, 0));
	  *source_mode_unsigned = MAX_MACHINE_MODE;
	  return EXTENDED_DEF;
3549
	case ZERO_EXTEND:
Razya Ladelsky committed
3550 3551 3552
	  *source_mode = MAX_MACHINE_MODE;
	  *source_mode_unsigned = GET_MODE (XEXP (rhs, 0));
	  return EXTENDED_DEF;
3553
	case CONST_INT:
Razya Ladelsky committed
3554 3555 3556 3557 3558 3559 3560 3561 3562

	  val = INTVAL (rhs);

	  /* Find the narrowest mode, val could fit into.  */
	  for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT), i = 0;
	       GET_MODE_BITSIZE (mode) < BITS_PER_WORD;
	       mode = GET_MODE_WIDER_MODE (mode), i++)
	    {
	      val2 = trunc_int_for_mode (val, mode);
3563
  	      if (val2 == val && *source_mode == MAX_MACHINE_MODE)
Razya Ladelsky committed
3564
		*source_mode = mode;
3565 3566
	      if (val == (val & (HOST_WIDE_INT)GET_MODE_MASK (mode))
		  && *source_mode_unsigned == MAX_MACHINE_MODE)
Razya Ladelsky committed
3567
		*source_mode_unsigned = mode;
3568 3569
	      if (*source_mode != MAX_MACHINE_MODE
		  && *source_mode_unsigned !=MAX_MACHINE_MODE)
Razya Ladelsky committed
3570 3571
		return EXTENDED_DEF;
	    }
3572 3573
	  if (*source_mode != MAX_MACHINE_MODE
	      || *source_mode_unsigned !=MAX_MACHINE_MODE)
Razya Ladelsky committed
3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584
	    return EXTENDED_DEF;
	  return NOT_RELEVANT;
	default:
	  return NOT_RELEVANT;
	}
    default:
      gcc_unreachable ();
    }
}


3585
/* Initialized the def_entry field for REF in INSN at INDEX with ET.  */
Razya Ladelsky committed
3586 3587

static void
3588
see_update_defs_relevancy (rtx insn, df_ref ref,
3589 3590 3591 3592
			   enum entry_type et,
			   enum machine_mode source_mode,
			   enum machine_mode source_mode_unsigned,
			   unsigned int index)
Razya Ladelsky committed
3593
{
3594
  struct see_entry_extra_info *curr_entry_extra_info 
3595
    = XNEW (struct see_entry_extra_info);
3596 3597
  curr_entry_extra_info->relevancy = et;
  curr_entry_extra_info->local_relevancy = et;
Razya Ladelsky committed
3598

3599
  DF_REF_ID (ref) = index;
Razya Ladelsky committed
3600

3601
  if (et != EXTENDED_DEF)
Razya Ladelsky committed
3602
    {
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618
      curr_entry_extra_info->source_mode = source_mode;
      curr_entry_extra_info->local_source_mode = source_mode;
    }
  else
    {
      curr_entry_extra_info->source_mode_signed = source_mode;
      curr_entry_extra_info->source_mode_unsigned = source_mode_unsigned;
    }
  def_entry[index].extra_info = curr_entry_extra_info;
  def_entry[index].reg = NULL;
  def_entry[index].pred = NULL;
  
  if (dump_file)
    {
      rtx reg = DF_REF_REAL_REG (ref);
      if (et == NOT_RELEVANT)
Razya Ladelsky committed
3619
	{
3620 3621 3622
	  fprintf (dump_file, "d%i insn %i reg %i ",
		   index, (insn ? INSN_UID (insn) : -1), REGNO (reg));
	  fprintf (dump_file, "NOT RELEVANT \n");
Razya Ladelsky committed
3623 3624 3625
	}
      else
	{
3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
	  fprintf (dump_file, "d%i insn %i reg %i ",
		   index, INSN_UID (insn), REGNO (reg));
	  fprintf (dump_file, "RELEVANT - ");
	  switch (et)
	    {
	    case SIGN_EXTENDED_DEF :
	      fprintf (dump_file, "SIGN_EXTENDED_DEF, source_mode = %s\n",
		       GET_MODE_NAME (source_mode));
	      break;
	    case ZERO_EXTENDED_DEF :
	      fprintf (dump_file, "ZERO_EXTENDED_DEF, source_mode = %s\n",
		       GET_MODE_NAME (source_mode));
	      break;
	    case EXTENDED_DEF :
	      fprintf (dump_file, "EXTENDED_DEF, ");
	      if (source_mode != MAX_MACHINE_MODE
		  && source_mode_unsigned != MAX_MACHINE_MODE)
		{
		  fprintf (dump_file, "positive const, ");
		  fprintf (dump_file, "source_mode_signed = %s, ",
			   GET_MODE_NAME (source_mode));
		  fprintf (dump_file, "source_mode_unsigned = %s\n",
			   GET_MODE_NAME (source_mode_unsigned));
		}
	      else if (source_mode != MAX_MACHINE_MODE)
		fprintf (dump_file, "source_mode_signed = %s\n",
			 GET_MODE_NAME (source_mode));
	      else
		fprintf (dump_file, "source_mode_unsigned = %s\n",
			 GET_MODE_NAME (source_mode_unsigned));
	      break;
	    default :
	      gcc_unreachable ();
	    }
Razya Ladelsky committed
3660
	}
3661 3662
    }
}
Razya Ladelsky committed
3663

3664 3665 3666 3667 3668

/* Updates the relevancy of all the uses and all defs.  

   The information of the u'th use is stored in use_entry[u] and the
   information of the d'th definition is stored in def_entry[d].
3669

3670
   Currently all the uses are relevant for the optimization except for
3671
   uses that are in LIBCALL or RETVAL instructions.  */
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687

static void
see_update_relevancy (void)
{
  unsigned int d = 0;
  unsigned int u = 0;
  enum entry_type et;
  enum machine_mode source_mode;
  enum machine_mode source_mode_unsigned;
  basic_block bb;

  if (!def_entry)
    return;

  FOR_ALL_BB (bb)
    {
3688 3689
      df_ref *use_rec;
      df_ref *def_rec;
3690 3691
      rtx insn;
      FOR_BB_INSNS (bb, insn)
Razya Ladelsky committed
3692
	{
3693 3694
	  unsigned int uid = INSN_UID (insn);
	  if (INSN_P (insn))
Razya Ladelsky committed
3695
	    {
Steven Bosscher committed
3696
	      et = RELEVANT_USE;
3697

3698
	      for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
Razya Ladelsky committed
3699
		{
3700
		  df_ref use = *use_rec;
3701 3702 3703 3704 3705 3706
		  see_update_uses_relevancy (insn, use, et, u);
		  u++;
		}
	      
	      for (use_rec = DF_INSN_UID_EQ_USES (uid); *use_rec; use_rec++)
		{
3707
		  df_ref use = *use_rec;
3708 3709 3710 3711 3712 3713 3714
		  see_update_uses_relevancy (insn, use, et, u);
		  u++;
		}

	      et = see_analyze_one_def (insn, &source_mode, &source_mode_unsigned);
	      for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
		{
3715
		  df_ref def = *def_rec;
3716 3717 3718
		  see_update_defs_relevancy (insn, def, et, source_mode, 
					       source_mode_unsigned, d);
		  d++;
Razya Ladelsky committed
3719 3720 3721
		}
	    }
	}
3722 3723 3724
      
      for (use_rec = df_get_artificial_uses (bb->index); *use_rec; use_rec++)
	{
3725
	  df_ref use = *use_rec;
3726 3727 3728 3729 3730 3731
	  see_update_uses_relevancy (NULL, use, NOT_RELEVANT, u);
	  u++;
	}

      for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
	{
3732
	  df_ref def = *def_rec;
3733 3734 3735 3736
	  see_update_defs_relevancy (NULL, def, NOT_RELEVANT, 
				       MAX_MACHINE_MODE, MAX_MACHINE_MODE, d);
	  d++;
	}
Razya Ladelsky committed
3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751
    }
}


/* Phase 1 top level function.
   In this phase the relevancy of all the definitions and uses are checked,
   later the webs are produces and the extensions are generated.
   These extensions are not emitted yet into the insns stream.

   returns true if at list one relevant web was found and there were no
   problems, otherwise return false.  */

static bool
see_propagate_extensions_to_uses (void)
{
3752 3753
  int num_relevant_refs;
  basic_block bb;
Razya Ladelsky committed
3754 3755 3756 3757 3758 3759

  if (dump_file)
    fprintf (dump_file,
      "* Phase 1: Propagate extensions to uses.  *\n");

  /* Update the relevancy of references using the DF object.  */
3760
  see_update_relevancy ();
Razya Ladelsky committed
3761 3762 3763 3764 3765

  /* Produce the webs and update the extra_info of the root.
     In general, a web is relevant if all its definitions and uses are relevant
     and there is at least one definition that was marked as SIGN_EXTENDED_DEF
     or ZERO_EXTENDED_DEF.  */
3766 3767 3768
  FOR_ALL_BB (bb)
    {
      rtx insn;
3769
      df_ref *use_rec;
Razya Ladelsky committed
3770

3771 3772 3773 3774 3775 3776 3777
      FOR_BB_INSNS (bb, insn)
	{
	  unsigned int uid = INSN_UID (insn);
	  if (INSN_P (insn))
	    {
	      for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
		{
3778
		  df_ref use = *use_rec;
3779 3780 3781 3782 3783
		  union_defs (use, def_entry, use_entry, see_update_leader_extra_info);
		}
	      
	      for (use_rec = DF_INSN_UID_EQ_USES (uid); *use_rec; use_rec++)
		{
3784
		  df_ref use = *use_rec;
3785 3786 3787 3788
		  union_defs (use, def_entry, use_entry, see_update_leader_extra_info);
		}
	    }
	}
Razya Ladelsky committed
3789

3790 3791
      for (use_rec = df_get_artificial_uses (bb->index); *use_rec; use_rec++)
	{
3792
	  df_ref use = *use_rec;
3793 3794 3795
	  union_defs (use, def_entry, use_entry, see_update_leader_extra_info);
	}
    }
Razya Ladelsky committed
3796

3797
  /* Generate use extensions for references and insert these
Razya Ladelsky committed
3798
     references to see_bb_splay_ar data structure.    */
3799
  num_relevant_refs = see_handle_relevant_refs ();
Razya Ladelsky committed
3800

3801
  return num_relevant_refs > 0;
Razya Ladelsky committed
3802 3803 3804 3805 3806
}


/* Main entry point for the sign extension elimination optimization.  */

3807
static void
Razya Ladelsky committed
3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
see_main (void)
{
  bool cont = false;
  int i = 0;

  /* Initialize global data structures.  */
  see_initialize_data_structures ();

  /* Phase 1: Propagate extensions to uses.  */
  cont = see_propagate_extensions_to_uses ();

  if (cont)
    {
      init_recog ();

      /* Phase 2: Merge and eliminate locally redundant extensions.  */
      see_merge_and_eliminate_extensions ();

      /* Phase 3: Eliminate globally redundant extensions.  */
      see_execute_LCM ();

      /* Phase 4: Commit changes to the insn stream.  */
      see_commit_changes ();

      if (dump_file)
	{
	  /* For debug purpose only.  */
	  fprintf (dump_file, "see_pre_extension_hash:\n");
	  htab_traverse (see_pre_extension_hash, see_print_pre_extension_expr,
      			 NULL);

	  for (i = 0; i < last_bb; i++)
	    {
 	      if (see_bb_hash_ar[i])
		/* Traverse over all the references in the basic block in
		   forward order.  */
		{
		  fprintf (dump_file,
			   "Searching register properties in bb %d\n", i);
		  htab_traverse (see_bb_hash_ar[i],
		  		 see_print_register_properties, NULL);
		}
	    }
	}
    }

  /* Free global data structures.  */
  see_free_data_structures ();
}


static bool
gate_handle_see (void)
{
3862
  return optimize > 1 && flag_see;
Razya Ladelsky committed
3863 3864 3865 3866 3867 3868
}

static unsigned int
rest_of_handle_see (void)
{
  see_main ();
3869 3870
  df_clear_flags (DF_DEFER_INSN_RESCAN);
  df_process_deferred_rescans ();
3871
  run_fast_dce ();
Razya Ladelsky committed
3872 3873 3874
  return 0;
}

3875
struct rtl_opt_pass pass_see =
Razya Ladelsky committed
3876
{
3877 3878
 {
  RTL_PASS,
Razya Ladelsky committed
3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889
  "see",				/* name */
  gate_handle_see,			/* gate */
  rest_of_handle_see,			/* execute */
  NULL,					/* sub */
  NULL,					/* next */
  0,					/* static_pass_number */
  TV_SEE,				/* tv_id */
  0,					/* properties_required */
  0,					/* properties_provided */
  0,					/* properties_destroyed */
  0,					/* todo_flags_start */
3890
  TODO_df_verify |
3891
  TODO_df_finish | TODO_verify_rtl_sharing |
3892 3893
  TODO_dump_func			/* todo_flags_finish */
 }
Razya Ladelsky committed
3894
};