-
Notifications
You must be signed in to change notification settings - Fork 0
/
GVN1.v
2994 lines (2875 loc) · 102 KB
/
GVN1.v
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
Require Import Bool.
Require Import Sorting.Permutation.
Require Import Omega.
Require Import sflib.
Require Import Common.
Require Import Value.
Require Import Lang.
Require Import Memory.
Require Import State.
Require Import LoadStore.
Require Import SmallStep.
Require Import SmallStepAux.
Require Import SmallStepWf.
Require Import Refinement.
Require Import SmallStepRefinement.
Require Import Reordering.
Module Ir.
Module GVN1.
(* Some cute lemmas *)
Lemma PTRSZ_MEMSZ:
Nat.shiftl 2 (Ir.PTRSZ - 1) = Ir.MEMSZ.
Proof. unfold Ir.MEMSZ.
rewrite Ir.PTRSZ_def. reflexivity. Qed.
Lemma PTRSZ_MEMSZ2:
Nat.double (Nat.shiftl 1 (Ir.PTRSZ - 1)) = Ir.MEMSZ.
Proof. unfold Ir.MEMSZ. rewrite Ir.PTRSZ_def. reflexivity. Qed.
(*********** A few more useful lemmas **************)
Lemma twos_compl_MEMSZ_PTRSZ:
forall a,
Ir.SmallStep.twos_compl (a mod Ir.MEMSZ) Ir.PTRSZ =
a mod Ir.MEMSZ.
Proof.
intros.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.mod_mod.
reflexivity.
assert (H := Ir.MEMSZ_pos).
omega.
Qed.
Lemma twos_compl_add_lt:
forall a b
(HLT:a + b < Ir.MEMSZ),
Ir.SmallStep.twos_compl_add a b Ir.PTRSZ = a + b.
Proof.
intros.
unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.mod_small. ss. ss.
Qed.
Lemma twos_compl_sub_common_MEMSZ_PTRSZ:
forall x y a,
Ir.SmallStep.twos_compl_sub ((a + x) mod Ir.MEMSZ)
((a + y) mod Ir.MEMSZ) Ir.PTRSZ =
Ir.SmallStep.twos_compl_sub (x mod Ir.MEMSZ) (y mod Ir.MEMSZ) Ir.PTRSZ.
Proof.
intros.
unfold Ir.SmallStep.twos_compl_sub.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
apply addm_subm_eq. pose Ir.MEMSZ_pos. omega.
Qed.
Lemma p2N_addr:
forall bid mb m ofs
(HGET:Some mb = Ir.Memory.get m bid),
Ir.SmallStep.p2N (Ir.plog bid ofs) m Ir.PTRSZ =
(Ir.MemBlock.addr mb + ofs) mod Ir.MEMSZ.
Proof.
intros.
unfold Ir.SmallStep.p2N.
unfold Ir.log_to_phy.
rewrite <- HGET.
rewrite Nat.min_id.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.mod_mod. reflexivity.
assert (H := Ir.MEMSZ_pos). omega.
Qed.
(**************************************************************
This file proves validity of the first GVN optimization case:
1. q is NULL or the result of an integer-to-pointer cast.
High-level structure of proof is as follows:
(1) We define the notion of `physicalized_ptr p1 p2`, meaning
that p2 is derived from (int* )(int)p1.
(Note that in GVN p2 will replace p1.)
(2) We show that a function get_deref, which returns a
dereferenced block (as well as offset), has some good
relation on p1 and p2.
To explain it briefly: if get_deref p1 succeeds,
get_deref p2 also succeeds and returns the same result.
The name of the lemma is physicalized_ptr_get_deref.
(3) Using this, we can show that load/store/free holds
refinement.
(4) For other operations: using p2 instead of p1 makes
the same result.
**************************************************************)
Inductive physicalized_ptr: Ir.Memory.t -> Ir.val -> Ir.val -> Prop :=
| ps_base:
forall m p1 p2
(HP2:Some p2 = Ir.ptr_to_phy m p1),
physicalized_ptr m (Ir.ptr p1) (Ir.ptr p2)
| ps_gep:
forall m p1 p2 idx t inb p1' p2'
(HBASE:physicalized_ptr m (Ir.ptr p1) (Ir.ptr p2))
(HP1':p1' = Ir.SmallStep.gep p1 idx t m inb)
(HP2':p2' = Ir.SmallStep.gep p2 idx t m inb),
physicalized_ptr m p1' p2'.
(*********************************************************
Specification of physicalized_ptr:
If there is 'icmp eq p1, p2',
and 'p2 = Phy (o, [], None)',
and the icmp evaluates to true.
then 'Some p2 = Ir.ptr_to_phy p1' holds.
*********************************************************)
Theorem physicalized_ptr_spec:
forall md st st' r ptrty op1 op2 p1 p2 o e
(HWF:Ir.Config.wf md st)
(HINST:Some (Ir.Inst.iicmp_eq r ptrty op1 op2) = Ir.Config.cur_inst md st)
(HOP1:Some (Ir.ptr p1) = Ir.Config.get_val st op1)
(HOP2:Some (Ir.ptr p2) = Ir.Config.get_val st op2)
(* p2 is pphy(o, [], None) *)
(HP2:p2 = Ir.pphy o nil None)
(HSTEP:Ir.SmallStep.sstep md st (Ir.SmallStep.sr_success e st'))
(* p1 == p2 is true *)
(HTRUE:Some (Ir.num 1) = Ir.Config.get_val st' (Ir.opreg r)),
Some p2 = Ir.ptr_to_phy (Ir.Config.m st) p1.
Proof.
intros.
inv HSTEP.
{ inv HISTEP; try congruence.
{ unfold Ir.SmallStep.inst_det_step in HNEXT.
rewrite <- HINST in HNEXT.
rewrite <- HOP1 in HNEXT.
rewrite <- HOP2 in HNEXT.
unfold Ir.SmallStep.icmp_eq_ptr in HNEXT.
destruct p1 eqn:HP1.
{ (* it's log. *)
dup HOP1.
inv HWF. symmetry in HOP0. apply wf_ptr in HOP0.
inv HOP0. exploit H. ss. intros HH. destruct HH as [HH1 HH2]. inv HH2.
inv HNEXT. unfold Ir.ptr_to_phy.
destruct (Ir.log_to_phy (Ir.Config.m st) b n) eqn:HLTP.
{ unfold Ir.log_to_phy in *. rewrite H1 in HLTP.
inv HLTP. rewrite Ir.SmallStep.get_val_update_reg_and_incrpc in HTRUE.
unfold Ir.Config.get_val in HTRUE.
rewrite Ir.Config.get_rval_update_rval_id in HTRUE.
inv HTRUE. des_ifs.
rewrite Nat.min_id in Heq.
rewrite twos_compl_MEMSZ_PTRSZ in Heq.
rewrite Nat.eqb_eq in Heq.
rewrite Heq. reflexivity.
{ unfold Ir.Config.get_rval in HTRUE.
unfold Ir.Config.update_rval in HTRUE.
des_ifs. congruence. }
}
{ unfold Ir.log_to_phy in HLTP.
des_ifs. }
}
{ (* it's phy. *)
inv HNEXT.
rewrite Ir.SmallStep.get_val_update_reg_and_incrpc in HTRUE.
unfold Ir.Config.get_val in HTRUE.
rewrite Ir.Config.get_rval_update_rval_id in HTRUE.
inv HTRUE.
des_ifs.
rewrite Nat.eqb_eq in Heq.
unfold Ir.ptr_to_phy.
rewrite Heq.
rewrite Nat.min_id.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
f_equal. f_equal; try reflexivity.
symmetry. apply Nat.mod_small.
inv HWF.
symmetry in HOP2. apply wf_ptr in HOP2.
inv HOP2. exploit H1. ss. eauto.
unfold Ir.Config.get_rval in HTRUE. unfold Ir.Config.update_rval in HTRUE.
des_ifs. congruence.
}
}
{ (* well, icmp eq cannot be nondet because op2 is phy *)
rewrite <- HINST in HCUR. inv HCUR.
rewrite <- HOP2 in HOP3.
inv HOP3.
unfold Ir.SmallStep.icmp_eq_ptr_nondet_cond in HNONDET. des_ifs.
}
}
{ (* it's not terminator. :) *)
apply Ir.Config.cur_inst_not_cur_terminator in HINST.
unfold Ir.SmallStep.t_step in HTSTEP. rewrite <- HINST in HTSTEP.
congruence.
}
Qed.
(*********************************************************
Two theorems that NULL and the result of inttoptr is
Phy(o, [], None)!
*********************************************************)
Theorem NULL_is_vanilla_Phy:
Ir.NULL = Ir.pphy 0 nil None.
Proof.
reflexivity.
Qed.
Theorem inttoptr_returns_vanilla_Phy:
forall md st r pty (opint:Ir.op) n st' e
(HWF:Ir.Config.wf md st)
(HINST:Some (Ir.Inst.iinttoptr r opint (Ir.ptrty pty)) = Ir.Config.cur_inst md st)
(HOP1:Some (Ir.num n) = Ir.Config.get_val st opint)
(HSTEP:Ir.SmallStep.sstep md st (Ir.SmallStep.sr_success e st')),
Some (Ir.ptr (Ir.pphy (Ir.SmallStep.twos_compl n Ir.PTRSZ) [] None)) =
Ir.Config.get_val st' (Ir.opreg r).
Proof.
intros.
inv HSTEP.
{ inv HISTEP; try congruence.
unfold Ir.SmallStep.inst_det_step in HNEXT.
rewrite <- HINST in HNEXT.
rewrite <- HOP1 in HNEXT.
inv HNEXT.
rewrite Ir.SmallStep.get_val_update_reg_and_incrpc.
unfold Ir.Config.get_val.
rewrite Ir.Config.get_rval_update_rval_id. reflexivity.
{ unfold Ir.Config.cur_inst in HINST.
unfold Ir.Config.cur_fdef_pc in HINST.
des_ifs.
}
}
{ (* not terminator. :)*)
apply Ir.Config.cur_inst_not_cur_terminator in HINST.
unfold Ir.SmallStep.t_step in HTSTEP.
rewrite <- HINST in HTSTEP.
congruence.
}
Qed.
(***** Properties of physicalized_ptr ******)
Lemma physicalized_ptr_nonlog:
forall m p1 p2
(HPP:physicalized_ptr m (Ir.ptr p1) (Ir.ptr p2)),
~ exists l o, p2 = Ir.plog l o.
Proof.
intros.
remember (Ir.ptr p1) as v1.
remember (Ir.ptr p2) as v2.
generalize dependent p1.
generalize dependent p2.
induction HPP.
{ intros. inv Heqv1. inv Heqv2.
unfold Ir.ptr_to_phy in HP2.
destruct p3.
{ unfold Ir.log_to_phy in HP2.
destruct (Ir.Memory.get m b).
{ intros HH. destruct HH. destruct H. rewrite H in HP2.
congruence. }
{ congruence. }
}
{ intros HH. destruct HH. destruct H. rewrite H in HP2.
congruence. }
}
{ intros. inv Heqv1. inv Heqv2.
intros HH.
destruct HH. destruct H0. rewrite H0 in H1.
eapply IHHPP.
reflexivity. reflexivity.
unfold Ir.SmallStep.gep in H1.
destruct p2.
{ destruct inb.
{ destruct (Ir.Memory.get m b) eqn:HGET.
destruct (Ir.MemBlock.inbounds n t0 &&
Ir.MemBlock.inbounds
(Ir.SmallStep.twos_compl_add n (idx * Ir.ty_bytesz t) Ir.MEMSZ) t0)
eqn:HINB.
eexists. eexists. reflexivity.
eexists. eexists. reflexivity.
eexists. eexists. reflexivity. }
{ eexists. eexists . reflexivity. }
}
{ destruct inb.
{ des_ifs. }
{ congruence. }
}
}
Qed.
Lemma physicalized_ptr_phy:
forall m o1 Is1 cid1 o2 Is2 cid2 v1 v2
(HPP:physicalized_ptr m v1 v2)
(HV1:v1 = Ir.ptr (Ir.pphy o1 Is1 cid1))
(HV2:v2 = Ir.ptr (Ir.pphy o2 Is2 cid2)),
o1 = o2 /\ lsubseq Is1 Is2 /\ cid2 = None.
Proof.
intros.
generalize dependent o1.
generalize dependent Is1.
generalize dependent cid1.
generalize dependent o2.
generalize dependent Is2.
generalize dependent cid2.
induction HPP.
{ intros.
inv HV1.
unfold Ir.ptr_to_phy in HP2. inv HP2.
inv HV2.
split. reflexivity.
split. constructor.
reflexivity.
}
{ intros.
destruct p2'; try congruence.
destruct p1'; try congruence.
inv HV2.
inv HV1.
destruct p2.
{ eapply physicalized_ptr_nonlog in HPP.
exfalso. apply HPP. eexists. eexists. reflexivity.
}
{ unfold Ir.SmallStep.gep in HP1'.
destruct p1 eqn:HP;
destruct inb eqn:HINB.
destruct (Ir.Memory.get m b) eqn:HGET.
destruct (Ir.MemBlock.inbounds n0 t0 &&
Ir.MemBlock.inbounds
(Ir.SmallStep.twos_compl_add n0 (idx * Ir.ty_bytesz t)
Ir.PTRSZ) t0)
eqn:HINB2.
ss.
ss.
ss.
congruence.
{ unfold Ir.SmallStep.gep in HP2'.
exploit IHHPP. ss. ss. intros HH. inv HH. inv H0.
des_ifs.
{ split. ss. split. do 2 constructor. ss. ss. }
{ split. ss. split. do 2 constructor. ss. ss. }
}
{ unfold Ir.SmallStep.gep in HP2'.
inv HP2'. inv HP1'.
exploit IHHPP. ss. ss. intros HH. inv HH. inv H0.
ss.
}
}
}
Qed.
Lemma physicalized_ptr_convert:
forall m l1 o1 o2 Is2 cid2 v1 v2 mb
(HPP:physicalized_ptr m v1 v2)
(HV1:v1 = Ir.ptr (Ir.plog l1 o1))
(HV2:v2 = Ir.ptr (Ir.pphy o2 Is2 cid2))
(HGET:Some mb = Ir.Memory.get m l1),
(Ir.MemBlock.addr mb + o1) mod Ir.MEMSZ = o2.
Proof.
intros.
generalize dependent l1.
generalize dependent o1.
generalize dependent o2.
generalize dependent Is2.
generalize dependent cid2.
generalize dependent mb.
induction HPP.
{ intros.
inv HV1.
unfold Ir.ptr_to_phy in HP2. inv HP2.
inv HV2.
unfold Ir.log_to_phy in H0.
rewrite <- HGET in H0.
congruence.
}
{ intros.
destruct p2'; try congruence.
destruct p1'; try congruence.
inv HV2.
inv HV1.
destruct p2.
{ eapply physicalized_ptr_nonlog in HPP.
exfalso. apply HPP. eexists. eexists. reflexivity.
}
{ unfold Ir.SmallStep.gep in HP1'.
destruct p1 eqn:HP.
{ (* log *)
destruct inb eqn:HINB.
{ (* inbounds *)
destruct (Ir.Memory.get m b) eqn:HGETB; try ss.
exploit IHHPP.
{ reflexivity. }
{ reflexivity. }
{ rewrite HGETB. reflexivity. }
intros HH.
unfold Ir.SmallStep.gep in HP2'.
destruct ((idx * (Ir.ty_bytesz t) <?
Nat.shiftl 1 (Ir.PTRSZ - 1))) eqn:H11.
{ (* positive offset add *)
destruct (n + idx * Ir.ty_bytesz t <? Ir.MEMSZ) eqn:H2; try congruence.
inversion HP2'. subst o2. subst Is2. subst cid2.
rewrite PeanoNat.Nat.ltb_lt in H2.
destruct (Ir.MemBlock.inbounds n0 t0 &&
Ir.MemBlock.inbounds
(Ir.SmallStep.twos_compl_add n0 (idx * Ir.ty_bytesz t)
Ir.PTRSZ) t0)
eqn:HINB2.
{ inversion HP1'. subst l1. subst o1.
unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.add_mod_idemp_r.
rewrite <- HH.
rewrite Nat.add_mod_idemp_l.
rewrite PeanoNat.Nat.add_assoc.
rewrite HGETB in HGET. inv HGET. reflexivity.
apply Ir.MEMSZ_nonzero. apply Ir.MEMSZ_nonzero.
}
{ ss. }
}
{ (* negative offset add *)
des_ifs.
rewrite HGETB in HGET.
inv HGET.
unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.add_mod_idemp_r.
rewrite Nat.add_mod_idemp_l.
rewrite PeanoNat.Nat.add_assoc.
reflexivity.
apply Ir.MEMSZ_nonzero. apply Ir.MEMSZ_nonzero.
}
}
{ (* no inbounds *)
unfold Ir.SmallStep.gep in HP2'.
inv HP2'.
inv HP1'.
exploit IHHPP;try reflexivity; try eassumption.
intros HH. rewrite <- HH.
unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.add_mod_idemp_r.
rewrite Nat.add_mod_idemp_l.
rewrite PeanoNat.Nat.add_assoc. reflexivity.
apply Ir.MEMSZ_nonzero. apply Ir.MEMSZ_nonzero.
}
}
{ des_ifs. }
}
}
Qed.
Lemma addr_pos:
forall mb (HWF:Ir.MemBlock.wf mb),
0 < Ir.MemBlock.addr mb.
Proof.
intros.
inv HWF. unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P mb).
{ inv wf_twin. }
{ simpl. apply neq_0_lt. intros HH. subst.
exploit wf_notnull. constructor. ss. ss. eauto. }
Unshelve. apply 0.
Qed.
Ltac case1 := left; split; reflexivity.
Ltac case2 := right; left; split; [ reflexivity | eexists; reflexivity ].
Ltac case3 := right; right; split; [ eexists; reflexivity | eexists; reflexivity ].
Lemma physicalized_ptr_valty:
forall m v1 v2
(HWF:Ir.Memory.wf m)
(HPP:physicalized_ptr m v1 v2),
(v1 = Ir.poison /\ v2 = Ir.poison) \/
(v1 = Ir.poison /\ exists p2, v2 = Ir.ptr p2) \/
((exists p1, v1 = Ir.ptr p1) /\ exists p2, v2 = Ir.ptr p2).
Proof.
intros.
generalize dependent HWF.
induction HPP.
{ unfold Ir.ptr_to_phy in HP2.
destruct p1.
{ unfold Ir.log_to_phy in HP2.
destruct (Ir.Memory.get m b).
{ right. right. split. eexists. reflexivity.
exists (Ir.pphy ((Ir.MemBlock.addr t + n) mod Ir.MEMSZ) [] None).
congruence. }
congruence.
}
{ inv HP2. case3. }
}
{ intros.
destruct IHHPP.
{ assumption. }
{ destruct H. congruence. }
destruct H.
{ destruct H. congruence. }
destruct H.
destruct H. destruct H0.
inversion H. subst x. inversion H0. subst x0.
(* p2 is never logical. *)
destruct p2.
eapply physicalized_ptr_nonlog in HPP. exfalso. apply HPP.
eexists. eexists. reflexivity.
unfold Ir.SmallStep.gep in HP2'.
unfold Ir.SmallStep.gep in HP1'.
des_ifs; try case1; try case2; try case3.
{ eapply physicalized_ptr_convert in HPP; try reflexivity.
2: rewrite Heq. 2: reflexivity.
rename n0 into ofs.
rename n into absofs.
remember (idx * Ir.ty_bytesz t) as d.
subst absofs.
rewrite PeanoNat.Nat.ltb_nlt in Heq2.
rewrite PeanoNat.Nat.ltb_lt in Heq1.
rewrite andb_true_iff in Heq0.
destruct Heq0.
unfold Ir.MemBlock.inbounds in H2.
unfold Ir.SmallStep.twos_compl_add in H2.
unfold Ir.SmallStep.twos_compl in H2.
rewrite PTRSZ_MEMSZ in H2.
rewrite Ir.MemBlock.inbounds_mod in Heq2; try assumption.
rewrite PeanoNat.Nat.leb_le in H2.
apply not_lt in Heq2.
unfold Ir.MemBlock.inbounds in H1.
rewrite PeanoNat.Nat.leb_le in H1.
assert (Ir.MemBlock.n t0 < Nat.shiftl 1 (Ir.PTRSZ - 1)).
{ inv HWF.
assert (Ir.MemBlock.wf t0).
{ eapply wf_blocks.
symmetry in Heq.
eapply Ir.Memory.get_In in Heq. eassumption.
reflexivity. }
assert (HH := Ir.MemBlock.blocksz_lt t0 H3).
apply not_ge in HH.
assumption. }
rewrite Nat.mod_small in H2.
assert (Ir.MemBlock.addr t0 + Ir.MemBlock.n t0 < Ir.MEMSZ).
{ inv HWF.
exploit wf_blocks. symmetry in Heq.
eapply Ir.Memory.get_In in Heq. eassumption. reflexivity.
intros HH.
inv HH.
eapply wf_inmem.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P t0).
simpl in wf_twin. inv wf_twin. simpl. intuition. }
rewrite <- PTRSZ_MEMSZ2 in Heq2, H4.
unfold Nat.double in *. omega.
rewrite <- PTRSZ_MEMSZ2. unfold Nat.double.
omega.
inv HWF. eapply wf_blocks.
eapply Ir.Memory.get_In. rewrite Heq. reflexivity. reflexivity.
}
{ rewrite Nat.leb_gt in Heq2.
rewrite Nat.ltb_ge in Heq1.
rewrite andb_true_iff in Heq0.
inv Heq0.
exploit physicalized_ptr_convert.
eassumption. ss. ss. rewrite Heq. ss. intros HH.
rewrite Ir.MemBlock.inbounds_mod in HH; try assumption.
unfold Ir.MemBlock.inbounds in *.
rewrite Nat.leb_le in H1, H2.
unfold Ir.SmallStep.twos_compl_add in H2.
unfold Ir.SmallStep.twos_compl in H2.
rewrite Nat.mod_small in H2.
exploit Ir.MemBlock.blocksz_lt.
{ inv HWF. eapply wf_blocks. eapply Ir.Memory.get_In. rewrite Heq. ss. ss. }
{ omega. }
intros HH2. inv HH2. (* False *)
{ rewrite <- HH in Heq2. rewrite Ir.PTRSZ_MEMSZ.
assert (0 < Ir.MemBlock.addr t0).
{ inv HWF. exploit wf_blocks. eapply Ir.Memory.get_In. rewrite Heq. ss. ss.
intros. apply addr_pos. ss.
}
omega.
}
{ inv HWF. eapply wf_blocks. eapply Ir.Memory.get_In. rewrite Heq. ss. ss. }
}
{ exploit physicalized_ptr_phy. eassumption. ss. ss.
intros HH. inv HH. inv H2. congruence. }
{ exploit physicalized_ptr_phy. eassumption. ss. ss.
intros HH. inv HH. inv H2. congruence. }
}
Qed.
(**** lemmas regarding twos_compl_add and inbounds_abs *****)
Lemma inbounds_added_abs_true:
forall m b t0 n0 n ofs
(wf_m:Ir.Memory.wf m)
(HGET:Ir.Memory.get m b = Some t0)
(HPP:(Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ = n)
(HINB:Ir.MemBlock.inbounds
(Ir.SmallStep.twos_compl_add n0 ofs Ir.PTRSZ) t0 = true),
Ir.MemBlock.inbounds_abs
((n + ofs) mod Ir.MEMSZ) t0 = true.
Proof.
intros.
erewrite Ir.MemBlock.inbounds_inbounds_abs in HINB; try reflexivity.
rewrite <- HPP.
assert ((Ir.SmallStep.twos_compl_add n0 ofs Ir.PTRSZ
+ Ir.MemBlock.addr t0) =
((Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ + ofs)
mod Ir.MEMSZ).
{ unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.add_mod_idemp_l.
rewrite Nat.add_comm.
rewrite <- Nat.add_assoc with (n := Ir.MemBlock.addr t0).
rewrite <- Nat.add_mod_idemp_r with (b := (n0 + ofs)).
rewrite Nat.mod_small with
(a := (Ir.MemBlock.addr t0 + (n0 + ofs)
mod Ir.MEMSZ)).
reflexivity.
{ (* Ir.MemBlock.addr t0 + (n0 + idx * Ir.ty_bytesz t)
mod Ir.MEMSZ < Ir.MEMSZ *)
unfold Ir.MemBlock.inbounds_abs in HINB.
unfold in_range in HINB.
rewrite andb_true_iff in HINB.
destruct HINB.
rewrite PeanoNat.Nat.leb_le in H0, H.
unfold Ir.SmallStep.twos_compl_add in H0.
unfold Ir.SmallStep.twos_compl in H0.
rewrite PTRSZ_MEMSZ in H0.
rewrite Nat.add_comm with (m := Ir.MemBlock.addr t0) in H0.
assert (fst (Ir.MemBlock.P0_range t0) + snd (Ir.MemBlock.P0_range t0)
< Ir.MEMSZ).
{ unfold Ir.MemBlock.P0_range.
simpl.
destruct wf_m.
symmetry in HGET.
eapply Ir.Memory.get_In in HGET;try reflexivity.
apply wf_blocks in HGET.
destruct HGET.
apply wf_inmem.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P t0).
{ simpl in wf_twin. unfold Ir.TWINCNT in wf_twin. congruence. }
{ simpl. left. reflexivity. }
}
eapply Nat.le_lt_trans.
eapply H0.
eassumption.
}
apply Ir.MEMSZ_nonzero.
apply Ir.MEMSZ_nonzero.
}
rewrite H in HINB.
assumption.
Qed.
Lemma inbounds_abs_true:
forall m b t0 n0 n
(wf_m:Ir.Memory.wf m)
(HGET:Ir.Memory.get m b = Some t0)
(HPP:(Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ = n)
(HINB:Ir.MemBlock.inbounds n0 t0 = true),
Ir.MemBlock.inbounds_abs n t0 = true.
Proof.
intros.
erewrite Ir.MemBlock.inbounds_inbounds_abs in HINB;
try reflexivity.
rewrite <- HPP.
assert ((Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ =
n0 + Ir.MemBlock.addr t0).
{ unfold Ir.MemBlock.inbounds_abs in HINB.
unfold in_range in HINB.
rewrite andb_true_iff in HINB.
destruct HINB.
rewrite Nat.leb_le in H0.
unfold Ir.MemBlock.P0_range in H0.
simpl in H0.
inv wf_m.
symmetry in HGET.
eapply Ir.Memory.get_In in HGET; try reflexivity.
apply wf_blocks in HGET.
inv HGET.
rewrite Nat.mod_small.
omega.
eapply Nat.le_lt_trans.
rewrite Nat.add_comm.
eassumption.
eapply wf_inmem.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P t0).
{ simpl in wf_twin. unfold Ir.TWINCNT in wf_twin. omega. }
{ simpl. eauto. }
}
rewrite H. assumption.
Qed.
Lemma inbounds_added_abs_true2:
forall m b t0 n0 n ofs sz
(wf_m:Ir.Memory.wf m)
(HGET:Ir.Memory.get m b = Some t0)
(HPP:(Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ = n)
(HINB:Ir.MemBlock.inbounds
(Ir.SmallStep.twos_compl_add n0 ofs Ir.PTRSZ + sz) t0 = true),
Ir.MemBlock.inbounds_abs
((n + ofs) mod Ir.MEMSZ + sz) t0 = true.
Proof.
intros.
erewrite Ir.MemBlock.inbounds_inbounds_abs in HINB; try reflexivity.
rewrite <- HPP.
assert ((Ir.SmallStep.twos_compl_add n0 ofs Ir.PTRSZ
+ Ir.MemBlock.addr t0) =
((Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ + ofs)
mod Ir.MEMSZ).
{ unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite PTRSZ_MEMSZ.
rewrite Nat.add_mod_idemp_l.
rewrite Nat.add_comm.
rewrite <- Nat.add_assoc with (n := Ir.MemBlock.addr t0).
rewrite <- Nat.add_mod_idemp_r with (b := (n0 + ofs)).
rewrite Nat.mod_small with
(a := (Ir.MemBlock.addr t0 + (n0 + ofs)
mod Ir.MEMSZ)).
reflexivity.
{ (* Ir.MemBlock.addr t0 + (n0 + idx * Ir.ty_bytesz t)
mod Ir.MEMSZ < Ir.MEMSZ *)
unfold Ir.MemBlock.inbounds_abs in HINB.
unfold in_range in HINB.
rewrite andb_true_iff in HINB.
destruct HINB.
rewrite PeanoNat.Nat.leb_le in H0, H.
unfold Ir.SmallStep.twos_compl_add in H0.
unfold Ir.SmallStep.twos_compl in H0.
rewrite PTRSZ_MEMSZ in H0.
rewrite Nat.add_comm with (m := Ir.MemBlock.addr t0) in H0.
assert (fst (Ir.MemBlock.P0_range t0) + snd (Ir.MemBlock.P0_range t0)
< Ir.MEMSZ).
{ unfold Ir.MemBlock.P0_range.
simpl.
destruct wf_m.
symmetry in HGET.
eapply Ir.Memory.get_In in HGET;try reflexivity.
apply wf_blocks in HGET.
destruct HGET.
apply wf_inmem.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P t0).
{ simpl in wf_twin. unfold Ir.TWINCNT in wf_twin. congruence. }
{ simpl. left. reflexivity. }
}
eapply Nat.le_lt_trans.
eapply Nat.le_trans with (m := Ir.MemBlock.addr t0 + ((n0 + ofs) mod Ir.MEMSZ + sz)).
omega.
eapply H0.
eassumption.
}
apply Ir.MEMSZ_nonzero.
apply Ir.MEMSZ_nonzero.
}
rewrite <- Nat.add_assoc in HINB.
rewrite Nat.add_comm with (n := sz) in HINB.
rewrite Nat.add_assoc in HINB.
rewrite H in HINB.
assumption.
Qed.
Lemma inbounds_abs_true2:
forall m b t0 n0 n sz
(wf_m:Ir.Memory.wf m)
(HGET:Ir.Memory.get m b = Some t0)
(HPP:(Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ = n)
(HINB:Ir.MemBlock.inbounds (n0 + sz) t0 = true),
Ir.MemBlock.inbounds_abs (n + sz) t0 = true.
Proof.
intros.
erewrite Ir.MemBlock.inbounds_inbounds_abs in HINB;
try reflexivity.
rewrite <- HPP.
assert ((Ir.MemBlock.addr t0 + n0) mod Ir.MEMSZ =
n0 + Ir.MemBlock.addr t0).
{ unfold Ir.MemBlock.inbounds_abs in HINB.
unfold in_range in HINB.
rewrite andb_true_iff in HINB.
destruct HINB.
rewrite Nat.leb_le in H0.
unfold Ir.MemBlock.P0_range in H0.
simpl in H0.
inv wf_m.
symmetry in HGET.
eapply Ir.Memory.get_In in HGET; try reflexivity.
apply wf_blocks in HGET.
inv HGET.
rewrite Nat.mod_small.
omega.
eapply Nat.le_lt_trans.
rewrite Nat.add_comm.
eapply Nat.le_trans with (m := n0 + sz + Ir.MemBlock.addr t0).
omega.
eassumption.
eapply wf_inmem.
unfold Ir.MemBlock.addr.
destruct (Ir.MemBlock.P t0).
{ simpl in wf_twin. unfold Ir.TWINCNT in wf_twin. omega. }
{ simpl. eauto. }
}
rewrite H.
rewrite <- Nat.add_assoc.
rewrite Nat.add_comm with (m := sz).
rewrite Nat.add_assoc.
assumption.
Qed.
Lemma inbounds_tcadd_abs:
forall m b t0 ofs n n0
(wf_m:Ir.Memory.wf m)
(HGET:Ir.Memory.get m b = Some t0)
(HINB:Ir.MemBlock.inbounds
(Ir.SmallStep.twos_compl_add n ofs Ir.PTRSZ) t0 = true)
(HPP:(Ir.MemBlock.addr t0 + n) mod Ir.MEMSZ = n0),
Ir.MemBlock.inbounds_abs
(Ir.SmallStep.twos_compl_add n0 ofs Ir.PTRSZ) t0 = true.
Proof.
intros.
unfold Ir.SmallStep.twos_compl_add.
unfold Ir.SmallStep.twos_compl.
rewrite Ir.PTRSZ_MEMSZ.
eapply inbounds_added_abs_true; try eassumption.
Qed.
(***** A few lemmas about physicalized_ptr ******)
Lemma physicalized_ptr_log_I:
forall v1 v2 m
(HPP:physicalized_ptr m v1 v2),
forall md l1 o1 o2 I2 cid2 mb st
(HM:m = Ir.Config.m st)
(HWF:Ir.Config.wf md st)
(HV1:v1 = Ir.ptr (Ir.plog l1 o1))
(HV2:v2 = Ir.ptr (Ir.pphy o2 I2 cid2))
(HGET:Some mb = Ir.Memory.get (Ir.Config.m st) l1),
List.forallb (fun i => Ir.MemBlock.inbounds_abs i mb) I2 = true.
Proof.
intros v1 v2 st HPP.
induction HPP.
{ intros.
unfold Ir.ptr_to_phy in HP2.
destruct p1.
{ unfold Ir.log_to_phy in HP2.
inv HV1.
rewrite <- HGET in HP2.
inv HP2.
inv HV2. reflexivity. }
{ inv HP2. inv HV2. reflexivity. }
}
{ intros.
inv HV1.
inv HV2.
unfold Ir.SmallStep.gep in H.
des_ifs.
{ unfold Ir.SmallStep.gep in H1.
des_ifs.
{ rewrite Heq in HGET. inv HGET. symmetry in Heq.
simpl.
dup HWF. inv HWF.
eapply physicalized_ptr_convert in HPP; try eassumption; try reflexivity.
rewrite andb_true_iff in Heq0.
destruct Heq0.
rewrite <- HPP.
symmetry in Heq.
erewrite inbounds_abs_true with (n0 := n); try eassumption; try reflexivity.
erewrite inbounds_tcadd_abs; try eassumption; try reflexivity.
erewrite IHHPP; try reflexivity; try eassumption.
congruence.
}
{ rewrite Heq in HGET. inv HGET. symmetry in Heq.
simpl.
dup HWF. inv HWF.
eapply physicalized_ptr_convert in HPP; try eassumption; try reflexivity.
rewrite andb_true_iff in Heq0.
destruct Heq0.
rewrite <- HPP.
symmetry in Heq.
erewrite inbounds_abs_true with (n0 := n); try eassumption; try reflexivity.
erewrite inbounds_tcadd_abs; try eassumption; try reflexivity.
erewrite IHHPP; try reflexivity; try eassumption.
congruence.
}
}
{ unfold Ir.SmallStep.gep in H1.
des_ifs.
erewrite IHHPP; try reflexivity; try eassumption.
}
}
Qed.
(* NOTE: This lemma does not hold anymore if function call is introduced.
This lemma should be replaced with something else which gives criteria
to cid. (ex: cid is never bogus) *)
Lemma physicalized_ptr_log_cid:
forall v1 v2 m
(HPP:physicalized_ptr m v1 v2),
forall md l1 o1 o2 I2 cid2 mb st
(HM:m = Ir.Config.m st)
(HWF:Ir.Config.wf md st)
(HV1:v1 = Ir.ptr (Ir.plog l1 o1))
(HV2:v2 = Ir.ptr (Ir.pphy o2 I2 cid2))
(HGET:Some mb = Ir.Memory.get (Ir.Config.m st) l1),
cid2 = None.
Proof.
intros v1 v2 m HPP.
induction HPP.
{ intros. inv HV1. inv HV2. unfold Ir.ptr_to_phy in HP2.
unfold Ir.log_to_phy in HP2.
des_ifs.
}
{ intros. inv HV1. inv HV2.
unfold Ir.SmallStep.gep in *.
des_ifs.
{ eapply IHHPP.
reflexivity. eassumption. reflexivity. reflexivity. eassumption. }
{ eapply IHHPP.
reflexivity. eassumption. reflexivity. reflexivity. eassumption. }
{ eapply IHHPP.
reflexivity. eassumption. reflexivity. reflexivity. eassumption. }
}
Qed.
Lemma physicalized_ptr_log_get:
forall v1 v2 m
(HPP:physicalized_ptr m v1 v2),
forall md l1 o1 st
(HM:m = Ir.Config.m st)
(HWF:Ir.Config.wf md st)
(HV1:v1 = Ir.ptr (Ir.plog l1 o1)),
exists mb, Some mb = Ir.Memory.get (Ir.Config.m st) l1.
Proof.
intros v1 v2 m HPP.
induction HPP.
{ intros. inv HV1. unfold Ir.ptr_to_phy in HP2.
unfold Ir.log_to_phy in HP2.
des_ifs. eexists. reflexivity.
}
{ intros. inv HV1.
unfold Ir.SmallStep.gep in *.
des_ifs.
{ eapply IHHPP.
reflexivity. eassumption. reflexivity. }
{ eapply IHHPP.
reflexivity. eassumption. reflexivity. }
}
Qed.
Lemma physicalized_ptr_get_deref:
forall md st sz p1 p2
(HWF:Ir.Config.wf md st)
(HSZ:sz> 0)
(HPP:physicalized_ptr (Ir.Config.m st) (Ir.ptr p1) (Ir.ptr p2)),
(exists blk, Ir.get_deref (Ir.Config.m st) p1 sz = [blk] /\
Ir.get_deref (Ir.Config.m st) p2 sz = [blk]) \/
(Ir.get_deref (Ir.Config.m st) p1 sz = []).
Proof.
intros.
destruct p2.
{ (* p2 is never log -> no *)
eapply physicalized_ptr_nonlog in HPP.
exfalso. eapply HPP. eauto. }
destruct p1.
{ (* p1 is log! *)
dup HPP.
dup HPP.
dup HPP.
eapply physicalized_ptr_log_get in HPP; try reflexivity; try eassumption.
destruct HPP.
eapply physicalized_ptr_convert in HPP0; try reflexivity; try eassumption.
eapply physicalized_ptr_log_I in HPP1; try reflexivity; try eassumption.
eapply physicalized_ptr_log_cid in HPP2; try reflexivity; try eassumption.
remember (Ir.get_deref (Ir.Config.m st) (Ir.plog b n0) sz) as res.