1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
|
/* CRC optimization.
Copyright (C) 2006-2022 Free Software Foundation, Inc.
Contributed by Mariam Arutunian <mariamarutunian@gmail.com>
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/>. */
/* This pass performs crc optimization. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "ssa.h"
#include "gimple-iterator.h"
#include "tree-cfg.h"
#include "tree-ssa-loop-niter.h"
#include "cfgloop.h"
#include "gimple-range.h"
#include "tree-scalar-evolution.h"
#include "hwint.h"
#include "crc_verification.h"
#include "internal-fn.h"
#include "tree-into-ssa.h"
#include "tree-ssa-loop-manip.h"
#include "predict.h"
#include "cfghooks.h"
#include "tree-ssa.h"
#include "tree-ssa-live.h"
#include "tree-dfa.h"
#include "dominance.h"
#include "tree-ssa-dce.h"
#include "tree-cfgcleanup.h"
class crc_optimization {
private:
/* shift_before_xor will contain the statement doing shift one operation
(if exists and satisfies the CRC) before xor operation. */
gimple *shift_before_xor;
/* shift_after_xor will contain the statement doing shift one operation
(if shift_before_xor doesn't exist and satisfies the CRC) after xor
operation on the same variable. */
gimple *shift_after_xor;
/* Phi statement, result may be the crc variable. */
gphi *first_phi_for_crc;
/* Sometimes polynomial may not be constant
and xor-ed variable may depend on two variables.
The result of phi statement may contain the polynomial. */
gphi *second_phi_for_crc;
/* Phi statement, result maybe data (if exists). */
gphi *data;
/* The loop, which probably calculates CRC. */
class loop *crc_loop;
unsigned HOST_WIDE_INT loop_iteration_number;
/* Function's return value size. */
unsigned HOST_WIDE_INT return_size;
/* Depending on the value of is_left_shift,
may be forward or reversed CRC.
If is_left_shift, then it is bit_forward implementations,
otherwise bit_reversed. */
bool is_left_shift;
/* Will be true, if crc variable and if condition depend on each other. */
bool crc_if_dep;
/* If the value is false, then xor operation isn't for CRC calculation,
otherwise it may calculate CRC. */
bool clean_xor_maybe_crc;
void set_initial_values ()
{
shift_before_xor = nullptr;
shift_after_xor = nullptr;
first_phi_for_crc = nullptr;
second_phi_for_crc = nullptr;
data = nullptr;
is_left_shift = false;
crc_if_dep = false;
clean_xor_maybe_crc = true;
}
/* This is the main function which checks whether given function
calculates CRC and extracts the details of the CRC calculation.
The main idea is to find innermost loop with 8, 16, 24, 32 iterations.
Find xor in the loop (xor is the key operation for naive crc calculation).
Check that before/after being xor-ed the variable is shifted by one.
Xor must be done under condition of MSB/LSB being 1. */
bool function_may_calculate_crc (function *fun);
/* Checks the loop iteration number.
The loop for CRC calculation may do 8, 16, 24, 32 iterations. */
bool maybe_crc_iteration_count (class loop *func_loop);
/* Finds and returns the block of a condition which is the destination
block of the block containing the check of MSB/LSB being 1.
If the block is found returns it, else returns nullptr. */
basic_block find_block_of_internal_condition (basic_block xor_bb);
/* Check whether found xor_stmt is for calculating crc.
The function crc_fun calculates crc only if there is a shift operation
in the crc_loop. */
bool xor_calculates_crc (function *crc_fun, class loop *crc_loop,
const gimple *stmt);
/* This function goes up through the def-chain of the name,
until doesn't encounter a phi statement or
if it does not meet certain conditions
depending on the passed continue_to_check_dep function.
The continue_to_check_dep may be the check_def_stmt_for_xor
or check_def_stmt_for_if function. */
void
get_dep (tree name,
bool (crc_optimization::*continue_to_check_dep) (gimple *stmt));
/* Checks whether the def_stmt statement, dependent from xor's operands,
does shift operation for calculating crc
or is a phi statement. Keeps phi statements of the loop's header.
Returns false, if there is an instruction which may not exist
in the CRC loop.
Returns true, if the def-chain examination must be continued. */
bool continue_to_check_dep_for_xor (gimple *stmt);
/* Checks whether if's condition and xor-ed variable
are dependent from the same variable.
(The crc variable is xor-ed if variable's MSB/LSB is 1).
Also determines phi instruction's of data and crc
(for further polynomial extraction). */
bool continue_to_check_dep_for_if (gimple *stmt);
/* This function checks that xor is done under the condition
of MSB/LSB being one.
Checks that if condition's variable and xor-ed variable
depend on same variable and if there is a shift 1 in the same block with
xor, on the opposite branch must be another shift 1 to the same direction.
xor_bb is the basic block, where xor operation is done.
pred_bb is the predecessor basic block of the xor_bb,
it is assumed that the last stmt of pred_bb checks the condition
under which xor is done. */
bool
crc_cond_and_shift (const basic_block &pred_bb, const basic_block &xor_bb);
/* This function gets the condition cond and
returns true if MSB/LSB bit is checked for 1. */
static bool cond_true_is_checked_for_bit_one (const gcond *cond);
/* This functions checks whether the pair of xor's shift exists
in the given bb. If there is a shift with xor in the same block,
then in the opposite block must be another shift. */
bool exists_shift_for_opp_xor_shift (basic_block bb);
/* This function walks through the uses of xor-ed variable
(within the crc_loop) and checks whether it is shifted one.
Operations which may corrupt crc value mustn't occur between xor and shift,
otherwise the loop isn't calculating CRC. */
void find_shift_after_xor (class loop *crc_loop, tree lhs);
/* Checks whether assigment statement does shift operation
(checks that shift 1 is done),
if it doesn't - checks whether it is acceptable operation
to be between shift and xor.
find_shift_before_xor argument is for checking whether we search for shift
before xor or after. */
bool can_not_be_shift_of_crc (gimple *assign_stmt,
bool find_shift_before_xor);
/* Checks whether it's ok that the statement is between shift and xor.
This check is not that accurate. But it's enough to filter not CRCs. */
static bool is_acceptable_statement (const tree_code &stmt_code);
/* Get the return value size of the function
and assign to return_size member. */
void set_return_value_size (function *crc_fun);
/* This function checks whether calculated crc value
(maybe modified) is returned.
By walking down through the use-def chain of lhs
return true if we encounter return statement. */
bool returned_value_depends_on_crc (tree lhs);
/* Prints extracted details of CRC calculation. */
void print_crc_information ();
/* Replaces function body with CRC_IFN call.
Returns true if replacement is succeeded, otherwise false. */
bool
faster_crc_code_generation (function *fun, value *polynomial);
/* Build tree for the polynomial without leading 1. */
tree build_polynomial_without_1 (tree crc_arg, value *polynomial);
public:
unsigned int execute (function *fun);
};
/* TODO: Move the function.
Set GIMPLE_PHI and GIMPLE statements of the crc loop not visited. */
void
set_loop_statements_not_visited (class loop *loop)
{
// TODO: find faster way.
basic_block *bbs = get_loop_body_in_dom_order (loop);
for (unsigned int i = 0; i < loop->num_nodes; i++)
{
basic_block bb = bbs[i];
/* Set phis not visited. */
for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
gsi_next (&gsi))
{
gphi *stmt = gsi.phi ();
gimple_set_visited (stmt, false);
}
/* Set statements not visited. */
for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
gimple_set_visited (stmt, false);
}
}
free (bbs);
}
/* TODO: Move the function.
Set GIMPLE_PHI and GIMPLE statements of the function not visited. */
static void
set_all_statements_not_visited (function *fun)
{
basic_block bb;
FOR_EACH_BB_FN (bb, fun)
{
for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
gsi_next (&gsi))
{
gphi *stmt = gsi.phi ();
gimple_set_visited (stmt, false);
}
for (gimple_stmt_iterator gsi = gsi_start_nondebug_bb (bb);
!gsi_end_p (gsi); gsi_next_nondebug (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
gimple_set_visited (stmt, false);
}
}
}
/* Prints extracted details of CRC calculation. */
void
crc_optimization::print_crc_information ()
{
if (dump_file)
{
fprintf (dump_file,
"Loop iteration number is %ld.\n"
"Return size is %ld.\n",
loop_iteration_number, return_size);
if (is_left_shift)
fprintf (dump_file, "Bit forward.\n");
else
fprintf (dump_file, "Bit reversed.\n");
}
}
/* This function checks whether calculated crc value
(maybe modified) is returned.
By walking down through the use-def chain of lhs
return true if we encounter return statement. */
bool
crc_optimization::returned_value_depends_on_crc (tree lhs)
{
bool crc_is_returned = false;
imm_use_iterator imm_iter;
use_operand_p use_p;
if (TREE_CODE (lhs) != SSA_NAME)
return false;
/* Iterate through the immediate uses of the current variable.
If we encounter return statement - return true. */
FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
{
gimple *stmt = USE_STMT (use_p);
if (gimple_visited_p (stmt))
return false;
gimple_set_visited (stmt, true);
if (gimple_code (stmt) == GIMPLE_RETURN)
return true;
else if (gimple_code (stmt) == GIMPLE_PHI)
crc_is_returned
= returned_value_depends_on_crc (gimple_phi_result (stmt));
else if (is_gimple_assign (stmt))
crc_is_returned
= returned_value_depends_on_crc (gimple_assign_lhs (stmt));
if (crc_is_returned)
return true;
}
return false;
}
/* Get the return value size of the function
and assign to return_size member. */
void
crc_optimization::set_return_value_size (function *crc_fun)
{
return_size = 0;
tree tree_return_value_size = DECL_SIZE (DECL_RESULT (crc_fun->decl));
if (tree_fits_uhwi_p (tree_return_value_size))
{
return_size = tree_to_uhwi (tree_return_value_size);
}
}
/* This function walks through the uses of xor-ed variable (within the crc_loop)
and checks whether it is shifted one.
Operations which may corrupt crc value mustn't occur between xor and shift,
otherwise the loop isn't calculating CRC. */
void
crc_optimization::find_shift_after_xor (class loop *crc_loop, tree lhs)
{
imm_use_iterator imm_iter;
use_operand_p use_p;
if (TREE_CODE (lhs) != SSA_NAME)
return;
/* Iterate through the immediate uses of the current variable.
If there is a shift return true,
if before shift there is other instruction (besides phi) return false. */
FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
{
gimple *stmt = USE_STMT (use_p);
if (gimple_visited_p (stmt))
return;
gimple_set_visited (stmt, true);
// Consider only statements within the loop
if (!flow_bb_inside_loop_p (crc_loop, gimple_bb (stmt)))
return;
/* If encountered phi statement, check immediate use of its result
otherwise, if encountered assign statement, check whether it does shift
(some other operations are allowed to be between shift and xor). */
if (gimple_code (stmt) == GIMPLE_PHI)
{
/* Don't continue finding if encountered the loop's beginning. */
if (bb_loop_header_p (gimple_bb (stmt)))
return;
find_shift_after_xor
(crc_loop, gimple_phi_result (stmt));
}
else if (is_gimple_assign (stmt)
&& can_not_be_shift_of_crc (stmt, false))
{
shift_after_xor = nullptr;
return;
}
}
}
/* This function checks whether the pair of xor's shift exists in the given bb.
If there is a shift with xor in the same block,
then in the opposite block must be another shift. */
bool
crc_optimization::exists_shift_for_opp_xor_shift (basic_block bb)
{
/* Walk through the instructions of given basic block. */
for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
!gsi_end_p (bsi); gsi_next_nondebug (&bsi))
{
gimple *stmt = gsi_stmt (bsi);
/* Find assigment statement with shift operation.
Check that shift's direction is same with the shift done
on the path with xor. */
if (is_gimple_assign (stmt))
{
if (gimple_assign_rhs_code (stmt) == LSHIFT_EXPR && is_left_shift)
return true;
else if (gimple_assign_rhs_code (stmt) == RSHIFT_EXPR
&& !is_left_shift)
return true;
}
}
/* If there is no shift, return false. */
return false;
}
/* This function gets the condition cond and
returns true if MSB/LSB bit is checked for 1. */
bool
crc_optimization::cond_true_is_checked_for_bit_one (const gcond *cond)
{
if (!cond)
return false;
tree lhs = gimple_cond_lhs (cond);
tree rhs = gimple_cond_rhs (cond);
enum tree_code code = gimple_cond_code (cond);
/* If the condition is
something == 1 or 1 == something -> return true. */
if ((integer_onep (lhs) || integer_onep (rhs)) && code == EQ_EXPR)
return true;
/* If the condition is
0 != something or 0 > something -> return true. */
if (integer_zerop (lhs) && (code == NE_EXPR || code == GT_EXPR))
return true;
/* If the condition is
something != 0 or something < 0 -> return true. */
if (integer_zerop (rhs) && (code == NE_EXPR || code == LT_EXPR))
return true;
return false;
}
/* This function checks that xor is done under the condition
of MSB/LSB being one.
Checks that if condition's variable and xor-ed variable
depend on same variable and if there is a shift1 in the same block with xor,
on the opposite branch must be another shift1 to the same direction.
xor_bb is the basic block, where xor operation is done.
pred_bb is the predecessor basic block of the xor_bb,
it is assumed that the last stmt of pred_bb checks the condition
under which xor is done. */
bool
crc_optimization::crc_cond_and_shift (const basic_block &pred_bb,
const basic_block &xor_bb)
{
gcond *cond = nullptr;
/* Check whether pred_bb contains condition.
We will consider only those cases
when xor is done immediately under condition. */
if (is_a<gcond *> (last_stmt (pred_bb)))
cond = as_a<gcond *> (last_stmt (pred_bb));
if (!cond)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "No condition. Not crc.\n");
return false;
}
edge true_edge;
edge false_edge;
extract_true_false_edges_from_block (pred_bb, &true_edge, &false_edge);
basic_block xor_opposite_block;
/* Check that xor is done in case the MSB/LSB is 1. */
if ((cond_true_is_checked_for_bit_one (cond) && true_edge->dest == xor_bb))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Xor is done on true branch.\n");
xor_opposite_block = false_edge->dest;
}
else if (!cond_true_is_checked_for_bit_one (cond)
&& false_edge->dest == xor_bb)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Xor is done on false branch.\n");
xor_opposite_block = true_edge->dest;
}
else
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"Xor is done if MSB/LSB is not one, not crc.\n");
return false;
}
/* Check whether there is a dependence between
if condition's variable and xor-ed variable.
Also determine phi statements of crc and data. */
tree lhs = gimple_cond_lhs (cond);
tree rhs = gimple_cond_rhs (cond);
set_loop_statements_not_visited (crc_loop);
if (TREE_CODE (lhs) == SSA_NAME)
get_dep (lhs, &crc_optimization::continue_to_check_dep_for_if);
else if (TREE_CODE (rhs) == SSA_NAME)
get_dep (rhs, &crc_optimization::continue_to_check_dep_for_if);
else
return false; /* Both parts of condition are not SSA_NAME. */
/* Return false if there is no dependence between if condition's variable
and xor-ed variable. */
if (!crc_if_dep)
return false;
/* If the found shift is in the same block with xor,
check whether another shift exists in the opposite block. */
if ((shift_before_xor && gimple_bb (shift_before_xor) == xor_bb)
|| (shift_after_xor && gimple_bb (shift_after_xor) == xor_bb))
return exists_shift_for_opp_xor_shift (xor_opposite_block);
return true;
}
/* Checks whether it's ok that the statement is between shift and xor.
This check is not that accurate. But it's enough to filter not CRCs. */
bool
crc_optimization::is_acceptable_statement (const tree_code &stmt_code)
{
return stmt_code == BIT_IOR_EXPR
|| stmt_code == BIT_AND_EXPR
|| stmt_code == BIT_XOR_EXPR
|| stmt_code == MINUS_EXPR
|| stmt_code == PLUS_EXPR
|| TREE_CODE_CLASS (stmt_code) == tcc_unary;
}
/* Checks whether assigment statement does shift operation
(checks that shift 1 is done),
if it doesn't - checks whether it is acceptable operation
to be between shift and xor.
find_shift_before_xor argument is for checking whether we search for shift
before xor or after. */
bool
crc_optimization::can_not_be_shift_of_crc (gimple *assign_stmt,
bool find_shift_before_xor)
{
tree_code stmt_code = gimple_assign_rhs_code (assign_stmt);
if (stmt_code == LSHIFT_EXPR || stmt_code == RSHIFT_EXPR)
{
is_left_shift = (stmt_code == LSHIFT_EXPR);
/* Check that shift one is done,
keep shift statement. */
if (integer_onep (gimple_assign_rhs2 (assign_stmt)))
{
if (find_shift_before_xor)
{
if (shift_before_xor)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"Already there is one shift "
"on the path to xor, not crc.\n");
clean_xor_maybe_crc = false;
return true;
}
shift_before_xor = assign_stmt;
}
else
{
shift_after_xor = assign_stmt;
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"Found <<1 or >>1 after xor.\n");
}
return false;
}
}
/* No need for more strict checks,
not CRCs may be filtered by the verification stage. */
else if (!is_acceptable_statement (stmt_code))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"\nStmt with the following operation "
"code %s between xor and shift, "
"may not be CRC.\n", get_tree_code_name (stmt_code));
return true;
}
return false;
}
/* This function goes up through the def-chain of the given name,
until doesn't encounter a phi statement or
if it does not meet certain conditions
depending on the passed continue_to_check_dep function.
The continue_to_check_dep may be the check_def_stmt_for_xor
or check_def_stmt_for_if function. */
void
crc_optimization::get_dep (tree name,
bool (crc_optimization::*continue_to_check_dep) (
gimple *ssa))
{
if (!(name && TREE_CODE (name) == SSA_NAME))
return;
/* No definition chain for default defs. */
if (SSA_NAME_IS_DEFAULT_DEF (name))
return;
gimple *stmt = SSA_NAME_DEF_STMT (name);
if (!stmt)
return;
/* Don't go outside the loop. */
if (!flow_bb_inside_loop_p (crc_loop, gimple_bb (stmt)))
return;
/* Check the statement, extract important information.
Stop examining def-chain if there is no need of other information. */
if (!((this->*continue_to_check_dep) (stmt)))
return;
/* If it is an assigment statement,
get and check def-chain for the first and second operands.
Otherwise if it's a phi statement, not declared in loop's header,
get and check def-chain for its values. */
if (is_a<gassign *> (stmt))
{
tree ssa1 = gimple_assign_rhs1 (stmt);
tree ssa2 = gimple_assign_rhs2 (stmt);
get_dep (ssa1, continue_to_check_dep);
get_dep (ssa2, continue_to_check_dep);
}
else if (is_a<gphi *> (stmt) && !bb_loop_header_p (gimple_bb (stmt)))
{
for (unsigned i = 0; i < gimple_phi_num_args (stmt); i++)
{
tree val = gimple_phi_arg_def (stmt, i);
get_dep (val, continue_to_check_dep);
}
}
}
/* Checks whether the def_stmt statement, dependent from xor's operands,
does shift operation for calculating crc
or is a phi statement. Keeps phi statements of the loop's header.
Returns false, if there is an instruction which may not exist
in the CRC loop.
Returns true, if the def-chain examination must be continued. */
bool
crc_optimization::continue_to_check_dep_for_xor (gimple *def_stmt)
{
if (!clean_xor_maybe_crc)
return false;
if (gimple_visited_p (def_stmt))
return false;
gimple_set_visited (def_stmt, true);
if (is_gimple_assign (def_stmt))
{
if (can_not_be_shift_of_crc (def_stmt, true))
{
clean_xor_maybe_crc = false;
return false;
}
}
else if (is_a<gphi *> (def_stmt))
{
/* Keep phi statement. */
if (bb_loop_header_p (gimple_bb (def_stmt)))
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"Phi's definition is in the loop header.\n");
/* The case when polynomial's value is determined by
a phi statement. */
if (first_phi_for_crc)
{
second_phi_for_crc = as_a<gphi *> (def_stmt);
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Set second phi.\n");
}
else
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Set first phi.\n");
first_phi_for_crc = as_a<gphi *> (def_stmt);
}
return true;
}
}
return true;
}
/* Checks whether if's condition and xor-ed variable
are dependent from the same variable.
(The crc variable is xor-ed if variable's MSB/LSB is 1).
Also determines phi instruction's of data and crc
(for further polynomial extraction). */
bool
crc_optimization::continue_to_check_dep_for_if (gimple *def_stmt)
{
gimple_set_visited (def_stmt, true);
if (is_a<gphi *> (def_stmt) && bb_loop_header_p (gimple_bb (def_stmt)))
{
/* Checks whether if's condition and xor-ed variable are dependent
from the same variable. */
if ((first_phi_for_crc && first_phi_for_crc == def_stmt)
|| (second_phi_for_crc && second_phi_for_crc == def_stmt))
{
crc_if_dep = true;
/* If the second phi equals def_stmt,
then it is used for crc calculation,
keep crc phi in first_phi_for_crc.
(This is needed for the polynomial extraction.) */
if ((second_phi_for_crc && second_phi_for_crc == def_stmt))
{
first_phi_for_crc = second_phi_for_crc;
}
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file,
"The condition of if statement has dependence "
"from the same variable as xor.\n"
"CRC's phi statement is:\n");
print_gimple_stmt (dump_file, def_stmt, dump_flags);
}
}
/* If we encounter a phi statement which isn't the crc,
then it may be the data.
(This is needed for the polynomial extraction.) */
else
{
/* Cond statement for crc calculation
may depend only on data and crc.
If it doesn't then it's not for crc calculation. */
if (data && data != def_stmt)
{
crc_if_dep = false;
return false;
}
data = as_a<gphi *> (def_stmt);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file,
"Found data's phi statement:\n");
print_gimple_stmt (dump_file, def_stmt, dump_flags);
}
}
}
return true;
}
/* In the case when xor's block doesn't have single predecessor,
we need to check whether it's parent blocks have single predecessor,
and it's the same block for both predecessor blocks.
If it is, most probably that block's predecessor may contain the check
for MSB/LSB being one.
This function searches the block of a condition which is the destination
block of the block containing the check of MSB/LSB being 1.
If the block is found returns it, else returns nullptr.
Here we can detect implementations like the following:
if ((crc & 0x80) != 0)
{
crc = (byte)(crc << 1);
crc = ((byte)(b & (1 << i)) != 0) //block_of_internal_condition
? (byte)(crc | 0x01) : (byte)(crc & 0xFE);
crc = (byte)(crc ^ generator);
}
else
{
crc = (byte)(crc << 1);
crc = ((byte)(b & (1 << i)) != 0)
? (byte)(crc | 0x01) : (byte)(crc & 0xFE);
}
*/
basic_block
crc_optimization::find_block_of_internal_condition (basic_block xor_bb)
{
basic_block parent_bb = EDGE_PRED (xor_bb, 0)->src;
basic_block block_of_internal_condition = EDGE_PRED (parent_bb, 0)->src;
for (size_t i = 1; i < EDGE_COUNT (xor_bb->preds); ++i)
{
edge e = EDGE_PRED (xor_bb, i);
if (!(single_pred_p (e->src)
&& EDGE_PRED (e->src, 0)->src == block_of_internal_condition))
return nullptr;
}
return block_of_internal_condition;
}
/* Check whether found xor_stmt is for calculating crc.
The function crc_fun calculates crc only if there is a shift operation
in the crc_loop. */
bool
crc_optimization::xor_calculates_crc (function *crc_fun, class loop *crc_loop,
const gimple *xor_stmt)
{
tree crc_var = gimple_assign_lhs (xor_stmt);
set_initial_values ();
set_loop_statements_not_visited (crc_loop);
get_dep (crc_var,
&crc_optimization::continue_to_check_dep_for_xor);
if (!clean_xor_maybe_crc)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Xor isn't used for CRC calculation.\n");
return false;
}
/* Check the case when shift is done after xor. */
if (!shift_before_xor)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "No shift before xor, trying to find after xor.\n");
// TODO: It's time consuming to get loop's body,
// (set_all_statements_not_visited may be time consuming too).
// Do it better.
set_loop_statements_not_visited (crc_loop);
find_shift_after_xor (crc_loop, crc_var);
if (!shift_after_xor)
return false;
}
/* Check that xor is done if MSB/LSB is one.
In presence of shift in the same loop with xor,
check for its pair in another branch.
If all checks succeed, then it is a crc. */
basic_block bb = gimple_bb (xor_stmt);
if (!single_pred_p (bb))
{
bb = find_block_of_internal_condition (bb);
if (!bb || !single_pred_p (bb))
{
if (dump_file)
fprintf (dump_file,
"Xor bb doesn't have single predecessor.\n");
return false;
}
}
basic_block block_of_condition = single_pred (bb);
if (crc_cond_and_shift (block_of_condition, bb))
{
set_all_statements_not_visited (crc_fun);
bool crc_is_returned = returned_value_depends_on_crc (crc_var);
if (dump_file)
{
if (crc_is_returned)
{
fprintf (dump_file,
"\n%s function maybe calculates CRC "
"and returns it.\n",
function_name (crc_fun));
}
else
{
fprintf (dump_file,
"\n%s function maybe calculates CRC.\n",
function_name (crc_fun));
}
}
return true;
}
return false;
}
/* Checks the loop iteration number.
The loop for CRC calculation may do 8, 16, 24, 32 iterations. */
bool
crc_optimization::maybe_crc_iteration_count (class loop *func_loop)
{
loop_iteration_number = 0;
tree n_inters = number_of_latch_executions (func_loop);
if (n_inters == NULL_TREE || n_inters == chrec_dont_know)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"Loop iteration number is chrec_dont_know.\n");
return false;
}
else if (tree_fits_uhwi_p (n_inters))
{
loop_iteration_number = tree_to_uhwi (n_inters);
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Loop iteration number is %ld.\n",
loop_iteration_number);
if (!(loop_iteration_number == 7 || loop_iteration_number == 15
|| loop_iteration_number == 23 || loop_iteration_number == 31
|| loop_iteration_number == 63))
return false;
}
return true;
}
/* This is the main function which checks whether given function calculates CRC
and extracts the details of the CRC calculation.
The main idea is to find innermost loop with 8, 16, 24, 32 iterations.
Find xor in the loop (xor is the key operation for naive crc calculation).
Check that before/after being xor-ed the variable is shifted by one.
Xor must be done under condition of MSB/LSB being 1. */
bool
crc_optimization::function_may_calculate_crc (function *fun)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "\nExamining %s function.\n",
function_name (fun));
if (number_of_loops (fun) <= 1)
return false;
/* Get loops of the function. */
auto loop_list = loops_list (fun, LI_ONLY_INNERMOST);
for (auto loop: loop_list)
{
/* Only examine innermost loops. */
if (!loop || loop->inner)
continue;
if (!maybe_crc_iteration_count (loop))
continue;
crc_loop = loop;
basic_block *bbs = get_loop_body_in_dom_order (loop);
/* Walk bbs of the loop. */
for (unsigned int i = 0; i < loop->num_nodes; i++)
{
basic_block bb = bbs[i];
/* Walk instructions of bb. */
for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb);
!gsi_end_p (bsi); gsi_next_nondebug (&bsi))
{
gimple *stmt = gsi_stmt (bsi);
/* If there is an xor instruction,
check that it is calculating crc. */
if (is_gimple_assign (stmt)
&& gimple_assign_rhs_code (stmt) == BIT_XOR_EXPR)
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file,
"Found xor, "
"checking whether it is for crc calculation.\n");
if (xor_calculates_crc (fun, loop, stmt))
{
set_return_value_size (fun);
print_crc_information ();
free (bbs);
return true;
}
}
}
}
free (bbs);
}
return false;
}
/* Remove all statements and basic blocks of the function
except for the return statement. */
gimple *
remove_stmts_besides_return (function *fun, class loop *crc_loop)
{
gimple_stmt_iterator gsi;
basic_block bb, next_bb;
gimple *return_stmt = NULL;
cancel_loop_tree (crc_loop);
for (bb = fun->cfg->x_entry_block_ptr->next_bb;
bb != fun->cfg->x_exit_block_ptr; bb = next_bb)
{
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple *gs = gsi_stmt (gsi);
// TODO: Check whether it's better to get return
// statement during symbolic execution step and change.
if (gimple_code (gs) == GIMPLE_RETURN)
return_stmt = gs;
}
next_bb = bb->next_bb;
// TODO: what if return statement's block isn't the last one?!
if (!return_stmt)
delete_basic_block (bb);
else
{
// TODO: Check whether there can be other statements
// besides return in the block. Delete them if any.
/* Delete phi statements of the return block. */
for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);)
gsi_remove (&gsi, true);
}
}
make_edge (fun->cfg->x_entry_block_ptr, return_stmt->bb,
EDGE_FALLTHRU);
return return_stmt;
}
tree
crc_optimization::build_polynomial_without_1 (tree crc_arg, value * polynomial)
{
unsigned HOST_WIDE_INT cst_polynomial = 0;
for (size_t i = 0; i < (*polynomial).length (); i++)
{
value_bit *const_bit;
if (is_left_shift)
const_bit = (*polynomial)[(*polynomial).length () - 1 - i];
else
const_bit = (*polynomial)[i];
cst_polynomial <<= 1;
cst_polynomial ^= (as_a<bit *> (const_bit))->get_val () ? 1 : 0;
}
return build_int_cstu (TREE_TYPE (crc_arg), cst_polynomial);
}
/* Replaces function body with CRC_IFN call.
Returns true if replacement is succeeded, otherwise false. */
bool
crc_optimization::faster_crc_code_generation (function *fun,
value *polynomial)
{
if (!(data && first_phi_for_crc))
return false;
// TODO: Use another way of figuring out which argument is data, as this won't
// work correctly in case when data is xor-ed with crc before the loop.
tree crc_arg = PHI_ARG_DEF (first_phi_for_crc, 1);
tree data_arg = PHI_ARG_DEF (data, 1);
tree polynomial_arg = build_polynomial_without_1 (crc_arg, polynomial);
if (!direct_internal_fn_supported_p
(IFN_CRC, tree_pair (TREE_TYPE (data_arg), TREE_TYPE (crc_arg)),
OPTIMIZE_FOR_BOTH))
{
if (dump_file)
fprintf (dump_file, "No matching optab entry\n");
return false;
}
gimple *return_stmt = remove_stmts_besides_return (fun, crc_loop);
/* Add IFN call and return the value. */
gcall *call
= gimple_build_call_internal (IFN_CRC, 3,
crc_arg,
data_arg,
polynomial_arg);
tree result = make_ssa_name (TREE_TYPE (DECL_RESULT (fun->decl)));
gimple_call_set_lhs (call, result);
gimple_set_location (call, fun->function_start_locus);
gimple_stmt_iterator si = gsi_start_bb (return_stmt->bb);
gsi_insert_before (&si, call, GSI_SAME_STMT);
use_operand_p imm_use_p;
imm_use_iterator iterator;
gimple *stmt;
FOR_EACH_IMM_USE_STMT (stmt, iterator,
gimple_return_retval (as_a<greturn *> (return_stmt)))
{
FOR_EACH_IMM_USE_ON_STMT (imm_use_p, iterator)
SET_USE (imm_use_p, result);
update_stmt (stmt);
}
/* Fix up CFG. */
remove_unused_locals ();
scev_reset ();
mark_virtual_operands_for_renaming (fun);
free_dominance_info (fun, CDI_DOMINATORS);
calculate_dominance_info (CDI_DOMINATORS);
rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
return true;
}
unsigned int
crc_optimization::execute (function *fun)
{
if (function_may_calculate_crc (fun))
{
crc_symbolic_execution symbolically_execute_function;
if (!symbolically_execute_function.execute (fun))
{
if (dump_file)
fprintf (dump_file, "\nCRC verification didn't succeed "
"during symbolic execution!\n");
return 0;
}
crc_symbolic_execution execute_loop;
std::pair <tree, value *>
calc_polynom = execute_loop.extract_polynomial (crc_loop,
first_phi_for_crc,
data, is_left_shift);
if (!calc_polynom.second)
return 0;
value *lfsr
= state::create_lfsr (calc_polynom.first,
calc_polynom.second, is_left_shift);
if (!lfsr)
{
if (dump_file)
fprintf (dump_file, "Couldn't create LFSR!\n");
return 0;
}
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "\nLFSR value is \n");
state::print_value (lfsr);
}
if (all_states_match_lfsr (lfsr, is_left_shift,
symbolically_execute_function
.get_final_states ()))
{
if (dump_file)
fprintf (dump_file, "%s function calculates CRC!\n",
function_name (fun));
if (!faster_crc_code_generation (fun, calc_polynom.second))
{
if (dump_file)
fprintf (dump_file, "Couldn't generate faster CRC code.\n");
}
}
else
{
if (dump_file && (dump_flags & TDF_DETAILS))
fprintf (dump_file, "Returned state and LFSR differ.\n");
}
delete lfsr;
}
return 0;
}
namespace
{
const pass_data pass_data_crc_optimization
= {
GIMPLE_PASS, /* type */
"crc", /* name */
OPTGROUP_NONE, /* optinfo_flags */
TV_GIMPLE_CRC_OPTIMIZATION, /* tv_id */
(PROP_cfg | PROP_ssa), /* properties_required */
0, /* properties_provided */
0, /* properties_destroyed */
0, /* todo_flags_start */
0, /* todo_flags_finish */
};
class pass_crc_optimization : public gimple_opt_pass {
public:
pass_crc_optimization (gcc::context *ctxt)
: gimple_opt_pass (pass_data_crc_optimization, ctxt)
{}
/* opt_pass methods: */
virtual bool gate (function *)
{
return flag_gimple_crc_optimization;
}
virtual unsigned int execute (function *);
}; // class pass_crc_optimization
unsigned int
pass_crc_optimization::execute (function *fun)
{
return crc_optimization ().execute (fun);
}
} // anon namespace
gimple_opt_pass *
make_pass_crc_optimization (gcc::context *ctxt)
{
return new pass_crc_optimization (ctxt);
}
|