Toolchain Tips

Table of Contents

1. Toolchain Tips

1.1. toolchain triple

具体可以参考 llvm 的 Triple.cpp

<arch>  - <vendor> - <os>    - <environment> - <object_format>

riscv64 - unknown  - linux   - gnu
mips64  - mti      - windows - musl
x86-64  - unknown                            - elf

1.2. register spill

int foo(int a, int b) {
    int a0 = a + b;
    int a1 = a - b;

    int b0 = a0 + a;
    int b1 = a1 - b;

    int c0 = b0 + a0 + a;
    int c1 = b1 - a1 - b;

    int d0 = c0 + b0 + a0 + a;
    int d1 = c1 - b1 - a1 - b;

    int k = d0 + d1 + a0 + b0;

    return a0 + b0 + c0 + d0 + a1 + b1 + c1 + d1 + k;
}
$> riscv64-unknown-linux-gnu-gcc --version
riscv64-unknown-linux-gnu-gcc () 13.2.0

$> riscv64-unknown-linux-gnu-gcc test.c -O2  -c -S \
  -ffixed-s0 -ffixed-s1 -ffixed-s2 -ffixed-s3 -ffixed-s4 \
  -ffixed-s5 -ffixed-s6 -ffixed-s7 -ffixed-s8 -ffixed-s9 \
  -ffixed-s10 -ffixed-s11 -ffixed-t0 -ffixed-t1 -ffixed-t2 \
  -ffixed-t3 -ffixed-t4 -ffixed-t5 -ffixed-t6

$> cat test.s

foo:
        addw    a2,a0,a1
        addw    a5,a0,a2
        addw    a7,a2,a5
        addi    sp,sp,-16
        addw    a3,a7,a0
        sw      a5,12(sp)    <------ spill
        addw    a5,a5,a3
        addw    a5,a5,a2
        subw    a4,a0,a1
        negw    a6,a1
        addw    a0,a5,a0
        addw    a3,a7,a3
        subw    a5,a6,a4
        addw    a3,a3,a0
        addw    a3,a3,a4
        subw    a5,a5,a4
        subw    a4,a4,a1
        addw    a4,a3,a4
        subw    a3,a6,a1
        subw    a5,a5,a1
        addw    a4,a4,a3
        addw    a0,a0,a5
        addw    a4,a4,a5
        lw      a5,12(sp)
        addw    a0,a0,a2
        addw    a0,a0,a5
        addw    a0,a4,a0
        addi    sp,sp,16
        jr      ra

为了造成 spill, 需要让每个变量的使用范围 (live interval) 越大越好, 所以让每个局部变量都依赖前面所有的中间结果, 让 live interval 变成类似这种形式:

-------------------------
  -----------------------
    ---------------------
      -------------------
        -----------------
          ---------------

所以为了避免 spill, 局部变量的作用域需要越小越好.

Author: [email protected]
Date: 2024-01-16 Tue 17:04
Last updated: 2024-01-26 Fri 16:49

知识共享许可协议