1 Calling Conventions Architecture Review: Caller- and Callee-Saved - - PowerPoint PPT Presentation

1
SMART_READER_LITE
LIVE PREVIEW

1 Calling Conventions Architecture Review: Caller- and Callee-Saved - - PowerPoint PPT Presentation

Prelude Register Allocation II The Sears Tower in Chicago was the worlds Last time tallest building from 1974 until 1998. Register allocation Global allocation via graph coloring Today More register allocation Fun facts


slide-1
SLIDE 1

1

CS553 Lecture Register Allocation II 1

Prelude

The Sears Tower in Chicago was the world’s tallest building from 1974 until 1998. Fun facts

– The Sears Tower contains enough concrete to build an eight-lane, five- mile-long highway, enough steel to build 50,000 automobiles, and enough telephone wiring to wrap around the world 1.75 times. – At the very top of the building, the maximum wind drift is just one foot. – Six roof-mounted robotic window-washing machines clean all 16,1000 windows on the Sears tower. – My son Kevin visited the “Serious” tower no less than 5 times during the two years we lived in Chicago.

CS553 Lecture Register Allocation II 2

Register Allocation II

Last time

– Register allocation – Global allocation via graph coloring

Today

– More register allocation – Allocation across procedure calls

CS553 Lecture Register Allocation II 3

Interference Graph Allocators

Chaitin Briggs

CS553 Lecture Register Allocation II 4

Register Allocation and Procedure Calls

Problem

– Register values may change across procedure calls – The allocator must be sensitive to this

Two approaches

– Work within a well-defined calling convention – Use interprocedural allocation (not covering this)

slide-2
SLIDE 2

2

CS553 Lecture Register Allocation II 5

Calling Conventions

Goals

– Fast calls (pass arguments in registers, minimal register saving/restoring) – Language-independent – Support debugging, profiling, garbage collection, etc.

Complicating Issues

– Varargs – Passing/returning aggregates – Exceptions, non-local returns – setjmp()/longjmp()

CS553 Lecture Register Allocation II 6

Architecture Review: Caller- and Callee-Saved Registers

Partition registers into two categories

– Caller-saved – Callee-saved

Caller-saved registers

– Caller must save/restore these registers when live across call – Callee is free to use them

Example foo()

{ rcaller = 4 save rcaller goo() restore rcaller rcaller? } goo() { rcaller = 99 } goo() is free to modify rcaller caller callee

CS553 Lecture Register Allocation II 7

Architecture Review: Caller- and Callee-Saved Registers

Callee-saved registers

– Callee must save/restore these registers when it uses them – Caller expects callee to not change them

Example

foo() { rcallee = 4 goo() rcallee? } goo() { save rcallee rcallee = 99 restore rcallee } goo() promises not to modify rcallee caller callee

CS553 Lecture Register Allocation II 8

Architectures with Callee and Caller Registers

SPARC

– hardware-saved %i0-%i7, %o0-%o8

Alpha

– 7 callee-saved out of 32 registers

MIPS

– caller-saved: $t0-$t9, $a0-$a3, $v0-$v1 – callee-saved: $s0-$s7, $ra, $fp

PPC

– 18 callee-saved – 14 caller-saved

StarCore EABI

– 4 callee-saved – 28 caller-saved

slide-3
SLIDE 3

3

CS553 Lecture Register Allocation II 9

Register Allocation and Calling Conventions

Insensitive register allocation

– Save all live caller-saved registers before call; restore after – Save all used callee-saved registers at procedure entry; restore at return – Suboptimal

Sensitive register allocation

– Encode calling convention constraints in the IR and interference graph – How? A variable that is not live across calls should go in caller-saved registers A variable that is live across multiple calls should go in callee-saved registers

foo() { t = … … = t s = … f() g() … = s }

Use precolored nodes

CS553 Lecture Register Allocation II 10

r1 r2 s1 f3 s2 s4 s3

floating point integer

Precolored Nodes

Add architectural registers to interference graph

– Precolored (mutually interfering) – Not simplifiable – Not spillable

Express allocation constraints

– Integers usually can’t be stored in floating point registers – Some instructions can only store result in certain registers – Caller-saved and callee-saved registers. . .

floating point integer

CS553 Lecture Register Allocation II 11

Precolored Nodes and Calling Conventions

Callee-saved registers

– Treat entry as def of all callee-saved registers – Treat exit as use of them all – Allocator must “spill” callee-saved registers to use them

Caller-saved registers

– Variables live across call interfere with all caller-saved registers

foo() { def(r3) use(r3) }

Live range of callee-saved registers

CS553 Lecture Register Allocation II 12

Example

foo(): def(r3) t1 := r3 a := ... b := ... ... a ... call goo ... b ... r3 := t1 use(r3) return

r1 r3 t1

r1, r2 caller-saved r3 callee-saved

b a r2

slide-4
SLIDE 4

4

CS553 Lecture Register Allocation II 13

MiniJava compiler, Tiger pg. 198, Ch 11, Ch 12

Callee-saved registers

– MipsFrame.java, calleeSaves = { RA, S0, S1, S2, S3, S4, S5, S6, S7}, – In MipsFrameRegAlloc.java procEntryExit1 – at entry, copy calleeSaves into temporaries – at end, copy from temporaries back into calleeSaves – In MipsFrameRegAlloc.java procEntryExit2 – at entry, could put a source statement with defs, but is not necessary – at end, already inserting a sink statement that uses all calleeSaves

Caller-saved registers

– MipsFrame.java, callerSaves = {T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, V0, V1}; – Put them all in destlist for the function call, see Codegen.java call to frame.callDefs() – Put use in sink statement so they are live from call to end of procedure

Special registers

– Cannot use for register allocation, might want to make FP (which is S8) special – In MipsFrame.java procEntryExit2 put uses of the special registers in sink stmt

Argument registers can also be used as caller-saved registers

– Codegen.java pushes all arguments onto the stack right now

CS553 Lecture Register Allocation II 14

Tradeoffs

Callee-saved registers

+ Decreases code size: one procedure body may have multiple calls + Small procedures tend to need fewer registers than large ones; callee-save makes sense because procedure sizes are shrinking − May increase execution time: For long-lived variables, may save and restore registers multiple times, once for each procedure, instead of a single end-to-end save/restore

The larger “problem”

– We’re making local decisions for policies that require global information

CS553 Lecture Register Allocation II 15

Concepts

Register allocation and procedure calls Calling conventions

– Caller- vs. callee-saved registers – Precoloring

CS553 Lecture Register Allocation II 16

Next Time

Reading – Make sure to read the Briggs paper Lecture – Register Allocation III

slide-5
SLIDE 5

5

CS553 Lecture Register Allocation II 17

Problem with Callee-Saved Registers

Run-time systems (e.g., setjmp()/longjmp() and debuggers) need to

know register values in any stack frame – Caller-saved registers are on stack frame at known location – Callee-saved registers?

F2: save r1,r2 F4: save r3 F3: F1:

r1, r2 caller-saved r3 callee-saved