Skip to content

cmd/compile: make rulegen produce less verbose code #33644

@mvdan

Description

@mvdan

This is a continuation to #30810, once the rewrite has been merged. A number of ideas will be proposed below. The purpose of this is to:

  • Produce less lines of code, reducing the size of diffs and files on disk
  • Produce simpler code, making it more readable to humans
  • Hopefully reduce compile time of the generated code slightly

Here are my ideas so far:

  1. Simplifying of boolean expressions. For example, turn !(d == 32-c) into d != 32-c.

  2. Joining of contiguous break conditions, such as:

// verbose
if v_0.Op != OpAMD64SHLLconst {
        break
}
if v_0.AuxInt != 3 {
        break
}

// simpler
if v_0.Op != OpAMD64SHLLconst || v_0.AuxInt != 3 {
        break
}
  1. Removing of uninteresting lines, such as the cond line here:
// match: (ADDL x (SHLLconst [3] y))
// cond:
// result: (LEAL8 x y)
  1. Move declarations closer to their uses, reducing the number of variables in scope in some cases, such as:
for {
        carry := v.Args[2]
        x := v.Args[0] // Not actually used until the very end!
        v_1 := v.Args[1]
        if v_1.Op != OpAMD64MOVQconst {
                break
        }
        c := v_1.AuxInt
        if !(is32Bit(c)) {
                break
        }
        v.reset(OpAMD64ADCQconst)
        v.AuxInt = c
        v.AddArg(x)
        v.AddArg(carry)
        return true
}
  1. Reorder slice accesses to reduce the amount of manual bounds checks speed-ups:
// verbose
_ = v.Args[1]
x := v.Args[0]
v_1 := v.Args[1]

// simpler
v_1 := v.Args[1]
x := v.Args[0]

These are the ideas I have so far. If we think any of them would reduce readability, they can be dropped. I'm also open to more ideas and suggestions, of course. I'd send these as separate CLs so we can measure their impact on the generated code separately.

/cc @josharian @randall77 @laboger @mundaym @cherrymui

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions