LLVM API Documentation
00001 //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the machine instruction level if-conversion pass. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "ifcvt" 00015 #include "llvm/CodeGen/Passes.h" 00016 #include "BranchFolding.h" 00017 #include "llvm/ADT/STLExtras.h" 00018 #include "llvm/ADT/SmallSet.h" 00019 #include "llvm/ADT/Statistic.h" 00020 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 00021 #include "llvm/CodeGen/MachineFunctionPass.h" 00022 #include "llvm/CodeGen/MachineInstrBuilder.h" 00023 #include "llvm/CodeGen/MachineModuleInfo.h" 00024 #include "llvm/CodeGen/MachineRegisterInfo.h" 00025 #include "llvm/MC/MCInstrItineraries.h" 00026 #include "llvm/Support/CommandLine.h" 00027 #include "llvm/Support/Debug.h" 00028 #include "llvm/Support/ErrorHandling.h" 00029 #include "llvm/Support/raw_ostream.h" 00030 #include "llvm/Target/TargetInstrInfo.h" 00031 #include "llvm/Target/TargetLowering.h" 00032 #include "llvm/Target/TargetMachine.h" 00033 #include "llvm/Target/TargetRegisterInfo.h" 00034 using namespace llvm; 00035 00036 // Hidden options for help debugging. 00037 static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden); 00038 static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden); 00039 static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden); 00040 static cl::opt<bool> DisableSimple("disable-ifcvt-simple", 00041 cl::init(false), cl::Hidden); 00042 static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false", 00043 cl::init(false), cl::Hidden); 00044 static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle", 00045 cl::init(false), cl::Hidden); 00046 static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev", 00047 cl::init(false), cl::Hidden); 00048 static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false", 00049 cl::init(false), cl::Hidden); 00050 static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev", 00051 cl::init(false), cl::Hidden); 00052 static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond", 00053 cl::init(false), cl::Hidden); 00054 static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold", 00055 cl::init(true), cl::Hidden); 00056 00057 STATISTIC(NumSimple, "Number of simple if-conversions performed"); 00058 STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed"); 00059 STATISTIC(NumTriangle, "Number of triangle if-conversions performed"); 00060 STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed"); 00061 STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed"); 00062 STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed"); 00063 STATISTIC(NumDiamonds, "Number of diamond if-conversions performed"); 00064 STATISTIC(NumIfConvBBs, "Number of if-converted blocks"); 00065 STATISTIC(NumDupBBs, "Number of duplicated blocks"); 00066 STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated"); 00067 00068 namespace { 00069 class IfConverter : public MachineFunctionPass { 00070 enum IfcvtKind { 00071 ICNotClassfied, // BB data valid, but not classified. 00072 ICSimpleFalse, // Same as ICSimple, but on the false path. 00073 ICSimple, // BB is entry of an one split, no rejoin sub-CFG. 00074 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition. 00075 ICTriangleRev, // Same as ICTriangle, but true path rev condition. 00076 ICTriangleFalse, // Same as ICTriangle, but on the false path. 00077 ICTriangle, // BB is entry of a triangle sub-CFG. 00078 ICDiamond // BB is entry of a diamond sub-CFG. 00079 }; 00080 00081 /// BBInfo - One per MachineBasicBlock, this is used to cache the result 00082 /// if-conversion feasibility analysis. This includes results from 00083 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its 00084 /// classification, and common tail block of its successors (if it's a 00085 /// diamond shape), its size, whether it's predicable, and whether any 00086 /// instruction can clobber the 'would-be' predicate. 00087 /// 00088 /// IsDone - True if BB is not to be considered for ifcvt. 00089 /// IsBeingAnalyzed - True if BB is currently being analyzed. 00090 /// IsAnalyzed - True if BB has been analyzed (info is still valid). 00091 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed. 00092 /// IsBrAnalyzable - True if AnalyzeBranch() returns false. 00093 /// HasFallThrough - True if BB may fallthrough to the following BB. 00094 /// IsUnpredicable - True if BB is known to be unpredicable. 00095 /// ClobbersPred - True if BB could modify predicates (e.g. has 00096 /// cmp, call, etc.) 00097 /// NonPredSize - Number of non-predicated instructions. 00098 /// ExtraCost - Extra cost for multi-cycle instructions. 00099 /// ExtraCost2 - Some instructions are slower when predicated 00100 /// BB - Corresponding MachineBasicBlock. 00101 /// TrueBB / FalseBB- See AnalyzeBranch(). 00102 /// BrCond - Conditions for end of block conditional branches. 00103 /// Predicate - Predicate used in the BB. 00104 struct BBInfo { 00105 bool IsDone : 1; 00106 bool IsBeingAnalyzed : 1; 00107 bool IsAnalyzed : 1; 00108 bool IsEnqueued : 1; 00109 bool IsBrAnalyzable : 1; 00110 bool HasFallThrough : 1; 00111 bool IsUnpredicable : 1; 00112 bool CannotBeCopied : 1; 00113 bool ClobbersPred : 1; 00114 unsigned NonPredSize; 00115 unsigned ExtraCost; 00116 unsigned ExtraCost2; 00117 MachineBasicBlock *BB; 00118 MachineBasicBlock *TrueBB; 00119 MachineBasicBlock *FalseBB; 00120 SmallVector<MachineOperand, 4> BrCond; 00121 SmallVector<MachineOperand, 4> Predicate; 00122 BBInfo() : IsDone(false), IsBeingAnalyzed(false), 00123 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false), 00124 HasFallThrough(false), IsUnpredicable(false), 00125 CannotBeCopied(false), ClobbersPred(false), NonPredSize(0), 00126 ExtraCost(0), ExtraCost2(0), BB(0), TrueBB(0), FalseBB(0) {} 00127 }; 00128 00129 /// IfcvtToken - Record information about pending if-conversions to attempt: 00130 /// BBI - Corresponding BBInfo. 00131 /// Kind - Type of block. See IfcvtKind. 00132 /// NeedSubsumption - True if the to-be-predicated BB has already been 00133 /// predicated. 00134 /// NumDups - Number of instructions that would be duplicated due 00135 /// to this if-conversion. (For diamonds, the number of 00136 /// identical instructions at the beginnings of both 00137 /// paths). 00138 /// NumDups2 - For diamonds, the number of identical instructions 00139 /// at the ends of both paths. 00140 struct IfcvtToken { 00141 BBInfo &BBI; 00142 IfcvtKind Kind; 00143 bool NeedSubsumption; 00144 unsigned NumDups; 00145 unsigned NumDups2; 00146 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0) 00147 : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {} 00148 }; 00149 00150 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by 00151 /// basic block number. 00152 std::vector<BBInfo> BBAnalysis; 00153 00154 const TargetLoweringBase *TLI; 00155 const TargetInstrInfo *TII; 00156 const TargetRegisterInfo *TRI; 00157 const InstrItineraryData *InstrItins; 00158 const MachineBranchProbabilityInfo *MBPI; 00159 MachineRegisterInfo *MRI; 00160 00161 bool PreRegAlloc; 00162 bool MadeChange; 00163 int FnNum; 00164 public: 00165 static char ID; 00166 IfConverter() : MachineFunctionPass(ID), FnNum(-1) { 00167 initializeIfConverterPass(*PassRegistry::getPassRegistry()); 00168 } 00169 00170 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00171 AU.addRequired<MachineBranchProbabilityInfo>(); 00172 MachineFunctionPass::getAnalysisUsage(AU); 00173 } 00174 00175 virtual bool runOnMachineFunction(MachineFunction &MF); 00176 00177 private: 00178 bool ReverseBranchCondition(BBInfo &BBI); 00179 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups, 00180 const BranchProbability &Prediction) const; 00181 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, 00182 bool FalseBranch, unsigned &Dups, 00183 const BranchProbability &Prediction) const; 00184 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, 00185 unsigned &Dups1, unsigned &Dups2) const; 00186 void ScanInstructions(BBInfo &BBI); 00187 BBInfo &AnalyzeBlock(MachineBasicBlock *BB, 00188 std::vector<IfcvtToken*> &Tokens); 00189 bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond, 00190 bool isTriangle = false, bool RevBranch = false); 00191 void AnalyzeBlocks(MachineFunction &MF, std::vector<IfcvtToken*> &Tokens); 00192 void InvalidatePreds(MachineBasicBlock *BB); 00193 void RemoveExtraEdges(BBInfo &BBI); 00194 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind); 00195 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind); 00196 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, 00197 unsigned NumDups1, unsigned NumDups2); 00198 void PredicateBlock(BBInfo &BBI, 00199 MachineBasicBlock::iterator E, 00200 SmallVectorImpl<MachineOperand> &Cond, 00201 SmallSet<unsigned, 4> &Redefs, 00202 SmallSet<unsigned, 4> *LaterRedefs = 0); 00203 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, 00204 SmallVectorImpl<MachineOperand> &Cond, 00205 SmallSet<unsigned, 4> &Redefs, 00206 bool IgnoreBr = false); 00207 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true); 00208 00209 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB, 00210 unsigned Cycle, unsigned Extra, 00211 const BranchProbability &Prediction) const { 00212 return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra, 00213 Prediction); 00214 } 00215 00216 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB, 00217 unsigned TCycle, unsigned TExtra, 00218 MachineBasicBlock &FBB, 00219 unsigned FCycle, unsigned FExtra, 00220 const BranchProbability &Prediction) const { 00221 return TCycle > 0 && FCycle > 0 && 00222 TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra, 00223 Prediction); 00224 } 00225 00226 // blockAlwaysFallThrough - Block ends without a terminator. 00227 bool blockAlwaysFallThrough(BBInfo &BBI) const { 00228 return BBI.IsBrAnalyzable && BBI.TrueBB == NULL; 00229 } 00230 00231 // IfcvtTokenCmp - Used to sort if-conversion candidates. 00232 static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) { 00233 int Incr1 = (C1->Kind == ICDiamond) 00234 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups; 00235 int Incr2 = (C2->Kind == ICDiamond) 00236 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups; 00237 if (Incr1 > Incr2) 00238 return true; 00239 else if (Incr1 == Incr2) { 00240 // Favors subsumption. 00241 if (C1->NeedSubsumption == false && C2->NeedSubsumption == true) 00242 return true; 00243 else if (C1->NeedSubsumption == C2->NeedSubsumption) { 00244 // Favors diamond over triangle, etc. 00245 if ((unsigned)C1->Kind < (unsigned)C2->Kind) 00246 return true; 00247 else if (C1->Kind == C2->Kind) 00248 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber(); 00249 } 00250 } 00251 return false; 00252 } 00253 }; 00254 00255 char IfConverter::ID = 0; 00256 } 00257 00258 char &llvm::IfConverterID = IfConverter::ID; 00259 00260 INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false) 00261 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 00262 INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false) 00263 00264 bool IfConverter::runOnMachineFunction(MachineFunction &MF) { 00265 TLI = MF.getTarget().getTargetLowering(); 00266 TII = MF.getTarget().getInstrInfo(); 00267 TRI = MF.getTarget().getRegisterInfo(); 00268 MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 00269 MRI = &MF.getRegInfo(); 00270 InstrItins = MF.getTarget().getInstrItineraryData(); 00271 if (!TII) return false; 00272 00273 PreRegAlloc = MRI->isSSA(); 00274 00275 bool BFChange = false; 00276 if (!PreRegAlloc) { 00277 // Tail merge tend to expose more if-conversion opportunities. 00278 BranchFolder BF(true, false); 00279 BFChange = BF.OptimizeFunction(MF, TII, 00280 MF.getTarget().getRegisterInfo(), 00281 getAnalysisIfAvailable<MachineModuleInfo>()); 00282 } 00283 00284 DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'" 00285 << MF.getName() << "\'"); 00286 00287 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) { 00288 DEBUG(dbgs() << " skipped\n"); 00289 return false; 00290 } 00291 DEBUG(dbgs() << "\n"); 00292 00293 MF.RenumberBlocks(); 00294 BBAnalysis.resize(MF.getNumBlockIDs()); 00295 00296 std::vector<IfcvtToken*> Tokens; 00297 MadeChange = false; 00298 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + 00299 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds; 00300 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) { 00301 // Do an initial analysis for each basic block and find all the potential 00302 // candidates to perform if-conversion. 00303 bool Change = false; 00304 AnalyzeBlocks(MF, Tokens); 00305 while (!Tokens.empty()) { 00306 IfcvtToken *Token = Tokens.back(); 00307 Tokens.pop_back(); 00308 BBInfo &BBI = Token->BBI; 00309 IfcvtKind Kind = Token->Kind; 00310 unsigned NumDups = Token->NumDups; 00311 unsigned NumDups2 = Token->NumDups2; 00312 00313 delete Token; 00314 00315 // If the block has been evicted out of the queue or it has already been 00316 // marked dead (due to it being predicated), then skip it. 00317 if (BBI.IsDone) 00318 BBI.IsEnqueued = false; 00319 if (!BBI.IsEnqueued) 00320 continue; 00321 00322 BBI.IsEnqueued = false; 00323 00324 bool RetVal = false; 00325 switch (Kind) { 00326 default: llvm_unreachable("Unexpected!"); 00327 case ICSimple: 00328 case ICSimpleFalse: { 00329 bool isFalse = Kind == ICSimpleFalse; 00330 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break; 00331 DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ? 00332 " false" : "") 00333 << "): BB#" << BBI.BB->getNumber() << " (" 00334 << ((Kind == ICSimpleFalse) 00335 ? BBI.FalseBB->getNumber() 00336 : BBI.TrueBB->getNumber()) << ") "); 00337 RetVal = IfConvertSimple(BBI, Kind); 00338 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 00339 if (RetVal) { 00340 if (isFalse) ++NumSimpleFalse; 00341 else ++NumSimple; 00342 } 00343 break; 00344 } 00345 case ICTriangle: 00346 case ICTriangleRev: 00347 case ICTriangleFalse: 00348 case ICTriangleFRev: { 00349 bool isFalse = Kind == ICTriangleFalse; 00350 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev); 00351 if (DisableTriangle && !isFalse && !isRev) break; 00352 if (DisableTriangleR && !isFalse && isRev) break; 00353 if (DisableTriangleF && isFalse && !isRev) break; 00354 if (DisableTriangleFR && isFalse && isRev) break; 00355 DEBUG(dbgs() << "Ifcvt (Triangle"); 00356 if (isFalse) 00357 DEBUG(dbgs() << " false"); 00358 if (isRev) 00359 DEBUG(dbgs() << " rev"); 00360 DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:" 00361 << BBI.TrueBB->getNumber() << ",F:" 00362 << BBI.FalseBB->getNumber() << ") "); 00363 RetVal = IfConvertTriangle(BBI, Kind); 00364 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 00365 if (RetVal) { 00366 if (isFalse) { 00367 if (isRev) ++NumTriangleFRev; 00368 else ++NumTriangleFalse; 00369 } else { 00370 if (isRev) ++NumTriangleRev; 00371 else ++NumTriangle; 00372 } 00373 } 00374 break; 00375 } 00376 case ICDiamond: { 00377 if (DisableDiamond) break; 00378 DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:" 00379 << BBI.TrueBB->getNumber() << ",F:" 00380 << BBI.FalseBB->getNumber() << ") "); 00381 RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2); 00382 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 00383 if (RetVal) ++NumDiamonds; 00384 break; 00385 } 00386 } 00387 00388 Change |= RetVal; 00389 00390 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev + 00391 NumTriangleFalse + NumTriangleFRev + NumDiamonds; 00392 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit) 00393 break; 00394 } 00395 00396 if (!Change) 00397 break; 00398 MadeChange |= Change; 00399 } 00400 00401 // Delete tokens in case of early exit. 00402 while (!Tokens.empty()) { 00403 IfcvtToken *Token = Tokens.back(); 00404 Tokens.pop_back(); 00405 delete Token; 00406 } 00407 00408 Tokens.clear(); 00409 BBAnalysis.clear(); 00410 00411 if (MadeChange && IfCvtBranchFold) { 00412 BranchFolder BF(false, false); 00413 BF.OptimizeFunction(MF, TII, 00414 MF.getTarget().getRegisterInfo(), 00415 getAnalysisIfAvailable<MachineModuleInfo>()); 00416 } 00417 00418 MadeChange |= BFChange; 00419 return MadeChange; 00420 } 00421 00422 /// findFalseBlock - BB has a fallthrough. Find its 'false' successor given 00423 /// its 'true' successor. 00424 static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB, 00425 MachineBasicBlock *TrueBB) { 00426 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), 00427 E = BB->succ_end(); SI != E; ++SI) { 00428 MachineBasicBlock *SuccBB = *SI; 00429 if (SuccBB != TrueBB) 00430 return SuccBB; 00431 } 00432 return NULL; 00433 } 00434 00435 /// ReverseBranchCondition - Reverse the condition of the end of the block 00436 /// branch. Swap block's 'true' and 'false' successors. 00437 bool IfConverter::ReverseBranchCondition(BBInfo &BBI) { 00438 DebugLoc dl; // FIXME: this is nowhere 00439 if (!TII->ReverseBranchCondition(BBI.BrCond)) { 00440 TII->RemoveBranch(*BBI.BB); 00441 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl); 00442 std::swap(BBI.TrueBB, BBI.FalseBB); 00443 return true; 00444 } 00445 return false; 00446 } 00447 00448 /// getNextBlock - Returns the next block in the function blocks ordering. If 00449 /// it is the end, returns NULL. 00450 static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) { 00451 MachineFunction::iterator I = BB; 00452 MachineFunction::iterator E = BB->getParent()->end(); 00453 if (++I == E) 00454 return NULL; 00455 return I; 00456 } 00457 00458 /// ValidSimple - Returns true if the 'true' block (along with its 00459 /// predecessor) forms a valid simple shape for ifcvt. It also returns the 00460 /// number of instructions that the ifcvt would need to duplicate if performed 00461 /// in Dups. 00462 bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups, 00463 const BranchProbability &Prediction) const { 00464 Dups = 0; 00465 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) 00466 return false; 00467 00468 if (TrueBBI.IsBrAnalyzable) 00469 return false; 00470 00471 if (TrueBBI.BB->pred_size() > 1) { 00472 if (TrueBBI.CannotBeCopied || 00473 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize, 00474 Prediction)) 00475 return false; 00476 Dups = TrueBBI.NonPredSize; 00477 } 00478 00479 return true; 00480 } 00481 00482 /// ValidTriangle - Returns true if the 'true' and 'false' blocks (along 00483 /// with their common predecessor) forms a valid triangle shape for ifcvt. 00484 /// If 'FalseBranch' is true, it checks if 'true' block's false branch 00485 /// branches to the 'false' block rather than the other way around. It also 00486 /// returns the number of instructions that the ifcvt would need to duplicate 00487 /// if performed in 'Dups'. 00488 bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, 00489 bool FalseBranch, unsigned &Dups, 00490 const BranchProbability &Prediction) const { 00491 Dups = 0; 00492 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) 00493 return false; 00494 00495 if (TrueBBI.BB->pred_size() > 1) { 00496 if (TrueBBI.CannotBeCopied) 00497 return false; 00498 00499 unsigned Size = TrueBBI.NonPredSize; 00500 if (TrueBBI.IsBrAnalyzable) { 00501 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty()) 00502 // Ends with an unconditional branch. It will be removed. 00503 --Size; 00504 else { 00505 MachineBasicBlock *FExit = FalseBranch 00506 ? TrueBBI.TrueBB : TrueBBI.FalseBB; 00507 if (FExit) 00508 // Require a conditional branch 00509 ++Size; 00510 } 00511 } 00512 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction)) 00513 return false; 00514 Dups = Size; 00515 } 00516 00517 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB; 00518 if (!TExit && blockAlwaysFallThrough(TrueBBI)) { 00519 MachineFunction::iterator I = TrueBBI.BB; 00520 if (++I == TrueBBI.BB->getParent()->end()) 00521 return false; 00522 TExit = I; 00523 } 00524 return TExit && TExit == FalseBBI.BB; 00525 } 00526 00527 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along 00528 /// with their common predecessor) forms a valid diamond shape for ifcvt. 00529 bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, 00530 unsigned &Dups1, unsigned &Dups2) const { 00531 Dups1 = Dups2 = 0; 00532 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone || 00533 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone) 00534 return false; 00535 00536 MachineBasicBlock *TT = TrueBBI.TrueBB; 00537 MachineBasicBlock *FT = FalseBBI.TrueBB; 00538 00539 if (!TT && blockAlwaysFallThrough(TrueBBI)) 00540 TT = getNextBlock(TrueBBI.BB); 00541 if (!FT && blockAlwaysFallThrough(FalseBBI)) 00542 FT = getNextBlock(FalseBBI.BB); 00543 if (TT != FT) 00544 return false; 00545 if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable)) 00546 return false; 00547 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) 00548 return false; 00549 00550 // FIXME: Allow true block to have an early exit? 00551 if (TrueBBI.FalseBB || FalseBBI.FalseBB || 00552 (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)) 00553 return false; 00554 00555 // Count duplicate instructions at the beginning of the true and false blocks. 00556 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin(); 00557 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin(); 00558 MachineBasicBlock::iterator TIE = TrueBBI.BB->end(); 00559 MachineBasicBlock::iterator FIE = FalseBBI.BB->end(); 00560 while (TIB != TIE && FIB != FIE) { 00561 // Skip dbg_value instructions. These do not count. 00562 if (TIB->isDebugValue()) { 00563 while (TIB != TIE && TIB->isDebugValue()) 00564 ++TIB; 00565 if (TIB == TIE) 00566 break; 00567 } 00568 if (FIB->isDebugValue()) { 00569 while (FIB != FIE && FIB->isDebugValue()) 00570 ++FIB; 00571 if (FIB == FIE) 00572 break; 00573 } 00574 if (!TIB->isIdenticalTo(FIB)) 00575 break; 00576 ++Dups1; 00577 ++TIB; 00578 ++FIB; 00579 } 00580 00581 // Now, in preparation for counting duplicate instructions at the ends of the 00582 // blocks, move the end iterators up past any branch instructions. 00583 while (TIE != TIB) { 00584 --TIE; 00585 if (!TIE->isBranch()) 00586 break; 00587 } 00588 while (FIE != FIB) { 00589 --FIE; 00590 if (!FIE->isBranch()) 00591 break; 00592 } 00593 00594 // If Dups1 includes all of a block, then don't count duplicate 00595 // instructions at the end of the blocks. 00596 if (TIB == TIE || FIB == FIE) 00597 return true; 00598 00599 // Count duplicate instructions at the ends of the blocks. 00600 while (TIE != TIB && FIE != FIB) { 00601 // Skip dbg_value instructions. These do not count. 00602 if (TIE->isDebugValue()) { 00603 while (TIE != TIB && TIE->isDebugValue()) 00604 --TIE; 00605 if (TIE == TIB) 00606 break; 00607 } 00608 if (FIE->isDebugValue()) { 00609 while (FIE != FIB && FIE->isDebugValue()) 00610 --FIE; 00611 if (FIE == FIB) 00612 break; 00613 } 00614 if (!TIE->isIdenticalTo(FIE)) 00615 break; 00616 ++Dups2; 00617 --TIE; 00618 --FIE; 00619 } 00620 00621 return true; 00622 } 00623 00624 /// ScanInstructions - Scan all the instructions in the block to determine if 00625 /// the block is predicable. In most cases, that means all the instructions 00626 /// in the block are isPredicable(). Also checks if the block contains any 00627 /// instruction which can clobber a predicate (e.g. condition code register). 00628 /// If so, the block is not predicable unless it's the last instruction. 00629 void IfConverter::ScanInstructions(BBInfo &BBI) { 00630 if (BBI.IsDone) 00631 return; 00632 00633 bool AlreadyPredicated = !BBI.Predicate.empty(); 00634 // First analyze the end of BB branches. 00635 BBI.TrueBB = BBI.FalseBB = NULL; 00636 BBI.BrCond.clear(); 00637 BBI.IsBrAnalyzable = 00638 !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond); 00639 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL; 00640 00641 if (BBI.BrCond.size()) { 00642 // No false branch. This BB must end with a conditional branch and a 00643 // fallthrough. 00644 if (!BBI.FalseBB) 00645 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB); 00646 if (!BBI.FalseBB) { 00647 // Malformed bcc? True and false blocks are the same? 00648 BBI.IsUnpredicable = true; 00649 return; 00650 } 00651 } 00652 00653 // Then scan all the instructions. 00654 BBI.NonPredSize = 0; 00655 BBI.ExtraCost = 0; 00656 BBI.ExtraCost2 = 0; 00657 BBI.ClobbersPred = false; 00658 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end(); 00659 I != E; ++I) { 00660 if (I->isDebugValue()) 00661 continue; 00662 00663 if (I->isNotDuplicable()) 00664 BBI.CannotBeCopied = true; 00665 00666 bool isPredicated = TII->isPredicated(I); 00667 bool isCondBr = BBI.IsBrAnalyzable && I->isConditionalBranch(); 00668 00669 if (!isCondBr) { 00670 if (!isPredicated) { 00671 BBI.NonPredSize++; 00672 unsigned ExtraPredCost = 0; 00673 unsigned NumCycles = TII->getInstrLatency(InstrItins, &*I, 00674 &ExtraPredCost); 00675 if (NumCycles > 1) 00676 BBI.ExtraCost += NumCycles-1; 00677 BBI.ExtraCost2 += ExtraPredCost; 00678 } else if (!AlreadyPredicated) { 00679 // FIXME: This instruction is already predicated before the 00680 // if-conversion pass. It's probably something like a conditional move. 00681 // Mark this block unpredicable for now. 00682 BBI.IsUnpredicable = true; 00683 return; 00684 } 00685 } 00686 00687 if (BBI.ClobbersPred && !isPredicated) { 00688 // Predicate modification instruction should end the block (except for 00689 // already predicated instructions and end of block branches). 00690 if (isCondBr) { 00691 // A conditional branch is not predicable, but it may be eliminated. 00692 continue; 00693 } 00694 00695 // Predicate may have been modified, the subsequent (currently) 00696 // unpredicated instructions cannot be correctly predicated. 00697 BBI.IsUnpredicable = true; 00698 return; 00699 } 00700 00701 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are 00702 // still potentially predicable. 00703 std::vector<MachineOperand> PredDefs; 00704 if (TII->DefinesPredicate(I, PredDefs)) 00705 BBI.ClobbersPred = true; 00706 00707 if (!TII->isPredicable(I)) { 00708 BBI.IsUnpredicable = true; 00709 return; 00710 } 00711 } 00712 } 00713 00714 /// FeasibilityAnalysis - Determine if the block is a suitable candidate to be 00715 /// predicated by the specified predicate. 00716 bool IfConverter::FeasibilityAnalysis(BBInfo &BBI, 00717 SmallVectorImpl<MachineOperand> &Pred, 00718 bool isTriangle, bool RevBranch) { 00719 // If the block is dead or unpredicable, then it cannot be predicated. 00720 if (BBI.IsDone || BBI.IsUnpredicable) 00721 return false; 00722 00723 // If it is already predicated, check if its predicate subsumes the new 00724 // predicate. 00725 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Pred)) 00726 return false; 00727 00728 if (BBI.BrCond.size()) { 00729 if (!isTriangle) 00730 return false; 00731 00732 // Test predicate subsumption. 00733 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end()); 00734 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 00735 if (RevBranch) { 00736 if (TII->ReverseBranchCondition(Cond)) 00737 return false; 00738 } 00739 if (TII->ReverseBranchCondition(RevPred) || 00740 !TII->SubsumesPredicate(Cond, RevPred)) 00741 return false; 00742 } 00743 00744 return true; 00745 } 00746 00747 /// AnalyzeBlock - Analyze the structure of the sub-CFG starting from 00748 /// the specified block. Record its successors and whether it looks like an 00749 /// if-conversion candidate. 00750 IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB, 00751 std::vector<IfcvtToken*> &Tokens) { 00752 BBInfo &BBI = BBAnalysis[BB->getNumber()]; 00753 00754 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) 00755 return BBI; 00756 00757 BBI.BB = BB; 00758 BBI.IsBeingAnalyzed = true; 00759 00760 ScanInstructions(BBI); 00761 00762 // Unanalyzable or ends with fallthrough or unconditional branch, or if is not 00763 // considered for ifcvt anymore. 00764 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) { 00765 BBI.IsBeingAnalyzed = false; 00766 BBI.IsAnalyzed = true; 00767 return BBI; 00768 } 00769 00770 // Do not ifcvt if either path is a back edge to the entry block. 00771 if (BBI.TrueBB == BB || BBI.FalseBB == BB) { 00772 BBI.IsBeingAnalyzed = false; 00773 BBI.IsAnalyzed = true; 00774 return BBI; 00775 } 00776 00777 // Do not ifcvt if true and false fallthrough blocks are the same. 00778 if (!BBI.FalseBB) { 00779 BBI.IsBeingAnalyzed = false; 00780 BBI.IsAnalyzed = true; 00781 return BBI; 00782 } 00783 00784 BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB, Tokens); 00785 BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens); 00786 00787 if (TrueBBI.IsDone && FalseBBI.IsDone) { 00788 BBI.IsBeingAnalyzed = false; 00789 BBI.IsAnalyzed = true; 00790 return BBI; 00791 } 00792 00793 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 00794 bool CanRevCond = !TII->ReverseBranchCondition(RevCond); 00795 00796 unsigned Dups = 0; 00797 unsigned Dups2 = 0; 00798 bool TNeedSub = !TrueBBI.Predicate.empty(); 00799 bool FNeedSub = !FalseBBI.Predicate.empty(); 00800 bool Enqueued = false; 00801 00802 BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB); 00803 00804 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) && 00805 MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) + 00806 TrueBBI.ExtraCost), TrueBBI.ExtraCost2, 00807 *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) + 00808 FalseBBI.ExtraCost),FalseBBI.ExtraCost2, 00809 Prediction) && 00810 FeasibilityAnalysis(TrueBBI, BBI.BrCond) && 00811 FeasibilityAnalysis(FalseBBI, RevCond)) { 00812 // Diamond: 00813 // EBB 00814 // / \_ 00815 // | | 00816 // TBB FBB 00817 // \ / 00818 // TailBB 00819 // Note TailBB can be empty. 00820 Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups, 00821 Dups2)); 00822 Enqueued = true; 00823 } 00824 00825 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) && 00826 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, 00827 TrueBBI.ExtraCost2, Prediction) && 00828 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) { 00829 // Triangle: 00830 // EBB 00831 // | \_ 00832 // | | 00833 // | TBB 00834 // | / 00835 // FBB 00836 Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups)); 00837 Enqueued = true; 00838 } 00839 00840 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) && 00841 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, 00842 TrueBBI.ExtraCost2, Prediction) && 00843 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) { 00844 Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups)); 00845 Enqueued = true; 00846 } 00847 00848 if (ValidSimple(TrueBBI, Dups, Prediction) && 00849 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, 00850 TrueBBI.ExtraCost2, Prediction) && 00851 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) { 00852 // Simple (split, no rejoin): 00853 // EBB 00854 // | \_ 00855 // | | 00856 // | TBB---> exit 00857 // | 00858 // FBB 00859 Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups)); 00860 Enqueued = true; 00861 } 00862 00863 if (CanRevCond) { 00864 // Try the other path... 00865 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups, 00866 Prediction.getCompl()) && 00867 MeetIfcvtSizeLimit(*FalseBBI.BB, 00868 FalseBBI.NonPredSize + FalseBBI.ExtraCost, 00869 FalseBBI.ExtraCost2, Prediction.getCompl()) && 00870 FeasibilityAnalysis(FalseBBI, RevCond, true)) { 00871 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups)); 00872 Enqueued = true; 00873 } 00874 00875 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups, 00876 Prediction.getCompl()) && 00877 MeetIfcvtSizeLimit(*FalseBBI.BB, 00878 FalseBBI.NonPredSize + FalseBBI.ExtraCost, 00879 FalseBBI.ExtraCost2, Prediction.getCompl()) && 00880 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) { 00881 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups)); 00882 Enqueued = true; 00883 } 00884 00885 if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) && 00886 MeetIfcvtSizeLimit(*FalseBBI.BB, 00887 FalseBBI.NonPredSize + FalseBBI.ExtraCost, 00888 FalseBBI.ExtraCost2, Prediction.getCompl()) && 00889 FeasibilityAnalysis(FalseBBI, RevCond)) { 00890 Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups)); 00891 Enqueued = true; 00892 } 00893 } 00894 00895 BBI.IsEnqueued = Enqueued; 00896 BBI.IsBeingAnalyzed = false; 00897 BBI.IsAnalyzed = true; 00898 return BBI; 00899 } 00900 00901 /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion 00902 /// candidates. 00903 void IfConverter::AnalyzeBlocks(MachineFunction &MF, 00904 std::vector<IfcvtToken*> &Tokens) { 00905 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) { 00906 MachineBasicBlock *BB = I; 00907 AnalyzeBlock(BB, Tokens); 00908 } 00909 00910 // Sort to favor more complex ifcvt scheme. 00911 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp); 00912 } 00913 00914 /// canFallThroughTo - Returns true either if ToBB is the next block after BB or 00915 /// that all the intervening blocks are empty (given BB can fall through to its 00916 /// next block). 00917 static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) { 00918 MachineFunction::iterator PI = BB; 00919 MachineFunction::iterator I = llvm::next(PI); 00920 MachineFunction::iterator TI = ToBB; 00921 MachineFunction::iterator E = BB->getParent()->end(); 00922 while (I != TI) { 00923 // Check isSuccessor to avoid case where the next block is empty, but 00924 // it's not a successor. 00925 if (I == E || !I->empty() || !PI->isSuccessor(I)) 00926 return false; 00927 PI = I++; 00928 } 00929 return true; 00930 } 00931 00932 /// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed 00933 /// to determine if it can be if-converted. If predecessor is already enqueued, 00934 /// dequeue it! 00935 void IfConverter::InvalidatePreds(MachineBasicBlock *BB) { 00936 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(), 00937 E = BB->pred_end(); PI != E; ++PI) { 00938 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()]; 00939 if (PBBI.IsDone || PBBI.BB == BB) 00940 continue; 00941 PBBI.IsAnalyzed = false; 00942 PBBI.IsEnqueued = false; 00943 } 00944 } 00945 00946 /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB. 00947 /// 00948 static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB, 00949 const TargetInstrInfo *TII) { 00950 DebugLoc dl; // FIXME: this is nowhere 00951 SmallVector<MachineOperand, 0> NoCond; 00952 TII->InsertBranch(*BB, ToBB, NULL, NoCond, dl); 00953 } 00954 00955 /// RemoveExtraEdges - Remove true / false edges if either / both are no longer 00956 /// successors. 00957 void IfConverter::RemoveExtraEdges(BBInfo &BBI) { 00958 MachineBasicBlock *TBB = NULL, *FBB = NULL; 00959 SmallVector<MachineOperand, 4> Cond; 00960 if (!TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond)) 00961 BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); 00962 } 00963 00964 /// InitPredRedefs / UpdatePredRedefs - Defs by predicated instructions are 00965 /// modeled as read + write (sort like two-address instructions). These 00966 /// routines track register liveness and add implicit uses to if-converted 00967 /// instructions to conform to the model. 00968 static void InitPredRedefs(MachineBasicBlock *BB, SmallSet<unsigned,4> &Redefs, 00969 const TargetRegisterInfo *TRI) { 00970 for (MachineBasicBlock::livein_iterator I = BB->livein_begin(), 00971 E = BB->livein_end(); I != E; ++I) { 00972 unsigned Reg = *I; 00973 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 00974 SubRegs.isValid(); ++SubRegs) 00975 Redefs.insert(*SubRegs); 00976 } 00977 } 00978 00979 static void UpdatePredRedefs(MachineInstr *MI, SmallSet<unsigned,4> &Redefs, 00980 const TargetRegisterInfo *TRI, 00981 bool AddImpUse = false) { 00982 SmallVector<unsigned, 4> Defs; 00983 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00984 const MachineOperand &MO = MI->getOperand(i); 00985 if (!MO.isReg()) 00986 continue; 00987 unsigned Reg = MO.getReg(); 00988 if (!Reg) 00989 continue; 00990 if (MO.isDef()) 00991 Defs.push_back(Reg); 00992 else if (MO.isKill()) { 00993 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 00994 SubRegs.isValid(); ++SubRegs) 00995 Redefs.erase(*SubRegs); 00996 } 00997 } 00998 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI); 00999 for (unsigned i = 0, e = Defs.size(); i != e; ++i) { 01000 unsigned Reg = Defs[i]; 01001 if (!Redefs.insert(Reg)) { 01002 if (AddImpUse) 01003 // Treat predicated update as read + write. 01004 MIB.addReg(Reg, RegState::Implicit | RegState::Undef); 01005 } else { 01006 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) 01007 Redefs.insert(*SubRegs); 01008 } 01009 } 01010 } 01011 01012 static void UpdatePredRedefs(MachineBasicBlock::iterator I, 01013 MachineBasicBlock::iterator E, 01014 SmallSet<unsigned,4> &Redefs, 01015 const TargetRegisterInfo *TRI) { 01016 while (I != E) { 01017 UpdatePredRedefs(I, Redefs, TRI); 01018 ++I; 01019 } 01020 } 01021 01022 /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG. 01023 /// 01024 bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) { 01025 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 01026 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 01027 BBInfo *CvtBBI = &TrueBBI; 01028 BBInfo *NextBBI = &FalseBBI; 01029 01030 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 01031 if (Kind == ICSimpleFalse) 01032 std::swap(CvtBBI, NextBBI); 01033 01034 if (CvtBBI->IsDone || 01035 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) { 01036 // Something has changed. It's no longer safe to predicate this block. 01037 BBI.IsAnalyzed = false; 01038 CvtBBI->IsAnalyzed = false; 01039 return false; 01040 } 01041 01042 if (CvtBBI->BB->hasAddressTaken()) 01043 // Conservatively abort if-conversion if BB's address is taken. 01044 return false; 01045 01046 if (Kind == ICSimpleFalse) 01047 if (TII->ReverseBranchCondition(Cond)) 01048 llvm_unreachable("Unable to reverse branch condition!"); 01049 01050 // Initialize liveins to the first BB. These are potentiall redefined by 01051 // predicated instructions. 01052 SmallSet<unsigned, 4> Redefs; 01053 InitPredRedefs(CvtBBI->BB, Redefs, TRI); 01054 InitPredRedefs(NextBBI->BB, Redefs, TRI); 01055 01056 if (CvtBBI->BB->pred_size() > 1) { 01057 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 01058 // Copy instructions in the true block, predicate them, and add them to 01059 // the entry block. 01060 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, Redefs); 01061 01062 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so 01063 // explicitly remove CvtBBI as a successor. 01064 BBI.BB->removeSuccessor(CvtBBI->BB); 01065 } else { 01066 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond, Redefs); 01067 01068 // Merge converted block into entry block. 01069 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 01070 MergeBlocks(BBI, *CvtBBI); 01071 } 01072 01073 bool IterIfcvt = true; 01074 if (!canFallThroughTo(BBI.BB, NextBBI->BB)) { 01075 InsertUncondBranch(BBI.BB, NextBBI->BB, TII); 01076 BBI.HasFallThrough = false; 01077 // Now ifcvt'd block will look like this: 01078 // BB: 01079 // ... 01080 // t, f = cmp 01081 // if t op 01082 // b BBf 01083 // 01084 // We cannot further ifcvt this block because the unconditional branch 01085 // will have to be predicated on the new condition, that will not be 01086 // available if cmp executes. 01087 IterIfcvt = false; 01088 } 01089 01090 RemoveExtraEdges(BBI); 01091 01092 // Update block info. BB can be iteratively if-converted. 01093 if (!IterIfcvt) 01094 BBI.IsDone = true; 01095 InvalidatePreds(BBI.BB); 01096 CvtBBI->IsDone = true; 01097 01098 // FIXME: Must maintain LiveIns. 01099 return true; 01100 } 01101 01102 /// IfConvertTriangle - If convert a triangle sub-CFG. 01103 /// 01104 bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) { 01105 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 01106 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 01107 BBInfo *CvtBBI = &TrueBBI; 01108 BBInfo *NextBBI = &FalseBBI; 01109 DebugLoc dl; // FIXME: this is nowhere 01110 01111 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 01112 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) 01113 std::swap(CvtBBI, NextBBI); 01114 01115 if (CvtBBI->IsDone || 01116 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) { 01117 // Something has changed. It's no longer safe to predicate this block. 01118 BBI.IsAnalyzed = false; 01119 CvtBBI->IsAnalyzed = false; 01120 return false; 01121 } 01122 01123 if (CvtBBI->BB->hasAddressTaken()) 01124 // Conservatively abort if-conversion if BB's address is taken. 01125 return false; 01126 01127 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) 01128 if (TII->ReverseBranchCondition(Cond)) 01129 llvm_unreachable("Unable to reverse branch condition!"); 01130 01131 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) { 01132 if (ReverseBranchCondition(*CvtBBI)) { 01133 // BB has been changed, modify its predecessors (except for this 01134 // one) so they don't get ifcvt'ed based on bad intel. 01135 for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(), 01136 E = CvtBBI->BB->pred_end(); PI != E; ++PI) { 01137 MachineBasicBlock *PBB = *PI; 01138 if (PBB == BBI.BB) 01139 continue; 01140 BBInfo &PBBI = BBAnalysis[PBB->getNumber()]; 01141 if (PBBI.IsEnqueued) { 01142 PBBI.IsAnalyzed = false; 01143 PBBI.IsEnqueued = false; 01144 } 01145 } 01146 } 01147 } 01148 01149 // Initialize liveins to the first BB. These are potentially redefined by 01150 // predicated instructions. 01151 SmallSet<unsigned, 4> Redefs; 01152 InitPredRedefs(CvtBBI->BB, Redefs, TRI); 01153 InitPredRedefs(NextBBI->BB, Redefs, TRI); 01154 01155 bool HasEarlyExit = CvtBBI->FalseBB != NULL; 01156 if (CvtBBI->BB->pred_size() > 1) { 01157 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 01158 // Copy instructions in the true block, predicate them, and add them to 01159 // the entry block. 01160 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, Redefs, true); 01161 01162 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so 01163 // explicitly remove CvtBBI as a successor. 01164 BBI.BB->removeSuccessor(CvtBBI->BB); 01165 } else { 01166 // Predicate the 'true' block after removing its branch. 01167 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB); 01168 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond, Redefs); 01169 01170 // Now merge the entry of the triangle with the true block. 01171 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 01172 MergeBlocks(BBI, *CvtBBI, false); 01173 } 01174 01175 // If 'true' block has a 'false' successor, add an exit branch to it. 01176 if (HasEarlyExit) { 01177 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(), 01178 CvtBBI->BrCond.end()); 01179 if (TII->ReverseBranchCondition(RevCond)) 01180 llvm_unreachable("Unable to reverse branch condition!"); 01181 TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond, dl); 01182 BBI.BB->addSuccessor(CvtBBI->FalseBB); 01183 } 01184 01185 // Merge in the 'false' block if the 'false' block has no other 01186 // predecessors. Otherwise, add an unconditional branch to 'false'. 01187 bool FalseBBDead = false; 01188 bool IterIfcvt = true; 01189 bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB); 01190 if (!isFallThrough) { 01191 // Only merge them if the true block does not fallthrough to the false 01192 // block. By not merging them, we make it possible to iteratively 01193 // ifcvt the blocks. 01194 if (!HasEarlyExit && 01195 NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough && 01196 !NextBBI->BB->hasAddressTaken()) { 01197 MergeBlocks(BBI, *NextBBI); 01198 FalseBBDead = true; 01199 } else { 01200 InsertUncondBranch(BBI.BB, NextBBI->BB, TII); 01201 BBI.HasFallThrough = false; 01202 } 01203 // Mixed predicated and unpredicated code. This cannot be iteratively 01204 // predicated. 01205 IterIfcvt = false; 01206 } 01207 01208 RemoveExtraEdges(BBI); 01209 01210 // Update block info. BB can be iteratively if-converted. 01211 if (!IterIfcvt) 01212 BBI.IsDone = true; 01213 InvalidatePreds(BBI.BB); 01214 CvtBBI->IsDone = true; 01215 if (FalseBBDead) 01216 NextBBI->IsDone = true; 01217 01218 // FIXME: Must maintain LiveIns. 01219 return true; 01220 } 01221 01222 /// IfConvertDiamond - If convert a diamond sub-CFG. 01223 /// 01224 bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, 01225 unsigned NumDups1, unsigned NumDups2) { 01226 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 01227 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 01228 MachineBasicBlock *TailBB = TrueBBI.TrueBB; 01229 // True block must fall through or end with an unanalyzable terminator. 01230 if (!TailBB) { 01231 if (blockAlwaysFallThrough(TrueBBI)) 01232 TailBB = FalseBBI.TrueBB; 01233 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!"); 01234 } 01235 01236 if (TrueBBI.IsDone || FalseBBI.IsDone || 01237 TrueBBI.BB->pred_size() > 1 || 01238 FalseBBI.BB->pred_size() > 1) { 01239 // Something has changed. It's no longer safe to predicate these blocks. 01240 BBI.IsAnalyzed = false; 01241 TrueBBI.IsAnalyzed = false; 01242 FalseBBI.IsAnalyzed = false; 01243 return false; 01244 } 01245 01246 if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken()) 01247 // Conservatively abort if-conversion if either BB has its address taken. 01248 return false; 01249 01250 // Put the predicated instructions from the 'true' block before the 01251 // instructions from the 'false' block, unless the true block would clobber 01252 // the predicate, in which case, do the opposite. 01253 BBInfo *BBI1 = &TrueBBI; 01254 BBInfo *BBI2 = &FalseBBI; 01255 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 01256 if (TII->ReverseBranchCondition(RevCond)) 01257 llvm_unreachable("Unable to reverse branch condition!"); 01258 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond; 01259 SmallVector<MachineOperand, 4> *Cond2 = &RevCond; 01260 01261 // Figure out the more profitable ordering. 01262 bool DoSwap = false; 01263 if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred) 01264 DoSwap = true; 01265 else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) { 01266 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize) 01267 DoSwap = true; 01268 } 01269 if (DoSwap) { 01270 std::swap(BBI1, BBI2); 01271 std::swap(Cond1, Cond2); 01272 } 01273 01274 // Remove the conditional branch from entry to the blocks. 01275 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 01276 01277 // Initialize liveins to the first BB. These are potentially redefined by 01278 // predicated instructions. 01279 SmallSet<unsigned, 4> Redefs; 01280 InitPredRedefs(BBI1->BB, Redefs, TRI); 01281 01282 // Remove the duplicated instructions at the beginnings of both paths. 01283 MachineBasicBlock::iterator DI1 = BBI1->BB->begin(); 01284 MachineBasicBlock::iterator DI2 = BBI2->BB->begin(); 01285 MachineBasicBlock::iterator DIE1 = BBI1->BB->end(); 01286 MachineBasicBlock::iterator DIE2 = BBI2->BB->end(); 01287 // Skip dbg_value instructions 01288 while (DI1 != DIE1 && DI1->isDebugValue()) 01289 ++DI1; 01290 while (DI2 != DIE2 && DI2->isDebugValue()) 01291 ++DI2; 01292 BBI1->NonPredSize -= NumDups1; 01293 BBI2->NonPredSize -= NumDups1; 01294 01295 // Skip past the dups on each side separately since there may be 01296 // differing dbg_value entries. 01297 for (unsigned i = 0; i < NumDups1; ++DI1) { 01298 if (!DI1->isDebugValue()) 01299 ++i; 01300 } 01301 while (NumDups1 != 0) { 01302 ++DI2; 01303 if (!DI2->isDebugValue()) 01304 --NumDups1; 01305 } 01306 01307 UpdatePredRedefs(BBI1->BB->begin(), DI1, Redefs, TRI); 01308 BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1); 01309 BBI2->BB->erase(BBI2->BB->begin(), DI2); 01310 01311 // Remove branch from 'true' block and remove duplicated instructions. 01312 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB); 01313 DI1 = BBI1->BB->end(); 01314 for (unsigned i = 0; i != NumDups2; ) { 01315 // NumDups2 only counted non-dbg_value instructions, so this won't 01316 // run off the head of the list. 01317 assert (DI1 != BBI1->BB->begin()); 01318 --DI1; 01319 // skip dbg_value instructions 01320 if (!DI1->isDebugValue()) 01321 ++i; 01322 } 01323 BBI1->BB->erase(DI1, BBI1->BB->end()); 01324 01325 // Remove 'false' block branch and find the last instruction to predicate. 01326 BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB); 01327 DI2 = BBI2->BB->end(); 01328 while (NumDups2 != 0) { 01329 // NumDups2 only counted non-dbg_value instructions, so this won't 01330 // run off the head of the list. 01331 assert (DI2 != BBI2->BB->begin()); 01332 --DI2; 01333 // skip dbg_value instructions 01334 if (!DI2->isDebugValue()) 01335 --NumDups2; 01336 } 01337 01338 // Remember which registers would later be defined by the false block. 01339 // This allows us not to predicate instructions in the true block that would 01340 // later be re-defined. That is, rather than 01341 // subeq r0, r1, #1 01342 // addne r0, r1, #1 01343 // generate: 01344 // sub r0, r1, #1 01345 // addne r0, r1, #1 01346 SmallSet<unsigned, 4> RedefsByFalse; 01347 SmallSet<unsigned, 4> ExtUses; 01348 if (TII->isProfitableToUnpredicate(*BBI1->BB, *BBI2->BB)) { 01349 for (MachineBasicBlock::iterator FI = BBI2->BB->begin(); FI != DI2; ++FI) { 01350 if (FI->isDebugValue()) 01351 continue; 01352 SmallVector<unsigned, 4> Defs; 01353 for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) { 01354 const MachineOperand &MO = FI->getOperand(i); 01355 if (!MO.isReg()) 01356 continue; 01357 unsigned Reg = MO.getReg(); 01358 if (!Reg) 01359 continue; 01360 if (MO.isDef()) { 01361 Defs.push_back(Reg); 01362 } else if (!RedefsByFalse.count(Reg)) { 01363 // These are defined before ctrl flow reach the 'false' instructions. 01364 // They cannot be modified by the 'true' instructions. 01365 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 01366 SubRegs.isValid(); ++SubRegs) 01367 ExtUses.insert(*SubRegs); 01368 } 01369 } 01370 01371 for (unsigned i = 0, e = Defs.size(); i != e; ++i) { 01372 unsigned Reg = Defs[i]; 01373 if (!ExtUses.count(Reg)) { 01374 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 01375 SubRegs.isValid(); ++SubRegs) 01376 RedefsByFalse.insert(*SubRegs); 01377 } 01378 } 01379 } 01380 } 01381 01382 // Predicate the 'true' block. 01383 PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1, Redefs, &RedefsByFalse); 01384 01385 // Predicate the 'false' block. 01386 PredicateBlock(*BBI2, DI2, *Cond2, Redefs); 01387 01388 // Merge the true block into the entry of the diamond. 01389 MergeBlocks(BBI, *BBI1, TailBB == 0); 01390 MergeBlocks(BBI, *BBI2, TailBB == 0); 01391 01392 // If the if-converted block falls through or unconditionally branches into 01393 // the tail block, and the tail block does not have other predecessors, then 01394 // fold the tail block in as well. Otherwise, unless it falls through to the 01395 // tail, add a unconditional branch to it. 01396 if (TailBB) { 01397 BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()]; 01398 bool CanMergeTail = !TailBBI.HasFallThrough && 01399 !TailBBI.BB->hasAddressTaken(); 01400 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB; 01401 // check if there are any other predecessors besides those. 01402 unsigned NumPreds = TailBB->pred_size(); 01403 if (NumPreds > 1) 01404 CanMergeTail = false; 01405 else if (NumPreds == 1 && CanMergeTail) { 01406 MachineBasicBlock::pred_iterator PI = TailBB->pred_begin(); 01407 if (*PI != BBI1->BB && *PI != BBI2->BB) 01408 CanMergeTail = false; 01409 } 01410 if (CanMergeTail) { 01411 MergeBlocks(BBI, TailBBI); 01412 TailBBI.IsDone = true; 01413 } else { 01414 BBI.BB->addSuccessor(TailBB); 01415 InsertUncondBranch(BBI.BB, TailBB, TII); 01416 BBI.HasFallThrough = false; 01417 } 01418 } 01419 01420 // RemoveExtraEdges won't work if the block has an unanalyzable branch, 01421 // which can happen here if TailBB is unanalyzable and is merged, so 01422 // explicitly remove BBI1 and BBI2 as successors. 01423 BBI.BB->removeSuccessor(BBI1->BB); 01424 BBI.BB->removeSuccessor(BBI2->BB); 01425 RemoveExtraEdges(BBI); 01426 01427 // Update block info. 01428 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true; 01429 InvalidatePreds(BBI.BB); 01430 01431 // FIXME: Must maintain LiveIns. 01432 return true; 01433 } 01434 01435 static bool MaySpeculate(const MachineInstr *MI, 01436 SmallSet<unsigned, 4> &LaterRedefs, 01437 const TargetInstrInfo *TII) { 01438 bool SawStore = true; 01439 if (!MI->isSafeToMove(TII, 0, SawStore)) 01440 return false; 01441 01442 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 01443 const MachineOperand &MO = MI->getOperand(i); 01444 if (!MO.isReg()) 01445 continue; 01446 unsigned Reg = MO.getReg(); 01447 if (!Reg) 01448 continue; 01449 if (MO.isDef() && !LaterRedefs.count(Reg)) 01450 return false; 01451 } 01452 01453 return true; 01454 } 01455 01456 /// PredicateBlock - Predicate instructions from the start of the block to the 01457 /// specified end with the specified condition. 01458 void IfConverter::PredicateBlock(BBInfo &BBI, 01459 MachineBasicBlock::iterator E, 01460 SmallVectorImpl<MachineOperand> &Cond, 01461 SmallSet<unsigned, 4> &Redefs, 01462 SmallSet<unsigned, 4> *LaterRedefs) { 01463 bool AnyUnpred = false; 01464 bool MaySpec = LaterRedefs != 0; 01465 for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) { 01466 if (I->isDebugValue() || TII->isPredicated(I)) 01467 continue; 01468 // It may be possible not to predicate an instruction if it's the 'true' 01469 // side of a diamond and the 'false' side may re-define the instruction's 01470 // defs. 01471 if (MaySpec && MaySpeculate(I, *LaterRedefs, TII)) { 01472 AnyUnpred = true; 01473 continue; 01474 } 01475 // If any instruction is predicated, then every instruction after it must 01476 // be predicated. 01477 MaySpec = false; 01478 if (!TII->PredicateInstruction(I, Cond)) { 01479 #ifndef NDEBUG 01480 dbgs() << "Unable to predicate " << *I << "!\n"; 01481 #endif 01482 llvm_unreachable(0); 01483 } 01484 01485 // If the predicated instruction now redefines a register as the result of 01486 // if-conversion, add an implicit kill. 01487 UpdatePredRedefs(I, Redefs, TRI, true); 01488 } 01489 01490 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate)); 01491 01492 BBI.IsAnalyzed = false; 01493 BBI.NonPredSize = 0; 01494 01495 ++NumIfConvBBs; 01496 if (AnyUnpred) 01497 ++NumUnpred; 01498 } 01499 01500 /// CopyAndPredicateBlock - Copy and predicate instructions from source BB to 01501 /// the destination block. Skip end of block branches if IgnoreBr is true. 01502 void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, 01503 SmallVectorImpl<MachineOperand> &Cond, 01504 SmallSet<unsigned, 4> &Redefs, 01505 bool IgnoreBr) { 01506 MachineFunction &MF = *ToBBI.BB->getParent(); 01507 01508 for (MachineBasicBlock::iterator I = FromBBI.BB->begin(), 01509 E = FromBBI.BB->end(); I != E; ++I) { 01510 // Do not copy the end of the block branches. 01511 if (IgnoreBr && I->isBranch()) 01512 break; 01513 01514 MachineInstr *MI = MF.CloneMachineInstr(I); 01515 ToBBI.BB->insert(ToBBI.BB->end(), MI); 01516 ToBBI.NonPredSize++; 01517 unsigned ExtraPredCost = 0; 01518 unsigned NumCycles = TII->getInstrLatency(InstrItins, &*I, &ExtraPredCost); 01519 if (NumCycles > 1) 01520 ToBBI.ExtraCost += NumCycles-1; 01521 ToBBI.ExtraCost2 += ExtraPredCost; 01522 01523 if (!TII->isPredicated(I) && !MI->isDebugValue()) { 01524 if (!TII->PredicateInstruction(MI, Cond)) { 01525 #ifndef NDEBUG 01526 dbgs() << "Unable to predicate " << *I << "!\n"; 01527 #endif 01528 llvm_unreachable(0); 01529 } 01530 } 01531 01532 // If the predicated instruction now redefines a register as the result of 01533 // if-conversion, add an implicit kill. 01534 UpdatePredRedefs(MI, Redefs, TRI, true); 01535 } 01536 01537 if (!IgnoreBr) { 01538 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(), 01539 FromBBI.BB->succ_end()); 01540 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB); 01541 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL; 01542 01543 for (unsigned i = 0, e = Succs.size(); i != e; ++i) { 01544 MachineBasicBlock *Succ = Succs[i]; 01545 // Fallthrough edge can't be transferred. 01546 if (Succ == FallThrough) 01547 continue; 01548 ToBBI.BB->addSuccessor(Succ); 01549 } 01550 } 01551 01552 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(), 01553 std::back_inserter(ToBBI.Predicate)); 01554 std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate)); 01555 01556 ToBBI.ClobbersPred |= FromBBI.ClobbersPred; 01557 ToBBI.IsAnalyzed = false; 01558 01559 ++NumDupBBs; 01560 } 01561 01562 /// MergeBlocks - Move all instructions from FromBB to the end of ToBB. 01563 /// This will leave FromBB as an empty block, so remove all of its 01564 /// successor edges except for the fall-through edge. If AddEdges is true, 01565 /// i.e., when FromBBI's branch is being moved, add those successor edges to 01566 /// ToBBI. 01567 void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) { 01568 assert(!FromBBI.BB->hasAddressTaken() && 01569 "Removing a BB whose address is taken!"); 01570 01571 ToBBI.BB->splice(ToBBI.BB->end(), 01572 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end()); 01573 01574 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(), 01575 FromBBI.BB->succ_end()); 01576 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB); 01577 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL; 01578 01579 for (unsigned i = 0, e = Succs.size(); i != e; ++i) { 01580 MachineBasicBlock *Succ = Succs[i]; 01581 // Fallthrough edge can't be transferred. 01582 if (Succ == FallThrough) 01583 continue; 01584 FromBBI.BB->removeSuccessor(Succ); 01585 if (AddEdges && !ToBBI.BB->isSuccessor(Succ)) 01586 ToBBI.BB->addSuccessor(Succ); 01587 } 01588 01589 // Now FromBBI always falls through to the next block! 01590 if (NBB && !FromBBI.BB->isSuccessor(NBB)) 01591 FromBBI.BB->addSuccessor(NBB); 01592 01593 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(), 01594 std::back_inserter(ToBBI.Predicate)); 01595 FromBBI.Predicate.clear(); 01596 01597 ToBBI.NonPredSize += FromBBI.NonPredSize; 01598 ToBBI.ExtraCost += FromBBI.ExtraCost; 01599 ToBBI.ExtraCost2 += FromBBI.ExtraCost2; 01600 FromBBI.NonPredSize = 0; 01601 FromBBI.ExtraCost = 0; 01602 FromBBI.ExtraCost2 = 0; 01603 01604 ToBBI.ClobbersPred |= FromBBI.ClobbersPred; 01605 ToBBI.HasFallThrough = FromBBI.HasFallThrough; 01606 ToBBI.IsAnalyzed = false; 01607 FromBBI.IsAnalyzed = false; 01608 }