X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FCodeGen%2FBranchFolding.cpp;h=d25152b1e436ca5ccd62dd36d1267a48172b40ba;hb=08ada178b8a9f2b380fb6c2bc2693c615532278e;hp=4bf943495656f3d27fa55b5d32bd13bf84c0ada7;hpb=0ffa3de9e9b342c0fb529853e14632c66ec9725f;p=oota-llvm.git diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp index 4bf94349565..d25152b1e43 100644 --- a/lib/CodeGen/BranchFolding.cpp +++ b/lib/CodeGen/BranchFolding.cpp @@ -27,6 +27,7 @@ #include "llvm/Target/TargetRegisterInfo.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/STLExtras.h" @@ -191,14 +192,14 @@ bool BranchFolder::runOnMachineFunction(MachineFunction &MF) { for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; I++) { MachineBasicBlock *MBB = I, *TBB = 0, *FBB = 0; SmallVector Cond; - if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond)) + if (!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond, true)) EverMadeChange |= MBB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); EverMadeChange |= OptimizeImpDefsBlock(MBB); } RS = RegInfo->requiresRegisterScavenging(MF) ? new RegScavenger() : NULL; - MMI = getAnalysisToUpdate(); + MMI = getAnalysisIfAvailable(); bool MadeChangeThisIteration = true; while (MadeChangeThisIteration) { @@ -434,7 +435,7 @@ static void FixTail(MachineBasicBlock* CurMBB, MachineBasicBlock *SuccBB, MachineBasicBlock *TBB = 0, *FBB = 0; SmallVector Cond; if (I != MF->end() && - !TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond)) { + !TII->AnalyzeBranch(*CurMBB, TBB, FBB, Cond, true)) { MachineBasicBlock *NextBB = I; if (TBB == NextBB && !Cond.empty() && !FBB) { if (!TII->ReverseBranchCondition(Cond)) { @@ -461,9 +462,9 @@ static bool MergeCompare(const std::pair &p, // _GLIBCXX_DEBUG checks strict weak ordering, which involves comparing // an object with itself. #ifndef _GLIBCXX_DEBUG - assert(0 && "Predecessor appears twice"); + llvm_unreachable("Predecessor appears twice"); #endif - return(false); + return false; } } @@ -711,7 +712,7 @@ bool BranchFolder::TailMergeBlocks(MachineFunction &MF) { continue; MachineBasicBlock *TBB = 0, *FBB = 0; SmallVector Cond; - if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond)) { + if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond, true)) { // Failing case: IBB is the target of a cbr, and // we cannot reverse the branch. SmallVector NewCond(Cond); @@ -845,7 +846,7 @@ bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB, bool BranchFolder::CanFallThrough(MachineBasicBlock *CurBB) { MachineBasicBlock *TBB = 0, *FBB = 0; SmallVector Cond; - bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond); + bool CurUnAnalyzable = TII->AnalyzeBranch(*CurBB, TBB, FBB, Cond, true); return CanFallThrough(CurBB, CurUnAnalyzable, TBB, FBB, Cond); } @@ -892,8 +893,24 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { while (!MBB->pred_empty()) { MachineBasicBlock *Pred = *(MBB->pred_end()-1); Pred->ReplaceUsesOfBlockWith(MBB, FallThrough); + // If this resulted in a predecessor with true and false edges + // both going to the fallthrough block, clean up; + // BranchFolding doesn't like this. + MachineBasicBlock::succ_iterator SI = Pred->succ_begin(); + bool found = false; + while (SI != Pred->succ_end()) { + if (*SI == FallThrough) { + if (!found) { + found = true; + ++SI; + } else { + SI = Pred->removeSuccessor(SI); + } + } else { + ++SI; + } + } } - // If MBB was the target of a jump table, update jump tables to go to the // fallthrough instead. MBB->getParent()->getJumpTableInfo()-> @@ -910,7 +927,7 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { MachineBasicBlock *PriorTBB = 0, *PriorFBB = 0; SmallVector PriorCond; bool PriorUnAnalyzable = - TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond); + TII->AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, true); if (!PriorUnAnalyzable) { // If the CFG for the prior block has extra edges, remove them. MadeChange |= PrevBB.CorrectExtraCFGEdges(PriorTBB, PriorFBB, @@ -1023,7 +1040,7 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { // Analyze the branch in the current block. MachineBasicBlock *CurTBB = 0, *CurFBB = 0; SmallVector CurCond; - bool CurUnAnalyzable = TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond); + bool CurUnAnalyzable= TII->AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond, true); if (!CurUnAnalyzable) { // If the CFG for the prior block has extra edges, remove them. MadeChange |= MBB->CorrectExtraCFGEdges(CurTBB, CurFBB, !CurCond.empty()); @@ -1092,6 +1109,21 @@ void BranchFolder::OptimizeBlock(MachineBasicBlock *MBB) { } else { DidChange = true; PMBB->ReplaceUsesOfBlockWith(MBB, CurTBB); + // If this change resulted in PMBB ending in a conditional + // branch where both conditions go to the same destination, + // change this to an unconditional branch (and fix the CFG). + MachineBasicBlock *NewCurTBB = 0, *NewCurFBB = 0; + SmallVector NewCurCond; + bool NewCurUnAnalyzable = TII->AnalyzeBranch(*PMBB, NewCurTBB, + NewCurFBB, NewCurCond, true); + if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) { + TII->RemoveBranch(*PMBB); + NewCurCond.clear(); + TII->InsertBranch(*PMBB, NewCurTBB, 0, NewCurCond); + MadeChange = true; + ++NumBranchOpts; + PMBB->CorrectExtraCFGEdges(NewCurTBB, NewCurFBB, false); + } } }