Commit Graph

1543 Commits

Author SHA1 Message Date
Jakob Stoklund Olesen
7bc2e82b16 Implement the basics of the x86-64 ABI.
This is just a rough sketch to get us started. There are bound to be
some issues.

This also legalizes signatures for x86-32, but probably not correctly.
It's basically implementing the x86-64 ABI for 32-bit.
2017-06-30 10:41:26 -07:00
Jakob Stoklund Olesen
983048cdd1 Generate an enum with all the register units in a target.
It is sometimes useful to create constant lists of register units by
name. The generated RU enum can be used for that.
2017-06-30 10:39:51 -07:00
Jakob Stoklund Olesen
aa3bf4467e Hook up the handling of tied register constraints.
Tests are forthcoming, we need to implement Intel ABI lowering first.
2017-06-30 08:41:54 -07:00
Jakob Stoklund Olesen
18dc420352 Repair constraint violations during spilling.
The following constraints may need to be resolved during spilling
because the resolution increases register pressure:

- A tied operand whose value is live through the instruction.
- A fixed register constraint for a value used more than once.
- A register use of a spilled value needs to account for the reload
  register.
2017-06-29 16:51:05 -07:00
Jakob Stoklund Olesen
2b999d9bd6 Add an Index<Value> implementation to Liveness.
Use it to access live ranges that are supposed to be there.
2017-06-29 15:13:04 -07:00
Jakob Stoklund Olesen
138d3c75c6 Spill live-ins and EBB arguments if there are too many. 2017-06-29 14:07:19 -07:00
Jakob Stoklund Olesen
ae661631aa Only color EBB arguments that have register affinity.
It is possible to pass a register value as an argument to an EBB that
expects a "None" affinity. In that case, the destination EBB value
should not be colored.
2017-06-29 13:29:43 -07:00
Jakob Stoklund Olesen
d6f73ac4c8 Split spill_from() into spill_candidate() and spill_reg().
We'll need to pick a spill candidate from a set and allow for the search
to fail to find anything.

This also allows slightly better panic messages when we run out of
registers.
2017-06-29 11:11:46 -07:00
Jakob Stoklund Olesen
588ef0ad2f Propagate affinities for EBB arguments.
A priory, an EBB argument value only gets an affinity if it is used
directly by a non-ghost instruction. A use by a branch passing arguments
to an EBB doesn't count.

When an EBB argument value does have an affinity, the values passed by
all the predecessors must also have affinities. This can cause EBB
argument values to get affinities recursively.

- Add a second pass to the liveness computation for propagating EBB
  argument affinities, possibly recursively.
- Verify EBB argument affinities correctly: A value passed to a branch
  must have an affinity only if the corresponding EBB argument value in
  the destination has an affinity.
2017-06-29 10:30:26 -07:00
Jakob Stoklund Olesen
e7a543ea33 Make sure return values are assigned an affinity.
When an EBB argument value is used only as a return value, it still
needs to be given a register affinity. Otherwise it would appear as a
ghost value with no affinity.

Do the same to call arguments.
2017-06-29 09:24:05 -07:00
Jakob Stoklund Olesen
0574dcdeee Don't coalesce incoming stack arguments.
A function parameter in an incoming_arg stack slot should not be
coalesced into any virtual registers. We don't want to force the whole
virtual register to spill to the incoming_arg slot.
2017-06-28 15:37:38 -07:00
Jakob Stoklund Olesen
b2fda76c5f Assign stack slots to incoming function arguments.
Function arguments that don't fit in registers are passed on the stack.

Create "incoming_arg" stack slots representing the stack arguments, and
assign them to the value arguments during spilling.
2017-06-28 15:03:59 -07:00
Jakob Stoklund Olesen
05cf44a156 Add an offset to StackSlotData.
The offset is relative to the stack pointer in the calling function, so
it excludes the return address pushed by the call instruction itself on
Intel ISAs.

Change the ArgumentLoc::Stack offset to an i32, so it matches the stack
slot offsets.
2017-06-28 14:38:13 -07:00
Jakob Stoklund Olesen
bbdf07a64e Color EBB arguments.
When coloring registers for a branch instruction, also make sure that
the values passed as EBB arguments are in the registers expected by the
EBB.

The first time a branch to an EBB is processed, assign the EBB arguments
to the registers where the branch arguments already reside so no
regmoves are needed.
2017-06-27 16:35:38 -07:00
Jakob Stoklund Olesen
165e80d9bf Process ghost instruction kills during coloring.
Ghost instructions don't generate code, but they can keep registers
alive. The coloring pass needs to process values killed by ghost
instructions so it knows when the registers are freed up.

Also track register pressure changes from ghost kills in the spiller.
2017-06-27 16:35:11 -07:00
Dimo
c073d919f4 Cleanup ValueType.get_names to with_bits form previous PR; Add computation of inverse image of typeset across a derived function - TypeSet.map_inverse; Change TypeVar.constrain_type to perform a more-general computation using inverse images of TypeSets; Tests for map_inverse; 2017-06-26 11:11:28 -07:00
d1m0
7c298078c8 Add image computation of typesets; Remove TypeVar.singleton_type - instead derive singleton type from typeset; (#104) 2017-06-23 11:57:24 -07:00
Jakob Stoklund Olesen
222ea74830 Spill whole virtual registers at a time.
When the spiller decides to spill a value, bring along all of the values
in its virtual register. This ensures that we won't have problems with
computing register pressure around EBB arguments. They will always be
register-to-register or stack-to-stack with related values using the
same stack slot.

This also means that the reloading pass won't have to deal with spilled
EBB arguments.
2017-06-23 10:43:59 -07:00
Dan Gohman
a79703c23f Add rusty-tags.* to .gitignore. 2017-06-23 09:36:21 -07:00
d1m0
da24bd422a Convert interval sets inside TypeSet/ValueTypeSet in general sets (#102)
* Convert TypeSet fields to sets; Add BitSet<T> type to rust; Encode ValueTypeSets using BitSet; (still need mypy cleanup)

* nits

* cleanup nits

* forgot mypy type annotations

* rustfmt fixes

* Round 1 comments: filer b2, b4; doc comments in python; move bitset in its own toplevel module; Use Into<u32>

* fixes

* Revert comment to appease rustfmt
2017-06-22 16:47:14 -07:00
Jakob Stoklund Olesen
85b624d13b Add a coalescing pass to the register allocator.
Coalescing means creating virtual registers and transforming the code
into conventional SSA form. This means that every value used as a branch
argument will belong to the same virtual register as the corresponding
EBB argument value.

Conventional SSA form makes it easy to avoid memory-memory copies when
spilling values, and the virtual registers can be used as hints when
picking registers too. This reduces the number of register moves needed
for EBB arguments.
2017-06-22 15:03:48 -07:00
Jakob Stoklund Olesen
d5055275c4 Virtual registers.
Add a VirtRegs collection which tracks virtual registers.

A virtual register is a set of related SSA values whose live ranges
don't interfere. It is advantageous to use the same register or spill
slot for al the values in a virtual register. It reduces copies for EBB
arguments.
2017-06-22 14:51:10 -07:00
Jakob Stoklund Olesen
d079dead9a Skip ghost instructions when coloring.
Ghost instructions don't have an encoding, and don't appear in the
output. The values they define do not need to be assigned to registers,
so they can be skipped.
2017-06-22 14:49:43 -07:00
Jakob Stoklund Olesen
03a856f4da Implement Display and Debug for the program point types. 2017-06-22 14:49:43 -07:00
Jakob Stoklund Olesen
10e4b2fa06 Add two interference checking methods to LiveInterval.
The overlaps_def() method tests if a definition would conflict with the
live range.

The reaches_use() method tests if a live range is live at an
instruction.
2017-06-22 14:49:43 -07:00
Dan Gohman
0f53fe1913 Add a simple_gvn test that includes some basic control flow. 2017-06-22 14:34:21 -07:00
Jakob Stoklund Olesen
e15c332471 Encode iconst.i32 for RISC-V.
For large constants with the low 12 bits clear, we already have the
"lui" encoding. Add "addi %x0" encodings for signed 12-bit constants.
2017-06-22 12:13:37 -07:00
Dan Gohman
4a5d48fe11 Documentation fixes (#103)
* Clarify that extended basic blocks are abbreviated as EBB.

* Fix typo.

* Fix a typo.

* Fix typos.

* Use the same phrase to indicate scalar-only as other places in the doc.

* Mention that `band_imm` and friends are scalar-only.

And mention that they're equivalent to their respective
non-immediate-form counterparts.
2017-06-22 12:01:32 -07:00
Jakob Stoklund Olesen
b6cff6a98a Move EntityRef and entity_impl! into a new module.
The EntityRef trait is used by more than just the EntityMap now, so it
should live in its own module.

Also move the entity_impl! macro into the new module so it can be used
for defining new entity references anywhere.
2017-06-20 10:16:27 -07:00
Dan Gohman
0c7316ae28 Lint fixes (#99)
* Replace a single-character string literal with a character literal.

* Use is_some() instead of comparing with Some(_).

* Add code-quotes around type names in comments.

* Use !...is_empty() instead of len() != 0.

* Tidy up redundant returns.

* Remove redundant .clone() calls.

* Remove unnecessary explicit lifetime parameters.

* Tidy up unnecessary '&'s.

* Add parens to make operator precedence explicit.

* Use debug_assert_eq instead of debug_assert with ==.

* Replace a &Vec argument with a &[...].

* Replace `a = a op b` with `a op= b`.

* Avoid unnecessary closures.

* Avoid .iter() and .iter_mut() for iterating over containers.

* Remove unneeded qualification.
2017-06-19 16:24:10 -07:00
Aleksey Kuznetsov
3693735874 Implement an iterator over encodings (#96)
* Implement an iterator over encodings

* Implement TargetIsa::legal_encodings

* Exclude non-boolean settings of isa flags bytes

* Address flake8 long line error
2017-06-19 08:52:19 -07:00
Jakob Stoklund Olesen
1dd8d913aa Assign spill slots to spilled values.
As soon as a value is spilled, also assign it to a spill slot.

For now, create a new spill slot for each spilled value. In the future,
values will be sharing spill slots of they are phi-related.
2017-06-16 13:34:18 -07:00
Jakob Stoklund Olesen
ddbf46bef4 Add typedefs for the common entity maps.
The various entity maps in a function end up being referenced in
multiple places, so create typedefs for them.
2017-06-16 13:21:05 -07:00
Jakob Stoklund Olesen
605bda2925 Add a stack frame manager.
Use a new StackSlots struct to keep track of a function's stack slots
instead of just an entity map. This let's us build more internal data
structures for tracking the stack slots if necessary.

Start by adding a make_spill_slot() function that will be used by the
register allocator.
2017-06-16 11:55:44 -07:00
Jakob Stoklund Olesen
7b97933996 Track stack slot kinds.
Add a StackSlotKind enumeration to help keep track of the different
kinds of stack slots supported:

- Incoming and outgoing function arguments on the stack.
- Spill slots and locals.

Change the text format syntax for declaring a stack slot to use a kind
keyword rather than just 'stack_slot'.
2017-06-16 11:01:22 -07:00
Jakob Stoklund Olesen
db62f435f8 Make register copies for incompatible operands.
An instruction may have fixed operand constraints that make it
impossibly to use a single register value to satisfy two at a time.

Detect when the same value is used for multiple fixed register operands
and insert copies during the spilling pass.
2017-06-15 13:12:13 -07:00
Jakob Stoklund Olesen
4503306f0e Add RISC-V encodings for call_indirect. 2017-06-14 16:14:16 -07:00
Jakob Stoklund Olesen
3e1e2b6e5e Add RISC-V encodings for copy instructions. 2017-06-14 15:36:25 -07:00
Jakob Stoklund Olesen
96f1228211 Always call reassign_in for register ABI arguments.
Even if an argument is already in the correct register, make sure that
we detect conflicts by registering the no-op move. This also means that
the ABI argument register won't be turned into a variable for the
solver.
2017-06-14 12:10:57 -07:00
Jakob Stoklund Olesen
897c363714 Update docopt dependency to 0.8.0.
This breaks our depending on two different versions of the regex
library.

This updated docopt uses serde instead of rustc_serialize.
2017-06-14 10:38:06 -07:00
Jakob Stoklund Olesen
454910407f Spill values live across calls.
Calls clobber many registers, so spill everything that is live across a
call for now.

In the future, we may add support for callee-saved registers.
2017-06-14 08:55:01 -07:00
Jakob Stoklund Olesen
0974b4a6e2 Extract spill insertion into a reload::insert_spill function.
Make sure that spill instructions are generated in the same way
everywhere, including adding encoding and updating live ranges.
2017-06-13 15:46:11 -07:00
Jakob Stoklund Olesen
396b998dad Handle ABI arguments correctly in the reload pass.
Values passed as arguments to calls and return instructions may also be
reload candidates.
2017-06-13 15:13:36 -07:00
Jakob Stoklund Olesen
63a372fd80 Basic spilling implementation.
Add a spilling pass which lowers register pressure by assigning SSA
values to the stack. Important missing features:

- Resolve conflicts where an instruction uses the same value more than
  once in incompatible ways.
- Deal with EBB arguments.

Fix bugs in the reload pass exposed by the first test case:

- Create live ranges for temporary registers.
- Set encodings on created spill and fill instructions.
2017-06-13 13:58:20 -07:00
Jakob Stoklund Olesen
9cea092bc3 Track transient register counts in Pressure.
The register pressure tracker now has to separate register counts: base
and transient.

The transient counts are used to track spikes of register pressure, such
as dead defs or temporary registers needed to satisfy instruction
constraints.

The base counts represent long-lived variables.
2017-06-13 13:44:00 -07:00
Jakob Stoklund Olesen
5336bbd4cc Add RISC-V encodings for spill and fill.
Add a Stack() class for specifying operand constraints for values on the
stack.

Add encoding recipes for RISC-V spill and fill instructions. Don't
implement the encoding recipe functions yet since we don't have the
stack slot layout yet.
2017-06-13 13:39:52 -07:00
Jakob Stoklund Olesen
ea8a8a95a8 Remove the ebb_dominates function.
This is now subsumed by the generic 'dominates' function.
2017-06-12 14:59:34 -07:00
Jakob Stoklund Olesen
a1dfd0f06f Generalize DominatorTree::dominates.
This is now a generic function that can test arbitrary combinations of
instructions and EBBs for dominance.

It can handle anything that converts into an expanded program point,
including a ValueDef.

Also fix a bug if the earlier dominates() function which didn't properly
handle block layouts that were not topologically ordered.
2017-06-12 14:52:42 -07:00
Jakob Stoklund Olesen
4f9ff548bd Generalize rpo_cmp to handle all program points.
When comparing instructions in the same EBB, behave like the RPO visits
instructions in program order.

- Add a Layout::pp_ebb() method for convenience. It gets the EBB
  containing any program point.
- Add a conversion from ValueDef to ExpandedProgramPoint so it can be
  used with the rpo_cmp method.
2017-06-12 14:11:15 -07:00
Aleksey Kuznetsov
706eef23d3 Binary function names (#91)
* Function names should start with %

* Create FunctionName from string

* Implement displaying of FunctionName as %nnnn with fallback to #xxxx

* Run rustfmt and fix FunctionName::with_string in parser

* Implement FunctionName::new as a generic function

* Binary function names should start with #

* Implement NameRepr for function name

* Fix examples in docs to reflect that function names start with %

* Rebase and fix filecheck tests
2017-06-10 10:30:37 -07:00