| 1 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 2 |
from rpython.jit.metainterp.resoperation import rop |
| 3 |
from rpython.jit.metainterp.history import (Const, ConstInt, ConstPtr, |
| 4 |
ConstFloat, INT, REF, FLOAT, VECTOR, TargetToken, AbstractFailDescr) |
| 5 |
from rpython.jit.backend.llsupport.descr import CallDescr |
| 6 |
from rpython.jit.backend.x86.regloc import (FrameLoc, RegLoc, ConstFloatLoc, |
| 7 |
FloatImmedLoc, ImmedLoc, imm, imm0, imm1, ecx, eax, edx, ebx, esi, edi, |
| 8 |
ebp, r8, r9, r10, r11, r12, r13, r14, r15, xmm0, xmm1, xmm2, xmm3, xmm4, |
| 9 |
xmm5, xmm6, xmm7, xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, |
| 10 |
X86_64_SCRATCH_REG, X86_64_XMM_SCRATCH_REG,) |
| 11 |
from rpython.jit.backend.x86 import rx86, callbuilder |
| 12 |
|
| 13 |
from rpython.jit.backend.llsupport.regalloc import (SAVE_DEFAULT_REGS, |
| 14 |
SAVE_GCREF_REGS, SAVE_ALL_REGS) |
| 15 |
|
| 16 |
from rpython.jit.backend.x86.regalloc import ( |
| 17 |
gpr_reg_mgr_cls, xmm_reg_mgr_cls, compute_gc_level, |
| 18 |
X86RegisterManager, X86XMMRegisterManager, |
| 19 |
X86_64_RegisterManager, X86_64_XMMRegisterManager, compute_gc_level |
| 20 |
) |
| 21 |
|
| 22 |
from rpython.jit.backend.x86.arch import IS_X86_32, IS_X86_64 |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class X86RegisterHints(object): |
| 30 |
def add_hints(self, longevity, inputargs, operations): |
| 31 |
self.longevity = longevity |
| 32 |
for i in range(len(operations)): |
| 33 |
op = operations[i] |
| 34 |
if rop.has_no_side_effect(op.opnum) and op not in self.longevity: |
| 35 |
continue |
| 36 |
oplist[op.getopnum()](self, op, i) |
| 37 |
|
| 38 |
def not_implemented_op(self, op, position): |
| 39 |
|
| 40 |
pass |
| 41 |
|
| 42 |
|
| 43 |
def consider_int_neg(self, op, position): |
| 44 |
self.longevity.try_use_same_register(op.getarg(0), op) |
| 45 |
consider_int_invert = consider_int_neg |
| 46 |
|
| 47 |
def _consider_binop_part(self, op, position, symm=False): |
| 48 |
x = op.getarg(0) |
| 49 |
y = op.getarg(1) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if symm: |
| 54 |
if isinstance(x, Const): |
| 55 |
x, y = y, x |
| 56 |
elif (not isinstance(y, Const) and |
| 57 |
self.longevity[x].last_usage > position and |
| 58 |
self.longevity[y].last_usage == position): |
| 59 |
x, y = y, x |
| 60 |
|
| 61 |
if not isinstance(x, Const): |
| 62 |
self.longevity.try_use_same_register(x, op) |
| 63 |
|
| 64 |
def _consider_binop(self, op, position): |
| 65 |
self._consider_binop_part(op, position) |
| 66 |
|
| 67 |
def _consider_binop_symm(self, op, position): |
| 68 |
self._consider_binop_part(op, position, symm=True) |
| 69 |
|
| 70 |
consider_int_mul = _consider_binop_symm |
| 71 |
consider_int_and = _consider_binop_symm |
| 72 |
consider_int_or = _consider_binop_symm |
| 73 |
consider_int_xor = _consider_binop_symm |
| 74 |
|
| 75 |
consider_int_mul_ovf = _consider_binop_symm |
| 76 |
consider_int_sub_ovf = _consider_binop |
| 77 |
consider_int_add_ovf = _consider_binop_symm |
| 78 |
|
| 79 |
def consider_int_add(self, op, position): |
| 80 |
y = op.getarg(1) |
| 81 |
if isinstance(y, ConstInt) and rx86.fits_in_32bits(y.value): |
| 82 |
pass |
| 83 |
else: |
| 84 |
self._consider_binop_symm(op, position) |
| 85 |
|
| 86 |
consider_nursery_ptr_increment = consider_int_add |
| 87 |
|
| 88 |
def consider_int_sub(self, op, position): |
| 89 |
y = op.getarg(1) |
| 90 |
if isinstance(y, ConstInt) and rx86.fits_in_32bits(-y.value): |
| 91 |
pass |
| 92 |
else: |
| 93 |
self._consider_binop(op, position) |
| 94 |
|
| 95 |
def _consider_float_op(self, op, position): |
| 96 |
x = op.getarg(0) |
| 97 |
if not isinstance(x, Const): |
| 98 |
self.longevity.try_use_same_register(x, op) |
| 99 |
|
| 100 |
consider_float_add = _consider_float_op |
| 101 |
consider_float_sub = _consider_float_op |
| 102 |
consider_float_mul = _consider_float_op |
| 103 |
consider_float_truediv = _consider_float_op |
| 104 |
consider_float_neg = _consider_float_op |
| 105 |
consider_float_abs = _consider_float_op |
| 106 |
|
| 107 |
def consider_int_lshift(self, op, position): |
| 108 |
x, y = op.getarg(0), op.getarg(1) |
| 109 |
if not isinstance(y, Const): |
| 110 |
self.longevity.fixed_register(position, ecx, y) |
| 111 |
if not isinstance(x, Const): |
| 112 |
self.longevity.try_use_same_register(x, op) |
| 113 |
|
| 114 |
consider_int_rshift = consider_int_lshift |
| 115 |
consider_uint_rshift = consider_int_lshift |
| 116 |
|
| 117 |
def consider_uint_mul_high(self, op, position): |
| 118 |
|
| 119 |
|
| 120 |
self.longevity.fixed_register(position, eax) |
| 121 |
self.longevity.fixed_register(position, edx) |
| 122 |
|
| 123 |
def consider_call_malloc_nursery(self, op, position): |
| 124 |
self.longevity.fixed_register(position, ecx, op) |
| 125 |
self.longevity.fixed_register(position, edx) |
| 126 |
|
| 127 |
consider_call_malloc_nursery_varsize = consider_call_malloc_nursery |
| 128 |
consider_call_malloc_nursery_varsize_frame = consider_call_malloc_nursery |
| 129 |
|
| 130 |
def _consider_call(self, op, position, guard_not_forced=False, first_arg_index=1): |
| 131 |
calldescr = op.getdescr() |
| 132 |
assert isinstance(calldescr, CallDescr) |
| 133 |
gc_level = compute_gc_level(calldescr, guard_not_forced) |
| 134 |
args = op.getarglist()[first_arg_index:] |
| 135 |
argtypes = calldescr.get_arg_types() |
| 136 |
CallHints64(self.longevity).hint(position, args, argtypes, gc_level) |
| 137 |
|
| 138 |
def _consider_real_call(self, op, position): |
| 139 |
effectinfo = op.getdescr().get_extra_info() |
| 140 |
assert effectinfo is not None |
| 141 |
oopspecindex = effectinfo.oopspecindex |
| 142 |
if oopspecindex != EffectInfo.OS_NONE: |
| 143 |
|
| 144 |
return |
| 145 |
self._consider_call(op, position) |
| 146 |
|
| 147 |
consider_call_i = _consider_real_call |
| 148 |
consider_call_r = _consider_real_call |
| 149 |
consider_call_f = _consider_real_call |
| 150 |
consider_call_n = _consider_real_call |
| 151 |
|
| 152 |
def _consider_call_may_force(self, op, position): |
| 153 |
self._consider_call(op, position, guard_not_forced=True) |
| 154 |
consider_call_may_force_i = _consider_call_may_force |
| 155 |
consider_call_may_force_r = _consider_call_may_force |
| 156 |
consider_call_may_force_f = _consider_call_may_force |
| 157 |
consider_call_may_force_n = _consider_call_may_force |
| 158 |
|
| 159 |
def _consider_call_release_gil(self, op, position): |
| 160 |
|
| 161 |
self._consider_call(op, position, guard_not_forced=True, first_arg_index=2) |
| 162 |
consider_call_release_gil_i = _consider_call_release_gil |
| 163 |
consider_call_release_gil_f = _consider_call_release_gil |
| 164 |
consider_call_release_gil_n = _consider_call_release_gil |
| 165 |
|
| 166 |
oplist = [X86RegisterHints.not_implemented_op] * rop._LAST |
| 167 |
|
| 168 |
for name, value in X86RegisterHints.__dict__.iteritems(): |
| 169 |
if name.startswith('consider_'): |
| 170 |
name = name[len('consider_'):] |
| 171 |
num = getattr(rop, name.upper()) |
| 172 |
oplist[num] = value |
| 173 |
|
| 174 |
class CallHints32(object): |
| 175 |
|
| 176 |
def _block_non_caller_save(self, position, save_all_regs, hinted_gpr=None, hinted_xmm=None): |
| 177 |
if hinted_gpr is None: |
| 178 |
hinted_gpr = [] |
| 179 |
if hinted_xmm is None: |
| 180 |
hinted_xmm = [] |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if save_all_regs == SAVE_ALL_REGS: |
| 186 |
regs = gpr_reg_mgr_cls.all_regs |
| 187 |
else: |
| 188 |
regs = gpr_reg_mgr_cls.save_around_call_regs |
| 189 |
for reg in regs: |
| 190 |
if reg not in hinted_gpr: |
| 191 |
self.longevity.fixed_register(position, reg) |
| 192 |
for reg in xmm_reg_mgr_cls.all_regs: |
| 193 |
if reg not in hinted_xmm: |
| 194 |
self.longevity.fixed_register(position, reg) |
| 195 |
|
| 196 |
def hint(self, position, args, argtypes, save_all_regs): |
| 197 |
self._block_non_caller_save(position, save_all_regs) |
| 198 |
|
| 199 |
|
| 200 |
class CallHints64(CallHints32): |
| 201 |
ARGUMENTS_GPR = callbuilder.CallBuilder64.ARGUMENTS_GPR |
| 202 |
ARGUMENTS_XMM = callbuilder.CallBuilder64.ARGUMENTS_XMM |
| 203 |
|
| 204 |
def __init__(self, longevity): |
| 205 |
self.longevity = longevity |
| 206 |
|
| 207 |
def hint(self, position, args, argtypes, save_all_regs): |
| 208 |
hinted_xmm = [] |
| 209 |
hinted_gpr = [] |
| 210 |
hinted_args = [] |
| 211 |
next_arg_gpr = 0 |
| 212 |
next_arg_xmm = 0 |
| 213 |
for i in range(len(args)): |
| 214 |
arg = args[i] |
| 215 |
if arg.type == "f" or (i < len(argtypes) and argtypes[i] == 'S'): |
| 216 |
if next_arg_xmm < len(self.ARGUMENTS_XMM): |
| 217 |
tgt = self.ARGUMENTS_XMM[next_arg_xmm] |
| 218 |
if not arg.is_constant() and arg not in hinted_args: |
| 219 |
self.longevity.fixed_register(position, tgt, arg) |
| 220 |
hinted_xmm.append(tgt) |
| 221 |
hinted_args.append(arg) |
| 222 |
next_arg_xmm += 1 |
| 223 |
else: |
| 224 |
if next_arg_gpr < len(self.ARGUMENTS_GPR): |
| 225 |
tgt = self.ARGUMENTS_GPR[next_arg_gpr] |
| 226 |
if not arg.is_constant() and arg not in hinted_args: |
| 227 |
self.longevity.fixed_register(position, tgt, arg) |
| 228 |
hinted_gpr.append(tgt) |
| 229 |
hinted_args.append(arg) |
| 230 |
next_arg_gpr += 1 |
| 231 |
self._block_non_caller_save(position, save_all_regs, hinted_gpr, hinted_xmm) |
| 232 |
|
| 233 |
|
| 234 |
if IS_X86_32: |
| 235 |
CallHints = CallHints32 |
| 236 |
if IS_X86_64: |
| 237 |
CallHints = CallHints64 |