/
analysis.m
1517 lines (1312 loc) · 57.2 KB
/
analysis.m
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
%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2003-2004, 2006-2011 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%---------------------------------------------------------------------------%
%
% File: analysis.m.
% Main authors: stayl, wangp.
%
% An inter-module analysis framework, as described in
%
% Nicholas Nethercote. The Analysis Framework of HAL,
% Chapter 7: Inter-module Analysis, Master's Thesis,
% University of Melbourne, September 2001, revised April 2002.
% <http://njn.valgrind.org/pubs/masters2001.ps>.
%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- module analysis.
:- interface.
:- import_module hlds.
:- import_module hlds.hlds_module.
:- import_module hlds.hlds_pred.
:- import_module libs.
:- import_module libs.globals.
:- import_module mdbcomp.
:- import_module mdbcomp.prim_data.
:- import_module mdbcomp.sym_name.
:- import_module parse_tree.
:- import_module parse_tree.error_spec.
:- import_module parse_tree.file_names.
:- import_module parse_tree.prog_data.
:- import_module bool.
:- import_module io.
:- import_module list.
:- import_module maybe.
:- import_module set.
:- import_module term.
:- import_module unit.
%---------------------------------------------------------------------------%
% The intention is that eventually any compiler can use this library
% via .NET by defining an instance of this type class.
:- typeclass compiler(Compiler) where [
func compiler_name(Compiler) = string,
% Describe the analyses which can be performed by a compiler.
%
pred analyses(Compiler, analysis_name, analysis_type),
mode analyses(in, in, out) is semidet,
mode analyses(in, out, out) is multi,
% module_name_to_read_file_name(Compiler, Globals, Ext,
% ModuleName, MaybeFileName, !IO)
%
pred module_name_to_read_file_name(Compiler::in, globals::in,
ext::in, module_name::in, maybe_error(string)::out,
io::di, io::uo) is det,
% module_name_to_write_file_name(Compiler, Globals, Ext,
% ModuleName, FileName, !IO)
%
pred module_name_to_write_file_name(Compiler::in, globals::in,
ext::in, module_name::in, string::out, io::di, io::uo) is det
].
:- type analysis_name == string.
:- type analysis_type
---> some [FuncInfo, Call, Answer]
analysis_type(
unit(Call),
unit(Answer)
) => analysis(FuncInfo, Call, Answer).
% An analysis is defined by a type describing call patterns and
% a type defining answer patterns. If the analysis needs to store
% more information about the function being analysed (e.g. arity)
% it should be stored as part of the type for call patterns.
%
:- typeclass analysis(FuncInfo, Call, Answer)
<= (call_pattern(FuncInfo, Call),
answer_pattern(FuncInfo, Answer))
where
[
func analysis_name(Call::unused, Answer::unused) =
(analysis_name::out) is det,
% The version number should be changed when the Call or Answer
% types are changed so that results which use the old types
% can be discarded.
%
func analysis_version_number(Call::unused, Answer::unused) =
(int::out) is det,
func preferred_fixpoint_type(Call::unused, Answer::unused) =
(fixpoint_type::out) is det,
func bottom(FuncInfo::in, Call::unused) = (Answer::out) is det,
func top(FuncInfo::in, Call::unused) = (Answer::out) is det,
pred get_func_info(module_info::in, module_name::in, func_id::in,
Call::unused, Answer::unused, FuncInfo::out) is det
].
:- type fixpoint_type
---> least_fixpoint
% Start at `bottom'.
% Must run to completion.
; greatest_fixpoint.
% Start at `top'.
% Can stop at any time.
:- typeclass call_pattern(FuncInfo, Call)
<= (partial_order(FuncInfo, Call),
to_term(Call))
where [].
:- typeclass answer_pattern(FuncInfo, Answer)
<= (partial_order(FuncInfo, Answer),
to_term(Answer))
where [].
:- type analysis_result(Call, Answer)
---> analysis_result(
ar_call :: Call,
ar_answer :: Answer,
ar_status :: analysis_status
).
:- typeclass partial_order(FuncInfo, T) <= (T -> FuncInfo) where
[
pred more_precise_than(FuncInfo::in, T::in, T::in) is semidet,
pred equivalent(FuncInfo::in, T::in, T::in) is semidet
].
:- typeclass to_term(S) where [
func to_term(S) = term,
pred from_term(term::in, S::out) is semidet
].
:- type no_func_info
---> no_func_info.
% A call pattern that can be used by analyses that do not need
% finer granularity.
%
:- type any_call
---> any_call.
:- instance call_pattern(no_func_info, any_call).
:- instance partial_order(no_func_info, any_call).
:- instance to_term(any_call).
% The status of a module or a specific analysis result.
%
:- type analysis_status
---> invalid
; suboptimal
; optimal.
% This will need to encode language specific details like whether
% it is a predicate or a function, and the arity and mode number.
:- type func_id
---> func_id(
fid_pf :: pred_or_func,
fid_name :: string,
fid_arity :: pred_form_arity,
fid_mode :: proc_id
).
:- type analysis_info.
:- func init_analysis_info(Compiler, module_name, bool) = analysis_info
<= compiler(Compiler).
%---------------------------------------------------------------------------%
% Record the dependency of the module being analysed on the analysis
% result of another module.
% Does nothing if not making the analysis registry or if the result
% that is depended upon comes from a non-local module.
% Automatically makes a request if the call pattern hasn't been seen
% before for that function.
%
:- pred record_dependency(module_name::in, func_id::in, FuncInfo::in,
Call::in, Answer::unused, analysis_info::in, analysis_info::out) is det
<= analysis(FuncInfo, Call, Answer).
% Record an analysis result for a function.
% Abort if the function is not from the module being analysed.
% Does nothing if not making the analysis registry.
%
:- pred record_result(module_name::in, func_id::in, Call::in, Answer::in,
analysis_status::in, analysis_info::in, analysis_info::out) is det
<= analysis(FuncInfo, Call, Answer).
% Record a request from the module being analysed on a function defined
% in an imported module.
% Does nothing if not making the analysis registry or if the function is
% defined in a non-local module.
%
:- pred record_request(analysis_name::in, module_name::in,
func_id::in, Call::in, analysis_info::in, analysis_info::out) is det
<= call_pattern(FuncInfo, Call).
%---------------------------------------------------------------------------%
% Look up all results for a given function.
% If the module is `invalid' then the result list will be empty.
%
% N.B. Newly recorded results will NOT be found. This is intended
% for looking up results from _other_ modules.
%
:- pred lookup_results(analysis_info::in, module_name::in, func_id::in,
list(analysis_result(Call, Answer))::out) is det
<= analysis(FuncInfo, Call, Answer).
% Look up all results for a given function and call pattern CP such
% that the results have call patterns CP' that are equivalent to CP
% or less specific than CP.
%
% N.B. Newly recorded results will NOT be found. This is intended
% for looking up results from _other_ modules.
%
:- pred lookup_matching_results(analysis_info::in, module_name::in,
func_id::in, FuncInfo::in, Call::in,
list(analysis_result(Call, Answer))::out) is det
<= analysis(FuncInfo, Call, Answer).
% Look up the best result matching a given call.
%
% N.B. Newly recorded results will NOT be found. This is intended
% for looking up results from _other_ modules.
%
% If the returned best result has a call pattern that is different
% from the given call pattern, then it is the analysis writer's
% responsibility to request a more precise analysis from the called module,
% using `record_request'.
%
:- pred lookup_best_result(analysis_info::in, module_name::in, func_id::in,
FuncInfo::in, Call::in, maybe(analysis_result(Call, Answer))::out) is det
<= analysis(FuncInfo, Call, Answer).
% Look up call patterns for all results for a given function.
% Even if the module is `invalid' the call patterns will be returned.
%
% You should use this when you want to know which call patterns were
% produced for a procedure defined in the current module in previous
% passes.
%
:- pred lookup_existing_call_patterns(analysis_info::in, analysis_name::in,
module_name::in, func_id::in, list(Call)::out) is det
<= call_pattern(FuncInfo, Call).
% Lookup all the requests for a given function.
% Abort if the function is not from the module being analysed.
%
:- pred lookup_requests(analysis_info::in, analysis_name::in, module_name::in,
func_id::in, list(Call)::out) is det
<= call_pattern(FuncInfo, Call).
%---------------------------------------------------------------------------%
% prepare_intermodule_analysis(ProgressStream, Globals,
% ImportedModuleNames, LocalModuleNames, !Info, !IO)
%
% This predicate should be called before any pass begins to use the
% analysis framework. It ensures that all the analysis files
% are loaded so that lookups can be satisfied. ImportedModuleNames is the
% set of all modules that are directly or indirectly imported by the
% module being analysed. LocalModuleNames is the set of non-"library"
% modules.
%
:- pred prepare_intermodule_analysis(io.text_output_stream::in, globals::in,
set(module_name)::in, set(module_name)::in, list(error_spec)::out,
analysis_info::in, analysis_info::out, io::di, io::uo) is det.
% module_is_local(Info, ModuleName, IsLocal).
%
% IsLocal is `yes' if the module is not a "library" module, i.e. we are
% able to reanalyse the module. The set of local modules is set in
% `prepare_intermodule_analysis'.
%
:- pred module_is_local(analysis_info::in, module_name::in, bool::out) is det.
% Should be called after all analysis is completed to write the
% requests and results for the current compilation to the
% analysis files.
%
:- pred write_analysis_files(io.text_output_stream::in, Compiler::in,
module_info::in, set(module_name)::in, analysis_info::in,
list(error_spec)::out, io::di, io::uo) is det <= compiler(Compiler).
% do_read_module_overall_status(Compiler, Globals, ModuleName,
% MaybeModuleStatus, !IO)
%
% Attempt to read the overall status from a module `.analysis' file.
%
:- pred do_read_module_overall_status(Compiler::in, globals::in,
module_name::in, analysis_status::out, io::di, io::uo) is det
<= compiler(Compiler).
%---------------------------------------------------------------------------%
% Least upper bound of two analysis_status values.
%
:- func lub(analysis_status, analysis_status) = analysis_status.
%---------------------------------------------------------------------------%
:- pred set_analysis_debug(maybe(io.text_output_stream)::in,
io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- include_module analysis.file.
:- import_module analysis.file.
:- import_module parse_tree.module_cmds. % XXX unwanted dependency
:- import_module map.
:- import_module require.
:- import_module string.
:- import_module term_context.
:- import_module type_desc.
:- import_module univ.
%---------------------------------------------------------------------------%
:- type analysis_info
---> some [Compiler]
analysis_info(
compiler :: Compiler,
% The module being analysed.
this_module :: module_name,
% Whether we are making the analysis registry or just using
% results for .analysis files.
make_analysis_registry :: make_analysis_registry,
% The set of local modules, i.e. for which we can issue
% requests.
local_module_names :: set(module_name),
% Holds outstanding requests for more specialised variants
% of procedures. Requests are added to this map as analyses
% proceed and written out to disk at the end of the
% compilation of this module.
analysis_requests_map :: analysis_map(analysis_request),
% The overall status of each module.
module_status_map :: map(module_name, analysis_status),
% The "old" map stores analysis results read in from disk.
% New results generated while analysing the current module
% are added to the "new" map. After all the analyses
% the two maps are compared to see which analysis results
% have changed. Other modules may need to be marked or
% invalidated as a result. Then "new" results are moved
% into the "old" map, from where they can be written to disk.
old_analysis_results :: analysis_map(some_analysis_result),
new_analysis_results :: module_analysis_map(
some_analysis_result),
% The Inter-module Dependency Graph records dependencies
% of an entire module's analysis results on another module's
% answer patterns. e.g. assume module M1 contains function F1
% that has an analysis result that used the answer F2:CP2->AP2
% from module M2. If AP2 changes then all of M1 will either be
% marked `suboptimal' or `invalid'. Finer-grained dependency
% tracking would allow only F1 to be recompiled, instead of
% all of M1, but we don't do that.
%
% IMDGs are loaded from disk into the old map. During analysis,
% any dependences of the current module on other modules
% are added into the new map. At the end of analysis, all the
% arcs which terminate at the current module are cleared
% from the old map, and replaced by those in the new map.
%
% XXX: Check if we really need two maps.
old_imdg_map :: analysis_map(imdg_arc),
new_imdg_map :: analysis_map(imdg_arc)
)
=> compiler(Compiler).
:- type make_analysis_registry
---> make_analysis_registry
; use_analysis_registry_only.
% An analysis result is a call pattern paired with an answer.
% The result has a status associated with it.
%
:- type some_analysis_result
---> some [FuncInfo, Call, Answer]
some_analysis_result(
some_ar_call :: Call,
some_ar_answer :: Answer,
some_ar_status :: analysis_status
)
=> analysis(FuncInfo, Call, Answer).
:- type analysis_request
---> some [FuncInfo, Call]
analysis_request(
req_call :: Call,
req_caller :: module_name
)
=> call_pattern(FuncInfo, Call).
:- type imdg_arc
---> some [FuncInfo, Call]
imdg_arc(
% Call pattern of the analysis result being depended on.
imdg_call :: Call,
% The module that _depends on_ this function's result.
imdg_caller :: module_name
)
=> call_pattern(FuncInfo, Call).
:- type analysis_map(T) == map(module_name, module_analysis_map(T)).
:- type module_analysis_map(T) == map(analysis_name, func_analysis_map(T)).
:- type func_analysis_map(T) == map(func_id, list(T)).
%---------------------------------------------------------------------------%
%
% The "any" call pattern
%
:- instance call_pattern(no_func_info, any_call) where [].
:- instance partial_order(no_func_info, any_call) where [
( more_precise_than(_, _, _) :-
semidet_fail
),
( equivalent(no_func_info, _, _) :-
semidet_succeed
)
].
:- instance to_term(any_call) where [
( to_term(any_call) = Term :-
Term = term.functor(atom("any"), [], dummy_context)
),
( from_term(Term, any_call) :-
Term = term.functor(atom("any"), [], _)
)
].
%---------------------------------------------------------------------------%
init_analysis_info(Compiler, ThisModuleName, MakeAnalysisRegBool) = Info :-
(
MakeAnalysisRegBool = yes,
MakeAnalysisReg = make_analysis_registry
;
MakeAnalysisRegBool = no,
MakeAnalysisReg = use_analysis_registry_only
),
Info = 'new analysis_info'(Compiler, ThisModuleName, MakeAnalysisReg,
set.init, map.init, map.init, map.init, map.init, map.init, map.init).
%---------------------------------------------------------------------------%
record_dependency(CalleeModuleName, FuncId, FuncInfo, Call, DummyAnswer,
!Info) :-
ThisModule = !.Info ^ this_module,
( if ThisModule = CalleeModuleName then
unexpected($pred, "dependency on self")
else
true
),
MakeAnalysisReg = !.Info ^ make_analysis_registry,
module_is_local(!.Info, CalleeModuleName, IsLocal),
( if
MakeAnalysisReg = make_analysis_registry,
IsLocal = yes
then
AnalysisName = analysis_name(Call, DummyAnswer),
record_dependency_2(ThisModule, AnalysisName, CalleeModuleName, FuncId,
Call, !Info),
% If the call pattern that's being depended on hasn't been analysed
% before, make a request for it.
lookup_exactly_matching_result_even_from_invalid_modules(!.Info,
CalleeModuleName, FuncId, FuncInfo, Call, MaybeResult),
(
MaybeResult = no,
record_request(AnalysisName, CalleeModuleName, FuncId, Call, !Info)
;
MaybeResult = yes(Result),
same_type(Result, analysis_result(Call, DummyAnswer, _))
)
else
true
).
:- pred record_dependency_2(module_name::in, analysis_name::in,
module_name::in, func_id::in, Call::in,
analysis_info::in, analysis_info::out) is det
<= call_pattern(FuncInfo, Call).
record_dependency_2(CallerModuleName, AnalysisName, CalleeModuleName, FuncId,
Call, !Info) :-
NewIMDGMap0 = !.Info ^ new_imdg_map,
( if map.search(NewIMDGMap0, CalleeModuleName, Analyses0) then
Analyses1 = Analyses0
else
Analyses1 = map.init
),
( if map.search(Analyses1, AnalysisName, Funcs0) then
Funcs1 = Funcs0
else
Funcs1 = map.init
),
( if map.search(Funcs1, FuncId, FuncArcs0) then
FuncArcs1 = FuncArcs0
else
FuncArcs1 = []
),
Dep = 'new imdg_arc'(Call, CallerModuleName),
% XXX This should really be a set to begin with.
( if list.member(Dep, FuncArcs1) then
true
else
FuncArcs = [Dep | FuncArcs1],
map.set(FuncId, FuncArcs, Funcs1, Funcs),
map.set(AnalysisName, Funcs, Analyses1, Analyses),
map.set(CalleeModuleName, Analyses, NewIMDGMap0, NewIMDGMap),
!Info ^ new_imdg_map := NewIMDGMap
).
:- pred lookup_exactly_matching_result_even_from_invalid_modules(
analysis_info::in, module_name::in, func_id::in, FuncInfo::in, Call::in,
maybe(analysis_result(Call, Answer))::out) is det
<= analysis(FuncInfo, Call, Answer).
lookup_exactly_matching_result_even_from_invalid_modules(Info, ModuleName,
FuncId, FuncInfo, Call, MaybeResult) :-
AllowInvalidModules = yes,
lookup_results_1(Info, ModuleName, FuncId, AllowInvalidModules,
AllResultsList),
ResultList = list.filter(
( pred(R::in) is semidet :-
equivalent(FuncInfo, Call, R ^ ar_call)
), AllResultsList),
(
ResultList = [],
MaybeResult = no
;
ResultList = [Result],
MaybeResult = yes(Result)
;
ResultList = [_, _ | _],
unexpected($pred, "zero or one exactly matching results expected")
).
%---------------------%
record_result(ModuleName, FuncId, CallPattern, AnswerPattern, Status, !Info) :-
( if ModuleName = !.Info ^ this_module then
true
else
unexpected($pred,
"recording result for procedure defined in another module")
),
MakeAnalysisReg = !.Info ^ make_analysis_registry,
(
MakeAnalysisReg = make_analysis_registry,
Map0 = !.Info ^ new_analysis_results,
record_result_in_analysis_map(FuncId, CallPattern, AnswerPattern,
Status, Map0, Map),
!Info ^ new_analysis_results := Map
;
MakeAnalysisReg = use_analysis_registry_only
).
:- pred record_result_in_analysis_map(func_id::in,
Call::in, Answer::in, analysis_status::in,
module_analysis_map(some_analysis_result)::in,
module_analysis_map(some_analysis_result)::out) is det
<= analysis(FuncInfo, Call, Answer).
record_result_in_analysis_map(FuncId, CallPattern, AnswerPattern, Status,
ModuleResults0, ModuleResults) :-
AnalysisName = analysis_name(CallPattern, AnswerPattern),
( if map.search(ModuleResults0, AnalysisName, AnalysisResults0) then
AnalysisResults1 = AnalysisResults0
else
AnalysisResults1 = map.init
),
( if map.search(AnalysisResults1, FuncId, FuncResults0) then
FuncResults1 = FuncResults0
else
FuncResults1 = []
),
Result = 'new some_analysis_result'(CallPattern, AnswerPattern, Status),
FuncResults = [Result | FuncResults1],
ModuleResults =
map.set(ModuleResults0, AnalysisName,
map.set(AnalysisResults1, FuncId, FuncResults)).
%---------------------%
record_request(AnalysisName, ModuleName, FuncId, CallPattern, !Info) :-
ThisModule = !.Info ^ this_module,
( if ThisModule = ModuleName then
unexpected($pred, "request on self")
else
true
),
MakeAnalysisReg = !.Info ^ make_analysis_registry,
module_is_local(!.Info, ModuleName, IsLocal),
( if
MakeAnalysisReg = make_analysis_registry,
IsLocal = yes
then
record_request_2(ThisModule, AnalysisName, ModuleName, FuncId,
CallPattern, !Info)
else
true
).
:- pred record_request_2(module_name::in, analysis_name::in, module_name::in,
func_id::in, Call::in, analysis_info::in, analysis_info::out) is det
<= call_pattern(FuncInfo, Call).
record_request_2(CallerModule, AnalysisName, ModuleName, FuncId, CallPattern,
!Info) :-
RequestsMap0 = !.Info ^ analysis_requests_map,
( if map.search(RequestsMap0, ModuleName, ModuleResults0) then
ModuleResults1 = ModuleResults0
else
ModuleResults1 = map.init
),
( if map.search(ModuleResults1, AnalysisName, AnalysisResults0) then
AnalysisResults1 = AnalysisResults0
else
AnalysisResults1 = map.init
),
( if map.search(AnalysisResults1, FuncId, FuncResults0) then
FuncResults1 = FuncResults0
else
FuncResults1 = []
),
Request = 'new analysis_request'(CallPattern, CallerModule),
FuncResults = [Request | FuncResults1],
map.set(FuncId, FuncResults, AnalysisResults1, AnalysisResults),
map.set(AnalysisName, AnalysisResults, ModuleResults1, ModuleResults),
map.set(ModuleName, ModuleResults, RequestsMap0, RequestsMap),
!Info ^ analysis_requests_map := RequestsMap.
%---------------------------------------------------------------------------%
lookup_results(Info, ModuleName, FuncId, ResultList) :-
AllowInvalidModules = no,
lookup_results_1(Info, ModuleName, FuncId, AllowInvalidModules,
ResultList).
:- pred lookup_results_1(analysis_info::in, module_name::in, func_id::in,
bool::in, list(analysis_result(Call, Answer))::out) is det
<= analysis(FuncInfo, Call, Answer).
lookup_results_1(Info, ModuleName, FuncId, AllowInvalidModules, ResultList) :-
trace [io(!IO)] (
get_debug_analysis_stream(MaybeDebugStream, !IO),
(
MaybeDebugStream = no
;
MaybeDebugStream = yes(DebugStream),
ModuleNameStr = sym_name_to_string(ModuleName),
FuncIdStr = string.string(FuncId),
io.format(DebugStream,
"%% Looking up analysis results for %s.%s\n",
[s(ModuleNameStr), s(FuncIdStr)], !IO)
)
),
( if
AllowInvalidModules = no,
map.search(Info ^ module_status_map, ModuleName, invalid)
then
ResultList = []
else
lookup_results_2(Info ^ old_analysis_results, ModuleName, FuncId,
ResultList),
trace [io(!IO)] (
get_debug_analysis_stream(MaybeDebugStream, !IO),
(
MaybeDebugStream = no
;
MaybeDebugStream = yes(DebugStream),
io.write_string(DebugStream, "% Found these results: ", !IO),
io.print_line(DebugStream, ResultList, !IO)
)
)
).
:- pred lookup_results_2(analysis_map(some_analysis_result)::in,
module_name::in, func_id::in, list(analysis_result(Call, Answer))::out)
is det <= analysis(FuncInfo, Call, Answer).
lookup_results_2(Map, ModuleName, FuncId, ResultList) :-
AnalysisName = analysis_name(_ : Call, _ : Answer),
( if
map.search(Map, ModuleName, ModuleResults),
map.search(ModuleResults, AnalysisName, AnalysisResults),
map.search(AnalysisResults, FuncId, Results)
then
% XXX we might have to discard results which are
% `invalid' or `fixpoint_invalid' if they are written at all
ResultList = list.map(
( func(Result) = analysis_result(Call, Answer, Status) :-
Result = some_analysis_result(Call0, Answer0, Status),
det_univ_to_type(univ(Call0), Call),
det_univ_to_type(univ(Answer0), Answer)
), Results)
else
ResultList = []
).
%---------------------%
lookup_matching_results(Info, ModuleName, FuncId, FuncInfo, Call,
ResultList) :-
lookup_results(Info, ModuleName, FuncId, AllResultsList),
ResultList = list.filter(
( pred(Result::in) is semidet :-
ResultCall = Result ^ ar_call,
( more_precise_than(FuncInfo, Call, ResultCall)
; equivalent(FuncInfo, Call, ResultCall)
)
), AllResultsList).
%---------------------%
lookup_best_result(Info, ModuleName, FuncId, FuncInfo, Call,
MaybeBestResult) :-
trace [io(!IO)] (
get_debug_analysis_stream(MaybeDebugStream, !IO),
(
MaybeDebugStream = no
;
MaybeDebugStream = yes(DebugStream),
ModuleNameStr = sym_name_to_string(ModuleName),
FuncIdStr = string.string(FuncId),
io.format(DebugStream,
"%% Looking up best analysis result for %s.%s\n",
[s(ModuleNameStr), s(FuncIdStr)], !IO)
)
),
lookup_matching_results(Info, ModuleName, FuncId, FuncInfo, Call,
MatchingResults),
(
MatchingResults = [],
MaybeBestResult = no
;
MatchingResults = [Result | Results],
list.foldl(more_precise_answer(FuncInfo), Results, Result, BestResult),
MaybeBestResult = yes(BestResult)
).
:- pred more_precise_answer(FuncInfo::in,
analysis_result(Call, Answer)::in, analysis_result(Call, Answer)::in,
analysis_result(Call, Answer)::out) is det
<= analysis(FuncInfo, Call, Answer).
more_precise_answer(FuncInfo, Result, Best0, Best) :-
ResultAnswer = Result ^ ar_answer,
BestAnswer0 = Best0 ^ ar_answer,
( if more_precise_than(FuncInfo, ResultAnswer, BestAnswer0) then
Best = Result
else
Best = Best0
).
%---------------------%
lookup_existing_call_patterns(Info, AnalysisName, ModuleName, FuncId, Calls) :-
( if ModuleName = Info ^ this_module then
true
else
unexpected($pred, "not this_module")
),
Map = Info ^ old_analysis_results,
( if
map.search(Map, ModuleName, ModuleResults),
map.search(ModuleResults, AnalysisName, AnalysisResults),
map.search(AnalysisResults, FuncId, Results)
then
Calls = list.map(
( func(Result) = Call :-
Result = some_analysis_result(Call0, _Answer, _Status),
det_univ_to_type(univ(Call0), Call)
), Results)
else
Calls = []
).
%---------------------%
lookup_requests(Info, AnalysisName, ModuleName, FuncId, CallPatterns) :-
( if ModuleName = Info ^ this_module then
true
else
unexpected($pred, "not this_module")
),
( if
map.search(Info ^ analysis_requests_map, ModuleName, ModuleRequests),
map.search(ModuleRequests, AnalysisName, AnalysisRequests),
map.search(AnalysisRequests, FuncId, CallPatterns0)
then
CallPatterns1 = list.filter_map(
( func(analysis_request(Call0, _)) = Call is semidet :-
univ(Call) = univ(Call0)
), CallPatterns0),
% Requests simply get appended to `.request' files so when we read them
% back in there may be duplicates.
list.sort_and_remove_dups(CallPatterns1, CallPatterns)
else
CallPatterns = []
).
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
% The algorithm is from Nick's thesis, pp. 108-9.
% Or my corruption thereof.
% See the `analysis/README' file for a reference.
%
% For each new analysis result (P^M:DP --> Ans_new):
% Read in the registry of M if necessary
% If there is an existing analysis result (P^M:DP --> Ans_old):
% if Ans_new \= Ans_old:
% Replace the entry in the registry with P^M:DP --> Ans_new
% if Ans_new `more_precise_than` Ans_old
% Status = suboptimal
% else
% Status = invalid
% For each entry (Q^N:DQ --> P^M:DP) in the IMDG:
% % Mark Q^N:DQ --> _ (_) with Status
% Actually, we don't do that. We only mark the module N's _overall_
% status with the least upper bound of its old status and Status.
% Else (P:DP --> Ans_old) did not exist:
% Insert result (P:DP --> Ans_new) into the registry.
%
% Finally, clear out the "new" analysis results map. When we write out
% the analysis files we will do it from the "old" results map.
%
% In a similar way, any new results which satisfy a request cause the
% module that made the request to be marked suboptimal.
%
:- pred update_analysis_registry(module_info::in,
analysis_info::in, analysis_info::out, io::di, io::uo) is det.
update_analysis_registry(ModuleInfo, !Info, !IO) :-
get_debug_analysis_stream(MaybeDebugStream, !IO),
(
MaybeDebugStream = no
;
MaybeDebugStream = yes(DebugStream),
io.write_string(DebugStream, "% Updating analysis registry.\n", !IO)
),
NewResults = !.Info ^ new_analysis_results,
update_analysis_registry_2(ModuleInfo, !.Info ^ this_module, NewResults,
!Info, !IO),
!Info ^ new_analysis_results := map.init.
:- pred update_analysis_registry_2(module_info::in, module_name::in,
module_analysis_map(some_analysis_result)::in,
analysis_info::in, analysis_info::out, io::di, io::uo) is det.
update_analysis_registry_2(ModuleInfo, ModuleName, ModuleMap, !Info, !IO) :-
map.foldl2(update_analysis_registry_3(ModuleInfo, ModuleName), ModuleMap,
!Info, !IO).
:- pred update_analysis_registry_3(module_info::in, module_name::in,
analysis_name::in, func_analysis_map(some_analysis_result)::in,
analysis_info::in, analysis_info::out, io::di, io::uo) is det.
update_analysis_registry_3(ModuleInfo, ModuleName, AnalysisName, FuncMap,
!Info, !IO) :-
map.foldl2(
update_analysis_registry_4(ModuleInfo, ModuleName, AnalysisName),
FuncMap, !Info, !IO).
:- pred update_analysis_registry_4(module_info::in, module_name::in,
analysis_name::in, func_id::in, list(some_analysis_result)::in,
analysis_info::in, analysis_info::out, io::di, io::uo) is det.
update_analysis_registry_4(ModuleInfo, ModuleName, AnalysisName, FuncId,
NewResults, !Info, !IO) :-
% XXX Currently we do not prevent there being more than one recorded result
% for a given call pattern.
list.foldl2(
update_analysis_registry_5(ModuleInfo, ModuleName, AnalysisName,
FuncId),
NewResults, !Info, !IO).
:- pred update_analysis_registry_5(module_info::in, module_name::in,
analysis_name::in, func_id::in, some_analysis_result::in,
analysis_info::in, analysis_info::out, io::di, io::uo) is det.
update_analysis_registry_5(ModuleInfo, ModuleName, AnalysisName, FuncId,
NewResult, !Info, !IO) :-
NewResult = some_analysis_result(Call, NewAnswer, NewStatus),
get_func_info(ModuleInfo, ModuleName, FuncId, Call, NewAnswer, FuncInfo),
lookup_exactly_matching_result_even_from_invalid_modules(!.Info,
ModuleName, FuncId, FuncInfo, Call, MaybeResult),
module_info_get_globals(ModuleInfo, Globals),
(
MaybeResult = yes(OldResult),
% There was a previous answer for this call pattern.
OldResult = analysis_result(_OldCall, OldAnswer, OldStatus),
( if equivalent(FuncInfo, NewAnswer, OldAnswer) then
get_debug_analysis_stream(MaybeDebugStream, !IO),
(
MaybeDebugStream = no
;
MaybeDebugStream = yes(DebugStream),
write_no_change_in_result(DebugStream, ModuleName, FuncId,
Call, NewAnswer, !IO)
),
( if NewStatus = OldStatus then
true
else
OldMap0 = !.Info ^ old_analysis_results,
replace_result_in_analysis_map(ModuleName, FuncId, FuncInfo,
Call, NewAnswer, NewStatus, OldMap0, OldMap),
!Info ^ old_analysis_results := OldMap
)
else
% Answer has changed.
% Replace the old answer in the registry with the new answer.
OldMap0 = !.Info ^ old_analysis_results,
replace_result_in_analysis_map(ModuleName, FuncId, FuncInfo,
Call, NewAnswer, NewStatus, OldMap0, OldMap),
!Info ^ old_analysis_results := OldMap,
% If the answer is more precise than before then dependent modules
% should be marked suboptimal. Otherwise the answer is less precise
% than it was before, so dependent modules should be invalidated.
( if more_precise_than(FuncInfo, NewAnswer, OldAnswer) then
Status = suboptimal
else
Status = invalid
),
map.lookup(!.Info ^ old_imdg_map, ModuleName, OldArcs),
DepModules = imdg_dependent_modules(OldArcs, AnalysisName,
FuncId, FuncInfo, Call),
get_debug_analysis_stream(MaybeDebugStream, !IO),
(
MaybeDebugStream = no
;
MaybeDebugStream = yes(DebugStream),
write_changed_answer(DebugStream, OldAnswer, NewAnswer,
Status, DepModules, !IO)
),
set.fold2(taint_module_overall_status(Globals, Status), DepModules,
!Info, !IO)
)
;
MaybeResult = no,
% There was no previous answer for this call pattern.
% Just add this result to the registry.
OldAnalysisResults0 = !.Info ^ old_analysis_results,
map.lookup(OldAnalysisResults0, ModuleName, OldMap0),
record_result_in_analysis_map(FuncId, Call, NewAnswer, NewStatus,
OldMap0, OldMap),
map.det_update(ModuleName, OldMap,
OldAnalysisResults0, OldAnalysisResults),
!Info ^ old_analysis_results := OldAnalysisResults
),
% If this new result satisfies a request, then mark the requesting modules
% as suboptimal so they can be reanalysed.
%
% Ideally, we could compare the new answer with either a default answer
% that the calling module probably used, or each request could optionally
% record what answer the caller assumed. Then we could avoid reanalysing
% the calling module unnecessarily. (This only reason we don't implement
% the former is that the structure reuse analysis doesn't implement
% the `top' typeclass method.)
( if
map.search(!.Info ^ analysis_requests_map, ModuleName, ModuleRequests),