IfConversion.cpp revision 6a00fcb240adf82a3bd82068da70150ddb832407
1//===-- IfConversion.cpp - Machine code if conversion pass. ---------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the machine instruction level if-conversion pass. 11// 12//===----------------------------------------------------------------------===// 13 14#define DEBUG_TYPE "ifcvt" 15#include "llvm/Function.h" 16#include "llvm/CodeGen/Passes.h" 17#include "llvm/CodeGen/MachineModuleInfo.h" 18#include "llvm/CodeGen/MachineFunctionPass.h" 19#include "llvm/Target/TargetInstrInfo.h" 20#include "llvm/Target/TargetLowering.h" 21#include "llvm/Target/TargetMachine.h" 22#include "llvm/Support/CommandLine.h" 23#include "llvm/Support/Debug.h" 24#include "llvm/ADT/DepthFirstIterator.h" 25#include "llvm/ADT/Statistic.h" 26#include "llvm/ADT/STLExtras.h" 27using namespace llvm; 28 29// Hidden options for help debugging. 30static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden); 31static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden); 32static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden); 33static cl::opt<bool> DisableSimple("disable-ifcvt-simple", 34 cl::init(false), cl::Hidden); 35static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false", 36 cl::init(false), cl::Hidden); 37static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle", 38 cl::init(false), cl::Hidden); 39static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev", 40 cl::init(false), cl::Hidden); 41static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false", 42 cl::init(false), cl::Hidden); 43static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev", 44 cl::init(false), cl::Hidden); 45static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond", 46 cl::init(false), cl::Hidden); 47 48STATISTIC(NumSimple, "Number of simple if-conversions performed"); 49STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed"); 50STATISTIC(NumTriangle, "Number of triangle if-conversions performed"); 51STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed"); 52STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed"); 53STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed"); 54STATISTIC(NumDiamonds, "Number of diamond if-conversions performed"); 55STATISTIC(NumIfConvBBs, "Number of if-converted blocks"); 56STATISTIC(NumDupBBs, "Number of duplicated blocks"); 57 58namespace { 59 class VISIBILITY_HIDDEN IfConverter : public MachineFunctionPass { 60 enum IfcvtKind { 61 ICNotClassfied, // BB data valid, but not classified. 62 ICSimpleFalse, // Same as ICSimple, but on the false path. 63 ICSimple, // BB is entry of an one split, no rejoin sub-CFG. 64 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition. 65 ICTriangleRev, // Same as ICTriangle, but true path rev condition. 66 ICTriangleFalse, // Same as ICTriangle, but on the false path. 67 ICTriangle, // BB is entry of a triangle sub-CFG. 68 ICDiamond // BB is entry of a diamond sub-CFG. 69 }; 70 71 /// BBInfo - One per MachineBasicBlock, this is used to cache the result 72 /// if-conversion feasibility analysis. This includes results from 73 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its 74 /// classification, and common tail block of its successors (if it's a 75 /// diamond shape), its size, whether it's predicable, and whether any 76 /// instruction can clobber the 'would-be' predicate. 77 /// 78 /// IsDone - True if BB is not to be considered for ifcvt. 79 /// IsBeingAnalyzed - True if BB is currently being analyzed. 80 /// IsAnalyzed - True if BB has been analyzed (info is still valid). 81 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed. 82 /// IsBrAnalyzable - True if AnalyzeBranch() returns false. 83 /// HasFallThrough - True if BB may fallthrough to the following BB. 84 /// IsUnpredicable - True if BB is known to be unpredicable. 85 /// ClobbersPred - True if BB could modify predicates (e.g. has 86 /// cmp, call, etc.) 87 /// NonPredSize - Number of non-predicated instructions. 88 /// BB - Corresponding MachineBasicBlock. 89 /// TrueBB / FalseBB- See AnalyzeBranch(). 90 /// BrCond - Conditions for end of block conditional branches. 91 /// Predicate - Predicate used in the BB. 92 struct BBInfo { 93 bool IsDone : 1; 94 bool IsBeingAnalyzed : 1; 95 bool IsAnalyzed : 1; 96 bool IsEnqueued : 1; 97 bool IsBrAnalyzable : 1; 98 bool HasFallThrough : 1; 99 bool IsUnpredicable : 1; 100 bool CannotBeCopied : 1; 101 bool ClobbersPred : 1; 102 unsigned NonPredSize; 103 MachineBasicBlock *BB; 104 MachineBasicBlock *TrueBB; 105 MachineBasicBlock *FalseBB; 106 SmallVector<MachineOperand, 4> BrCond; 107 SmallVector<MachineOperand, 4> Predicate; 108 BBInfo() : IsDone(false), IsBeingAnalyzed(false), 109 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false), 110 HasFallThrough(false), IsUnpredicable(false), 111 CannotBeCopied(false), ClobbersPred(false), NonPredSize(0), 112 BB(0), TrueBB(0), FalseBB(0) {} 113 }; 114 115 /// IfcvtToken - Record information about pending if-conversions to attemp: 116 /// BBI - Corresponding BBInfo. 117 /// Kind - Type of block. See IfcvtKind. 118 /// NeedSubsumsion - True if the to be predicated BB has already been 119 /// predicated. 120 /// NumDups - Number of instructions that would be duplicated due 121 /// to this if-conversion. (For diamonds, the number of 122 /// identical instructions at the beginnings of both 123 /// paths). 124 /// NumDups2 - For diamonds, the number of identical instructions 125 /// at the ends of both paths. 126 struct IfcvtToken { 127 BBInfo &BBI; 128 IfcvtKind Kind; 129 bool NeedSubsumsion; 130 unsigned NumDups; 131 unsigned NumDups2; 132 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0) 133 : BBI(b), Kind(k), NeedSubsumsion(s), NumDups(d), NumDups2(d2) {} 134 }; 135 136 /// Roots - Basic blocks that do not have successors. These are the starting 137 /// points of Graph traversal. 138 std::vector<MachineBasicBlock*> Roots; 139 140 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by 141 /// basic block number. 142 std::vector<BBInfo> BBAnalysis; 143 144 const TargetLowering *TLI; 145 const TargetInstrInfo *TII; 146 bool MadeChange; 147 public: 148 static char ID; 149 IfConverter() : MachineFunctionPass(&ID) {} 150 151 virtual bool runOnMachineFunction(MachineFunction &MF); 152 virtual const char *getPassName() const { return "If Converter"; } 153 154 private: 155 bool ReverseBranchCondition(BBInfo &BBI); 156 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups) const; 157 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, 158 bool FalseBranch, unsigned &Dups) const; 159 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, 160 unsigned &Dups1, unsigned &Dups2) const; 161 void ScanInstructions(BBInfo &BBI); 162 BBInfo &AnalyzeBlock(MachineBasicBlock *BB, 163 std::vector<IfcvtToken*> &Tokens); 164 bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond, 165 bool isTriangle = false, bool RevBranch = false); 166 bool AnalyzeBlocks(MachineFunction &MF, 167 std::vector<IfcvtToken*> &Tokens); 168 void InvalidatePreds(MachineBasicBlock *BB); 169 void RemoveExtraEdges(BBInfo &BBI); 170 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind); 171 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind); 172 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, 173 unsigned NumDups1, unsigned NumDups2); 174 void PredicateBlock(BBInfo &BBI, 175 MachineBasicBlock::iterator E, 176 SmallVectorImpl<MachineOperand> &Cond); 177 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, 178 SmallVectorImpl<MachineOperand> &Cond, 179 bool IgnoreBr = false); 180 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI); 181 182 bool MeetIfcvtSizeLimit(unsigned Size) const { 183 return Size > 0 && Size <= TLI->getIfCvtBlockSizeLimit(); 184 } 185 186 // blockAlwaysFallThrough - Block ends without a terminator. 187 bool blockAlwaysFallThrough(BBInfo &BBI) const { 188 return BBI.IsBrAnalyzable && BBI.TrueBB == NULL; 189 } 190 191 // IfcvtTokenCmp - Used to sort if-conversion candidates. 192 static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) { 193 int Incr1 = (C1->Kind == ICDiamond) 194 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups; 195 int Incr2 = (C2->Kind == ICDiamond) 196 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups; 197 if (Incr1 > Incr2) 198 return true; 199 else if (Incr1 == Incr2) { 200 // Favors subsumsion. 201 if (C1->NeedSubsumsion == false && C2->NeedSubsumsion == true) 202 return true; 203 else if (C1->NeedSubsumsion == C2->NeedSubsumsion) { 204 // Favors diamond over triangle, etc. 205 if ((unsigned)C1->Kind < (unsigned)C2->Kind) 206 return true; 207 else if (C1->Kind == C2->Kind) 208 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber(); 209 } 210 } 211 return false; 212 } 213 }; 214 215 char IfConverter::ID = 0; 216} 217 218static RegisterPass<IfConverter> 219X("if-converter", "If Converter"); 220 221FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); } 222 223bool IfConverter::runOnMachineFunction(MachineFunction &MF) { 224 TLI = MF.getTarget().getTargetLowering(); 225 TII = MF.getTarget().getInstrInfo(); 226 if (!TII) return false; 227 228 static int FnNum = -1; 229 DOUT << "\nIfcvt: function (" << ++FnNum << ") \'" 230 << MF.getFunction()->getName() << "\'"; 231 232 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) { 233 DOUT << " skipped\n"; 234 return false; 235 } 236 DOUT << "\n"; 237 238 MF.RenumberBlocks(); 239 BBAnalysis.resize(MF.getNumBlockIDs()); 240 241 // Look for root nodes, i.e. blocks without successors. 242 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) 243 if (I->succ_empty()) 244 Roots.push_back(I); 245 246 std::vector<IfcvtToken*> Tokens; 247 MadeChange = false; 248 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + 249 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds; 250 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) { 251 // Do an intial analysis for each basic block and finding all the potential 252 // candidates to perform if-convesion. 253 bool Change = AnalyzeBlocks(MF, Tokens); 254 while (!Tokens.empty()) { 255 IfcvtToken *Token = Tokens.back(); 256 Tokens.pop_back(); 257 BBInfo &BBI = Token->BBI; 258 IfcvtKind Kind = Token->Kind; 259 260 // If the block has been evicted out of the queue or it has already been 261 // marked dead (due to it being predicated), then skip it. 262 if (BBI.IsDone) 263 BBI.IsEnqueued = false; 264 if (!BBI.IsEnqueued) 265 continue; 266 267 BBI.IsEnqueued = false; 268 269 bool RetVal = false; 270 switch (Kind) { 271 default: assert(false && "Unexpected!"); 272 break; 273 case ICSimple: 274 case ICSimpleFalse: { 275 bool isFalse = Kind == ICSimpleFalse; 276 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break; 277 DOUT << "Ifcvt (Simple" << (Kind == ICSimpleFalse ? " false" :"") 278 << "): BB#" << BBI.BB->getNumber() << " (" 279 << ((Kind == ICSimpleFalse) 280 ? BBI.FalseBB->getNumber() 281 : BBI.TrueBB->getNumber()) << ") "; 282 RetVal = IfConvertSimple(BBI, Kind); 283 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n"; 284 if (RetVal) { 285 if (isFalse) NumSimpleFalse++; 286 else NumSimple++; 287 } 288 break; 289 } 290 case ICTriangle: 291 case ICTriangleRev: 292 case ICTriangleFalse: 293 case ICTriangleFRev: { 294 bool isFalse = Kind == ICTriangleFalse; 295 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev); 296 if (DisableTriangle && !isFalse && !isRev) break; 297 if (DisableTriangleR && !isFalse && isRev) break; 298 if (DisableTriangleF && isFalse && !isRev) break; 299 if (DisableTriangleFR && isFalse && isRev) break; 300 DOUT << "Ifcvt (Triangle"; 301 if (isFalse) 302 DOUT << " false"; 303 if (isRev) 304 DOUT << " rev"; 305 DOUT << "): BB#" << BBI.BB->getNumber() << " (T:" 306 << BBI.TrueBB->getNumber() << ",F:" 307 << BBI.FalseBB->getNumber() << ") "; 308 RetVal = IfConvertTriangle(BBI, Kind); 309 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n"; 310 if (RetVal) { 311 if (isFalse) { 312 if (isRev) NumTriangleFRev++; 313 else NumTriangleFalse++; 314 } else { 315 if (isRev) NumTriangleRev++; 316 else NumTriangle++; 317 } 318 } 319 break; 320 } 321 case ICDiamond: { 322 if (DisableDiamond) break; 323 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:" 324 << BBI.TrueBB->getNumber() << ",F:" 325 << BBI.FalseBB->getNumber() << ") "; 326 RetVal = IfConvertDiamond(BBI, Kind, Token->NumDups, Token->NumDups2); 327 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n"; 328 if (RetVal) NumDiamonds++; 329 break; 330 } 331 } 332 333 Change |= RetVal; 334 335 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev + 336 NumTriangleFalse + NumTriangleFRev + NumDiamonds; 337 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit) 338 break; 339 } 340 341 if (!Change) 342 break; 343 MadeChange |= Change; 344 } 345 346 // Delete tokens in case of early exit. 347 while (!Tokens.empty()) { 348 IfcvtToken *Token = Tokens.back(); 349 Tokens.pop_back(); 350 delete Token; 351 } 352 353 Tokens.clear(); 354 Roots.clear(); 355 BBAnalysis.clear(); 356 357 return MadeChange; 358} 359 360/// findFalseBlock - BB has a fallthrough. Find its 'false' successor given 361/// its 'true' successor. 362static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB, 363 MachineBasicBlock *TrueBB) { 364 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), 365 E = BB->succ_end(); SI != E; ++SI) { 366 MachineBasicBlock *SuccBB = *SI; 367 if (SuccBB != TrueBB) 368 return SuccBB; 369 } 370 return NULL; 371} 372 373/// ReverseBranchCondition - Reverse the condition of the end of the block 374/// branchs. Swap block's 'true' and 'false' successors. 375bool IfConverter::ReverseBranchCondition(BBInfo &BBI) { 376 if (!TII->ReverseBranchCondition(BBI.BrCond)) { 377 TII->RemoveBranch(*BBI.BB); 378 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond); 379 std::swap(BBI.TrueBB, BBI.FalseBB); 380 return true; 381 } 382 return false; 383} 384 385/// getNextBlock - Returns the next block in the function blocks ordering. If 386/// it is the end, returns NULL. 387static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) { 388 MachineFunction::iterator I = BB; 389 MachineFunction::iterator E = BB->getParent()->end(); 390 if (++I == E) 391 return NULL; 392 return I; 393} 394 395/// ValidSimple - Returns true if the 'true' block (along with its 396/// predecessor) forms a valid simple shape for ifcvt. It also returns the 397/// number of instructions that the ifcvt would need to duplicate if performed 398/// in Dups. 399bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups) const { 400 Dups = 0; 401 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) 402 return false; 403 404 if (TrueBBI.IsBrAnalyzable) 405 return false; 406 407 if (TrueBBI.BB->pred_size() > 1) { 408 if (TrueBBI.CannotBeCopied || 409 TrueBBI.NonPredSize > TLI->getIfCvtDupBlockSizeLimit()) 410 return false; 411 Dups = TrueBBI.NonPredSize; 412 } 413 414 return true; 415} 416 417/// ValidTriangle - Returns true if the 'true' and 'false' blocks (along 418/// with their common predecessor) forms a valid triangle shape for ifcvt. 419/// If 'FalseBranch' is true, it checks if 'true' block's false branch 420/// branches to the false branch rather than the other way around. It also 421/// returns the number of instructions that the ifcvt would need to duplicate 422/// if performed in 'Dups'. 423bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, 424 bool FalseBranch, unsigned &Dups) const { 425 Dups = 0; 426 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) 427 return false; 428 429 if (TrueBBI.BB->pred_size() > 1) { 430 if (TrueBBI.CannotBeCopied) 431 return false; 432 433 unsigned Size = TrueBBI.NonPredSize; 434 if (TrueBBI.IsBrAnalyzable) { 435 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty()) 436 // End with an unconditional branch. It will be removed. 437 --Size; 438 else { 439 MachineBasicBlock *FExit = FalseBranch 440 ? TrueBBI.TrueBB : TrueBBI.FalseBB; 441 if (FExit) 442 // Require a conditional branch 443 ++Size; 444 } 445 } 446 if (Size > TLI->getIfCvtDupBlockSizeLimit()) 447 return false; 448 Dups = Size; 449 } 450 451 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB; 452 if (!TExit && blockAlwaysFallThrough(TrueBBI)) { 453 MachineFunction::iterator I = TrueBBI.BB; 454 if (++I == TrueBBI.BB->getParent()->end()) 455 return false; 456 TExit = I; 457 } 458 return TExit && TExit == FalseBBI.BB; 459} 460 461static 462MachineBasicBlock::iterator firstNonBranchInst(MachineBasicBlock *BB, 463 const TargetInstrInfo *TII) { 464 MachineBasicBlock::iterator I = BB->end(); 465 while (I != BB->begin()) { 466 --I; 467 if (!I->getDesc().isBranch()) 468 break; 469 } 470 return I; 471} 472 473/// ValidDiamond - Returns true if the 'true' and 'false' blocks (along 474/// with their common predecessor) forms a valid diamond shape for ifcvt. 475bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, 476 unsigned &Dups1, unsigned &Dups2) const { 477 Dups1 = Dups2 = 0; 478 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone || 479 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone) 480 return false; 481 482 MachineBasicBlock *TT = TrueBBI.TrueBB; 483 MachineBasicBlock *FT = FalseBBI.TrueBB; 484 485 if (!TT && blockAlwaysFallThrough(TrueBBI)) 486 TT = getNextBlock(TrueBBI.BB); 487 if (!FT && blockAlwaysFallThrough(FalseBBI)) 488 FT = getNextBlock(FalseBBI.BB); 489 if (TT != FT) 490 return false; 491 if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable)) 492 return false; 493 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) 494 return false; 495 496 // FIXME: Allow true block to have an early exit? 497 if (TrueBBI.FalseBB || FalseBBI.FalseBB || 498 (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)) 499 return false; 500 501 MachineBasicBlock::iterator TI = TrueBBI.BB->begin(); 502 MachineBasicBlock::iterator FI = FalseBBI.BB->begin(); 503 while (TI != TrueBBI.BB->end() && FI != FalseBBI.BB->end()) { 504 if (!TI->isIdenticalTo(FI)) 505 break; 506 ++Dups1; 507 ++TI; 508 ++FI; 509 } 510 511 TI = firstNonBranchInst(TrueBBI.BB, TII); 512 FI = firstNonBranchInst(FalseBBI.BB, TII); 513 while (TI != TrueBBI.BB->begin() && FI != FalseBBI.BB->begin()) { 514 if (!TI->isIdenticalTo(FI)) 515 break; 516 ++Dups2; 517 --TI; 518 --FI; 519 } 520 521 return true; 522} 523 524/// ScanInstructions - Scan all the instructions in the block to determine if 525/// the block is predicable. In most cases, that means all the instructions 526/// in the block are isPredicable(). Also checks if the block contains any 527/// instruction which can clobber a predicate (e.g. condition code register). 528/// If so, the block is not predicable unless it's the last instruction. 529void IfConverter::ScanInstructions(BBInfo &BBI) { 530 if (BBI.IsDone) 531 return; 532 533 bool AlreadyPredicated = BBI.Predicate.size() > 0; 534 // First analyze the end of BB branches. 535 BBI.TrueBB = BBI.FalseBB = NULL; 536 BBI.BrCond.clear(); 537 BBI.IsBrAnalyzable = 538 !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond); 539 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL; 540 541 if (BBI.BrCond.size()) { 542 // No false branch. This BB must end with a conditional branch and a 543 // fallthrough. 544 if (!BBI.FalseBB) 545 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB); 546 assert(BBI.FalseBB && "Expected to find the fallthrough block!"); 547 } 548 549 // Then scan all the instructions. 550 BBI.NonPredSize = 0; 551 BBI.ClobbersPred = false; 552 bool SeenCondBr = false; 553 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end(); 554 I != E; ++I) { 555 const TargetInstrDesc &TID = I->getDesc(); 556 if (TID.isNotDuplicable()) 557 BBI.CannotBeCopied = true; 558 559 bool isPredicated = TII->isPredicated(I); 560 bool isCondBr = BBI.IsBrAnalyzable && TID.isConditionalBranch(); 561 562 if (!isCondBr) { 563 if (!isPredicated) 564 BBI.NonPredSize++; 565 else if (!AlreadyPredicated) { 566 // FIXME: This instruction is already predicated before the 567 // if-conversion pass. It's probably something like a conditional move. 568 // Mark this block unpredicable for now. 569 BBI.IsUnpredicable = true; 570 return; 571 } 572 573 } 574 575 if (BBI.ClobbersPred && !isPredicated) { 576 // Predicate modification instruction should end the block (except for 577 // already predicated instructions and end of block branches). 578 if (isCondBr) { 579 SeenCondBr = true; 580 581 // Conditional branches is not predicable. But it may be eliminated. 582 continue; 583 } 584 585 // Predicate may have been modified, the subsequent (currently) 586 // unpredicated instructions cannot be correctly predicated. 587 BBI.IsUnpredicable = true; 588 return; 589 } 590 591 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are 592 // still potentially predicable. 593 std::vector<MachineOperand> PredDefs; 594 if (TII->DefinesPredicate(I, PredDefs)) 595 BBI.ClobbersPred = true; 596 597 if (!TID.isPredicable()) { 598 BBI.IsUnpredicable = true; 599 return; 600 } 601 } 602} 603 604/// FeasibilityAnalysis - Determine if the block is a suitable candidate to be 605/// predicated by the specified predicate. 606bool IfConverter::FeasibilityAnalysis(BBInfo &BBI, 607 SmallVectorImpl<MachineOperand> &Pred, 608 bool isTriangle, bool RevBranch) { 609 // If the block is dead or unpredicable, then it cannot be predicated. 610 if (BBI.IsDone || BBI.IsUnpredicable) 611 return false; 612 613 // If it is already predicated, check if its predicate subsumes the new 614 // predicate. 615 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Pred)) 616 return false; 617 618 if (BBI.BrCond.size()) { 619 if (!isTriangle) 620 return false; 621 622 // Test predicate subsumsion. 623 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end()); 624 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 625 if (RevBranch) { 626 if (TII->ReverseBranchCondition(Cond)) 627 return false; 628 } 629 if (TII->ReverseBranchCondition(RevPred) || 630 !TII->SubsumesPredicate(Cond, RevPred)) 631 return false; 632 } 633 634 return true; 635} 636 637/// AnalyzeBlock - Analyze the structure of the sub-CFG starting from 638/// the specified block. Record its successors and whether it looks like an 639/// if-conversion candidate. 640IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB, 641 std::vector<IfcvtToken*> &Tokens) { 642 BBInfo &BBI = BBAnalysis[BB->getNumber()]; 643 644 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) 645 return BBI; 646 647 BBI.BB = BB; 648 BBI.IsBeingAnalyzed = true; 649 650 ScanInstructions(BBI); 651 652 // Unanalyable or ends with fallthrough or unconditional branch. 653 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty()) { 654 BBI.IsBeingAnalyzed = false; 655 BBI.IsAnalyzed = true; 656 return BBI; 657 } 658 659 // Do not ifcvt if either path is a back edge to the entry block. 660 if (BBI.TrueBB == BB || BBI.FalseBB == BB) { 661 BBI.IsBeingAnalyzed = false; 662 BBI.IsAnalyzed = true; 663 return BBI; 664 } 665 666 BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB, Tokens); 667 BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens); 668 669 if (TrueBBI.IsDone && FalseBBI.IsDone) { 670 BBI.IsBeingAnalyzed = false; 671 BBI.IsAnalyzed = true; 672 return BBI; 673 } 674 675 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 676 bool CanRevCond = !TII->ReverseBranchCondition(RevCond); 677 678 unsigned Dups = 0; 679 unsigned Dups2 = 0; 680 bool TNeedSub = TrueBBI.Predicate.size() > 0; 681 bool FNeedSub = FalseBBI.Predicate.size() > 0; 682 bool Enqueued = false; 683 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) && 684 MeetIfcvtSizeLimit(TrueBBI.NonPredSize - (Dups + Dups2)) && 685 MeetIfcvtSizeLimit(FalseBBI.NonPredSize - (Dups + Dups2)) && 686 FeasibilityAnalysis(TrueBBI, BBI.BrCond) && 687 FeasibilityAnalysis(FalseBBI, RevCond)) { 688 // Diamond: 689 // EBB 690 // / \_ 691 // | | 692 // TBB FBB 693 // \ / 694 // TailBB 695 // Note TailBB can be empty. 696 Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups, 697 Dups2)); 698 Enqueued = true; 699 } 700 701 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups) && 702 MeetIfcvtSizeLimit(TrueBBI.NonPredSize) && 703 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) { 704 // Triangle: 705 // EBB 706 // | \_ 707 // | | 708 // | TBB 709 // | / 710 // FBB 711 Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups)); 712 Enqueued = true; 713 } 714 715 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups) && 716 MeetIfcvtSizeLimit(TrueBBI.NonPredSize) && 717 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) { 718 Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups)); 719 Enqueued = true; 720 } 721 722 if (ValidSimple(TrueBBI, Dups) && 723 MeetIfcvtSizeLimit(TrueBBI.NonPredSize) && 724 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) { 725 // Simple (split, no rejoin): 726 // EBB 727 // | \_ 728 // | | 729 // | TBB---> exit 730 // | 731 // FBB 732 Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups)); 733 Enqueued = true; 734 } 735 736 if (CanRevCond) { 737 // Try the other path... 738 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups) && 739 MeetIfcvtSizeLimit(FalseBBI.NonPredSize) && 740 FeasibilityAnalysis(FalseBBI, RevCond, true)) { 741 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups)); 742 Enqueued = true; 743 } 744 745 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups) && 746 MeetIfcvtSizeLimit(FalseBBI.NonPredSize) && 747 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) { 748 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups)); 749 Enqueued = true; 750 } 751 752 if (ValidSimple(FalseBBI, Dups) && 753 MeetIfcvtSizeLimit(FalseBBI.NonPredSize) && 754 FeasibilityAnalysis(FalseBBI, RevCond)) { 755 Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups)); 756 Enqueued = true; 757 } 758 } 759 760 BBI.IsEnqueued = Enqueued; 761 BBI.IsBeingAnalyzed = false; 762 BBI.IsAnalyzed = true; 763 return BBI; 764} 765 766/// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion 767/// candidates. It returns true if any CFG restructuring is done to expose more 768/// if-conversion opportunities. 769bool IfConverter::AnalyzeBlocks(MachineFunction &MF, 770 std::vector<IfcvtToken*> &Tokens) { 771 bool Change = false; 772 std::set<MachineBasicBlock*> Visited; 773 for (unsigned i = 0, e = Roots.size(); i != e; ++i) { 774 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited), 775 E = idf_ext_end(Roots[i], Visited); I != E; ++I) { 776 MachineBasicBlock *BB = *I; 777 AnalyzeBlock(BB, Tokens); 778 } 779 } 780 781 // Sort to favor more complex ifcvt scheme. 782 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp); 783 784 return Change; 785} 786 787/// canFallThroughTo - Returns true either if ToBB is the next block after BB or 788/// that all the intervening blocks are empty (given BB can fall through to its 789/// next block). 790static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) { 791 MachineFunction::iterator I = BB; 792 MachineFunction::iterator TI = ToBB; 793 MachineFunction::iterator E = BB->getParent()->end(); 794 while (++I != TI) 795 if (I == E || !I->empty()) 796 return false; 797 return true; 798} 799 800/// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed 801/// to determine if it can be if-converted. If predecessor is already enqueued, 802/// dequeue it! 803void IfConverter::InvalidatePreds(MachineBasicBlock *BB) { 804 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(), 805 E = BB->pred_end(); PI != E; ++PI) { 806 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()]; 807 if (PBBI.IsDone || PBBI.BB == BB) 808 continue; 809 PBBI.IsAnalyzed = false; 810 PBBI.IsEnqueued = false; 811 } 812} 813 814/// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB. 815/// 816static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB, 817 const TargetInstrInfo *TII) { 818 SmallVector<MachineOperand, 0> NoCond; 819 TII->InsertBranch(*BB, ToBB, NULL, NoCond); 820} 821 822/// RemoveExtraEdges - Remove true / false edges if either / both are no longer 823/// successors. 824void IfConverter::RemoveExtraEdges(BBInfo &BBI) { 825 MachineBasicBlock *TBB = NULL, *FBB = NULL; 826 SmallVector<MachineOperand, 4> Cond; 827 if (!TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond)) 828 BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); 829} 830 831/// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG. 832/// 833bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) { 834 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 835 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 836 BBInfo *CvtBBI = &TrueBBI; 837 BBInfo *NextBBI = &FalseBBI; 838 839 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 840 if (Kind == ICSimpleFalse) 841 std::swap(CvtBBI, NextBBI); 842 843 if (CvtBBI->IsDone || 844 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) { 845 // Something has changed. It's no longer safe to predicate this block. 846 BBI.IsAnalyzed = false; 847 CvtBBI->IsAnalyzed = false; 848 return false; 849 } 850 851 if (Kind == ICSimpleFalse) 852 if (TII->ReverseBranchCondition(Cond)) 853 assert(false && "Unable to reverse branch condition!"); 854 855 if (CvtBBI->BB->pred_size() > 1) { 856 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 857 // Copy instructions in the true block, predicate them add them to 858 // the entry block. 859 CopyAndPredicateBlock(BBI, *CvtBBI, Cond); 860 } else { 861 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond); 862 863 // Merge converted block into entry block. 864 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 865 MergeBlocks(BBI, *CvtBBI); 866 } 867 868 bool IterIfcvt = true; 869 if (!canFallThroughTo(BBI.BB, NextBBI->BB)) { 870 InsertUncondBranch(BBI.BB, NextBBI->BB, TII); 871 BBI.HasFallThrough = false; 872 // Now ifcvt'd block will look like this: 873 // BB: 874 // ... 875 // t, f = cmp 876 // if t op 877 // b BBf 878 // 879 // We cannot further ifcvt this block because the unconditional branch 880 // will have to be predicated on the new condition, that will not be 881 // available if cmp executes. 882 IterIfcvt = false; 883 } 884 885 RemoveExtraEdges(BBI); 886 887 // Update block info. BB can be iteratively if-converted. 888 if (!IterIfcvt) 889 BBI.IsDone = true; 890 InvalidatePreds(BBI.BB); 891 CvtBBI->IsDone = true; 892 893 // FIXME: Must maintain LiveIns. 894 return true; 895} 896 897/// IfConvertTriangle - If convert a triangle sub-CFG. 898/// 899bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) { 900 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 901 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 902 BBInfo *CvtBBI = &TrueBBI; 903 BBInfo *NextBBI = &FalseBBI; 904 905 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 906 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) 907 std::swap(CvtBBI, NextBBI); 908 909 if (CvtBBI->IsDone || 910 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) { 911 // Something has changed. It's no longer safe to predicate this block. 912 BBI.IsAnalyzed = false; 913 CvtBBI->IsAnalyzed = false; 914 return false; 915 } 916 917 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) 918 if (TII->ReverseBranchCondition(Cond)) 919 assert(false && "Unable to reverse branch condition!"); 920 921 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) { 922 if (ReverseBranchCondition(*CvtBBI)) { 923 // BB has been changed, modify its predecessors (except for this 924 // one) so they don't get ifcvt'ed based on bad intel. 925 for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(), 926 E = CvtBBI->BB->pred_end(); PI != E; ++PI) { 927 MachineBasicBlock *PBB = *PI; 928 if (PBB == BBI.BB) 929 continue; 930 BBInfo &PBBI = BBAnalysis[PBB->getNumber()]; 931 if (PBBI.IsEnqueued) { 932 PBBI.IsAnalyzed = false; 933 PBBI.IsEnqueued = false; 934 } 935 } 936 } 937 } 938 939 bool HasEarlyExit = CvtBBI->FalseBB != NULL; 940 bool DupBB = CvtBBI->BB->pred_size() > 1; 941 if (DupBB) { 942 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 943 // Copy instructions in the true block, predicate them add them to 944 // the entry block. 945 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true); 946 } else { 947 // Predicate the 'true' block after removing its branch. 948 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB); 949 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond); 950 } 951 952 if (!DupBB) { 953 // Now merge the entry of the triangle with the true block. 954 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 955 MergeBlocks(BBI, *CvtBBI); 956 } 957 958 // If 'true' block has a 'false' successor, add an exit branch to it. 959 if (HasEarlyExit) { 960 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(), 961 CvtBBI->BrCond.end()); 962 if (TII->ReverseBranchCondition(RevCond)) 963 assert(false && "Unable to reverse branch condition!"); 964 TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond); 965 BBI.BB->addSuccessor(CvtBBI->FalseBB); 966 } 967 968 // Merge in the 'false' block if the 'false' block has no other 969 // predecessors. Otherwise, add a unconditional branch from to 'false'. 970 bool FalseBBDead = false; 971 bool IterIfcvt = true; 972 bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB); 973 if (!isFallThrough) { 974 // Only merge them if the true block does not fallthrough to the false 975 // block. By not merging them, we make it possible to iteratively 976 // ifcvt the blocks. 977 if (!HasEarlyExit && 978 NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough) { 979 MergeBlocks(BBI, *NextBBI); 980 FalseBBDead = true; 981 } else { 982 InsertUncondBranch(BBI.BB, NextBBI->BB, TII); 983 BBI.HasFallThrough = false; 984 } 985 // Mixed predicated and unpredicated code. This cannot be iteratively 986 // predicated. 987 IterIfcvt = false; 988 } 989 990 RemoveExtraEdges(BBI); 991 992 // Update block info. BB can be iteratively if-converted. 993 if (!IterIfcvt) 994 BBI.IsDone = true; 995 InvalidatePreds(BBI.BB); 996 CvtBBI->IsDone = true; 997 if (FalseBBDead) 998 NextBBI->IsDone = true; 999 1000 // FIXME: Must maintain LiveIns. 1001 return true; 1002} 1003 1004/// IfConvertDiamond - If convert a diamond sub-CFG. 1005/// 1006bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, 1007 unsigned NumDups1, unsigned NumDups2) { 1008 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 1009 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 1010 MachineBasicBlock *TailBB = TrueBBI.TrueBB; 1011 // True block must fall through or ended with unanalyzable terminator. 1012 if (!TailBB) { 1013 if (blockAlwaysFallThrough(TrueBBI)) 1014 TailBB = FalseBBI.TrueBB; 1015 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!"); 1016 } 1017 1018 if (TrueBBI.IsDone || FalseBBI.IsDone || 1019 TrueBBI.BB->pred_size() > 1 || 1020 FalseBBI.BB->pred_size() > 1) { 1021 // Something has changed. It's no longer safe to predicate these blocks. 1022 BBI.IsAnalyzed = false; 1023 TrueBBI.IsAnalyzed = false; 1024 FalseBBI.IsAnalyzed = false; 1025 return false; 1026 } 1027 1028 // Merge the 'true' and 'false' blocks by copying the instructions 1029 // from the 'false' block to the 'true' block. That is, unless the true 1030 // block would clobber the predicate, in that case, do the opposite. 1031 BBInfo *BBI1 = &TrueBBI; 1032 BBInfo *BBI2 = &FalseBBI; 1033 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 1034 if (TII->ReverseBranchCondition(RevCond)) 1035 assert(false && "Unable to reverse branch condition!"); 1036 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond; 1037 SmallVector<MachineOperand, 4> *Cond2 = &RevCond; 1038 1039 // Figure out the more profitable ordering. 1040 bool DoSwap = false; 1041 if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred) 1042 DoSwap = true; 1043 else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) { 1044 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize) 1045 DoSwap = true; 1046 } 1047 if (DoSwap) { 1048 std::swap(BBI1, BBI2); 1049 std::swap(Cond1, Cond2); 1050 } 1051 1052 // Remove the conditional branch from entry to the blocks. 1053 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 1054 1055 // Remove the duplicated instructions at the beginnings of both paths. 1056 MachineBasicBlock::iterator DI1 = BBI1->BB->begin(); 1057 MachineBasicBlock::iterator DI2 = BBI2->BB->begin(); 1058 BBI1->NonPredSize -= NumDups1; 1059 BBI2->NonPredSize -= NumDups1; 1060 while (NumDups1 != 0) { 1061 ++DI1; 1062 ++DI2; 1063 --NumDups1; 1064 } 1065 BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1); 1066 BBI2->BB->erase(BBI2->BB->begin(), DI2); 1067 1068 // Predicate the 'true' block after removing its branch. 1069 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB); 1070 DI1 = BBI1->BB->end(); 1071 for (unsigned i = 0; i != NumDups2; ++i) 1072 --DI1; 1073 BBI1->BB->erase(DI1, BBI1->BB->end()); 1074 PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1); 1075 1076 // Predicate the 'false' block. 1077 BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB); 1078 DI2 = BBI2->BB->end(); 1079 while (NumDups2 != 0) { 1080 --DI2; 1081 --NumDups2; 1082 } 1083 PredicateBlock(*BBI2, DI2, *Cond2); 1084 1085 // Merge the true block into the entry of the diamond. 1086 MergeBlocks(BBI, *BBI1); 1087 MergeBlocks(BBI, *BBI2); 1088 1089 // If the if-converted block fallthrough or unconditionally branch into the 1090 // tail block, and the tail block does not have other predecessors, then 1091 // fold the tail block in as well. Otherwise, unless it falls through to the 1092 // tail, add a unconditional branch to it. 1093 if (TailBB) { 1094 BBInfo TailBBI = BBAnalysis[TailBB->getNumber()]; 1095 if (TailBB->pred_size() == 1 && !TailBBI.HasFallThrough) { 1096 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); 1097 MergeBlocks(BBI, TailBBI); 1098 TailBBI.IsDone = true; 1099 } else { 1100 InsertUncondBranch(BBI.BB, TailBB, TII); 1101 BBI.HasFallThrough = false; 1102 } 1103 } 1104 1105 RemoveExtraEdges(BBI); 1106 1107 // Update block info. 1108 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true; 1109 InvalidatePreds(BBI.BB); 1110 1111 // FIXME: Must maintain LiveIns. 1112 return true; 1113} 1114 1115/// PredicateBlock - Predicate instructions from the start of the block to the 1116/// specified end with the specified condition. 1117void IfConverter::PredicateBlock(BBInfo &BBI, 1118 MachineBasicBlock::iterator E, 1119 SmallVectorImpl<MachineOperand> &Cond) { 1120 for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) { 1121 if (TII->isPredicated(I)) 1122 continue; 1123 if (!TII->PredicateInstruction(I, Cond)) { 1124 cerr << "Unable to predicate " << *I << "!\n"; 1125 abort(); 1126 } 1127 } 1128 1129 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate)); 1130 1131 BBI.IsAnalyzed = false; 1132 BBI.NonPredSize = 0; 1133 1134 NumIfConvBBs++; 1135} 1136 1137/// CopyAndPredicateBlock - Copy and predicate instructions from source BB to 1138/// the destination block. Skip end of block branches if IgnoreBr is true. 1139void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, 1140 SmallVectorImpl<MachineOperand> &Cond, 1141 bool IgnoreBr) { 1142 MachineFunction &MF = *ToBBI.BB->getParent(); 1143 1144 for (MachineBasicBlock::iterator I = FromBBI.BB->begin(), 1145 E = FromBBI.BB->end(); I != E; ++I) { 1146 const TargetInstrDesc &TID = I->getDesc(); 1147 bool isPredicated = TII->isPredicated(I); 1148 // Do not copy the end of the block branches. 1149 if (IgnoreBr && !isPredicated && TID.isBranch()) 1150 break; 1151 1152 MachineInstr *MI = MF.CloneMachineInstr(I); 1153 ToBBI.BB->insert(ToBBI.BB->end(), MI); 1154 ToBBI.NonPredSize++; 1155 1156 if (!isPredicated) 1157 if (!TII->PredicateInstruction(MI, Cond)) { 1158 cerr << "Unable to predicate " << *MI << "!\n"; 1159 abort(); 1160 } 1161 } 1162 1163 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(), 1164 FromBBI.BB->succ_end()); 1165 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB); 1166 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL; 1167 1168 for (unsigned i = 0, e = Succs.size(); i != e; ++i) { 1169 MachineBasicBlock *Succ = Succs[i]; 1170 // Fallthrough edge can't be transferred. 1171 if (Succ == FallThrough) 1172 continue; 1173 if (!ToBBI.BB->isSuccessor(Succ)) 1174 ToBBI.BB->addSuccessor(Succ); 1175 } 1176 1177 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(), 1178 std::back_inserter(ToBBI.Predicate)); 1179 std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate)); 1180 1181 ToBBI.ClobbersPred |= FromBBI.ClobbersPred; 1182 ToBBI.IsAnalyzed = false; 1183 1184 NumDupBBs++; 1185} 1186 1187/// MergeBlocks - Move all instructions from FromBB to the end of ToBB. 1188/// 1189void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) { 1190 ToBBI.BB->splice(ToBBI.BB->end(), 1191 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end()); 1192 1193 // Redirect all branches to FromBB to ToBB. 1194 std::vector<MachineBasicBlock *> Preds(FromBBI.BB->pred_begin(), 1195 FromBBI.BB->pred_end()); 1196 for (unsigned i = 0, e = Preds.size(); i != e; ++i) { 1197 MachineBasicBlock *Pred = Preds[i]; 1198 if (Pred == ToBBI.BB) 1199 continue; 1200 Pred->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB); 1201 } 1202 1203 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(), 1204 FromBBI.BB->succ_end()); 1205 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB); 1206 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL; 1207 1208 for (unsigned i = 0, e = Succs.size(); i != e; ++i) { 1209 MachineBasicBlock *Succ = Succs[i]; 1210 // Fallthrough edge can't be transferred. 1211 if (Succ == FallThrough) 1212 continue; 1213 FromBBI.BB->removeSuccessor(Succ); 1214 if (!ToBBI.BB->isSuccessor(Succ)) 1215 ToBBI.BB->addSuccessor(Succ); 1216 } 1217 1218 // Now FromBBI always fall through to the next block! 1219 if (NBB && !FromBBI.BB->isSuccessor(NBB)) 1220 FromBBI.BB->addSuccessor(NBB); 1221 1222 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(), 1223 std::back_inserter(ToBBI.Predicate)); 1224 FromBBI.Predicate.clear(); 1225 1226 ToBBI.NonPredSize += FromBBI.NonPredSize; 1227 FromBBI.NonPredSize = 0; 1228 1229 ToBBI.ClobbersPred |= FromBBI.ClobbersPred; 1230 ToBBI.HasFallThrough = FromBBI.HasFallThrough; 1231 ToBBI.IsAnalyzed = false; 1232 FromBBI.IsAnalyzed = false; 1233} 1234