home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__src1 / explow.c < prev    next >
C/C++ Source or Header  |  1993-07-23  |  15KB  |  552 lines

  1. /* Subroutines for manipulating rtx's in semantically interesting ways.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include "rtl.h"
  23. #include "tree.h"
  24. #include "flags.h"
  25. #include "expr.h"
  26.  
  27. /* Return an rtx for the sum of X and the integer C.  */
  28.  
  29. rtx
  30. plus_constant (x, c)
  31.      register rtx x;
  32.      register int c;
  33. {
  34.   register RTX_CODE code = GET_CODE (x);
  35.   register enum machine_mode mode = GET_MODE (x);
  36.   int all_constant = 0;
  37.  
  38.   if (c == 0)
  39.     return x;
  40.  
  41.   if (code == CONST_INT)
  42.     return gen_rtx (CONST_INT, VOIDmode, (INTVAL (x) + c));
  43.  
  44.   /* If adding to something entirely constant, set a flag
  45.      so that we can add a CONST around the result.  */
  46.   if (code == CONST)
  47.     {
  48.       x = XEXP (x, 0);
  49.       all_constant = 1;
  50.     }
  51.   else if (code == SYMBOL_REF || code == LABEL_REF)
  52.     all_constant = 1;
  53.  
  54.   /* The interesting case is adding the integer to a sum.
  55.      Look for constant term in the sum and combine
  56.      with C.  For an integer constant term, we make a combined
  57.      integer.  For a constant term that is not an explicit integer,
  58.      we cannot really combine, but group them together anyway.  */
  59.  
  60.   if (GET_CODE (x) == PLUS)
  61.     {
  62.       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  63.     {
  64.       c += INTVAL (XEXP (x, 0));
  65.       x = XEXP (x, 1);
  66.     }
  67.       else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  68.     {
  69.       c += INTVAL (XEXP (x, 1));
  70.       x = XEXP (x, 0);
  71.     }
  72.       else if (CONSTANT_P (XEXP (x, 0)))
  73.     {
  74.       return gen_rtx (PLUS, mode,
  75.               plus_constant (XEXP (x, 0), c),
  76.               XEXP (x, 1));
  77.     }
  78.       else if (CONSTANT_P (XEXP (x, 1)))
  79.     {
  80.       return gen_rtx (PLUS, mode,
  81.               XEXP (x, 0),
  82.               plus_constant (XEXP (x, 1), c));
  83.     }
  84. #ifdef OLD_INDEXING
  85.       /* Detect adding a constant to an indexed address
  86.      of the form (PLUS (MULT (REG) (CONST)) regs-and-constants).
  87.      Keep the (MULT ...) at the top level of addition so that
  88.      the result is still suitable for indexing and constants
  89.      are combined.  */
  90.       else if (GET_CODE (XEXP (x, 0)) == MULT)
  91.     {
  92.       return gen_rtx (PLUS, mode, XEXP (x, 0),
  93.               plus_constant (XEXP (x, 1), c));
  94.     }
  95.       else if (GET_CODE (XEXP (x, 1)) == MULT)
  96.     {
  97.       return gen_rtx (PLUS, mode, plus_constant (XEXP (x, 0), c),
  98.               XEXP (x, 1));
  99.     }
  100. #endif
  101.     }
  102.   if (c != 0)
  103.     x = gen_rtx (PLUS, mode, x, gen_rtx (CONST_INT, VOIDmode, c));
  104.  
  105.   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
  106.     return x;
  107.   else if (all_constant)
  108.     return gen_rtx (CONST, mode, x);
  109.   else
  110.     return x;
  111. }
  112.  
  113. /* If X is a sum, return a new sum like X but lacking any constant terms.
  114.    Add all the removed constant terms into *CONSTPTR.
  115.    X itself is not altered.  The result != X if and only if
  116.    it is not isomorphic to X.  */
  117.  
  118. rtx
  119. eliminate_constant_term (x, constptr)
  120.      rtx x;
  121.      int *constptr;
  122. {
  123.   int c;
  124.   register rtx x0, x1;
  125.  
  126.   if (GET_CODE (x) != PLUS)
  127.     return x;
  128.  
  129.   /* First handle constants appearing at this level explicitly.  */
  130.   if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  131.     {
  132.       *constptr += INTVAL (XEXP (x, 0));
  133.       return eliminate_constant_term (XEXP (x, 1), constptr);
  134.     }
  135.  
  136.   if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  137.     {
  138.       *constptr += INTVAL (XEXP (x, 1));
  139.       return eliminate_constant_term (XEXP (x, 0), constptr);
  140.     }
  141.  
  142.   c = 0;
  143.   x0 = eliminate_constant_term (XEXP (x, 0), &c);
  144.   x1 = eliminate_constant_term (XEXP (x, 1), &c);
  145.   if (x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
  146.     {
  147.       *constptr += c;
  148.       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  149.     }
  150.   return x;
  151. }
  152.  
  153. /* Return an rtx for the size in bytes of the value of EXP.  */
  154.  
  155. rtx
  156. expr_size (exp)
  157.      tree exp;
  158. {
  159.   return expand_expr (size_in_bytes (TREE_TYPE (exp)), 0, SImode, 0);
  160. }
  161.  
  162. /* Not yet really written since C does not need it.  */
  163.  
  164. rtx
  165. lookup_static_chain ()
  166. {
  167.   abort ();
  168. }
  169.  
  170. /* Return a copy of X in which all memory references
  171.    and all constants that involve symbol refs
  172.    have been replaced with new temporary registers.
  173.    Also emit code to load the memory locations and constants
  174.    into those registers.
  175.  
  176.    If X contains no such constants or memory references,
  177.    X itself (not a copy) is returned.
  178.  
  179.    X may contain no arithmetic except addition, subtraction and multiplication.
  180.    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
  181.  
  182. static rtx
  183. break_out_memory_refs (x)
  184.      register rtx x;
  185. {
  186.   if (GET_CODE (x) == MEM || GET_CODE (x) == CONST
  187.       || GET_CODE (x) == SYMBOL_REF)
  188.     {
  189.       register rtx temp = force_reg (Pmode, x);
  190.       mark_reg_pointer (temp);
  191.       x = temp;
  192.     }
  193.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  194.        || GET_CODE (x) == MULT)
  195.     {
  196.       register rtx op0 = break_out_memory_refs (XEXP (x, 0));
  197.       register rtx op1 = break_out_memory_refs (XEXP (x, 1));
  198.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  199.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  200.     }
  201.   return x;
  202. }
  203.  
  204. /* Given a memory address or facsimile X, construct a new address,
  205.    currently equivalent, that is stable: future stores won't change it.
  206.  
  207.    X must be composed of constants, register and memory references
  208.    combined with addition, subtraction and multiplication:
  209.    in other words, just what you can get from expand_expr if sum_ok is 1.
  210.  
  211.    Works by making copies of all regs and memory locations used
  212.    by X and combining them the same way X does.
  213.    You could also stabilize the reference to this address
  214.    by copying the address to a register with copy_to_reg;
  215.    but then you wouldn't get indexed addressing in the reference.  */
  216.  
  217. rtx
  218. copy_all_regs (x)
  219.      register rtx x;
  220. {
  221.   if (GET_CODE (x) == REG)
  222.     {
  223.       if (REGNO (x) != FRAME_POINTER_REGNUM)
  224.     x = copy_to_reg (x);
  225.     }
  226.   else if (GET_CODE (x) == MEM)
  227.     x = copy_to_reg (x);
  228.   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
  229.        || GET_CODE (x) == MULT)
  230.     {
  231.       register rtx op0 = copy_all_regs (XEXP (x, 0));
  232.       register rtx op1 = copy_all_regs (XEXP (x, 1));
  233.       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
  234.     x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
  235.     }
  236.   return x;
  237. }
  238.  
  239. /* Return something equivalent to X but valid as a memory address
  240.    for something of mode MODE.  When X is not itself valid, this
  241.    works by copying X or subexpressions of it into registers.  */
  242.  
  243. rtx
  244. memory_address (mode, x)
  245.      enum machine_mode mode;
  246.      register rtx x;
  247. {
  248.   register rtx oldx;
  249.  
  250.   /* By passing constant addresses thru registers
  251.      we get a chance to cse them.  */
  252.   if (! cse_not_expected && CONSTANT_P (x))
  253.     return force_reg (Pmode, x);
  254.  
  255.   /* Accept a QUEUED that refers to a REG
  256.      even though that isn't a valid address.
  257.      On attempting to put this in an insn we will call protect_from_queue
  258.      which will turn it into a REG, which is valid.  */
  259.   if (GET_CODE (x) == QUEUED
  260.       && GET_CODE (QUEUED_VAR (x)) == REG)
  261.     return x;
  262.  
  263.   /* We get better cse by rejecting indirect addressing at this stage.
  264.      Let the combiner create indirect addresses where appropriate.
  265.      For now, generate the code so that the subexpressions useful to share
  266.      are visible.  But not if cse won't be done!  */
  267.   oldx = x;
  268.   if (! cse_not_expected && GET_CODE (x) != REG)
  269.     x = break_out_memory_refs (x);
  270.  
  271.   /* At this point, any valid address is accepted.  */
  272.   GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
  273.  
  274.   /* If it was valid before but breaking out memory refs invalidated it,
  275.      use it the old way.  */
  276.   if (memory_address_p (mode, oldx))
  277.     goto win2;
  278.  
  279.   /* Perform machine-dependent transformations on X
  280.      in certain cases.  This is not necessary since the code
  281.      below can handle all possible cases, but machine-dependent
  282.      transformations can make better code.  */
  283.   LEGITIMIZE_ADDRESS (x, oldx, mode, win);
  284.  
  285.   /* PLUS and MULT can appear in special ways
  286.      as the result of attempts to make an address usable for indexing.
  287.      Usually they are dealt with by calling force_operand, below.
  288.      But a sum containing constant terms is special
  289.      if removing them makes the sum a valid address:
  290.      then we generate that address in a register
  291.      and index off of it.  We do this because it often makes
  292.      shorter code, and because the addresses thus generated
  293.      in registers often become common subexpressions.  */
  294.   if (GET_CODE (x) == PLUS)
  295.     {
  296.       int constant_term = 0;
  297.       rtx y = eliminate_constant_term (x, &constant_term);
  298.       if (constant_term == 0
  299.       || ! memory_address_p (mode, y))
  300.     return force_operand (x, 0);
  301.  
  302.       y = plus_constant (copy_to_reg (y), constant_term);
  303.       if (! memory_address_p (mode, y))
  304.     return force_operand (x, 0);
  305.       return y;
  306.     }
  307.   if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
  308.     return force_operand (x, 0);
  309.  
  310.   /* Last resort: copy the value to a register, since
  311.      the register is a valid address.  */
  312.   return force_reg (Pmode, x);
  313.  
  314.  win2:
  315.   x = oldx;
  316.  win:
  317.   if (flag_force_addr && optimize && GET_CODE (x) != REG
  318.       /* Don't copy an addr via a reg if it is one of our stack slots.
  319.      If we did, it would cause invalid REG_EQUIV notes for parms.  */
  320.       && ! (GET_CODE (x) == PLUS
  321.         && (XEXP (x, 0) == frame_pointer_rtx
  322.         || XEXP (x, 0) == arg_pointer_rtx)))
  323.     return force_reg (Pmode, x);
  324.   return x;
  325. }
  326.  
  327. /* Like `memory_address' but pretend `flag_force_addr' is 0.  */
  328.  
  329. rtx
  330. memory_address_noforce (mode, x)
  331.      enum machine_mode mode;
  332.      rtx x;
  333. {
  334.   int ambient_force_addr = flag_force_addr;
  335.   rtx val;
  336.  
  337.   flag_force_addr = 0;
  338.   val = memory_address (mode, x);
  339.   flag_force_addr = ambient_force_addr;
  340.   return val;
  341. }
  342.  
  343. /* Return a modified copy of X with its memory address copied
  344.    into a temporary register to protect it from side effects.
  345.    If X is not a MEM, it is returned unchanged (and not copied).
  346.    Perhaps even if it is a MEM, if there is no need to change it.  */
  347.  
  348. rtx
  349. stabilize (x)
  350.      rtx x;
  351. {
  352.   register rtx addr;
  353.   if (GET_CODE (x) != MEM)
  354.     return x;
  355.   addr = XEXP (x, 0);
  356.   if (rtx_unstable_p (addr))
  357.     {
  358.       rtx temp = copy_all_regs (addr);
  359.       rtx mem;
  360.       if (GET_CODE (temp) != REG)
  361.     temp = copy_to_reg (temp);
  362.       mem = gen_rtx (MEM, GET_MODE (x), temp);
  363.       /* Mark returned memref with in_struct
  364.      if it's in an array or structure. */
  365.       if (GET_CODE (addr) == PLUS || MEM_IN_STRUCT_P (x))
  366.     MEM_IN_STRUCT_P (mem) = 1;
  367.       return mem;
  368.     }
  369.   return x;
  370. }
  371.  
  372. /* Copy the value or contents of X to a new temp reg and return that reg.  */
  373.  
  374. rtx
  375. copy_to_reg (x)
  376.      rtx x;
  377. {
  378.   register rtx temp = gen_reg_rtx (GET_MODE (x));
  379.   emit_move_insn (temp, x);
  380.   return temp;
  381. }
  382.  
  383. /* Like copy_to_reg but always give the new register mode Pmode
  384.    in case X is a constant.  */
  385.  
  386. rtx
  387. copy_addr_to_reg (x)
  388.      rtx x;
  389. {
  390.   register rtx temp = gen_reg_rtx (Pmode);
  391.   emit_move_insn (temp, x);
  392.   return temp;
  393. }
  394.  
  395. /* Like copy_to_reg but always give the new register mode MODE
  396.    in case X is a constant.  */
  397.  
  398. rtx
  399. copy_to_mode_reg (mode, x)
  400.      enum machine_mode mode;
  401.      rtx x;
  402. {
  403.   register rtx temp = gen_reg_rtx (mode);
  404.   if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode)
  405.     abort ();
  406.   emit_move_insn (temp, x);
  407.   return temp;
  408. }
  409.  
  410. /* Load X into a register if it is not already one.
  411.    Use mode MODE for the register.
  412.    X should be valid for mode MODE, but it may be a constant which
  413.    is valid for all integer modes; that's why caller must specify MODE.
  414.  
  415.    The caller must not alter the value in the register we return,
  416.    since we mark it as a "constant" register.  */
  417.  
  418. rtx
  419. force_reg (mode, x)
  420.      enum machine_mode mode;
  421.      rtx x;
  422. {
  423.   register rtx temp, insn;
  424.  
  425.   if (GET_CODE (x) == REG)
  426.     return x;
  427.   temp = gen_reg_rtx (mode);
  428.   insn = emit_move_insn (temp, x);
  429.   /* Let optimizers know that TEMP's value never changes
  430.      and that X can be substituted for it.  */
  431.   if (CONSTANT_P (x))
  432.     REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUIV, x, REG_NOTES (insn));
  433.   return temp;
  434. }
  435.  
  436. /* If X is a memory ref, copy its contents to a new temp reg and return
  437.    that reg.  Otherwise, return X.  */
  438.  
  439. rtx
  440. force_not_mem (x)
  441.      rtx x;
  442. {
  443.   register rtx temp;
  444.   if (GET_CODE (x) != MEM)
  445.     return x;
  446.   temp = gen_reg_rtx (GET_MODE (x));
  447.   emit_move_insn (temp, x);
  448.   return temp;
  449. }
  450.  
  451. /* Copy X to TARGET (if it's nonzero and a reg)
  452.    or to a new temp reg and return that reg.  */
  453.  
  454. rtx
  455. copy_to_suggested_reg (x, target)
  456.      rtx x, target;
  457. {
  458.   register rtx temp;
  459.   if (target && GET_CODE (target) == REG)
  460.     temp = target;
  461.   else
  462.     temp = gen_reg_rtx (GET_MODE (x));
  463.   emit_move_insn (temp, x);
  464.   return temp;
  465. }
  466.  
  467. /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
  468.    This pops when ADJUST is positive.  ADJUST need not be constant.  */
  469.  
  470. void
  471. adjust_stack (adjust)
  472.      rtx adjust;
  473. {
  474.   adjust = protect_from_queue (adjust, 0);
  475.  
  476. #ifdef STACK_GROWS_DOWNWARD
  477.   emit_insn (gen_add2_insn (stack_pointer_rtx, adjust));
  478. #else
  479.   emit_insn (gen_sub2_insn (stack_pointer_rtx, adjust));
  480. #endif
  481. }
  482.  
  483. /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
  484.    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
  485.  
  486. void
  487. anti_adjust_stack (adjust)
  488.      rtx adjust;
  489. {
  490.   adjust = protect_from_queue (adjust, 0);
  491.  
  492. #ifdef STACK_GROWS_DOWNWARD
  493.   emit_insn (gen_sub2_insn (stack_pointer_rtx, adjust));
  494. #else
  495.   emit_insn (gen_add2_insn (stack_pointer_rtx, adjust));
  496. #endif
  497. }
  498.  
  499. /* Round the size of a block to be pushed up to the boundary required
  500.    by this machine.  SIZE is the desired size, which need not be constant.  */
  501.  
  502. rtx
  503. round_push (size)
  504.      rtx size;
  505. {
  506. #ifdef STACK_BOUNDARY
  507.   int align = STACK_BOUNDARY / BITS_PER_UNIT;
  508.   if (align == 1)
  509.     ;
  510.   if (GET_CODE (size) == CONST_INT)
  511.     {
  512.       int new = (INTVAL (size) + align - 1) / align * align;
  513.       if (INTVAL (size) != new)
  514.     size = gen_rtx (CONST_INT, VOIDmode, new);
  515.     }
  516.   else
  517.     {
  518.       size = expand_divmod (0, CEIL_DIV_EXPR, Pmode, size,
  519.                 gen_rtx (CONST_INT, VOIDmode, align),
  520.                 0, 1);
  521.       size = expand_mult (Pmode, size,
  522.               gen_rtx (CONST_INT, VOIDmode, align),
  523.               0, 1);
  524.     }
  525. #endif /* STACK_BOUNDARY */
  526.   return size;
  527. }
  528.  
  529. /* Return an rtx representing the register or memory location
  530.    in which a scalar value of data type VALTYPE
  531.    was returned by a function call to function FUNC.
  532.    FUNC is a FUNCTION_DECL node if the precise function is known,
  533.    otherwise 0.  */
  534.  
  535. rtx
  536. hard_function_value (valtype, func)
  537.      tree valtype;
  538.      tree func;
  539. {
  540.   return FUNCTION_VALUE (valtype, func);
  541. }
  542.  
  543. /* Return an rtx representing the register or memory location
  544.    in which a scalar value of mode MODE was returned by a library call.  */
  545.  
  546. rtx
  547. hard_libcall_value (mode)
  548.      enum machine_mode mode;
  549. {
  550.   return LIBCALL_VALUE (mode);
  551. }
  552.