summaryrefslogtreecommitdiff
path: root/backend/MyAllocation.v
blob: 0d5f3e3d3a1abffa8f491db0847dcbc9737192be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
Require Import IRC.
Require Import IRCColoring.
Require Import Graph_translation.
Require Import MyRegisters.
Require Import Locations.
Require Import RTLtyping.
Require Import ZArith.
Require Import AST.
Require Import Typed_interfgraphs.
Require Import Edges.
Require Import Graph_Facts.
Require Import Interference_adjacency.
Require Import InterfGraph.
Require Import Conventions.
Require Import Palettes.
Require Import InterfGraph_Construction.
Require Import WS.
Require Import Conservative_criteria.
Require Import IRC_graph.
Require Import IRC_Graph_Functions.
Require Import InterfGraphMapImp.
Require Import Registers.

Import Props Edge RegFacts.

Module ColFacts := FMapFacts.Facts ColorMap.

Definition graph_coloring_aux x int_map float_map env :=
match (Regs.get_type (Regs.P x) env) with
| Tint => match (map_to_coloring int_map (Regs.P x)) with 
          | Some (Regs.P z)  => S (Local Z0 Tint)
          | Some (Regs.M z) => R z
          | None => S (Local (Zpos x) Tint)
          end
| Tfloat => match (map_to_coloring float_map (Regs.P x)) with
            | Some (Regs.P z) => S (Local Z0 Tfloat)
            | Some (Regs.M z) => R z
            | None => S (Local (Zpos x) Tfloat)
            end
end.

Definition reg_translation s :=
Regset.fold (fun v s => VertexSet.add (Regs.reg_to_Reg v) s) s VertexSet.empty.

Definition mreg_translation s :=
MRegset.fold (fun v s => VertexSet.add (Regs.mreg_to_Reg v) s) s VertexSet.empty.

Definition Typed_interfgraphs g env  :=
let regreg_interf_partition :=
regreg_edge_type_partition (interf_reg_reg g) env in
let int_regreg_interf_edge := rr1 regreg_interf_partition in
let float_regreg_interf_edge := rr2 regreg_interf_partition in
let int_regreg_interf_reg := rr3 regreg_interf_partition in
let float_regreg_interf_reg := rr4 regreg_interf_partition in
let regmreg_interf_partition :=
regmreg_edge_type_partition (interf_reg_mreg g) env in
let int_regmreg_interf_edge := rm1 regmreg_interf_partition in
let float_regmreg_interf_edge := rm2 regmreg_interf_partition in
let int_regmreg_interf_reg := rm3 regmreg_interf_partition in
let float_regmreg_interf_reg := rm4 regmreg_interf_partition in
let int_regmreg_interf_mreg := rm5 regmreg_interf_partition in
let float_regmreg_interf_mreg := rm6 regmreg_interf_partition in
let regreg_pref_partition :=
regreg_edge_type_partition (pref_reg_reg g) env in
let int_regreg_pref_edge := rr1 regreg_pref_partition in
let float_regreg_pref_edge := rr2 regreg_pref_partition in
let int_regreg_pref_reg := rr3 regreg_pref_partition in
let float_regreg_pref_reg := rr4 regreg_pref_partition in
let regmreg_pref_partition :=
regmreg_edge_type_partition (pref_reg_mreg g) env in
let int_regmreg_pref_edge := rm1 regmreg_pref_partition in
let float_regmreg_pref_edge := rm2 regmreg_pref_partition in
let int_regmreg_pref_reg := rm3 regmreg_pref_partition in
let float_regmreg_pref_reg := rm4 regmreg_pref_partition in
let int_regmreg_pref_mreg := rm5 regmreg_pref_partition in
let float_regmreg_pref_mreg := rm6 regmreg_pref_partition in
let int_regs := Regset.union int_regreg_interf_reg 
                            (Regset.union int_regmreg_interf_reg
                              (Regset.union int_regreg_pref_reg int_regmreg_pref_reg)) in
let float_regs := Regset.union float_regreg_interf_reg 
                            (Regset.union float_regmreg_interf_reg
                              (Regset.union float_regreg_pref_reg float_regmreg_pref_reg)) in
let int_mregs := MRegset.union int_regmreg_interf_mreg int_regmreg_pref_mreg in
let float_mregs := MRegset.union float_regmreg_interf_mreg float_regmreg_pref_mreg in
let int_Regs := VertexSet.union (reg_translation int_regs) (mreg_translation int_mregs) in
let float_Regs := VertexSet.union (reg_translation float_regs) (mreg_translation float_mregs) in
(int_Regs,
mkgraph int_regreg_interf_edge   int_regmreg_interf_edge   int_regreg_pref_edge   int_regmreg_pref_edge,
float_Regs,
mkgraph float_regreg_interf_edge float_regmreg_interf_edge float_regreg_pref_edge float_regmreg_pref_edge).

Lemma extremities_int_interf_graph : forall g env,
forall e, EdgeSet.In e (interfgraph_affinity_edges (snd (fst (fst (Typed_interfgraphs g env))))) \/
             EdgeSet.In e (interfgraph_interference_edges (snd (fst (fst (Typed_interfgraphs g env))))) ->
             VertexSet.In (fst_ext e) (fst (fst (fst (Typed_interfgraphs g env)))) /\
             VertexSet.In (snd_ext e) (fst (fst (fst (Typed_interfgraphs g env)))).

Proof.
Admitted.

Lemma extremities_float_interf_graph : forall g env,
forall e, EdgeSet.In e (interfgraph_affinity_edges (snd (Typed_interfgraphs g env))) \/
             EdgeSet.In e (interfgraph_interference_edges (snd (Typed_interfgraphs g env))) ->
             VertexSet.In (fst_ext e) (snd (fst (Typed_interfgraphs g env))) /\
             VertexSet.In (snd_ext e) (snd (fst (Typed_interfgraphs g env))).

Proof.
Admitted.

Definition my_graph_coloring g env :=
let typed_graphs := Typed_interfgraphs g env in
let intR := fst (fst (fst typed_graphs)) in
let intG := snd (fst (fst typed_graphs)) in
let floatR := snd (fst typed_graphs) in
let floatG := snd typed_graphs in
let int_graph := graph_translation intG intR (extremities_int_interf_graph g env) in
let float_graph := graph_translation floatG floatR (extremities_float_interf_graph g env) in
let int_map := (IRC_map (graph_to_IRC_graph int_graph int_palette)) in
let float_map := (IRC_map (graph_to_IRC_graph float_graph float_palette)) in
fun x => graph_coloring_aux x int_map float_map env.

Section Coloring_to_allocation.

Variable g : graph.
Variable env : regenv.
Definition typed_graphs := Typed_interfgraphs g env.
Definition intR := fst (fst (fst typed_graphs)).
Definition intG := snd (fst (fst typed_graphs)).
Definition floatR := snd (fst typed_graphs).
Definition floatG := snd typed_graphs.
Definition int_graph := graph_translation intG intR (extremities_int_interf_graph g env).
Definition float_graph := graph_translation floatG floatR (extremities_float_interf_graph g env).
Definition int_map := (IRC_map (graph_to_IRC_graph int_graph int_palette)).
Definition float_map := (IRC_map (graph_to_IRC_graph float_graph float_palette)).
Definition int_coloring := map_to_coloring int_map.
Definition float_coloring := map_to_coloring float_map.

Hypothesis temporaries_out : forall x,
In_graph (Regs.M x) int_graph -> ~List.In (R x) temporaries.

Hypothesis correct_palette_int : forall x,
VertexSet.In x (precolored int_graph) -> VertexSet.In x int_palette.

Hypothesis correct_palette_float : forall x,
VertexSet.In x (precolored float_graph) -> VertexSet.In x float_palette.

Lemma proper_coloring_int : proper_coloring int_coloring int_graph int_palette.

Proof.
intros. apply proper_coloring_IRC_aux.
intro. apply correct_palette_int.
Qed.

Lemma proper_coloring_float : proper_coloring float_coloring float_graph float_palette.

Proof.
intros. apply proper_coloring_IRC_aux.
intro. apply correct_palette_float.
Qed.

Import SetoidList.

Lemma exists_refl : forall x,
exists y, Regs.M x = Regs.M y.

Proof.
intro x. exists x. auto.
Qed.

Lemma mreg_int_palette : forall x,
VertexSet.In x int_palette ->
exists y, x = Regs.M y.

Proof.
unfold int_palette. intros x H.
repeat (destruct (proj1 (Props.Dec.F.add_iff _ _ _) H);
        [inversion H0;subst; apply exists_refl|generalize H0; clear H H0; intro H]).
elim (VertexSet.empty_1 H).
Qed.

Lemma mreg_float_palette : forall x,
VertexSet.In x float_palette ->
exists y, x = Regs.M y.

Proof.
unfold float_palette. intros x H.
repeat (destruct (proj1 (Props.Dec.F.add_iff _ _ _) H);
        [inversion H0;subst; apply exists_refl|generalize H0; clear H H0; intro H]).
elim (VertexSet.empty_1 H).
Qed.

Lemma register_heuristic_mreg : forall x r,
(IRC int_graph int_palette) x = Some r ->
exists y, r = Regs.M y.

Proof.
intros x r H.
apply mreg_int_palette.
generalize (proper_coloring_IRC_aux int_graph int_palette correct_palette_int).
intro H0.
unfold proper_coloring in H0.
unfold proper_coloring_3 in H0.
do 2 destruct H0 as [_ H0].
apply H0 with (x := x).
rewrite H. apply OptionReg.eq_refl.
Qed.

Lemma register_heuristic_mreg_float : forall x r,
(IRC float_graph float_palette) x = Some r ->
exists y, r = Regs.M y.

Proof.
intros x r H.
apply mreg_float_palette.
generalize (proper_coloring_IRC_aux float_graph float_palette correct_palette_float).
intro H0.
unfold proper_coloring in H0.
unfold proper_coloring_3 in H0.
do 2 destruct H0 as [_ H0].
apply H0 with (x := x).
rewrite H. apply OptionReg.eq_refl.
Qed.

Lemma int_palette_type : forall x,
VertexSet.In x int_palette ->
Regs.get_type x env = Tint.

Proof.
unfold int_palette. intros x H.
repeat (destruct (proj1 (Props.Dec.F.add_iff _ _ _) H);
        [inversion H0;subst; auto|generalize H0; clear H H0; intro H]).
elim (VertexSet.empty_1 H).
Qed.

Lemma float_palette_type : forall x,
VertexSet.In x float_palette ->
Regs.get_type x env = Tfloat.

Proof.
unfold float_palette. intros x H.
repeat (destruct (proj1 (Props.Dec.F.add_iff _ _ _) H);
        [inversion H0;subst; auto|generalize H0; clear H H0; intro H]).
elim (VertexSet.empty_1 H).
Qed.

Lemma register_heuristic_type_int : forall x r,
IRC int_graph int_palette x = Some r ->
Regs.get_type r env = Tint.

Proof.
intros x r H.
apply int_palette_type.
generalize (proper_coloring_IRC_aux int_graph int_palette (correct_palette_int)).
intro H0.
unfold proper_coloring in H0. do 2 destruct H0 as [_ H0].
unfold proper_coloring_3 in H0.
apply (H0 x r).
rewrite H. apply OptionReg.eq_refl.
Qed.

Lemma register_heuristic_type_float : forall x r,
IRC float_graph float_palette x = Some r ->
Regs.get_type r env = Tfloat.

Proof.
intros x r H.
apply float_palette_type.
generalize (proper_coloring_IRC_aux float_graph float_palette correct_palette_float).
intro H0.
unfold proper_coloring in H0. do 2 destruct H0 as [_ H0].
unfold proper_coloring_3 in H0.
apply (H0 x r).
rewrite H. apply OptionReg.eq_refl.
Qed.

Lemma Loc_reg_eq_type : forall x,
Regs.get_type (Regs.P x) env = Loc.type (my_graph_coloring g env x).

Proof.
intro x.
unfold my_graph_coloring.
change (snd (fst (fst (Typed_interfgraphs g env)))) with intG.
change (fst (fst (fst (Typed_interfgraphs g env)))) with intR.
change (snd (fst (Typed_interfgraphs g env))) with floatR.
change (snd (Typed_interfgraphs g env)) with floatG.
fold int_graph; fold float_graph.
unfold graph_coloring_aux.
case_eq (Regs.get_type (Regs.P x) env); intros HH.
change (map_to_coloring (IRC_map (graph_to_IRC_graph int_graph int_palette))) with
             (IRC int_graph int_palette).
case_eq (IRC int_graph int_palette (Regs.P x)); intros.
generalize (register_heuristic_mreg _ _ H). intro. destruct H0. rewrite H0.
generalize (register_heuristic_type_int _ _ H).
unfold Regs.get_type. rewrite H0. simpl. auto.
simpl. auto.
change (map_to_coloring (IRC_map (graph_to_IRC_graph float_graph float_palette))) with
             (IRC float_graph float_palette).
case_eq (IRC float_graph float_palette (Regs.P x)); intros.
generalize (register_heuristic_mreg_float _ _ H). intro. destruct H0. rewrite H0.
generalize (register_heuristic_type_float _ _ H).
unfold Regs.get_type. rewrite H0. simpl. auto.
simpl. auto.
Qed.

Lemma regreg_in_fst_partition : forall e s,
SetRegReg.In e s ->
env (fst e) = Tint ->
env (snd e) = Tint ->
SetRegReg.In e (rr1 (regreg_edge_type_partition s env)).

Proof.
intros.
unfold regreg_edge_type_partition.
set (f:=(fun (e0 : SetRegReg.elt) (s0 : regregpartition) =>
         match env (fst e0) with
         | Tint =>
             match env (snd e0) with
             | Tint =>
                 (SetRegReg.add e0 (rr1 s0), rr2 s0,
                 Regset.add (fst e0) (Regset.add (snd e0) (rr3 s0)), 
                 rr4 s0)
             | Tfloat =>
                 (rr1 s0, rr2 s0, Regset.add (fst e0) (rr3 s0),
                 Regset.add (snd e0) (rr4 s0))
             end
         | Tfloat =>
             match env (snd e0) with
             | Tint =>
                 (rr1 s0, rr2 s0, Regset.add (snd e0) (rr3 s0),
                 Regset.add (fst e0) (rr4 s0))
             | Tfloat =>
                 (rr1 s0, SetRegReg.add e0 (rr2 s0), rr3 s0,
                 Regset.add (fst e0) (Regset.add (snd e0) (rr4 s0)))
             end
         end)).
unfold regregpartition in *. fold f.

generalize (SetRegReg.empty,SetRegReg.empty, Regset.empty, Regset.empty).
generalize SetRegReg.elements_1. intro HH.
generalize (HH s e H). clear H HH. intro HH.
intro p. rewrite SetRegReg.fold_1. generalize p. clear p.
induction (SetRegReg.elements s).
inversion HH.
inversion HH.
 subst. intro p. do 3 destruct p. simpl.

assert (f a (t2,t3,t1,t0) = (SetRegReg.add a t2, t3, Regset.add (fst a) (Regset.add (snd a) t1),t0)).
unfold f.

destruct H2. rewrite H in *. rewrite H2 in *. rewrite H0. rewrite H1. simpl. reflexivity.
rewrite H.
destruct H2.

assert (forall x s1 s2 s3 s4, SetRegReg.In x s1 ->
                        SetRegReg.In x (rr1 (fold_left
                            (fun (a0 : SetRegReg.t*SetRegReg.t*Regset.t*Regset.t) (e0 : SetRegReg.elt) => f e0 a0)
                            l (s1, s2, s3, s4)))).

clear H H0 H1 H2 HH IHl.

induction l. 
simpl. auto.
intros x s1 s2 s3 s4 H2.
simpl.

assert (f a0 (s1,s2,s3,s4) = (SetRegReg.add a0 s1, s2,Regset.add (fst a0) (Regset.add (snd a0) s3), s4) \/
        f a0 (s1,s2,s3,s4) = (s1, SetRegReg.add a0 s2, s3, Regset.add (fst a0)(Regset.add (snd a0) s4)) \/
        f a0 (s1,s2,s3,s4) = (s1,s2,Regset.add (fst a0) s3, Regset.add (snd a0) s4)\/
        f a0 (s1,s2,s3,s4) = (s1,s2,Regset.add (snd a0) s3, Regset.add (fst a0) s4)).

unfold f.
destruct (env (fst a0)); destruct (env (snd a0));
unfold rr1,rr2,rr3,rr4; simpl; auto.

destruct H.

rewrite H. apply IHl. apply SetRegReg.add_2. assumption.

destruct H. 
rewrite H.
apply IHl. assumption.
destruct H; rewrite H.
apply IHl. assumption.
apply IHl. assumption.

apply H4. apply SetRegReg.add_1. intuition.

subst. simpl. intro p. apply IHl. assumption.
Qed.

Lemma regreg_in_snd_partition : forall e s,
SetRegReg.In e s ->
env (fst e) = Tfloat ->
env (snd e) = Tfloat ->
SetRegReg.In e (rr2 (regreg_edge_type_partition s env)).

Proof.
intros.
unfold regreg_edge_type_partition.
set (f:=(fun (e0 : SetRegReg.elt) (s0 : regregpartition) =>
         match env (fst e0) with
         | Tint =>
             match env (snd e0) with
             | Tint =>
                 (SetRegReg.add e0 (rr1 s0), rr2 s0,
                 Regset.add (fst e0) (Regset.add (snd e0) (rr3 s0)), 
                 rr4 s0)
             | Tfloat =>
                 (rr1 s0, rr2 s0, Regset.add (fst e0) (rr3 s0),
                 Regset.add (snd e0) (rr4 s0))
             end
         | Tfloat =>
             match env (snd e0) with
             | Tint =>
                 (rr1 s0, rr2 s0, Regset.add (snd e0) (rr3 s0),
                 Regset.add (fst e0) (rr4 s0))
             | Tfloat =>
                 (rr1 s0, SetRegReg.add e0 (rr2 s0), rr3 s0,
                 Regset.add (fst e0) (Regset.add (snd e0) (rr4 s0)))
             end
         end)).
unfold regregpartition in *. fold f.

generalize (SetRegReg.empty,SetRegReg.empty, Regset.empty, Regset.empty).
generalize SetRegReg.elements_1. intro HH.
generalize (HH s e H). clear H HH. intro HH.
intro p. rewrite SetRegReg.fold_1. generalize p. clear p.
induction (SetRegReg.elements s).
inversion HH.
inversion HH.
 subst. intro p. do 3 destruct p. simpl.

assert (f a (t2,t3,t1,t0) = (t2, SetRegReg.add a t3, t1, Regset.add (fst a) (Regset.add (snd a) t0))).
unfold f.

destruct H2. rewrite H in *. rewrite H2 in *. rewrite H0. rewrite H1. simpl. reflexivity.
rewrite H.
destruct H2.

assert (forall x s1 s2 s3 s4, SetRegReg.In x s2 ->
                        SetRegReg.In x (rr2 (fold_left
                            (fun (a0 : SetRegReg.t*SetRegReg.t*Regset.t*Regset.t) (e0 : SetRegReg.elt) => f e0 a0)
                            l (s1, s2, s3, s4)))).

clear H H0 H1 H2 HH IHl.

induction l. 
simpl. auto.
intros x s1 s2 s3 s4 H2.
simpl.

assert (f a0 (s1,s2,s3,s4) = (SetRegReg.add a0 s1, s2,Regset.add (fst a0) (Regset.add (snd a0) s3), s4) \/
        f a0 (s1,s2,s3,s4) = (s1, SetRegReg.add a0 s2, s3, Regset.add (fst a0)(Regset.add (snd a0) s4)) \/
        f a0 (s1,s2,s3,s4) = (s1,s2,Regset.add (fst a0) s3, Regset.add (snd a0) s4)\/
        f a0 (s1,s2,s3,s4) = (s1,s2,Regset.add (snd a0) s3, Regset.add (fst a0) s4)).

unfold f.
destruct (env (fst a0)); destruct (env (snd a0));
unfold rr1,rr2,rr3,rr4; simpl; auto.

destruct H.

rewrite H. apply IHl. assumption.

destruct H. 
rewrite H.
apply IHl. apply SetRegReg.add_2. assumption.
destruct H; rewrite H.
apply IHl. assumption.
apply IHl. assumption.

apply H4. apply SetRegReg.add_1. intuition.

subst. simpl. intro p. apply IHl. assumption.
Qed.

Lemma interf_int_regreg_translation :
        interf_reg_reg (snd (fst (fst (Typed_interfgraphs g env)))) =
        rr1 (regreg_edge_type_partition (interf_reg_reg g) env).

Proof.
unfold Typed_interfgraphs.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. reflexivity.
Qed.

Lemma interf_float_regreg_translation :
        interf_reg_reg (snd (Typed_interfgraphs g env)) =
        rr2 (regreg_edge_type_partition (interf_reg_reg g) env).

Proof.
unfold Typed_interfgraphs.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. reflexivity.
Qed.

Lemma correct_alloc_1 : check_coloring_1 g (my_graph_coloring g env) = true.

Proof.
unfold check_coloring_1.
apply SetRegReg.for_all_1.

unfold compat_bool.
intros x y H. destruct H as [H H0].
rewrite H. rewrite H0. reflexivity.

unfold SetRegReg.For_all.
intros x H.
generalize (Loc_reg_eq_type (fst x)). generalize (Loc_reg_eq_type (snd x)).
unfold my_graph_coloring in *.
change (snd (fst (fst (Typed_interfgraphs g env)))) with intG.
change (fst (fst (fst (Typed_interfgraphs g env)))) with intR.
change (snd (fst (Typed_interfgraphs g env))) with floatR.
change (snd (Typed_interfgraphs g env)) with floatG.
fold int_graph; fold float_graph.
unfold graph_coloring_aux.
change (map_to_coloring (IRC_map (graph_to_IRC_graph int_graph int_palette))) with
             (IRC int_graph int_palette).
change (map_to_coloring (IRC_map (graph_to_IRC_graph float_graph float_palette))) with
             (IRC float_graph float_palette).
intros Locty1 Locty2.
case_eq (Regs.get_type (Regs.P (fst x)) env); intros HH.
case_eq (Regs.get_type (Regs.P (snd x)) env); intros HH0.
case_eq (IRC int_graph int_palette (Regs.P (fst x))); intros.
destruct (register_heuristic_mreg (Regs.P (fst x)) t0 H0). rewrite H1. simpl.
case_eq (IRC int_graph int_palette (Regs.P (snd x))); intros.
destruct (register_heuristic_mreg (Regs.P (snd x)) t1 H2). rewrite H3.

generalize (proper_coloring_IRC_aux int_graph int_palette (correct_palette_int)).
intro H4. unfold proper_coloring in H4.
destruct H4 as [H4 _].
unfold proper_coloring_1 in H4.
assert (~Regs.eq (Regs.M x0) (Regs.M x1)).
apply (H4 (Regs.P (fst x), Regs.P (snd x), None)).
unfold Edge.interf_edge. auto.
unfold int_graph.
right. simpl.
apply regreg_IE_translation. unfold intG, typed_graphs.
rewrite interf_int_regreg_translation.
apply regreg_in_fst_partition. destruct x. auto. auto. auto.
change_rewrite. rewrite H0. rewrite H1. apply OptionReg.eq_refl.
change_rewrite. rewrite H2. rewrite H3. apply OptionReg.eq_refl.
destruct (Loc.eq (R x0) (R x1)). subst.
elim H5. inversion e. auto.
reflexivity.
destruct (Loc.eq (R x0) (S (Local (Zpos (snd x)) Tint))). 
inversion e.
reflexivity.
case_eq (IRC int_graph int_palette (Regs.P (snd x))); intros.
destruct (register_heuristic_mreg (Regs.P (snd x)) t0 H1). rewrite H2.
destruct (Loc.eq (S (Local (Zpos (fst x)) Tint)) (R x0)).
inversion e.
reflexivity.
destruct (Loc.eq (S (Local (Zpos (fst x)) Tint)) (S (Local (Zpos (snd x)) Tint))).
inversion e.
elim (set_reg_reg_diff_ext _ _ (or_introl _ H) H3).
reflexivity.

rewrite HH in *. rewrite HH0 in *.
set (l1 := match IRC int_graph int_palette (Regs.P (fst x)) with
      | Some (Regs.P _) => S (Local 0 Tint)
      | Some (Regs.M z) => R z
      | None => S (Local (Zpos (fst x)) Tint)
      end) in *.
set (l2 :=  match IRC float_graph float_palette (Regs.P (snd x)) with
      | Some (Regs.P _) => S (Local 0 Tfloat)
      | Some (Regs.M z) => R z
      | None => S (Local (Zpos (snd x)) Tfloat)
      end) in *.
destruct (Loc.eq l1 l2). rewrite e in Locty2. rewrite <-Locty1 in Locty2. congruence.
reflexivity.

case_eq (Regs.get_type (Regs.P (snd x)) env); intros HH0.

rewrite HH in *. rewrite HH0 in *.
set (l1 :=match IRC float_graph float_palette (Regs.P (fst x)) with
      | Some (Regs.P _) => S (Local 0 Tfloat)
      | Some (Regs.M z) => R z
      | None => S (Local (Zpos (fst x)) Tfloat)
      end ) in *.
set (l2 :=  match IRC int_graph int_palette (Regs.P (snd x)) with
      | Some (Regs.P _) => S (Local 0 Tint)
      | Some (Regs.M z) => R z
      | None => S (Local (Zpos (snd x)) Tint)
      end) in *.
destruct (Loc.eq l1 l2). rewrite e in Locty2. rewrite <-Locty1 in Locty2. congruence.
reflexivity.

case_eq (IRC float_graph float_palette (Regs.P (fst x))); intros.
destruct (register_heuristic_mreg_float (Regs.P (fst x)) t0 H0). rewrite H1. simpl.
case_eq (IRC float_graph float_palette (Regs.P (snd x))); intros.
destruct (register_heuristic_mreg_float (Regs.P (snd x)) t1 H2). rewrite H3.

generalize (proper_coloring_IRC_aux float_graph float_palette (correct_palette_float)).
intro H4. unfold proper_coloring in H4.
destruct H4 as [H4 _].
unfold proper_coloring_1 in H4.
assert (~Regs.eq (Regs.M x0) (Regs.M x1)).
apply (H4 (Regs.P (fst x), Regs.P (snd x), None)).
unfold Edge.interf_edge. auto.
unfold float_graph.
right. simpl.
apply regreg_IE_translation. unfold floatG, typed_graphs.
rewrite interf_float_regreg_translation.
apply regreg_in_snd_partition. destruct x. auto. auto. auto.
change_rewrite. rewrite H0. rewrite H1. apply OptionReg.eq_refl.
change_rewrite. rewrite H2. rewrite H3. apply OptionReg.eq_refl.
destruct (Loc.eq (R x0) (R x1)). subst.
elim H5. inversion e. auto.
reflexivity.
destruct (Loc.eq (R x0) (S (Local (Zpos (snd x)) Tfloat))). 
inversion e.
reflexivity.
case_eq (IRC float_graph float_palette (Regs.P (snd x))); intros.
destruct (register_heuristic_mreg_float (Regs.P (snd x)) t0 H1). rewrite H2.
destruct (Loc.eq (S (Local (Zpos (fst x)) Tfloat)) (R x0)).
inversion e.
reflexivity.
destruct (Loc.eq (S (Local (Zpos (fst x)) Tfloat)) (S (Local (Zpos (snd x)) Tfloat))).
inversion e.
elim (set_reg_reg_diff_ext _ _ (or_introl _ H) H3).
reflexivity.
Qed.

Lemma interf_int_regmreg_translation :
        interf_reg_mreg (snd (fst (fst (Typed_interfgraphs g env)))) =
        rm1 (regmreg_edge_type_partition (interf_reg_mreg g) env).

Proof.
unfold Typed_interfgraphs.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. reflexivity.
Qed.

Lemma interf_float_regmreg_translation :
        interf_reg_mreg (snd (Typed_interfgraphs g env)) =
        rm2 (regmreg_edge_type_partition (interf_reg_mreg g) env).

Proof.
unfold Typed_interfgraphs.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. reflexivity.
Qed.

Lemma regmreg_in_fst_partition : forall e s,
SetRegMreg.In e s ->
env (fst e) = Tint ->
mreg_type (snd e) = Tint ->
SetRegMreg.In e (rm1 (regmreg_edge_type_partition s env)).

Proof.
intros.
unfold regmreg_edge_type_partition.
set (f := (fun (e0 : SetRegMreg.elt) (s0 : regmregpartition) =>
         match env (fst e0) with
         | Tint =>
             match mreg_type (snd e0) with
             | Tint =>
                 (SetRegMreg.add e0 (rm1 s0), rm2 s0,
                 Regset.add (fst e0) (rm3 s0), rm4 s0,
                 MRegset.add (snd e0) (rm5 s0), rm6 s0)
             | Tfloat =>
                 (rm1 s0, rm2 s0, Regset.add (fst e0) (rm3 s0), rm4 s0,
                 rm5 s0, MRegset.add (snd e0) (rm6 s0))
             end
         | Tfloat =>
             match mreg_type (snd e0) with
             | Tint =>
                 (rm1 s0, rm2 s0, rm3 s0, Regset.add (fst e0) (rm4 s0),
                 MRegset.add (snd e0) (rm5 s0), rm6 s0)
             | Tfloat =>
                 (rm1 s0, SetRegMreg.add e0 (rm2 s0), rm3 s0,
                 Regset.add (fst e0) (rm4 s0), rm5 s0,
                 MRegset.add (snd e0) (rm6 s0))
             end
         end)).
unfold regmregpartition in *. fold f.

generalize (SetRegMreg.empty, SetRegMreg.empty, Regset.empty, Regset.empty, MRegset.empty, MRegset.empty).
generalize SetRegMreg.elements_1. intro HH.
generalize (HH s e H). clear H HH. intro HH.
intro p. rewrite SetRegMreg.fold_1. generalize p. clear p.
induction (SetRegMreg.elements s).
inversion HH.
inversion HH.
 subst. intro p. do 5 destruct p.  simpl.

assert (f a (t4,t5,t3,t2,t1,t0) = (SetRegMreg.add a t4, t5, Regset.add (fst a) t3, t2, MRegset.add (snd a) t1, t0)).
unfold f.

destruct H2. rewrite H in *. rewrite H2 in *. rewrite H0. rewrite H1. simpl. reflexivity.
rewrite H.
destruct H2.

assert (forall x s1 s2 s3 s4 s5 s6, SetRegMreg.In x s1 ->
                        SetRegMreg.In x (rm1 (fold_left
                            (fun (a0 : SetRegMreg.t * SetRegMreg.t * Regset.t * Regset.t *MRegset.t * MRegset.t)
                                    (e0 : SetRegMreg.elt) => f e0 a0)
                            l (s1, s2, s3, s4, s5, s6)))).

clear H H0 H1 H2 HH IHl.

induction l. 
simpl. auto.
intros x s1 s2 s3 s4 s5 s6 H2.
simpl.

assert (f a0 (s1,s2, s3, s4, s5, s6) = (SetRegMreg.add a0 s1, s2, Regset.add (fst a0) s3, s4, MRegset.add (snd a0) s5, s6) \/
        f a0 (s1,s2,s3,s4,s5,s6) = (s1, SetRegMreg.add a0 s2, s3, Regset.add (fst a0) s4, s5, MRegset.add (snd a0) s6) \/
        f a0 (s1,s2,s3,s4,s5,s6) = (s1,s2,Regset.add (fst a0) s3, s4,s5,MRegset.add (snd a0) s6) \/
        f a0 (s1,s2,s3,s4,s5,s6) = (s1,s2,s3, Regset.add (fst a0) s4, MRegset.add (snd a0) s5, s6)).

unfold f.
destruct (env (fst a0)); destruct (mreg_type (snd a0)); auto.

destruct H.

rewrite H. apply IHl. apply SetRegMreg.add_2. assumption.

destruct H. 
rewrite H.
apply IHl. assumption.
destruct H; rewrite H; apply IHl; assumption.

apply H4. apply SetRegMreg.add_1. intuition.

subst. simpl. intro p. apply IHl. assumption.
Qed.

Lemma regmreg_in_snd_partition : forall e s,
SetRegMreg.In e s ->
env (fst e) = Tfloat ->
mreg_type (snd e) = Tfloat ->
SetRegMreg.In e (rm2 (regmreg_edge_type_partition s env)).

Proof.
intros.
unfold regmreg_edge_type_partition.
set (f := (fun (e0 : SetRegMreg.elt) (s0 : regmregpartition) =>
         match env (fst e0) with
         | Tint =>
             match mreg_type (snd e0) with
             | Tint =>
                 (SetRegMreg.add e0 (rm1 s0), rm2 s0,
                 Regset.add (fst e0) (rm3 s0), rm4 s0,
                 MRegset.add (snd e0) (rm5 s0), rm6 s0)
             | Tfloat =>
                 (rm1 s0, rm2 s0, Regset.add (fst e0) (rm3 s0), rm4 s0,
                 rm5 s0, MRegset.add (snd e0) (rm6 s0))
             end
         | Tfloat =>
             match mreg_type (snd e0) with
             | Tint =>
                 (rm1 s0, rm2 s0, rm3 s0, Regset.add (fst e0) (rm4 s0),
                 MRegset.add (snd e0) (rm5 s0), rm6 s0)
             | Tfloat =>
                 (rm1 s0, SetRegMreg.add e0 (rm2 s0), rm3 s0,
                 Regset.add (fst e0) (rm4 s0), rm5 s0,
                 MRegset.add (snd e0) (rm6 s0))
             end
         end)).
unfold regmregpartition in *. fold f.

generalize (SetRegMreg.empty, SetRegMreg.empty, Regset.empty, Regset.empty, MRegset.empty, MRegset.empty).
generalize SetRegMreg.elements_1. intro HH.
generalize (HH s e H). clear H HH. intro HH.
intro p. rewrite SetRegMreg.fold_1. generalize p. clear p.
induction (SetRegMreg.elements s).
inversion HH.
inversion HH.
 subst. intro p. do 5 destruct p.  simpl.

assert (f a (t4,t5,t3,t2,t1,t0) = (t4, SetRegMreg.add a t5, t3, Regset.add (fst a) t2, t1, MRegset.add (snd a) t0)).
unfold f.

destruct H2. rewrite H in *. rewrite H2 in *. rewrite H0. rewrite H1. simpl. reflexivity.
rewrite H.
destruct H2.

assert (forall x s1 s2 s3 s4 s5 s6, SetRegMreg.In x s2 ->
                        SetRegMreg.In x (rm2 (fold_left
                            (fun (a0 : SetRegMreg.t * SetRegMreg.t * Regset.t * Regset.t *MRegset.t * MRegset.t)
                                    (e0 : SetRegMreg.elt) => f e0 a0)
                            l (s1, s2, s3, s4, s5, s6)))).

clear H H0 H1 H2 HH IHl.

induction l. 
simpl. auto.
intros x s1 s2 s3 s4 s5 s6 H2.
simpl.

assert (f a0 (s1,s2, s3, s4, s5, s6) = (SetRegMreg.add a0 s1, s2, Regset.add (fst a0) s3, s4, MRegset.add (snd a0) s5, s6) \/
        f a0 (s1,s2,s3,s4,s5,s6) = (s1, SetRegMreg.add a0 s2, s3, Regset.add (fst a0) s4, s5, MRegset.add (snd a0) s6) \/
        f a0 (s1,s2,s3,s4,s5,s6) = (s1,s2,Regset.add (fst a0) s3, s4,s5,MRegset.add (snd a0) s6) \/
        f a0 (s1,s2,s3,s4,s5,s6) = (s1,s2,s3, Regset.add (fst a0) s4, MRegset.add (snd a0) s5, s6)).

unfold f.
destruct (env (fst a0)); destruct (mreg_type (snd a0)); auto.

destruct H.

rewrite H. apply IHl. assumption.

destruct H.
rewrite H.
apply IHl. apply SetRegMreg.add_2. assumption.
destruct H; rewrite H; apply IHl; assumption.

apply H4. apply SetRegMreg.add_1. intuition.

subst. simpl. intro p. apply IHl. assumption.
Qed.

Section fold_assoc_map.

Variable A : Type.

Lemma fold_left_compat_map : forall (f : ColorMap.t Regs.t -> A -> ColorMap.t Regs.t) l e e',
ColorMap.Equal e e' ->
(forall e1 e2 a, ColorMap.Equal e1 e2 -> ColorMap.Equal (f e1 a) (f e2 a)) ->
ColorMap.Equal (fold_left f l e) (fold_left f l e').

Proof.
intro f;induction l;simpl.
auto.
intros e e' H H0 H1.
apply (IHl (f e a) (f e' a)).
apply H0;assumption.
assumption.
Qed.

Lemma fold_left_assoc_map : forall l (f : ColorMap.t Regs.t -> A -> ColorMap.t Regs.t) x h,
(forall (y z : A) s, ColorMap.Equal (f (f s y) z) (f (f s z) y)) ->
(forall e1 e2 a, ColorMap.Equal e1 e2 -> ColorMap.Equal (f e1 a) (f e2 a)) ->
ColorMap.Equal (fold_left f (h :: l) x) (f (fold_left f l x) h).

Proof.
induction l;simpl;intros f x h H H0.
intuition.
rewrite <-IHl;simpl;try assumption.
apply fold_left_compat_map;[apply H|];auto.
Qed.

End fold_assoc_map.

Lemma mreg_refl_coloring_aux : forall x gpalette,
VertexSet.In x (precolored (irc_g gpalette)) ->
VertexSet.Subset (precolored (irc_g gpalette)) (pal gpalette) ->
OptionReg.eq (map_to_coloring (IRC_map gpalette) x) (Some x).

Proof.
intros. functional induction IRC_map gpalette; simpl in *.

(* simplify *)
generalize (simplify_inv _ _ e). intro.
generalize (simplify_inv2 _ _ e). intro. destruct H2. simpl in *. clear e.
rewrite H2 in *. clear H2. unfold available_coloring.
set (palette := pal g0) in *. set (wl := irc_wl g0) in *. set (g1 := irc_g g0) in *.
case_eq ( VertexSet.choose
         (VertexSet.diff palette
            (forbidden_colors r
               (IRC_map
                  (simplify_irc r g0
                     (VertexSet.choose_1 (s:=get_simplifyWL wl) x0))) g1))).
intros. unfold map_to_coloring.
rewrite ColFacts.add_neq_o.
apply IHt0. unfold simplify_irc. simpl.
rewrite precolored_remove_vertex. apply VertexSet.remove_2.
intro. rewrite <-H3 in H.
generalize (In_simplify_props _ _ _ _ _ _ _ _ (VertexSet.choose_1 H1) (refl_equal _) (HWS_irc g0)). intro.
destruct H4. destruct H5. destruct H6. elim H7. auto. auto.
unfold simplify_irc. simpl. rewrite precolored_remove_vertex.
intro. intro. apply H0. apply (VertexSet.remove_3 H3).
intro. rewrite <-H3 in H.
generalize (In_simplify_props _ _ _ _ _ _ _ _ (VertexSet.choose_1 H1) (refl_equal _) (HWS_irc g0)). intro.
destruct H4. destruct H5. destruct H6. elim H7. auto. intro.
apply IHt0. unfold simplify_irc. simpl.
rewrite precolored_remove_vertex. apply VertexSet.remove_2.
intro. rewrite <-H3 in H.
generalize (In_simplify_props _ _ _ _ _ _ _ _ (VertexSet.choose_1 H1) (refl_equal _) (HWS_irc g0)). intro.
destruct H4. destruct H5. destruct H6. elim H7. auto. auto.
unfold simplify_irc. simpl. rewrite precolored_remove_vertex.
intro. intro. apply H0. apply (VertexSet.remove_3 H3).

(* coalesce *)
assert (forall e', EdgeSet.In e' (get_movesWL (irc_wl g0)) -> In_graph_edge e' (irc_g g0)).
intros.
generalize (In_move_props _ _ _ _ _ _ _ _ H1 (refl_equal _) (HWS_irc g0)).
intuition.
generalize (coalesce_inv _ _ e0). simpl. intro.
generalize (coalesce_inv_2 _ _ e0). intro. destruct H3. destruct H3. simpl in H3. rewrite H3 in *. clear H3.
generalize (any_coalescible_edge_1 _ _ _ _ H1 H2).
intro. destruct H3.
rewrite <-(moves_AE _ _ _ (HWS_irc g0)) in H4.
generalize (proj2 (proj1 (In_graph_aff_edge_in_AE _ _) H4)). intro.
generalize (any_coalescible_edge_2 _ _ _ _ H1 H2). intro.
unfold complete_coloring.
case_eq (ColorMap.find (elt:=Regs.t) (fst_ext e1)
              (IRC_map (merge_irc e1 g0 x0 x1))). 
intros.
unfold map_to_coloring.
rewrite ColFacts.add_neq_o.
apply IHt0.
assert (Edge.aff_edge e1).
rewrite (moves_AE _ _ _ (HWS_irc g0)) in H4.
generalize (In_move_props _ _ _ _ _ _ _ _ H4 (refl_equal _) (HWS_irc g0)).
intuition.
unfold merge_irc. simpl.
rewrite (precolored_merge _ _ H5 H8 _).
apply VertexSet.remove_2. intro. rewrite H9 in H6. elim H6. auto. auto.
unfold VertexSet.Subset in *.
intros. unfold merge_irc in *. simpl in *.
apply H0. rewrite precolored_merge in H8. apply (VertexSet.remove_3 H8).
intro. rewrite H8 in H6. elim H6. auto.
intro.
apply IHt0.
assert (Edge.aff_edge e1).
rewrite (moves_AE _ _ _ (HWS_irc g0)) in H4.
generalize (In_move_props _ _ _ _ _ _ _ _ H4 (refl_equal _) (HWS_irc g0)).
intuition.
unfold merge_irc. simpl.
rewrite (precolored_merge _ _ H5 H8 _).
apply VertexSet.remove_2. intro. rewrite H9 in H6. elim H6. auto. auto.
unfold VertexSet.Subset in *.
intros. unfold merge_irc in *. simpl in *.
apply H0. rewrite precolored_merge in H8. apply (VertexSet.remove_3 H8).

(* freeze *)
generalize (freeze_inv _ _ e1). intro.
generalize (freeze_inv2 _ _ e1). intro. destruct H2. destruct H2. simpl in *. clear e1.
rewrite H2 in *. clear H2. unfold delete_preference_edges_irc2 in *. simpl in *.
apply IHt0.
rewrite precolored_delete_preference_edges. assumption.
unfold VertexSet.Subset in *.
intros. rewrite precolored_delete_preference_edges in H2. auto.

(* spill *)
generalize e2. clear e e0 e1 e2. intro e.
generalize (spill_inv _ _ e). intro.
generalize (spill_inv2 _ _ e). intro. destruct H2. simpl in *. clear e.
rewrite H2 in *. clear H2. unfold available_coloring.
set (palette := pal g0) in *. set (wl := irc_wl g0) in *. set (g1 := irc_g g0) in *.
case_eq ( VertexSet.choose
         (VertexSet.diff palette
            (forbidden_colors r
               (IRC_map
                  (spill_irc r g0
                     (lowest_cost_in r (get_spillWL wl) g1 x0))) g1))).
intros. unfold map_to_coloring.
rewrite ColFacts.add_neq_o.
apply IHt0. unfold spill_irc. simpl.
rewrite precolored_remove_vertex. apply VertexSet.remove_2.
intro. rewrite <-H3 in H.
generalize (In_spill_props _ _ _ _ _ _ _ _ (lowest_cost_in _ _ _ H1) (refl_equal _) (HWS_irc g0)). intro.
destruct H4. destruct H5. elim H6. auto. auto.
unfold spill_irc. simpl. rewrite precolored_remove_vertex.
intro. intro. apply H0. apply (VertexSet.remove_3 H3).
intro. rewrite <-H3 in H.
generalize (In_spill_props _ _ _ _ _ _ _ _ (lowest_cost_in _ _  _ H1) (refl_equal _) (HWS_irc g0)). intro.
destruct H4. destruct H5. elim H6. auto. intro.
apply IHt0. unfold spill_irc. simpl.
rewrite precolored_remove_vertex. apply VertexSet.remove_2.
intro. rewrite <-H3 in H.
generalize (In_spill_props _ _ _ _ _ _ _ _ (lowest_cost_in _ _ _ H1) (refl_equal _) (HWS_irc g0)). intro.
destruct H4. destruct H5. elim H6. auto. auto.
unfold simplify_irc. simpl. rewrite precolored_remove_vertex.
intro. intro. apply H0. apply (VertexSet.remove_3 H3).

(* ending case *)
set (palette := pal g0) in *.
set (g1 := irc_g g0) in *.
assert (map_to_coloring (precoloring_map g1) x = Some x).
unfold precoloring_map.
rewrite VertexSet.fold_1.
generalize VertexSet.elements_1. intro HH.
generalize (HH (precolored g1) x H). clear HH. intro HH.
generalize (NoDupA_elements (precolored g1)). intro HHH.
induction (VertexSet.elements (precolored g1)).
simpl. inversion HH.

unfold map_to_coloring.
rewrite fold_left_assoc_map.
inversion HH. subst.
rewrite ColFacts.add_eq_o.
inversion H2; subst; auto.
apply Regs.eq_sym. auto.
subst.
unfold map_to_coloring in IHl. unfold Regs.t. 
rewrite ColFacts.add_neq_o.
apply IHl. assumption. inversion HHH. assumption.
inversion HHH. subst. intro H6.
elim H4.
inversion H6; subst; auto.

intros.
unfold ColorMap.Equal. 
intro.
destruct (Regs.eq_dec z y0).
rewrite ColFacts.add_eq_o.
destruct (Regs.eq_dec y y0).
rewrite ColFacts.add_eq_o.
inversion e3; inversion e4; subst; rewrite H6; auto.
auto.
rewrite ColFacts.add_neq_o.
rewrite ColFacts.add_eq_o.
reflexivity.
assumption.
assumption.
assumption.
rewrite ColFacts.add_neq_o.
destruct (Regs.eq_dec y y0).
rewrite ColFacts.add_eq_o.
rewrite ColFacts.add_eq_o.
reflexivity.
assumption.
assumption.
rewrite ColFacts.add_neq_o.
rewrite ColFacts.add_neq_o.
rewrite ColFacts.add_neq_o.
reflexivity.
assumption.
assumption.
assumption.
assumption.

intros.
apply ColFacts.add_m.
apply Regs.eq_refl.
reflexivity.
assumption.

rewrite H1. apply OptionReg.eq_refl.
Qed.

Lemma mreg_refl_coloring : forall x g palette,
VertexSet.In x (precolored g) ->
VertexSet.Subset (precolored g) palette ->
OptionReg.eq (IRC g palette x) (Some x).

Proof.
intros.
apply mreg_refl_coloring_aux;
unfold graph_to_IRC_graph; simpl; auto.
Qed.

Lemma loc_type_reg_type_equiv : forall x,
Loc.type (R x) = Regs.get_type (Regs.M x) env.

Proof.
intro x.
unfold Loc.type. unfold Regs.get_type. reflexivity.
Qed.

Lemma correct_alloc_2 : check_coloring_2 g (my_graph_coloring g env) = true.

Proof.
unfold check_coloring_2.
apply SetRegMreg.for_all_1.

unfold compat_bool.
intros x y H. destruct H as [H H0].
rewrite H. rewrite H0. reflexivity.

unfold SetRegMreg.For_all.
intros x H.
generalize (Loc_reg_eq_type (fst x)). generalize (loc_type_reg_type_equiv (snd x)).
unfold my_graph_coloring in *.
change (snd (fst (fst (Typed_interfgraphs g env)))) with intG.
change (fst (fst (fst (Typed_interfgraphs g env)))) with intR.
change (snd (fst (Typed_interfgraphs g env))) with floatR.
change (snd (Typed_interfgraphs g env)) with floatG.
fold int_graph; fold float_graph.
unfold graph_coloring_aux.
change (map_to_coloring (IRC_map (graph_to_IRC_graph int_graph int_palette))) with
             (IRC int_graph int_palette).
change (map_to_coloring (IRC_map (graph_to_IRC_graph float_graph float_palette))) with
             (IRC float_graph float_palette).
intros Locty1 Locty2.
case_eq (Regs.get_type (Regs.P (fst x)) env); intros HH. rewrite HH in *.
case_eq (Regs.get_type (Regs.M (snd x)) env); intros HH0.
case_eq (IRC int_graph int_palette (Regs.P (fst x))); intros. rewrite H0 in *.
destruct (register_heuristic_mreg (Regs.P (fst x)) t0 H0). rewrite H1 in *. simpl.
case_eq (IRC int_graph int_palette (Regs.M (snd x))); intros.
destruct (register_heuristic_mreg (Regs.M (snd x)) t1 H2).

generalize (proper_coloring_IRC_aux int_graph int_palette (correct_palette_int)).
intro H4. unfold proper_coloring in H4.
destruct H4 as [H4 _].
unfold proper_coloring_1 in H4.
assert (~Regs.eq (Regs.M x0) (Regs.M x1)).
apply (H4 (Regs.P (fst x), Regs.M (snd x), None)).
unfold Edge.interf_edge. auto.
unfold int_graph.
right. simpl.
apply regmreg_IE_translation. unfold intG, typed_graphs.
rewrite interf_int_regmreg_translation.
apply regmreg_in_fst_partition. destruct x. auto. auto. auto.
change_rewrite. rewrite H0. apply OptionReg.eq_refl.
change_rewrite. rewrite H2. rewrite H3. apply OptionReg.eq_refl.
destruct (Loc.eq (R x0) (R x1)). subst.
elim H5. inversion e. auto.

destruct (Loc.eq (R x0) (R (snd x))). inversion e. clear e.
generalize (proper_coloring_IRC_aux int_graph int_palette correct_palette_int).
intro H6. unfold proper_coloring in H6.
destruct H6 as [H6 HH5]. destruct HH5 as [HH5 _].
unfold proper_coloring_1 in H6.
assert (~Regs.req t0 (Regs.M (snd x))).
apply (H6 (Regs.P (fst x), Regs.M (snd x), None)).
unfold Edge.interf_edge. auto.
unfold int_graph. unfold intG, typed_graphs.
right. simpl.
apply regmreg_IE_translation. simpl.
apply regmreg_in_fst_partition. destruct x. auto.
auto.
auto.
change_rewrite. rewrite H0. rewrite H1. apply OptionReg.eq_refl.
change_rewrite. apply mreg_refl_coloring. subst.
apply (proj2 (precolored_equiv _ _)).
unfold is_precolored. simpl.
split. auto.

assert (EdgeSet.In (Regs.reg_to_Reg (fst x), Regs.mreg_to_Reg (snd x), None)
                   (IE int_graph)).
unfold int_graph.
apply regmreg_IE_translation. destruct x. simpl.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. 
replace p2 with (fst (regmreg_edge_type_partition (interf_reg_mreg g) env)).
apply regmreg_in_fst_partition. auto.
assumption.
assumption.
rewrite H7. auto.
apply (proj2 (In_graph_edge_in_ext (Regs.P (fst x), Regs.M (snd x), None)
             _ (or_intror _ H1))).
assumption.

subst. elim H8. apply Regs.eq_refl. reflexivity.

assert (OptionReg.eq (IRC int_graph int_palette (Regs.M (snd x))) (Some (Regs.M (snd x)))) as Hsnd.
apply mreg_refl_coloring. subst.
apply (proj2 (precolored_equiv _ _)).
unfold is_precolored. simpl.
split. auto.

assert (EdgeSet.In (Regs.reg_to_Reg (fst x), Regs.mreg_to_Reg (snd x), None)
                   (IE int_graph)).
unfold int_graph.
apply regmreg_IE_translation. destruct x. simpl.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. 
replace p2 with (fst (regmreg_edge_type_partition (interf_reg_mreg g) env)).
apply regmreg_in_fst_partition. auto.
assumption.
assumption.
rewrite H4. auto.
apply (proj2 (In_graph_edge_in_ext (Regs.P (fst x), Regs.M (snd x), None)
             _ (or_intror _ H1))).
assumption.

rewrite H2 in Hsnd. inversion Hsnd.

destruct (Loc.eq (S (Local (Zpos (fst x)) Tint)) (R (snd x))). 
inversion e. reflexivity.

case_eq (IRC int_graph int_palette (Regs.P (fst x))); intros. rewrite H0 in Locty2.
case_eq t0; intros; rewrite H1 in *.
destruct (Loc.eq (S (Local 0 Tint)) (R (snd x))).
inversion e. reflexivity.
destruct (Loc.eq (R m) (R (snd x))).
inversion e. subst. unfold Regs.get_type in HH0. unfold Loc.type in Locty2. congruence.
reflexivity.
destruct (Loc.eq (S (Local (Zpos (fst x)) Tint)) (R (snd x))).
inversion e. reflexivity.

case_eq (Regs.get_type (Regs.M (snd x)) env); intros HH0.

case_eq (IRC float_graph float_palette (Regs.P (fst x))); intros. rewrite H0 in Locty2.
case_eq t0; intros; rewrite H1 in *.
destruct (Loc.eq (S (Local 0 Tfloat)) (R (snd x))).
inversion e. reflexivity.
destruct (Loc.eq (R m) (R (snd x))).
inversion e. subst. rewrite HH in Locty2. unfold Regs.get_type in HH0. unfold Loc.type in Locty2. congruence.
reflexivity.
destruct (Loc.eq (S (Local (Zpos (fst x)) Tfloat)) (R (snd x))).
inversion e. reflexivity.

case_eq (IRC float_graph float_palette (Regs.P (fst x))); intros. rewrite H0 in *.
destruct (register_heuristic_mreg_float (Regs.P (fst x)) t0 H0). rewrite H1 in *. simpl.
case_eq (IRC float_graph float_palette (Regs.M (snd x))); intros.
destruct (register_heuristic_mreg_float (Regs.M (snd x)) t1 H2).

generalize (proper_coloring_IRC_aux float_graph float_palette (correct_palette_float)).
intro H4. unfold proper_coloring in H4.
destruct H4 as [H4 _].
unfold proper_coloring_1 in H4.
assert (~Regs.eq (Regs.M x0) (Regs.M x1)).
apply (H4 (Regs.P (fst x), Regs.M (snd x), None)).
unfold Edge.interf_edge. auto.
unfold float_graph.
right. simpl.
apply regmreg_IE_translation. unfold floatG, typed_graphs.
rewrite interf_float_regmreg_translation.
apply regmreg_in_snd_partition. destruct x. auto. auto. auto.
change_rewrite. rewrite H0. apply OptionReg.eq_refl.
change_rewrite. rewrite H2. rewrite H3. apply OptionReg.eq_refl.
destruct (Loc.eq (R x0) (R x1)). subst.
elim H5. inversion e. auto.

destruct (Loc.eq (R x0) (R (snd x))). inversion e. clear e.
generalize (proper_coloring_IRC_aux float_graph float_palette correct_palette_float).
intro H6. unfold proper_coloring in H6.
destruct H6 as [H6 HH5]. destruct HH5 as [HH5 _].
unfold proper_coloring_1 in H6.
assert (~Regs.req t0 (Regs.M (snd x))).
apply (H6 (Regs.P (fst x), Regs.M (snd x), None)).
unfold Edge.interf_edge. auto.
unfold int_graph. unfold floatG, typed_graphs.
right. simpl.
apply regmreg_IE_translation. simpl.
apply regmreg_in_snd_partition. destruct x. auto.
auto.
auto.
change_rewrite. rewrite H0. rewrite H1. apply OptionReg.eq_refl.
change_rewrite. apply mreg_refl_coloring. subst.
apply (proj2 (precolored_equiv _ _)).
unfold is_precolored. simpl.
split. auto.

assert (EdgeSet.In (Regs.reg_to_Reg (fst x), Regs.mreg_to_Reg (snd x), None)
                   (IE float_graph)).
unfold int_graph.
apply regmreg_IE_translation. destruct x. simpl.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. 
replace p2 with (fst (regmreg_edge_type_partition (interf_reg_mreg g) env)).
apply regmreg_in_snd_partition. auto.
assumption.
assumption.
rewrite H7. auto.
apply (proj2 (In_graph_edge_in_ext (Regs.P (fst x), Regs.M (snd x), None)
             _ (or_intror _ H1))).
assumption.

subst. elim H8. apply Regs.eq_refl. reflexivity.

assert (OptionReg.eq (IRC float_graph float_palette (Regs.M (snd x))) (Some (Regs.M (snd x)))) as Hsnd.
apply mreg_refl_coloring. subst.
apply (proj2 (precolored_equiv _ _)).
unfold is_precolored. simpl.
split. auto.

assert (EdgeSet.In (Regs.reg_to_Reg (fst x), Regs.mreg_to_Reg (snd x), None)
                   (IE float_graph)).
unfold int_graph.
apply regmreg_IE_translation. destruct x. simpl.
case_eq (regreg_edge_type_partition (interf_reg_reg g) env); intros.
case_eq (regreg_edge_type_partition (pref_reg_reg g) env); intros.
case_eq (regmreg_edge_type_partition (interf_reg_mreg g) env); intros.
case_eq (regmreg_edge_type_partition (pref_reg_mreg g) env); intros.
simpl. 
replace p2 with (fst (regmreg_edge_type_partition (interf_reg_mreg g) env)).
apply regmreg_in_snd_partition. auto.
assumption.
assumption.
rewrite H4. auto.
apply (proj2 (In_graph_edge_in_ext (Regs.P (fst x), Regs.M (snd x), None)
             _ (or_intror _ H1))).
assumption.

rewrite H2 in Hsnd. inversion Hsnd.

destruct (Loc.eq (S (Local (Zpos (fst x)) Tfloat)) (R (snd x))). 
inversion e. reflexivity.
Qed.

Import Registers.

Lemma in_palette_not_in_temporaries : forall x,
VertexSet.In (Regs.M x) int_palette ->
~In (R x) temporaries.

Proof.
unfold int_palette. intros x H.
repeat (destruct (proj1 (Props.Dec.F.add_iff _ _ _) H);[
        inversion H0; subst; intro H1; inversion H1;
        [inversion H2| repeat (destruct H2 as [H2|H2];[inversion H2|])];
        assumption
        | generalize H0; clear H H0; intro H]).
elim (VertexSet.empty_1 H).
Qed.

Lemma in_palette_not_in_temporaries_float : forall x,
VertexSet.In (Regs.M x) float_palette ->
~In (R x) temporaries.

Proof.
unfold int_palette. intros x H.
repeat (destruct (proj1 (Props.Dec.F.add_iff _ _ _) H);[
        inversion H0; subst; intro H1; inversion H1;
        [inversion H2| repeat (destruct H2 as [H2|H2];[inversion H2|])];
        assumption
        | generalize H0; clear H H0; intro H]).
elim (VertexSet.empty_1 H).
Qed.

Lemma coloring_acceptable_loc : forall x,
loc_is_acceptable (my_graph_coloring g env x) = true.

Proof.
intro x.
unfold loc_is_acceptable.
unfold my_graph_coloring in *.
change (snd (fst (fst (Typed_interfgraphs g env)))) with intG.
change (fst (fst (fst (Typed_interfgraphs g env)))) with intR.
change (snd (fst (Typed_interfgraphs g env))) with floatR.
change (snd (Typed_interfgraphs g env)) with floatG.
fold int_graph; fold float_graph.
unfold graph_coloring_aux.
change (map_to_coloring (IRC_map (graph_to_IRC_graph int_graph int_palette))) with
             (IRC int_graph int_palette).
change (map_to_coloring (IRC_map (graph_to_IRC_graph float_graph float_palette))) with
             (IRC float_graph float_palette).
case_eq (Regs.get_type (Regs.P x) env); intros.
case_eq (IRC int_graph int_palette (Regs.P x)); intros.
unfold IRC in *.
generalize (register_heuristic_mreg _ _ H0). intro H2.
destruct H2. rewrite H1.
destruct (List.In_dec Loc.eq (R x0) temporaries).
assert (~In (R x0) temporaries).
apply in_palette_not_in_temporaries.
rewrite <-H1.
generalize (proper_coloring_IRC_aux int_graph int_palette
            (correct_palette_int)). intro H3.
unfold proper_coloring in H3.
do 2 destruct H3 as [_ H3].
unfold proper_coloring_3 in H3.
apply (H3 (Regs.P x) t0).
unfold IRC in *. rewrite H0. apply OptionReg.eq_refl.
elim (H2 i).
reflexivity.
unfold IRC in *. auto.
case_eq (IRC float_graph float_palette (Regs.P x)); intros.
unfold IRC in *.
generalize (register_heuristic_mreg_float _ _ H0). intro H2.
destruct H2. rewrite H1.
destruct (List.In_dec Loc.eq (R x0) temporaries).
assert (~In (R x0) temporaries).
apply in_palette_not_in_temporaries_float.
rewrite <-H1.
generalize (proper_coloring_IRC_aux float_graph float_palette
            correct_palette_float). intro H3.
unfold proper_coloring in H3.
do 2 destruct H3 as [_ H3].
unfold proper_coloring_3 in H3.
apply (H3 (Regs.P x) t0).
unfold IRC in *. rewrite H0 in *. apply OptionReg.eq_refl.
elim (H2 i).
reflexivity.
unfold IRC in *. auto.
Qed.

Lemma correct_alloc_3 : check_coloring_3 (all_interf_regs g) env (my_graph_coloring g env) = true.

Proof.
unfold check_coloring_3.
apply Regset.for_all_1.

unfold compat_bool.
intros. subst. reflexivity.

unfold Regset.For_all.
intros x H.
rewrite coloring_acceptable_loc. simpl.
unfold same_typ.
rewrite <-Loc_reg_eq_type.
simpl. destruct (env x); reflexivity.

Qed.

Theorem correct_alloc : check_coloring g env (all_interf_regs g) (my_graph_coloring g env) = true.

Proof.
unfold check_coloring.
rewrite correct_alloc_1.
rewrite correct_alloc_2.
rewrite correct_alloc_3.
auto.
Qed.

End Coloring_to_allocation.

Lemma precolored_sub_int_palette : forall x g env,
VertexSet.In x (precolored (int_graph g env)) -> VertexSet.In x int_palette.

Proof.
Admitted.

Lemma precolored_sub_float_palette : forall x g env,
VertexSet.In x (precolored (float_graph g env)) -> VertexSet.In x float_palette.

Proof.
Admitted.

Theorem allocation_correct : forall g env,
check_coloring g env (all_interf_regs g) (my_graph_coloring g env) = true.

Proof.
intros. apply correct_alloc.
intros. apply (precolored_sub_int_palette x g env). assumption.
intros. apply (precolored_sub_float_palette x g env). assumption.
Qed.