tyfkda / xcc

Standalone C compiler/assembler/linker/libc for x86-64/aarch64/riscv64/wasm
https://tyfkda.github.io/xcc/
MIT License
197 stars 14 forks source link

Remove redundant return/br on wcc #129

Closed tyfkda closed 11 months ago

tyfkda commented 11 months ago
int foo(int x) {
    char arr[100];
    if (x >= 0)
        return x;
    else
        return -x;
}
;;=== 0x50: CODE, len=53
(func $foo (;0;) (type 0)
  (local i32)
  global.get 0
  local.set 1
  global.get 0
  i32.const 104
  i32.sub
  global.set 0
  block (result i32)
    local.get 0
    i32.const 0
    i32.ge_s
    if
      local.get 0
      br 1   ;; <= here
    else
      i32.const 0
      local.get 0
      i32.sub
      br 1   ;; <= here
    end
    unreachable   ;; <= here
  end
  local.get 1
  global.set 0
)

return statements in last if statement are not treated as at function-end, so return or br instruction is put to exit from function.