ControlCplusControlV/Scribe

Lifetime Management

ControlCplusControlV opened this issue · 1 comments

Lifetime Management

Lifetime Management

  • If a variable is not going to be used after a given expression, no need to save it to memory.
  • If a variable is updated several times in a row, no need to save intermediate results to memory.

Test Cases

  1. Input Yul
{
    let a := 700
    let b := 100
    let c := 200
    let d := add(a, c)
}

  1. Assembly Output

// a gets pushed to the stack
// b gets pushed to the stack
// The stack is full, so the top value will attempt to save to memory
//Since b is the top value, the optimizer will check if b is used later
//if so, b gets saved to memory, if not, b gets dropped
//then c gets pushed to the stack
//a and c are added
  1. Input Yul
let c = 1000
let b = 15
let a = 6
    
let d = mul(a, b)
let e = mul(d, a)
  1. Assembly Output
// Assembly output should not put d back on the stack,
//  because it is immediately used after, since in 
//  multiplication the intermediate value 
//  must be stored in memory.