Line data Source code
1 : /* CRC optimization.
2 : Copyright (C) 2022-2026 Free Software Foundation, Inc.
3 : Contributed by Mariam Arutunian <mariamarutunian@gmail.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
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/>. */
20 :
21 : /* This pass performs CRC optimization. */
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "gimple-iterator.h"
31 : #include "tree-cfg.h"
32 : #include "cfgloop.h"
33 : #include "tree-scalar-evolution.h"
34 : #include "tree-ssa-loop-manip.h"
35 : #include "crc-verification.h"
36 : #include "internal-fn.h"
37 : #include "predict.h"
38 :
39 : class crc_optimization {
40 : private:
41 : /* Record of statements already seen. */
42 : bitmap m_visited_stmts;
43 :
44 : /* Input CRC of the loop. */
45 : tree m_crc_arg;
46 :
47 : /* Input data of the loop. */
48 : tree m_data_arg;
49 :
50 : /* The statement doing shift 1 operation before/after xor operation. */
51 : gimple *m_shift_stmt;
52 :
53 : /* Phi statement from the head of the loop for CRC. */
54 : gphi *m_phi_for_crc;
55 :
56 : /* Phi statement for the data from the head of the loop if exists,
57 : otherwise - nullptr. */
58 : gphi *m_phi_for_data;
59 :
60 : /* The loop, which probably calculates CRC. */
61 : class loop *m_crc_loop;
62 :
63 : /* Polynomial used in CRC calculation. */
64 : unsigned HOST_WIDE_INT m_polynomial;
65 :
66 : /* Depending on the value of M_IS_BIT_FORWARD, may be forward or reversed CRC.
67 : If M_IS_BIT_FORWARD, then it is bit-forward implementation,
68 : otherwise bit-reversed. */
69 : bool m_is_bit_forward;
70 :
71 : /* Sets initial values for CRC analyses. */
72 : void set_initial_values ();
73 :
74 : /* This is the main function that checks whether the given LOOP
75 : calculates CRC and extracts details of the CRC calculation.
76 :
77 : The main idea is to find the innermost loop with 8, 16, 24, 32, 64
78 : iterations and xor instruction (xor is the key operation for naive CRC
79 : calculation). Then, checks that the variable is shifted by one before/after
80 : being used in xor.
81 : Xor must be done under the condition of MSB/LSB being 1. */
82 : bool loop_may_calculate_crc (class loop *loop);
83 :
84 : /* Symbolically executes the loop and checks that LFSR and resulting states
85 : match.
86 : Returns true if it is verified that the loop calculates CRC.
87 : Otherwise, return false.
88 : OUTPUT_CRC is the phi statement which will hold the calculated CRC value
89 : after the loop exit. */
90 : bool loop_calculates_crc (gphi *output_crc,
91 : std::pair<tree, value *> calc_polynom);
92 :
93 : /* Returns true if there is only two conditional blocks in the loop
94 : (one may be for the CRC bit check and the other for the loop counter).
95 : This may filter out some real CRCs, where more than one condition
96 : is checked for the CRC calculation. */
97 : static bool loop_contains_two_conditional_bb (basic_block *loop_bbs,
98 : unsigned loop_num_nodes);
99 :
100 : /* Checks the FUNC_LOOP loop's iteration number.
101 : The loop for CRC calculation may do 8, 16, 24, 32, 64 iterations. */
102 : bool satisfies_crc_loop_iteration_count (class loop *func_loop);
103 :
104 : /* This function checks if the XOR_STMT is used for CRC calculation.
105 : It verifies the presence of a shift operation in the CRC_FUN function
106 : inside the CRC loop. It examines operands of XOR, its dependencies, the
107 : relative position of the shift operation, and the existence of a shift
108 : operation in the opposite branch of conditional statements. It also
109 : checks if XOR is performed when MSB/LSB is one.
110 : If these conditions are met, the XOR operation may be part of a CRC
111 : calculation. The function returns true if these conditions are fulfilled,
112 : otherwise, it returns false. */
113 : bool xor_calculates_crc (function *crc_fun, const gassign *xor_stmt);
114 :
115 : /* Returns true if we can get definition of the VARIABLE, and the definition
116 : it's not outside the loop. Otherwise, returns false. */
117 : bool passes_checks_for_def_chain (tree variable);
118 :
119 : /* This function goes up through the def-use chains of the parameter NAME.
120 : Gathers all the statements within the loop,
121 : from which the variable depends on and adds to the USE_DEFS.
122 : Returns false, if there is a statement that may not exist in the CRC
123 : loop. Otherwise, returns true. */
124 : bool set_defs (tree name, auto_vec<gimple *>& use_defs,
125 : bool keep_only_header_phis);
126 :
127 : /* Set M_PHI_FOR_CRC and M_PHI_FOR_DATA fields.
128 : Returns false if there are more than two (as in CRC calculation only CRC's
129 : and data's phi may exist) or no phi statements in STMTS (at least there
130 : must be CRC's phi).
131 : Otherwise, returns true. */
132 : bool set_crc_and_data_phi (auto_vec<gimple *>& stmts);
133 :
134 : /* Returns true if the variable checked in the condition depends on possible
135 : CRC value. Otherwise, returns false. */
136 : bool cond_depends_on_crc (auto_vec<gimple *>& stmts);
137 :
138 :
139 : /* Checks that the condition is for checking CRC.
140 : Returns true if xor is done under the condition of MSB/LSB being 1, and
141 : the condition's variable and xor-ed variable depend on the same variable.
142 : Otherwise, returns false.
143 : XOR_BB is the basic block, where the xor operation is done.
144 : PRED_BB is the predecessor basic block of the XOR_BB, it is assumed that
145 : the last stmt of PRED_BB checks the condition under which xor is done. */
146 : bool crc_cond (basic_block pred_bb, basic_block xor_bb);
147 :
148 : /* Returns true if xor is done in case the MSB/LSB is 1.
149 : Otherwise, returns false.
150 : In CRC calculation algorithms CRC is xor-ed with the polynomial only
151 : if MSB/LSB is 1.
152 :
153 : PRED_BB is the block containing the condition for the xor.
154 : XOR_BB is the one of the successor blocks of PRED_BB, it is assumed that
155 : CRC is xor-ed with the polynomial in XOR_BB.
156 : COND is the condition, which is checked to satisfy the CRC condition. */
157 : bool is_crc_satisfiable_cond (basic_block pred_bb, basic_block xor_bb,
158 : gcond *cond);
159 :
160 : /* Checks that the variable used in the condition COND is the assumed CRC
161 : (or depends on the assumed CRC).
162 : Also sets data member m_phi_for_data if it isn't set and exists. */
163 : bool is_crc_checked (gcond *cond);
164 :
165 : /* Returns true if condition COND checks MSB/LSB bit is 1.
166 : Otherwise, return false. */
167 : static bool cond_true_is_checked_for_bit_one (const gcond *cond);
168 :
169 : /* Returns opposite block of the XOR_BB from PRED_BB's dest blocks. */
170 : static basic_block get_xor_bb_opposite (basic_block pred_bb,
171 : basic_block xor_bb);
172 :
173 : /* Checks whether the pair of xor's shift exists in the opposite
174 : basic block (OPPOSITE_BB).
175 : If there is a shift and xor in the same block,
176 : then in the opposite block must be another shift. */
177 : bool exists_shift_for_opp_xor_shift (basic_block opposite_bb);
178 :
179 : /* Follow def-use chains of XORED_CRC and return the statement where
180 : XORED_CRC is shifted by one bit position. Only PHI statements are
181 : allowed between XORED_CRC and the shift in the def-use chain.
182 :
183 : If no such statement is found, return NULL. */
184 : gimple *find_shift_after_xor (tree xored_crc);
185 :
186 : /* Returns the statement which does shift 1 operation.
187 : If there is no such statement, returns nullptr.
188 : STMTS - are the statements within the loop before xor operation on
189 : which possible CRC variable depends. */
190 : gimple *find_shift_before_xor (const auto_vec <gimple *> &stmts);
191 :
192 : /* Returns true if ASSIGN_STMT does shift with 1.
193 : Otherwise, returns false. */
194 : bool can_be_crc_shift (gimple *assign_stmt);
195 :
196 : /* Returns true if the operation done in ASSIGN_STMT can occur during CRC
197 : calculation. Otherwise, returns false. */
198 : bool can_not_be_crc_stmt (gimple *assign_stmt);
199 :
200 : /* Returns true if the statement with STMT_CODE may occur in CRC calculation.
201 : Otherwise, returns false. */
202 : static bool is_acceptable_stmt_code (const tree_code &stmt_code);
203 :
204 : /* Prints extracted details of CRC calculation. */
205 : void dump_crc_information ();
206 :
207 : /* Returns true if OUTPUT_CRC's result is the input of m_phi_for_crc.
208 : Otherwise, returns false. */
209 : bool is_output_crc (gphi *output_crc);
210 :
211 : /* Swaps m_phi_for_crc and m_phi_for_data if they are mixed. */
212 : void swap_crc_and_data_if_needed (gphi *output_crc);
213 :
214 : /* Validates CRC and data arguments and
215 : sets them for potential CRC loop replacement.
216 :
217 : The function extracts the CRC and data arguments from PHI nodes and
218 : performs several checks to ensure that the CRC and data are suitable for
219 : replacing the CRC loop with a more efficient implementation.
220 :
221 : Returns true if the arguments are valid and the loop replacement is possible,
222 : false otherwise. */
223 : bool validate_crc_and_data ();
224 :
225 : /* Convert polynomial to unsigned HOST_WIDE_INT. */
226 : void construct_constant_polynomial (value *polynomial);
227 :
228 : /* Returns phi statement which may hold the calculated CRC. */
229 : gphi *get_output_phi ();
230 :
231 : /* Attempts to optimize a CRC calculation calculated by LOOP by replacing it
232 : with a call to an internal function (IFN_CRC or IFN_CRC_REV).
233 : Returns true if replacement succeeded, otherwise false. */
234 : bool optimize_crc_loop (class loop *loop, gphi *output_crc);
235 :
236 : public:
237 234241 : crc_optimization () : m_visited_stmts (BITMAP_ALLOC (NULL)),
238 234241 : m_crc_loop (nullptr), m_polynomial (0)
239 : {
240 234241 : set_initial_values ();
241 234241 : }
242 234241 : ~crc_optimization ()
243 : {
244 234241 : BITMAP_FREE (m_visited_stmts);
245 : }
246 : unsigned int execute (function *fun);
247 : };
248 :
249 : /* Prints extracted details of CRC calculation. */
250 :
251 : void
252 428 : crc_optimization::dump_crc_information ()
253 : {
254 428 : if (dump_file)
255 : {
256 368 : fprintf (dump_file,
257 : "Loop iteration number is " HOST_WIDE_INT_PRINT_UNSIGNED ".\n",
258 368 : tree_to_uhwi (m_crc_loop->nb_iterations));
259 368 : if (m_is_bit_forward)
260 172 : fprintf (dump_file, "Bit forward.\n");
261 : else
262 196 : fprintf (dump_file, "Bit reversed.\n");
263 : }
264 428 : }
265 :
266 : /* Goes down by def-use chain (within the CRC loop) and returns the statement
267 : where variable (dependent on xor-ed variable) is shifted with 1.
268 : Between xor and shift operations only phi statements are allowed.
269 : Otherwise, returns nullptr. */
270 :
271 : gimple *
272 33 : crc_optimization::find_shift_after_xor (tree xored_crc)
273 : {
274 33 : imm_use_iterator imm_iter;
275 33 : use_operand_p use_p;
276 :
277 33 : gcc_assert (TREE_CODE (xored_crc) == SSA_NAME);
278 :
279 33 : unsigned v = SSA_NAME_VERSION (xored_crc);
280 33 : if (bitmap_bit_p (m_visited_stmts, v))
281 : return nullptr;
282 33 : bitmap_set_bit (m_visited_stmts, v);
283 :
284 : /* Iterate through the immediate uses of the XORED_CRC.
285 : If there is a shift return true,
286 : if before shift there is other instruction (besides phi) return false. */
287 43 : FOR_EACH_IMM_USE_FAST (use_p, imm_iter, xored_crc)
288 : {
289 38 : gimple *stmt = USE_STMT (use_p);
290 : // Consider only statements within the loop
291 38 : if (!flow_bb_inside_loop_p (m_crc_loop, gimple_bb (stmt)))
292 5 : continue;
293 :
294 : /* If encountered phi statement, check immediate use of its result.
295 : Otherwise, if encountered assign statement, check whether it does shift
296 : (some other operations are allowed to be between shift and xor). */
297 33 : if (gimple_code (stmt) == GIMPLE_PHI)
298 : {
299 : /* Don't continue searching if encountered the loop's beginning. */
300 21 : if (bb_loop_header_p (gimple_bb (stmt)))
301 5 : continue;
302 :
303 16 : return find_shift_after_xor (gimple_phi_result (stmt));
304 : }
305 12 : else if (is_gimple_assign (stmt))
306 : {
307 : /* Check that stmt does shift by 1.
308 : There are no other statements between
309 : xor and shift, in CRC cases we detect. */
310 12 : if (can_be_crc_shift (stmt))
311 : return stmt;
312 1 : return nullptr;
313 : }
314 0 : else if (!is_gimple_debug (stmt))
315 : return nullptr;
316 28 : }
317 5 : return nullptr;
318 : }
319 :
320 : /* Returns opposite block of the XOR_BB from PRED_BB's dest blocks. */
321 :
322 : basic_block
323 52 : crc_optimization::get_xor_bb_opposite (basic_block pred_bb, basic_block xor_bb)
324 : {
325 : /* Check that the predecessor block has exactly two successors. */
326 52 : if (EDGE_COUNT (pred_bb->succs) != 2)
327 : return nullptr;
328 :
329 52 : edge e0 = EDGE_SUCC (pred_bb, 0);
330 52 : edge e1 = EDGE_SUCC (pred_bb, 1);
331 :
332 : /* Ensure neither outgoing edge is marked as complex. */
333 52 : if ((e0->flags & EDGE_COMPLEX)
334 52 : || (e1->flags & EDGE_COMPLEX))
335 : return nullptr;
336 :
337 : /* Check that one of the successors is indeed XOR_BB. */
338 52 : gcc_assert ((e0->dest == xor_bb)
339 : || (e1->dest == xor_bb));
340 :
341 : /* Return the opposite block of XOR_BB. */
342 52 : if (EDGE_SUCC (pred_bb, 0)->dest != xor_bb)
343 : return e0->dest;
344 52 : return e1->dest;
345 : }
346 :
347 : /* Checks whether the pair of xor's shift exists in the opposite
348 : basic block (OPPOSITE_BB).
349 : If there is a shift and xor in the same block,
350 : then in the opposite block must be another shift. */
351 :
352 : bool
353 52 : crc_optimization::exists_shift_for_opp_xor_shift (basic_block opposite_bb)
354 : {
355 52 : if (!opposite_bb)
356 : return false;
357 :
358 : /* Walk through the instructions of given basic block. */
359 52 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (opposite_bb);
360 53 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
361 : {
362 51 : gimple *stmt = gsi_stmt (bsi);
363 : /* Find assignment statement with shift operation.
364 : Check that shift's direction is same with the shift done
365 : on the path with xor, and it is a shift by one. */
366 51 : if (is_gimple_assign (stmt))
367 : {
368 51 : if ((gimple_assign_rhs_code (stmt)
369 51 : == gimple_assign_rhs_code (m_shift_stmt))
370 51 : && integer_onep (gimple_assign_rhs2 (stmt)))
371 50 : return true;
372 : }
373 : }
374 : /* If there is no shift, return false. */
375 2 : return false;
376 : }
377 :
378 : /* Returns true if condition COND checks MSB/LSB bit is 1.
379 : Otherwise, return false. */
380 :
381 : bool
382 433 : crc_optimization::cond_true_is_checked_for_bit_one (const gcond *cond)
383 : {
384 433 : if (!cond)
385 : return false;
386 :
387 433 : tree rhs = gimple_cond_rhs (cond);
388 433 : enum tree_code code = gimple_cond_code (cond);
389 :
390 : /* If the condition is something == 1 -> return true. */
391 433 : if (code == EQ_EXPR && integer_onep (rhs))
392 : return true;
393 :
394 : /* If the condition is something != 0 or something < 0 -> return true. */
395 431 : if ((code == NE_EXPR || code == LT_EXPR)
396 431 : && integer_zerop (rhs))
397 : return true;
398 :
399 : return false;
400 : }
401 :
402 : /* Returns true if xor is done in case the MSB/LSB is 1.
403 : Otherwise, returns false.
404 : In CRC calculation algorithms CRC is xor-ed with the polynomial only
405 : if MSB/LSB is 1.
406 :
407 : PRED_BB is the block containing the condition for the xor.
408 : XOR_BB is the one of the successor blocks of PRED_BB, it is assumed that CRC
409 : is xor-ed with the polynomial in XOR_BB.
410 : COND is the condition, which is checked to satisfy the CRC condition. */
411 :
412 : bool
413 433 : crc_optimization::is_crc_satisfiable_cond (basic_block pred_bb,
414 : basic_block xor_bb,
415 : gcond *cond)
416 : {
417 433 : edge true_edge;
418 433 : edge false_edge;
419 433 : extract_true_false_edges_from_block (pred_bb, &true_edge, &false_edge);
420 433 : bool cond_is_checked_for_bit_one = cond_true_is_checked_for_bit_one (cond);
421 : /* Check that xor is done in case the MSB/LSB is 1. */
422 433 : if (cond_is_checked_for_bit_one && true_edge->dest == xor_bb)
423 : {
424 432 : if (dump_file && (dump_flags & TDF_DETAILS))
425 294 : fprintf (dump_file, "Xor is done on true branch.\n");
426 : }
427 1 : else if (!cond_is_checked_for_bit_one && false_edge->dest == xor_bb)
428 : {
429 1 : if (dump_file && (dump_flags & TDF_DETAILS))
430 1 : fprintf (dump_file, "Xor is done on false branch.\n");
431 : }
432 : else
433 : {
434 0 : if (dump_file && (dump_flags & TDF_DETAILS))
435 0 : fprintf (dump_file,
436 : "Xor is done if MSB/LSB is not one, not CRC.\n");
437 : return false;
438 : }
439 : return true;
440 : }
441 :
442 : /* Checks that the variable used in the condition COND is the assumed CRC
443 : (or depends on the assumed CRC).
444 : Also sets data member m_phi_for_data if it isn't set and exists. */
445 :
446 : bool
447 433 : crc_optimization::is_crc_checked (gcond *cond)
448 : {
449 433 : tree lhs = gimple_cond_lhs (cond);
450 :
451 : /* As conditions are in canonical form, only left part must be an
452 : SSA_NAME. */
453 433 : if (TREE_CODE (lhs) == SSA_NAME)
454 : {
455 : /* Return whether there is a dependence between if condition's variable
456 : and xor-ed variable. Also set phi statement of data if it is not
457 : determined earlier and is used in the loop. */
458 433 : auto_vec<gimple *> cond_dep_stmts (m_crc_loop->num_nodes);
459 433 : bool set_defs_succeeded = set_defs (lhs, cond_dep_stmts, true);
460 433 : bitmap_clear (m_visited_stmts);
461 433 : if (!set_defs_succeeded)
462 : return false;
463 430 : return cond_depends_on_crc (cond_dep_stmts);
464 433 : }
465 :
466 : /* Return false if there is no dependence between if condition's variable
467 : and xor-ed variable. */
468 : return false;
469 : }
470 :
471 : /* Checks that the condition is for checking CRC.
472 : Returns true if xor is done under the condition of MSB/LSB being 1, and
473 : the condition's variable and xor-ed variable depend on the same variable.
474 : Otherwise, returns false.
475 : XOR_BB is the basic block, where the xor operation is done.
476 : PRED_BB is the predecessor basic block of the XOR_BB, it is assumed that
477 : the last stmt of PRED_BB checks the condition under which xor is done. */
478 :
479 : bool
480 433 : crc_optimization::crc_cond (basic_block pred_bb, basic_block xor_bb)
481 : {
482 : /* Check whether PRED_BB contains condition. We will consider only those
483 : cases when xor is done immediately under the condition. */
484 866 : gcond *cond = safe_dyn_cast<gcond *> (gsi_stmt (gsi_last_bb (pred_bb)));
485 433 : if (!cond)
486 : {
487 0 : if (dump_file && (dump_flags & TDF_DETAILS))
488 0 : fprintf (dump_file, "No condition.\n");
489 : return false;
490 : }
491 :
492 : /* Check that xor is done in case the MSB/LSB is 1. */
493 433 : if (!is_crc_satisfiable_cond (pred_bb, xor_bb, cond))
494 : return false;
495 :
496 : /* Check that CRC's MSB/LSB is checked in the condition.
497 : Set data member if not set and exists. */
498 433 : if (!is_crc_checked (cond))
499 : {
500 5 : if (dump_file && (dump_flags & TDF_DETAILS))
501 0 : fprintf (dump_file, "The condition is not related to the CRC check.\n");
502 : return false;
503 : }
504 : return true;
505 : }
506 :
507 : /* Returns true if the statement with STMT_CODE may occur in CRC calculation.
508 : Otherwise, returns false. */
509 :
510 : bool
511 1502 : crc_optimization::is_acceptable_stmt_code (const tree_code &stmt_code)
512 : {
513 1502 : return (stmt_code == BIT_IOR_EXPR)
514 1502 : || (stmt_code == BIT_AND_EXPR)
515 : || (stmt_code == BIT_XOR_EXPR)
516 : || (stmt_code == MINUS_EXPR)
517 : || (stmt_code == PLUS_EXPR)
518 : || (stmt_code == RSHIFT_EXPR)
519 : || (stmt_code == LSHIFT_EXPR)
520 457 : || (TREE_CODE_CLASS (stmt_code) == tcc_unary);
521 : }
522 :
523 : /* Returns true if ASSIGN_STMT does shift with 1. Otherwise, returns false. */
524 :
525 : bool
526 458 : crc_optimization::can_be_crc_shift (gimple *assign_stmt)
527 : {
528 458 : tree_code stmt_code = gimple_assign_rhs_code (assign_stmt);
529 458 : if (stmt_code == LSHIFT_EXPR || stmt_code == RSHIFT_EXPR)
530 : {
531 440 : m_is_bit_forward = (stmt_code == LSHIFT_EXPR);
532 : /* Check that shift one is done, keep shift statement. */
533 440 : if (integer_onep (gimple_assign_rhs2 (assign_stmt)))
534 : {
535 435 : if (m_shift_stmt)
536 : {
537 0 : if (dump_file && (dump_flags & TDF_DETAILS))
538 0 : fprintf (dump_file,
539 : "Already there is one shift.\n");
540 : return false;
541 : }
542 435 : if (dump_file && (dump_flags & TDF_DETAILS))
543 295 : fprintf (dump_file,
544 : "Found <<1 or >>1.\n");
545 : return true;
546 : }
547 : /* This filters out cases, when xor-ed variable is shifted by values
548 : other than 1. */
549 : }
550 : return false;
551 : }
552 :
553 : /* Returns true if the operation done in ASSIGN_STMT can occur during CRC
554 : calculation. Otherwise, returns false. */
555 :
556 : bool
557 1502 : crc_optimization::can_not_be_crc_stmt (gimple *assign_stmt)
558 : {
559 1502 : tree_code stmt_code = gimple_assign_rhs_code (assign_stmt);
560 1502 : if (!is_acceptable_stmt_code (stmt_code))
561 : {
562 11 : if (dump_file && (dump_flags & TDF_DETAILS))
563 0 : fprintf (dump_file,
564 : "\nStmt with the following operation "
565 : "code %s between xor and shift, "
566 : "may not be CRC.\n", get_tree_code_name (stmt_code));
567 :
568 : return true;
569 : }
570 : return false;
571 : }
572 :
573 : /* Returns true if we can get definition of the VARIABLE, and the definition
574 : is not outside the loop. Otherwise, returns false. */
575 :
576 : bool
577 3851 : crc_optimization::passes_checks_for_def_chain (tree variable)
578 : {
579 3851 : if (!(variable && TREE_CODE (variable) == SSA_NAME))
580 : return false;
581 :
582 : /* No definition chain for default defs. */
583 2628 : if (SSA_NAME_IS_DEFAULT_DEF (variable))
584 : return false;
585 :
586 2628 : gimple *stmt = SSA_NAME_DEF_STMT (variable);
587 :
588 2628 : if (!stmt)
589 : return false;
590 :
591 : /* Don't go outside the loop. */
592 2628 : if (!flow_bb_inside_loop_p (m_crc_loop, gimple_bb (stmt)))
593 : return false;
594 :
595 : return true;
596 : }
597 :
598 : /* This function goes up through the def-use chains of the parameter NAME.
599 : Gathers all the statements within the loop,
600 : from which the variable depends on and adds to the USE_DEFS.
601 : Returns false, if there is a statement that may not exist in the CRC
602 : loop. Otherwise, returns true. */
603 :
604 : bool
605 2386 : crc_optimization::set_defs (tree name, auto_vec<gimple *> &use_defs,
606 : bool keep_only_header_phis = false)
607 : {
608 3851 : if (!passes_checks_for_def_chain (name))
609 : return true;
610 :
611 : /* Don't consider already visited names. */
612 2623 : unsigned v = SSA_NAME_VERSION (name);
613 2623 : if (bitmap_bit_p (m_visited_stmts, v))
614 : return true;
615 2621 : bitmap_set_bit (m_visited_stmts, v);
616 :
617 : /* In CRC implementations with constant polynomial maximum 12 use_def
618 : statements may occur. This limit is based on an analysis of various CRC
619 : implementations as well as theoretical possibilities.
620 : TODO: Find a better solution. */
621 2621 : if (use_defs.length () > 12)
622 : return false;
623 :
624 2621 : gimple *stmt = SSA_NAME_DEF_STMT (name);
625 :
626 : /* If it's not specified to keep only header phi's,
627 : then keep all statements. */
628 2621 : if (!keep_only_header_phis)
629 1025 : use_defs.safe_push (stmt);
630 :
631 : /* If it is an assignment statement,
632 : get and check def-use chain for the first and second operands. */
633 2621 : if (is_a<gassign *> (stmt))
634 : {
635 1502 : if (can_not_be_crc_stmt (stmt))
636 : return false;
637 :
638 1491 : tree ssa1 = gimple_assign_rhs1 (stmt);
639 1491 : tree ssa2 = gimple_assign_rhs2 (stmt);
640 1491 : if (!set_defs (ssa1, use_defs, keep_only_header_phis))
641 : return false;
642 : if (!set_defs (ssa2, use_defs, keep_only_header_phis))
643 : return false;
644 : return true;
645 : }
646 : /* If it's a phi statement, not declared in loop's header,
647 : get and check def-use chain for its values. For the one declared in loop's
648 : header just return true and keep it, if keep_only_header_phis is true. */
649 1119 : else if (is_a<gphi *> (stmt))
650 : {
651 1106 : if (bb_loop_header_p (gimple_bb (stmt)))
652 : {
653 : /* If it's specified to keep only header phi's, keep it. */
654 1106 : if (keep_only_header_phis)
655 656 : use_defs.safe_push (stmt);
656 : }
657 : else
658 : {
659 0 : for (unsigned i = 0; i < gimple_phi_num_args (stmt); i++)
660 : {
661 0 : tree val = gimple_phi_arg_def (stmt, i);
662 0 : if (!set_defs (val, use_defs, keep_only_header_phis))
663 : return false;
664 : }
665 : }
666 : return true;
667 : }
668 :
669 : /* Return false for other than assignment and phi statement. */
670 : return false;
671 : }
672 :
673 : /* Returns the statement which does shift 1 operation.
674 : If there is no such statement, returns nullptr.
675 : STMTS - are the statements within the loop before xor operation on
676 : which possible CRC variable depends. */
677 :
678 : gimple *
679 441 : crc_optimization::find_shift_before_xor (const auto_vec<gimple *> &stmts)
680 : {
681 1409 : for (auto stmt_it = stmts.begin (); stmt_it != stmts.end (); stmt_it++)
682 : {
683 : /* If it is an assignment statement, check that is does shift 1. */
684 467 : if (is_a<gassign *> (*stmt_it))
685 : {
686 446 : if (can_be_crc_shift (*stmt_it))
687 424 : return *stmt_it;
688 : }
689 : }
690 : return nullptr;
691 : }
692 :
693 : /* This function sets M_PHI_FOR_CRC and M_PHI_FOR_DATA fields.
694 : At this step phi nodes for CRC and data may be mixed in places.
695 : It is fixed later with the "swap_crc_and_data_if_needed" function.
696 : The function returns false if there are more than two (as in CRC calculation
697 : only CRC's and data's phi may exist) or no phi statements in STMTS (at least
698 : there must be CRC's phi).
699 : Otherwise, returns true. */
700 :
701 : bool
702 441 : crc_optimization::set_crc_and_data_phi (auto_vec<gimple *> &stmts)
703 : {
704 3283 : for (auto stmt_it = stmts.begin (); stmt_it != stmts.end (); stmt_it++)
705 : {
706 980 : if (is_a<gphi *> (*stmt_it) && bb_loop_header_p (gimple_bb (*stmt_it)))
707 : {
708 450 : if (!m_phi_for_crc)
709 441 : m_phi_for_crc = as_a<gphi *> (*stmt_it);
710 9 : else if (!m_phi_for_data)
711 9 : m_phi_for_data = as_a<gphi *> (*stmt_it);
712 : else
713 : {
714 0 : if (dump_file && (dump_flags & TDF_DETAILS))
715 0 : fprintf (dump_file, "Xor-ed variable depends on more than 2 "
716 : "phis.\n");
717 : return false;
718 : }
719 : }
720 : }
721 441 : return m_phi_for_crc;
722 : }
723 :
724 : /* Returns true if the variable checked in the condition depends on possible
725 : CRC value. Otherwise, returns false. */
726 :
727 : bool
728 430 : crc_optimization::cond_depends_on_crc (auto_vec<gimple *>& stmts)
729 : {
730 430 : bool con_depends_on_crc = false;
731 :
732 : /* The condition may depend maximum on data and CRC phi's. */
733 430 : if (stmts.length () > 2)
734 : return false;
735 :
736 2590 : for (auto stmt_it = stmts.begin (); stmt_it != stmts.end (); stmt_it++)
737 : {
738 656 : if (is_a<gphi *> (*stmt_it) && bb_loop_header_p (gimple_bb (*stmt_it)))
739 : {
740 : /* Check whether variable checked in the condition depends on
741 : M_PHI_FOR_CRC.
742 : Here don't stop the check, to set data if needed. */
743 656 : if (m_phi_for_crc == (*stmt_it))
744 : con_depends_on_crc = true;
745 228 : else if (m_phi_for_data && m_phi_for_data == (*stmt_it))
746 : return true;
747 : /* If M_PHI_FOR_DATA isn't determined, the phi statement maybe for the
748 : data. Just set it. */
749 222 : else if (!m_phi_for_data)
750 222 : m_phi_for_data = as_a<gphi *> (*stmt_it);
751 : }
752 : }
753 : return con_depends_on_crc;
754 : }
755 :
756 : /* Sets initial values for the CRC analysis.
757 : This function is used multiple times during the analyses. */
758 :
759 : void
760 234998 : crc_optimization::set_initial_values ()
761 : {
762 234998 : m_crc_arg = nullptr;
763 234998 : m_data_arg = nullptr;
764 234998 : m_shift_stmt = nullptr;
765 234998 : m_phi_for_crc = nullptr;
766 234998 : m_phi_for_data = nullptr;
767 234998 : m_is_bit_forward = false;
768 234998 : }
769 :
770 : /* This function checks if the XOR_STMT is used for CRC calculation.
771 : It verifies the presence of a shift operation in the CRC_FUN function inside
772 : the CRC loop. It examines operands of XOR, its dependencies, the relative
773 : position of the shift operation, and the existence of a shift operation in
774 : the opposite branch of conditional statements. It also checks if XOR is
775 : performed when MSB/LSB is one.
776 : If these conditions are met, the XOR operation may be part of a CRC
777 : calculation. The function returns true if these conditions are fulfilled,
778 : otherwise, it returns false. */
779 :
780 : bool
781 757 : crc_optimization::xor_calculates_crc (function *crc_fun,
782 : const gassign *xor_stmt)
783 : {
784 757 : tree crc_var = gimple_assign_lhs (xor_stmt);
785 757 : set_initial_values ();
786 757 : tree ssa1 = gimple_assign_rhs1 (xor_stmt);
787 757 : tree ssa2 = gimple_assign_rhs2 (xor_stmt);
788 757 : if (TREE_CODE (ssa2) != INTEGER_CST)
789 : {
790 295 : if (dump_file && (dump_flags & TDF_DETAILS))
791 191 : fprintf (dump_file, "Second operand of the "
792 : "xor statement isn't an integer constant.\n");
793 : return false;
794 : }
795 :
796 : /* Get the statements within the loop on which xor-ed variable depends. */
797 462 : auto_vec<gimple *> xor_dep_stmts (m_crc_loop->num_nodes);
798 462 : bool set_defs_succeeded = set_defs (ssa1, xor_dep_stmts);
799 462 : bitmap_clear (m_visited_stmts);
800 462 : if (!set_defs_succeeded)
801 : {
802 21 : xor_dep_stmts.release ();
803 21 : return false;
804 : }
805 :
806 441 : m_shift_stmt = find_shift_before_xor (xor_dep_stmts);
807 :
808 441 : if (!set_crc_and_data_phi (xor_dep_stmts))
809 : {
810 0 : if (dump_file && (dump_flags & TDF_DETAILS))
811 0 : fprintf (dump_file, "Xor isn't used for CRC calculation.\n");
812 : return false;
813 : }
814 :
815 : /* Check the case when shift is done after xor. */
816 441 : if (!m_shift_stmt)
817 : {
818 17 : if (dump_file && (dump_flags & TDF_DETAILS))
819 9 : fprintf (dump_file, "No shift before xor, trying to find after xor.\n");
820 :
821 17 : m_shift_stmt = find_shift_after_xor (crc_var);
822 17 : bitmap_clear (m_visited_stmts);
823 17 : if (!m_shift_stmt)
824 : return false;
825 : }
826 :
827 : /* Get the basic block where xor operation is done. */
828 435 : basic_block xor_bb = gimple_bb (xor_stmt);
829 :
830 : /* Get the predecessor basic block of xor's block. */
831 897 : if (!single_pred_p (xor_bb))
832 : return false;
833 435 : basic_block block_of_condition = single_pred (xor_bb);
834 :
835 :
836 : /* If the found shift operation is in the same block as the xor operation,
837 : verify whether another shift exists in the opposite branch of the
838 : conditional statements. */
839 435 : if (m_shift_stmt && gimple_bb (m_shift_stmt) == xor_bb)
840 : {
841 52 : basic_block opposite_block = get_xor_bb_opposite (block_of_condition,
842 : xor_bb);
843 52 : if (!exists_shift_for_opp_xor_shift (opposite_block))
844 : {
845 2 : if (dump_file && (dump_flags & TDF_DETAILS))
846 0 : fprintf (dump_file,
847 : "Opposite block doesn't contain shift's pair.\n");
848 : return false;
849 : }
850 : }
851 :
852 : /* Check that xor is done if MSB/LSB is one.
853 : If all checks succeed, then it may be a CRC. */
854 433 : if (crc_cond (block_of_condition, xor_bb))
855 : {
856 428 : if (dump_file)
857 368 : fprintf (dump_file,
858 : "\n%s function maybe contains CRC calculation.\n",
859 : function_name (crc_fun));
860 : return true;
861 : }
862 :
863 : return false;
864 462 : }
865 :
866 : /* Returns true if there is only two conditional blocks in the loop,
867 : one may be for the CRC bit check and the other for the loop counter.
868 : This may filter out some real CRCs, where more than one condition
869 : is checked for the CRC calculation and branch-less CRCs. */
870 :
871 : bool
872 20358 : crc_optimization::loop_contains_two_conditional_bb (basic_block *loop_bbs,
873 : unsigned loop_num_nodes)
874 : {
875 20358 : unsigned conditional_bb_count = 0;
876 : /* Iterate through the loop until the conditional branches count
877 : is below 3. */
878 79701 : for (unsigned i = 0; i < loop_num_nodes && conditional_bb_count <= 2; i++)
879 : {
880 59343 : basic_block bb = loop_bbs[i];
881 59343 : if (!single_succ_p (bb))
882 27618 : conditional_bb_count++;
883 : }
884 20358 : return conditional_bb_count == 2;
885 : }
886 :
887 : /* Checks the FUNC_LOOP loop's iteration number.
888 : The loop for CRC calculation may do 8, 16, 24, 32, 64 iterations. */
889 :
890 : bool
891 528788 : crc_optimization::satisfies_crc_loop_iteration_count (class loop *func_loop)
892 : {
893 : /* Calculate the number of times the latch of the loop is executed.
894 : The function sets NB_ITERATIONS field of the loop. */
895 528788 : number_of_latch_executions (func_loop);
896 528788 : tree n_inters = func_loop->nb_iterations;
897 528788 : if (n_inters == NULL_TREE || n_inters == chrec_dont_know)
898 : {
899 269177 : if (dump_file && (dump_flags & TDF_DETAILS))
900 57 : fprintf (dump_file,
901 : "Loop iteration number is chrec_dont_know.\n");
902 : return false;
903 :
904 : }
905 259611 : else if (tree_fits_uhwi_p (n_inters))
906 : {
907 164428 : unsigned HOST_WIDE_INT
908 164428 : loop_iteration_number = tree_to_uhwi (n_inters);
909 164428 : if (dump_file && (dump_flags & TDF_DETAILS))
910 330 : fprintf (dump_file, "Loop iteration number is "
911 : HOST_WIDE_INT_PRINT_UNSIGNED ".\n", loop_iteration_number);
912 :
913 164428 : if ((loop_iteration_number == 7 || loop_iteration_number == 15
914 : || loop_iteration_number == 23 || loop_iteration_number == 31
915 : || loop_iteration_number == 63))
916 : return true;
917 : }
918 239253 : if (stderr && (dump_flags & TDF_DETAILS))
919 30 : fprintf (dump_file, "Loop iteration number isn't a constant.\n");
920 : return false;
921 : }
922 :
923 : /* This is the main function that checks whether the given LOOP
924 : calculates CRC and extracts details of the CRC calculation.
925 :
926 : The main idea is to find the innermost loop with 8, 16, 24, 32, 64
927 : iterations and xor instruction (xor is the key operation for naive CRC
928 : calculation). Then, checks that the variable is shifted by one before/after
929 : being used in xor.
930 : Xor must be done under the condition of MSB/LSB being 1. */
931 :
932 : bool
933 528788 : crc_optimization::loop_may_calculate_crc (class loop *loop)
934 : {
935 : /* Only examine innermost loops. */
936 528788 : if (!loop || loop->inner)
937 : return false;
938 :
939 528788 : if (!satisfies_crc_loop_iteration_count (loop))
940 : return false;
941 :
942 20358 : m_crc_loop = loop;
943 20358 : basic_block *loop_bbs = get_loop_body_in_dom_order (m_crc_loop);
944 :
945 : /* Filter out the cases, which don't have exactly two conditions in the loop.
946 : One for the CRC bit check, the other for the loop counter. */
947 20358 : if (!loop_contains_two_conditional_bb (loop_bbs, m_crc_loop->num_nodes))
948 : {
949 15452 : if (dump_file && (dump_flags & TDF_DETAILS))
950 6 : fprintf (dump_file,
951 : "The number of conditional "
952 : "branches in the loop isn't 2.\n");
953 15452 : free (loop_bbs);
954 15452 : return false;
955 : }
956 :
957 : unsigned short checked_xor_count = 0;
958 : /* Walk bbs of the loop. */
959 27657 : for (unsigned int i = 0; i < m_crc_loop->num_nodes; i++)
960 : {
961 23196 : basic_block bb = loop_bbs[i];
962 : /* Walk instructions of the bb. */
963 23196 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
964 76238 : !gsi_end_p (bsi); gsi_next_nondebug (&bsi))
965 : {
966 95667 : auto stmt = dyn_cast<gassign *> (gsi_stmt (bsi));
967 : /* If there is an xor instruction,
968 : check that it is calculating CRC. */
969 54583 : if (stmt && gimple_assign_rhs_code (stmt) == BIT_XOR_EXPR)
970 : {
971 757 : if (dump_file && (dump_flags & TDF_DETAILS))
972 486 : fprintf (dump_file,
973 : "Found xor, "
974 : "checking whether it is for CRC calculation.\n");
975 :
976 757 : if (xor_calculates_crc (cfun, stmt))
977 : {
978 428 : dump_crc_information ();
979 428 : free (loop_bbs);
980 445 : return true;
981 : }
982 :
983 329 : if (++checked_xor_count == 2)
984 : {
985 17 : free (loop_bbs);
986 17 : return false;
987 : }
988 : }
989 : }
990 : }
991 4461 : free (loop_bbs);
992 4461 : return false;
993 : }
994 :
995 : /* Symbolically executes the loop and checks that LFSR and resulting states
996 : match.
997 : Returns true if it is verified that the loop calculates CRC.
998 : Otherwise, return false.
999 : OUTPUT_CRC is the phi statement which will hold the calculated CRC value
1000 : after the loop exit. */
1001 :
1002 : bool
1003 385 : crc_optimization::loop_calculates_crc (gphi *output_crc,
1004 : std::pair<tree, value *> calc_polynom)
1005 : {
1006 : /* Create LFSR state using extracted polynomial. */
1007 385 : value * lfsr = state::create_lfsr (calc_polynom.first, calc_polynom.second,
1008 : m_is_bit_forward);
1009 385 : if (!lfsr)
1010 : {
1011 25 : if (dump_file && (dump_flags & TDF_DETAILS))
1012 20 : fprintf (dump_file, "Couldn't create LFSR!\n");
1013 : return false;
1014 : }
1015 :
1016 360 : if (dump_file && (dump_flags & TDF_DETAILS))
1017 : {
1018 233 : fprintf (dump_file, "\nLFSR value is \n");
1019 233 : state::print_value (lfsr);
1020 : }
1021 :
1022 : /* Execute the loop with symbolic values
1023 : (symbolic value is assigned to the variable when its value isn't known)
1024 : to keep states, for further comparison. */
1025 360 : bool is_crc = true;
1026 360 : crc_symbolic_execution loop_executor (m_crc_loop, output_crc);
1027 6561 : while (!loop_executor.is_last_iteration ())
1028 : {
1029 5852 : if (!loop_executor.symb_execute_crc_loop ())
1030 : {
1031 0 : if (dump_file)
1032 0 : fprintf (dump_file, "\nCRC verification didn't succeed "
1033 : "during symbolic execution!\n");
1034 : is_crc = false;
1035 : break;
1036 : }
1037 :
1038 : /* Check whether LFSR and obtained states are same. */
1039 5852 : tree calculated_crc = PHI_ARG_DEF_FROM_EDGE (output_crc,
1040 : single_exit (m_crc_loop));
1041 5852 : if (!all_states_match_lfsr (lfsr, m_is_bit_forward, calculated_crc,
1042 : loop_executor.get_final_states ()))
1043 : {
1044 11 : if (dump_file && (dump_flags & TDF_DETAILS))
1045 9 : fprintf (dump_file, "Returned state and LFSR differ.\n");
1046 : is_crc = false;
1047 : break;
1048 : }
1049 : }
1050 360 : delete lfsr;
1051 360 : return is_crc;
1052 360 : }
1053 :
1054 : /* Returns true if OUTPUT_CRC's result is the input of M_PHI_FOR_CRC.
1055 : Otherwise, returns false. */
1056 :
1057 : bool
1058 409 : crc_optimization::is_output_crc (gphi *output_crc)
1059 : {
1060 409 : tree crc_of_exit
1061 409 : = PHI_ARG_DEF_FROM_EDGE (output_crc, single_exit (m_crc_loop));
1062 409 : tree crc_of_latch
1063 409 : = PHI_ARG_DEF_FROM_EDGE (m_phi_for_crc, loop_latch_edge (m_crc_loop));
1064 409 : if (crc_of_exit == crc_of_latch)
1065 : {
1066 406 : if (dump_file && (dump_flags & TDF_DETAILS))
1067 : {
1068 274 : fprintf (dump_file, "Output CRC is ");
1069 274 : print_gimple_expr (dump_file, (gimple *) output_crc, dump_flags);
1070 274 : fprintf (dump_file, "\n");
1071 : }
1072 : return true;
1073 : }
1074 : else
1075 : {
1076 3 : if (dump_file && (dump_flags & TDF_DETAILS))
1077 3 : fprintf (dump_file, "Output CRC and determined input CRC "
1078 : "differ.\n");
1079 : return false;
1080 : }
1081 : }
1082 :
1083 : /* Swaps M_PHI_FOR_CRC and M_PHI_FOR_DATA if they are mixed. */
1084 :
1085 : void
1086 409 : crc_optimization::swap_crc_and_data_if_needed (gphi *output_crc)
1087 : {
1088 409 : tree crc_of_exit
1089 409 : = PHI_ARG_DEF_FROM_EDGE (output_crc, single_exit (m_crc_loop));
1090 409 : edge crc_loop_latch = loop_latch_edge (m_crc_loop);
1091 409 : if (crc_of_exit != PHI_ARG_DEF_FROM_EDGE (m_phi_for_crc, crc_loop_latch))
1092 : {
1093 9 : if (m_phi_for_data
1094 9 : && crc_of_exit == PHI_ARG_DEF_FROM_EDGE (m_phi_for_data,
1095 : crc_loop_latch))
1096 : {
1097 6 : std::swap (m_phi_for_crc, m_phi_for_data);
1098 : }
1099 : }
1100 409 : }
1101 :
1102 : /* Validates CRC and data arguments and
1103 : sets them for potential CRC loop replacement.
1104 :
1105 : The function extracts the CRC and data arguments from PHI nodes and
1106 : performs several checks to ensure that the CRC and data are suitable for
1107 : replacing the CRC loop with a more efficient implementation.
1108 :
1109 : Returns true if the arguments are valid and the loop replacement is possible,
1110 : false otherwise. */
1111 :
1112 406 : bool crc_optimization::validate_crc_and_data ()
1113 : {
1114 : /* Set m_crc_arg and check if fits in word_mode. */
1115 406 : gcc_assert (m_phi_for_crc);
1116 406 : m_crc_arg = PHI_ARG_DEF_FROM_EDGE (m_phi_for_crc,
1117 : loop_preheader_edge (m_crc_loop));
1118 406 : gcc_assert (m_crc_arg);
1119 :
1120 406 : unsigned HOST_WIDE_INT
1121 406 : data_size = tree_to_uhwi (m_crc_loop->nb_iterations) + 1;
1122 : /* We don't support the case where data is larger than the CRC. */
1123 406 : if (TYPE_PRECISION (TREE_TYPE (m_crc_arg)) < data_size)
1124 : return false;
1125 :
1126 : /* Set m_data_arg if a PHI node for data exists,
1127 : and check its size against loop iterations.
1128 : This is the case when data and CRC are XOR-ed in the loop. */
1129 406 : if (m_phi_for_data)
1130 : {
1131 211 : if (dump_file && (dump_flags & TDF_DETAILS))
1132 169 : fprintf (dump_file,
1133 : "Data and CRC are xor-ed in the for loop. Initializing data "
1134 : "with its value.\n");
1135 211 : m_data_arg = PHI_ARG_DEF_FROM_EDGE (m_phi_for_data,
1136 : loop_preheader_edge (m_crc_loop));
1137 211 : gcc_assert (m_data_arg);
1138 211 : if (TYPE_PRECISION (TREE_TYPE (m_data_arg)) != data_size)
1139 : {
1140 9 : if (dump_file && (dump_flags & TDF_DETAILS))
1141 9 : fprintf (dump_file,
1142 : "Loop iteration number and data's size differ.\n");
1143 : return false;
1144 : }
1145 : return true;
1146 : }
1147 : return true;
1148 : }
1149 :
1150 : /* Convert polynomial to unsigned HOST_WIDE_INT. */
1151 :
1152 : void
1153 385 : crc_optimization::construct_constant_polynomial (value *polynomial)
1154 : {
1155 385 : m_polynomial = 0;
1156 12337 : for (unsigned i = 0; i < (*polynomial).length (); i++)
1157 : {
1158 11952 : value_bit *const_bit;
1159 11952 : if (m_is_bit_forward)
1160 4856 : const_bit = (*polynomial)[(*polynomial).length () - 1 - i];
1161 : else
1162 7096 : const_bit = (*polynomial)[i];
1163 11952 : m_polynomial <<= 1;
1164 19456 : m_polynomial ^= (as_a<bit *> (const_bit))->get_val () ? 1 : 0;
1165 : }
1166 385 : }
1167 :
1168 : /* Returns phi statement which may hold the calculated CRC. */
1169 :
1170 : gphi *
1171 428 : crc_optimization::get_output_phi ()
1172 : {
1173 428 : edge loop_exit = single_exit (m_crc_loop);
1174 428 : if (!loop_exit)
1175 : {
1176 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1177 0 : fprintf (dump_file, "The loop doesn't have single exit.\n");
1178 : return nullptr;
1179 : }
1180 428 : basic_block bb = loop_exit->dest;
1181 428 : gphi *output_crc = nullptr;
1182 428 : int phi_count = 0;
1183 :
1184 : /* We are only interested in cases when there is only one phi at the
1185 : loop exit, and that phi can potentially represent the CRC.
1186 : If there are other phis present, it indicates that additional values are
1187 : being calculated within the loop that are used outside it. */
1188 895 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1189 467 : gsi_next (&gsi))
1190 : {
1191 486 : tree phi_result = gimple_phi_result (gsi.phi ());
1192 :
1193 : /* Don't consider virtual operands. */
1194 972 : if (!virtual_operand_p (phi_result))
1195 : {
1196 447 : if (phi_count < 1)
1197 : {
1198 : output_crc = gsi.phi ();
1199 : phi_count++;
1200 : }
1201 : else
1202 : {
1203 19 : if (dump_file && (dump_flags & TDF_DETAILS))
1204 18 : fprintf (dump_file, "There is more than one output phi.\n");
1205 19 : return nullptr;
1206 : }
1207 : }
1208 : }
1209 :
1210 409 : if (output_crc)
1211 : {
1212 409 : if (gimple_phi_num_args (output_crc) == 1)
1213 : return output_crc;
1214 : }
1215 :
1216 0 : if (dump_file && (dump_flags & TDF_DETAILS))
1217 0 : fprintf (dump_file, "Couldn't determine output CRC.\n");
1218 : return nullptr;
1219 : }
1220 :
1221 : /* Attempts to optimize a CRC calculation loop by replacing it with a call to
1222 : an internal function (IFN_CRC or IFN_CRC_REV).
1223 : Returns true if replacement succeeded, otherwise false. */
1224 :
1225 : bool
1226 349 : crc_optimization::optimize_crc_loop (class loop *loop, gphi *output_crc)
1227 : {
1228 349 : if (!output_crc)
1229 : {
1230 0 : if (dump_file)
1231 0 : fprintf (dump_file, "Couldn't determine output CRC.\n");
1232 : return false;
1233 : }
1234 :
1235 349 : if (!m_data_arg)
1236 : {
1237 169 : if (dump_file && (dump_flags & TDF_DETAILS))
1238 82 : fprintf (dump_file,
1239 : "Data and CRC are xor-ed before for loop. Initializing data "
1240 : "with 0.\n");
1241 : /* Create a new variable for the data.
1242 : Determine the data's size with the loop iteration count. */
1243 169 : unsigned HOST_WIDE_INT
1244 169 : data_size = tree_to_uhwi (m_crc_loop->nb_iterations) + 1;
1245 169 : tree type = build_nonstandard_integer_type (data_size, 1);
1246 : /* For the CRC calculation, it doesn't matter CRC is calculated for the
1247 : (CRC^data, 0) or (CRC, data). */
1248 169 : m_data_arg = build_int_cstu (type, 0);
1249 : }
1250 :
1251 : /* Build tree node for the polynomial from its constant value. */
1252 349 : tree polynomial_arg = build_int_cstu (TREE_TYPE (m_crc_arg), m_polynomial);
1253 349 : gcc_assert (polynomial_arg);
1254 :
1255 349 : internal_fn ifn;
1256 349 : if (m_is_bit_forward)
1257 : ifn = IFN_CRC;
1258 : else
1259 184 : ifn = IFN_CRC_REV;
1260 :
1261 349 : tree phi_result = gimple_phi_result (output_crc);
1262 349 : location_t loc;
1263 349 : loc = EXPR_LOCATION (phi_result);
1264 :
1265 : /* If the target does not have an expansion for CRC optimally then the table
1266 : based lookup will need at least 255-bytes of RODATA and won't be smaller
1267 : than the original code itself. */
1268 349 : if (!optimize_loop_for_speed_p (loop))
1269 : {
1270 2 : tree data_type = TREE_TYPE (m_data_arg);
1271 2 : tree result_type = TREE_TYPE (phi_result);
1272 2 : auto ty_pair = tree_pair (data_type, result_type);
1273 2 : if (!direct_internal_fn_supported_p (ifn, ty_pair, OPTIMIZE_FOR_BOTH))
1274 2 : return false;
1275 : }
1276 :
1277 347 : basic_block *bbs = get_loop_body (loop);
1278 347 : reset_debug_uses_outside_loop (loop, bbs, loop->num_nodes);
1279 347 : free (bbs);
1280 :
1281 : /* Add IFN call and write the return value in the phi_result. */
1282 347 : gcall *call = gimple_build_call_internal (ifn, 3, m_crc_arg, m_data_arg,
1283 : polynomial_arg);
1284 :
1285 347 : gimple_call_set_lhs (call, phi_result);
1286 347 : gimple_set_location (call, loc);
1287 347 : gimple_stmt_iterator si = gsi_after_labels (gimple_bb (output_crc));
1288 347 : gsi_insert_before (&si, call, GSI_SAME_STMT);
1289 :
1290 : /* Remove phi statement, which was holding CRC result. */
1291 347 : gimple_stmt_iterator tmp_gsi = gsi_for_stmt (output_crc);
1292 347 : remove_phi_node (&tmp_gsi, false);
1293 :
1294 : /* Alter the exit condition of the loop to always exit. */
1295 347 : gcond* loop_exit_cond = get_loop_exit_condition (m_crc_loop);
1296 347 : if (single_exit (m_crc_loop)->flags & EDGE_TRUE_VALUE)
1297 3 : gimple_cond_make_true (loop_exit_cond);
1298 : else
1299 344 : gimple_cond_make_false (loop_exit_cond);
1300 347 : update_stmt (loop_exit_cond);
1301 347 : return true;
1302 : }
1303 :
1304 : unsigned int
1305 234241 : crc_optimization::execute (function *fun)
1306 : {
1307 234241 : if (dump_file && (dump_flags & TDF_DETAILS))
1308 363 : fprintf (dump_file, "\nExamining %s function.\n",
1309 : function_name (fun));
1310 :
1311 468482 : if (number_of_loops (fun) <= 1)
1312 : return 0;
1313 :
1314 : /* Get loops of the function. */
1315 234241 : auto loop_list = loops_list (fun, LI_ONLY_INNERMOST);
1316 1231432 : for (auto loop: loop_list)
1317 : {
1318 : /* Perform initial checks to filter out non-CRCs. */
1319 528788 : if (loop_may_calculate_crc (loop))
1320 : {
1321 : /* Get the phi which will hold the calculated CRC. */
1322 428 : gphi *output_crc = get_output_phi ();
1323 428 : if (!output_crc)
1324 : break;
1325 :
1326 409 : swap_crc_and_data_if_needed (output_crc);
1327 409 : if (!is_output_crc (output_crc))
1328 : break;
1329 406 : if (!validate_crc_and_data ())
1330 : break;
1331 :
1332 397 : edge loop_latch = loop_latch_edge (m_crc_loop);
1333 397 : tree calced_crc = PHI_ARG_DEF_FROM_EDGE (m_phi_for_crc, loop_latch);
1334 397 : crc_symbolic_execution execute_loop (m_crc_loop, nullptr);
1335 : /* Execute the loop assigning specific values to CRC and data
1336 : for extracting the polynomial. */
1337 397 : std::pair <tree, value *>
1338 397 : calc_polynom = execute_loop.extract_polynomial (m_phi_for_crc,
1339 : m_phi_for_data,
1340 : calced_crc,
1341 : m_is_bit_forward);
1342 :
1343 397 : value *polynom_value = calc_polynom.second;
1344 : /* Stop analysis if we couldn't get the polynomial's value. */
1345 397 : if (!polynom_value)
1346 : break;
1347 :
1348 : /* Stop analysis in case optimize_size is specified
1349 : and table-based would be generated. This check is only needed for
1350 : TARGET_CRC case, as polynomial's value isn't known in the
1351 : beginning. */
1352 385 : construct_constant_polynomial (polynom_value);
1353 :
1354 385 : if (!loop_calculates_crc (output_crc, calc_polynom))
1355 : break;
1356 :
1357 349 : if (dump_file)
1358 292 : fprintf (dump_file, "The loop with %d header BB index "
1359 292 : "calculates CRC!\n", m_crc_loop->header->index);
1360 :
1361 349 : if (!optimize_crc_loop (loop, output_crc))
1362 : {
1363 2 : if (dump_file)
1364 0 : fprintf (dump_file, "Couldn't generate faster CRC code.\n");
1365 : }
1366 397 : }
1367 : }
1368 234241 : return 0;
1369 234241 : }
1370 :
1371 : namespace
1372 : {
1373 :
1374 : const pass_data pass_data_crc_optimization
1375 : = {
1376 : GIMPLE_PASS, /* type */
1377 : "crc", /* name */
1378 : OPTGROUP_NONE, /* optinfo_flags */
1379 : TV_GIMPLE_CRC_OPTIMIZATION, /* tv_id */
1380 : (PROP_cfg | PROP_ssa), /* properties_required */
1381 : 0, /* properties_provided */
1382 : 0, /* properties_destroyed */
1383 : 0, /* todo_flags_start */
1384 : 0, /* todo_flags_finish */
1385 : };
1386 :
1387 : class pass_crc_optimization : public gimple_opt_pass {
1388 : public:
1389 302985 : pass_crc_optimization (gcc::context *ctxt)
1390 605970 : : gimple_opt_pass (pass_data_crc_optimization, ctxt)
1391 : {}
1392 :
1393 : /* opt_pass methods: */
1394 251589 : virtual bool gate (function *)
1395 : {
1396 251589 : return flag_optimize_crc;
1397 : }
1398 :
1399 : virtual unsigned int execute (function *);
1400 :
1401 : }; // class pass_crc_optimization
1402 :
1403 : unsigned int
1404 234241 : pass_crc_optimization::execute (function *fun)
1405 : {
1406 234241 : return crc_optimization ().execute (fun);
1407 : }
1408 :
1409 : } // anon namespace
1410 :
1411 : gimple_opt_pass *
1412 302985 : make_pass_crc_optimization (gcc::context *ctxt)
1413 : {
1414 302985 : return new pass_crc_optimization (ctxt);
1415 : }
|