microsoft / llvm-mctoll

llvm-mctoll
Other
806 stars 123 forks source link

[X86-64] If a BB is not terminated, insert an unreachable instruction #152

Closed martin-fink closed 2 years ago

martin-fink commented 2 years ago

If a basic block was unterminated, mctoll would crash while raising it, as subsequent passes would assume that every BB has a terminator instruction. Example:

void exit_with_msg(const char *msg) {
  printf("%s\n", msg);
  exit(0);
}

int main() {
  exit_with_msg("Bye!");
  return 0;
}

If exit_with_msg is not inlined, the main function is compiled to the following assembly code:

main:                                   # @main
        push    rax
        mov     edi, offset .L.str.1
        call    exit_with_msg

With this PR, mctoll generates the following LLVM code instead of crashing:

define dso_local void @main() {
entry:
  %RSP_P.0 = alloca i64, align 1
  store i64 3735928559, i64* %RSP_P.0, align 8
  %0 = ptrtoint i8* getelementptr inbounds ([26 x i8], [26 x i8]* @rodata_11, i32 0, i32 21) to i64, !ROData_Index !1
  call void @exit_with_msg(i64 %0)
  unreachable
}
bharadwajy commented 2 years ago

Thanks for the PR. Pushed the changes.