org.jikesrvm.compilers.opt.controlflow
Class MIRBranchOptimizations

java.lang.Object
  extended by org.jikesrvm.compilers.opt.driver.CompilerPhase
      extended by org.jikesrvm.compilers.opt.controlflow.BranchOptimizationDriver
          extended by org.jikesrvm.compilers.opt.controlflow.MIRBranchOptimizations

public final class MIRBranchOptimizations
extends BranchOptimizationDriver

Perform simple peephole optimizations for MIR branches.


Field Summary
 
Fields inherited from class org.jikesrvm.compilers.opt.driver.CompilerPhase
container
 
Constructor Summary
MIRBranchOptimizations(int level)
           
 
Method Summary
private  void flipConditionalBranch(Instruction cb)
          Flip a conditional branch and remove the trailing goto.
private  boolean isFlipCandidate(Instruction cb, Instruction target)
          Is a conditional branch a candidate to be flipped?
protected  boolean optimizeBranchInstruction(IR ir, Instruction s, BasicBlock bb)
          This method actually does the work of attempting to peephole optimize a branch instruction.
private  boolean processCondBranch(IR ir, Instruction cb, BasicBlock bb)
          Perform optimizations for a conditional branch.
private  boolean processGoto(IR ir, Instruction g, BasicBlock bb)
          Perform optimizations for an unconditonal branch.
private  boolean processTwoTargetConditionalBranch(IR ir, Instruction cb, BasicBlock bb)
          Perform optimizations for a two way conditional branch.
 
Methods inherited from class org.jikesrvm.compilers.opt.controlflow.BranchOptimizationDriver
applyPeepholeBranchOpts, firstLabelFollowing, firstRealInstructionFollowing, getName, maximizeBasicBlocks, newExecution, perform, perform, printingEnabled, removeUnreachableCode, shouldPerform
 
Methods inherited from class org.jikesrvm.compilers.opt.driver.CompilerPhase
dumpIR, dumpIR, getClassConstructor, getCompilerPhaseConstructor, getCompilerPhaseConstructor, performPhase, reportAdditionalStats, setContainer, verify
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MIRBranchOptimizations

public MIRBranchOptimizations(int level)
Parameters:
level - the minimum optimization level at which the branch optimizations should be performed.
Method Detail

optimizeBranchInstruction

protected boolean optimizeBranchInstruction(IR ir,
                                            Instruction s,
                                            BasicBlock bb)
This method actually does the work of attempting to peephole optimize a branch instruction. See Muchnick ~p.590

Specified by:
optimizeBranchInstruction in class BranchOptimizationDriver
Parameters:
ir - the containing IR
s - the branch instruction to optimize
bb - the containing basic block
Returns:
true if an optimization was applied, false otherwise

processGoto

private boolean processGoto(IR ir,
                            Instruction g,
                            BasicBlock bb)
Perform optimizations for an unconditonal branch.

Patterns:

    1)      GOTO A       replaced by  GOTO B
         A: GOTO B

    2)   GOTO next instruction eliminated
    3)      GOTO A       replaced by  GOTO B
         A: LABEL
            BBEND
         B:
 

 

Precondition: MIR_Branch.conforms(g)

Parameters:
ir - governing IR
g - the instruction to optimize
bb - the basic block holding g
Returns:
true if made a transformation

processCondBranch

private boolean processCondBranch(IR ir,
                                  Instruction cb,
                                  BasicBlock bb)
Perform optimizations for a conditional branch.
 1)   IF .. GOTO A          replaced by  IF .. GOTO B
      ...
   A: GOTO B
 2)   conditional branch to next instruction eliminated
 3)   IF (condition) GOTO A  replaced by  IF (!condition) GOTO B
      GOTO B                           A: ...
   A: ...
 4)   IF .. GOTO A       replaced by  IF .. GOTO B
   A: LABEL
      BBEND
   B:
 5)  fallthrough to a goto: replicate goto to enable other optimizations.
 

Precondition: MIR_CondBranch.conforms(cb)

Parameters:
ir - the governing IR
cb - the instruction to optimize
bb - the basic block holding if
Returns:
true iff made a transformation

processTwoTargetConditionalBranch

private boolean processTwoTargetConditionalBranch(IR ir,
                                                  Instruction cb,
                                                  BasicBlock bb)
Perform optimizations for a two way conditional branch.
 1)   IF .. GOTO A          replaced by  IF .. GOTO B
      ...
   A: GOTO B
 2)   conditional branch to next instruction eliminated
 3)   IF .. GOTO A       replaced by  IF .. GOTO B
   A: LABEL
      BBEND
   B:
 4)  fallthrough to a goto: replicate goto to enable other optimizations.
 

Precondition: MIR_CondBranch2.conforms(cb)

Parameters:
ir - the governing IR
cb - the instruction to optimize
bb - the basic block holding if
Returns:
true iff made a transformation

isFlipCandidate

private boolean isFlipCandidate(Instruction cb,
                                Instruction target)
Is a conditional branch a candidate to be flipped? See comment 3) of processCondBranch

Precondition: MIR_CondBranch.conforms(cb)

Parameters:
cb - the conditional branch instruction
target - the target instruction (real instruction) of the conditional branch
Returns:
boolean result

flipConditionalBranch

private void flipConditionalBranch(Instruction cb)
Flip a conditional branch and remove the trailing goto. See comment 3) of processCondBranch

Precondition isFlipCandidate(cb)

Parameters:
cb - the conditional branch instruction