Program: 149912
Project: 149912
Buggy source:
/** 
 * Perform optimizations for a conditional branch. <pre> 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) special case to generate Boolean compare opcode 5) special case to generate conditional move sequence 6)   IF .. GOTO A       replaced by  IF .. GOTO B A: LABEL BBEND B: 7)  fallthrough to a goto: replicate goto to enable other optimizations. </pre> <p> Precondition: IfCmp.conforms(cb)
 * @param ir the governing IR
 * @param cb the instruction to optimize
 * @param bb the basic block holding if
 * @return true iff made a transformation
 */
private boolean processConditionalBranch(OPT_IR ir,OPT_Instruction cb,OPT_BasicBlock bb){
  OPT_BasicBlock targetBlock=cb.getBranchTarget();
  if (targetBlock.getLandingPad())   return false;
  OPT_Instruction targetLabel=targetBlock.firstInstruction();
  OPT_Instruction targetInst=firstRealInstructionFollowing(targetLabel);
  if (targetInst == null || targetInst == cb) {
    return false;
  }
  boolean endsBlock=cb.nextInstructionInCodeOrder().operator() == BBEND;
  if (endsBlock) {
    OPT_Instruction nextLabel=firstLabelFollowing(cb);
    if (targetLabel == nextLabel) {
      cb.remove();
      return true;
    }
    OPT_Instruction nextI=firstRealInstructionFollowing(nextLabel);
    if (nextI != null && Goto.conforms(nextI)) {
      cb.insertAfter(nextI.copyWithoutLinks());
      bb.recomputeNormalOut(ir);
      return true;
    }
  }
  if (generateBooleanCompare(ir,bb,cb,targetBlock)) {
    return true;
  }
  if (generateCondMove(ir,bb,cb)) {
    return true;
  }
  OPT_BasicBlock fallThrough=bb.getFallThroughBlock();
  if (fallThrough != null) {
    OPT_Instruction fallThroughInstruction=fallThrough.firstRealInstruction();
    if ((fallThroughInstruction != null) && Goto.conforms(fallThroughInstruction)) {
      bb.appendInstruction(fallThroughInstruction.copyWithoutLinks());
      bb.recomputeNormalOut(ir);
    }
  }
  if (Goto.conforms(targetInst)) {
    OPT_Instruction target2=firstRealInstructionFollowing(targetInst.getBranchTarget().firstInstruction());
    if (target2 == targetInst) {
      return false;
    }
    IfCmp.setTarget(cb,(OPT_BranchOperand)Goto.getTarget(targetInst).copy());
    bb.recomputeNormalOut(ir);
    return true;
  }
  if (targetBlock.isEmpty()) {
    OPT_BasicBlock nextBlock=targetBlock.getFallThroughBlock();
    IfCmp.setTarget(cb,nextBlock.makeJumpTarget());
    bb.recomputeNormalOut(ir);
    return true;
  }
  if (isFlipCandidate(cb,targetInst)) {
    flipConditionalBranch(cb);
    bb.recomputeNormalOut(ir);
    return true;
  }
  return false;
}

Fixed version (ground truth):
/** 
 * Perform optimizations for a conditional branch. <pre> 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) special case to generate Boolean compare opcode 5) special case to generate conditional move sequence 6)   IF .. GOTO A       replaced by  IF .. GOTO B A: LABEL BBEND B: 7)  fallthrough to a goto: replicate goto to enable other optimizations. </pre> <p> Precondition: IfCmp.conforms(cb)
 * @param ir the governing IR
 * @param cb the instruction to optimize
 * @param bb the basic block holding if
 * @return true iff made a transformation
 */
private boolean processConditionalBranch(OPT_IR ir,OPT_Instruction cb,OPT_BasicBlock bb){
  OPT_BasicBlock targetBlock=cb.getBranchTarget();
  if (targetBlock.getLandingPad())   return false;
  OPT_Instruction targetLabel=targetBlock.firstInstruction();
  OPT_Instruction targetInst=firstRealInstructionFollowing(targetLabel);
  if (targetInst == null || targetInst == cb) {
    return false;
  }
  boolean endsBlock=cb.nextInstructionInCodeOrder().operator() == BBEND;
  if (endsBlock) {
    OPT_Instruction nextLabel=firstLabelFollowing(cb);
    if (targetLabel == nextLabel) {
      cb.remove();
      return true;
    }
    OPT_Instruction nextI=firstRealInstructionFollowing(nextLabel);
    if (nextI != null && Goto.conforms(nextI)) {
      OPT_BasicBlock gotoTarget=nextI.getBranchTarget();
      OPT_Instruction gotoLabel=gotoTarget.firstInstruction();
      OPT_Instruction gotoInst=firstRealInstructionFollowing(gotoLabel);
      if (gotoInst != nextI) {
        cb.insertAfter(nextI.copyWithoutLinks());
        bb.recomputeNormalOut(ir);
        return true;
      }
    }
  }
  if (generateBooleanCompare(ir,bb,cb,targetBlock)) {
    return true;
  }
  if (generateCondMove(ir,bb,cb)) {
    return true;
  }
  OPT_BasicBlock fallThrough=bb.getFallThroughBlock();
  if (fallThrough != null) {
    OPT_Instruction fallThroughInstruction=fallThrough.firstRealInstruction();
    if ((fallThroughInstruction != null) && Goto.conforms(fallThroughInstruction)) {
      bb.appendInstruction(fallThroughInstruction.copyWithoutLinks());
      bb.recomputeNormalOut(ir);
    }
  }
  if (Goto.conforms(targetInst)) {
    OPT_Instruction target2=firstRealInstructionFollowing(targetInst.getBranchTarget().firstInstruction());
    if (target2 == targetInst) {
      return false;
    }
    IfCmp.setTarget(cb,(OPT_BranchOperand)Goto.getTarget(targetInst).copy());
    bb.recomputeNormalOut(ir);
    return true;
  }
  if (targetBlock.isEmpty()) {
    OPT_BasicBlock nextBlock=targetBlock.getFallThroughBlock();
    IfCmp.setTarget(cb,nextBlock.makeJumpTarget());
    bb.recomputeNormalOut(ir);
    return true;
  }
  if (isFlipCandidate(cb,targetInst)) {
    flipConditionalBranch(cb);
    bb.recomputeNormalOut(ir);
    return true;
  }
  return false;
}