Debian Codegraph @

lib/mlrisc-lib/MLRISC/x86/omit-frameptr/x86omit-frameptr.sml

raw
1 (* x86omit-frameptr.sml
2 *
3 * COPYRIGHT (c) 2019 The Fellowship of SML/NJ (http://www.smlnj.org)
4 * All rights reserved.
5 *
6 * replaces uses and definitions of a virtual frame pointer (vfp) with the appropriate
7 * operation on the stack pointer.
8 *
9 * Invariant: fp = sp + delta && stack grows from high to low && fp >= sp
10 *
11 * Assumptions: At the entry node fp = sp + idelta
12 *
13 * The tricky business is to recognize that things that look like register may
14 * really be memory registers.
15 *)
16
17 functor X86OmitFramePointer (
18 structure I : X86INSTR
19 structure CFG : CONTROL_FLOW_GRAPH (* where I = I *)
20 where type I.addressing_mode = I.addressing_mode
21 and type I.ea = I.ea
22 and type I.instr = I.instr
23 and type I.instruction = I.instruction
24 and type I.operand = I.operand
25 structure MemRegs : MEMORY_REGISTERS (* where I = I *)
26 where type I.Constant.const = I.Constant.const
27 and type I.Region.region = I.Region.region
28 and type I.T.Basis.cond = I.T.Basis.cond
29 and type I.T.Basis.div_rounding_mode = I.T.Basis.div_rounding_mode
30 and type I.T.Basis.ext = I.T.Basis.ext
31 and type I.T.Basis.fcond = I.T.Basis.fcond
32 and type I.T.Basis.rounding_mode = I.T.Basis.rounding_mode
33 and type ('s,'r,'f,'c) I.T.Extension.ccx = ('s,'r,'f,'c) I.T.Extension.ccx
34 and type ('s,'r,'f,'c) I.T.Extension.fx = ('s,'r,'f,'c) I.T.Extension.fx
35 and type ('s,'r,'f,'c) I.T.Extension.rx = ('s,'r,'f,'c) I.T.Extension.rx
36 and type ('s,'r,'f,'c) I.T.Extension.sx = ('s,'r,'f,'c) I.T.Extension.sx
37 and type I.T.I.div_rounding_mode = I.T.I.div_rounding_mode
38 and type I.T.ccexp = I.T.ccexp
39 and type I.T.fexp = I.T.fexp
40 (* and type I.T.labexp = I.T.labexp *)
41 and type I.T.mlrisc = I.T.mlrisc
42 and type I.T.oper = I.T.oper
43 and type I.T.rep = I.T.rep
44 and type I.T.rexp = I.T.rexp
45 and type I.T.stm = I.T.stm
46 (* and type I.addressing_mode = I.addressing_mode *)
47 and type I.binaryOp = I.binaryOp
48 and type I.bitOp = I.bitOp
49 and type I.cond = I.cond
50 and type I.fbinOp = I.fbinOp
51 and type I.fenvOp = I.fenvOp
52 and type I.fibinOp = I.fibinOp
53 and type I.fsize = I.fsize
54 and type I.funOp = I.funOp
55 and type I.instr = I.instr
56 and type I.instruction = I.instruction
57 and type I.isize = I.isize
58 and type I.move = I.move
59 and type I.multDivOp = I.multDivOp
60 and type I.operand = I.operand
61 and type I.shiftOp = I.shiftOp
62 and type I.unaryOp = I.unaryOp
63 val memRegBase : CellsBasis.cell option): OMIT_FRAME_POINTER =
64 struct
65 structure CFG = CFG
66 structure I = I
67 structure C = I.C
68 structure CB = CellsBasis
69 structure HT = IntHashTable
70 val sp = C.esp
71
72 val dumpCfg = MLRiscControl.mkFlag ("dump-cfg-after-omit-frame-pointer",
73 "whether CFG is shown after omit-framepointer phase")
74
75 fun error msg = MLRiscErrorMsg.error("X86OmitFramePointer", msg)
76
77 fun omitframeptr{vfp:CB.cell, idelta:Int32.int option, cfg as Graph.GRAPH graph} = let
78
79 (* rewrite a list of instructions where the gap between fp and sp is delta *)
80 fun rewrite(instrs, idelta) = let
81
82 (* What kind of register? *)
83 datatype which = SP | FP | OTHER
84 fun isSp cell = CB.sameColor(cell, sp)
85 fun isVfp cell = CB.sameColor(cell, vfp)
86 fun which(cell) = if isSp(cell) then SP else if isVfp(cell) then FP else OTHER
87 fun either(cell) = isSp(cell) orelse isVfp(cell)
88
89
90 (* Has the instruction been rewritten? *)
91 val changedFlag = ref false
92
93
94 (*
95 * rewrite a single instruction assuming gap (fp=sp+delta)
96 * returns NONE is instruction is deleted and SOME(instruction) otherwise.
97 *)
98
99 fun doInstr(instr, delta:Int32.int option) = let
100 (* if a delta exists then add to it,
101 * otherwise maintain that there is no delta
102 *)
103 fun addToDelta i =
104 (case delta
105 of SOME d => SOME(i+d)
106 | NONE => NONE
107 (*esac*))
108
109 fun incOffset(i) =
110 (case delta
111 of NONE => error "incOffset"
112 | SOME k => k+i
113 (*esac*))
114
115 fun incDisp(I.Immed i) = I.Immed(incOffset(i))
116 | incDisp _ = error "incDisp" (* CONSTANTS? *)
117
118 fun operand(opnd as I.Displace{base, disp, mem}) =
119 if isVfp base then
120 (changedFlag := true;
121 I.Displace{base=sp, mem=mem, disp=incDisp(disp)})
122 else opnd
123 | operand(opnd as I.Indexed{base, index, scale, disp, mem}) =
124 if isVfp index then
125 error "operand: frame pointer used in index"
126 else (case base
127 of NONE => opnd
128 | SOME b =>
129 if isVfp b then
130 (changedFlag := true;
131 I.Indexed{base=SOME(sp), index=index, scale=scale, mem=mem,
132 disp=incDisp(disp)})
133 else opnd
134 (*esac*))
135 | operand(opnd as I.MemReg _) =
136 operand(MemRegs.memReg{reg=opnd, base=Option.valOf memRegBase})
137 | operand(opnd as I.FDirect _) =
138 operand(MemRegs.memReg{reg=opnd, base=Option.valOf memRegBase})
139 | operand(opnd) = opnd
140
141
142 fun annotate(i, k:Int32.int option) = let
143 val instr =
144 if !changedFlag then
145 (changedFlag := false;
146 case k
147 of NONE => i
148 | SOME d =>
149 if d <> 0 then let
150 val cmt = "offset adjusted to " ^ Int32.toString d
151 val ann = #create MLRiscAnnotations.COMMENT cmt
152 in I.ANNOTATION{i=i, a=ann}
153 end
154 else i
155 (*esac*))
156 else i
157 in (SOME(instr),k)
158 end
159
160 fun unchanged(i:I.instr) = annotate(I.INSTR i, delta)
161 fun changedto(i, k) = annotate(I.INSTR i, k)
162
163 fun compare(test, lsrc, rsrc) = unchanged(test{lsrc=operand(lsrc), rsrc=operand(rsrc)})
164 fun float(oper, opnd) = unchanged(oper(operand(opnd)))
165
166 fun doX86Instr (instr: I.instr) =
167 (case instr
168 of I.JMP(opnd,labs) => unchanged(I.JMP(operand opnd, labs))
169 | I.JCC{cond:I.cond, opnd:I.operand} =>
170 unchanged(I.JCC{cond=cond, opnd=operand(opnd)})
171 | I.CALL{opnd, defs, uses, cutsTo, mem, return, pops=0} =>
172 unchanged(I.CALL{opnd=operand(opnd), defs=defs, uses=uses,
173 cutsTo=cutsTo, mem=mem, pops=0,
174 return=return})
175 | I.CALL{opnd, defs, uses, cutsTo, mem, return, pops} =>
176 changedto(I.CALL{opnd=operand(opnd), defs=defs, uses=uses,
177 cutsTo=cutsTo, mem=mem, pops=pops,
178 return=return},
179 addToDelta(~pops))
180 | I.ENTER{src1=I.Immed i1, src2=I.Immed i2} => changedto(instr, addToDelta(i1 + i2*4))
181 | I.LEAVE => (SOME(I.INSTR instr), NONE)
182 | I.RET opnd => (SOME(I.INSTR instr), NONE)
183 | I.MOVE{mvOp:I.move, src=I.Direct s, dst=I.Direct d} =>
184 (case (which d, which s)
185 of (FP, SP) => (NONE, SOME 0)
186 | (SP, FP) => (case delta
187 of NONE => error "MOVE: (SP, FP)"
188 | SOME 0 => (NONE, SOME 0)
189 | SOME n => let
190 val addr = I.Displace{base=sp, disp=I.Immed(n), mem=I.Region.stack}
191 in
192 (SOME(I.lea{r32=sp, addr=addr}), SOME 0)
193 end
194 (*esac*))
195 | (OTHER, OTHER) => unchanged(instr)
196 | (FP, FP) => (NONE, delta)
197 | (SP, SP) => (NONE, delta)
198 | (FP, _) => error "MOVE: to FP"
199 | (SP, _) => error "MOVE: to SP"
200 | (OTHER, SP) => unchanged(instr)
201 | (OTHER, FP) => error "MOVE: FP to OTHER" (* d:=sp+delta; lazy!*)
202 (*esac*))
203 | I.MOVE{mvOp, src, dst as I.Direct d} =>
204 if either(d) then error "MOVE: assignment to FP/SP"
205 else unchanged(I.MOVE{mvOp=mvOp, src=operand(src), dst=dst})
206 | I.MOVE{mvOp, src, dst} =>
207 unchanged(I.MOVE{mvOp=mvOp, src=operand(src), dst=operand(dst)})
208 | I.LEA{r32:CB.cell, addr as I.Displace{base, disp=I.Immed d, mem}} =>
209 (case (which r32, which base)
210 of (SP, SP) =>
211 (* assumes stack grows from high to low.
212 * if sp is incremented by a positive delta, then the gap is
213 * reduced by delta-d;
214 * if sp is decremented, the the gap is increased and d is negative.
215 *)
216 changedto(instr, addToDelta(~d))
217 | (SP, FP) =>
218 (* sp = fp + d
219 * or sp = sp + delta + d
220 *)
221 changedto(I.LEA{r32=r32, addr=operand(addr)}, SOME(incOffset(d)))
222 | (FP, FP) =>
223 (* fp = fp + d
224 * if d is positive, then the gap is increased to delta+d,
225 * if d is negative, then the gap is reduced.
226 *)
227 (NONE, SOME(incOffset(d)))
228 | (FP, SP) => (NONE, addToDelta(d))
229 | (SP, OTHER) => error "LEA: sp changed by non-immed"
230 | (FP, OTHER) => error "LEA: fp changed by non-immed"
231 | _ => unchanged(instr)
232 (*esac*))
233 | I.LEA{r32, addr} =>
234 if either(r32) then error "LEA: SP/FP changed by non-immed"
235 else unchanged(I.LEA{r32=r32, addr=operand(addr)})
236 | I.CMPL{lsrc: I.operand, rsrc: I.operand} => compare(I.CMPL, lsrc, rsrc)
237 | I.CMPW{lsrc: I.operand, rsrc: I.operand} => compare(I.CMPW, lsrc, rsrc)
238 | I.CMPB{lsrc: I.operand, rsrc: I.operand} => compare(I.CMPB, lsrc, rsrc)
239 | I.TESTL{lsrc: I.operand, rsrc: I.operand} => compare(I.TESTL, lsrc, rsrc)
240 | I.TESTW{lsrc: I.operand, rsrc: I.operand} => compare(I.TESTW, lsrc, rsrc)
241 | I.TESTB{lsrc: I.operand, rsrc: I.operand} => compare(I.TESTB, lsrc, rsrc)
242 | I.BITOP{bitOp:I.bitOp, lsrc: I.operand, rsrc: I.operand} =>
243 unchanged(I.BITOP{bitOp=bitOp, lsrc=operand(lsrc), rsrc=operand(rsrc)})
244 | I.BINARY{binOp=I.ADDL, src=I.Immed(k), dst=I.Direct(d)} =>
245 (case which d
246 of SP => changedto(instr, addToDelta(~k))
247 | FP => (NONE, SOME(incOffset(k)))
248 | OTHER => unchanged(instr)
249 (*esac*))
250 | I.BINARY{binOp=I.SUBL, src=I.Immed(k), dst=I.Direct(d)} =>
251 (case which d
252 of SP => changedto(instr, addToDelta(k))
253 | FP => (NONE, SOME(incOffset(~k)))
254 | OTHER => unchanged(instr)
255 (*esac*))
256 | I.BINARY{binOp, dst as I.Direct(d), src} =>
257 if either(d) then error "binary: assignment to SP | FP"
258 else unchanged(I.BINARY{binOp=binOp, src=operand(src), dst=dst})
259 | I.BINARY{binOp, src, dst} =>
260 unchanged(I.BINARY{binOp=binOp, src=operand(src), dst=operand(dst)})
261 | I.CMPXCHG{lock:bool, sz:I.isize, src:I.operand, dst:I.operand} =>
262 unchanged(I.CMPXCHG{lock=lock, sz=sz, src=operand(src), dst=operand(dst)})
263 | I.MULTDIV{multDivOp:I.multDivOp, src:I.operand} =>
264 unchanged(I.MULTDIV{multDivOp=multDivOp, src=operand(src)})
265 | I.MUL3{dst:CB.cell, src2:Int32.int, src1:I.operand} =>
266 if either(dst) then error "MUL3: assignment to FP/SP"
267 else unchanged(I.MUL3{dst=dst, src2=src2, src1=operand(src1)})
268 | I.UNARY{unOp=I.INCL, opnd as I.Direct(r)} =>
269 (case (which r)
270 of SP => changedto(instr, addToDelta(~1))
271 | FP => (NONE, SOME(incOffset(1)))
272 | OTHER => unchanged(I.UNARY{unOp=I.INCL, opnd=opnd})
273 (*esac*))
274 | I.UNARY{unOp=I.DECL, opnd as I.Direct(r)} =>
275 (case (which r)
276 of SP => changedto(instr, addToDelta(1))
277 | FP => (NONE, SOME(incOffset(~1)))
278 | OTHER => unchanged(I.UNARY{unOp=I.DECL, opnd=opnd})
279 (*esac*))
280 | I.UNARY{unOp, opnd} => unchanged(I.UNARY{unOp=unOp, opnd=operand(opnd)})
281 | I.SET{cond:I.cond, opnd:I.operand} =>
282 unchanged(I.SET{cond=cond, opnd=operand(opnd)})
283 | I.CMOV{cond:I.cond, src as I.Direct(s), dst:CB.cell} =>
284 if either(s) orelse either(dst) then
285 error "CMOV: FP/SP in conditional move"
286 else unchanged(I.CMOV{cond=cond, src=operand(src), dst=dst})
287 | I.PUSHL opnd => changedto(I.PUSHL(operand(opnd)), addToDelta(4))
288 | I.PUSHW opnd => changedto(I.PUSHW(operand(opnd)), addToDelta(2))
289 | I.PUSHB opnd => changedto(I.PUSHB(operand(opnd)), addToDelta(1))
290 | I.POP opnd => changedto(I.POP(operand(opnd)), addToDelta(~4))
291 | I.FBINARY{binOp:I.fbinOp, src:I.operand, dst:I.operand} =>
292 unchanged(I.FBINARY{binOp=binOp, src=operand(src), dst=operand(dst)})
293 | I.FIBINARY{binOp:I.fibinOp, src:I.operand} =>
294 unchanged(I.FIBINARY{binOp=binOp, src=operand(src)})
295 | I.FUCOM opnd => unchanged(I.FUCOM(operand opnd))
296 | I.FUCOMP opnd => unchanged(I.FUCOMP(operand (opnd)))
297 | I.FCOMI opnd => unchanged(I.FCOMI(operand opnd))
298 | I.FCOMIP opnd => unchanged(I.FCOMIP(operand (opnd)))
299 | I.FUCOMI opnd => unchanged(I.FUCOMI(operand opnd))
300 | I.FUCOMIP opnd => unchanged(I.FUCOMIP(operand (opnd)))
301 | I.FSTPL opnd => float(I.FSTPL, opnd)
302 | I.FSTPS opnd => float(I.FSTPS, opnd)
303 | I.FSTPT opnd => float(I.FSTPT, opnd)
304 | I.FSTL opnd => float(I.FSTL, opnd)
305 | I.FSTS opnd => float(I.FSTS, opnd)
306 | I.FLDL opnd => float(I.FLDL, opnd)
307 | I.FLDS opnd => float(I.FLDS, opnd)
308 | I.FLDT opnd => float(I.FLDT, opnd)
309 | I.FILD opnd => float(I.FILD, opnd)
310 | I.FILDL opnd => float(I.FILDLL, opnd)
311 | I.FILDLL opnd => float(I.FILDLL, opnd)
312 | I.FENV{fenvOp:I.fenvOp, opnd:I.operand} =>
313 unchanged(I.FENV{fenvOp=fenvOp, opnd=operand(opnd)})
314 | I.FMOVE{fsize:I.fsize, src:I.operand, dst:I.operand} =>
315 unchanged(I.FMOVE{fsize=fsize, src=operand(src), dst=operand(dst)})
316 | I.FILOAD{isize:I.isize, ea:I.operand, dst:I.operand} =>
317 unchanged(I.FILOAD{isize=isize, ea=operand(ea), dst=operand(dst)})
318 | I.FBINOP{fsize, binOp, lsrc, rsrc, dst} =>
319 unchanged(I.FBINOP{fsize=fsize, binOp=binOp, lsrc=operand(lsrc),
320 rsrc=operand(rsrc), dst=operand(dst)})
321 | I.FIBINOP{isize, binOp, lsrc, rsrc, dst} =>
322 unchanged(I.FIBINOP{isize=isize, binOp=binOp, lsrc=operand(lsrc),
323 rsrc=operand(rsrc), dst=operand(dst)})
324 | I.FUNOP{fsize:I.fsize, unOp:I.funOp, src:I.operand, dst:I.operand} =>
325 unchanged(I.FUNOP{fsize=fsize, unOp=unOp, src=operand(src),
326 dst=operand(dst)})
327 | I.FCMP{i,fsize:I.fsize, lsrc:I.operand, rsrc:I.operand} =>
328 unchanged(I.FCMP{i=i,fsize=fsize, lsrc=operand(lsrc), rsrc=operand(rsrc)})
329 | _ => unchanged(instr)
330 (*esac*))
331 in
332 case instr
333 of I.ANNOTATION{i,a} => let
334 val (instr, delta) = doInstr(i, delta)
335 in
336 case instr
337 of NONE => (NONE, delta)
338 | SOME(i) => annotate(I.ANNOTATION{i=i, a=a}, delta)
339 end
340 | I.COPY{k=CB.GP, dst, src, ...} => let
341 (* the situation where SP <- FP is somewhat complicated.
342 * The copy must be extracted, and a lea generated.
343 * Should it be before or after the parallel copy? Depends on if SP is used.
344 * However, will such a thing ever exist in a parallel copy!?
345 *)
346 fun okay(s, d, acc) =
347 (case (which s, which d)
348 of (FP, SP) => true
349 | (SP, FP) => error "COPY:SP<-FP; lazy!"
350 | (SP, OTHER) => error "COPY:SP<-OTHER"
351 | (FP, OTHER) => error "COPY:FP<-OTHER"
352 | (OTHER, SP) => error "COPY:OTHER<-SP"
353 | (OTHER, FP) => error "COPY:OTHER<-FP"
354 | _ => acc
355 (*esac*))
356 in annotate(instr, if ListPair.foldl okay false (dst, src) then SOME 0 else delta)
357 end
358 | I.INSTR instr => doX86Instr instr
359 | _ => annotate(instr, delta) (* unchanged *)
360 end (*doInstr*)
361
362 (* rewrite instructions *)
363 fun doInstrs([], instrs, delta) = (instrs, delta)
364 | doInstrs(instr::rest, acc, delta) = let
365 val (instr, delta2) = doInstr(instr, delta)
366 in
367 case instr
368 of NONE => doInstrs(rest, acc, delta2)
369 | SOME(i) => doInstrs(rest, i::acc, delta2)
370 end
371
372
373 in doInstrs(instrs, [], idelta)
374 end (* rewrite *)
375
376
377
378
379 (* rewrite blocks using a depth first traversal of the blocks *)
380 val info : {visited:bool, delta: Int32.int option} HT.hash_table =
381 HT.mkTable(32, General.Fail "X86OmitFramePtr: Not Found")
382 val noInfo = {visited=false, delta=NONE}
383
384 fun dfs (nid, delta) = let
385 fun doSucc(delta) =
386 app (fn snid => dfs(snid, delta)) (#succ graph nid)
387 val CFG.BLOCK{insns, kind, ...} = #node_info graph nid
388 in
389 case kind
390 of CFG.STOP => ()
391 | CFG.START => doSucc(delta)
392 | CFG.NORMAL => let
393 val {visited, delta=d} = Option.getOpt(HT.find info nid, noInfo)
394 fun sameDelta(NONE, NONE) = true
395 | sameDelta(SOME i1: Int32.int option, SOME i2) = i1 = i2
396 | sameDelta _ = false
397 in
398 if visited then (if sameDelta(d, delta) then () else error "dfs")
399 else let
400 val (instrs, delta2) = rewrite(rev(!insns), delta)
401 in
402 insns := instrs;
403 HT.insert info (nid, {visited=true, delta=delta});
404 doSucc(delta2)
405 end
406 end
407 (*esac*)
408 end
409
410 val CB.CELL{col, ...} = vfp
411 in
412 (*
413 * check that virtual frame pointer is a pseudo register or
414 * aliased to the stack pointer.
415 *)
416 case !col
417 of CB.PSEUDO => app (fn nid => dfs(nid, idelta)) (#entries graph ())
418 | _ => error "virtual frame pointer not a pseudo register"
419 (*esac*)
420
421 (* output cluster *)
422 (* if !dumpCfg then
423 PC.printCluster TextIO.stdOut "after omit frame pointer" cl
424 else () *)
425 end
426 end
427