Debian Codegraph @

lib/mlrisc-lib/MLRISC/SSA/ssa-untrap.sml

raw
1 (*
2 * This module removes unnecessary overflow trapping arithmetic instructions.
3 *
4 * There are two types of trapping arithmetic instructions generated in all the
5 * architectures. The first type of architectures have arithmetic instructions
6 * that traps for overflow in one single instruction (PA-RISC, Alpha).
7 * Other architectures have instructions that sets an overflow flag and
8 * require explicit tests (Sparc, x86, PowerPC).
9 *
10 * -- Allen ([email protected])
11 *)
12 functor SSAUntrap(SSAInstrGen : SSA_INSTRGEN) : SSA_OPTIMIZATION =
13 struct
14
15 structure Gen = SSAInstrGen
16 structure SSA = SSAInstrGen.SSA
17 structure CFG = SSA.CFG
18 structure RTL = SSA.RTL
19 structure T = RTL.T
20 structure T' = SSA.MLTreeComp.T
21 structure G = Graph
22 structure A = Array
23
24 type flowgraph = SSA.ssa
25
26 val name = "Remove Trapping Arithmetic"
27
28 val untrapped = MLRiscControl.getCounter "ssa-untrapped"
29
30 datatype interval = BOT | TOP | RANGE of int * int
31
32 fun meet(BOT,x) = x
33 | meet(x,BOT) = x
34 | meet(TOP,x) = TOP
35 | meet(x,TOP) = TOP
36 | meet(RANGE(a,b), RANGE(c,d)) = RANGE(Int.min(a,c),Int.max(b,c))
37
38 fun meets [] = BOT
39 | meets (a::b) = meet(a,meets b)
40
41 fun run(SSA as G.GRAPH ssa) =
42 let val Dom as G.GRAPH dom = SSA.dom SSA
43 val rtlTbl = SSA.rtlTbl SSA
44 val defsTbl = SSA.defsTbl SSA
45 val usesTbl = SSA.usesTbl SSA
46 val ssaOpTbl= SSA.ssaOpTbl SSA
47 val showOp = SSA.showOp SSA
48 val showVal = SSA.showVal SSA
49 val {ops, ...} = SSA.nodes SSA
50
51 fun untrap(i, rtl, defs, uses) =
52 let fun isConst x = List.nth(uses,x) < 0
53 in case rtl of
54 T.RTL{e,...} => untrap(i, e, defs, uses)
55 | T.MV(ty, z, T.ADDT(_,T.REG(_,x), T.REG(_,y))) =>
56 if isConst x orelse isConst y then
57 let val t = T.MV(ty, z, T.ADD(ty, T.REG(ty,x), T.REG(ty,y)))
58 val mltree =
59 Gen.translate SSA {defs=defs, uses=uses, rtl=t}
60 in Gen.replace SSA {id=i, mltree=mltree}
61 end
62 else false
63 | T.MV(ty, z, T.SUBT(_,T.REG(_,x), T.REG(_,y))) =>
64 if isConst x orelse isConst y then
65 let val t = T.MV(ty, z, T.SUB(ty, T.REG(ty,x), T.REG(ty,y)))
66 val mltree =
67 Gen.translate SSA {defs=defs, uses=uses, rtl=t}
68 in Gen.replace SSA {id=i, mltree=mltree}
69 end
70 else false
71 | _ => false
72 end
73
74 fun process i =
75 let val rtl = A.sub(rtlTbl, i)
76 val uses = A.sub(usesTbl, i)
77 val defs = A.sub(defsTbl, i)
78 in if untrap(i, rtl, defs, uses) then
79 (untrapped := !untrapped + 1;
80 print("TRAP "^showOp i^"\n")
81 )
82 else ()
83 end
84
85 fun walk X =
86 let val ops = A.sub(ops, X)
87 fun scan [] = ()
88 | scan(i::ops) =
89 let val rtl = A.sub(rtlTbl, i)
90 in scan ops
91 end
92 in scan ops;
93 app walk (#succ dom X)
94 end
95
96 in SSA.forallNodes SSA process;
97 walk (hd (#entries dom ()));
98 SSA
99 end
100
101 end