Commit Graph

1642 Commits

Author SHA1 Message Date
Dan Gohman
ef5ad630c8 Use the WasmRuntime's global list rather than keeping a separate list. 2017-10-09 17:29:39 -07:00
Dan Gohman
3841552b7c Use the WasmRuntime's type list rather than keeping a separate list. 2017-10-09 17:29:39 -07:00
Jakob Stoklund Olesen
90ed698e83 Add an unreachable code elimination pass.
The register allocator doesn't even try to compile unreachable EBBs, so
any values defined in such blocks won't be assigned registers.

Since the dominator tree already has determined which EBBs are
reachable, we should just eliminate any unreachable blocks instead o
trying to do something with the dead code.

Not that this is not a "dead code elimination" pass which would also
remove individual instructions whose results are not used.
2017-10-09 15:26:27 -07:00
Dan Gohman
6aeeaebbd3 Disallow branching to the entry block.
Functions that would otherwise start with a loop should start with a
separate ebb which just branches to the header of the loop.
2017-10-09 15:02:17 -07:00
Jakob Stoklund Olesen
893a6716c6 Enforce all instruction constraints in iterate_solution().
During iterate_solution(), live-through values may be converted to
solver variables so they can be moved out of the way in order to satisfy
all constraints. Make sure that the instruction's operand constraints
are also considered for these new variables.

Add a program_complete_input_constraints() which turns all the
instruction's input operands into variables with the proper constraints.
That makes it safe for try_add_var() to re-add these values as variables
with looser generic constraints.

The solver's add_var() function is split into three functions: add_var
for use before inputs_done(), and add_killed_var/add_through_var for use
after.
2017-10-09 14:08:37 -07:00
Jakob Stoklund Olesen
4a2bf6d9a6 Use a more compact display of AllocatableSet.
Since only Intel uses named registers, we can use a one-char shorthand
for the registers.
2017-10-09 14:08:37 -07:00
Jakob Stoklund Olesen
ecd537ecd6 Avoid widening TailRecipe register constraints automatically.
Most recipes with an ABCD constraint can handle the full GPR register
class when a REX prefix is applied, but not all. The "icscc" macro
recipe always generates a setCC instruction with no REX prefix, so it
can only write the ABCD registers, even in its REX form.

Don't automatically rewrite ABCD constraints to GPR constraints when
applying a REX prefix to a tail recipe. Instead, allow individual ABCD
recipes to specify a "when_prefixed" alternative recipe to use. This
also eliminates the spurious Rex*abcd recipe names which didn't have an
ABCD constraint.

Also allow recipes to specify that a REX prefix is required by setting
the prefix_required flag. This is used by recipes like t8jccb which
explicitly accesses an 8-bit register with a GPR constraint which is
only valid with a prefix.
2017-10-09 14:08:37 -07:00
Jakob Stoklund Olesen
ac8c8a676a Constrain solver variables as little as possible.
When solver variables represent operands on the current instruction,
they need to be constrained as required by the instructions, but
variables that are simply moved out of the way should only be
constrained to their top-level register class. The live range affinity
is just a hint, not a requirement.
2017-10-09 14:08:37 -07:00
Jakob Stoklund Olesen
12a8d6cce1 Avoid diverting values that are live on an outgoing CFG edge.
When try_add_var is looking for values that can be moved out of the way
in order to satisfy constraints for the current instruction, avoid
values that are live on a CFG edge originating at the current (branch)
instruction.

These values must be in their globally assigned location when entering
the branch destination EBB.

This is covered by the existing regalloc/iterate.cton test case which
fails with an upcoming commit.
2017-10-09 14:08:37 -07:00
Dan Gohman
0c4500897f Clarify FunctionName's role in its comment. 2017-10-09 13:50:03 -07:00
Dan Gohman
d1766f0ba4 Simplify code by removing unnecessary Options. 2017-10-06 15:59:30 -07:00
Jakob Stoklund Olesen
56862c3da9 Roll back to mypy 0.521 to avoid a bug in 0.530.
Filed here: https://github.com/python/mypy/issues/4069

We should go back to tracking the latest mypy releases as soon as
possible.
2017-10-06 12:12:32 -07:00
Jakob Stoklund Olesen
b3fa47cacc Add support for emergency spill slots.
- Create a new kind of stack slot: emergency_slot.
- Add a get_emergency_slot() method which finds a suitable emergency
  slot given a list of slots already in use.
- Use emergency spill slots when schedule_moves needs them.
2017-10-06 10:45:13 -07:00
Jakob Stoklund Olesen
d0b4c76262 Use a non-allocating sort algorithm.
The sort_unstable* functions are available in stable Rust now. These
functions never allocate memory.
2017-10-06 09:21:30 -07:00
Jakob Stoklund Olesen
b562fdcd5c Remove the dfg::resolve_copies() method.
This method was important back when result values couldn't be moved
between instructions. Now that results can be moved, value aliases do
everything we need.

Copy instructions are still used to break interferences in the register
allocator's coalescing phase, but there isn't really any reason to use a
copy instruction over a value alias anywhere else.

After and during register allocation, copy instructions are significant,
so we never want to "see through" them like the resolve_copies()
function did.

This is related to #166, but probably doesn't fix the underlying
problem.
2017-10-05 14:46:34 -07:00
Jakob Stoklund Olesen
30aeb57083 Add a value location verifier.
This is a verification pass that can be run after register allocation.
It verifies that value locations are consistent with constraints on
their uses, and that the register diversions are consistent.

Make it clear that register diversions are local to an EBB only. This
affects what branch relaxation is allowed to do.

The verify_locations() takes an optional Liveness parameter which is
used to check that no diverted values are live across CFG edges.
2017-10-05 13:59:18 -07:00
Dan Gohman
e4ef2cbf22 Support ISA-specific settings in the --isa command-line option. 2017-10-05 10:06:40 -07:00
Jakob Stoklund Olesen
73d4bb47c0 Intel encodings for regspill and regfill.
These are always SP-based.
2017-10-04 17:02:09 -07:00
Jakob Stoklund Olesen
826d4062fb Apply register diversions during binemit tests.
When "binemit" tests encode instructions, keep track of the current set
of register diversions, and use the diverted locations to check operand
constraints.

This matches how constraints are applied during a real binemit phase.
2017-10-04 17:02:09 -07:00
Jakob Stoklund Olesen
dda3efcbdd Add regspill and regfill instructions.
These are parallels to the existing regmove instruction, but the divert
the value to and from a stack slot.

Like regmove diversions, this is a temporary diversion that must be
local to the EBB.
2017-10-04 17:02:09 -07:00
Jakob Stoklund Olesen
d4aeec6ece Generalize RegDiversions to track stack locations too.
For emergency spilling, we need to be able to temporarily divert an SSA
value to a stack slot if there are no available registers.
2017-10-04 17:02:09 -07:00
Jakob Stoklund Olesen
e32aa8ab60 Emergency spilling for the solver's move scheduler.
The register constraint solver schedules a set of move instructions to
execute before the instruction getting colored. In extreme cases, this
is not possible because there are no available registers to break cycles
in the register assignments that must be scheduled.

When that happens, we spill one register to an emergency slot so it
becomes available for implementing the assignment cycle. Then the
original register is restored.

The coloring pass can't yet understand the spill and fill move types.
This will be implemented next.
2017-10-04 17:02:09 -07:00
Jakob Stoklund Olesen
ce4d723a73 Give RegClassData a reference to its parent RegInfo.
This makes it possible to materialize new RegClass references without
requiring a RegInfo reference to be passed around.

- Move the RegInfo::toprc() method to RegClassData.
- Rename RegClassData::intersect() to intersect_index() and provide a
  new intersect() which returns a register class.
- Remove some &RegInfo parameters that are no longer needed.
2017-10-04 17:02:09 -07:00
Dan Gohman
7410ddfe08 Use the WasmRuntime's signature list rather than keeping a separate list.
This way, if the runtime modifies the signature, such as to add special
arguments, they are reflected in the resulting function defintions.
2017-10-04 16:57:39 -07:00
Dan Gohman
196795017b Tidy up handling of the DummyRuntime. 2017-10-04 16:57:39 -07:00
Jakob Stoklund Olesen
fb0999ce33 Check the top-level register class for available registers.
Fixes #165.

The constraint solver's schedule_move() function sometimes need to use
an extra available register when the moves to be scheduled contains
cycles.

The pending moves have associated register classes that come from the
constraint programming. Since the moves have hard-coded to and from
registers, these register classes are only meant to indicate the
register sizes. In particular, we can use the whole top-level register
class when scavenging for a spare register to break a cycle.
2017-10-03 14:12:18 -07:00
Jakob Stoklund Olesen
739d414d18 Convert regalloc::coloring to use an EncCursor.
No functional change intended, this is just a big fight with the borrow
checker.
2017-10-03 13:39:43 -07:00
Jakob Stoklund Olesen
7c023b2430 Don't omit the controlling typevar for instructions without results.
The controlling type variable passed to the format constructor in the
InstBuilder trait is not just used to generate the result values. In an
EncCursor, it is also used to encode the instruction, so VOID doesn't
work.
2017-10-03 13:39:43 -07:00
Jakob Stoklund Olesen
e10b3117cb Rename enc_flt() to enc_both().
This encoding method is not only used for floating point instructions.
2017-10-03 13:27:00 -07:00
Jakob Stoklund Olesen
c091a695e6 Fix coalescer bug exposed by the gvn-unremovable-phi test.
When we detect interference between the values that have already been
merged into the candidate virtual register and an EBB argument, we first
try to resolve the conflict by splitting. We also check if the existing
interfering value is fundamentally incompatible with the branch
instruction so it needs to be removed from the virtual register,
restarting the merge operation.

However, this existing interfering value is not necessarily the only
interference, so the split is not guaranteed to resolve the conflict. If
it turns out that splitting didn't resolve the conflict, restart the
merge after removing this second conflicting value.
2017-10-03 11:13:46 -07:00
Jakob Stoklund Olesen
ef048b8899 Allow for call args in incoming stack slots.
A value passed as an argument to a function call may live in an incoming
stack slot initially. Fix the call legalizer so it copies such an
argument into the expected outgoing stack slot for the call.
2017-10-03 11:13:46 -07:00
Dan Gohman
374ed3a07b Fix dominator-tree queries on unreachable nodes. 2017-10-03 11:03:06 -07:00
Dan Gohman
d585f09423 Don't silently swallow lexer errors. 2017-10-03 10:05:46 -07:00
Dan Gohman
d857aacec3 Sniff the wasm magic bytes, rather than relying on the filename extension. 2017-10-03 09:43:37 -07:00
Dan Gohman
ba14499fe9 Factor out the code for reading a file into a utility function. 2017-10-03 09:43:30 -07:00
Dan Gohman
f064418652 Refactor set/isa parsing into a utility function. 2017-10-03 09:43:27 -07:00
Dan Gohman
12ab4cd914 Add a cton-util compile command. 2017-10-03 09:42:50 -07:00
Dan Gohman
7c7b1651d8 Do a full compile in 'cton-util wasm'.
This removes the `optimize` option, as one can do that with
`--set`, eg. `--set opt_level=best`. And it adds an option to
print the compilation output.

And, it enables simple_gvn and licm for opt_level=best.
2017-10-03 09:39:07 -07:00
Dan Gohman
25b8b45a41 Avoid unnecessary BufReaders. 2017-10-03 09:34:41 -07:00
Dan Gohman
eba55cb1da Make the "wat2wasm" error message not panic. 2017-10-03 09:31:42 -07:00
Dan Gohman
1efa670f60 Do a full compile in 'cton-util wasm'.
This removes the `optimize` option, as one can do that with
`--set`, eg. `--set opt_level=best`. And it adds an option to
print the compilation output.
2017-10-03 09:30:45 -07:00
Jakob Stoklund Olesen
3a34c35f95 Don't swap the arguments to the select instruction.
Both WebAssembly and Cretonne use true-value, false-value.
2017-10-02 14:43:30 -07:00
Jakob Stoklund Olesen
5f56f81251 Resolve all value aliases when computing live ranges.
Value aliases are only in the way during register allocation, so make
sure they are all dead as we enter the register allocation passes.
2017-09-29 15:54:06 -07:00
Jakob Stoklund Olesen
c82e68efea Eliminate the ABCD register class constaint in REX encodings.
Some REX-less encodings require an ABCD input because they are looking
at 8-bit registers. This constraint doesn't apply with a REX prefix
where the low 8 bits of all registers are addressable.
2017-09-29 15:29:25 -07:00
Jakob Stoklund Olesen
51a6901a7f Implement coloring::iterate_solution().
It can happen that the currently live registers are blocking a smaller
register class completely, so the only way of solving the allocation
problem is to turn some of the live-through registers into solver
variables.

When the quick_solve attempt fails, try to free up registers in the
critical register class by turning live-through values into solver
variables.
2017-09-29 14:55:35 -07:00
Jakob Stoklund Olesen
86e22e7de5 Add long-range encodings for conditional branches.
The brz and brnz instructions get support for 32-bit jump displacements
for long range branches.

Also change the way branch ranges are specified on tail recipes for the
Intel instructions. All branch displacements are relative to the end of
the instruction, so just compute the branch range origin as the
instruction size instead of trying to specify it in the tail recipe
definitions.
2017-09-29 13:18:29 -07:00
Jakob Stoklund Olesen
50ccd000a9 Implement branch relaxation.
Now that we have the legal_encodings iterator, it is simpler to find an
alternative branch encoding with a better range.
2017-09-29 12:42:34 -07:00
Jakob Stoklund Olesen
45888ab84e Reload for spilled call return values.
When the return value from a call has been spilled, the reload pass
needs to insert a spill instruction right after the call instruction
which returns its results in registers.
2017-09-29 11:25:38 -07:00
Jakob Stoklund Olesen
711e5cd644 Handle srem INT_MIN, -1 correctly.
The x86_divmodx traps on integer overflow, but the srem instruction is
not supposed to trap with a -1 divisor.

Generate a legalization expansion for srem that special-cases the -1
divisor to simply return 0.
2017-09-29 08:53:49 -07:00
Jakob Stoklund Olesen
53404a9387 Check for invalid special type constraints.
The extend and reduce instructions have additional type constraints.

Stop inserting sextend instructions after ctz, clz, and popcnt when
translating from WebAssembly. The Cretonne instructions have the same
signature as the WebAssembly equivalents.
2017-09-28 16:30:19 -07:00