2018-04-24 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / config / nds32 / nds32-predicates.c
blob5e01430c8e31d4775f8ee20e39ddc962e62bc29f
1 /* Predicate functions of Andes NDS32 cpu for GNU compiler
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* ------------------------------------------------------------------------ */
23 #define IN_TARGET_CODE 1
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "memmodel.h"
33 #include "tm_p.h"
34 #include "optabs.h" /* For GEN_FCN. */
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "tm-constrs.h"
38 #include "insn-attr.h"
40 /* ------------------------------------------------------------------------ */
42 /* A subroutine that checks multiple load and store
43 using consecutive registers.
44 OP is a parallel rtx we would like to check.
45 LOAD_P indicates whether we are checking load operation.
46 PAR_INDEX is starting element of parallel rtx.
47 FIRST_ELT_REGNO is used to tell starting register number.
48 COUNT helps us to check consecutive register numbers. */
49 static bool
50 nds32_consecutive_registers_load_store_p (rtx op,
51 bool load_p,
52 int par_index,
53 int first_elt_regno,
54 int count)
56 int i;
57 int check_regno;
58 rtx elt;
59 rtx elt_reg;
60 rtx elt_mem;
62 for (i = 0; i < count; i++)
64 /* Pick up each element from parallel rtx. */
65 elt = XVECEXP (op, 0, i + par_index);
67 /* If this element is not a 'set' rtx, return false immediately. */
68 if (GET_CODE (elt) != SET)
69 return false;
71 /* Pick up reg and mem of this element. */
72 elt_reg = load_p ? SET_DEST (elt) : SET_SRC (elt);
73 elt_mem = load_p ? SET_SRC (elt) : SET_DEST (elt);
75 /* If elt_reg is not a expected reg rtx, return false. */
76 if (GET_CODE (elt_reg) != REG || GET_MODE (elt_reg) != SImode)
77 return false;
78 /* If elt_mem is not a expected mem rtx, return false. */
79 if (GET_CODE (elt_mem) != MEM || GET_MODE (elt_mem) != SImode)
80 return false;
82 /* The consecutive registers should be in (Rb,Rb+1...Re) order. */
83 check_regno = first_elt_regno + i;
85 /* If the register number is not continuous, return false. */
86 if (REGNO (elt_reg) != (unsigned int) check_regno)
87 return false;
90 return true;
93 /* Function to check whether the OP is a valid load/store operation.
94 This is a helper function for the predicates:
95 'nds32_load_multiple_operation' and 'nds32_store_multiple_operation'
96 in predicates.md file.
98 The OP is supposed to be a parallel rtx.
99 For each element within this parallel rtx:
100 (set (reg) (mem addr)) is the form for load operation.
101 (set (mem addr) (reg)) is the form for store operation.
102 We have to extract reg and mem of every element and
103 check if the information is valid for multiple load/store operation. */
104 bool
105 nds32_valid_multiple_load_store_p (rtx op, bool load_p, bool bim_p)
107 int count;
108 int first_elt_regno;
109 int update_base_elt_idx;
110 int offset;
111 rtx elt;
112 rtx update_base;
114 /* Get the counts of elements in the parallel rtx.
115 Last one is update base register if bim_p.
116 and pick up the first element. */
117 if (bim_p)
119 count = XVECLEN (op, 0) - 1;
120 elt = XVECEXP (op, 0, 1);
122 else
124 count = XVECLEN (op, 0);
125 elt = XVECEXP (op, 0, 0);
128 /* Perform some quick check for the first element in the parallel rtx. */
129 if (GET_CODE (elt) != SET
130 || count <= 1
131 || count > 25)
132 return false;
134 /* Pick up regno of first element for further detail checking.
135 Note that the form is different between load and store operation. */
136 if (load_p)
138 if (GET_CODE (SET_DEST (elt)) != REG
139 || GET_CODE (SET_SRC (elt)) != MEM)
140 return false;
142 first_elt_regno = REGNO (SET_DEST (elt));
144 else
146 if (GET_CODE (SET_SRC (elt)) != REG
147 || GET_CODE (SET_DEST (elt)) != MEM)
148 return false;
150 first_elt_regno = REGNO (SET_SRC (elt));
153 /* Perform detail check for each element.
154 Refer to nds32-multiple.md for more information
155 about following checking.
156 The starting element of parallel rtx is index 0. */
157 if (!nds32_consecutive_registers_load_store_p (op, load_p, bim_p ? 1 : 0,
158 first_elt_regno,
159 count))
160 return false;
162 if (bim_p)
164 update_base_elt_idx = 0;
165 update_base = XVECEXP (op, 0, update_base_elt_idx);
166 if (!REG_P (SET_DEST (update_base)))
167 return false;
168 if (GET_CODE (SET_SRC (update_base)) != PLUS)
169 return false;
170 else
172 offset = count * UNITS_PER_WORD;
173 elt = XEXP (SET_SRC (update_base), 1);
174 if (GET_CODE (elt) != CONST_INT
175 || (INTVAL (elt) != offset))
176 return false;
180 /* Pass all test, this is a valid rtx. */
181 return true;
184 /* Function to check whether the OP is a valid stack push/pop operation.
185 For a valid stack operation, it must satisfy following conditions:
186 1. Consecutive registers push/pop operations.
187 2. Valid $fp/$gp/$lp push/pop operations.
188 3. The last element must be stack adjustment rtx.
189 See the prologue/epilogue implementation for details. */
190 bool
191 nds32_valid_stack_push_pop_p (rtx op, bool push_p)
193 int index;
194 int total_count;
195 int rest_count;
196 int first_regno;
197 int save_fp, save_gp, save_lp;
198 rtx elt;
199 rtx elt_reg;
200 rtx elt_mem;
201 rtx elt_plus;
203 /* Get the counts of elements in the parallel rtx. */
204 total_count = XVECLEN (op, 0);
206 /* Perform some quick check for that every element should be 'set'. */
207 for (index = 0; index < total_count; index++)
209 elt = XVECEXP (op, 0, index);
210 if (GET_CODE (elt) != SET)
211 return false;
214 /* For push operation, the parallel rtx looks like:
215 (parallel [(set (mem (plus (reg:SI SP_REGNUM) (const_int -32)))
216 (reg:SI Rb))
217 (set (mem (plus (reg:SI SP_REGNUM) (const_int -28)))
218 (reg:SI Rb+1))
220 (set (mem (plus (reg:SI SP_REGNUM) (const_int -16)))
221 (reg:SI Re))
222 (set (mem (plus (reg:SI SP_REGNUM) (const_int -12)))
223 (reg:SI FP_REGNUM))
224 (set (mem (plus (reg:SI SP_REGNUM) (const_int -8)))
225 (reg:SI GP_REGNUM))
226 (set (mem (plus (reg:SI SP_REGNUM) (const_int -4)))
227 (reg:SI LP_REGNUM))
228 (set (reg:SI SP_REGNUM)
229 (plus (reg:SI SP_REGNUM) (const_int -32)))])
231 For pop operation, the parallel rtx looks like:
232 (parallel [(set (reg:SI Rb)
233 (mem (reg:SI SP_REGNUM)))
234 (set (reg:SI Rb+1)
235 (mem (plus (reg:SI SP_REGNUM) (const_int 4))))
237 (set (reg:SI Re)
238 (mem (plus (reg:SI SP_REGNUM) (const_int 16))))
239 (set (reg:SI FP_REGNUM)
240 (mem (plus (reg:SI SP_REGNUM) (const_int 20))))
241 (set (reg:SI GP_REGNUM)
242 (mem (plus (reg:SI SP_REGNUM) (const_int 24))))
243 (set (reg:SI LP_REGNUM)
244 (mem (plus (reg:SI SP_REGNUM) (const_int 28))))
245 (set (reg:SI SP_REGNUM)
246 (plus (reg:SI SP_REGNUM) (const_int 32)))]) */
248 /* 1. Consecutive registers push/pop operations.
249 We need to calculate how many registers should be consecutive.
250 The $sp adjustment rtx, $fp push rtx, $gp push rtx,
251 and $lp push rtx are excluded. */
253 /* Detect whether we have $fp, $gp, or $lp in the parallel rtx. */
254 save_fp = reg_mentioned_p (gen_rtx_REG (SImode, FP_REGNUM), op);
255 save_gp = reg_mentioned_p (gen_rtx_REG (SImode, GP_REGNUM), op);
256 save_lp = reg_mentioned_p (gen_rtx_REG (SImode, LP_REGNUM), op);
257 /* Exclude last $sp adjustment rtx. */
258 rest_count = total_count - 1;
259 /* Exclude $fp, $gp, and $lp if they are in the parallel rtx. */
260 if (save_fp)
261 rest_count--;
262 if (save_gp)
263 rest_count--;
264 if (save_lp)
265 rest_count--;
267 if (rest_count > 0)
269 elt = XVECEXP (op, 0, 0);
270 /* Pick up register element. */
271 elt_reg = push_p ? SET_SRC (elt) : SET_DEST (elt);
272 first_regno = REGNO (elt_reg);
274 /* The 'push' operation is a kind of store operation.
275 The 'pop' operation is a kind of load operation.
276 Pass corresponding false/true as second argument (bool load_p).
277 The par_index is supposed to start with index 0. */
278 if (!nds32_consecutive_registers_load_store_p (op,
279 !push_p ? true : false,
281 first_regno,
282 rest_count))
283 return false;
286 /* 2. Valid $fp/$gp/$lp push/pop operations.
287 Remember to set start index for checking them. */
289 /* The rest_count is the start index for checking $fp/$gp/$lp. */
290 index = rest_count;
291 /* If index < 0, this parallel rtx is definitely
292 not a valid stack push/pop operation. */
293 if (index < 0)
294 return false;
296 /* Check $fp/$gp/$lp one by one.
297 We use 'push_p' to pick up reg rtx and mem rtx. */
298 if (save_fp)
300 elt = XVECEXP (op, 0, index);
301 elt_mem = push_p ? SET_DEST (elt) : SET_SRC (elt);
302 elt_reg = push_p ? SET_SRC (elt) : SET_DEST (elt);
303 index++;
305 if (GET_CODE (elt_mem) != MEM
306 || GET_CODE (elt_reg) != REG
307 || REGNO (elt_reg) != FP_REGNUM)
308 return false;
310 if (save_gp)
312 elt = XVECEXP (op, 0, index);
313 elt_mem = push_p ? SET_DEST (elt) : SET_SRC (elt);
314 elt_reg = push_p ? SET_SRC (elt) : SET_DEST (elt);
315 index++;
317 if (GET_CODE (elt_mem) != MEM
318 || GET_CODE (elt_reg) != REG
319 || REGNO (elt_reg) != GP_REGNUM)
320 return false;
322 if (save_lp)
324 elt = XVECEXP (op, 0, index);
325 elt_mem = push_p ? SET_DEST (elt) : SET_SRC (elt);
326 elt_reg = push_p ? SET_SRC (elt) : SET_DEST (elt);
327 index++;
329 if (GET_CODE (elt_mem) != MEM
330 || GET_CODE (elt_reg) != REG
331 || REGNO (elt_reg) != LP_REGNUM)
332 return false;
335 /* 3. The last element must be stack adjustment rtx.
336 Its form of rtx should be:
337 (set (reg:SI SP_REGNUM)
338 (plus (reg:SI SP_REGNUM) (const_int X)))
339 The X could be positive or negative value. */
341 /* Pick up the last element. */
342 elt = XVECEXP (op, 0, total_count - 1);
344 /* Extract its destination and source rtx. */
345 elt_reg = SET_DEST (elt);
346 elt_plus = SET_SRC (elt);
348 /* Check this is (set (stack_reg) (plus stack_reg const)) pattern. */
349 if (GET_CODE (elt_reg) != REG
350 || GET_CODE (elt_plus) != PLUS
351 || REGNO (elt_reg) != SP_REGNUM)
352 return false;
354 /* Pass all test, this is a valid rtx. */
355 return true;
358 /* Function to check if 'bclr' instruction can be used with IVAL. */
360 nds32_can_use_bclr_p (int ival)
362 int one_bit_count;
364 /* Calculate the number of 1-bit of (~ival), if there is only one 1-bit,
365 it means the original ival has only one 0-bit,
366 So it is ok to perform 'bclr' operation. */
368 one_bit_count = popcount_hwi ((unsigned HOST_WIDE_INT) (~ival));
370 /* 'bclr' is a performance extension instruction. */
371 return (TARGET_EXT_PERF && (one_bit_count == 1));
374 /* Function to check if 'bset' instruction can be used with IVAL. */
376 nds32_can_use_bset_p (int ival)
378 int one_bit_count;
380 /* Caculate the number of 1-bit of ival, if there is only one 1-bit,
381 it is ok to perform 'bset' operation. */
383 one_bit_count = popcount_hwi ((unsigned HOST_WIDE_INT) (ival));
385 /* 'bset' is a performance extension instruction. */
386 return (TARGET_EXT_PERF && (one_bit_count == 1));
389 /* Function to check if 'btgl' instruction can be used with IVAL. */
391 nds32_can_use_btgl_p (int ival)
393 int one_bit_count;
395 /* Caculate the number of 1-bit of ival, if there is only one 1-bit,
396 it is ok to perform 'btgl' operation. */
398 one_bit_count = popcount_hwi ((unsigned HOST_WIDE_INT) (ival));
400 /* 'btgl' is a performance extension instruction. */
401 return (TARGET_EXT_PERF && (one_bit_count == 1));
404 /* Function to check if 'bitci' instruction can be used with IVAL. */
406 nds32_can_use_bitci_p (int ival)
408 /* If we are using V3 ISA, we have 'bitci' instruction.
409 Try to see if we can present 'andi' semantic with
410 such 'bit-clear-immediate' operation.
411 For example, 'andi $r0,$r0,0xfffffffc' can be
412 presented with 'bitci $r0,$r0,3'. */
413 return (TARGET_ISA_V3
414 && (ival < 0)
415 && satisfies_constraint_Iu15 (gen_int_mode (~ival, SImode)));
418 /* Return true if is load/store with SYMBOL_REF addressing mode
419 and memory mode is SImode. */
420 bool
421 nds32_symbol_load_store_p (rtx_insn *insn)
423 rtx mem_src = NULL_RTX;
425 switch (get_attr_type (insn))
427 case TYPE_LOAD:
428 mem_src = SET_SRC (PATTERN (insn));
429 break;
430 case TYPE_STORE:
431 mem_src = SET_DEST (PATTERN (insn));
432 break;
433 default:
434 break;
437 /* Find load/store insn with addressing mode is SYMBOL_REF. */
438 if (mem_src != NULL_RTX)
440 if ((GET_CODE (mem_src) == ZERO_EXTEND)
441 || (GET_CODE (mem_src) == SIGN_EXTEND))
442 mem_src = XEXP (mem_src, 0);
444 if ((GET_CODE (XEXP (mem_src, 0)) == SYMBOL_REF)
445 || (GET_CODE (XEXP (mem_src, 0)) == LO_SUM))
446 return true;
449 return false;
452 /* Vaild memory operand for floating-point loads and stores */
453 bool
454 nds32_float_mem_operand_p (rtx op)
456 machine_mode mode = GET_MODE (op);
457 rtx addr = XEXP (op, 0);
459 /* Not support [symbol] [const] memory */
460 if (GET_CODE (addr) == SYMBOL_REF
461 || GET_CODE (addr) == CONST
462 || GET_CODE (addr) == LO_SUM)
463 return false;
465 if (GET_CODE (addr) == PLUS)
467 if (GET_CODE (XEXP (addr, 0)) == SYMBOL_REF)
468 return false;
470 /* Restrict const range: (imm12s << 2) */
471 if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
473 if ((mode == SImode || mode == SFmode)
474 && NDS32_SINGLE_WORD_ALIGN_P (INTVAL (XEXP (addr, 1)))
475 && !satisfies_constraint_Is14 ( XEXP(addr, 1)))
476 return false;
478 if ((mode == DImode || mode == DFmode)
479 && NDS32_DOUBLE_WORD_ALIGN_P (INTVAL (XEXP (addr, 1)))
480 && !satisfies_constraint_Is14 (XEXP (addr, 1)))
481 return false;
485 return true;
489 nds32_cond_move_p (rtx cmp_rtx)
491 machine_mode cmp0_mode = GET_MODE (XEXP (cmp_rtx, 0));
492 machine_mode cmp1_mode = GET_MODE (XEXP (cmp_rtx, 1));
493 enum rtx_code cond = GET_CODE (cmp_rtx);
495 if ((cmp0_mode == DFmode || cmp0_mode == SFmode)
496 && (cmp1_mode == DFmode || cmp1_mode == SFmode)
497 && (cond == ORDERED || cond == UNORDERED))
498 return true;
499 return false;
502 bool
503 nds32_const_double_range_ok_p (rtx op, machine_mode mode,
504 HOST_WIDE_INT lower, HOST_WIDE_INT upper)
506 if (GET_CODE (op) != CONST_DOUBLE
507 || GET_MODE (op) != mode)
508 return false;
510 const REAL_VALUE_TYPE *rv;
511 long val;
513 rv = CONST_DOUBLE_REAL_VALUE (op);
514 REAL_VALUE_TO_TARGET_SINGLE (*rv, val);
516 return val >= lower && val < upper;
518 /* ------------------------------------------------------------------------ */