Iteration on dterei's metadata design
[ghc.git] / compiler / llvmGen / LlvmCodeGen / Regs.hs
1 --------------------------------------------------------------------------------
2 -- | Deal with Cmm registers
3 --
4
5 module LlvmCodeGen.Regs (
6 lmGlobalRegArg, lmGlobalRegVar, alwaysLive,
7 stgTBAA, top, base, stack, heap, rx, other, tbaa, getTBAA
8 ) where
9
10 #include "HsVersions.h"
11
12 import Llvm
13
14 import CmmExpr
15 import DynFlags
16 import FastString
17 import Outputable ( panic )
18
19 -- | Get the LlvmVar function variable storing the real register
20 lmGlobalRegVar :: DynFlags -> GlobalReg -> LlvmVar
21 lmGlobalRegVar dflags = pVarLift . lmGlobalReg dflags "_Var"
22
23 -- | Get the LlvmVar function argument storing the real register
24 lmGlobalRegArg :: DynFlags -> GlobalReg -> LlvmVar
25 lmGlobalRegArg dflags = lmGlobalReg dflags "_Arg"
26
27 {- Need to make sure the names here can't conflict with the unique generated
28 names. Uniques generated names containing only base62 chars. So using say
29 the '_' char guarantees this.
30 -}
31 lmGlobalReg :: DynFlags -> String -> GlobalReg -> LlvmVar
32 lmGlobalReg dflags suf reg
33 = case reg of
34 BaseReg -> ptrGlobal $ "Base" ++ suf
35 Sp -> ptrGlobal $ "Sp" ++ suf
36 Hp -> ptrGlobal $ "Hp" ++ suf
37 VanillaReg 1 _ -> wordGlobal $ "R1" ++ suf
38 VanillaReg 2 _ -> wordGlobal $ "R2" ++ suf
39 VanillaReg 3 _ -> wordGlobal $ "R3" ++ suf
40 VanillaReg 4 _ -> wordGlobal $ "R4" ++ suf
41 VanillaReg 5 _ -> wordGlobal $ "R5" ++ suf
42 VanillaReg 6 _ -> wordGlobal $ "R6" ++ suf
43 VanillaReg 7 _ -> wordGlobal $ "R7" ++ suf
44 VanillaReg 8 _ -> wordGlobal $ "R8" ++ suf
45 SpLim -> wordGlobal $ "SpLim" ++ suf
46 FloatReg 1 -> floatGlobal $"F1" ++ suf
47 FloatReg 2 -> floatGlobal $"F2" ++ suf
48 FloatReg 3 -> floatGlobal $"F3" ++ suf
49 FloatReg 4 -> floatGlobal $"F4" ++ suf
50 FloatReg 5 -> floatGlobal $"F5" ++ suf
51 FloatReg 6 -> floatGlobal $"F6" ++ suf
52 DoubleReg 1 -> doubleGlobal $ "D1" ++ suf
53 DoubleReg 2 -> doubleGlobal $ "D2" ++ suf
54 DoubleReg 3 -> doubleGlobal $ "D3" ++ suf
55 DoubleReg 4 -> doubleGlobal $ "D4" ++ suf
56 DoubleReg 5 -> doubleGlobal $ "D5" ++ suf
57 DoubleReg 6 -> doubleGlobal $ "D6" ++ suf
58 XmmReg 1 -> xmmGlobal $ "XMM1" ++ suf
59 XmmReg 2 -> xmmGlobal $ "XMM2" ++ suf
60 XmmReg 3 -> xmmGlobal $ "XMM3" ++ suf
61 XmmReg 4 -> xmmGlobal $ "XMM4" ++ suf
62 XmmReg 5 -> xmmGlobal $ "XMM5" ++ suf
63 XmmReg 6 -> xmmGlobal $ "XMM6" ++ suf
64 _other -> panic $ "LlvmCodeGen.Reg: GlobalReg (" ++ (show reg)
65 ++ ") not supported!"
66 -- LongReg, HpLim, CCSS, CurrentTSO, CurrentNusery, HpAlloc
67 -- EagerBlackholeInfo, GCEnter1, GCFun, BaseReg, PicBaseReg
68 where
69 wordGlobal name = LMNLocalVar (fsLit name) (llvmWord dflags)
70 ptrGlobal name = LMNLocalVar (fsLit name) (llvmWordPtr dflags)
71 floatGlobal name = LMNLocalVar (fsLit name) LMFloat
72 doubleGlobal name = LMNLocalVar (fsLit name) LMDouble
73 xmmGlobal name = LMNLocalVar (fsLit name) (LMVector 4 (LMInt 32))
74
75 -- | A list of STG Registers that should always be considered alive
76 alwaysLive :: [GlobalReg]
77 alwaysLive = [BaseReg, Sp, Hp, SpLim, HpLim, node]
78
79 -- | STG Type Based Alias Analysis metadata
80 stgTBAA :: [MetaDecl]
81 stgTBAA
82 = [ MetaUnamed topN $ MetaStr (fsLit "top")
83 , MetaUnamed stackN $ MetaStruct [MetaStr (fsLit "stack"), MetaNode topN]
84 , MetaUnamed heapN $ MetaStruct [MetaStr (fsLit "heap"), MetaNode topN]
85 , MetaUnamed rxN $ MetaStruct [MetaStr (fsLit "rx"), MetaNode heapN]
86 , MetaUnamed baseN $ MetaStruct [MetaStr (fsLit "base"), MetaNode topN]
87 -- FIX: Not 100% sure about 'others' place. Might need to be under 'heap'.
88 -- OR I think the big thing is Sp is never aliased, so might want
89 -- to change the hieracy to have Sp on its own branch that is never
90 -- aliased (e.g never use top as a TBAA node).
91 , MetaUnamed otherN $ MetaStruct [MetaStr (fsLit "other"), MetaNode topN]
92 ]
93
94 -- | Id values
95 topN, stackN, heapN, rxN, baseN, otherN:: Int
96 topN = 0
97 stackN = 1
98 heapN = 2
99 rxN = 3
100 baseN = 4
101 otherN = 5
102
103 -- | The various TBAA types
104 top, heap, stack, rx, base, other :: MetaAnnot
105 top = MetaAnnot tbaa (MetaNode topN)
106 heap = MetaAnnot tbaa (MetaNode heapN)
107 stack = MetaAnnot tbaa (MetaNode stackN)
108 rx = MetaAnnot tbaa (MetaNode rxN)
109 base = MetaAnnot tbaa (MetaNode baseN)
110 other = MetaAnnot tbaa (MetaNode otherN)
111
112 -- | The TBAA metadata identifier
113 tbaa :: LMString
114 tbaa = fsLit "tbaa"
115
116 -- | Get the correct TBAA metadata information for this register type
117 getTBAA :: GlobalReg -> MetaAnnot
118 getTBAA BaseReg = base
119 getTBAA Sp = stack
120 getTBAA Hp = heap
121 getTBAA (VanillaReg _ _) = rx
122 getTBAA _ = top
123