2
* Copyright (c) 2001 Stefan Kral
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
open K7RegisterAllocationBasics
27
module HandleOnDemandInstructions : sig
28
val addOndemandInstructions :
29
viregfileentry VIntRegMap.t -> k7vinstr list ->
30
viregfileentry VIntRegMap.t * K7Basics.k7vinstr list
35
let fetchInstrsM = fetchStateM >>= fun x -> unitM (fst x)
36
let fetchVIRegFileM = fetchStateM >>= fun x -> unitM (snd x)
38
let storeInstrsM x = fetchStateM >>= fun s -> storeStateM (repl1of2 x s)
39
let storeVIRegFileM x = fetchStateM >>= fun s -> storeStateM (repl2of2 x s)
41
let addInstrM x = fetchInstrsM >>= consM x >>= storeInstrsM
43
let rec addOndemandInstrs1M instr =
44
iterM addOndemandInstrs2M (snd (k7vinstrToSrcvregs instr)) >>
47
and addOndemandInstrs2M vireg =
48
fetchVIRegFileM >>= fun viregfile ->
49
match vintregmap_find vireg viregfile with
50
| Some (IOnDemand zs) ->
51
storeVIRegFileM (VIntRegMap.add vireg IFresh viregfile) >>
52
iterM addOndemandInstrs1M zs
56
let rec addOndemandInstrsM instrs =
57
iterM addOndemandInstrs1M instrs >>
58
fetchVIRegFileM >>= fun viregfile' ->
59
fetchInstrsM >>= fun instrs' ->
60
unitM (viregfile',List.rev instrs')
62
let addOndemandInstructions viregfile instrs =
63
StateMonad.runM addOndemandInstrsM instrs ([],viregfile)
66
open HandleOnDemandInstructions
68
(****************************************************************************)
70
let processInitcode viregfile = function
71
| AddIntOnDemandCode(reg,xs) -> VIntRegMap.add reg (IOnDemand xs) viregfile
74
let makeInitialMap rregfile = function
75
| FixRegister(vreg, rreg) -> (rreg, IFixed vreg)::(remove_assoc rreg rregfile)
78
let addRegRefForInstr (n0,vsrefs0,virefs0) instr =
79
let (_,_,(vsregs,viregs)) = k7vinstrToVregs instr in
80
let vsrefs = fold_right (fun reg -> vsimdregmap_addE reg n0) vsregs vsrefs0
81
and virefs = fold_right (fun reg -> vintregmap_addE reg n0) viregs virefs0 in
82
(succ n0,vsrefs,virefs)
84
let getRegRefsForInstrs instrs =
85
let s0 = (0,VSimdRegMap.empty,VIntRegMap.empty) in
86
let (_,vsrefs0,virefs0) = fold_left addRegRefForInstr s0 instrs in
87
(VSimdRegMap.map rev vsrefs0, VIntRegMap.map rev virefs0)
89
let addRegsForInstr i = (@.)(k7vinstrToDstvregs i @. k7vinstrToSrcvregs i)
91
let prepareRegAlloc initcode instrs0 =
92
let viregfile0 = fold_left processInitcode VIntRegMap.empty initcode
93
and vsregfile0 = VSimdRegMap.empty in
94
let (viregfile1, instrs) = addOndemandInstructions viregfile0 instrs0 in
95
let (vsregs,viregs) = fold_right addRegsForInstr instrs ([],[]) in
96
let rimap0 = map (fun reg -> (reg,IFree)) k7rintregs in
97
let rimap1 = fold_left makeInitialMap rimap0 initcode in
98
(((map (fun reg -> (reg,SFree)) k7rmmxregs), rimap1),
99
(fold_right (fun reg -> VSimdRegMap.add reg SFresh) vsregs vsregfile0,
100
fold_right (fun reg -> VIntRegMap.add reg IFresh) viregs viregfile1),
101
getRegRefsForInstrs instrs,