Adds support for transforming integer division and remainder by constants
into sequences that do not involve division instructions.
* div/rem by constant powers of two are turned into right shifts, plus some
fixups for the signed cases.
* div/rem by constant non-powers of two are turned into double length
multiplies by a magic constant, plus some fixups involving shifts,
addition and subtraction, that depends on the constant, the word size and
the signedness involved.
* The following cases are transformed: div and rem, signed or unsigned, 32
or 64 bit. The only un-transformed cases are: unsigned div and rem by
zero, signed div and rem by zero or -1.
* This is all incorporated within a new transformation pass, "preopt", in
lib/cretonne/src/preopt.rs.
* In preopt.rs, fn do_preopt() is the main driver. It is designed to be
extensible to transformations of other kinds of instructions. Currently
it merely uses a helper to identify div/rem transformation candidates and
another helper to perform the transformation.
* In preopt.rs, fn get_div_info() pattern matches to find candidates, both
cases where the second arg is an immediate, and cases where the second
arg is an identifier bound to an immediate at its definition point.
* In preopt.rs, fn do_divrem_transformation() does the heavy lifting of the
transformation proper. It in turn uses magic{S,U}{32,64} to calculate the
magic numbers required for the transformations.
* There are many test cases for the transformation proper:
filetests/preopt/div_by_const_non_power_of_2.cton
filetests/preopt/div_by_const_power_of_2.cton
filetests/preopt/rem_by_const_non_power_of_2.cton
filetests/preopt/rem_by_const_power_of_2.cton
filetests/preopt/div_by_const_indirect.cton
preopt.rs also contains a set of tests for magic number generation.
* The main (non-power-of-2) transformation requires instructions that return
the high word of a double-length multiply. For this, instructions umulhi
and smulhi have been added to the core instruction set. These will map
directly to single instructions on most non-intel targets.
* intel does not have an instruction exactly like that. For intel,
instructions x86_umulx and x86_smulx have been added. These map to real
instructions and return both result words. The intel legaliser will
rewrite {s,u}mulhi into x86_{s,u}mulx uses that throw away the lower half
word. Tests:
filetests/isa/intel/legalize-mulhi.cton (new file)
filetests/isa/intel/binary64.cton (added x86_{s,u}mulx encoding tests)
174 lines
5.3 KiB
Python
174 lines
5.3 KiB
Python
"""
|
|
Supplementary instruction definitions for Intel.
|
|
|
|
This module defines additional instructions that are useful only to the Intel
|
|
target ISA.
|
|
"""
|
|
|
|
from base.types import iflags
|
|
from cdsl.operands import Operand
|
|
from cdsl.typevar import TypeVar
|
|
from cdsl.instructions import Instruction, InstructionGroup
|
|
|
|
|
|
GROUP = InstructionGroup("x86", "Intel-specific instruction set")
|
|
|
|
iWord = TypeVar('iWord', 'A scalar integer machine word', ints=(32, 64))
|
|
|
|
nlo = Operand('nlo', iWord, doc='Low part of numerator')
|
|
nhi = Operand('nhi', iWord, doc='High part of numerator')
|
|
d = Operand('d', iWord, doc='Denominator')
|
|
q = Operand('q', iWord, doc='Quotient')
|
|
r = Operand('r', iWord, doc='Remainder')
|
|
|
|
udivmodx = Instruction(
|
|
'x86_udivmodx', r"""
|
|
Extended unsigned division.
|
|
|
|
Concatenate the bits in `nhi` and `nlo` to form the numerator.
|
|
Interpret the bits as an unsigned number and divide by the unsigned
|
|
denominator `d`. Trap when `d` is zero or if the quotient is larger
|
|
than the range of the output.
|
|
|
|
Return both quotient and remainder.
|
|
""",
|
|
ins=(nlo, nhi, d), outs=(q, r), can_trap=True)
|
|
|
|
sdivmodx = Instruction(
|
|
'x86_sdivmodx', r"""
|
|
Extended signed division.
|
|
|
|
Concatenate the bits in `nhi` and `nlo` to form the numerator.
|
|
Interpret the bits as a signed number and divide by the signed
|
|
denominator `d`. Trap when `d` is zero or if the quotient is outside
|
|
the range of the output.
|
|
|
|
Return both quotient and remainder.
|
|
""",
|
|
ins=(nlo, nhi, d), outs=(q, r), can_trap=True)
|
|
|
|
argL = Operand('argL', iWord)
|
|
argR = Operand('argR', iWord)
|
|
resLo = Operand('resLo', iWord)
|
|
resHi = Operand('resHi', iWord)
|
|
|
|
umulx = Instruction(
|
|
'x86_umulx', r"""
|
|
Unsigned integer multiplication, producing a double-length result.
|
|
|
|
Polymorphic over all scalar integer types, but does not support vector
|
|
types.
|
|
""",
|
|
ins=(argL, argR), outs=(resLo, resHi))
|
|
|
|
smulx = Instruction(
|
|
'x86_smulx', r"""
|
|
Signed integer multiplication, producing a double-length result.
|
|
|
|
Polymorphic over all scalar integer types, but does not support vector
|
|
types.
|
|
""",
|
|
ins=(argL, argR), outs=(resLo, resHi))
|
|
|
|
Float = TypeVar(
|
|
'Float', 'A scalar or vector floating point number',
|
|
floats=True, simd=True)
|
|
IntTo = TypeVar(
|
|
'IntTo', 'An integer type with the same number of lanes',
|
|
ints=(32, 64), simd=True)
|
|
|
|
x = Operand('x', Float)
|
|
a = Operand('a', IntTo)
|
|
|
|
cvtt2si = Instruction(
|
|
'x86_cvtt2si', r"""
|
|
Convert with truncation floating point to signed integer.
|
|
|
|
The source floating point operand is converted to a signed integer by
|
|
rounding towards zero. If the result can't be represented in the output
|
|
type, returns the smallest signed value the output type can represent.
|
|
|
|
This instruction does not trap.
|
|
""",
|
|
ins=x, outs=a)
|
|
|
|
x = Operand('x', Float)
|
|
a = Operand('a', Float)
|
|
y = Operand('y', Float)
|
|
|
|
fmin = Instruction(
|
|
'x86_fmin', r"""
|
|
Floating point minimum with Intel semantics.
|
|
|
|
This is equivalent to the C ternary operator `x < y ? x : y` which
|
|
differs from :inst:`fmin` when either operand is NaN or when comparing
|
|
+0.0 to -0.0.
|
|
|
|
When the two operands don't compare as LT, `y` is returned unchanged,
|
|
even if it is a signalling NaN.
|
|
""",
|
|
ins=(x, y), outs=a)
|
|
|
|
fmax = Instruction(
|
|
'x86_fmax', r"""
|
|
Floating point maximum with Intel semantics.
|
|
|
|
This is equivalent to the C ternary operator `x > y ? x : y` which
|
|
differs from :inst:`fmax` when either operand is NaN or when comparing
|
|
+0.0 to -0.0.
|
|
|
|
When the two operands don't compare as GT, `y` is returned unchanged,
|
|
even if it is a signalling NaN.
|
|
""",
|
|
ins=(x, y), outs=a)
|
|
|
|
|
|
x = Operand('x', iWord)
|
|
|
|
push = Instruction(
|
|
'x86_push', r"""
|
|
Pushes a value onto the stack.
|
|
|
|
Decrements the stack pointer and stores the specified value on to the top.
|
|
|
|
This is polymorphic in i32 and i64. However, it is only implemented for i64
|
|
in 64-bit mode, and only for i32 in 32-bit mode.
|
|
""",
|
|
ins=x, can_store=True, other_side_effects=True)
|
|
|
|
pop = Instruction(
|
|
'x86_pop', r"""
|
|
Pops a value from the stack.
|
|
|
|
Loads a value from the top of the stack and then increments the stack
|
|
pointer.
|
|
|
|
This is polymorphic in i32 and i64. However, it is only implemented for i64
|
|
in 64-bit mode, and only for i32 in 32-bit mode.
|
|
""",
|
|
outs=x, can_load=True, other_side_effects=True)
|
|
|
|
y = Operand('y', iWord)
|
|
rflags = Operand('rflags', iflags)
|
|
|
|
bsr = Instruction(
|
|
'x86_bsr', r"""
|
|
Bit Scan Reverse -- returns the bit-index of the most significant 1
|
|
in the word. Result is undefined if the argument is zero. However, it
|
|
sets the Z flag depending on the argument, so it is at least easy to
|
|
detect and handle that case.
|
|
|
|
This is polymorphic in i32 and i64. It is implemented for both i64 and
|
|
i32 in 64-bit mode, and only for i32 in 32-bit mode.
|
|
""",
|
|
ins=x, outs=(y, rflags))
|
|
|
|
bsf = Instruction(
|
|
'x86_bsf', r"""
|
|
Bit Scan Forwards -- returns the bit-index of the least significant 1
|
|
in the word. Is otherwise identical to 'bsr', just above.
|
|
""",
|
|
ins=x, outs=(y, rflags))
|
|
|
|
GROUP.close()
|