Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/omit-frameptr/amd64omit-frameptr.sml

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