7.6. Exercises

  1. Examine the code to B::Bblock. As the documentation says, " A basic block is a series of operations which is known to execute from start to finish, with no possiblity of branching or halting."

    If there are any ops following a return operation inside a basic block, they will never be executed and can be considered dead code. Write a module which detects and reports this type of dead code.

  2. Add line numbers to your dead code reporting module by examining the nearest CV.

  3. Extend the module to report unreachable code after unconditional next and last statements.

  1. Write a module which tracks variable access and reports dead code based on it. For instance, Perl can optimize

    #!/usr/bin/perl
    if (0) {
        ...
    }	    
    	    
    away to nothing, but cannot currently remove
    #!/usr/bin/perl
    $foo=0;
    if ($foo) { 
        ...
    }	      

  2. Write a module which describes other optimizations that can be made. For instance, given

    $a = $x * $y;
    $b = $x * $y;
    optimize to
    $b = $a = $x * $y;