clang++ on unreachable code

Hi All,

I had a question on how clang frontend treats unreachable code. I am using llvm-2.9 and clang++ and the flags used to compile the code are –O0 –g –Wall.

The code that I compiled and the corresponding bitcode is given below.

int main ()

{

int a=100;

return 1;

a++;

return a;

}

define i32 @main() nounwind {

bb:

%tmp = alloca i32, align 4

%a = alloca i32, align 4

store i32 0, i32* %tmp

call void @llvm.dbg.declare(metadata !{i32* %a}, metadata !6), !dbg !8

store i32 100, i32* %a, align 4, !dbg !9

ret i32 1, !dbg !10

}

Note that the code corresponding to a++; and return a; statements are not generated by frontend. Looks like the frontend detected that a++ and return a; to be dead code. My question is under what circumstances does the frontend do this optimization and is it possible to tell the frontend to NOT do the optimization?.

Sreekumar

PS: I modified the code to the following to add an IF statement as shown below.

int main ()

{

int a=100;

if(a) return 1;

a++;

return a;

}

The resulting code was still logically the same as the code given above. For this code, clang++ did generate the code corresponding to the dead code.

The pruning optimizations clang uses are extremely simple; the exact
details aren't guaranteed, but we basically only prune code after a
return statement or noreturn call and code guarded by a if/switch with
an operand that is obviously constant.

There isn't any flag to change this because it doesn't really make
sense; dead code doesn't do anything useful. What are you trying to
do?

-Eli

The pruning optimizations clang uses are extremely simple; the exact
details aren't guaranteed, but we basically only prune code after a
return statement or noreturn call and code guarded by a if/switch with
an operand that is obviously constant.

There isn't any flag to change this because it doesn't really make
sense; dead code doesn't do anything useful. What are you trying to
do?

Thanks Eli for answering the question. I am porting an analysis framework from CIL to LLVM. The analysis framework can report such dead code and I was trying to replicate that same using LLVM frontend. I was not able to replicate for this example as Clang already removes such code before it gets to my tool. But as you mentioned, since the pruning is simple, it is not a big problem for my application in general.

Thanks again.
Sreekumar