aboutsummaryrefslogtreecommitdiffstats
path: root/aarch64/PeepholeOracle.ml
blob: a6945b9f4ed189600ba2ec9b519a7c31a6c5c6aa (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
(* *************************************************************)
(*                                                             *)
(*             The Compcert verified compiler                  *)
(*                                                             *)
(*           Léo Gourdin        UGA, VERIMAG                   *)
(*                                                             *)
(*  Copyright VERIMAG. All rights reserved.                    *)
(*  This file is distributed under the terms of the INRIA      *)
(*  Non-Commercial License Agreement.                          *)
(*                                                             *)
(* *************************************************************)

open Camlcoq
open Asmblock
open Asm
open Int64
open Printf

(* If true, the oracle will print a msg for each applied peephole *)
let debug = false

(* Functions to verify the immediate offset range for ldp/stp *)
let is_valid_immofs_32 z =
  if z <= 252 && z >= -256 && z mod 4 = 0 then true else false

let is_valid_immofs_64 z =
  if z <= 504 && z >= -512 && z mod 8 = 0 then true else false

(* Functions to check if a ldp/stp replacement is valid according to args *)
let is_valid_ldrw rd1 rd2 b1 b2 n1 n2 =
  let z1 = to_int (camlint64_of_coqint n1) in
  let z2 = to_int (camlint64_of_coqint n2) in
  if
    (not (ireg_eq rd1 rd2))
    && iregsp_eq b1 b2
    && (not (iregsp_eq (RR1 rd1) b2))
    && (z2 = z1 + 4 || z2 = z1 - 4)
    && is_valid_immofs_32 z1
  then true
  else false

let is_valid_ldrx rd1 rd2 b1 b2 n1 n2 =
  let z1 = to_int (camlint64_of_coqint n1) in
  let z2 = to_int (camlint64_of_coqint n2) in
  if
    (not (ireg_eq rd1 rd2))
    && iregsp_eq b1 b2
    && (not (iregsp_eq (RR1 rd1) b2))
    && (z2 = z1 + 8 || z2 = z1 - 8)
    && is_valid_immofs_64 z1
  then true
  else false

let is_valid_strw b1 b2 n1 n2 =
  let z1 = to_int (camlint64_of_coqint n1) in
  let z2 = to_int (camlint64_of_coqint n2) in
  if iregsp_eq b1 b2 && z2 = z1 + 4 && is_valid_immofs_32 z1 then true
  else false

let is_valid_strx b1 b2 n1 n2 =
  let z1 = to_int (camlint64_of_coqint n1) in
  let z2 = to_int (camlint64_of_coqint n2) in
  if iregsp_eq b1 b2 && z2 = z1 + 8 && is_valid_immofs_64 z1 then true
  else false

let dreg_of_ireg r = IR (RR1 r)

(* Return true if an intermediate
 * affectation eliminates the potential
 * candidate *)
let verify_load_affect reg rd b rev =
  let b = IR b in
  let rd = dreg_of_ireg rd in
  if not rev then dreg_eq reg b else dreg_eq reg b || dreg_eq reg rd

(* Return true if an intermediate
 * read eliminates the potential
 * candidate *)
let verify_load_read reg rd b rev =
  let rd = dreg_of_ireg rd in
  dreg_eq reg rd

(* Return true if an intermediate
 * affectation eliminates the potential
 * candidate *)
let verify_store_affect reg rs b rev =
  let b = IR b in
  let rs = dreg_of_ireg rs in
  dreg_eq reg b || dreg_eq reg rs

(* Affect a symbolic memory list of potential replacements
 * for a given write in reg *)
let rec affect_symb_mem reg insta pot_rep stype rev =
  match pot_rep with
  | [] -> []
  | h0 :: t0 -> (
      match (insta.(h0), stype) with
      | PLoad (PLd_rd_a (_, IR (RR1 rd), ADimm (b, n))), "ldr" ->
          if verify_load_affect reg rd b rev then
            affect_symb_mem reg insta t0 stype rev
          else h0 :: affect_symb_mem reg insta t0 stype rev
      | PStore (PSt_rs_a (_, IR (RR1 rs), ADimm (b, n))), "str" ->
          if verify_store_affect reg rs b rev then
            affect_symb_mem reg insta t0 stype rev
          else h0 :: affect_symb_mem reg insta t0 stype rev
      | _, _ ->
          failwith "affect_symb_mem: Found an inconsistent inst in pot_rep")

(* Affect a symbolic memory list of potential replacements
 * for a given read in reg *)
let rec read_symb_mem reg insta pot_rep stype rev =
  match pot_rep with
  | [] -> []
  | h0 :: t0 -> (
      match (insta.(h0), stype) with
      | PLoad (PLd_rd_a (_, IR (RR1 rd), ADimm (b, n))), "ldr" ->
          if verify_load_read reg rd b rev then
            read_symb_mem reg insta t0 stype rev
          else h0 :: read_symb_mem reg insta t0 stype rev
      | PStore (PSt_rs_a (_, IR (RR1 rs), ADimm (b, n))), "str" ->
          h0 :: read_symb_mem reg insta t0 stype rev
      | _, _ -> failwith "read_symb_mem: Found an inconsistent inst in pot_rep")

(* Update a symbolic memory list of potential replacements
 * for any addressing *)
let update_pot_rep_addressing a insta pot_rep stype rev =
  match a with
  | ADimm (base, _) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev
  | ADreg (base, r) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
  | ADlsl (base, r, _) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
  | ADsxt (base, r, _) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
  | ADuxt (base, r, _) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg r) insta !pot_rep stype rev
  | ADadr (base, _, _) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev
  | ADpostincr (base, _) ->
      pot_rep := read_symb_mem (IR base) insta !pot_rep stype rev

(* Update a symbolic memory list of potential replacements
 * for any basic instruction *)
let update_pot_rep_basic inst insta pot_rep stype rev =
  match inst with
  | PArith i -> (
      match i with
      | PArithP (_, rd) ->
          pot_rep := affect_symb_mem rd insta !pot_rep stype rev
      | PArithPP (_, rd, rs) ->
          pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
          pot_rep := read_symb_mem rs insta !pot_rep stype rev
      | PArithPPP (_, rd, rs1, rs2) ->
          pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
          pot_rep := read_symb_mem rs1 insta !pot_rep stype rev;
          pot_rep := read_symb_mem rs2 insta !pot_rep stype rev
      | PArithRR0R (_, rd, rs1, rs2) ->
          pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
          (match rs1 with
          | RR0 rs1 ->
              pot_rep :=
                read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
          | _ -> ());
          pot_rep := read_symb_mem (dreg_of_ireg rs2) insta !pot_rep stype rev
      | PArithRR0 (_, rd, rs) -> (
          pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
          match rs with
          | RR0 rs1 ->
              pot_rep :=
                read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
          | _ -> ())
      | PArithARRRR0 (_, rd, rs1, rs2, rs3) -> (
          pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
          pot_rep := read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev;
          pot_rep := read_symb_mem (dreg_of_ireg rs2) insta !pot_rep stype rev;
          match rs3 with
          | RR0 rs1 ->
              pot_rep :=
                read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
          | _ -> ())
      | PArithComparisonPP (_, rs1, rs2) ->
          pot_rep := read_symb_mem rs1 insta !pot_rep stype rev;
          pot_rep := read_symb_mem rs2 insta !pot_rep stype rev
      | PArithComparisonR0R (_, rs1, rs2) ->
          (match rs1 with
          | RR0 rs1 ->
              pot_rep :=
                read_symb_mem (dreg_of_ireg rs1) insta !pot_rep stype rev
          | _ -> ());
          pot_rep := read_symb_mem (dreg_of_ireg rs2) insta !pot_rep stype rev
      | PArithComparisonP (_, rs1) ->
          pot_rep := read_symb_mem rs1 insta !pot_rep stype rev
      | Pcset (rd, _) ->
          pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev
      | Pfmovi (_, _, rs) -> (
          match rs with
          | RR0 rs ->
              pot_rep :=
                read_symb_mem (dreg_of_ireg rs) insta !pot_rep stype rev
          | _ -> ())
      | Pcsel (rd, rs1, rs2, _) ->
          pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
          pot_rep := read_symb_mem rs1 insta !pot_rep stype rev;
          pot_rep := read_symb_mem rs2 insta !pot_rep stype rev
      | Pfnmul (_, rd, rs1, rs2) -> ())
  | PLoad i -> (
      (* Here, we consider a different behavior for load and store potential candidates:
         * a load does not obviously cancel the ldp candidates, but it does for any stp candidate. *)
      match stype with
      | "ldr" -> (
          match i with
          | PLd_rd_a (_, rd, a) ->
              pot_rep := affect_symb_mem rd insta !pot_rep stype rev;
              update_pot_rep_addressing a insta pot_rep stype rev
          | Pldp (_, rd1, rd2, _, _, a) ->
              pot_rep :=
                affect_symb_mem (dreg_of_ireg rd1) insta !pot_rep stype rev;
              pot_rep :=
                affect_symb_mem (dreg_of_ireg rd2) insta !pot_rep stype rev;
              update_pot_rep_addressing a insta pot_rep stype rev)
      | _ -> pot_rep := [])
  | PStore _ -> (
      (* Here, we consider that a store cancel all ldp candidates, but it is far more complicated for stp ones :
         * if we cancel stp candidates here, we would prevent ourselves to apply the non-consec store peephole.
         * To solve this issue, the store candidates cleaning is managed directly in the peephole function below. *)
      match stype with "ldr" -> pot_rep := [] | _ -> ())
  | Pallocframe (_, _) -> pot_rep := []
  | Pfreeframe (_, _) -> pot_rep := []
  | Ploadsymbol (rd, _) ->
      pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev
  | Pcvtsw2x (rd, rs) ->
      pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg rs) insta !pot_rep stype rev
  | Pcvtuw2x (rd, rs) ->
      pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg rs) insta !pot_rep stype rev
  | Pcvtx2w rd ->
      pot_rep := affect_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev;
      pot_rep := read_symb_mem (dreg_of_ireg rd) insta !pot_rep stype rev
  | Pnop -> ()

(* Try to find the index of the first previous compatible
 * replacement in a given symbolic memory *)
let rec search_compat_rep r2 b2 n2 insta pot_rep stype =
  match pot_rep with
  | [] -> None
  | h0 :: t0 -> (
      match (insta.(h0), stype) with
      | PLoad (PLd_rd_a (ld1, IR (RR1 rd1), ADimm (b1, n1))), "ldrw" ->
          if is_valid_ldrw rd1 r2 b1 b2 n1 n2 then
            Some (h0, chunk_load ld1, rd1, b1, n1)
          else search_compat_rep r2 b2 n2 insta t0 stype
      | PLoad (PLd_rd_a (ld1, IR (RR1 rd1), ADimm (b1, n1))), "ldrx" ->
          if is_valid_ldrx rd1 r2 b1 b2 n1 n2 then
            Some (h0, chunk_load ld1, rd1, b1, n1)
          else search_compat_rep r2 b2 n2 insta t0 stype
      | PStore (PSt_rs_a (st1, IR (RR1 rs1), ADimm (b1, n1))), "strw" ->
          if is_valid_strw b1 b2 n1 n2 then
            Some (h0, chunk_store st1, rs1, b1, n1)
          else search_compat_rep r2 b2 n2 insta t0 stype
      | PStore (PSt_rs_a (st1, IR (RR1 rs1), ADimm (b1, n1))), "strx" ->
          if is_valid_strx b1 b2 n1 n2 then
            Some (h0, chunk_store st1, rs1, b1, n1)
          else search_compat_rep r2 b2 n2 insta t0 stype
      | _, _ ->
          failwith "search_compat_rep: Found an inconsistent inst in pot_rep")

(* Try to find the index of the first previous compatible
 * replacement in a given symbolic memory (when iterating in the reversed list) *)
let rec search_compat_rep_inv r2 b2 n2 insta pot_rep stype =
  match pot_rep with
  | [] -> None
  | h0 :: t0 -> (
      match (insta.(h0), stype) with
      | PLoad (PLd_rd_a (ld1, IR (RR1 rd1), ADimm (b1, n1))), "ldrw" ->
          if is_valid_ldrw r2 rd1 b2 b1 n2 n1 then
            Some (h0, chunk_load ld1, rd1, b1, n1)
          else search_compat_rep_inv r2 b2 n2 insta t0 stype
      | PLoad (PLd_rd_a (ld1, IR (RR1 rd1), ADimm (b1, n1))), "ldrx" ->
          if is_valid_ldrx r2 rd1 b2 b1 n2 n1 then
            Some (h0, chunk_load ld1, rd1, b1, n1)
          else search_compat_rep_inv r2 b2 n2 insta t0 stype
      | PStore (PSt_rs_a (st1, IR (RR1 rs1), ADimm (b1, n1))), "strw" ->
          if is_valid_strw b2 b1 n2 n1 then
            Some (h0, chunk_store st1, rs1, b1, n1)
          else search_compat_rep_inv r2 b2 n2 insta t0 stype
      | PStore (PSt_rs_a (st1, IR (RR1 rs1), ADimm (b1, n1))), "strx" ->
          if is_valid_strx b2 b1 n2 n1 then
            Some (h0, chunk_store st1, rs1, b1, n1)
          else search_compat_rep_inv r2 b2 n2 insta t0 stype
      | _, _ ->
          failwith
            "search_compat_rep_inv: Found an inconsistent inst in pot_rep")

(* This is useful to manage the case were the immofs
 * of the first ldr/str is greater than the second one *)
let min_is_rev n1 n2 =
  let z1 = to_int (camlint64_of_coqint n1) in
  let z2 = to_int (camlint64_of_coqint n2) in
  if z1 < z2 then true else false

(* Below functions were added to merge pattern matching cases in peephole,
 * thanks to this, we can make the chunk difference (int/any) compatible. *)
let trans_ldi (ldi : load_rd_a) : load_rd1_rd2_a =
  match ldi with
  | Pldrw | Pldrw_a -> Pldpw
  | Pldrx | Pldrx_a -> Pldpx
  | _ -> failwith "trans_ldi: Found a non compatible load to translate"

let trans_sti (sti : store_rs_a) : store_rs1_rs2_a =
  match sti with
  | Pstrw | Pstrw_a -> Pstpw
  | Pstrx | Pstrx_a -> Pstpx
  | _ -> failwith "trans_sti: Found a non compatible store to translate"

let is_compat_load (ldi : load_rd_a) =
  match ldi with Pldrw | Pldrw_a | Pldrx | Pldrx_a -> true | _ -> false

let are_compat_load (ldi1 : load_rd_a) (ldi2 : load_rd_a) =
  match ldi1 with
  | Pldrw | Pldrw_a -> ( match ldi2 with Pldrw | Pldrw_a -> true | _ -> false)
  | Pldrx | Pldrx_a -> ( match ldi2 with Pldrx | Pldrx_a -> true | _ -> false)
  | _ -> false

let is_compat_store (sti : store_rs_a) =
  match sti with Pstrw | Pstrw_a | Pstrx | Pstrx_a -> true | _ -> false

let are_compat_store (sti1 : store_rs_a) (sti2 : store_rs_a) =
  match sti1 with
  | Pstrw | Pstrw_a -> ( match sti2 with Pstrw | Pstrw_a -> true | _ -> false)
  | Pstrx | Pstrx_a -> ( match sti2 with Pstrx | Pstrx_a -> true | _ -> false)
  | _ -> false

let get_load_string (ldi : load_rd_a) =
  match ldi with
  | Pldrw | Pldrw_a -> "ldrw"
  | Pldrx | Pldrx_a -> "ldrx"
  | _ -> failwith "get_load_string: Found a non compatible load to translate"

let get_store_string (sti : store_rs_a) =
  match sti with
  | Pstrw | Pstrw_a -> "strw"
  | Pstrx | Pstrx_a -> "strx"
  | _ -> failwith "get_store_string: Found a non compatible store to translate"

let is_valid_ldr rd1 rd2 b1 b2 n1 n2 stype =
  match stype with
  | "ldrw" -> is_valid_ldrw rd1 rd2 b1 b2 n1 n2
  | _ -> is_valid_ldrx rd1 rd2 b1 b2 n1 n2

let is_valid_str b1 b2 n1 n2 stype =
  match stype with
  | "strw" -> is_valid_strw b1 b2 n1 n2
  | _ -> is_valid_strx b1 b2 n1 n2

(* Main peephole function in backward style *)
let pair_rep_inv insta =
  (* Each list below is a symbolic mem representation
   * for one type of inst. Lists contains integers which
   * are the indices of insts in the main array "insta". *)
  let pot_ldrw_rep = ref [] in
  let pot_ldrx_rep = ref [] in
  let pot_strw_rep = ref [] in
  let pot_strx_rep = ref [] in
  for i = Array.length insta - 1 downto 1 do
    let h0 = insta.(i) in
    let h1 = insta.(i - 1) in
    (* Here we need to update every symbolic memory according to the matched inst *)
    update_pot_rep_basic h0 insta pot_ldrw_rep "ldr" true;
    update_pot_rep_basic h0 insta pot_ldrx_rep "ldr" true;
    update_pot_rep_basic h0 insta pot_strw_rep "str" true;
    update_pot_rep_basic h0 insta pot_strx_rep "str" true;
    match (h0, h1) with
    (* Non-consecutive ldr *)
    | PLoad (PLd_rd_a (ldi, IR (RR1 rd1), ADimm (b1, n1))), _ -> (
        if is_compat_load ldi then
          let pot_rep =
            match ldi with Pldrw | Pldrw_a -> pot_ldrw_rep | _ -> pot_ldrx_rep
          in
          (* Search a previous compatible load *)
          match
            search_compat_rep_inv rd1 b1 n1 insta !pot_rep (get_load_string ldi)
          with
          (* If we can't find a candidate, add the current load as a potential future one *)
          | None -> pot_rep := i :: !pot_rep
          (* Else, perform the peephole *)
          | Some (rep, c, r, b, n) ->
              (* The two lines below are used to filter the elected candidate *)
              let filt x = x != rep in
              pot_rep := List.filter filt !pot_rep;
              insta.(rep) <- Pnop;
              if min_is_rev n n1 then (
                if debug then eprintf "LDP_BACK_SPACED_PEEP_IMM_INC\n";
                insta.(i) <-
                  PLoad
                    (Pldp
                       (trans_ldi ldi, r, rd1, c, chunk_load ldi, ADimm (b, n))))
              else (
                if debug then eprintf "LDP_BACK_SPACED_PEEP_IMM_DEC\n";
                insta.(i) <-
                  PLoad
                    (Pldp
                       (trans_ldi ldi, rd1, r, chunk_load ldi, c, ADimm (b, n1))))
        )
    (* Non-consecutive str *)
    | PStore (PSt_rs_a (sti, IR (RR1 rd1), ADimm (b1, n1))), _ -> (
        if is_compat_store sti then
          let pot_rep =
            match sti with Pstrw | Pstrw_a -> pot_strw_rep | _ -> pot_strx_rep
          in
          (* Search a previous compatible store *)
          match
            search_compat_rep_inv rd1 b1 n1 insta !pot_rep
              (get_store_string sti)
          with
          (* If we can't find a candidate, clean and add the current store as a potential future one *)
          | None ->
              pot_strw_rep := [];
              pot_strx_rep := [];
              pot_rep := i :: !pot_rep
          (* Else, perform the peephole *)
          | Some (rep, c, r, b, n) ->
              (* The two lines below are used to filter the elected candidate *)
              let filt x = x != rep in
              pot_rep := List.filter filt !pot_rep;
              insta.(rep) <- Pnop;
              if debug then eprintf "STP_BACK_SPACED_PEEP_IMM_INC\n";
              insta.(i) <-
                PStore
                  (Pstp
                     (trans_sti sti, rd1, r, chunk_store sti, c, ADimm (b, n1)))
          (* Any other inst *))
    | i, _ -> (
        (* Clear list of candidates if there is a non supported store *)
        match i with
        | PStore _ ->
            pot_strw_rep := [];
            pot_strx_rep := []
        | _ -> ())
  done

(* Main peephole function in forward style *)
let pair_rep insta =
  (* Each list below is a symbolic mem representation
   * for one type of inst. Lists contains integers which
   * are the indices of insts in the main array "insta". *)
  let pot_ldrw_rep = ref [] in
  let pot_ldrx_rep = ref [] in
  let pot_strw_rep = ref [] in
  let pot_strx_rep = ref [] in
  for i = 0 to Array.length insta - 2 do
    let h0 = insta.(i) in
    let h1 = insta.(i + 1) in
    (* Here we need to update every symbolic memory according to the matched inst *)
    update_pot_rep_basic h0 insta pot_ldrw_rep "ldr" false;
    update_pot_rep_basic h0 insta pot_ldrx_rep "ldr" false;
    update_pot_rep_basic h0 insta pot_strw_rep "str" false;
    update_pot_rep_basic h0 insta pot_strx_rep "str" false;
    match (h0, h1) with
    (* Consecutive ldr *)
    | ( PLoad (PLd_rd_a (ldi1, IR (RR1 rd1), ADimm (b1, n1))),
        PLoad (PLd_rd_a (ldi2, IR (RR1 rd2), ADimm (b2, n2))) ) ->
        if are_compat_load ldi1 ldi2 then
          if is_valid_ldr rd1 rd2 b1 b2 n1 n2 (get_load_string ldi1) then (
            if min_is_rev n1 n2 then (
              if debug then eprintf "LDP_CONSEC_PEEP_IMM_INC\n";
              insta.(i) <-
                PLoad
                  (Pldp
                     ( trans_ldi ldi1,
                       rd1,
                       rd2,
                       chunk_load ldi1,
                       chunk_load ldi2,
                       ADimm (b1, n1) )))
            else (
              if debug then eprintf "LDP_CONSEC_PEEP_IMM_DEC\n";
              insta.(i) <-
                PLoad
                  (Pldp
                     ( trans_ldi ldi1,
                       rd2,
                       rd1,
                       chunk_load ldi2,
                       chunk_load ldi1,
                       ADimm (b1, n2) )));
            insta.(i + 1) <- Pnop)
    (* Non-consecutive ldr *)
    | PLoad (PLd_rd_a (ldi, IR (RR1 rd1), ADimm (b1, n1))), _ -> (
        if is_compat_load ldi then
          let pot_rep =
            match ldi with Pldrw | Pldrw_a -> pot_ldrw_rep | _ -> pot_ldrx_rep
          in
          (* Search a previous compatible load *)
          match
            search_compat_rep rd1 b1 n1 insta !pot_rep (get_load_string ldi)
          with
          (* If we can't find a candidate, add the current load as a potential future one *)
          | None -> pot_rep := i :: !pot_rep
          (* Else, perform the peephole *)
          | Some (rep, c, r, b, n) ->
              (* The two lines below are used to filter the elected candidate *)
              let filt x = x != rep in
              pot_rep := List.filter filt !pot_rep;
              insta.(rep) <- Pnop;
              if min_is_rev n n1 then (
                if debug then eprintf "LDP_FORW_SPACED_PEEP_IMM_INC\n";
                insta.(i) <-
                  PLoad
                    (Pldp
                       (trans_ldi ldi, r, rd1, c, chunk_load ldi, ADimm (b, n))))
              else (
                if debug then eprintf "LDP_FORW_SPACED_PEEP_IMM_DEC\n";
                insta.(i) <-
                  PLoad
                    (Pldp
                       (trans_ldi ldi, rd1, r, chunk_load ldi, c, ADimm (b, n1))))
        )
    (* Consecutive str *)
    | ( PStore (PSt_rs_a (sti1, IR (RR1 rd1), ADimm (b1, n1))),
        PStore (PSt_rs_a (sti2, IR (RR1 rd2), ADimm (b2, n2))) ) ->
        (* Regardless of whether we can perform the peephole or not,
         * we have to clean the potential candidates for stp now as we are
         * looking at two new store instructions. *)
        pot_strw_rep := [];
        pot_strx_rep := [];
        if are_compat_store sti1 sti2 then
          if is_valid_str b1 b2 n1 n2 (get_store_string sti1) then (
            if debug then eprintf "STP_CONSEC_PEEP_IMM_INC\n";
            insta.(i) <-
              PStore
                (Pstp
                   ( trans_sti sti1,
                     rd1,
                     rd2,
                     chunk_store sti1,
                     chunk_store sti2,
                     ADimm (b1, n1) ));
            insta.(i + 1) <- Pnop)
    (* Non-consecutive str *)
    | PStore (PSt_rs_a (sti, IR (RR1 rd1), ADimm (b1, n1))), _ -> (
        if is_compat_store sti then
          let pot_rep =
            match sti with Pstrw | Pstrw_a -> pot_strw_rep | _ -> pot_strx_rep
          in
          (* Search a previous compatible store *)
          match
            search_compat_rep rd1 b1 n1 insta !pot_rep (get_store_string sti)
          with
          (* If we can't find a candidate, clean and add the current store as a potential future one *)
          | None ->
              pot_strw_rep := [];
              pot_strx_rep := [];
              pot_rep := i :: !pot_rep
          (* Else, perform the peephole *)
          | Some (rep, c, r, b, n) ->
              (* The two lines below are used to filter the elected candidate *)
              let filt x = x != rep in
              pot_rep := List.filter filt !pot_rep;
              insta.(rep) <- Pnop;
              if debug then eprintf "STP_FORW_SPACED_PEEP_IMM_INC\n";
              insta.(i) <-
                PStore
                  (Pstp (trans_sti sti, r, rd1, c, chunk_store sti, ADimm (b, n)))
        )
    (* Any other inst *)
    | i, _ -> (
        (* Clear list of candidates if there is a non supported store *)
        match i with
        | PStore _ ->
            pot_strw_rep := [];
            pot_strx_rep := []
        | _ -> ())
  done

(* Calling peephole if flag is set *)
let optimize_bdy (bdy : basic list) : basic list =
  if !Clflags.option_fcoalesce_mem then (
    let insta = Array.of_list bdy in
    pair_rep insta;
    pair_rep_inv insta;
    Array.to_list insta)
  else bdy

(* Called peephole function from Coq *)
let peephole_opt bdy =
  Timing.time_coq
    [
      'P'; 'e'; 'e'; 'p'; 'h'; 'o'; 'l'; 'e'; ' '; 'o'; 'r'; 'a'; 'c'; 'l'; 'e';
    ]
    optimize_bdy bdy