LCOV - code coverage report
Current view: top level - gcc - tree-ssa-address.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 83.1 % 509 423
Test Date: 2026-11-14 16:39:50 Functions: 91.7 % 24 22
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Memory address lowering and addressing mode selection.
       2              :    Copyright (C) 2004-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it
       7              : under the terms of the GNU General Public License as published by the
       8              : Free Software Foundation; either version 3, or (at your option) any
       9              : later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT
      12              : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : /* Utility functions for manipulation with TARGET_MEM_REFs -- tree expressions
      21              :    that directly map to addressing modes of the target.  */
      22              : 
      23              : #include "config.h"
      24              : #include "system.h"
      25              : #include "coretypes.h"
      26              : #include "backend.h"
      27              : #include "target.h"
      28              : #include "rtl.h"
      29              : #include "tree.h"
      30              : #include "gimple.h"
      31              : #include "memmodel.h"
      32              : #include "stringpool.h"
      33              : #include "tree-vrp.h"
      34              : #include "tree-ssanames.h"
      35              : #include "expmed.h"
      36              : #include "insn-config.h"
      37              : #include "emit-rtl.h"
      38              : #include "recog.h"
      39              : #include "tree-pretty-print.h"
      40              : #include "fold-const.h"
      41              : #include "stor-layout.h"
      42              : #include "gimple-iterator.h"
      43              : #include "gimplify-me.h"
      44              : #include "tree-ssa-loop-ivopts.h"
      45              : #include "expr.h"
      46              : #include "tree-dfa.h"
      47              : #include "dumpfile.h"
      48              : #include "tree-affine.h"
      49              : #include "gimplify.h"
      50              : #include "builtins.h"
      51              : 
      52              : /* FIXME: We compute address costs using RTL.  */
      53              : #include "tree-ssa-address.h"
      54              : 
      55              : /* TODO -- handling of symbols (according to Richard Hendersons
      56              :    comments, http://gcc.gnu.org/ml/gcc-patches/2005-04/msg00949.html):
      57              : 
      58              :    There are at least 5 different kinds of symbols that we can run up against:
      59              : 
      60              :      (1) binds_local_p, small data area.
      61              :      (2) binds_local_p, eg local statics
      62              :      (3) !binds_local_p, eg global variables
      63              :      (4) thread local, local_exec
      64              :      (5) thread local, !local_exec
      65              : 
      66              :    Now, (1) won't appear often in an array context, but it certainly can.
      67              :    All you have to do is set -GN high enough, or explicitly mark any
      68              :    random object __attribute__((section (".sdata"))).
      69              : 
      70              :    All of these affect whether or not a symbol is in fact a valid address.
      71              :    The only one tested here is (3).  And that result may very well
      72              :    be incorrect for (4) or (5).
      73              : 
      74              :    An incorrect result here does not cause incorrect results out the
      75              :    back end, because the expander in expr.cc validizes the address.  However
      76              :    it would be nice to improve the handling here in order to produce more
      77              :    precise results.  */
      78              : 
      79              : /* A "template" for memory address, used to determine whether the address is
      80              :    valid for mode.  */
      81              : 
      82              : struct GTY (()) mem_addr_template {
      83              :   rtx ref;                      /* The template.  */
      84              :   rtx * GTY ((skip)) step_p;    /* The point in template where the step should be
      85              :                                    filled in.  */
      86              :   rtx * GTY ((skip)) off_p;     /* The point in template where the offset should
      87              :                                    be filled in.  */
      88              : };
      89              : 
      90              : 
      91              : /* The templates.  Each of the low five bits of the index corresponds to one
      92              :    component of TARGET_MEM_REF being present, while the high bits identify
      93              :    the address space.  See TEMPL_IDX.  */
      94              : 
      95              : static GTY(()) vec<mem_addr_template, va_gc> *mem_addr_template_list;
      96              : 
      97              : #define TEMPL_IDX(AS, SYMBOL, BASE, INDEX, STEP, OFFSET) \
      98              :   (((int) (AS) << 5) \
      99              :    | ((SYMBOL != 0) << 4) \
     100              :    | ((BASE != 0) << 3) \
     101              :    | ((INDEX != 0) << 2) \
     102              :    | ((STEP != 0) << 1) \
     103              :    | (OFFSET != 0))
     104              : 
     105              : /* Stores address for memory reference with parameters SYMBOL, BASE, INDEX,
     106              :    STEP and OFFSET to *ADDR using address mode ADDRESS_MODE.  Stores pointers
     107              :    to where step is placed to *STEP_P and offset to *OFFSET_P.  */
     108              : 
     109              : static void
     110      1056794 : gen_addr_rtx (machine_mode address_mode,
     111              :               rtx symbol, rtx base, rtx index, rtx step, rtx offset,
     112              :               rtx *addr, rtx **step_p, rtx **offset_p)
     113              : {
     114      1056794 :   rtx act_elem;
     115              : 
     116      1056794 :   *addr = NULL_RTX;
     117      1056794 :   if (step_p)
     118       195646 :     *step_p = NULL;
     119      1056794 :   if (offset_p)
     120       195646 :     *offset_p = NULL;
     121              : 
     122      1056794 :   if (index && index != const0_rtx)
     123              :     {
     124       553473 :       act_elem = index;
     125       553473 :       if (step)
     126              :         {
     127       268062 :           act_elem = gen_rtx_MULT (address_mode, act_elem, step);
     128              : 
     129       268062 :           if (step_p)
     130        69974 :             *step_p = &XEXP (act_elem, 1);
     131              :         }
     132              : 
     133       553473 :       *addr = act_elem;
     134              :     }
     135              : 
     136      1056794 :   if (base && base != const0_rtx)
     137              :     {
     138       856414 :       if (*addr)
     139       353585 :         *addr = simplify_gen_binary (PLUS, address_mode, base, *addr);
     140              :       else
     141       502829 :         *addr = base;
     142              :     }
     143              : 
     144      1056794 :   if (symbol)
     145              :     {
     146       215561 :       act_elem = symbol;
     147       215561 :       if (offset)
     148              :         {
     149        53904 :           act_elem = gen_rtx_PLUS (address_mode, act_elem, offset);
     150              : 
     151        53904 :           if (offset_p)
     152        14251 :             *offset_p = &XEXP (act_elem, 1);
     153              : 
     154        53904 :           if (GET_CODE (symbol) == SYMBOL_REF
     155        53904 :               || GET_CODE (symbol) == LABEL_REF
     156        16130 :               || GET_CODE (symbol) == CONST)
     157        37774 :             act_elem = gen_rtx_CONST (address_mode, act_elem);
     158              :         }
     159              : 
     160       215561 :       if (*addr)
     161       215069 :         *addr = gen_rtx_PLUS (address_mode, *addr, act_elem);
     162              :       else
     163          492 :         *addr = act_elem;
     164              :     }
     165       841233 :   else if (offset)
     166              :     {
     167       361696 :       if (*addr)
     168              :         {
     169       361696 :           *addr = gen_rtx_PLUS (address_mode, *addr, offset);
     170       361696 :           if (offset_p)
     171        69742 :             *offset_p = &XEXP (*addr, 1);
     172              :         }
     173              :       else
     174              :         {
     175            0 :           *addr = offset;
     176            0 :           if (offset_p)
     177            0 :             *offset_p = addr;
     178              :         }
     179              :     }
     180              : 
     181      1056794 :   if (!*addr)
     182            0 :     *addr = const0_rtx;
     183      1056794 : }
     184              : 
     185              : /* Returns address for TARGET_MEM_REF with parameters given by ADDR
     186              :    in address space AS.
     187              :    If REALLY_EXPAND is false, just make fake registers instead
     188              :    of really expanding the operands, and perform the expansion in-place
     189              :    by using one of the "templates".  */
     190              : 
     191              : rtx
     192     18809642 : addr_for_mem_ref (struct mem_address *addr, addr_space_t as,
     193              :                   bool really_expand)
     194              : {
     195     18809642 :   scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
     196     18809642 :   scalar_int_mode pointer_mode = targetm.addr_space.pointer_mode (as);
     197     18809642 :   rtx address, sym, bse, idx, st, off;
     198     18809642 :   struct mem_addr_template *templ;
     199              : 
     200     18809642 :   if (addr->step && !integer_onep (addr->step))
     201      5681339 :     st = immed_wide_int_const (wi::to_wide (addr->step), pointer_mode);
     202              :   else
     203              :     st = NULL_RTX;
     204              : 
     205     18809642 :   if (addr->offset && !integer_zerop (addr->offset))
     206              :     {
     207      7842131 :       poly_offset_int dc
     208      7842131 :         = poly_offset_int::from (wi::to_poly_wide (addr->offset), SIGNED);
     209      7842131 :       off = immed_wide_int_const (dc, pointer_mode);
     210              :     }
     211              :   else
     212              :     off = NULL_RTX;
     213              : 
     214     18809642 :   if (!really_expand)
     215              :     {
     216     35896988 :       unsigned int templ_index
     217     39065039 :         = TEMPL_IDX (as, addr->symbol, addr->base, addr->index, st, off);
     218              : 
     219     17948494 :       if (templ_index >= vec_safe_length (mem_addr_template_list))
     220       107657 :         vec_safe_grow_cleared (mem_addr_template_list, templ_index + 1, true);
     221              : 
     222              :       /* Reuse the templates for addresses, so that we do not waste memory.  */
     223     17948494 :       templ = &(*mem_addr_template_list)[templ_index];
     224     17948494 :       if (!templ->ref)
     225              :         {
     226       195646 :           sym = (addr->symbol ?
     227        31318 :                  gen_rtx_SYMBOL_REF (pointer_mode, ggc_strdup ("test_symbol"))
     228              :                  : NULL_RTX);
     229       195646 :           bse = (addr->base ?
     230       169359 :                  gen_raw_REG (pointer_mode, LAST_VIRTUAL_REGISTER + 1)
     231              :                  : NULL_RTX);
     232       195646 :           idx = (addr->index ?
     233       142627 :                  gen_raw_REG (pointer_mode, LAST_VIRTUAL_REGISTER + 2)
     234              :                  : NULL_RTX);
     235              : 
     236       195646 :           gen_addr_rtx (pointer_mode, sym, bse, idx,
     237              :                         st? const0_rtx : NULL_RTX,
     238              :                         off? const0_rtx : NULL_RTX,
     239              :                         &templ->ref,
     240              :                         &templ->step_p,
     241              :                         &templ->off_p);
     242              :         }
     243              : 
     244     17948494 :       if (st)
     245      5483251 :         *templ->step_p = st;
     246     17948494 :       if (off)
     247      7510524 :         *templ->off_p = off;
     248              : 
     249     17948494 :       return templ->ref;
     250              :     }
     251              : 
     252              :   /* Otherwise really expand the expressions.  */
     253       861148 :   sym = (addr->symbol
     254       861148 :          ? expand_expr (addr->symbol, NULL_RTX, pointer_mode, EXPAND_NORMAL)
     255              :          : NULL_RTX);
     256       861148 :   bse = (addr->base
     257       861148 :          ? expand_expr (addr->base, NULL_RTX, pointer_mode, EXPAND_NORMAL)
     258              :          : NULL_RTX);
     259       861148 :   idx = (addr->index
     260       861148 :          ? expand_expr (addr->index, NULL_RTX, pointer_mode, EXPAND_NORMAL)
     261              :          : NULL_RTX);
     262              : 
     263              :   /* addr->base could be an SSA_NAME that was set to a constant value.  The
     264              :      call to expand_expr may expose that constant.  If so, fold the value
     265              :      into OFF and clear BSE.  Otherwise we may later try to pull a mode from
     266              :      BSE to generate a REG, which won't work with constants because they
     267              :      are modeless.  */
     268       861148 :   if (bse && GET_CODE (bse) == CONST_INT)
     269              :     {
     270            0 :       if (off)
     271            0 :         off = simplify_gen_binary (PLUS, pointer_mode, bse, off);
     272              :       else
     273              :         off = bse;
     274            0 :       gcc_assert (GET_CODE (off) == CONST_INT);
     275              :       bse = NULL_RTX;
     276              :     }
     277       861148 :   gen_addr_rtx (pointer_mode, sym, bse, idx, st, off, &address, NULL, NULL);
     278       861148 :   if (pointer_mode != address_mode)
     279            8 :     address = convert_memory_address (address_mode, address);
     280       861148 :   return address;
     281              : }
     282              : 
     283              : /* implement addr_for_mem_ref() directly from a tree, which avoids exporting
     284              :    the mem_address structure.  */
     285              : 
     286              : rtx
     287       861148 : addr_for_mem_ref (tree exp, addr_space_t as, bool really_expand)
     288              : {
     289       861148 :   struct mem_address addr;
     290       861148 :   get_address_description (exp, &addr);
     291       861148 :   return addr_for_mem_ref (&addr, as, really_expand);
     292              : }
     293              : 
     294              : /* Returns address of MEM_REF in TYPE.  */
     295              : 
     296              : tree
     297        56214 : tree_mem_ref_addr (tree type, tree mem_ref)
     298              : {
     299        56214 :   tree addr;
     300        56214 :   tree act_elem;
     301        56214 :   tree step = TMR_STEP (mem_ref), offset = TMR_OFFSET (mem_ref);
     302        56214 :   tree addr_base = NULL_TREE, addr_off = NULL_TREE;
     303              : 
     304        56214 :   addr_base = fold_convert (type, TMR_BASE (mem_ref));
     305              : 
     306        56214 :   act_elem = TMR_INDEX (mem_ref);
     307        56214 :   if (act_elem)
     308              :     {
     309         2673 :       if (step)
     310         2290 :         act_elem = fold_build2 (MULT_EXPR, TREE_TYPE (act_elem),
     311              :                                 act_elem, step);
     312              :       addr_off = act_elem;
     313              :     }
     314              : 
     315        56214 :   act_elem = TMR_INDEX2 (mem_ref);
     316        56214 :   if (act_elem)
     317              :     {
     318            0 :       if (addr_off)
     319            0 :         addr_off = fold_build2 (PLUS_EXPR, TREE_TYPE (addr_off),
     320              :                                 addr_off, act_elem);
     321              :       else
     322              :         addr_off = act_elem;
     323              :     }
     324              : 
     325        56214 :   if (offset && !integer_zerop (offset))
     326              :     {
     327        40576 :       if (addr_off)
     328          490 :         addr_off = fold_build2 (PLUS_EXPR, TREE_TYPE (addr_off), addr_off,
     329              :                                 fold_convert (TREE_TYPE (addr_off), offset));
     330              :       else
     331              :         addr_off = offset;
     332              :     }
     333              : 
     334        56214 :   if (addr_off)
     335        42759 :     addr = fold_build_pointer_plus (addr_base, addr_off);
     336              :   else
     337              :     addr = addr_base;
     338              : 
     339        56214 :   return addr;
     340              : }
     341              : 
     342              : /* Returns true if a memory reference in MODE and with parameters given by
     343              :    ADDR is valid on the current target.  */
     344              : 
     345              : bool
     346     11355734 : valid_mem_ref_p (machine_mode mode, addr_space_t as,
     347              :                  struct mem_address *addr, code_helper ch)
     348              : {
     349     11355734 :   rtx address;
     350              : 
     351     11355734 :   address = addr_for_mem_ref (addr, as, false);
     352     11355734 :   if (!address)
     353              :     return false;
     354              : 
     355     11355734 :   return memory_address_addr_space_p (mode, address, as, ch);
     356              : }
     357              : 
     358              : /* Checks whether a TARGET_MEM_REF with type TYPE and parameters given by ADDR
     359              :    is valid on the current target and if so, creates and returns the
     360              :    TARGET_MEM_REF.  If VERIFY is false omit the verification step.  */
     361              : 
     362              : static tree
     363       903025 : create_mem_ref_raw (tree type, tree alias_ptr_type, struct mem_address *addr,
     364              :                     bool verify)
     365              : {
     366       903025 :   tree base, index2;
     367              : 
     368       903025 :   if (verify
     369       903025 :       && !valid_mem_ref_p (TYPE_MODE (type), TYPE_ADDR_SPACE (type), addr))
     370        33108 :     return NULL_TREE;
     371              : 
     372       869917 :   if (addr->offset)
     373       348309 :     addr->offset = fold_convert (alias_ptr_type, addr->offset);
     374              :   else
     375       521608 :     addr->offset = build_int_cst (alias_ptr_type, 0);
     376              : 
     377       869917 :   if (addr->symbol)
     378              :     {
     379       111981 :       base = addr->symbol;
     380       111981 :       index2 = addr->base;
     381              :     }
     382       757936 :   else if (addr->base
     383       757936 :            && POINTER_TYPE_P (TREE_TYPE (addr->base)))
     384              :     {
     385              :       base = addr->base;
     386              :       index2 = NULL_TREE;
     387              :     }
     388              :   else
     389              :     {
     390           24 :       base = build_int_cst (build_pointer_type (type), 0);
     391           24 :       index2 = addr->base;
     392              :     }
     393              : 
     394              :   /* If possible use a plain MEM_REF instead of a TARGET_MEM_REF.
     395              :      ???  As IVOPTs does not follow restrictions to where the base
     396              :      pointer may point to create a MEM_REF only if we know that
     397              :      base is valid.  */
     398       683695 :   if ((TREE_CODE (base) == ADDR_EXPR || TREE_CODE (base) == INTEGER_CST)
     399       186246 :       && (!index2 || integer_zerop (index2))
     400      1046003 :       && (!addr->index || integer_zerop (addr->index)))
     401         1896 :     return fold_build2 (MEM_REF, type, base, addr->offset);
     402              : 
     403       868021 :   return build5 (TARGET_MEM_REF, type,
     404       868021 :                  base, addr->offset, addr->index, addr->step, index2);
     405              : }
     406              : 
     407              : /* Returns true if OBJ is an object whose address is a link time constant.  */
     408              : 
     409              : static bool
     410      1665329 : fixed_address_object_p (tree obj)
     411              : {
     412      1665329 :   return (VAR_P (obj)
     413      1625988 :           && (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
     414      2303305 :           && ! DECL_DLLIMPORT_P (obj));
     415              : }
     416              : 
     417              : /* If ADDR contains an address of object that is a link time constant,
     418              :    move it to PARTS->symbol.  */
     419              : 
     420              : void
     421      4552372 : move_fixed_address_to_symbol (struct mem_address *parts, aff_tree *addr)
     422              : {
     423      4552372 :   unsigned i;
     424      4552372 :   tree val = NULL_TREE;
     425              : 
     426      9667956 :   for (i = 0; i < addr->n; i++)
     427              :     {
     428      5753560 :       if (addr->elts[i].coef != 1)
     429      1176369 :         continue;
     430              : 
     431      4577191 :       val = addr->elts[i].val;
     432      4577191 :       if (TREE_CODE (val) == ADDR_EXPR
     433      4577191 :           && fixed_address_object_p (TREE_OPERAND (val, 0)))
     434              :         break;
     435              :     }
     436              : 
     437      4552372 :   if (i == addr->n)
     438              :     return;
     439              : 
     440       637976 :   parts->symbol = val;
     441       637976 :   aff_combination_remove_elt (addr, i);
     442              : }
     443              : 
     444              : /* Return true if ADDR contains an instance of BASE_HINT and it's moved to
     445              :    PARTS->base.  */
     446              : 
     447              : static bool
     448       471413 : move_hint_to_base (tree type, struct mem_address *parts, tree base_hint,
     449              :                    aff_tree *addr)
     450              : {
     451       471413 :   unsigned i;
     452       471413 :   tree val = NULL_TREE;
     453              : 
     454       492546 :   for (i = 0; i < addr->n; i++)
     455              :     {
     456       492522 :       if (addr->elts[i].coef != 1)
     457        20609 :         continue;
     458              : 
     459       471913 :       val = addr->elts[i].val;
     460       471913 :       if (operand_equal_p (val, base_hint, 0))
     461              :         break;
     462              :     }
     463              : 
     464       471413 :   if (i == addr->n)
     465              :     return false;
     466              : 
     467              :   /* Cast value to appropriate pointer type.  We cannot use a pointer
     468              :      to TYPE directly, as the back-end will assume registers of pointer
     469              :      type are aligned, and just the base itself may not actually be.
     470              :      We use void pointer to the type's address space instead.  */
     471       471389 :   qualifier_set qual {TYPE_UNQUALIFIED, TYPE_ADDR_SPACE (type)};
     472       471389 :   type = build_qualified_type (void_type_node, qual);
     473       471389 :   parts->base = fold_convert (build_pointer_type (type), val);
     474       471389 :   aff_combination_remove_elt (addr, i);
     475       471389 :   return true;
     476              : }
     477              : 
     478              : /* If ADDR contains an address of a dereferenced pointer, move it to
     479              :    PARTS->base.  */
     480              : 
     481              : static void
     482       283788 : move_pointer_to_base (struct mem_address *parts, aff_tree *addr)
     483              : {
     484       283788 :   unsigned i;
     485       283788 :   tree val = NULL_TREE;
     486              : 
     487       309064 :   for (i = 0; i < addr->n; i++)
     488              :     {
     489       309040 :       if (addr->elts[i].coef != 1)
     490        24085 :         continue;
     491              : 
     492       284955 :       val = addr->elts[i].val;
     493       284955 :       if (POINTER_TYPE_P (TREE_TYPE (val)))
     494              :         break;
     495              :     }
     496              : 
     497       283788 :   if (i == addr->n)
     498              :     return;
     499              : 
     500       283764 :   parts->base = val;
     501       283764 :   aff_combination_remove_elt (addr, i);
     502              : }
     503              : 
     504              : /* Moves the loop variant part V in linear address ADDR to be the index
     505              :    of PARTS.  */
     506              : 
     507              : static void
     508       394335 : move_variant_to_index (struct mem_address *parts, aff_tree *addr, tree v)
     509              : {
     510       394335 :   unsigned i;
     511       394335 :   tree val = NULL_TREE;
     512              : 
     513       394335 :   gcc_assert (!parts->index);
     514       722914 :   for (i = 0; i < addr->n; i++)
     515              :     {
     516       722529 :       val = addr->elts[i].val;
     517       722529 :       if (operand_equal_p (val, v, 0))
     518              :         break;
     519              :     }
     520              : 
     521       394335 :   if (i == addr->n)
     522              :     return;
     523              : 
     524       393950 :   parts->index = fold_convert (sizetype, val);
     525       393950 :   parts->step = wide_int_to_tree (sizetype, addr->elts[i].coef);
     526       393950 :   aff_combination_remove_elt (addr, i);
     527              : }
     528              : 
     529              : /* Adds ELT to PARTS.  */
     530              : 
     531              : static void
     532        55545 : add_to_parts (struct mem_address *parts, tree elt)
     533              : {
     534        55545 :   tree type;
     535              : 
     536        55545 :   if (!parts->index)
     537              :     {
     538         6879 :       parts->index = fold_convert (sizetype, elt);
     539         6879 :       return;
     540              :     }
     541              : 
     542        48666 :   if (!parts->base)
     543              :     {
     544        10190 :       parts->base = elt;
     545        10190 :       return;
     546              :     }
     547              : 
     548              :   /* Add ELT to base.  As we have arbitrarily associated address parts
     549              :      make sure to use unsigned arithmetic.  */
     550        38476 :   type = TREE_TYPE (parts->base);
     551        38476 :   if (POINTER_TYPE_P (type))
     552        33544 :     parts->base
     553        33544 :       = fold_convert (TREE_TYPE (parts->base),
     554              :                       fold_build2 (PLUS_EXPR, sizetype,
     555              :                                    fold_convert (sizetype, parts->base),
     556              :                                    fold_convert (sizetype, elt)));
     557              :   else
     558         4932 :     parts->base = fold_build2 (PLUS_EXPR, type, parts->base, elt);
     559              : }
     560              : 
     561              : /* Returns true if multiplying by RATIO is allowed in an address.  Test the
     562              :    validity for a memory reference accessing memory of mode MODE in address
     563              :    space AS.  */
     564              : 
     565              : static bool
     566        21160 : multiplier_allowed_in_address_p (HOST_WIDE_INT ratio, machine_mode mode,
     567              :                                  addr_space_t as)
     568              : {
     569              : #define MAX_RATIO 128
     570        21160 :   unsigned int data_index = (int) as * MAX_MACHINE_MODE + (int) mode;
     571        21160 :   static vec<sbitmap> valid_mult_list;
     572        21160 :   sbitmap valid_mult;
     573              : 
     574        21160 :   if (data_index >= valid_mult_list.length ())
     575         1063 :     valid_mult_list.safe_grow_cleared (data_index + 1, true);
     576              : 
     577        21160 :   valid_mult = valid_mult_list[data_index];
     578        21160 :   if (!valid_mult)
     579              :     {
     580         1175 :       machine_mode address_mode = targetm.addr_space.address_mode (as);
     581         1175 :       rtx reg1 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 1);
     582         1175 :       rtx reg2 = gen_raw_REG (address_mode, LAST_VIRTUAL_REGISTER + 2);
     583         1175 :       rtx addr, scaled;
     584         1175 :       HOST_WIDE_INT i;
     585              : 
     586         1175 :       valid_mult = sbitmap_alloc (2 * MAX_RATIO + 1);
     587         1175 :       bitmap_clear (valid_mult);
     588         1175 :       scaled = gen_rtx_fmt_ee (MULT, address_mode, reg1, NULL_RTX);
     589         1175 :       addr = gen_rtx_fmt_ee (PLUS, address_mode, scaled, reg2);
     590       303150 :       for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
     591              :         {
     592       301975 :           XEXP (scaled, 1) = gen_int_mode (i, address_mode);
     593       301975 :           if (memory_address_addr_space_p (mode, addr, as)
     594       301975 :               || memory_address_addr_space_p (mode, scaled, as))
     595         4700 :             bitmap_set_bit (valid_mult, i + MAX_RATIO);
     596              :         }
     597              : 
     598         1175 :       if (dump_file && (dump_flags & TDF_DETAILS))
     599              :         {
     600            0 :           fprintf (dump_file, "  allowed multipliers:");
     601            0 :           for (i = -MAX_RATIO; i <= MAX_RATIO; i++)
     602            0 :             if (bitmap_bit_p (valid_mult, i + MAX_RATIO))
     603            0 :               fprintf (dump_file, " %d", (int) i);
     604            0 :           fprintf (dump_file, "\n");
     605            0 :           fprintf (dump_file, "\n");
     606              :         }
     607              : 
     608         1175 :       valid_mult_list[data_index] = valid_mult;
     609              :     }
     610              : 
     611        21160 :   if (ratio > MAX_RATIO || ratio < -MAX_RATIO)
     612              :     return false;
     613              : 
     614        20977 :   return bitmap_bit_p (valid_mult, ratio + MAX_RATIO);
     615              : }
     616              : 
     617              : /* Finds the most expensive multiplication in ADDR that can be
     618              :    expressed in an addressing mode and move the corresponding
     619              :    element(s) to PARTS.  */
     620              : 
     621              : static void
     622       471798 : most_expensive_mult_to_index (tree type, struct mem_address *parts,
     623              :                               aff_tree *addr, bool speed)
     624              : {
     625       471798 :   addr_space_t as = TYPE_ADDR_SPACE (type);
     626       471798 :   machine_mode address_mode = targetm.addr_space.address_mode (as);
     627       471798 :   HOST_WIDE_INT coef;
     628       471798 :   unsigned best_mult_cost = 0, acost;
     629       471798 :   tree mult_elt = NULL_TREE, elt;
     630       471798 :   unsigned i, j;
     631       471798 :   enum tree_code op_code;
     632              : 
     633       471798 :   offset_int best_mult = 0;
     634       493849 :   for (i = 0; i < addr->n; i++)
     635              :     {
     636        22051 :       if (!wi::fits_shwi_p (addr->elts[i].coef))
     637            0 :         continue;
     638              : 
     639        22051 :       coef = addr->elts[i].coef.to_shwi ();
     640        36101 :       if (coef == 1
     641        22051 :           || !multiplier_allowed_in_address_p (coef, TYPE_MODE (type), as))
     642        14050 :         continue;
     643              : 
     644         8001 :       acost = mult_by_coeff_cost (coef, address_mode, speed);
     645              : 
     646         8001 :       if (acost > best_mult_cost)
     647              :         {
     648         7853 :           best_mult_cost = acost;
     649         7853 :           best_mult = offset_int::from (addr->elts[i].coef, SIGNED);
     650              :         }
     651              :     }
     652              : 
     653       471798 :   if (!best_mult_cost)
     654       463945 :     return;
     655              : 
     656              :   /* Collect elements multiplied by best_mult.  */
     657        19827 :   for (i = j = 0; i < addr->n; i++)
     658              :     {
     659        11974 :       offset_int amult = offset_int::from (addr->elts[i].coef, SIGNED);
     660        11974 :       offset_int amult_neg = -wi::sext (amult, TYPE_PRECISION (addr->type));
     661              : 
     662        11974 :       if (amult == best_mult)
     663              :         op_code = PLUS_EXPR;
     664         4006 :       else if (amult_neg == best_mult)
     665              :         op_code = MINUS_EXPR;
     666              :       else
     667              :         {
     668          397 :           addr->elts[j] = addr->elts[i];
     669          397 :           j++;
     670          397 :           continue;
     671              :         }
     672              : 
     673        11577 :       elt = fold_convert (sizetype, addr->elts[i].val);
     674        11577 :       if (mult_elt)
     675         3724 :         mult_elt = fold_build2 (op_code, sizetype, mult_elt, elt);
     676         7853 :       else if (op_code == PLUS_EXPR)
     677              :         mult_elt = elt;
     678              :       else
     679          902 :         mult_elt = fold_build1 (NEGATE_EXPR, sizetype, elt);
     680              :     }
     681         7853 :   addr->n = j;
     682              : 
     683         7853 :   parts->index = mult_elt;
     684         7853 :   parts->step = wide_int_to_tree (sizetype, best_mult);
     685              : }
     686              : 
     687              : /* Splits address ADDR for a memory access of type TYPE into PARTS.
     688              :    If BASE_HINT is non-NULL, it specifies an SSA name to be used
     689              :    preferentially as base of the reference, and IV_CAND is the selected
     690              :    iv candidate used in ADDR.  Store true to VAR_IN_BASE if variant
     691              :    part of address is split to PARTS.base.
     692              : 
     693              :    TODO -- be more clever about the distribution of the elements of ADDR
     694              :    to PARTS.  Some architectures do not support anything but single
     695              :    register in address, possibly with a small integer offset; while
     696              :    create_mem_ref will simplify the address to an acceptable shape
     697              :    later, it would be more efficient to know that asking for complicated
     698              :    addressing modes is useless.  */
     699              : 
     700              : static void
     701       865748 : addr_to_parts (tree type, aff_tree *addr, tree iv_cand, tree base_hint,
     702              :                struct mem_address *parts, bool *var_in_base, bool speed)
     703              : {
     704       865748 :   tree part;
     705       865748 :   unsigned i;
     706              : 
     707       865748 :   parts->symbol = NULL_TREE;
     708       865748 :   parts->base = NULL_TREE;
     709       865748 :   parts->index = NULL_TREE;
     710       865748 :   parts->step = NULL_TREE;
     711              : 
     712       865748 :   if (maybe_ne (addr->offset, 0))
     713       344151 :     parts->offset = wide_int_to_tree (sizetype, addr->offset);
     714              :   else
     715       521597 :     parts->offset = NULL_TREE;
     716              : 
     717              :   /* Try to find a symbol.  */
     718       865748 :   move_fixed_address_to_symbol (parts, addr);
     719              : 
     720              :   /* Since at the moment there is no reliable way to know how to
     721              :      distinguish between pointer and its offset, we decide if var
     722              :      part is the pointer based on guess.  */
     723       865748 :   *var_in_base = (base_hint != NULL && parts->symbol == NULL);
     724       865748 :   if (*var_in_base)
     725       471413 :     *var_in_base = move_hint_to_base (type, parts, base_hint, addr);
     726              :   else
     727       394335 :     move_variant_to_index (parts, addr, iv_cand);
     728              : 
     729              :   /* First move the most expensive feasible multiplication to index.  */
     730       865748 :   if (!parts->index)
     731       471798 :     most_expensive_mult_to_index (type, parts, addr, speed);
     732              : 
     733              :   /* Move pointer into base.  */
     734       865748 :   if (!parts->symbol && !parts->base)
     735       283788 :     move_pointer_to_base (parts, addr);
     736              : 
     737              :   /* Then try to process the remaining elements.  */
     738       921293 :   for (i = 0; i < addr->n; i++)
     739              :     {
     740        55545 :       part = fold_convert (sizetype, addr->elts[i].val);
     741        55545 :       if (addr->elts[i].coef != 1)
     742        53092 :         part = fold_build2 (MULT_EXPR, sizetype, part,
     743              :                             wide_int_to_tree (sizetype, addr->elts[i].coef));
     744        55545 :       add_to_parts (parts, part);
     745              :     }
     746       865748 :   if (addr->rest)
     747            0 :     add_to_parts (parts, fold_convert (sizetype, addr->rest));
     748       865748 : }
     749              : 
     750              : /* Force the PARTS to register.  */
     751              : 
     752              : static void
     753       865748 : gimplify_mem_ref_parts (gimple_stmt_iterator *gsi, struct mem_address *parts)
     754              : {
     755       865748 :   if (parts->base)
     756       765343 :     parts->base = force_gimple_operand_gsi_1 (gsi, parts->base,
     757              :                                             is_gimple_mem_ref_addr, NULL_TREE,
     758              :                                             true, GSI_SAME_STMT);
     759       865748 :   if (parts->index)
     760       408682 :     parts->index = force_gimple_operand_gsi (gsi, parts->index,
     761              :                                              true, NULL_TREE,
     762              :                                              true, GSI_SAME_STMT);
     763       865748 : }
     764              : 
     765              : /* Return true if the OFFSET in PARTS is the only thing that is making
     766              :    it an invalid address for type TYPE.  */
     767              : 
     768              : static bool
     769        22291 : mem_ref_valid_without_offset_p (tree type, mem_address parts)
     770              : {
     771        22291 :   if (!parts.base)
     772            0 :     parts.base = parts.offset;
     773        22291 :   parts.offset = NULL_TREE;
     774        22291 :   return valid_mem_ref_p (TYPE_MODE (type), TYPE_ADDR_SPACE (type), &parts);
     775              : }
     776              : 
     777              : /* Fold PARTS->offset into PARTS->base, so that there is no longer
     778              :    a separate offset.  Emit any new instructions before GSI.  */
     779              : 
     780              : static void
     781            0 : add_offset_to_base (gimple_stmt_iterator *gsi, mem_address *parts)
     782              : {
     783            0 :   tree tmp = parts->offset;
     784            0 :   if (parts->base)
     785              :     {
     786            0 :       tmp = fold_build_pointer_plus (parts->base, tmp);
     787            0 :       tmp = force_gimple_operand_gsi_1 (gsi, tmp, is_gimple_mem_ref_addr,
     788              :                                         NULL_TREE, true, GSI_SAME_STMT);
     789              :     }
     790            0 :   parts->base = tmp;
     791            0 :   parts->offset = NULL_TREE;
     792            0 : }
     793              : 
     794              : /* Creates and returns a TARGET_MEM_REF for address ADDR.  If necessary
     795              :    computations are emitted in front of GSI.  TYPE is the mode
     796              :    of created memory reference. IV_CAND is the selected iv candidate in ADDR,
     797              :    and BASE_HINT is non NULL if IV_CAND comes from a base address
     798              :    object.  */
     799              : 
     800              : tree
     801       865748 : create_mem_ref (gimple_stmt_iterator *gsi, tree type, aff_tree *addr,
     802              :                 tree alias_ptr_type, tree iv_cand, tree base_hint, bool speed)
     803              : {
     804       865748 :   bool var_in_base;
     805       865748 :   tree mem_ref, tmp;
     806       865748 :   struct mem_address parts;
     807              : 
     808       865748 :   addr_to_parts (type, addr, iv_cand, base_hint, &parts, &var_in_base, speed);
     809       865748 :   gimplify_mem_ref_parts (gsi, &parts);
     810       865748 :   mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     811       865748 :   if (mem_ref)
     812              :     return mem_ref;
     813              : 
     814              :   /* The expression is too complicated.  Try making it simpler.  */
     815              : 
     816              :   /* Merge symbol into other parts.  */
     817        32942 :   if (parts.symbol)
     818              :     {
     819          497 :       tmp = parts.symbol;
     820          497 :       parts.symbol = NULL_TREE;
     821          497 :       gcc_assert (is_gimple_val (tmp));
     822              : 
     823          497 :       if (parts.base)
     824              :         {
     825           39 :           gcc_assert (useless_type_conversion_p (sizetype,
     826              :                                                  TREE_TYPE (parts.base)));
     827              : 
     828           39 :           if (parts.index)
     829              :             {
     830              :               /* Add the symbol to base, eventually forcing it to register.  */
     831           39 :               tmp = fold_build_pointer_plus (tmp, parts.base);
     832           39 :               tmp = force_gimple_operand_gsi_1 (gsi, tmp,
     833              :                                                 is_gimple_mem_ref_addr,
     834              :                                                 NULL_TREE, true,
     835              :                                                 GSI_SAME_STMT);
     836              :             }
     837              :           else
     838              :             {
     839              :               /* Move base to index, then move the symbol to base.  */
     840            0 :               parts.index = parts.base;
     841              :             }
     842           39 :           parts.base = tmp;
     843              :         }
     844              :       else
     845          458 :         parts.base = tmp;
     846              : 
     847          497 :       mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     848          497 :       if (mem_ref)
     849              :         return mem_ref;
     850              :     }
     851              : 
     852              :   /* Move multiplication to index by transforming address expression:
     853              :        [... + index << step + ...]
     854              :      into:
     855              :        index' = index << step;
     856              :        [... + index' + ,,,].  */
     857        32611 :   if (parts.step && !integer_onep (parts.step))
     858              :     {
     859        32600 :       gcc_assert (parts.index);
     860        32600 :       if (parts.offset && mem_ref_valid_without_offset_p (type, parts))
     861              :         {
     862            0 :           add_offset_to_base (gsi, &parts);
     863            0 :           mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     864            0 :           gcc_assert (mem_ref);
     865              :           return mem_ref;
     866              :         }
     867              : 
     868        32600 :       parts.index = force_gimple_operand_gsi (gsi,
     869              :                                 fold_build2 (MULT_EXPR, sizetype,
     870              :                                              parts.index, parts.step),
     871              :                                 true, NULL_TREE, true, GSI_SAME_STMT);
     872        32600 :       parts.step = NULL_TREE;
     873              : 
     874        32600 :       mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     875        32600 :       if (mem_ref)
     876              :         return mem_ref;
     877              :     }
     878              : 
     879              :   /* Add offset to invariant part by transforming address expression:
     880              :        [base + index + offset]
     881              :      into:
     882              :        base' = base + offset;
     883              :        [base' + index]
     884              :      or:
     885              :        index' = index + offset;
     886              :        [base + index']
     887              :      depending on which one is invariant.  */
     888           11 :   if (parts.offset && !integer_zerop (parts.offset))
     889              :     {
     890           11 :       tree old_base = unshare_expr (parts.base);
     891           11 :       tree old_index = unshare_expr (parts.index);
     892           11 :       tree old_offset = unshare_expr (parts.offset);
     893              : 
     894           11 :       tmp = parts.offset;
     895           11 :       parts.offset = NULL_TREE;
     896              :       /* Add offset to invariant part.  */
     897           11 :       if (!var_in_base)
     898              :         {
     899            5 :           if (parts.base)
     900              :             {
     901            5 :               tmp = fold_build_pointer_plus (parts.base, tmp);
     902            5 :               tmp = force_gimple_operand_gsi_1 (gsi, tmp,
     903              :                                                 is_gimple_mem_ref_addr,
     904              :                                                 NULL_TREE, true,
     905              :                                                 GSI_SAME_STMT);
     906              :             }
     907            5 :           parts.base = tmp;
     908              :         }
     909              :       else
     910              :         {
     911            6 :           if (parts.index)
     912              :             {
     913            0 :               tmp = fold_build_pointer_plus (parts.index, tmp);
     914            0 :               tmp = force_gimple_operand_gsi_1 (gsi, tmp,
     915              :                                                 is_gimple_mem_ref_addr,
     916              :                                                 NULL_TREE, true,
     917              :                                                 GSI_SAME_STMT);
     918              :             }
     919            6 :           parts.index = tmp;
     920              :         }
     921              : 
     922           11 :       mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     923           11 :       if (mem_ref)
     924              :         return mem_ref;
     925              : 
     926              :       /* Restore parts.base, index and offset so that we can check if
     927              :          [base + offset] addressing mode is supported in next step.
     928              :          This is necessary for targets only support [base + offset],
     929              :          but not [base + index] addressing mode.  */
     930            0 :       parts.base = old_base;
     931            0 :       parts.index = old_index;
     932            0 :       parts.offset = old_offset;
     933              :     }
     934              : 
     935              :   /* Transform [base + index + ...] into:
     936              :        base' = base + index;
     937              :        [base' + ...].  */
     938            0 :   if (parts.index)
     939              :     {
     940            0 :       tmp = parts.index;
     941            0 :       parts.index = NULL_TREE;
     942              :       /* Add index to base.  */
     943            0 :       if (parts.base)
     944              :         {
     945            0 :           tmp = fold_build_pointer_plus (parts.base, tmp);
     946            0 :           tmp = force_gimple_operand_gsi_1 (gsi, tmp,
     947              :                                             is_gimple_mem_ref_addr,
     948              :                                             NULL_TREE, true, GSI_SAME_STMT);
     949              :         }
     950            0 :       parts.base = tmp;
     951              : 
     952            0 :       mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     953            0 :       if (mem_ref)
     954              :         return mem_ref;
     955              :     }
     956              : 
     957              :   /* Transform [base + offset] into:
     958              :        base' = base + offset;
     959              :        [base'].  */
     960            0 :   if (parts.offset && !integer_zerop (parts.offset))
     961              :     {
     962            0 :       add_offset_to_base (gsi, &parts);
     963            0 :       mem_ref = create_mem_ref_raw (type, alias_ptr_type, &parts, true);
     964            0 :       if (mem_ref)
     965              :         return mem_ref;
     966              :     }
     967              : 
     968              :   /* Verify that the address is in the simplest possible shape
     969              :      (only a register).  If we cannot create such a memory reference,
     970              :      something is really wrong.  */
     971            0 :   gcc_assert (parts.symbol == NULL_TREE);
     972            0 :   gcc_assert (parts.index == NULL_TREE);
     973            0 :   gcc_assert (!parts.step || integer_onep (parts.step));
     974            0 :   gcc_assert (!parts.offset || integer_zerop (parts.offset));
     975            0 :   gcc_unreachable ();
     976              : }
     977              : 
     978              : /* Copies components of the address from OP to ADDR.  */
     979              : 
     980              : void
     981      2841589 : get_address_description (tree op, struct mem_address *addr)
     982              : {
     983      2841589 :   if (TREE_CODE (TMR_BASE (op)) == ADDR_EXPR)
     984              :     {
     985       581825 :       addr->symbol = TMR_BASE (op);
     986       581825 :       addr->base = TMR_INDEX2 (op);
     987              :     }
     988              :   else
     989              :     {
     990      2259764 :       addr->symbol = NULL_TREE;
     991      2259764 :       if (TMR_INDEX2 (op))
     992              :         {
     993          100 :           gcc_assert (integer_zerop (TMR_BASE (op)));
     994          100 :           addr->base = TMR_INDEX2 (op);
     995              :         }
     996              :       else
     997      2259664 :         addr->base = TMR_BASE (op);
     998              :     }
     999      2841589 :   addr->index = TMR_INDEX (op);
    1000      2841589 :   addr->step = TMR_STEP (op);
    1001      2841589 :   addr->offset = TMR_OFFSET (op);
    1002      2841589 : }
    1003              : 
    1004              : /* Copies the reference information from OLD_REF to NEW_REF, where
    1005              :    NEW_REF should be either a MEM_REF or a TARGET_MEM_REF.  */
    1006              : 
    1007              : void
    1008       912101 : copy_ref_info (tree new_ref, tree old_ref)
    1009              : {
    1010       912101 :   tree new_ptr_base = NULL_TREE;
    1011              : 
    1012       912101 :   gcc_assert (TREE_CODE (new_ref) == MEM_REF
    1013              :               || TREE_CODE (new_ref) == TARGET_MEM_REF);
    1014              : 
    1015       912101 :   TREE_SIDE_EFFECTS (new_ref) = TREE_SIDE_EFFECTS (old_ref);
    1016       912101 :   TREE_THIS_VOLATILE (new_ref) = TREE_THIS_VOLATILE (old_ref);
    1017              : 
    1018       912101 :   new_ptr_base = TREE_OPERAND (new_ref, 0);
    1019              : 
    1020       912101 :   tree base = get_base_address (old_ref);
    1021       912101 :   if (!base)
    1022              :     return;
    1023              : 
    1024              :   /* We can transfer points-to information from an old pointer
    1025              :      or decl base to the new one.  */
    1026       912101 :   if (new_ptr_base
    1027       912101 :       && TREE_CODE (new_ptr_base) == SSA_NAME
    1028      1221600 :       && !ssa_name_pt_solution (new_ptr_base))
    1029              :     {
    1030       400042 :       if ((TREE_CODE (base) == MEM_REF
    1031       400042 :            || TREE_CODE (base) == TARGET_MEM_REF)
    1032       318443 :           && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
    1033      1017227 :           && ssa_name_pt_solution (TREE_OPERAND (base, 0)))
    1034              :         {
    1035       288644 :           duplicate_ssa_name_pt_solution (new_ptr_base,
    1036       288644 :                                           TREE_OPERAND (base, 0));
    1037       288644 :           reset_flow_sensitive_info (new_ptr_base);
    1038              :         }
    1039       111398 :       else if (VAR_P (base)
    1040              :                || TREE_CODE (base) == PARM_DECL
    1041              :                || TREE_CODE (base) == RESULT_DECL)
    1042              :         {
    1043        81599 :           pt_solution pt;
    1044        81599 :           pt_solution_set_var (&pt, base);
    1045        81599 :           set_ssa_name_pt_solution (new_ptr_base, pt);
    1046              :         }
    1047              :     }
    1048              : 
    1049              :   /* We can transfer dependence info.  */
    1050       912101 :   if (!MR_DEPENDENCE_CLIQUE (new_ref)
    1051       912101 :       && (TREE_CODE (base) == MEM_REF
    1052       912101 :           || TREE_CODE (base) == TARGET_MEM_REF)
    1053      1626502 :       && MR_DEPENDENCE_CLIQUE (base))
    1054              :     {
    1055       183589 :       MR_DEPENDENCE_CLIQUE (new_ref) = MR_DEPENDENCE_CLIQUE (base);
    1056       183589 :       MR_DEPENDENCE_BASE (new_ref) = MR_DEPENDENCE_BASE (base);
    1057              :     }
    1058              : 
    1059              :   /* And alignment info.  Note we cannot transfer misalignment info
    1060              :      since that sits on the SSA name but this is flow-sensitive info
    1061              :      which we cannot transfer in this generic routine.  */
    1062       912101 :   unsigned old_align = get_object_alignment (old_ref);
    1063       912101 :   unsigned new_align = get_object_alignment (new_ref);
    1064       912101 :   if (new_align < old_align)
    1065          822 :     TREE_TYPE (new_ref) = build_aligned_type (TREE_TYPE (new_ref), old_align);
    1066              : }
    1067              : 
    1068              : /* Move constants in target_mem_ref REF to offset.  Returns the new target
    1069              :    mem ref if anything changes, NULL_TREE otherwise.  */
    1070              : 
    1071              : tree
    1072      1980441 : maybe_fold_tmr (tree ref)
    1073              : {
    1074      1980441 :   struct mem_address addr;
    1075      1980441 :   bool changed = false;
    1076      1980441 :   tree new_ref, off;
    1077              : 
    1078      1980441 :   get_address_description (ref, &addr);
    1079              : 
    1080      1980441 :   if (addr.base
    1081      1613129 :       && TREE_CODE (addr.base) == INTEGER_CST
    1082      1980447 :       && !integer_zerop (addr.base))
    1083              :     {
    1084            2 :       addr.offset = fold_binary_to_constant (PLUS_EXPR,
    1085            2 :                                              TREE_TYPE (addr.offset),
    1086              :                                              addr.offset, addr.base);
    1087            2 :       addr.base = NULL_TREE;
    1088            2 :       changed = true;
    1089              :     }
    1090              : 
    1091      1980441 :   if (addr.symbol
    1092      1980441 :       && TREE_CODE (TREE_OPERAND (addr.symbol, 0)) == MEM_REF)
    1093              :     {
    1094            0 :       addr.offset = fold_binary_to_constant
    1095            0 :                         (PLUS_EXPR, TREE_TYPE (addr.offset),
    1096              :                          addr.offset,
    1097            0 :                          TREE_OPERAND (TREE_OPERAND (addr.symbol, 0), 1));
    1098            0 :       addr.symbol = TREE_OPERAND (TREE_OPERAND (addr.symbol, 0), 0);
    1099            0 :       changed = true;
    1100              :     }
    1101      1980441 :   else if (addr.symbol
    1102      1980441 :            && handled_component_p (TREE_OPERAND (addr.symbol, 0)))
    1103              :     {
    1104            0 :       poly_int64 offset;
    1105            0 :       addr.symbol = build_fold_addr_expr
    1106              :                       (get_addr_base_and_unit_offset
    1107              :                          (TREE_OPERAND (addr.symbol, 0), &offset));
    1108            0 :       addr.offset = int_const_binop (PLUS_EXPR,
    1109            0 :                                      addr.offset, size_int (offset));
    1110            0 :       changed = true;
    1111              :     }
    1112              : 
    1113      1980441 :   if (addr.index && TREE_CODE (addr.index) == INTEGER_CST)
    1114              :     {
    1115         4167 :       off = addr.index;
    1116         4167 :       if (addr.step)
    1117              :         {
    1118         3964 :           off = fold_binary_to_constant (MULT_EXPR, sizetype,
    1119              :                                          off, addr.step);
    1120         3964 :           addr.step = NULL_TREE;
    1121              :         }
    1122              : 
    1123         4167 :       addr.offset = fold_binary_to_constant (PLUS_EXPR,
    1124         4167 :                                              TREE_TYPE (addr.offset),
    1125              :                                              addr.offset, off);
    1126         4167 :       addr.index = NULL_TREE;
    1127         4167 :       changed = true;
    1128              :     }
    1129              : 
    1130      1980441 :   if (!changed)
    1131              :     return NULL_TREE;
    1132              : 
    1133              :   /* If we have propagated something into this TARGET_MEM_REF and thus
    1134              :      ended up folding it, always create a new TARGET_MEM_REF regardless
    1135              :      if it is valid in this for on the target - the propagation result
    1136              :      wouldn't be anyway.  */
    1137         4169 :   new_ref = create_mem_ref_raw (TREE_TYPE (ref),
    1138         4169 :                                 TREE_TYPE (addr.offset), &addr, false);
    1139         4169 :   TREE_SIDE_EFFECTS (new_ref) = TREE_SIDE_EFFECTS (ref);
    1140         4169 :   TREE_THIS_VOLATILE (new_ref) = TREE_THIS_VOLATILE (ref);
    1141         4169 :   return new_ref;
    1142              : }
    1143              : 
    1144              : /* Return the preferred index scale factor for accessing memory of mode
    1145              :    MEM_MODE in the address space of pointer BASE.  Assume that we're
    1146              :    optimizing for speed if SPEED is true and for size otherwise.  */
    1147              : unsigned int
    1148       541754 : preferred_mem_scale_factor (tree base, machine_mode mem_mode,
    1149              :                             bool speed)
    1150              : {
    1151              :   /* For BLKmode, we can't do anything so return 1.  */
    1152       541754 :   if (mem_mode == BLKmode)
    1153              :     return 1;
    1154              : 
    1155       533421 :   struct mem_address parts = {};
    1156       533421 :   addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (base));
    1157       533421 :   unsigned int fact = GET_MODE_UNIT_SIZE (mem_mode);
    1158              : 
    1159              :   /* Addressing mode "base + index".  */
    1160       533421 :   parts.index = integer_one_node;
    1161       533421 :   parts.base = integer_one_node;
    1162       533421 :   rtx addr = addr_for_mem_ref (&parts, as, false);
    1163       533421 :   unsigned cost = address_cost (addr, mem_mode, as, speed);
    1164              : 
    1165              :   /* Addressing mode "base + index << scale".  */
    1166       533421 :   parts.step = wide_int_to_tree (sizetype, fact);
    1167       533421 :   addr = addr_for_mem_ref (&parts, as, false);
    1168       533421 :   unsigned new_cost = address_cost (addr, mem_mode, as, speed);
    1169              : 
    1170              :   /* Compare the cost of an address with an unscaled index with
    1171              :      a scaled index and return factor if useful. */
    1172       533421 :   if (new_cost < cost)
    1173            0 :     return GET_MODE_UNIT_SIZE (mem_mode);
    1174              :   return 1;
    1175              : }
    1176              : 
    1177              : /* Dump PARTS to FILE.  */
    1178              : 
    1179              : extern void dump_mem_address (FILE *, struct mem_address *);
    1180              : void
    1181            0 : dump_mem_address (FILE *file, struct mem_address *parts)
    1182              : {
    1183            0 :   if (parts->symbol)
    1184              :     {
    1185            0 :       fprintf (file, "symbol: ");
    1186            0 :       print_generic_expr (file, TREE_OPERAND (parts->symbol, 0), TDF_SLIM);
    1187            0 :       fprintf (file, "\n");
    1188              :     }
    1189            0 :   if (parts->base)
    1190              :     {
    1191            0 :       fprintf (file, "base: ");
    1192            0 :       print_generic_expr (file, parts->base, TDF_SLIM);
    1193            0 :       fprintf (file, "\n");
    1194              :     }
    1195            0 :   if (parts->index)
    1196              :     {
    1197            0 :       fprintf (file, "index: ");
    1198            0 :       print_generic_expr (file, parts->index, TDF_SLIM);
    1199            0 :       fprintf (file, "\n");
    1200              :     }
    1201            0 :   if (parts->step)
    1202              :     {
    1203            0 :       fprintf (file, "step: ");
    1204            0 :       print_generic_expr (file, parts->step, TDF_SLIM);
    1205            0 :       fprintf (file, "\n");
    1206              :     }
    1207            0 :   if (parts->offset)
    1208              :     {
    1209            0 :       fprintf (file, "offset: ");
    1210            0 :       print_generic_expr (file, parts->offset, TDF_SLIM);
    1211            0 :       fprintf (file, "\n");
    1212              :     }
    1213            0 : }
    1214              : 
    1215              : #include "gt-tree-ssa-address.h"
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.