forked from ephox-gcc-plugins/initify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitify_plugin.c
1865 lines (1452 loc) · 44.3 KB
/
initify_plugin.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
/*
* Copyright 2015-2017 by Emese Revfy <[email protected]>
* Licensed under the GPL v2
*
* Homepage:
* https://github.com/ephox-gcc-plugins/initify
*
* This plugin has two passes. The first one tries to find all functions that
* can be become __init/__exit. The second one moves string constants
* (local variables and function string arguments marked by
* the nocapture attribute) only referenced in __init/__exit functions
* to __initconst/__exitconst sections.
* Based on an idea from Mathias Krause <[email protected]>.
*
* The instrumentation pass of the latent_entropy plugin must run after
* the initify plugin to increase coverage.
*
* Options:
* -fplugin-arg-initify_plugin-disable
* -fplugin-arg-initify_plugin-verbose
* -fplugin-arg-initify_plugin-print_missing_attr
* -fplugin-arg-initify_plugin-search_init_exit_functions
* -fplugin-arg-initify_plugin-enable_init_to_exit_moves
* -fplugin-arg-initify_plugin-disable_verify_nocapture_functions
*
* Attribute: __attribute__((nocapture(x, y ...)))
* The nocapture gcc attribute can be on functions only.
* The attribute takes one or more unsigned integer constants as parameters
* that specify the function argument(s) of const char* type to initify.
* If the marked argument is a vararg then the plugin initifies
* all vararg arguments.
* There can be one negative value which means that the return of the function
* will be followed to find it is a nocapture attribute or not.
*
* Attribute: __attribute__((unverified_nocapture(x, y ...)))
* This attribute disables the compile data flow verification of the designated
* nocapture parameters of the function. Use it only on function parameters
* that are difficult for the plugin to analyze.
*
* Usage:
* $ make
* $ make run
*/
#include "gcc-common.h"
__visible int plugin_is_GPL_compatible;
static struct plugin_info initify_plugin_info = {
.version = "20170215",
.help = "disable\tturn off the initify plugin\n"
"verbose\tprint all initified strings and all"
" functions which should be __init/__exit\n"
"print_missing_attr\tprint functions which"
" can be marked by nocapture attribute\n"
"search_init_exit_functions\tfind functions"
" which should be marked by __init or __exit"
" attribute\n"
"enable_init_to_exit_moves\tmove a function"
" to the exit section if it is called by __init"
" and __exit functions too\n"
"disable_verify_nocapture_functions\tdisable"
" the search of capture uses in nocapture"
" functions\n"
};
#define ARGNUM_NONE 0
static bool verbose, print_missing_attr, search_init_exit_functions;
static bool enable_init_to_exit_moves, disable_verify_nocapture_functions;
enum section_type {
INIT, EXIT, BOTH, NONE
};
enum attribute_type {
UNVERIFIED, NOCAPTURE, PRINTF, BUILTINS, SYSCALL, NONE_ATTRIBUTE
};
#if BUILDING_GCC_VERSION >= 5000
typedef struct hash_set<const_gimple> gimple_set;
static inline bool pointer_set_insert(gimple_set *visited, const_gimple stmt)
{
return visited->add(stmt);
}
static inline bool pointer_set_contains(gimple_set *visited, const_gimple stmt)
{
return visited->contains(stmt);
}
static inline gimple_set* pointer_set_create(void)
{
return new hash_set<const_gimple>;
}
static inline void pointer_set_destroy(gimple_set *visited)
{
delete visited;
}
typedef struct hash_set<const_tree> tree_set;
static inline bool pointer_set_insert(tree_set *visited, const_tree node)
{
return visited->add(node);
}
static inline tree_set* tree_pointer_set_create(void)
{
return new hash_set<const_tree>;
}
static inline void pointer_set_destroy(tree_set *visited)
{
delete visited;
}
typedef struct hash_set<struct cgraph_node *> cgraph_set;
static inline bool pointer_set_insert(cgraph_set *visited, struct cgraph_node *node)
{
return visited->add(node);
}
static inline cgraph_set* cgraph_pointer_set_create(void)
{
return new hash_set<struct cgraph_node *>;
}
static inline void pointer_set_destroy(cgraph_set *visited)
{
delete visited;
}
#else
typedef struct pointer_set_t gimple_set;
typedef struct pointer_set_t tree_set;
typedef struct pointer_set_t cgraph_set;
static inline tree_set *tree_pointer_set_create(void)
{
return pointer_set_create();
}
static inline cgraph_set *cgraph_pointer_set_create(void)
{
return pointer_set_create();
}
#endif
static gimple initify_get_def_stmt(const_tree node)
{
gcc_assert(node != NULL_TREE);
if (TREE_CODE(node) != SSA_NAME)
return NULL;
return SSA_NAME_DEF_STMT(node);
}
static void search_constant_strings(bool *has_str_cst, gimple_set *visited, tree node);
static bool has_capture_use_local_var(const_tree vardecl);
static bool search_capture_ssa_use(gimple_set *visited_defs, tree node);
#define FUNCTION_PTR_P(node) \
(TREE_CODE(TREE_TYPE(node)) == POINTER_TYPE && \
(TREE_CODE(TREE_TYPE(TREE_TYPE(node))) == FUNCTION_TYPE || \
TREE_CODE(TREE_TYPE(TREE_TYPE(node))) == METHOD_TYPE))
static bool is_vararg_arg(tree arg_list, unsigned int num)
{
if (tree_last(arg_list) == void_list_node)
return false;
return num >= (unsigned int)list_length(arg_list);
}
static const_tree get_ptr_type(const_tree type)
{
gcc_assert(type != NULL_TREE);
if (TREE_CODE(type) != POINTER_TYPE)
return type;
return get_ptr_type(TREE_TYPE(type));
}
static bool check_parameter(tree *node, tree type_args, int idx)
{
const_tree type_arg, type, type_type, type_name, ptr_type;
if (is_vararg_arg(type_args, idx))
return true;
type_arg = chain_index(idx - 1, type_args);
type = TREE_VALUE(type_arg);
gcc_assert(type != NULL_TREE);
type_type = TREE_TYPE(type);
gcc_assert(type_type != NULL_TREE);
type_name = TYPE_NAME(type_type);
if (type_name != NULL_TREE && TREE_CODE(type_name) == IDENTIFIER_NODE && !strcmp(TYPE_NAME_POINTER(type_type), "va_format"))
return true;
if (TREE_CODE(type) != POINTER_TYPE) {
error("%u. parameter of the %qE function must be a pointer", idx, *node);
return false;
}
ptr_type = get_ptr_type(type_type);
if (!TYPE_READONLY(ptr_type)) {
error("%u. parameter of the %qE function must be readonly", idx, *node);
return false;
}
if (TREE_THIS_VOLATILE(ptr_type)) {
error("%u. parameter of the %qE function can't be volatile", idx, *node);
return false;
}
return true;
}
static bool check_marked_parameters(tree *node, tree type_args, const_tree args, const_tree name)
{
const_tree arg;
bool negative_val;
negative_val = false;
for (arg = args; arg; arg = TREE_CHAIN(arg)) {
int idx;
unsigned int abs_idx;
tree position = TREE_VALUE(arg);
if (TREE_CODE(position) != INTEGER_CST) {
error("%qE parameter of the %qE attribute isn't an integer (fn: %qE)", position, name, *node);
return false;
}
idx = (int)tree_to_shwi(position);
if (negative_val && idx < 0) {
error("Only one negative attribute value is supported (attribute: %qE fn: %qE)", name, *node);
return false;
}
if (idx < 0)
negative_val = true;
abs_idx = abs(idx);
if (abs_idx == 0)
continue;
if (!check_parameter(node, type_args, abs_idx))
return false;
}
return true;
}
static bool check_all_parameters(tree *node, tree type_args)
{
int arg, len = list_length(type_args);
if (tree_last(type_args) == void_list_node)
len -= 1;
for (arg = 1; arg <= len; arg++) {
if (!check_parameter(node, type_args, arg))
return false;
}
return true;
}
/* nocapture attribute:
* * to mark nocapture function arguments. If used on a vararg argument
* it applies to all of them that have no other uses.
* * attribute value 0 is ignored to allow reusing print attribute arguments
*/
static bool handle_initify_attributes(tree *node, tree name, tree args)
{
tree type_args = NULL_TREE;
switch (TREE_CODE(*node)) {
case FUNCTION_DECL:
type_args = TYPE_ARG_TYPES(TREE_TYPE(*node));
break;
case FUNCTION_TYPE:
case METHOD_TYPE:
type_args = TYPE_ARG_TYPES(*node);
break;
case TYPE_DECL: {
enum tree_code fn_code;
const_tree fntype = TREE_TYPE(*node);
fn_code = TREE_CODE(fntype);
if (fn_code == POINTER_TYPE)
fntype = TREE_TYPE(fntype);
fn_code = TREE_CODE(fntype);
if (fn_code == FUNCTION_TYPE || fn_code == METHOD_TYPE) {
type_args = TYPE_ARG_TYPES(fntype);
break;
}
/* FALLTHROUGH */
}
default:
debug_tree(*node);
error("%s: %qE attribute only applies to functions", __func__, name);
return false;
}
gcc_assert(type_args != NULL_TREE);
if (!check_marked_parameters(node, type_args, args, name))
return false;
return args != NULL_TREE || check_all_parameters(node, type_args);
}
static tree handle_nocapture_attribute(tree *node, tree name, tree args, int __unused flags, bool *no_add_attrs)
{
tree nocapture_attr;
*no_add_attrs = true;
if (!handle_initify_attributes(node, name, args))
return NULL_TREE;
nocapture_attr = lookup_attribute("nocapture", DECL_ATTRIBUTES(*node));
if (nocapture_attr)
chainon(TREE_VALUE(nocapture_attr), args);
else
*no_add_attrs = false;
return NULL_TREE;
}
static tree handle_unverified_nocapture_attribute(tree *node, tree name, tree args, int __unused flags, bool *no_add_attrs)
{
tree unverified_attr;
*no_add_attrs = true;
if (!handle_initify_attributes(node, name, args))
return NULL_TREE;
unverified_attr = lookup_attribute("unverified_nocapture", DECL_ATTRIBUTES(*node));
if (unverified_attr)
chainon(TREE_VALUE(unverified_attr), args);
else
*no_add_attrs = false;
return NULL_TREE;
}
static struct attribute_spec nocapture_attr = {
.name = "nocapture",
.min_length = 0,
.max_length = -1,
.decl_required = true,
.type_required = false,
.function_type_required = false,
.handler = handle_nocapture_attribute,
#if BUILDING_GCC_VERSION >= 4007
.affects_type_identity = false
#endif
};
static struct attribute_spec unverified_nocapture_attr = {
.name = "unverified_nocapture",
.min_length = 0,
.max_length = -1,
.decl_required = true,
.type_required = false,
.function_type_required = false,
.handler = handle_unverified_nocapture_attribute,
#if BUILDING_GCC_VERSION >= 4007
.affects_type_identity = false
#endif
};
static void register_attributes(void __unused *event_data, void __unused *data)
{
register_attribute(&nocapture_attr);
register_attribute(&unverified_nocapture_attr);
}
/* Determine whether the function is in the init or exit sections. */
static enum section_type get_init_exit_section(const_tree decl)
{
const char *str;
const_tree section, attr_value;
section = lookup_attribute("section", DECL_ATTRIBUTES(decl));
if (!section)
return NONE;
attr_value = TREE_VALUE(section);
gcc_assert(attr_value != NULL_TREE);
gcc_assert(list_length(attr_value) == 1);
str = TREE_STRING_POINTER(TREE_VALUE(attr_value));
if (!strncmp(str, ".init.", 6))
return INIT;
if (!strncmp(str, ".exit.", 6))
return EXIT;
return NONE;
}
static tree get_string_cst(tree var)
{
if (var == NULL_TREE)
return NULL_TREE;
if (TREE_CODE(var) == STRING_CST)
return var;
switch (TREE_CODE_CLASS(TREE_CODE(var))) {
case tcc_expression:
case tcc_reference: {
int i;
for (i = 0; i < TREE_OPERAND_LENGTH(var); i++) {
tree ret = get_string_cst(TREE_OPERAND(var, i));
if (ret != NULL_TREE)
return ret;
}
break;
}
default:
break;
}
return NULL_TREE;
}
static bool set_init_exit_section(tree decl)
{
gcc_assert(DECL_P(decl));
if (get_init_exit_section(decl) != NONE)
return false;
if (get_init_exit_section(current_function_decl) == INIT)
set_decl_section_name(decl, ".init.rodata.str");
else
set_decl_section_name(decl, ".exit.rodata.str");
return true;
}
/* Syscalls are always nocapture functions. */
static bool is_syscall(const_tree fn)
{
const char *name = DECL_NAME_POINTER(fn);
if (!strncmp(name, "sys_", 4))
return true;
if (!strncmp(name, "sys32_", 6))
return true;
if (!strncmp(name, "compat_sys_", 11))
return true;
return false;
}
/* These builtins are nocapture functions. */
static bool allowed_builtins(const_tree fn)
{
const char *name = DECL_NAME_POINTER(fn);
if (!strcmp(name, "__builtin_va_start"))
return true;
if (!strcmp(name, "__builtin_expect"))
return true;
if (!strcmp(name, "__builtin_memcpy"))
return true;
return false;
}
static enum attribute_type search_argnum_in_attribute_params(const_tree attr, int fn_arg_num, int fntype_arg_len)
{
const_tree attr_val;
for (attr_val = TREE_VALUE(attr); attr_val; attr_val = TREE_CHAIN(attr_val)) {
int attr_arg_val;
if (TREE_CODE(TREE_VALUE(attr_val)) == IDENTIFIER_NODE)
continue;
attr_arg_val = (int)abs(tree_to_shwi(TREE_VALUE(attr_val)));
if (attr_arg_val == fn_arg_num)
return NOCAPTURE;
if (attr_arg_val > fntype_arg_len && fn_arg_num >= attr_arg_val)
return NOCAPTURE;
}
return NONE_ATTRIBUTE;
}
/* Check that fn_arg_num is a nocapture argument, handle cloned functions too. */
static enum attribute_type lookup_nocapture_argument(const_tree fndecl, const_tree attr, int fn_arg_num, int fntype_arg_len)
{
const_tree orig_decl, clone_arg, orig_arg;
tree decl_list, orig_decl_list;
enum attribute_type orig_attribute;
struct cgraph_node *node = cgraph_get_node(fndecl);
orig_attribute = search_argnum_in_attribute_params(attr, fn_arg_num, fntype_arg_len);
if (orig_attribute == NONE_ATTRIBUTE)
return orig_attribute;
gcc_assert(node);
if (node->clone_of && node->clone.tree_map)
gcc_assert(!node->clone.args_to_skip);
if (!DECL_ARTIFICIAL(fndecl) && DECL_ABSTRACT_ORIGIN(fndecl) == NULL_TREE)
return orig_attribute;
orig_decl = DECL_ABSTRACT_ORIGIN(fndecl);
gcc_assert(orig_decl != NULL_TREE);
decl_list = DECL_ARGUMENTS(fndecl);
orig_decl_list = DECL_ARGUMENTS(orig_decl);
if (decl_list == NULL_TREE || orig_decl_list == NULL_TREE)
return NONE_ATTRIBUTE;
if (list_length(decl_list) == list_length(orig_decl_list))
return orig_attribute;
clone_arg = chain_index(fn_arg_num - 1, decl_list);
gcc_assert(clone_arg != NULL_TREE);
orig_arg = chain_index(fn_arg_num - 1, orig_decl_list);
gcc_assert(orig_arg != NULL_TREE);
if (!strcmp(DECL_NAME_POINTER(clone_arg), DECL_NAME_POINTER(orig_arg)))
return orig_attribute;
return NONE_ATTRIBUTE;
}
/* Check whether the function argument is nocapture. */
static enum attribute_type is_fndecl_nocapture_arg(const_tree fndecl, int fn_arg_num)
{
int fntype_arg_len;
const_tree type, attr = NULL_TREE;
bool fnptr = FUNCTION_PTR_P(fndecl);
if (!fnptr && is_syscall(fndecl))
return SYSCALL;
if (!fnptr && DECL_BUILT_IN(fndecl) && allowed_builtins(fndecl))
return BUILTINS;
if (fnptr)
type = TREE_TYPE(TREE_TYPE(fndecl));
else
type = TREE_TYPE(fndecl);
fntype_arg_len = type_num_arguments(type);
if (!fnptr)
attr = lookup_attribute("unverified_nocapture", DECL_ATTRIBUTES(fndecl));
if (attr != NULL_TREE && lookup_nocapture_argument(fndecl, attr, fn_arg_num, fntype_arg_len) != NONE_ATTRIBUTE)
return UNVERIFIED;
attr = lookup_attribute("format", TYPE_ATTRIBUTES(type));
if (attr != NULL_TREE && lookup_nocapture_argument(fndecl, attr, fn_arg_num, fntype_arg_len) != NONE_ATTRIBUTE)
return PRINTF;
if (fnptr)
return NONE_ATTRIBUTE;
attr = lookup_attribute("nocapture", DECL_ATTRIBUTES(fndecl));
if (attr == NULL_TREE)
return NONE_ATTRIBUTE;
if (TREE_VALUE(attr) == NULL_TREE)
return NOCAPTURE;
return lookup_nocapture_argument(fndecl, attr, fn_arg_num, fntype_arg_len);
}
/* Check whether arg_num is a nocapture argument that can be returned. */
static bool is_negative_nocapture_arg(const_tree fndecl, int arg_num)
{
const_tree attr, attr_val;
gcc_assert(arg_num <= 0);
if (FUNCTION_PTR_P(fndecl))
return false;
attr = lookup_attribute("nocapture", DECL_ATTRIBUTES(fndecl));
if (attr == NULL_TREE)
return false;
for (attr_val = TREE_VALUE(attr); attr_val; attr_val = TREE_CHAIN(attr_val)) {
int attr_arg_val;
if (arg_num == 0 && tree_int_cst_lt(TREE_VALUE(attr_val), integer_zero_node))
return true;
attr_arg_val = (int)tree_to_shwi(TREE_VALUE(attr_val));
if (attr_arg_val == arg_num)
return true;
}
return false;
}
static bool is_same_vardecl(const_tree op, const_tree vardecl)
{
const_tree decl;
if (op == vardecl)
return true;
if (TREE_CODE(op) == SSA_NAME)
decl = SSA_NAME_VAR(op);
else
decl = op;
if (decl == NULL_TREE || !DECL_P(decl))
return false;
if (TREE_CODE(decl) != TREE_CODE(vardecl))
return false;
return DECL_NAME(decl) && !strcmp(DECL_NAME_POINTER(decl), DECL_NAME_POINTER(vardecl));
}
static bool search_same_vardecl(const_tree value, const_tree vardecl)
{
int i;
for (i = 0; i < TREE_OPERAND_LENGTH(value); i++) {
const_tree op = TREE_OPERAND(value, i);
if (op == NULL_TREE)
continue;
if (is_same_vardecl(op, vardecl))
return true;
if (search_same_vardecl(op, vardecl))
return true;
}
return false;
}
static bool check_constructor(const_tree constructor, const_tree vardecl)
{
unsigned HOST_WIDE_INT cnt __unused;
tree value;
FOR_EACH_CONSTRUCTOR_VALUE(CONSTRUCTOR_ELTS(constructor), cnt, value) {
if (TREE_CODE(value) == CONSTRUCTOR)
return check_constructor(value, vardecl);
if (is_gimple_constant(value))
continue;
gcc_assert(TREE_OPERAND_LENGTH(value) > 0);
if (search_same_vardecl(value, vardecl))
return true;
}
return false;
}
static bool compare_ops(const_tree vardecl, tree op)
{
if (TREE_CODE(op) == TREE_LIST)
op = TREE_VALUE(op);
if (TREE_CODE(op) == SSA_NAME)
op = SSA_NAME_VAR(op);
if (op == NULL_TREE)
return false;
switch (TREE_CODE_CLASS(TREE_CODE(op))) {
case tcc_declaration:
return is_same_vardecl(op, vardecl);
case tcc_exceptional:
return check_constructor(op, vardecl);
case tcc_constant:
case tcc_statement:
case tcc_comparison:
return false;
default:
break;
}
gcc_assert(TREE_OPERAND_LENGTH(op) > 0);
return search_same_vardecl(op, vardecl);
}
static bool is_stmt_nocapture_arg(const gcall *stmt, int arg_num)
{
tree fndecl;
fndecl = gimple_call_fndecl(stmt);
if (fndecl == NULL_TREE)
fndecl = gimple_call_fn(stmt);
gcc_assert(fndecl != NULL_TREE);
if (is_fndecl_nocapture_arg(fndecl, arg_num) != NONE_ATTRIBUTE)
return true;
/*
* These are potentially nocapture functions that must be checked
* manually.
*/
if (print_missing_attr)
inform(gimple_location(stmt), "nocapture attribute is missing (fn: %E, arg: %u)\n", fndecl, arg_num);
return false;
}
/* Find the argument position of arg. */
static int get_arg_num(const gcall *call, const_tree arg)
{
int idx;
for (idx = 0; idx < (int)gimple_call_num_args(call); idx++) {
const_tree cur_arg = gimple_call_arg(call, idx);
if (cur_arg == arg)
return idx + 1;
}
debug_tree(arg);
debug_gimple_stmt(call);
gcc_unreachable();
}
/* Determine if the variable uses are only in nocapture functions. */
static bool only_nocapture_call(const_tree decl)
{
struct cgraph_edge *e;
struct cgraph_node *caller;
bool has_call = false;
gcc_assert(TREE_CODE(decl) == VAR_DECL);
caller = cgraph_get_node(current_function_decl);
for (e = caller->callees; e; e = e->next_callee) {
int idx;
const gcall *call = as_a_const_gcall(e->call_stmt);
for (idx = 0; idx < (int)gimple_call_num_args(call); idx++) {
const_tree arg = gimple_call_arg(call, idx);
if (TREE_CODE(arg) != ADDR_EXPR)
continue;
if (TREE_OPERAND(arg, 0) != decl)
continue;
has_call = true;
if (!is_stmt_nocapture_arg(call, idx + 1))
return false;
}
}
gcc_assert(has_call);
return has_call;
}
/* Determine if all uses of a va_format typed variable are nocapture. */
static bool is_va_format_use_nocapture(const_tree node)
{
const_tree decl, type;
if (TREE_CODE(node) != COMPONENT_REF)
return false;
decl = TREE_OPERAND(node, 0);
type = TREE_TYPE(decl);
gcc_assert(TREE_CODE(type) == RECORD_TYPE);
if (!TYPE_NAME(type) || strcmp(TYPE_NAME_POINTER(type), "va_format"))
return false;
return only_nocapture_call(decl);
}
/* If there is a cast to integer (from const char) then it is a nocapture data flow */
static bool is_cast_to_integer_type(gassign *assign)
{
const_tree lhs_type, lhs;
if (!gimple_assign_cast_p(assign))
return false;
lhs = gimple_assign_rhs1(assign);
lhs_type = TREE_TYPE(lhs);
return TYPE_MODE(lhs_type) != QImode;
}
/* Search the uses of a return value. */
static bool is_return_value_captured(gimple_set *visited_defs, const gcall *call)
{
tree ret = gimple_call_lhs(call);
gcc_assert(ret != NULL_TREE);
return search_capture_ssa_use(visited_defs, ret);
}
/* Check if arg_num is a nocapture argument. */
static bool is_call_arg_nocapture(gimple_set *visited_defs, const gcall *call, int arg_num)
{
tree fndecl = gimple_call_fndecl(call);
if (fndecl == NULL_TREE)
fndecl = gimple_call_fn(call);
if (fndecl == NULL_TREE)
return false;
if (is_negative_nocapture_arg(fndecl, -arg_num) && is_return_value_captured(visited_defs, call))
return false;
return is_stmt_nocapture_arg(call, arg_num);
}
/* Determine whether the function has at least one nocapture argument. */
static bool has_nocapture_param(const_tree fndecl)
{
const_tree attr;
if (fndecl == NULL_TREE)
return false;
if (is_syscall(fndecl))
return true;
attr = lookup_attribute("nocapture", DECL_ATTRIBUTES(fndecl));
if (attr == NULL_TREE)
attr = lookup_attribute("format", TYPE_ATTRIBUTES(TREE_TYPE(fndecl)));
return attr != NULL_TREE;
}
static void walk_def_stmt(bool *has_capture_use, gimple_set *visited, tree node)
{
gimple def_stmt;
const_tree parm_decl;
if (*has_capture_use)
return;
if (TREE_CODE(node) != SSA_NAME)
goto true_out;
parm_decl = SSA_NAME_VAR(node);
if (parm_decl != NULL_TREE && TREE_CODE(parm_decl) == PARM_DECL)
return;
def_stmt = initify_get_def_stmt(node);
if (pointer_set_insert(visited, def_stmt))
return;
switch (gimple_code(def_stmt)) {
case GIMPLE_CALL: {
tree fndecl = gimple_call_fndecl(def_stmt);
if (fndecl == NULL_TREE)
fndecl = gimple_call_fn(def_stmt);
gcc_assert(fndecl != NULL_TREE);
if (has_nocapture_param(fndecl))
goto true_out;
return;
}
case GIMPLE_ASM:
case GIMPLE_ASSIGN:
goto true_out;
case GIMPLE_NOP:
return;
case GIMPLE_PHI: {
unsigned int i;
for (i = 0; i < gimple_phi_num_args(def_stmt); i++) {
tree arg = gimple_phi_arg_def(def_stmt, i);
walk_def_stmt(has_capture_use, visited, arg);
}
return;
}
default:
debug_gimple_stmt(def_stmt);
error("%s: unknown gimple code", __func__);
gcc_unreachable();
}
gcc_unreachable();
true_out:
*has_capture_use = true;
}
static bool search_return_capture_use(const greturn *ret_stmt)
{
gimple_set *def_visited;
tree ret;
bool has_capture_use;
if (is_negative_nocapture_arg(current_function_decl, 0))
return false;
def_visited = pointer_set_create();
ret = gimple_return_retval(ret_stmt);
has_capture_use = false;
walk_def_stmt(&has_capture_use, def_visited, ret);
pointer_set_destroy(def_visited);
return has_capture_use;
}
static bool lhs_is_a_nocapture_parm_decl(const_tree lhs)
{
int arg_idx, len;
tree arg_list;
if (TREE_CODE(lhs) != PARM_DECL)
return false;
arg_list = DECL_ARGUMENTS(current_function_decl);
len = list_length(arg_list);
for (arg_idx = 0; arg_idx < len; arg_idx++) {
const_tree arg = chain_index(arg_idx, arg_list);
if (arg == lhs)
return is_fndecl_nocapture_arg(current_function_decl, arg_idx + 1) != NONE_ATTRIBUTE;
}
debug_tree(current_function_decl);
debug_tree(lhs);
gcc_unreachable();
}
static void has_capture_use_ssa_var(bool *has_capture_use, gimple_set *visited_defs, tree_set *use_visited, tree node)
{
imm_use_iterator imm_iter;
use_operand_p use_p;
if (pointer_set_insert(use_visited, node))
return;
if (*has_capture_use)
return;
if (is_va_format_use_nocapture(node))
return;
if (lhs_is_a_nocapture_parm_decl(node))
return;
if (TREE_CODE(node) != SSA_NAME)
goto true_out;
FOR_EACH_IMM_USE_FAST(use_p, imm_iter, node) {
gimple use_stmt = USE_STMT(use_p);
if (use_stmt == NULL)
return;
if (is_gimple_debug(use_stmt))
continue;
if (pointer_set_insert(visited_defs, use_stmt))
continue;
switch (gimple_code(use_stmt)) {
case GIMPLE_COND:
case GIMPLE_SWITCH:
return;
case GIMPLE_ASM:
goto true_out;
case GIMPLE_CALL: {
const gcall *call = as_a_const_gcall(use_stmt);
int arg_num = get_arg_num(call, node);
if (is_call_arg_nocapture(visited_defs, call, arg_num))
return;
goto true_out;
}
case GIMPLE_ASSIGN: {
tree lhs;
gassign *assign = as_a_gassign(use_stmt);
const_tree rhs = gimple_assign_rhs1(assign);
if (TREE_CODE(rhs) == INDIRECT_REF)
return;
#if BUILDING_GCC_VERSION >= 4006
if (TREE_CODE(rhs) == MEM_REF)
return;