Ticket #1512 (closed bug: fixed)

Opened 6 years ago

Last modified 4 years ago

NCG to handle cyclic fixups for rematching register assignment for jump blocks

Reported by: guest Owned by:
Priority: high Milestone: 6.8.1
Component: Compiler Version:
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Difficulty: Moderate (less than a day)
Test Case: Blocked By:
Blocking: Related Tickets:

Description

joinToTargets in  RegisterAlloc.hs generates a jump to a particular target. On the first jump to a target, the virtual register assignment is associated with this target. All following jumps to the target must ensure that the current virtual register assignment matches that one of the target. If that's not the case, fixup code is generated before the branch instruction.

In rare cases, that occur when compiling the RTS on i386 in -fPIC -dynamic mode we have to generate cyclic fixup code. Here is one issue that comes from compiling PrimOps?.cmm:

Assignment of target:

  • virtual register A in real register A
  • virtual register B in real register B.

Current assignment:

  • virtual register A in real register B
  • virtual register B in real register A.

Basically, we have to move A to B, and B to A. There is no move sequence that allows this without using a scratch register.  RegisterAlloc.hs detects this by using stronglyConnCompR in joinToTargets (Line 869). At the moment, it does not provide a solution for this, it just fails.

My trivial approach would be:

handleComponent (CyclicSCC ((vreg,src,dsts):rest)) 
  = let sccs = stronglyConnCompR rest
    in [makeMove vreg src (InMem empty_spill)] ++
       concatMap handleComponent sccs ++
       map (makeMove vreg (InMem empty_spill)) dsts

I'm only having trouble to find empty_spill, namely an empty spill slot on the stack. I have no clear idea what the (simulated) stack pointer is pointing at and whether it is save to assume that below (above?) is scratch space I can abuse for such a task.

Change History

Changed 6 years ago by duncan

Basically, we have to move A to B, and B to A. There is no move sequence that allows this without using a scratch register.

As Ian pointed out, actually there is!

x := x `xor` y;
y := x `xor` y;
x := x `xor` y

This might give you a cheaper way out than spilling and using a scratch register.

Changed 6 years ago by guest

I already considered xor swapping (that's why I restricted my claim to MOV instructions).  http://en.wikipedia.org/wiki/XOR_swap_algorithm advises against using the XOR swap trick on modern CPUs and claims it'd be more inefficient than a scratch location.

Also using pure swapping doesn't sound very appealing to me. Consider a more complicated cycle involving more than just 2 nodes. The return value of the SCC doesn't give you the cycle, but a set of nodes that contains a cycle. It might contain leaves not involved in the cycle (as far as I understood it). How would you swap (InReg 1) with (InReg 2 and InMem 3)?

Changed 6 years ago by igloo

  • priority changed from normal to high
  • milestone set to 6.8

This is something we need to fix before 6.8, right?

Changed 6 years ago by simonmar

  • status changed from new to closed
  • resolution set to fixed

Already done:

Sat Jul 14 09:32:41 BST 2007  Clemens Fruhwirth <clemens@endorphin.org>
  * joinToTargets to emit fixup code even when movement graph contains cycles

Changed 6 years ago by igloo

  • milestone changed from 6.8 branch to 6.8.1

Changed 5 years ago by simonmar

  • architecture changed from Unknown to Unknown/Multiple

Changed 5 years ago by simonmar

  • os changed from Unknown to Unknown/Multiple

Changed 4 years ago by simonmar

  • difficulty changed from Moderate (1 day) to Moderate (less than a day)
Note: See TracTickets for help on using tickets.