Strength reduction for operators and conditions.
This is the machine-dependent part of Constprop.
.
.
.
.
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.
Original definition:
Nondetfunction cond_strength_reduction
(cond: condition) (args: list reg) (vl: list aval) :=
match cond, args, vl with
| Ccomp c, r1 :: r2 :: nil, I n1 :: v2 :: nil =>
(Ccompimm (swap_comparison c) n1, r2 :: nil)
| Ccomp c, r1 :: r2 :: nil, v1 :: I n2 :: nil =>
(Ccompimm c n2, r1 :: nil)
| Ccompu c, r1 :: r2 :: nil, I n1 :: v2 :: nil =>
(Ccompuimm (swap_comparison c) n1, r2 :: nil)
| Ccompu c, r1 :: r2 :: nil, v1 :: I n2 :: nil =>
(Ccompuimm c n2, r1 :: nil)
| Ccompl c, r1 :: r2 :: nil, L n1 :: v2 :: nil =>
(Ccomplimm (swap_comparison c) n1, r2 :: nil)
| Ccompl c, r1 :: r2 :: nil, v1 :: L n2 :: nil =>
(Ccomplimm c n2, r1 :: nil)
| Ccomplu c, r1 :: r2 :: nil, L n1 :: v2 :: nil =>
(Ccompluimm (swap_comparison c) n1, r2 :: nil)
| Ccomplu c, r1 :: r2 :: nil, v1 :: L n2 :: nil =>
(Ccompluimm c n2, r1 :: nil)
| _, _, _ =>
(cond, args)
end.
Original definition:
Nondetfunction make_cmp (c: condition) (args: list reg) (vl: list aval) :=
match c, args, vl with
| Ccompimm Ceq n, r1 :: nil, v1 :: nil =>
if Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1) then (Omove, r1 :: nil)
else if Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1) then (Oxorimm Int.one, r1 :: nil)
else make_cmp_base c args vl
| Ccompimm Cne n, r1 :: nil, v1 :: nil =>
if Int.eq_dec n Int.zero && vincl v1 (Uns Ptop 1) then (Omove, r1 :: nil)
else if Int.eq_dec n Int.one && vincl v1 (Uns Ptop 1) then (Oxorimm Int.one, r1 :: nil)
else make_cmp_base c args vl
| _, _, _ =>
make_cmp_base c args vl
end.
Original definition:
Nondetfunction addr_strength_reduction_32_generic
(addr: addressing) (args: list reg) (vl: list aval) :=
match addr, args, vl with
| Aindexed2 ofs, r1 :: r2 :: nil, I n1 :: v2 :: nil =>
(Aindexed (Int.signed n1 + ofs), r2 :: nil)
| Aindexed2 ofs, r1 :: r2 :: nil, v1 :: I n2 :: nil =>
(Aindexed (Int.signed n2 + ofs), r1 :: nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, v1 :: I n2 :: nil =>
(Aindexed (Int.signed n2 * sc + ofs), r1 :: nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, I n1 :: v2 :: nil =>
(Ascaled sc (Int.signed n1 + ofs), r2 :: nil)
| _, _ =>
(addr, args)
end.
Original definition:
Nondetfunction addr_strength_reduction_32
(addr: addressing) (args: list reg) (vl: list aval) :=
if Archi.ptr64 then addr_strength_reduction_32_generic addr args vl else
match addr, args, vl with
| Aindexed ofs, r1 :: nil, Ptr(Gl symb n) :: nil =>
(Aglobal symb (Ptrofs.add n (Ptrofs.repr ofs)), nil)
| Aindexed ofs, r1 :: nil, Ptr(Stk n) :: nil =>
(Ainstack (Ptrofs.add n (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, Ptr(Gl symb n1) :: I n2 :: nil =>
(Aglobal symb (Ptrofs.add (Ptrofs.add n1 (Ptrofs.of_int n2)) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, I n1 :: Ptr(Gl symb n2) :: nil =>
(Aglobal symb (Ptrofs.add (Ptrofs.add n2 (Ptrofs.of_int n1)) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, Ptr(Stk n1) :: I n2 :: nil =>
(Ainstack (Ptrofs.add (Ptrofs.add n1 (Ptrofs.of_int n2)) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, I n1 :: Ptr(Stk n2) :: nil =>
(Ainstack (Ptrofs.add (Ptrofs.add (Ptrofs.of_int n1) n2) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, Ptr(Gl symb n1) :: v2 :: nil =>
(Abased symb (Ptrofs.add n1 (Ptrofs.repr ofs)), r2 :: nil)
| Aindexed2 ofs, r1 :: r2 :: nil, v1 :: Ptr(Gl symb n2) :: nil =>
(Abased symb (Ptrofs.add n2 (Ptrofs.repr ofs)), r1 :: nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, Ptr(Gl symb n1) :: I n2 :: nil =>
(Aglobal symb (Ptrofs.add (Ptrofs.add n1 (Ptrofs.of_int (Int.mul n2 (Int.repr sc)))) (Ptrofs.repr ofs)), nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, Ptr(Gl symb n1) :: v2 :: nil =>
(Abasedscaled sc symb (Ptrofs.add n1 (Ptrofs.repr ofs)), r2 :: nil)
| Abased id ofs, r1 :: nil, I n1 :: nil =>
(Aglobal id (Ptrofs.add ofs (Ptrofs.of_int n1)), nil)
| Abasedscaled sc id ofs, r1 :: nil, I n1 :: nil =>
(Aglobal id (Ptrofs.add ofs (Ptrofs.of_int (Int.mul n1 (Int.repr sc)))), nil)
| _, _ =>
addr_strength_reduction_32_generic addr args vl
end.
Original definition:
Nondetfunction addr_strength_reduction_64_generic
(addr: addressing) (args: list reg) (vl: list aval) :=
match addr, args, vl with
| Aindexed2 ofs, r1 :: r2 :: nil, L n1 :: v2 :: nil =>
(Aindexed (Int64.signed n1 + ofs), r2 :: nil)
| Aindexed2 ofs, r1 :: r2 :: nil, v1 :: L n2 :: nil =>
(Aindexed (Int64.signed n2 + ofs), r1 :: nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, v1 :: L n2 :: nil =>
(Aindexed (Int64.signed n2 * sc + ofs), r1 :: nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, L n1 :: v2 :: nil =>
(Ascaled sc (Int64.signed n1 + ofs), r2 :: nil)
| _, _ =>
(addr, args)
end.
Original definition:
Nondetfunction addr_strength_reduction_64
(addr: addressing) (args: list reg) (vl: list aval) :=
if negb Archi.ptr64 then addr_strength_reduction_64_generic addr args vl else
match addr, args, vl with
| Aindexed ofs, r1 :: nil, Ptr(Gl symb n) :: nil =>
(Aglobal symb (Ptrofs.add n (Ptrofs.repr ofs)), nil)
| Aindexed ofs, r1 :: nil, Ptr(Stk n) :: nil =>
(Ainstack (Ptrofs.add n (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, Ptr(Gl symb n1) :: L n2 :: nil =>
(Aglobal symb (Ptrofs.add (Ptrofs.add n1 (Ptrofs.of_int64 n2)) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, L n1 :: Ptr(Gl symb n2) :: nil =>
(Aglobal symb (Ptrofs.add (Ptrofs.add n2 (Ptrofs.of_int64 n1)) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, Ptr(Stk n1) :: L n2 :: nil =>
(Ainstack (Ptrofs.add (Ptrofs.add n1 (Ptrofs.of_int64 n2)) (Ptrofs.repr ofs)), nil)
| Aindexed2 ofs, r1 :: r2 :: nil, L n1 :: Ptr(Stk n2) :: nil =>
(Ainstack (Ptrofs.add (Ptrofs.add (Ptrofs.of_int64 n1) n2) (Ptrofs.repr ofs)), nil)
| Aindexed2scaled sc ofs, r1 :: r2 :: nil, Ptr(Gl symb n1) :: L n2 :: nil =>
(Aglobal symb (Ptrofs.add (Ptrofs.add n1 (Ptrofs.of_int64 (Int64.mul n2 (Int64.repr sc)))) (Ptrofs.repr ofs)), nil)
| _, _ =>
addr_strength_reduction_64_generic addr args vl
end.
).
).
).
Original definition:
Nondetfunction op_strength_reduction
(op: operation) (args: list reg) (vl: list aval) :=
match op, args, vl with
| Ocast8signed, r1 :: nil, v1 :: nil => make_cast8signed r1 v1
| Ocast8unsigned, r1 :: nil, v1 :: nil => make_cast8unsigned r1 v1
| Ocast16signed, r1 :: nil, v1 :: nil => make_cast16signed r1 v1
| Ocast16unsigned, r1 :: nil, v1 :: nil => make_cast16unsigned r1 v1
| Osub, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_addimm (Int.neg n2) r1
| Omul, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_mulimm n1 r2
| Omul, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_mulimm n2 r1
| Odiv, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_divimm n2 r1 r2
| Odivu, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_divuimm n2 r1 r2
| Omodu, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_moduimm n2 r1 r2
| Oand, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_andimm n1 r2 v2
| Oand, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_andimm n2 r1 v1
| Oandimm n, r1 :: nil, v1 :: nil => make_andimm n r1 v1
| Oor, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_orimm n1 r2
| Oor, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_orimm n2 r1
| Oxor, r1 :: r2 :: nil, I n1 :: v2 :: nil => make_xorimm n1 r2
| Oxor, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_xorimm n2 r1
| Oshl, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shlimm n2 r1 r2
| Oshr, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shrimm n2 r1 r2
| Oshru, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shruimm n2 r1 r2
| Olea addr, args, vl =>
let (addr', args') := addr_strength_reduction_32 addr args vl in
(Olea addr', args')
| Osubl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_addlimm (Int64.neg n2) r1
| Omull, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_mullimm n1 r2
| Omull, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_mullimm n2 r1
| Odivl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_divlimm n2 r1 r2
| Odivlu, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_divluimm n2 r1 r2
| Omodlu, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_modluimm n2 r1 r2
| Oandl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_andlimm n1 r2 v2
| Oandl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_andlimm n2 r1 v1
| Oandlimm n, r1 :: nil, v1 :: nil => make_andlimm n r1 v1
| Oorl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_orlimm n1 r2
| Oorl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_orlimm n2 r1
| Oxorl, r1 :: r2 :: nil, L n1 :: v2 :: nil => make_xorlimm n1 r2
| Oxorl, r1 :: r2 :: nil, v1 :: L n2 :: nil => make_xorlimm n2 r1
| Oshll, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shllimm n2 r1 r2
| Oshrl, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shrlimm n2 r1 r2
| Oshrlu, r1 :: r2 :: nil, v1 :: I n2 :: nil => make_shrluimm n2 r1 r2
| Oleal addr, args, vl =>
let (addr', args') := addr_strength_reduction_64 addr args vl in
(Oleal addr', args')
| Ocmp c, args, vl => make_cmp c args vl
| Omulf, r1 :: r2 :: nil, v1 :: F n2 :: nil => make_mulfimm n2 r1 r1 r2
| Omulf, r1 :: r2 :: nil, F n1 :: v2 :: nil => make_mulfimm n1 r2 r1 r2
| Omulfs, r1 :: r2 :: nil, v1 :: FS n2 :: nil => make_mulfsimm n2 r1 r1 r2
| Omulfs, r1 :: r2 :: nil, FS n1 :: v2 :: nil => make_mulfsimm n1 r2 r1 r2
| _, _, _ => (op, args)
end.