This is an archive of the discontinued LLVM Phabricator instance.

If a conditional branch jumps to the same target, remove the condition
ClosedPublic

Authored by reames on Mar 10 2015, 11:58 AM.

Details

Summary

Given much of inst combine is restricted to instructions which have one use, getting rid of a use on the condition can help the effectiveness of the optimizer. Also, it allows the condition to potentially be deleted by instcombine rather than waiting for another pass.

I noticed this completely by accident in another test case. It's not anything that actually came from a real workload.

p.s. We should probably do the same thing for switch instructions.

Diff Detail

Repository
rL LLVM

Event Timeline

reames updated this revision to Diff 21614.Mar 10 2015, 11:58 AM
reames retitled this revision from to If a conditional branch jumps to the same target, remove the condition.
reames updated this object.
reames edited the test plan for this revision. (Show Details)
reames added a subscriber: Unknown Object (MLST).
hfinkel accepted this revision.Mar 10 2015, 1:43 PM
hfinkel added a reviewer: hfinkel.
hfinkel added a subscriber: hfinkel.

LGTM. But, could you also please fix llvm::ConstantFoldTerminator in lib/Transforms/Utils/Local.cpp so that it handles the undef condition and transforms the branch with the undef condition into an unconditional branch. If we don't want to do that, then we should just pick true/false here instead of using undef.

This revision is now accepted and ready to land.Mar 10 2015, 1:43 PM

@Hal - Unless I'm missing something, this is already handled in
ConstantFoldTerminator.

if (Dest2 == Dest1) {       // Conditional branch to same location?
  // This branch matches something like this:
  //     br bool %cond, label %Dest, label %Dest
  // and changes it into:  br label %Dest

  // Let the basic block know that we are letting go of one copy of it.
  assert(BI->getParent() && "Terminator not inserted in block!");
  Dest1->removePredecessor(BI->getParent());

  // Replace the conditional branch with an unconditional one.
  Builder.CreateBr(Dest1);
  Value *Cond = BI->getCondition();
  BI->eraseFromParent();
  if (DeleteDeadConditions)
    RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
  return true;
}

Given this, I plan to submit as is.

This revision was automatically updated to reflect the committed changes.