Debian Codegraph @

rpython/jit/backend/x86/jump.py

raw symbols SCIP
1
2 from rpython.jit.backend.x86.regloc import ImmediateAssemblerLocation,\
3 FrameLoc
4
5 def remap_frame_layout(assembler, src_locations, dst_locations, tmpreg):
6 pending_dests = len(dst_locations)
7 srccount = {} # maps dst_locations to how many times the same
8 # location appears in src_locations
9 num_moves = 0
10 for dst in dst_locations:
11 key = dst._getregkey()
12 assert key not in srccount, "duplicate value in dst_locations!"
13 srccount[key] = 0
14 for i in range(len(dst_locations)):
15 src = src_locations[i]
16 if isinstance(src, ImmediateAssemblerLocation):
17 continue
18 key = src._getregkey()
19 if key in srccount:
20 if key == dst_locations[i]._getregkey():
21 # ignore a move "x = x"
22 # setting any "large enough" negative value is ok, but
23 # be careful of overflows, don't use -sys.maxint
24 srccount[key] = -len(dst_locations) - 1
25 pending_dests -= 1
26 else:
27 srccount[key] += 1
28
29 while pending_dests > 0:
30 progress = False
31 for i in range(len(dst_locations)):
32 dst = dst_locations[i]
33 key = dst._getregkey()
34 if srccount[key] == 0:
35 srccount[key] = -1 # means "it's done"
36 pending_dests -= 1
37 src = src_locations[i]
38 if not isinstance(src, ImmediateAssemblerLocation):
39 key = src._getregkey()
40 if key in srccount:
41 srccount[key] -= 1
42 _move(assembler, src, dst, tmpreg)
43 num_moves += 1
44 progress = True
45 if not progress:
46 # we are left with only pure disjoint cycles
47 sources = {} # maps dst_locations to src_locations
48 for i in range(len(dst_locations)):
49 src = src_locations[i]
50 dst = dst_locations[i]
51 sources[dst._getregkey()] = src
52 #
53 for i in range(len(dst_locations)):
54 dst = dst_locations[i]
55 originalkey = dst._getregkey()
56 if srccount[originalkey] >= 0:
57 assembler.regalloc_push(dst)
58 num_moves += 1
59 while True:
60 key = dst._getregkey()
61 assert srccount[key] == 1
62 # ^^^ because we are in a simple cycle
63 srccount[key] = -1
64 pending_dests -= 1
65 src = sources[key]
66 if src._getregkey() == originalkey:
67 break
68 _move(assembler, src, dst, tmpreg)
69 num_moves += 1
70 dst = src
71 assembler.regalloc_pop(dst)
72 num_moves += 1
73 assert pending_dests == 0
74 return num_moves
75
76 def _move(assembler, src, dst, tmpreg):
77 if dst.is_memory_reference() and src.is_memory_reference():
78 if isinstance(src, ImmediateAssemblerLocation):
79 assembler.regalloc_immedmem2mem(src, dst)
80 return
81 if tmpreg is None:
82 assembler.regalloc_push(src)
83 assembler.regalloc_pop(dst)
84 return
85 assembler.regalloc_mov(src, tmpreg)
86 assembler.mc.forget_scratch_register()
87 src = tmpreg
88 assembler.regalloc_mov(src, dst)
89
90 def remap_frame_layout_mixed(assembler,
91 src_locations1, dst_locations1, tmpreg1,
92 src_locations2, dst_locations2, tmpreg2):
93 # find and push the xmm stack locations from src_locations2 that
94 # are going to be overwritten by dst_locations1
95 from rpython.jit.backend.x86.arch import WORD
96 extrapushes = []
97 dst_keys = {}
98 for loc in dst_locations1:
99 dst_keys[loc._getregkey()] = None
100 src_locations2red = []
101 dst_locations2red = []
102 num_moves = 0
103 for i in range(len(src_locations2)):
104 loc = src_locations2[i]
105 dstloc = dst_locations2[i]
106 if isinstance(loc, FrameLoc):
107 key = loc._getregkey()
108 if (key in dst_keys or (loc.get_width() > WORD and
109 (key + WORD) in dst_keys)):
110 num_moves += 1
111 assembler.regalloc_push(loc)
112 extrapushes.append(dstloc)
113 continue
114 src_locations2red.append(loc)
115 dst_locations2red.append(dstloc)
116 src_locations2 = src_locations2red
117 dst_locations2 = dst_locations2red
118 #
119 # remap the integer and pointer registers and stack locations
120 num_moves += remap_frame_layout(assembler, src_locations1, dst_locations1, tmpreg1)
121 #
122 # remap the xmm registers and stack locations
123 num_moves += remap_frame_layout(assembler, src_locations2, dst_locations2, tmpreg2)
124 #
125 # finally, pop the extra xmm stack locations
126 while len(extrapushes) > 0:
127 loc = extrapushes.pop()
128 assembler.regalloc_pop(loc)
129 num_moves += 1
130 return num_moves