summaryrefslogtreecommitdiff
path: root/ia32/ConstpropOp.v
blob: 7dfa046504907c8cb9702e583165648318e586cc (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
(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          Xavier Leroy, INRIA Paris-Rocquencourt                     *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Static analysis and strength reduction for operators 
  and conditions.  This is the machine-dependent part of [Constprop]. *)

Require Import Coqlib.
Require Import AST.
Require Import Integers.
Require Import Floats.
Require Import Values.
Require Import Op.
Require Import Registers.

(** * Static analysis *)

(** To each pseudo-register at each program point, the static analysis 
  associates a compile-time approximation taken from the following set. *)

Inductive approx : Type :=
  | Novalue: approx      (** No value possible, code is unreachable. *)
  | Unknown: approx      (** All values are possible,
                             no compile-time information is available. *)
  | I: int -> approx     (** A known integer value. *)
  | F: float -> approx   (** A known floating-point value. *)
  | S: ident -> int -> approx.
                         (** The value is the address of the given global
                             symbol plus the given integer offset. *)

(** We now define the abstract interpretations of conditions and operators
  over this set of approximations.  For instance, the abstract interpretation
  of the operator [Oaddf] applied to two expressions [a] and [b] is
  [F(Float.add f g)] if [a] and [b] have static approximations [Vfloat f]
  and [Vfloat g] respectively, and [Unknown] otherwise.

  The static approximations are defined by large pattern-matchings over
  the approximations of the results.  We write these matchings in the
  indirect style described in file [Cmconstr] to avoid excessive
  duplication of cases in proofs. *)

(*
Definition eval_static_condition (cond: condition) (vl: list approx) :=
  match cond, vl with
  | Ccomp c, I n1 :: I n2 :: nil => Some(Int.cmp c n1 n2)
  | Ccompu c, I n1 :: I n2 :: nil => Some(Int.cmpu c n1 n2)
  | Ccompimm c n, I n1 :: nil => Some(Int.cmp c n1 n)
  | Ccompuimm c n, I n1 :: nil => Some(Int.cmpu c n1 n)
  | Ccompf c, F n1 :: F n2 :: nil => Some(Float.cmp c n1 n2)
  | Cnotcompf c, F n1 :: F n2 :: nil => Some(negb(Float.cmp c n1 n2))
  | Cmaskzero n, I n1 :: nil => Some(Int.eq (Int.and n1 n) Int.zero)
  | Cmasknotzero n, n1::nil => Some(negb(Int.eq (Int.and n1 n) Int.zero))
  | _, _ => None
  end.
*)

Inductive eval_static_condition_cases: forall (cond: condition) (vl: list approx), Type :=
  | eval_static_condition_case1:
      forall c n1 n2,
      eval_static_condition_cases (Ccomp c) (I n1 :: I n2 :: nil)
  | eval_static_condition_case2:
      forall c n1 n2,
      eval_static_condition_cases (Ccompu c) (I n1 :: I n2 :: nil)
  | eval_static_condition_case3:
      forall c n n1,
      eval_static_condition_cases (Ccompimm c n) (I n1 :: nil)
  | eval_static_condition_case4:
      forall c n n1,
      eval_static_condition_cases (Ccompuimm c n) (I n1 :: nil)
  | eval_static_condition_case5:
      forall c n1 n2,
      eval_static_condition_cases (Ccompf c) (F n1 :: F n2 :: nil)
  | eval_static_condition_case6:
      forall c n1 n2,
      eval_static_condition_cases (Cnotcompf c) (F n1 :: F n2 :: nil)
  | eval_static_condition_case7:
      forall n n1,
      eval_static_condition_cases (Cmaskzero n) (I n1 :: nil)
  | eval_static_condition_case8:
      forall n n1,
      eval_static_condition_cases (Cmasknotzero n) (I n1 :: nil)
  | eval_static_condition_default:
      forall (cond: condition) (vl: list approx),
      eval_static_condition_cases cond vl.

Definition eval_static_condition_match (cond: condition) (vl: list approx) :=
  match cond as z1, vl as z2 return eval_static_condition_cases z1 z2 with
  | Ccomp c, I n1 :: I n2 :: nil =>
      eval_static_condition_case1 c n1 n2
  | Ccompu c, I n1 :: I n2 :: nil =>
      eval_static_condition_case2 c n1 n2
  | Ccompimm c n, I n1 :: nil =>
      eval_static_condition_case3 c n n1
  | Ccompuimm c n, I n1 :: nil =>
      eval_static_condition_case4 c n n1
  | Ccompf c, F n1 :: F n2 :: nil =>
      eval_static_condition_case5 c n1 n2
  | Cnotcompf c, F n1 :: F n2 :: nil =>
      eval_static_condition_case6 c n1 n2
  | Cmaskzero n, I n1 :: nil =>
      eval_static_condition_case7 n n1
  | Cmasknotzero n, I n1 :: nil =>
      eval_static_condition_case8 n n1
  | cond, vl =>
      eval_static_condition_default cond vl
  end.

Definition eval_static_condition (cond: condition) (vl: list approx) :=
  match eval_static_condition_match cond vl with
  | eval_static_condition_case1 c n1 n2 =>
      Some(Int.cmp c n1 n2)
  | eval_static_condition_case2 c n1 n2 =>
      Some(Int.cmpu c n1 n2)
  | eval_static_condition_case3 c n n1 =>
      Some(Int.cmp c n1 n)
  | eval_static_condition_case4 c n n1 =>
      Some(Int.cmpu c n1 n)
  | eval_static_condition_case5 c n1 n2 =>
      Some(Float.cmp c n1 n2)
  | eval_static_condition_case6 c n1 n2 =>
      Some(negb(Float.cmp c n1 n2))
  | eval_static_condition_case7 n n1 =>
      Some(Int.eq (Int.and n1 n) Int.zero)
  | eval_static_condition_case8 n n1 =>
      Some(negb(Int.eq (Int.and n1 n) Int.zero))
  | eval_static_condition_default cond vl =>
      None
  end.

(*
Definition eval_static_addressing (addr: addressing) (vl: list approx) :=
  match op, vl with
  | Aindexed n, I n1::nil => I (Int.add n1 n)
  | Aindexed n, S id ofs::nil => S id (Int.add ofs n)
  | Aindexed2 n, I n1::I n2::nil => I (Int.add (Int.add n1 n2) n)
  | Aindexed2 n, S id ofs::I n2::nil => S id (Int.add (Int.add ofs n2) n)
  | Aindexed2 n, I n1::S id ofs::nil => S id (Int.add (Int.add ofs n1) n)
  | Ascaled sc n, I n1::nil => I (Int.add (Int.mul n1 sc) n)
  | Aindexed2scaled sc n, I n1::I n2::nil => I (Int.add n1 (Int.add (Int.mul n2 sc) n))
  | Aindexed2scaled sc n, S id ofs::I n2::nil => S id (Int.add ofs (Int.add (Int.mul n2 sc) n))
  | Aglobal id ofs, nil => S id ofs
  | Abased id ofs, I n1::nil => S id (Int.add ofs n1)
  | Abasedscaled sc id ofs, I n1::nil => S id (Int.add ofs (Int.mul sc n1))
  | _, _ => Unknown
  end.
*)

Inductive eval_static_addressing_cases: forall (addr: addressing) (vl: list approx), Type :=
  | eval_static_addressing_case1:
      forall n n1,
      eval_static_addressing_cases (Aindexed n) (I n1::nil)
  | eval_static_addressing_case2:
      forall n id ofs,
      eval_static_addressing_cases (Aindexed n) (S id ofs::nil)
  | eval_static_addressing_case3:
      forall n n1 n2,
      eval_static_addressing_cases (Aindexed2 n) (I n1::I n2::nil)
  | eval_static_addressing_case4:
      forall n id ofs n2,
      eval_static_addressing_cases (Aindexed2 n) (S id ofs::I n2::nil)
  | eval_static_addressing_case5:
      forall n n1 id ofs,
      eval_static_addressing_cases (Aindexed2 n) (I n1::S id ofs::nil)
  | eval_static_addressing_case6:
      forall sc n n1,
      eval_static_addressing_cases (Ascaled sc n) (I n1::nil)
  | eval_static_addressing_case7:
      forall sc n n1 n2,
      eval_static_addressing_cases (Aindexed2scaled sc n) (I n1::I n2::nil)
  | eval_static_addressing_case8:
      forall sc n id ofs n2,
      eval_static_addressing_cases (Aindexed2scaled sc n) (S id ofs::I n2::nil)
  | eval_static_addressing_case9:
      forall id ofs,
      eval_static_addressing_cases (Aglobal id ofs) (nil)
  | eval_static_addressing_case10:
      forall id ofs n1,
      eval_static_addressing_cases (Abased id ofs) (I n1::nil)
  | eval_static_addressing_case11:
      forall sc id ofs n1,
      eval_static_addressing_cases (Abasedscaled sc id ofs) (I n1::nil)
  | eval_static_addressing_default:
      forall (addr: addressing) (vl: list approx),
      eval_static_addressing_cases addr vl.

Definition eval_static_addressing_match (addr: addressing) (vl: list approx) :=
  match addr as z1, vl as z2 return eval_static_addressing_cases z1 z2 with
  | Aindexed n, I n1::nil =>
      eval_static_addressing_case1 n n1
  | Aindexed n, S id ofs::nil =>
      eval_static_addressing_case2 n id ofs
  | Aindexed2 n, I n1::I n2::nil =>
      eval_static_addressing_case3 n n1 n2
  | Aindexed2 n, S id ofs::I n2::nil =>
      eval_static_addressing_case4 n id ofs n2
  | Aindexed2 n, I n1::S id ofs::nil =>
      eval_static_addressing_case5 n n1 id ofs
  | Ascaled sc n, I n1::nil =>
      eval_static_addressing_case6 sc n n1
  | Aindexed2scaled sc n, I n1::I n2::nil =>
      eval_static_addressing_case7 sc n n1 n2
  | Aindexed2scaled sc n, S id ofs::I n2::nil =>
      eval_static_addressing_case8 sc n id ofs n2
  | Aglobal id ofs, nil =>
      eval_static_addressing_case9 id ofs
  | Abased id ofs, I n1::nil =>
      eval_static_addressing_case10 id ofs n1
  | Abasedscaled sc id ofs, I n1::nil =>
      eval_static_addressing_case11 sc id ofs n1
  | addr, vl =>
      eval_static_addressing_default addr vl
  end.

Definition eval_static_addressing (addr: addressing) (vl: list approx) :=
  match eval_static_addressing_match addr vl with
  | eval_static_addressing_case1 n n1 =>
      I (Int.add n1 n)
  | eval_static_addressing_case2 n id ofs =>
      S id (Int.add ofs n)
  | eval_static_addressing_case3 n n1 n2 =>
      I (Int.add (Int.add n1 n2) n)
  | eval_static_addressing_case4 n id ofs n2 =>
      S id (Int.add (Int.add ofs n2) n)
  | eval_static_addressing_case5 n n1 id ofs =>
      S id (Int.add (Int.add ofs n1) n)
  | eval_static_addressing_case6 sc n n1 =>
      I (Int.add (Int.mul n1 sc) n)
  | eval_static_addressing_case7 sc n n1 n2 =>
      I (Int.add n1 (Int.add (Int.mul n2 sc) n))
  | eval_static_addressing_case8 sc n id ofs n2 =>
      S id (Int.add ofs (Int.add (Int.mul n2 sc) n))
  | eval_static_addressing_case9 id ofs =>
      S id ofs
  | eval_static_addressing_case10 id ofs n1 =>
      S id (Int.add ofs n1)
  | eval_static_addressing_case11 sc id ofs n1 =>
      S id (Int.add ofs (Int.mul sc n1))
  | eval_static_addressing_default addr vl =>
      Unknown
  end.

(*
Definition eval_static_operation (op: operation) (vl: list approx) :=
  match op, vl with
  | Omove, v1::nil => v1
  | Ointconst n, nil => I n
  | Ofloatconst n, nil => F n
  | Ocast8signed, I n1 :: nil => I(Int.sign_ext 8 n1)
  | Ocast8unsigned, I n1 :: nil => I(Int.zero_ext 8 n1)
  | Ocast16signed, I n1 :: nil => I(Int.sign_ext 16 n1)
  | Ocast16unsigned, I n1 :: nil => I(Int.zero_ext 16 n1)
  | Oneg, I n1 :: nil => I(Int.neg n1)
  | Osub, I n1 :: I n2 :: nil => I(Int.sub n1 n2)
  | Osub, S s1 n1 :: I n2 :: nil => S s1 (Int.sub n1 n2)
  | Omul, I n1 :: I n2 :: nil => I(Int.mul n1 n2)
  | Omulimm n, I n1 :: nil => I(Int.mul n1 n)
  | Odiv, I n1 :: I n2 :: nil => if Int.eq n2 Int.zero then Unknown else I(Int.divs n1 n2)
  | Odivu, I n1 :: I n2 :: nil => if Int.eq n2 Int.zero then Unknown else I(Int.divu n1 n2)
  | Omod, I n1 :: I n2 :: nil => if Int.eq n2 Int.zero then Unknown else I(Int.mods n1 n2)
  | Omodu, I n1 :: I n2 :: nil => if Int.eq n2 Int.zero then Unknown else I(Int.modu n1 n2)
  | Oand, I n1 :: I n2 :: nil => I(Int.and n1 n2)
  | Oandimm n, I n1 :: nil => I(Int.and n1 n)
  | Oor, I n1 :: I n2 :: nil => I(Int.or n1 n2)
  | Oorimm n, I n1 :: nil => I(Int.or n1 n)
  | Oxor, I n1 :: I n2 :: nil => I(Int.xor n1 n2)
  | Oxorimm n, I n1 :: nil => I(Int.xor n1 n)
  | Oshl, I n1 :: I n2 :: nil => if Int.ltu n2 Int.iwordsize then I(Int.shl n1 n2) else Unknown
  | Oshlimm n, I n1 :: nil => if Int.ltu n Int.iwordsize then I(Int.shl n1 n) else Unknown
  | Oshr, I n1 :: I n2 :: nil => if Int.ltu n2 Int.iwordsize then I(Int.shr n1 n2) else Unknown
  | Oshrimm n, I n1 :: nil => if Int.ltu n Int.iwordsize then I(Int.shr n1 n) else Unknown
  | Oshrximm n, I n1 :: nil => if Int.ltu n Int.iwordsize then I(Int.shrx n1 n) else Unknown
  | Oshru, I n1 :: I n2 :: nil => if Int.ltu n2 Int.iwordsize then I(Int.shru n1 n2) else Unknown
  | Oshruimm n, I n1 :: nil => if Int.ltu n Int.iwordsize then I(Int.shru n1 n) else Unknown
  | Ororimm n, I n1 :: nil => if Int.ltu n Int.iwordsize then I(Int.ror n1 n) else Unknown
  | Olea mode, vl => eval_static_addressing mode vl
  | Onegf, F n1 :: nil => F(Float.neg n1)
  | Oabsf, F n1 :: nil => F(Float.abs n1)
  | Oaddf, F n1 :: F n2 :: nil => F(Float.add n1 n2)
  | Osubf, F n1 :: F n2 :: nil => F(Float.sub n1 n2)
  | Omulf, F n1 :: F n2 :: nil => F(Float.mul n1 n2)
  | Odivf, F n1 :: F n2 :: nil => F(Float.div n1 n2)
  | Osingleoffloat, F n1 :: nil => F(Float.singleoffloat n1)
  | Ointoffloat, F n1 :: nil => I(Float.intoffloat n1)
  | Ofloatofint, I n1 :: nil => F(Float.floatofint n1)
  | Ocmp c, vl => match eval_static_condition c vl with None => Unknown | Some b => I(if b then Int.one else Int.zero) end
  | _, _ => Unknown
  end.
*)

Inductive eval_static_operation_cases: forall (op: operation) (vl: list approx), Type :=
  | eval_static_operation_case1:
      forall v1,
      eval_static_operation_cases (Omove) (v1::nil)
  | eval_static_operation_case2:
      forall n,
      eval_static_operation_cases (Ointconst n) (nil)
  | eval_static_operation_case3:
      forall n,
      eval_static_operation_cases (Ofloatconst n) (nil)
  | eval_static_operation_case4:
      forall n1,
      eval_static_operation_cases (Ocast8signed) (I n1 :: nil)
  | eval_static_operation_case5:
      forall n1,
      eval_static_operation_cases (Ocast8unsigned) (I n1 :: nil)
  | eval_static_operation_case6:
      forall n1,
      eval_static_operation_cases (Ocast16signed) (I n1 :: nil)
  | eval_static_operation_case7:
      forall n1,
      eval_static_operation_cases (Ocast16unsigned) (I n1 :: nil)
  | eval_static_operation_case8:
      forall n1,
      eval_static_operation_cases (Oneg) (I n1 :: nil)
  | eval_static_operation_case9:
      forall n1 n2,
      eval_static_operation_cases (Osub) (I n1 :: I n2 :: nil)
  | eval_static_operation_case10:
      forall s1 n1 n2,
      eval_static_operation_cases (Osub) (S s1 n1 :: I n2 :: nil)
  | eval_static_operation_case11:
      forall n1 n2,
      eval_static_operation_cases (Omul) (I n1 :: I n2 :: nil)
  | eval_static_operation_case12:
      forall n n1,
      eval_static_operation_cases (Omulimm n) (I n1 :: nil)
  | eval_static_operation_case13:
      forall n1 n2,
      eval_static_operation_cases (Odiv) (I n1 :: I n2 :: nil)
  | eval_static_operation_case14:
      forall n1 n2,
      eval_static_operation_cases (Odivu) (I n1 :: I n2 :: nil)
  | eval_static_operation_case15:
      forall n1 n2,
      eval_static_operation_cases (Omod) (I n1 :: I n2 :: nil)
  | eval_static_operation_case16:
      forall n1 n2,
      eval_static_operation_cases (Omodu) (I n1 :: I n2 :: nil)
  | eval_static_operation_case17:
      forall n1 n2,
      eval_static_operation_cases (Oand) (I n1 :: I n2 :: nil)
  | eval_static_operation_case18:
      forall n n1,
      eval_static_operation_cases (Oandimm n) (I n1 :: nil)
  | eval_static_operation_case19:
      forall n1 n2,
      eval_static_operation_cases (Oor) (I n1 :: I n2 :: nil)
  | eval_static_operation_case20:
      forall n n1,
      eval_static_operation_cases (Oorimm n) (I n1 :: nil)
  | eval_static_operation_case21:
      forall n1 n2,
      eval_static_operation_cases (Oxor) (I n1 :: I n2 :: nil)
  | eval_static_operation_case22:
      forall n n1,
      eval_static_operation_cases (Oxorimm n) (I n1 :: nil)
  | eval_static_operation_case23:
      forall n1 n2,
      eval_static_operation_cases (Oshl) (I n1 :: I n2 :: nil)
  | eval_static_operation_case24:
      forall n n1,
      eval_static_operation_cases (Oshlimm n) (I n1 :: nil)
  | eval_static_operation_case25:
      forall n1 n2,
      eval_static_operation_cases (Oshr) (I n1 :: I n2 :: nil)
  | eval_static_operation_case26:
      forall n n1,
      eval_static_operation_cases (Oshrimm n) (I n1 :: nil)
  | eval_static_operation_case27:
      forall n n1,
      eval_static_operation_cases (Oshrximm n) (I n1 :: nil)
  | eval_static_operation_case28:
      forall n1 n2,
      eval_static_operation_cases (Oshru) (I n1 :: I n2 :: nil)
  | eval_static_operation_case29:
      forall n n1,
      eval_static_operation_cases (Oshruimm n) (I n1 :: nil)
  | eval_static_operation_case30:
      forall n n1,
      eval_static_operation_cases (Ororimm n) (I n1 :: nil)
  | eval_static_operation_case31:
      forall mode vl,
      eval_static_operation_cases (Olea mode) (vl)
  | eval_static_operation_case32:
      forall n1,
      eval_static_operation_cases (Onegf) (F n1 :: nil)
  | eval_static_operation_case33:
      forall n1,
      eval_static_operation_cases (Oabsf) (F n1 :: nil)
  | eval_static_operation_case34:
      forall n1 n2,
      eval_static_operation_cases (Oaddf) (F n1 :: F n2 :: nil)
  | eval_static_operation_case35:
      forall n1 n2,
      eval_static_operation_cases (Osubf) (F n1 :: F n2 :: nil)
  | eval_static_operation_case36:
      forall n1 n2,
      eval_static_operation_cases (Omulf) (F n1 :: F n2 :: nil)
  | eval_static_operation_case37:
      forall n1 n2,
      eval_static_operation_cases (Odivf) (F n1 :: F n2 :: nil)
  | eval_static_operation_case38:
      forall n1,
      eval_static_operation_cases (Osingleoffloat) (F n1 :: nil)
  | eval_static_operation_case39:
      forall n1,
      eval_static_operation_cases (Ointoffloat) (F n1 :: nil)
  | eval_static_operation_case41:
      forall n1,
      eval_static_operation_cases (Ofloatofint) (I n1 :: nil)
  | eval_static_operation_case43:
      forall c vl,
      eval_static_operation_cases (Ocmp c) vl
  | eval_static_operation_default:
      forall (op: operation) (vl: list approx),
      eval_static_operation_cases op vl.

Definition eval_static_operation_match (op: operation) (vl: list approx) :=
  match op as z1, vl as z2 return eval_static_operation_cases z1 z2 with
  | Omove, v1::nil =>
      eval_static_operation_case1 v1
  | Ointconst n, nil =>
      eval_static_operation_case2 n
  | Ofloatconst n, nil =>
      eval_static_operation_case3 n
  | Ocast8signed, I n1 :: nil =>
      eval_static_operation_case4 n1
  | Ocast8unsigned, I n1 :: nil =>
      eval_static_operation_case5 n1
  | Ocast16signed, I n1 :: nil =>
      eval_static_operation_case6 n1
  | Ocast16unsigned, I n1 :: nil =>
      eval_static_operation_case7 n1
  | Oneg, I n1 :: nil =>
      eval_static_operation_case8 n1
  | Osub, I n1 :: I n2 :: nil =>
      eval_static_operation_case9 n1 n2
  | Osub, S s1 n1 :: I n2 :: nil =>
      eval_static_operation_case10 s1 n1 n2
  | Omul, I n1 :: I n2 :: nil =>
      eval_static_operation_case11 n1 n2
  | Omulimm n, I n1 :: nil =>
      eval_static_operation_case12 n n1
  | Odiv, I n1 :: I n2 :: nil =>
      eval_static_operation_case13 n1 n2
  | Odivu, I n1 :: I n2 :: nil =>
      eval_static_operation_case14 n1 n2
  | Omod, I n1 :: I n2 :: nil =>
      eval_static_operation_case15 n1 n2
  | Omodu, I n1 :: I n2 :: nil =>
      eval_static_operation_case16 n1 n2
  | Oand, I n1 :: I n2 :: nil =>
      eval_static_operation_case17 n1 n2
  | Oandimm n, I n1 :: nil =>
      eval_static_operation_case18 n n1
  | Oor, I n1 :: I n2 :: nil =>
      eval_static_operation_case19 n1 n2
  | Oorimm n, I n1 :: nil =>
      eval_static_operation_case20 n n1
  | Oxor, I n1 :: I n2 :: nil =>
      eval_static_operation_case21 n1 n2
  | Oxorimm n, I n1 :: nil =>
      eval_static_operation_case22 n n1
  | Oshl, I n1 :: I n2 :: nil =>
      eval_static_operation_case23 n1 n2
  | Oshlimm n, I n1 :: nil =>
      eval_static_operation_case24 n n1
  | Oshr, I n1 :: I n2 :: nil =>
      eval_static_operation_case25 n1 n2
  | Oshrimm n, I n1 :: nil =>
      eval_static_operation_case26 n n1
  | Oshrximm n, I n1 :: nil =>
      eval_static_operation_case27 n n1
  | Oshru, I n1 :: I n2 :: nil =>
      eval_static_operation_case28 n1 n2
  | Oshruimm n, I n1 :: nil =>
      eval_static_operation_case29 n n1
  | Ororimm n, I n1 :: nil =>
      eval_static_operation_case30 n n1
  | Olea mode, vl =>
      eval_static_operation_case31 mode vl
  | Onegf, F n1 :: nil =>
      eval_static_operation_case32 n1
  | Oabsf, F n1 :: nil =>
      eval_static_operation_case33 n1
  | Oaddf, F n1 :: F n2 :: nil =>
      eval_static_operation_case34 n1 n2
  | Osubf, F n1 :: F n2 :: nil =>
      eval_static_operation_case35 n1 n2
  | Omulf, F n1 :: F n2 :: nil =>
      eval_static_operation_case36 n1 n2
  | Odivf, F n1 :: F n2 :: nil =>
      eval_static_operation_case37 n1 n2
  | Osingleoffloat, F n1 :: nil =>
      eval_static_operation_case38 n1
  | Ointoffloat, F n1 :: nil =>
      eval_static_operation_case39 n1
  | Ofloatofint, I n1 :: nil =>
      eval_static_operation_case41 n1
  | Ocmp c, vl =>
      eval_static_operation_case43 c vl
  | op, vl =>
      eval_static_operation_default op vl
  end.

Definition eval_static_operation (op: operation) (vl: list approx) :=
  match eval_static_operation_match op vl with
  | eval_static_operation_case1 v1 =>
      v1
  | eval_static_operation_case2 n =>
      I n
  | eval_static_operation_case3 n =>
      F n
  | eval_static_operation_case4 n1 =>
      I(Int.sign_ext 8 n1)
  | eval_static_operation_case5 n1 =>
      I(Int.zero_ext 8 n1)
  | eval_static_operation_case6 n1 =>
      I(Int.sign_ext 16 n1)
  | eval_static_operation_case7 n1 =>
      I(Int.zero_ext 16 n1)
  | eval_static_operation_case8 n1 =>
      I(Int.neg n1)
  | eval_static_operation_case9 n1 n2 =>
      I(Int.sub n1 n2)
  | eval_static_operation_case10 s1 n1 n2 =>
      S s1 (Int.sub n1 n2)
  | eval_static_operation_case11 n1 n2 =>
      I(Int.mul n1 n2)
  | eval_static_operation_case12 n n1 =>
      I(Int.mul n1 n)
  | eval_static_operation_case13 n1 n2 =>
      if Int.eq n2 Int.zero then Unknown else I(Int.divs n1 n2)
  | eval_static_operation_case14 n1 n2 =>
      if Int.eq n2 Int.zero then Unknown else I(Int.divu n1 n2)
  | eval_static_operation_case15 n1 n2 =>
      if Int.eq n2 Int.zero then Unknown else I(Int.mods n1 n2)
  | eval_static_operation_case16 n1 n2 =>
      if Int.eq n2 Int.zero then Unknown else I(Int.modu n1 n2)
  | eval_static_operation_case17 n1 n2 =>
      I(Int.and n1 n2)
  | eval_static_operation_case18 n n1 =>
      I(Int.and n1 n)
  | eval_static_operation_case19 n1 n2 =>
      I(Int.or n1 n2)
  | eval_static_operation_case20 n n1 =>
      I(Int.or n1 n)
  | eval_static_operation_case21 n1 n2 =>
      I(Int.xor n1 n2)
  | eval_static_operation_case22 n n1 =>
      I(Int.xor n1 n)
  | eval_static_operation_case23 n1 n2 =>
      if Int.ltu n2 Int.iwordsize then I(Int.shl n1 n2) else Unknown
  | eval_static_operation_case24 n n1 =>
      if Int.ltu n Int.iwordsize then I(Int.shl n1 n) else Unknown
  | eval_static_operation_case25 n1 n2 =>
      if Int.ltu n2 Int.iwordsize then I(Int.shr n1 n2) else Unknown
  | eval_static_operation_case26 n n1 =>
      if Int.ltu n Int.iwordsize then I(Int.shr n1 n) else Unknown
  | eval_static_operation_case27 n n1 =>
      if Int.ltu n (Int.repr 31) then I(Int.shrx n1 n) else Unknown
  | eval_static_operation_case28 n1 n2 =>
      if Int.ltu n2 Int.iwordsize then I(Int.shru n1 n2) else Unknown
  | eval_static_operation_case29 n n1 =>
      if Int.ltu n Int.iwordsize then I(Int.shru n1 n) else Unknown
  | eval_static_operation_case30 n n1 =>
      if Int.ltu n Int.iwordsize then I(Int.ror n1 n) else Unknown
  | eval_static_operation_case31 mode vl =>
      eval_static_addressing mode vl
  | eval_static_operation_case32 n1 =>
      F(Float.neg n1)
  | eval_static_operation_case33 n1 =>
      F(Float.abs n1)
  | eval_static_operation_case34 n1 n2 =>
      F(Float.add n1 n2)
  | eval_static_operation_case35 n1 n2 =>
      F(Float.sub n1 n2)
  | eval_static_operation_case36 n1 n2 =>
      F(Float.mul n1 n2)
  | eval_static_operation_case37 n1 n2 =>
      F(Float.div n1 n2)
  | eval_static_operation_case38 n1 =>
      F(Float.singleoffloat n1)
  | eval_static_operation_case39 n1 =>
      I(Float.intoffloat n1)
  | eval_static_operation_case41 n1 =>
      F(Float.floatofint n1)
  | eval_static_operation_case43 c vl =>
      match eval_static_condition c vl with
      | None => Unknown
      | Some b => I(if b then Int.one else Int.zero)
      end
  | eval_static_operation_default op vl =>
      Unknown
  end.

(** * Operator strength reduction *)

(** We now define auxiliary functions for strength reduction of
  operators and addressing modes: replacing an operator with a cheaper
  one if some of its arguments are statically known.  These are again
  large pattern-matchings expressed in indirect style. *)

Section STRENGTH_REDUCTION.

Variable app: reg -> approx.

Definition intval (r: reg) : option int :=
  match app r with I n => Some n | _ => None end.

Inductive cond_strength_reduction_cases: condition -> list reg -> Type :=
  | csr_case1:
      forall c r1 r2,
      cond_strength_reduction_cases (Ccomp c) (r1 :: r2 :: nil)
  | csr_case2:
      forall c r1 r2,
      cond_strength_reduction_cases (Ccompu c) (r1 :: r2 :: nil)
  | csr_default:
      forall c rl,
      cond_strength_reduction_cases c rl.

Definition cond_strength_reduction_match (cond: condition) (rl: list reg) :=
  match cond as x, rl as y return cond_strength_reduction_cases x y with
  | Ccomp c, r1 :: r2 :: nil =>
      csr_case1 c r1 r2
  | Ccompu c, r1 :: r2 :: nil =>
      csr_case2 c r1 r2
  | cond, rl =>
      csr_default cond rl
  end.

Definition cond_strength_reduction
              (cond: condition) (args: list reg) : condition * list reg :=
  match cond_strength_reduction_match cond args with
  | csr_case1 c r1 r2 =>
      match intval r1, intval r2 with
      | Some n, _ =>
          (Ccompimm (swap_comparison c) n, r2 :: nil)
      | _, Some n =>
          (Ccompimm c n, r1 :: nil)
      | _, _ =>
          (cond, args)
      end
  | csr_case2 c r1 r2 =>
      match intval r1, intval r2 with
      | Some n, _ =>
          (Ccompuimm (swap_comparison c) n, r2 :: nil)
      | _, Some n =>
          (Ccompuimm c n, r1 :: nil)
      | _, _ =>
          (cond, args)
      end
  | csr_default cond args =>
      (cond, args)
  end.

(*
Definition addr_strength_reduction (addr: addressing) (args: list reg) :=
  match addr, args with
  | Aindexed ofs, r1 :: nil => (* Aindexed *)
  | Aindexed2 ofs, r1 :: r2 :: nil => (* Aindexed2 *)
  | Aindexed2scaled sc ofs, r1 :: r2 :: nil => (* Aindexed2scaled *)
  | Abased id ofs, r1 :: nil => (* Abased *)
  | Abasedscaled sc id ofs, r1 :: nil => (* Abasedscaled *)
  | _, _ => (* default *)
  end.
*)

Inductive addr_strength_reduction_cases: forall (addr: addressing) (args: list reg), Type :=
  | addr_strength_reduction_case1:
      forall ofs r1,
      addr_strength_reduction_cases (Aindexed ofs) (r1 :: nil)
  | addr_strength_reduction_case2:
      forall ofs r1 r2,
      addr_strength_reduction_cases (Aindexed2 ofs) (r1 :: r2 :: nil)
  | addr_strength_reduction_case3:
      forall sc ofs r1 r2,
      addr_strength_reduction_cases (Aindexed2scaled sc ofs) (r1 :: r2 :: nil)
  | addr_strength_reduction_case4:
      forall id ofs r1,
      addr_strength_reduction_cases (Abased id ofs) (r1 :: nil)
  | addr_strength_reduction_case5:
      forall sc id ofs r1,
      addr_strength_reduction_cases (Abasedscaled sc id ofs) (r1 :: nil)
  | addr_strength_reduction_default:
      forall (addr: addressing) (args: list reg),
      addr_strength_reduction_cases addr args.

Definition addr_strength_reduction_match (addr: addressing) (args: list reg) :=
  match addr as z1, args as z2 return addr_strength_reduction_cases z1 z2 with
  | Aindexed ofs, r1 :: nil =>
      addr_strength_reduction_case1 ofs r1
  | Aindexed2 ofs, r1 :: r2 :: nil =>
      addr_strength_reduction_case2 ofs r1 r2
  | Aindexed2scaled sc ofs, r1 :: r2 :: nil =>
      addr_strength_reduction_case3 sc ofs r1 r2
  | Abased id ofs, r1 :: nil =>
      addr_strength_reduction_case4 id ofs r1
  | Abasedscaled sc id ofs, r1 :: nil =>
      addr_strength_reduction_case5 sc id ofs r1
  | addr, args =>
      addr_strength_reduction_default addr args
  end.

Definition addr_strength_reduction (addr: addressing) (args: list reg) :=
  match addr_strength_reduction_match addr args with
  | addr_strength_reduction_case1 ofs r1 =>
      (* Aindexed *)
      match app r1 with
      | S symb n => (Aglobal symb (Int.add ofs n), nil)
      | _ => (addr, args)
      end
  | addr_strength_reduction_case2 ofs r1 r2 =>
      (* Aindexed2 *)
      match app r1, app r2 with
      | S symb n1, I n2 => (Aglobal symb (Int.add (Int.add n1 n2) ofs), nil)
      | I n1, S symb n2 => (Aglobal symb (Int.add (Int.add n1 n2) ofs), nil)
      | S symb n1, _ => (Abased symb (Int.add n1 ofs), r2 :: nil)
      | _, S symb n2 => (Abased symb (Int.add n2 ofs), r1 :: nil)
      | I n1, _ => (Aindexed (Int.add n1 ofs), r2 :: nil)
      | _, I n2 => (Aindexed (Int.add n2 ofs), r1 :: nil)
      | _, _ => (addr, args)
      end
  | addr_strength_reduction_case3 sc ofs r1 r2 =>
      (* Aindexed2scaled *)
      match app r1, app r2 with
      | S symb n1, I n2 => (Aglobal symb (Int.add (Int.add n1 (Int.mul n2 sc)) ofs), nil)
      | S symb n1, _ => (Abasedscaled sc symb (Int.add n1 ofs), r2 :: nil)
      | _, I n2 => (Aindexed (Int.add (Int.mul n2 sc) ofs), r1 :: nil)
      | _, _ => (addr, args)
      end
  | addr_strength_reduction_case4 id ofs r1 =>
      (* Abased *)
      match app r1 with
      | I n1 => (Aglobal id (Int.add ofs n1), nil)
      | _ => (addr, args)
      end
  | addr_strength_reduction_case5 sc id ofs r1 =>
      (* Abasedscaled *)
      match app r1 with
      | I n1 => (Aglobal id (Int.add ofs (Int.mul sc n1)), nil)
      | _ => (addr, args)
      end
  | addr_strength_reduction_default addr args =>
      (addr, args)
  end.

Definition make_addimm (n: int) (r: reg) :=
  if Int.eq n Int.zero
  then (Omove, r :: nil)
  else (Oaddimm n, r :: nil).

Definition make_shlimm (n: int) (r: reg) :=
  if Int.eq n Int.zero
  then (Omove, r :: nil)
  else (Oshlimm n, r :: nil).

Definition make_shrimm (n: int) (r: reg) :=
  if Int.eq n Int.zero
  then (Omove, r :: nil)
  else (Oshrimm n, r :: nil).

Definition make_shruimm (n: int) (r: reg) :=
  if Int.eq n Int.zero
  then (Omove, r :: nil)
  else (Oshruimm n, r :: nil).

Definition make_mulimm (n: int) (r: reg) :=
  if Int.eq n Int.zero then
    (Ointconst Int.zero, nil)
  else if Int.eq n Int.one then
    (Omove, r :: nil)
  else
    match Int.is_power2 n with
    | Some l => make_shlimm l r
    | None => (Omulimm n, r :: nil)
    end.

Definition make_andimm (n: int) (r: reg) :=
  if Int.eq n Int.zero
  then (Ointconst Int.zero, nil)
  else if Int.eq n Int.mone then (Omove, r :: nil)
  else (Oandimm n, r :: nil).

Definition make_orimm (n: int) (r: reg) :=
  if Int.eq n Int.zero then (Omove, r :: nil)
  else if Int.eq n Int.mone then (Ointconst Int.mone, nil)
  else (Oorimm n, r :: nil).

Definition make_xorimm (n: int) (r: reg) :=
  if Int.eq n Int.zero
  then (Omove, r :: nil)
  else (Oxorimm n, r :: nil).

(*
Definition op_strength_reduction (op: operation) (args: list reg) :=
  match op, args with
  | Osub, r1 :: r2 :: nil => (* Osub *)
  | Omul, r1 :: r2 :: nil => (* Omul *)
  | Odiv, r1 :: r2 :: nil => (* Odiv *)
  | Odivu, r1 :: r2 :: nil => (* Odivu *)
  | Omodu, r1 :: r2 :: nil => (* Omodu *)
  | Oand, r1 :: r2 :: nil => (* Oand *)
  | Oor, r1 :: r2 :: nil => (* Oor *)
  | Oxor, r1 :: r2 :: nil => (* Oxor *)
  | Oshl, r1 :: r2 :: nil => (* Oshl *)
  | Oshr, r1 :: r2 :: nil => (* Oshr *)
  | Oshru, r1 :: r2 :: nil => (* Oshru *)
  | Olea addr, args => (* Olea *)
  | Ocmp c, args => (* Ocmp *)
  | _, _ => (* default *)
  end.
*)

Inductive op_strength_reduction_cases: forall (op: operation) (args: list reg), Type :=
  | op_strength_reduction_case2:
      forall r1 r2,
      op_strength_reduction_cases (Osub) (r1 :: r2 :: nil)
  | op_strength_reduction_case3:
      forall r1 r2,
      op_strength_reduction_cases (Omul) (r1 :: r2 :: nil)
  | op_strength_reduction_case4:
      forall r1 r2,
      op_strength_reduction_cases (Odiv) (r1 :: r2 :: nil)
  | op_strength_reduction_case5:
      forall r1 r2,
      op_strength_reduction_cases (Odivu) (r1 :: r2 :: nil)
  | op_strength_reduction_case7:
      forall r1 r2,
      op_strength_reduction_cases (Omodu) (r1 :: r2 :: nil)
  | op_strength_reduction_case8:
      forall r1 r2,
      op_strength_reduction_cases (Oand) (r1 :: r2 :: nil)
  | op_strength_reduction_case9:
      forall r1 r2,
      op_strength_reduction_cases (Oor) (r1 :: r2 :: nil)
  | op_strength_reduction_case10:
      forall r1 r2,
      op_strength_reduction_cases (Oxor) (r1 :: r2 :: nil)
  | op_strength_reduction_case11:
      forall r1 r2,
      op_strength_reduction_cases (Oshl) (r1 :: r2 :: nil)
  | op_strength_reduction_case12:
      forall r1 r2,
      op_strength_reduction_cases (Oshr) (r1 :: r2 :: nil)
  | op_strength_reduction_case13:
      forall r1 r2,
      op_strength_reduction_cases (Oshru) (r1 :: r2 :: nil)
  | op_strength_reduction_case14:
      forall addr args,
      op_strength_reduction_cases (Olea addr) (args)
  | op_strength_reduction_case15:
      forall c args,
      op_strength_reduction_cases (Ocmp c) (args)
  | op_strength_reduction_default:
      forall (op: operation) (args: list reg),
      op_strength_reduction_cases op args.

Definition op_strength_reduction_match (op: operation) (args: list reg) :=
  match op as z1, args as z2 return op_strength_reduction_cases z1 z2 with
  | Osub, r1 :: r2 :: nil =>
      op_strength_reduction_case2 r1 r2
  | Omul, r1 :: r2 :: nil =>
      op_strength_reduction_case3 r1 r2
  | Odiv, r1 :: r2 :: nil =>
      op_strength_reduction_case4 r1 r2
  | Odivu, r1 :: r2 :: nil =>
      op_strength_reduction_case5 r1 r2
  | Omodu, r1 :: r2 :: nil =>
      op_strength_reduction_case7 r1 r2
  | Oand, r1 :: r2 :: nil =>
      op_strength_reduction_case8 r1 r2
  | Oor, r1 :: r2 :: nil =>
      op_strength_reduction_case9 r1 r2
  | Oxor, r1 :: r2 :: nil =>
      op_strength_reduction_case10 r1 r2
  | Oshl, r1 :: r2 :: nil =>
      op_strength_reduction_case11 r1 r2
  | Oshr, r1 :: r2 :: nil =>
      op_strength_reduction_case12 r1 r2
  | Oshru, r1 :: r2 :: nil =>
      op_strength_reduction_case13 r1 r2
  | Olea addr, args =>
      op_strength_reduction_case14 addr args
  | Ocmp c, args =>
      op_strength_reduction_case15 c args
  | op, args =>
      op_strength_reduction_default op args
  end.

(** We must be careful to preserve 2-address constraints over the
    RTL code, which means that commutative operations cannot
    be specialized if their first argument is a constant. *)

Definition op_strength_reduction (op: operation) (args: list reg) :=
  match op_strength_reduction_match op args with
  | op_strength_reduction_case2 r1 r2 =>
      (* Osub *)
      match intval r2 with
      | Some n => make_addimm (Int.neg n) r1
      | _ => (op, args)
      end
  | op_strength_reduction_case3 r1 r2 =>
      (* Omul *)
      match intval r2 with
      | Some n => make_mulimm n r1
      | _ => (op, args)
      end
  | op_strength_reduction_case4 r1 r2 =>
      (* Odiv *)
      match intval r2 with
      | Some n =>
          match Int.is_power2 n with
          | Some l => if Int.ltu l (Int.repr 31)
                      then (Oshrximm l, r1 :: nil)
                      else (op, args)
          | None   => (op, args)
          end
      | None =>
          (op, args)
      end
  | op_strength_reduction_case5 r1 r2 =>
      (* Odivu *)
      match intval r2 with
      | Some n =>
          match Int.is_power2 n with
          | Some l => make_shruimm l r1
          | None   => (op, args)
          end
      | None =>
          (op, args)
      end
  | op_strength_reduction_case7 r1 r2 =>
      (* Omodu *)
      match intval r2 with
      | Some n =>
          match Int.is_power2 n with
          | Some l => (Oandimm (Int.sub n Int.one), r1 :: nil)
          | None   => (op, args)
          end
      | None =>
          (op, args)
      end
  | op_strength_reduction_case8 r1 r2 =>
      (* Oand *)
      match intval r2 with
      | Some n => make_andimm n r1
      | _ => (op, args)
      end
  | op_strength_reduction_case9 r1 r2 =>
      (* Oor *)
      match intval r2 with
      | Some n => make_orimm n r1
      | _ => (op, args)
      end
  | op_strength_reduction_case10 r1 r2 =>
      (* Oxor *)
      match intval r2 with
      | Some n => make_xorimm n r1
      | _ => (op, args)
      end
  | op_strength_reduction_case11 r1 r2 =>
      (* Oshl *)
      match intval r2 with
      | Some n =>
          if Int.ltu n Int.iwordsize
          then make_shlimm n r1
          else (op, args)
      | _ => (op, args)
      end
  | op_strength_reduction_case12 r1 r2 =>
      (* Oshr *)
      match intval r2 with
      | Some n =>
          if Int.ltu n Int.iwordsize
          then make_shrimm n r1
          else (op, args)
      | _ => (op, args)
      end
  | op_strength_reduction_case13 r1 r2 =>
      (* Oshru *)
      match intval r2 with
      | Some n =>
          if Int.ltu n Int.iwordsize
          then make_shruimm n r1
          else (op, args)
      | _ => (op, args)
      end
  | op_strength_reduction_case14 addr args =>
      (* Olea *)
      let (addr', args') := addr_strength_reduction addr args in
      (Olea addr', args')
  | op_strength_reduction_case15 c args =>
      (* Ocmp *)
      let (c', args') := cond_strength_reduction c args in
      (Ocmp c', args')
  | op_strength_reduction_default op args =>
      (* default *)
      (op, args)
  end.

End STRENGTH_REDUCTION.