summaryrefslogtreecommitdiff
path: root/backend/IRC_Graph_Functions.v
blob: fc691aa245b9913aa3497d54650efd744d40f0f8 (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
Require Import FSets.
Require Import InterfGraphMapImp.
Require Import Spill_WL.
Require Import ZArith.
Require Import Simplify_WL.
Require Import Spill_WL.
Require Import Merge_WL.
Require Import Freeze_WL.
Require Import IRC_graph.
Require Import Edges.
Require Import Conservative_criteria.
Require Import WS.

Import RegFacts Props OTFacts.

Definition any_vertex := VertexSet.choose.

(* simplify *)

Definition simplify_irc r ircg H :=
Make_IRC_Graph (remove_vertex r (irc_g ircg))
               (simplify_wl r ircg (irc_k ircg)) 
               (pal ircg)
               (irc_k ircg)
               (WS_simplify r ircg H)
               (Hk ircg).

Definition simplify g : option (Register.t * irc_graph) :=
let simplifyWL := get_simplifyWL (irc_wl g) in
match any_vertex simplifyWL as v return (any_vertex simplifyWL = v -> option (Register.t * irc_graph)) with
|Some r => fun H : any_vertex simplifyWL = Some r =>
           Some (r, simplify_irc r g (VertexSet.choose_1 H))
|None => fun H : any_vertex simplifyWL = None => None
end (refl_equal (any_vertex simplifyWL)).

Lemma simplify_inv_aux :
  forall g P,
    match simplify g with
    | Some x =>
         forall ( H : (any_vertex (get_simplifyWL (irc_wl g)) = Some (fst x))),
         (simplify_irc (fst x) g (VertexSet.choose_1 H) = snd x) -> P
    | None =>
         (any_vertex (get_simplifyWL (irc_wl g)) = None -> P)
    end -> P.
Proof.
  intros g P.
  unfold simplify.

  set (simplifyWL := get_simplifyWL (irc_wl g)) in *.
  set (Z := any_vertex simplifyWL) in *.

  refine
    (match Z as W
     return forall (H : Z = W),

match
  match W as v return (Z = v -> option (Register.t * irc_graph)) with
  | Some r =>
      fun H : Z = Some r => Some (r, simplify_irc r g (VertexSet.choose_1 H))
  | None => fun _ : Z = None => None
  end H
with
| Some x => forall H : Z = Some (fst x), simplify_irc (fst x) g (VertexSet.choose_1 H) = snd x -> P
| None => Z = None -> P
end -> P
  
     with
       | Some x => _
       | None => _
     end _).

simpl. intros. apply X with (H0 := H). reflexivity.
auto.
Qed.  

Lemma simplify_inv : forall g res,
simplify g = Some res ->
any_vertex (get_simplifyWL (irc_wl g)) = Some (fst res).
Proof.
  intros.
  apply simplify_inv_aux with g.
  rewrite H.
  auto.
Qed.

Lemma simplify_inv2 : forall g res,
simplify g = Some res ->
exists H, snd res = simplify_irc (fst res) g (VertexSet.choose_1 H).

Proof.
intros.
apply (simplify_inv_aux g). rewrite H.
simpl. intros. rewrite <-H1.
exists H0. reflexivity.
Qed.

(* merge *)

Definition merge_irc e ircg pin paff :=
let g' := merge e (irc_g ircg) pin paff in
Make_IRC_Graph g' 
              (merge_wl3 e ircg g' pin paff) 
              (pal ircg) 
              (irc_k ircg)
              (WS_coalesce _ _ pin paff)
              (Hk ircg).

Definition coalesce g : option (Edge.t * irc_graph) :=
let movesWL := get_movesWL (irc_wl g) in
let graph := irc_g g in
let HWS := HWS_irc g in
let k := irc_k g in
match any_coalescible_edge movesWL graph k as e 
return (any_coalescible_edge movesWL graph k = e -> option (Edge.t * irc_graph)) with
|Some edge => fun H : any_coalescible_edge movesWL graph k = Some edge =>
              let Hin := any_coalescible_edge_11 _ _ _ _ H in
              let Hing := proj2 (In_move_props _ _ _ _ _ _ _ _ Hin (refl_equal _) HWS) in
              let Haff := proj1 (In_move_props _ _ _ _ _ _ _ _ Hin (refl_equal _) HWS) in
              Some (edge,merge_irc edge g Hing Haff)
|None => fun H : any_coalescible_edge movesWL graph k = None => None
end (refl_equal (any_coalescible_edge movesWL graph k)).

Lemma coalesce_inv_aux :
  forall g P,
    match coalesce g with
    | Some x =>
         forall (H : (any_coalescible_edge (get_movesWL (irc_wl g)) (irc_g g) (irc_k g) = Some (fst x))),
         (merge_irc (fst x) g 
         (proj2 (In_move_props _ _ _ _ _ _ _ _ (any_coalescible_edge_11 _ _ _ _ H) (refl_equal _) (HWS_irc g)))
         (proj1 (In_move_props _ _ _ _ _ _ _ _ (any_coalescible_edge_11 _ _ _ _ H) (refl_equal _) (HWS_irc g))))
         = snd x -> P
    | None =>
         (any_coalescible_edge (get_movesWL (irc_wl g)) (irc_g g) (irc_k g) = None -> P)
    end -> P.
Proof.
  intros g P.
  unfold coalesce.

  set (movesWL := get_movesWL (irc_wl g)) in *.
  set (Z := any_coalescible_edge movesWL (irc_g g) (irc_k g)) in *.

  refine
    (match Z as W
     return forall (H : Z = W),

match
  match W as e return (Z = e -> option (Edge.t * irc_graph)) with
  | Some edge =>
      fun H : Z = Some edge => Some (edge,
merge_irc edge g
          (proj2
             (In_move_props edge (irc_g g)
                (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
                get_simplifyWL (irc_wl g), movesWL) movesWL
                (get_spillWL (irc_wl g)) (get_freezeWL (irc_wl g))
                (get_simplifyWL (irc_wl g)) (irc_k g)
                (any_coalescible_edge_11 edge (irc_g g) (irc_k g) movesWL H)
                (refl_equal
                   (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
                   get_simplifyWL (irc_wl g), movesWL)) (HWS_irc g)))
          (proj1
             (In_move_props edge (irc_g g)
                (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
                get_simplifyWL (irc_wl g), movesWL) movesWL
                (get_spillWL (irc_wl g)) (get_freezeWL (irc_wl g))
                (get_simplifyWL (irc_wl g)) (irc_k g)
                (any_coalescible_edge_11 edge (irc_g g) (irc_k g) movesWL H)
                (refl_equal
                   (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
                   get_simplifyWL (irc_wl g), movesWL)) (HWS_irc g))))
  | None => fun _ : Z = None => None
  end H
with
| Some x =>
    forall H : Z = Some (fst x),
    merge_irc (fst x) g
      (proj2
         (In_move_props (fst x) (irc_g g)
            (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
            get_simplifyWL (irc_wl g), movesWL) movesWL
            (get_spillWL (irc_wl g)) (get_freezeWL (irc_wl g))
            (get_simplifyWL (irc_wl g)) (irc_k g)
            (any_coalescible_edge_11 (fst x) (irc_g g) (irc_k g) movesWL H)
            (refl_equal
               (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
               get_simplifyWL (irc_wl g), movesWL)) (HWS_irc g)))
      (proj1
         (In_move_props (fst x) (irc_g g)
            (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
            get_simplifyWL (irc_wl g), movesWL) movesWL
            (get_spillWL (irc_wl g)) (get_freezeWL (irc_wl g))
            (get_simplifyWL (irc_wl g)) (irc_k g)
            (any_coalescible_edge_11 (fst x) (irc_g g) (irc_k g) movesWL H)
            (refl_equal
               (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
               get_simplifyWL (irc_wl g), movesWL)) (HWS_irc g))) = snd x ->
    P
| None => Z = None -> P
end -> P  
     with
       | Some x => _
       | None => _
     end _).

simpl. intros. apply X with (H0 := H). reflexivity.
auto.
Qed.  

Lemma coalesce_inv : forall g res,
coalesce g = Some res ->
any_coalescible_edge (get_movesWL (irc_wl g)) (irc_g g) (irc_k g) = Some (fst res).
Proof.
  intros.
  apply (coalesce_inv_aux g).
  rewrite H.
  auto.
Qed.

Lemma coalesce_inv_2 : forall g res,
coalesce g = Some res ->
exists H, exists H', snd res = merge_irc (fst res) g H H'.

Proof.
intros.
apply (coalesce_inv_aux g).
rewrite H.
simpl. intros.
exists ((proj2
          (In_move_props (fst res) (irc_g g)
             (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
             get_simplifyWL (irc_wl g), get_movesWL (irc_wl g))
             (get_movesWL (irc_wl g)) (get_spillWL (irc_wl g))
             (get_freezeWL (irc_wl g)) (get_simplifyWL (irc_wl g))
             (irc_k g)
             (any_coalescible_edge_11 (fst res) (irc_g g) (irc_k g)
                (get_movesWL (irc_wl g)) H0)
             (refl_equal
                (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
                get_simplifyWL (irc_wl g), get_movesWL (irc_wl g)))
             (HWS_irc g)))).
exists ((proj1
          (In_move_props (fst res) (irc_g g)
             (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
             get_simplifyWL (irc_wl g), get_movesWL (irc_wl g))
             (get_movesWL (irc_wl g)) (get_spillWL (irc_wl g))
             (get_freezeWL (irc_wl g)) (get_simplifyWL (irc_wl g))
             (irc_k g)
             (any_coalescible_edge_11 (fst res) (irc_g g) (irc_k g)
                (get_movesWL (irc_wl g)) H0)
             (refl_equal
                (get_spillWL (irc_wl g), get_freezeWL (irc_wl g),
                get_simplifyWL (irc_wl g), get_movesWL (irc_wl g)))
             (HWS_irc g)))).
auto.
Qed.

(* freeze *)

Definition delete_preference_edges_irc2 v ircg Hing Hdep :=
let k := irc_k ircg in
let g' := delete_preference_edges v (irc_g ircg) Hing in
Make_IRC_Graph g' 
               (delete_preferences_wl2 v ircg k) 
               (pal ircg)
               (irc_k ircg)
               (WS_freeze v ircg Hing Hdep)
               (Hk ircg).

Definition freeze g : option (Register.t * irc_graph) :=
let freezeWL := get_freezeWL (irc_wl g) in
let graph := irc_g g in
let HWS := HWS_irc g in
match any_vertex freezeWL as r
return (any_vertex freezeWL = r -> option (Register.t*irc_graph)) with
|Some x => fun H : any_vertex freezeWL = Some x =>
           let Hin := VertexSet.choose_1 H in
           let Hing := proj1 (proj2 (proj2 (In_freeze_props _ _ _ _ _ _ _ _ Hin (refl_equal _) HWS)))in
           Some (x,delete_preference_edges_irc2 x g Hing Hin)
|None => fun H : any_vertex freezeWL = None => None
end (refl_equal (any_vertex freezeWL)).

Lemma freeze_inv_aux :
  forall g P,
    match freeze g with
    | Some x =>
         forall ( H : (any_vertex (get_freezeWL (irc_wl g)) = Some (fst x))),
         (delete_preference_edges_irc2 (fst x) g 
         (proj1 (proj2 (proj2 (In_freeze_props _ _ _ _ _ _ _ _ (VertexSet.choose_1 H) (refl_equal _) (HWS_irc g)))))
         (VertexSet.choose_1 H) = snd x) -> P
    | None =>
         (any_vertex (get_freezeWL (irc_wl g)) = None -> P)
    end -> P.

Proof.
  intros g P.
  unfold freeze.

  set (freezeWL := get_freezeWL (irc_wl g)) in *.
  set (Z := any_vertex freezeWL) in *.

  refine
    (match Z as W
     return forall (H : Z = W),

match
  match W as v return (Z = v -> option (Register.t * irc_graph)) with
  | Some x =>
      fun H : Z = Some x => Some
        (x,
        delete_preference_edges_irc2 x g
          (proj1
             (proj2
                (proj2
                   (In_freeze_props x (irc_g g)
                      (get_spillWL (irc_wl g), freezeWL,
                      get_simplifyWL (irc_wl g), get_movesWL (irc_wl g))
                      freezeWL (get_spillWL (irc_wl g))
                      (get_simplifyWL (irc_wl g)) (get_movesWL (irc_wl g))
                      (irc_k g) (VertexSet.choose_1 H)
                      (refl_equal
                         (get_spillWL (irc_wl g), freezeWL,
                         get_simplifyWL (irc_wl g), get_movesWL (irc_wl g)))
                      (HWS_irc g))))) (VertexSet.choose_1 H))
  | None => fun _ : Z = None => None
  end H
with
| Some x => 

forall H : Z = Some (fst x),
    delete_preference_edges_irc2 (fst x) g
      (proj1
         (proj2
            (proj2
               (In_freeze_props (fst x) (irc_g g)
                  (get_spillWL (irc_wl g), freezeWL,
                  get_simplifyWL (irc_wl g), get_movesWL (irc_wl g)) freezeWL
                  (get_spillWL (irc_wl g)) (get_simplifyWL (irc_wl g))
                  (get_movesWL (irc_wl g)) (irc_k g)
                  (VertexSet.choose_1 H)
                  (refl_equal
                     (get_spillWL (irc_wl g), freezeWL,
                     get_simplifyWL (irc_wl g), get_movesWL (irc_wl g)))
                  (HWS_irc g))))) (VertexSet.choose_1 H) = snd x -> P
| None => Z = None -> P
end -> P
     with
       | Some x => _
       | None => _
     end _).

simpl. intros. apply X with (H0 := H). reflexivity.
auto.
Qed.

Lemma freeze_inv : forall g res,
freeze g = Some res ->
any_vertex (get_freezeWL (irc_wl g)) = Some (fst res).
Proof.
  intros.
  apply freeze_inv_aux with g.
  rewrite H.
  auto.
Qed.

Lemma freeze_inv2 : forall g res,
freeze g = Some res ->
exists H', exists H, snd res = delete_preference_edges_irc2 (fst res) g H H'.

Proof.
intros.
apply (freeze_inv_aux g). rewrite H.
simpl. intros. rewrite <-H1.
exists (VertexSet.choose_1 H0).
exists (proj1 (proj2 (proj2 (In_freeze_props _ _ _ _ _ _ _ _ (VertexSet.choose_1 H0) (refl_equal _) (HWS_irc g))))). reflexivity.
Qed.

(* spill *)
Definition spill_irc r ircg H :=
Make_IRC_Graph (remove_vertex r (irc_g ircg))
               (spill_wl r ircg (irc_k ircg)) 
               (pal ircg)
               (irc_k ircg)
               (WS_spill r ircg H)
               (Hk ircg).

Definition cost_order (opt : (Register.t*nat*nat)) y g :=
let (tmp, pref_card) := opt in
let (x, int_card) := tmp in
let y_int := VertexSet.cardinal (interference_adj y g) in
match lt_eq_lt_dec y_int int_card with
|inleft (left _) => opt
|inleft (right _) => let y_pref := VertexSet.cardinal (preference_adj y g) in
                              match le_lt_dec pref_card y_pref with
                     |left _ => opt
                     |right _ => (y, y_int, y_pref)
                     end
|inright _ => (y, y_int, VertexSet.cardinal (preference_adj y g))
end.

Definition lowest_cost_aux s g o :=
VertexSet.fold (fun v o => match o with
                         | Some opt => Some (cost_order opt v g)
                         | None => Some (v, VertexSet.cardinal (interference_adj v g),
                                                         VertexSet.cardinal (preference_adj v g))
                         end)
             s
             o.

Definition lowest_cost s g := 
match lowest_cost_aux s g None with
| Some r => Some (fst (fst r))
| None => None
end.

Lemma lowest_cost_aux_in : forall x s g o,
lowest_cost_aux s g o = Some x->
VertexSet.In (fst (fst x)) s \/ o = Some x.

Proof.
intros. unfold lowest_cost_aux in H.
set (f :=  (fun (v : VertexSet.elt) (o : option (MyRegisters.Regs.t * nat * nat)) =>
       match o with
       | Some opt => Some (cost_order opt v g)
       | None =>
           Some
             (v, VertexSet.cardinal (interference_adj v g),
             VertexSet.cardinal (preference_adj v g))
       end )) in *.
unfold VertexSet.elt in *.
fold f in H.
rewrite VertexSet.fold_1 in H.
set (f' := fun a e => f e a) in *.
unfold VertexSet.elt in *. fold f' in H.
generalize VertexSet.elements_2. intro HH.
generalize (HH s). clear HH. intro HH.
generalize x o H. clear H. 
induction (VertexSet.elements s). intros x0 o0 H.
simpl in H. right. auto.
simpl. intros.
assert (VertexSet.In (fst (fst x0)) s \/ (f' o0 a) = Some x0).
apply IHl.
intros. apply HH. right. auto.
auto.
destruct H0.
left. auto.
unfold f' in H0. unfold f in H0.
case_eq o0; intros.
rewrite H1 in *.
case_eq (cost_order p a g); intros.
rewrite H2 in H0. unfold cost_order in H2.
destruct p. simpl in *. destruct p. simpl in *.
destruct (lt_eq_lt_dec (VertexSet.cardinal (interference_adj a g)) n1). destruct s0.
right. rewrite H2. auto.
destruct (le_lt_dec n0 (VertexSet.cardinal (preference_adj a g))).
right. rewrite H2. auto.
left. apply HH. left. destruct p0. simpl in *.
destruct x0. destruct p. simpl in *.
inversion H0. inversion H2. subst. intuition.
left. apply HH. left. destruct p0. simpl in *.
destruct x0. destruct p. simpl in *.
inversion H0. inversion H2. subst. intuition.
rewrite H1 in H0.
unfold f' in H. rewrite H1 in H. simpl in H. rewrite H0 in H.
fold f' in H.
left. apply HH. inversion H0. simpl. left. intuition.
Qed.

Lemma lowest_cost_in : forall x s g,
lowest_cost s g = Some x ->
VertexSet.In x s.

Proof.
intros. unfold lowest_cost in H. 
case_eq (lowest_cost_aux s g None); intros; rewrite H0 in H.
generalize (lowest_cost_aux_in p s g None H0). intro.
destruct H1. inversion H. subst. auto.
congruence.
congruence.
Qed.

Definition spill g : option (Register.t * irc_graph) :=
let spillWL := get_spillWL (irc_wl g) in
match lowest_cost spillWL (irc_g g) as v return (lowest_cost spillWL (irc_g g) = v -> option (Register.t * irc_graph)) with
|Some r => fun H : lowest_cost spillWL (irc_g g) = Some r =>
           Some (r, spill_irc r g (lowest_cost_in _ _ _ H))
|None => fun H : lowest_cost spillWL (irc_g g) = None => None
end (refl_equal (lowest_cost spillWL (irc_g g))).

Lemma spill_inv_aux :
  forall g P,
    match spill g with
    | Some x =>
         forall ( H : (lowest_cost (get_spillWL (irc_wl g)) (irc_g g) = Some (fst x))),
         (spill_irc (fst x) g (lowest_cost_in _ _ _ H) = snd x) -> P
    | None =>
         (lowest_cost (get_spillWL (irc_wl g)) (irc_g g) = None -> P)
    end -> P.
Proof.
  intros g P.
  unfold spill.

  set (spillWL := get_spillWL (irc_wl g)) in *.
  set (Z := lowest_cost spillWL (irc_g g)) in *.

  refine
    (match Z as W
     return forall (H : Z = W),

match
  match W as v return (Z = v -> option (Register.t * irc_graph)) with
  | Some r =>
      fun H : Z = Some r => Some (r, spill_irc r g (lowest_cost_in _ _ _ H))
  | None => fun _ : Z = None => None
  end H
with
| Some x => forall H : Z = Some (fst x), spill_irc (fst x) g (lowest_cost_in _ _ _  H) = snd x -> P
| None => Z = None -> P
end -> P
  
     with
       | Some x => _
       | None => _
     end _).

simpl. intros. apply X with (H0 := H). reflexivity.
auto.
Qed.  

Lemma spill_inv : forall g res,
spill g = Some res ->
lowest_cost (get_spillWL (irc_wl g)) (irc_g g) = Some (fst res).
Proof.
  intros.
  apply spill_inv_aux with g.
  rewrite H.
  auto.
Qed.

Lemma spill_inv2 : forall g res,
spill g = Some res ->
exists H, snd res = spill_irc (fst res) g (lowest_cost_in (fst res) (get_spillWL (irc_wl g)) (irc_g g) H).

Proof.
intros.
apply (spill_inv_aux g). rewrite H.
simpl. intros. rewrite <-H1.
exists H0. reflexivity.
Qed.