ccmp.c 9.71 KB
Newer Older
1
/* Conditional compare related functions
2
   Copyright (C) 2014-2018 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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
Software Foundation; either version 3, or (at your option) any later
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
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
23
#include "backend.h"
24 25
#include "target.h"
#include "rtl.h"
26 27
#include "tree.h"
#include "gimple.h"
28
#include "memmodel.h"
29
#include "tm_p.h"
30 31 32 33
#include "ssa.h"
#include "expmed.h"
#include "optabs.h"
#include "emit-rtl.h"
34 35 36 37 38
#include "stor-layout.h"
#include "tree-ssa-live.h"
#include "tree-outof-ssa.h"
#include "cfgexpand.h"
#include "ccmp.h"
39
#include "predict.h"
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/* Check whether T is a simple boolean variable or a SSA name
   set by a comparison operator in the same basic block.  */
static bool
ccmp_tree_comparison_p (tree t, basic_block bb)
{
  gimple *g = get_gimple_for_ssa_name (t);
  tree_code tcode;

  /* If we have a boolean variable allow it and generate a compare
     to zero reg when expanding.  */
  if (!g)
    return (TREE_CODE (TREE_TYPE (t)) == BOOLEAN_TYPE);

  /* Check to see if SSA name is set by a comparison operator in
     the same basic block.  */ 
  if (!is_gimple_assign (g))
    return false;
  if (bb != gimple_bb (g))
    return false;
  tcode = gimple_assign_rhs_code (g);
  return TREE_CODE_CLASS (tcode) == tcc_comparison;
}

64 65 66 67 68 69 70 71 72 73 74 75 76
/* The following functions expand conditional compare (CCMP) instructions.
   Here is a short description about the over all algorithm:
     * ccmp_candidate_p is used to identify the CCMP candidate

     * expand_ccmp_expr is the main entry, which calls expand_ccmp_expr_1
       to expand CCMP.

     * expand_ccmp_expr_1 uses a recursive algorithm to expand CCMP.
       It calls two target hooks gen_ccmp_first and gen_ccmp_next to generate
       CCMP instructions.
	 - gen_ccmp_first expands the first compare in CCMP.
	 - gen_ccmp_next expands the following compares.

77 78 79 80
       Both hooks return a comparison with the CC register that is equivalent
       to the value of the gimple comparison.  This is used by the next CCMP
       and in the final conditional store.

81 82
     * We use cstorecc4 pattern to convert the CCmode intermediate to
       the integer mode result that expand_normal is expecting.
83 84 85 86 87 88 89 90 91

   Since the operands of the later compares might clobber CC reg, we do not
   emit the insns during expand.  We keep the insn sequences in two seq

     * prep_seq, which includes all the insns to prepare the operands.
     * gen_seq, which includes all the compare and conditional compares.

   If all checks OK in expand_ccmp_expr, it emits insns in prep_seq, then
   insns in gen_seq.  */
92 93 94

/* Check whether G is a potential conditional compare candidate.  */
static bool
95
ccmp_candidate_p (gimple *g)
96
{
97
  tree rhs;
98
  tree lhs, op0, op1;
99
  gimple *gs0, *gs1;
100 101 102 103 104
  tree_code tcode;
  basic_block bb;

  if (!g)
    return false;
105

106 107
  rhs = gimple_assign_rhs_to_tree (g);
  tcode = TREE_CODE (rhs);
108 109 110 111 112 113
  if (tcode != BIT_AND_EXPR && tcode != BIT_IOR_EXPR)
    return false;

  lhs = gimple_assign_lhs (g);
  op0 = TREE_OPERAND (rhs, 0);
  op1 = TREE_OPERAND (rhs, 1);
114
  bb = gimple_bb (g);
115 116 117 118 119

  if ((TREE_CODE (op0) != SSA_NAME) || (TREE_CODE (op1) != SSA_NAME)
      || !has_single_use (lhs))
    return false;

120 121
  gs0 = get_gimple_for_ssa_name (op0); /* gs0 may be NULL */
  gs1 = get_gimple_for_ssa_name (op1); /* gs1 may be NULL */
122

123
  if (ccmp_tree_comparison_p (op0, bb) && ccmp_tree_comparison_p (op1, bb))
124
    return true;
125
  if (ccmp_tree_comparison_p (op0, bb) && ccmp_candidate_p (gs1))
126
    return true;
127
  if (ccmp_tree_comparison_p (op1, bb) && ccmp_candidate_p (gs0))
128 129
    return true;
  /* We skip ccmp_candidate_p (gs1) && ccmp_candidate_p (gs0) since
130 131
     there is no way to set and maintain the CC flag on both sides of
     the logical operator at the same time.  */
132 133 134
  return false;
}

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
/* Extract the comparison we want to do from the tree.  */
void
get_compare_parts (tree t, int *up, rtx_code *rcode,
		   tree *rhs1, tree *rhs2)
{
  tree_code code;
  gimple *g = get_gimple_for_ssa_name (t);
  if (g)
    {
      *up = TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (g)));
      code = gimple_assign_rhs_code (g);
      *rcode = get_rtx_code (code, *up);
      *rhs1 = gimple_assign_rhs1 (g);
      *rhs2 = gimple_assign_rhs2 (g);
    }
  else
    {
      /* If g is not a comparison operator create a compare to zero.  */
      *up = 1;
      *rcode = NE;
      *rhs1 = t;
      *rhs2 = build_zero_cst (TREE_TYPE (t));
    }
}

160 161 162 163
/* PREV is a comparison with the CC register which represents the
   result of the previous CMP or CCMP.  The function expands the
   next compare based on G which is ANDed/ORed with the previous
   compare depending on CODE.
164
   PREP_SEQ returns all insns to prepare opearands for compare.
165
   GEN_SEQ returns all compare insns.  */
166
static rtx
167
expand_ccmp_next (tree op, tree_code code, rtx prev,
168
		  rtx_insn **prep_seq, rtx_insn **gen_seq)
169
{
170
  rtx_code rcode;
171 172
  int unsignedp;
  tree rhs1, rhs2;
173

174
  get_compare_parts(op, &unsignedp, &rcode, &rhs1, &rhs2);
175
  return targetm.gen_ccmp_next (prep_seq, gen_seq, prev, rcode,
176
				rhs1, rhs2, get_rtx_code (code, 0));
177 178
}

179 180 181 182 183 184 185 186
/* Expand conditional compare gimple G.  A typical CCMP sequence is like:

     CC0 = CMP (a, b);
     CC1 = CCMP (NE (CC0, 0), CMP (e, f));
     ...
     CCn = CCMP (NE (CCn-1, 0), CMP (...));

   hook gen_ccmp_first is used to expand the first compare.
187 188 189
   hook gen_ccmp_next is used to expand the following CCMP.
   PREP_SEQ returns all insns to prepare opearand.
   GEN_SEQ returns all compare insns.  */
190
static rtx
191
expand_ccmp_expr_1 (gimple *g, rtx_insn **prep_seq, rtx_insn **gen_seq)
192 193
{
  tree exp = gimple_assign_rhs_to_tree (g);
194
  tree_code code = TREE_CODE (exp);
195 196 197 198 199 200
  basic_block bb = gimple_bb (g);

  tree op0 = TREE_OPERAND (exp, 0);
  tree op1 = TREE_OPERAND (exp, 1);
  gimple *gs0 = get_gimple_for_ssa_name (op0);
  gimple *gs1 = get_gimple_for_ssa_name (op1);
201 202 203 204
  rtx tmp;

  gcc_assert (code == BIT_AND_EXPR || code == BIT_IOR_EXPR);

205
  if (ccmp_tree_comparison_p (op0, bb))
206
    {
207
      if (ccmp_tree_comparison_p (op1, bb))
208
	{
209 210
	  int unsignedp0, unsignedp1;
	  rtx_code rcode0, rcode1;
211 212
	  tree logical_op0_rhs1, logical_op0_rhs2;
	  tree logical_op1_rhs1, logical_op1_rhs2;
213
	  int speed_p = optimize_insn_for_speed_p ();
214

215
	  rtx tmp2 = NULL_RTX, ret = NULL_RTX, ret2 = NULL_RTX;
216 217
	  unsigned cost1 = MAX_COST;
	  unsigned cost2 = MAX_COST;
218

219 220 221 222 223
	  get_compare_parts (op0, &unsignedp0, &rcode0,
			     &logical_op0_rhs1, &logical_op0_rhs2);

	  get_compare_parts (op1, &unsignedp1, &rcode1,
			     &logical_op1_rhs1, &logical_op1_rhs2);
224

225
	  rtx_insn *prep_seq_1, *gen_seq_1;
226
	  tmp = targetm.gen_ccmp_first (&prep_seq_1, &gen_seq_1, rcode0,
227
					logical_op0_rhs1, logical_op0_rhs2);
228 229
	  if (tmp != NULL)
	    {
230
	      ret = expand_ccmp_next (op1, code, tmp, &prep_seq_1, &gen_seq_1);
231 232
	      cost1 = seq_cost (prep_seq_1, speed_p);
	      cost1 += seq_cost (gen_seq_1, speed_p);
233
	    }
234 235 236 237 238

	  /* FIXME: Temporary workaround for PR69619.
	     Avoid exponential compile time due to expanding gs0 and gs1 twice.
	     If gs0 and gs1 are complex, the cost will be high, so avoid
	     reevaluation if above an arbitrary threshold.  */
239
	  rtx_insn *prep_seq_2, *gen_seq_2;
240 241
	  if (tmp == NULL || cost1 < COSTS_N_INSNS (25))
	    tmp2 = targetm.gen_ccmp_first (&prep_seq_2, &gen_seq_2, rcode1,
242
					   logical_op1_rhs1, logical_op1_rhs2);
243 244
	  if (!tmp && !tmp2)
	    return NULL_RTX;
245 246
	  if (tmp2 != NULL)
	    {
247
	      ret2 = expand_ccmp_next (op0, code, tmp2, &prep_seq_2,
248
				       &gen_seq_2);
249 250
	      cost2 = seq_cost (prep_seq_2, speed_p);
	      cost2 += seq_cost (gen_seq_2, speed_p);
251 252 253 254 255 256 257 258 259 260
	    }
	  if (cost2 < cost1)
	    {
	      *prep_seq = prep_seq_2;
	      *gen_seq = gen_seq_2;
	      return ret2;
	    }
	  *prep_seq = prep_seq_1;
	  *gen_seq = gen_seq_1;
	  return ret;
261 262 263
	}
      else
	{
264 265 266
	  tmp = expand_ccmp_expr_1 (gs1, prep_seq, gen_seq);
	  if (!tmp)
	    return NULL_RTX;
267
	  return expand_ccmp_next (op0, code, tmp, prep_seq, gen_seq);
268 269 270 271 272 273
	}
    }
  else
    {
      gcc_assert (gimple_assign_rhs_code (gs0) == BIT_AND_EXPR
                  || gimple_assign_rhs_code (gs0) == BIT_IOR_EXPR);
274 275 276 277 278
      gcc_assert (ccmp_tree_comparison_p (op1, bb));
      tmp = expand_ccmp_expr_1 (gs0, prep_seq, gen_seq);
      if (!tmp)
	return NULL_RTX;
      return expand_ccmp_next (op1, code, tmp, prep_seq, gen_seq);
279 280 281 282 283
    }

  return NULL_RTX;
}

284
/* Main entry to expand conditional compare statement G.
285 286 287
   Return NULL_RTX if G is not a legal candidate or expand fail.
   Otherwise return the target.  */
rtx
288
expand_ccmp_expr (gimple *g, machine_mode mode)
289 290 291 292 293 294 295 296
{
  rtx_insn *last;
  rtx tmp;

  if (!ccmp_candidate_p (g))
    return NULL_RTX;

  last = get_last_insn ();
297 298

  rtx_insn *prep_seq = NULL, *gen_seq = NULL;
299
  tmp = expand_ccmp_expr_1 (g, &prep_seq, &gen_seq);
300 301 302

  if (tmp)
    {
303 304
      insn_code icode;
      machine_mode cc_mode = CCmode;
305
      rtx_code cmp_code = GET_CODE (tmp);
306

307
#ifdef SELECT_CC_MODE
308
      cc_mode = SELECT_CC_MODE (cmp_code, XEXP (tmp, 0), const0_rtx);
309 310 311 312 313
#endif
      icode = optab_handler (cstore_optab, cc_mode);
      if (icode != CODE_FOR_nothing)
	{
	  rtx target = gen_reg_rtx (mode);
314 315 316 317

	  emit_insn (prep_seq);
	  emit_insn (gen_seq);

318 319
	  tmp = emit_cstore (target, icode, cmp_code, cc_mode, cc_mode,
			     0, XEXP (tmp, 0), const0_rtx, 1, mode);
320 321 322 323 324 325 326 327
	  if (tmp)
	    return tmp;
	}
    }
  /* Clean up.  */
  delete_insns_since (last);
  return NULL_RTX;
}