forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-ssa-uninit.c
2784 lines (2357 loc) · 73.7 KB
/
tree-ssa-uninit.c
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
/* Predicate aware uninitialized variable warning.
Copyright (C) 2001-2020 Free Software Foundation, Inc.
Contributed by Xinliang David Li <[email protected]>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple.h"
#include "tree-pass.h"
#include "ssa.h"
#include "gimple-pretty-print.h"
#include "diagnostic-core.h"
#include "fold-const.h"
#include "gimple-iterator.h"
#include "tree-ssa.h"
#include "tree-cfg.h"
#include "cfghooks.h"
/* This implements the pass that does predicate aware warning on uses of
possibly uninitialized variables. The pass first collects the set of
possibly uninitialized SSA names. For each such name, it walks through
all its immediate uses. For each immediate use, it rebuilds the condition
expression (the predicate) that guards the use. The predicate is then
examined to see if the variable is always defined under that same condition.
This is done either by pruning the unrealizable paths that lead to the
default definitions or by checking if the predicate set that guards the
defining paths is a superset of the use predicate. */
/* Max PHI args we can handle in pass. */
const unsigned max_phi_args = 32;
/* Pointer set of potentially undefined ssa names, i.e.,
ssa names that are defined by phi with operands that
are not defined or potentially undefined. */
static hash_set<tree> *possibly_undefined_names = 0;
/* Bit mask handling macros. */
#define MASK_SET_BIT(mask, pos) mask |= (1 << pos)
#define MASK_TEST_BIT(mask, pos) (mask & (1 << pos))
#define MASK_EMPTY(mask) (mask == 0)
/* Returns the first bit position (starting from LSB)
in mask that is non zero. Returns -1 if the mask is empty. */
static int
get_mask_first_set_bit (unsigned mask)
{
int pos = 0;
if (mask == 0)
return -1;
while ((mask & (1 << pos)) == 0)
pos++;
return pos;
}
#define MASK_FIRST_SET_BIT(mask) get_mask_first_set_bit (mask)
/* Return true if T, an SSA_NAME, has an undefined value. */
static bool
has_undefined_value_p (tree t)
{
return (ssa_undefined_value_p (t)
|| (possibly_undefined_names
&& possibly_undefined_names->contains (t)));
}
/* Like has_undefined_value_p, but don't return true if TREE_NO_WARNING
is set on SSA_NAME_VAR. */
static inline bool
uninit_undefined_value_p (tree t)
{
if (!has_undefined_value_p (t))
return false;
if (SSA_NAME_VAR (t) && TREE_NO_WARNING (SSA_NAME_VAR (t)))
return false;
return true;
}
/* Emit warnings for uninitialized variables. This is done in two passes.
The first pass notices real uses of SSA names with undefined values.
Such uses are unconditionally uninitialized, and we can be certain that
such a use is a mistake. This pass is run before most optimizations,
so that we catch as many as we can.
The second pass follows PHI nodes to find uses that are potentially
uninitialized. In this case we can't necessarily prove that the use
is really uninitialized. This pass is run after most optimizations,
so that we thread as many jumps and possible, and delete as much dead
code as possible, in order to reduce false positives. We also look
again for plain uninitialized variables, since optimization may have
changed conditionally uninitialized to unconditionally uninitialized. */
/* Emit a warning for EXPR based on variable VAR at the point in the
program T, an SSA_NAME, is used being uninitialized. The exact
warning text is in MSGID and DATA is the gimple stmt with info about
the location in source code. When DATA is a GIMPLE_PHI, PHIARG_IDX
gives which argument of the phi node to take the location from. WC
is the warning code. */
static void
warn_uninit (enum opt_code wc, tree t, tree expr, tree var,
const char *gmsgid, void *data, location_t phiarg_loc)
{
gimple *context = (gimple *) data;
location_t location, cfun_loc;
expanded_location xloc, floc;
/* Ignore COMPLEX_EXPR as initializing only a part of a complex
turns in a COMPLEX_EXPR with the not initialized part being
set to its previous (undefined) value. */
if (is_gimple_assign (context)
&& gimple_assign_rhs_code (context) == COMPLEX_EXPR)
return;
if (!has_undefined_value_p (t))
return;
/* Anonymous SSA_NAMEs shouldn't be uninitialized, but ssa_undefined_value_p
can return true if the def stmt of anonymous SSA_NAME is COMPLEX_EXPR
created for conversion from scalar to complex. Use the underlying var of
the COMPLEX_EXPRs real part in that case. See PR71581. */
if (expr == NULL_TREE
&& var == NULL_TREE
&& SSA_NAME_VAR (t) == NULL_TREE
&& is_gimple_assign (SSA_NAME_DEF_STMT (t))
&& gimple_assign_rhs_code (SSA_NAME_DEF_STMT (t)) == COMPLEX_EXPR)
{
tree v = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (t));
if (TREE_CODE (v) == SSA_NAME
&& has_undefined_value_p (v)
&& zerop (gimple_assign_rhs2 (SSA_NAME_DEF_STMT (t))))
{
expr = SSA_NAME_VAR (v);
var = expr;
}
}
if (expr == NULL_TREE)
return;
/* TREE_NO_WARNING either means we already warned, or the front end
wishes to suppress the warning. */
if ((context
&& (gimple_no_warning_p (context)
|| (gimple_assign_single_p (context)
&& TREE_NO_WARNING (gimple_assign_rhs1 (context)))))
|| TREE_NO_WARNING (expr))
return;
if (context != NULL && gimple_has_location (context))
location = gimple_location (context);
else if (phiarg_loc != UNKNOWN_LOCATION)
location = phiarg_loc;
else
location = DECL_SOURCE_LOCATION (var);
location = linemap_resolve_location (line_table, location,
LRK_SPELLING_LOCATION, NULL);
cfun_loc = DECL_SOURCE_LOCATION (cfun->decl);
xloc = expand_location (location);
floc = expand_location (cfun_loc);
auto_diagnostic_group d;
if (warning_at (location, wc, gmsgid, expr))
{
TREE_NO_WARNING (expr) = 1;
if (location == DECL_SOURCE_LOCATION (var))
return;
if (xloc.file != floc.file
|| linemap_location_before_p (line_table, location, cfun_loc)
|| linemap_location_before_p (line_table, cfun->function_end_locus,
location))
inform (DECL_SOURCE_LOCATION (var), "%qD was declared here", var);
}
}
struct check_defs_data
{
/* If we found any may-defs besides must-def clobbers. */
bool found_may_defs;
};
/* Callback for walk_aliased_vdefs. */
static bool
check_defs (ao_ref *ref, tree vdef, void *data_)
{
check_defs_data *data = (check_defs_data *)data_;
gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
/* If this is a clobber then if it is not a kill walk past it. */
if (gimple_clobber_p (def_stmt))
{
if (stmt_kills_ref_p (def_stmt, ref))
return true;
return false;
}
/* Found a may-def on this path. */
data->found_may_defs = true;
return true;
}
static unsigned int
warn_uninitialized_vars (bool warn_possibly_uninitialized)
{
gimple_stmt_iterator gsi;
basic_block bb;
unsigned int vdef_cnt = 0;
unsigned int oracle_cnt = 0;
unsigned limit = 0;
FOR_EACH_BB_FN (bb, cfun)
{
basic_block succ = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
bool always_executed = dominated_by_p (CDI_POST_DOMINATORS, succ, bb);
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple *stmt = gsi_stmt (gsi);
use_operand_p use_p;
ssa_op_iter op_iter;
tree use;
if (is_gimple_debug (stmt))
continue;
/* We only do data flow with SSA_NAMEs, so that's all we
can warn about. */
FOR_EACH_SSA_USE_OPERAND (use_p, stmt, op_iter, SSA_OP_USE)
{
/* BIT_INSERT_EXPR first operand should not be considered
a use for the purpose of uninit warnings. */
if (gassign *ass = dyn_cast <gassign *> (stmt))
{
if (gimple_assign_rhs_code (ass) == BIT_INSERT_EXPR
&& use_p->use == gimple_assign_rhs1_ptr (ass))
continue;
}
use = USE_FROM_PTR (use_p);
if (always_executed)
warn_uninit (OPT_Wuninitialized, use, SSA_NAME_VAR (use),
SSA_NAME_VAR (use),
"%qD is used uninitialized in this function", stmt,
UNKNOWN_LOCATION);
else if (warn_possibly_uninitialized)
warn_uninit (OPT_Wmaybe_uninitialized, use, SSA_NAME_VAR (use),
SSA_NAME_VAR (use),
"%qD may be used uninitialized in this function",
stmt, UNKNOWN_LOCATION);
}
/* For limiting the alias walk below we count all
vdefs in the function. */
if (gimple_vdef (stmt))
vdef_cnt++;
if (gimple_assign_load_p (stmt)
&& gimple_has_location (stmt))
{
tree rhs = gimple_assign_rhs1 (stmt);
tree lhs = gimple_assign_lhs (stmt);
bool has_bit_insert = false;
use_operand_p luse_p;
imm_use_iterator liter;
if (TREE_NO_WARNING (rhs))
continue;
ao_ref ref;
ao_ref_init (&ref, rhs);
/* Do not warn if the base was marked so or this is a
hard register var. */
tree base = ao_ref_base (&ref);
if ((VAR_P (base)
&& DECL_HARD_REGISTER (base))
|| TREE_NO_WARNING (base))
continue;
/* Do not warn if the access is fully outside of the
variable. */
poly_int64 decl_size;
if (DECL_P (base)
&& known_size_p (ref.size)
&& ((known_eq (ref.max_size, ref.size)
&& known_le (ref.offset + ref.size, 0))
|| (known_ge (ref.offset, 0)
&& DECL_SIZE (base)
&& poly_int_tree_p (DECL_SIZE (base), &decl_size)
&& known_le (decl_size, ref.offset))))
continue;
/* Do not warn if the access is then used for a BIT_INSERT_EXPR. */
if (TREE_CODE (lhs) == SSA_NAME)
FOR_EACH_IMM_USE_FAST (luse_p, liter, lhs)
{
gimple *use_stmt = USE_STMT (luse_p);
/* BIT_INSERT_EXPR first operand should not be considered
a use for the purpose of uninit warnings. */
if (gassign *ass = dyn_cast <gassign *> (use_stmt))
{
if (gimple_assign_rhs_code (ass) == BIT_INSERT_EXPR
&& luse_p->use == gimple_assign_rhs1_ptr (ass))
{
has_bit_insert = true;
break;
}
}
}
if (has_bit_insert)
continue;
/* Limit the walking to a constant number of stmts after
we overcommit quadratic behavior for small functions
and O(n) behavior. */
if (oracle_cnt > 128 * 128
&& oracle_cnt > vdef_cnt * 2)
limit = 32;
check_defs_data data;
bool fentry_reached = false;
data.found_may_defs = false;
use = gimple_vuse (stmt);
int res = walk_aliased_vdefs (&ref, use,
check_defs, &data, NULL,
&fentry_reached, limit);
if (res == -1)
{
oracle_cnt += limit;
continue;
}
oracle_cnt += res;
if (data.found_may_defs)
continue;
/* Do not warn if it can be initialized outside this function.
If we did not reach function entry then we found killing
clobbers on all paths to entry. */
if (fentry_reached
/* ??? We'd like to use ref_may_alias_global_p but that
excludes global readonly memory and thus we get bougs
warnings from p = cond ? "a" : "b" for example. */
&& (!VAR_P (base)
|| is_global_var (base)))
continue;
/* We didn't find any may-defs so on all paths either
reached function entry or a killing clobber. */
location_t location
= linemap_resolve_location (line_table, gimple_location (stmt),
LRK_SPELLING_LOCATION, NULL);
if (always_executed)
{
if (warning_at (location, OPT_Wuninitialized,
"%qE is used uninitialized in this function",
rhs))
/* ??? This is only effective for decls as in
gcc.dg/uninit-B-O0.c. Avoid doing this for
maybe-uninit uses as it may hide important
locations. */
TREE_NO_WARNING (rhs) = 1;
}
else if (warn_possibly_uninitialized)
warning_at (location, OPT_Wmaybe_uninitialized,
"%qE may be used uninitialized in this function",
rhs);
}
}
}
return 0;
}
/* Checks if the operand OPND of PHI is defined by
another phi with one operand defined by this PHI,
but the rest operands are all defined. If yes,
returns true to skip this operand as being
redundant. Can be enhanced to be more general. */
static bool
can_skip_redundant_opnd (tree opnd, gimple *phi)
{
gimple *op_def;
tree phi_def;
int i, n;
phi_def = gimple_phi_result (phi);
op_def = SSA_NAME_DEF_STMT (opnd);
if (gimple_code (op_def) != GIMPLE_PHI)
return false;
n = gimple_phi_num_args (op_def);
for (i = 0; i < n; ++i)
{
tree op = gimple_phi_arg_def (op_def, i);
if (TREE_CODE (op) != SSA_NAME)
continue;
if (op != phi_def && uninit_undefined_value_p (op))
return false;
}
return true;
}
/* Returns a bit mask holding the positions of arguments in PHI
that have empty (or possibly empty) definitions. */
static unsigned
compute_uninit_opnds_pos (gphi *phi)
{
size_t i, n;
unsigned uninit_opnds = 0;
n = gimple_phi_num_args (phi);
/* Bail out for phi with too many args. */
if (n > max_phi_args)
return 0;
for (i = 0; i < n; ++i)
{
tree op = gimple_phi_arg_def (phi, i);
if (TREE_CODE (op) == SSA_NAME
&& uninit_undefined_value_p (op)
&& !can_skip_redundant_opnd (op, phi))
{
if (cfun->has_nonlocal_label || cfun->calls_setjmp)
{
/* Ignore SSA_NAMEs that appear on abnormal edges
somewhere. */
if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
continue;
}
MASK_SET_BIT (uninit_opnds, i);
}
}
return uninit_opnds;
}
/* Find the immediate postdominator PDOM of the specified
basic block BLOCK. */
static inline basic_block
find_pdom (basic_block block)
{
if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
return EXIT_BLOCK_PTR_FOR_FN (cfun);
else
{
basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
if (!bb)
return EXIT_BLOCK_PTR_FOR_FN (cfun);
return bb;
}
}
/* Find the immediate DOM of the specified basic block BLOCK. */
static inline basic_block
find_dom (basic_block block)
{
if (block == ENTRY_BLOCK_PTR_FOR_FN (cfun))
return ENTRY_BLOCK_PTR_FOR_FN (cfun);
else
{
basic_block bb = get_immediate_dominator (CDI_DOMINATORS, block);
if (!bb)
return ENTRY_BLOCK_PTR_FOR_FN (cfun);
return bb;
}
}
/* Returns true if BB1 is postdominating BB2 and BB1 is
not a loop exit bb. The loop exit bb check is simple and does
not cover all cases. */
static bool
is_non_loop_exit_postdominating (basic_block bb1, basic_block bb2)
{
if (!dominated_by_p (CDI_POST_DOMINATORS, bb2, bb1))
return false;
if (single_pred_p (bb1) && !single_succ_p (bb2))
return false;
return true;
}
/* Find the closest postdominator of a specified BB, which is control
equivalent to BB. */
static inline basic_block
find_control_equiv_block (basic_block bb)
{
basic_block pdom;
pdom = find_pdom (bb);
/* Skip the postdominating bb that is also loop exit. */
if (!is_non_loop_exit_postdominating (pdom, bb))
return NULL;
if (dominated_by_p (CDI_DOMINATORS, pdom, bb))
return pdom;
return NULL;
}
#define MAX_NUM_CHAINS 8
#define MAX_CHAIN_LEN 5
#define MAX_POSTDOM_CHECK 8
#define MAX_SWITCH_CASES 40
/* Computes the control dependence chains (paths of edges)
for DEP_BB up to the dominating basic block BB (the head node of a
chain should be dominated by it). CD_CHAINS is pointer to an
array holding the result chains. CUR_CD_CHAIN is the current
chain being computed. *NUM_CHAINS is total number of chains. The
function returns true if the information is successfully computed,
return false if there is no control dependence or not computed. */
static bool
compute_control_dep_chain (basic_block bb, basic_block dep_bb,
vec<edge> *cd_chains,
size_t *num_chains,
vec<edge> *cur_cd_chain,
int *num_calls)
{
edge_iterator ei;
edge e;
size_t i;
bool found_cd_chain = false;
size_t cur_chain_len = 0;
if (*num_calls > param_uninit_control_dep_attempts)
return false;
++*num_calls;
/* Could use a set instead. */
cur_chain_len = cur_cd_chain->length ();
if (cur_chain_len > MAX_CHAIN_LEN)
return false;
for (i = 0; i < cur_chain_len; i++)
{
edge e = (*cur_cd_chain)[i];
/* Cycle detected. */
if (e->src == bb)
return false;
}
FOR_EACH_EDGE (e, ei, bb->succs)
{
basic_block cd_bb;
int post_dom_check = 0;
if (e->flags & (EDGE_FAKE | EDGE_ABNORMAL))
continue;
cd_bb = e->dest;
cur_cd_chain->safe_push (e);
while (!is_non_loop_exit_postdominating (cd_bb, bb))
{
if (cd_bb == dep_bb)
{
/* Found a direct control dependence. */
if (*num_chains < MAX_NUM_CHAINS)
{
cd_chains[*num_chains] = cur_cd_chain->copy ();
(*num_chains)++;
}
found_cd_chain = true;
/* Check path from next edge. */
break;
}
/* Now check if DEP_BB is indirectly control dependent on BB. */
if (compute_control_dep_chain (cd_bb, dep_bb, cd_chains, num_chains,
cur_cd_chain, num_calls))
{
found_cd_chain = true;
break;
}
cd_bb = find_pdom (cd_bb);
post_dom_check++;
if (cd_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
|| post_dom_check > MAX_POSTDOM_CHECK)
break;
}
cur_cd_chain->pop ();
gcc_assert (cur_cd_chain->length () == cur_chain_len);
}
gcc_assert (cur_cd_chain->length () == cur_chain_len);
return found_cd_chain;
}
/* The type to represent a simple predicate. */
struct pred_info
{
tree pred_lhs;
tree pred_rhs;
enum tree_code cond_code;
bool invert;
};
/* The type to represent a sequence of predicates grouped
with .AND. operation. */
typedef vec<pred_info, va_heap, vl_ptr> pred_chain;
/* The type to represent a sequence of pred_chains grouped
with .OR. operation. */
typedef vec<pred_chain, va_heap, vl_ptr> pred_chain_union;
/* Converts the chains of control dependence edges into a set of
predicates. A control dependence chain is represented by a vector
edges. DEP_CHAINS points to an array of dependence chains.
NUM_CHAINS is the size of the chain array. One edge in a dependence
chain is mapped to predicate expression represented by pred_info
type. One dependence chain is converted to a composite predicate that
is the result of AND operation of pred_info mapped to each edge.
A composite predicate is presented by a vector of pred_info. On
return, *PREDS points to the resulting array of composite predicates.
*NUM_PREDS is the number of composite predictes. */
static bool
convert_control_dep_chain_into_preds (vec<edge> *dep_chains,
size_t num_chains,
pred_chain_union *preds)
{
bool has_valid_pred = false;
size_t i, j;
if (num_chains == 0 || num_chains >= MAX_NUM_CHAINS)
return false;
/* Now convert the control dep chain into a set
of predicates. */
preds->reserve (num_chains);
for (i = 0; i < num_chains; i++)
{
vec<edge> one_cd_chain = dep_chains[i];
has_valid_pred = false;
pred_chain t_chain = vNULL;
for (j = 0; j < one_cd_chain.length (); j++)
{
gimple *cond_stmt;
gimple_stmt_iterator gsi;
basic_block guard_bb;
pred_info one_pred;
edge e;
e = one_cd_chain[j];
guard_bb = e->src;
gsi = gsi_last_bb (guard_bb);
/* Ignore empty forwarder blocks. */
if (empty_block_p (guard_bb) && single_succ_p (guard_bb))
continue;
/* An empty basic block here is likely a PHI, and is not one
of the cases we handle below. */
if (gsi_end_p (gsi))
{
has_valid_pred = false;
break;
}
cond_stmt = gsi_stmt (gsi);
if (is_gimple_call (cond_stmt) && EDGE_COUNT (e->src->succs) >= 2)
/* Ignore EH edge. Can add assertion on the other edge's flag. */
continue;
/* Skip if there is essentially one succesor. */
if (EDGE_COUNT (e->src->succs) == 2)
{
edge e1;
edge_iterator ei1;
bool skip = false;
FOR_EACH_EDGE (e1, ei1, e->src->succs)
{
if (EDGE_COUNT (e1->dest->succs) == 0)
{
skip = true;
break;
}
}
if (skip)
continue;
}
if (gimple_code (cond_stmt) == GIMPLE_COND)
{
one_pred.pred_lhs = gimple_cond_lhs (cond_stmt);
one_pred.pred_rhs = gimple_cond_rhs (cond_stmt);
one_pred.cond_code = gimple_cond_code (cond_stmt);
one_pred.invert = !!(e->flags & EDGE_FALSE_VALUE);
t_chain.safe_push (one_pred);
has_valid_pred = true;
}
else if (gswitch *gs = dyn_cast<gswitch *> (cond_stmt))
{
/* Avoid quadratic behavior. */
if (gimple_switch_num_labels (gs) > MAX_SWITCH_CASES)
{
has_valid_pred = false;
break;
}
/* Find the case label. */
tree l = NULL_TREE;
unsigned idx;
for (idx = 0; idx < gimple_switch_num_labels (gs); ++idx)
{
tree tl = gimple_switch_label (gs, idx);
if (e->dest == label_to_block (cfun, CASE_LABEL (tl)))
{
if (!l)
l = tl;
else
{
l = NULL_TREE;
break;
}
}
}
/* If more than one label reaches this block or the case
label doesn't have a single value (like the default one)
fail. */
if (!l
|| !CASE_LOW (l)
|| (CASE_HIGH (l)
&& !operand_equal_p (CASE_LOW (l), CASE_HIGH (l), 0)))
{
has_valid_pred = false;
break;
}
one_pred.pred_lhs = gimple_switch_index (gs);
one_pred.pred_rhs = CASE_LOW (l);
one_pred.cond_code = EQ_EXPR;
one_pred.invert = false;
t_chain.safe_push (one_pred);
has_valid_pred = true;
}
else
{
has_valid_pred = false;
break;
}
}
if (!has_valid_pred)
break;
else
preds->safe_push (t_chain);
}
return has_valid_pred;
}
/* Computes all control dependence chains for USE_BB. The control
dependence chains are then converted to an array of composite
predicates pointed to by PREDS. PHI_BB is the basic block of
the phi whose result is used in USE_BB. */
static bool
find_predicates (pred_chain_union *preds,
basic_block phi_bb,
basic_block use_bb)
{
size_t num_chains = 0, i;
int num_calls = 0;
vec<edge> dep_chains[MAX_NUM_CHAINS];
auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
bool has_valid_pred = false;
basic_block cd_root = 0;
/* First find the closest bb that is control equivalent to PHI_BB
that also dominates USE_BB. */
cd_root = phi_bb;
while (dominated_by_p (CDI_DOMINATORS, use_bb, cd_root))
{
basic_block ctrl_eq_bb = find_control_equiv_block (cd_root);
if (ctrl_eq_bb && dominated_by_p (CDI_DOMINATORS, use_bb, ctrl_eq_bb))
cd_root = ctrl_eq_bb;
else
break;
}
compute_control_dep_chain (cd_root, use_bb, dep_chains, &num_chains,
&cur_chain, &num_calls);
has_valid_pred
= convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
for (i = 0; i < num_chains; i++)
dep_chains[i].release ();
return has_valid_pred;
}
/* Computes the set of incoming edges of PHI that have non empty
definitions of a phi chain. The collection will be done
recursively on operands that are defined by phis. CD_ROOT
is the control dependence root. *EDGES holds the result, and
VISITED_PHIS is a pointer set for detecting cycles. */
static void
collect_phi_def_edges (gphi *phi, basic_block cd_root,
auto_vec<edge> *edges,
hash_set<gimple *> *visited_phis)
{
size_t i, n;
edge opnd_edge;
tree opnd;
if (visited_phis->add (phi))
return;
n = gimple_phi_num_args (phi);
for (i = 0; i < n; i++)
{
opnd_edge = gimple_phi_arg_edge (phi, i);
opnd = gimple_phi_arg_def (phi, i);
if (TREE_CODE (opnd) != SSA_NAME)
{
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "\n[CHECK] Found def edge %d in ", (int) i);
print_gimple_stmt (dump_file, phi, 0);
}
edges->safe_push (opnd_edge);
}
else
{
gimple *def = SSA_NAME_DEF_STMT (opnd);
if (gimple_code (def) == GIMPLE_PHI
&& dominated_by_p (CDI_DOMINATORS, gimple_bb (def), cd_root))
collect_phi_def_edges (as_a<gphi *> (def), cd_root, edges,
visited_phis);
else if (!uninit_undefined_value_p (opnd))
{
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, "\n[CHECK] Found def edge %d in ",
(int) i);
print_gimple_stmt (dump_file, phi, 0);
}
edges->safe_push (opnd_edge);
}
}
}
}
/* For each use edge of PHI, computes all control dependence chains.
The control dependence chains are then converted to an array of
composite predicates pointed to by PREDS. */
static bool
find_def_preds (pred_chain_union *preds, gphi *phi)
{
size_t num_chains = 0, i, n;
vec<edge> dep_chains[MAX_NUM_CHAINS];
auto_vec<edge, MAX_CHAIN_LEN + 1> cur_chain;
auto_vec<edge> def_edges;
bool has_valid_pred = false;
basic_block phi_bb, cd_root = 0;
phi_bb = gimple_bb (phi);
/* First find the closest dominating bb to be
the control dependence root. */
cd_root = find_dom (phi_bb);
if (!cd_root)
return false;
hash_set<gimple *> visited_phis;
collect_phi_def_edges (phi, cd_root, &def_edges, &visited_phis);
n = def_edges.length ();
if (n == 0)
return false;
for (i = 0; i < n; i++)
{
size_t prev_nc, j;
int num_calls = 0;
edge opnd_edge;
opnd_edge = def_edges[i];
prev_nc = num_chains;
compute_control_dep_chain (cd_root, opnd_edge->src, dep_chains,
&num_chains, &cur_chain, &num_calls);
/* Now update the newly added chains with
the phi operand edge: */
if (EDGE_COUNT (opnd_edge->src->succs) > 1)
{
if (prev_nc == num_chains && num_chains < MAX_NUM_CHAINS)
dep_chains[num_chains++] = vNULL;
for (j = prev_nc; j < num_chains; j++)
dep_chains[j].safe_push (opnd_edge);
}
}
has_valid_pred
= convert_control_dep_chain_into_preds (dep_chains, num_chains, preds);
for (i = 0; i < num_chains; i++)
dep_chains[i].release ();
return has_valid_pred;
}
/* Dump a pred_info. */
static void
dump_pred_info (pred_info one_pred)
{
if (one_pred.invert)
fprintf (dump_file, " (.NOT.) ");
print_generic_expr (dump_file, one_pred.pred_lhs);
fprintf (dump_file, " %s ", op_symbol_code (one_pred.cond_code));
print_generic_expr (dump_file, one_pred.pred_rhs);
}
/* Dump a pred_chain. */
static void
dump_pred_chain (pred_chain one_pred_chain)
{
size_t np = one_pred_chain.length ();
for (size_t j = 0; j < np; j++)
{
dump_pred_info (one_pred_chain[j]);
if (j < np - 1)
fprintf (dump_file, " (.AND.) ");
else
fprintf (dump_file, "\n");
}
}
/* Dumps the predicates (PREDS) for USESTMT. */
static void
dump_predicates (gimple *usestmt, pred_chain_union preds, const char *msg)
{
fprintf (dump_file, "%s", msg);
if (usestmt)
{
print_gimple_stmt (dump_file, usestmt, 0);
fprintf (dump_file, "is guarded by :\n\n");
}
size_t num_preds = preds.length ();
for (size_t i = 0; i < num_preds; i++)
{
dump_pred_chain (preds[i]);
if (i < num_preds - 1)
fprintf (dump_file, "(.OR.)\n");
else
fprintf (dump_file, "\n\n");
}
}
/* Destroys the predicate set *PREDS. */
static void
destroy_predicate_vecs (pred_chain_union *preds)
{
size_t i;
size_t n = preds->length ();
for (i = 0; i < n; i++)
(*preds)[i].release ();
preds->release ();
}
/* Computes the 'normalized' conditional code with operand
swapping and condition inversion. */
static enum tree_code
get_cmp_code (enum tree_code orig_cmp_code, bool swap_cond, bool invert)
{
enum tree_code tc = orig_cmp_code;
if (swap_cond)
tc = swap_tree_comparison (orig_cmp_code);
if (invert)
tc = invert_tree_comparison (tc, false);
switch (tc)
{
case LT_EXPR: