[cfe-dev] clang++ on unreachable code

Kodakara, Sreekumar V sreekumar.v.kodakara at intel.com
Mon Jun 20 12:02:04 PDT 2011


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110620/7524818c/attachment.html>


More information about the cfe-dev mailing list