Here's a simple C file with an enum definition and a main function:
enum days {MON, TUE, WED, THU};
int main() {
enum days d;
d = WED;
return 0;
}
It transpiles to the following LLVM IR:
define dso_local i32 @main() #0 {
%1 = alloca i32, align 4
%2 = alloca i32, align 4
store i32 0, i32* %1, align 4
store i32 2, i32* %2, align 4
ret i32 0
}
%2 is evidently the d variable, which gets 2 assigned to it. What does %1 correspond to if zero is returned directly?
The
%1register was generated by clang to handle multiple return statements in a function. Imagine you were writing a function to compute an integer's factorial. Instead of thisYou'd probably do this
Why? Because Clang will insert that
resultvariable that holds the return value for you. Yay. That's the reason for that%1variable. Look at the IR for a slightly modified version of your code.Modified code,
IR,
Now you see
%1making itself useful huh? Most functions with a single return statement will have this variable stripped by one of LLVM's passes.