LLVM API Documentation
00001 //===-- MachineSink.cpp - Sinking for machine instructions ----------------===// 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 pass moves instructions into successor blocks when possible, so that 00011 // they aren't executed on paths where their results aren't needed. 00012 // 00013 // This pass is not intended to be a replacement or a complete alternative 00014 // for an LLVM-IR-level sinking pass. It is only designed to sink simple 00015 // constructs that are not exposed before lowering and instruction selection. 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #include "llvm/CodeGen/Passes.h" 00020 #include "llvm/ADT/SetVector.h" 00021 #include "llvm/ADT/SmallSet.h" 00022 #include "llvm/ADT/Statistic.h" 00023 #include "llvm/Analysis/AliasAnalysis.h" 00024 #include "llvm/CodeGen/MachineDominators.h" 00025 #include "llvm/CodeGen/MachineLoopInfo.h" 00026 #include "llvm/CodeGen/MachinePostDominators.h" 00027 #include "llvm/CodeGen/MachineRegisterInfo.h" 00028 #include "llvm/Support/CommandLine.h" 00029 #include "llvm/Support/Debug.h" 00030 #include "llvm/Support/raw_ostream.h" 00031 #include "llvm/Target/TargetInstrInfo.h" 00032 #include "llvm/Target/TargetMachine.h" 00033 #include "llvm/Target/TargetRegisterInfo.h" 00034 #include "llvm/Target/TargetSubtargetInfo.h" 00035 using namespace llvm; 00036 00037 #define DEBUG_TYPE "machine-sink" 00038 00039 static cl::opt<bool> 00040 SplitEdges("machine-sink-split", 00041 cl::desc("Split critical edges during machine sinking"), 00042 cl::init(true), cl::Hidden); 00043 00044 STATISTIC(NumSunk, "Number of machine instructions sunk"); 00045 STATISTIC(NumSplit, "Number of critical edges split"); 00046 STATISTIC(NumCoalesces, "Number of copies coalesced"); 00047 00048 namespace { 00049 class MachineSinking : public MachineFunctionPass { 00050 const TargetInstrInfo *TII; 00051 const TargetRegisterInfo *TRI; 00052 MachineRegisterInfo *MRI; // Machine register information 00053 MachineDominatorTree *DT; // Machine dominator tree 00054 MachinePostDominatorTree *PDT; // Machine post dominator tree 00055 MachineLoopInfo *LI; 00056 AliasAnalysis *AA; 00057 00058 // Remember which edges have been considered for breaking. 00059 SmallSet<std::pair<MachineBasicBlock*,MachineBasicBlock*>, 8> 00060 CEBCandidates; 00061 // Remember which edges we are about to split. 00062 // This is different from CEBCandidates since those edges 00063 // will be split. 00064 SetVector<std::pair<MachineBasicBlock*,MachineBasicBlock*> > ToSplit; 00065 00066 public: 00067 static char ID; // Pass identification 00068 MachineSinking() : MachineFunctionPass(ID) { 00069 initializeMachineSinkingPass(*PassRegistry::getPassRegistry()); 00070 } 00071 00072 bool runOnMachineFunction(MachineFunction &MF) override; 00073 00074 void getAnalysisUsage(AnalysisUsage &AU) const override { 00075 AU.setPreservesCFG(); 00076 MachineFunctionPass::getAnalysisUsage(AU); 00077 AU.addRequired<AliasAnalysis>(); 00078 AU.addRequired<MachineDominatorTree>(); 00079 AU.addRequired<MachinePostDominatorTree>(); 00080 AU.addRequired<MachineLoopInfo>(); 00081 AU.addPreserved<MachineDominatorTree>(); 00082 AU.addPreserved<MachinePostDominatorTree>(); 00083 AU.addPreserved<MachineLoopInfo>(); 00084 } 00085 00086 void releaseMemory() override { 00087 CEBCandidates.clear(); 00088 } 00089 00090 private: 00091 bool ProcessBlock(MachineBasicBlock &MBB); 00092 bool isWorthBreakingCriticalEdge(MachineInstr *MI, 00093 MachineBasicBlock *From, 00094 MachineBasicBlock *To); 00095 /// \brief Postpone the splitting of the given critical 00096 /// edge (\p From, \p To). 00097 /// 00098 /// We do not split the edges on the fly. Indeed, this invalidates 00099 /// the dominance information and thus triggers a lot of updates 00100 /// of that information underneath. 00101 /// Instead, we postpone all the splits after each iteration of 00102 /// the main loop. That way, the information is at least valid 00103 /// for the lifetime of an iteration. 00104 /// 00105 /// \return True if the edge is marked as toSplit, false otherwise. 00106 /// False can be retruned if, for instance, this is not profitable. 00107 bool PostponeSplitCriticalEdge(MachineInstr *MI, 00108 MachineBasicBlock *From, 00109 MachineBasicBlock *To, 00110 bool BreakPHIEdge); 00111 bool SinkInstruction(MachineInstr *MI, bool &SawStore); 00112 bool AllUsesDominatedByBlock(unsigned Reg, MachineBasicBlock *MBB, 00113 MachineBasicBlock *DefMBB, 00114 bool &BreakPHIEdge, bool &LocalUse) const; 00115 MachineBasicBlock *FindSuccToSinkTo(MachineInstr *MI, MachineBasicBlock *MBB, 00116 bool &BreakPHIEdge); 00117 bool isProfitableToSinkTo(unsigned Reg, MachineInstr *MI, 00118 MachineBasicBlock *MBB, 00119 MachineBasicBlock *SuccToSinkTo); 00120 00121 bool PerformTrivialForwardCoalescing(MachineInstr *MI, 00122 MachineBasicBlock *MBB); 00123 }; 00124 } // end anonymous namespace 00125 00126 char MachineSinking::ID = 0; 00127 char &llvm::MachineSinkingID = MachineSinking::ID; 00128 INITIALIZE_PASS_BEGIN(MachineSinking, "machine-sink", 00129 "Machine code sinking", false, false) 00130 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 00131 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 00132 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 00133 INITIALIZE_PASS_END(MachineSinking, "machine-sink", 00134 "Machine code sinking", false, false) 00135 00136 bool MachineSinking::PerformTrivialForwardCoalescing(MachineInstr *MI, 00137 MachineBasicBlock *MBB) { 00138 if (!MI->isCopy()) 00139 return false; 00140 00141 unsigned SrcReg = MI->getOperand(1).getReg(); 00142 unsigned DstReg = MI->getOperand(0).getReg(); 00143 if (!TargetRegisterInfo::isVirtualRegister(SrcReg) || 00144 !TargetRegisterInfo::isVirtualRegister(DstReg) || 00145 !MRI->hasOneNonDBGUse(SrcReg)) 00146 return false; 00147 00148 const TargetRegisterClass *SRC = MRI->getRegClass(SrcReg); 00149 const TargetRegisterClass *DRC = MRI->getRegClass(DstReg); 00150 if (SRC != DRC) 00151 return false; 00152 00153 MachineInstr *DefMI = MRI->getVRegDef(SrcReg); 00154 if (DefMI->isCopyLike()) 00155 return false; 00156 DEBUG(dbgs() << "Coalescing: " << *DefMI); 00157 DEBUG(dbgs() << "*** to: " << *MI); 00158 MRI->replaceRegWith(DstReg, SrcReg); 00159 MI->eraseFromParent(); 00160 00161 // Conservatively, clear any kill flags, since it's possible that they are no 00162 // longer correct. 00163 MRI->clearKillFlags(SrcReg); 00164 00165 ++NumCoalesces; 00166 return true; 00167 } 00168 00169 /// AllUsesDominatedByBlock - Return true if all uses of the specified register 00170 /// occur in blocks dominated by the specified block. If any use is in the 00171 /// definition block, then return false since it is never legal to move def 00172 /// after uses. 00173 bool 00174 MachineSinking::AllUsesDominatedByBlock(unsigned Reg, 00175 MachineBasicBlock *MBB, 00176 MachineBasicBlock *DefMBB, 00177 bool &BreakPHIEdge, 00178 bool &LocalUse) const { 00179 assert(TargetRegisterInfo::isVirtualRegister(Reg) && 00180 "Only makes sense for vregs"); 00181 00182 // Ignore debug uses because debug info doesn't affect the code. 00183 if (MRI->use_nodbg_empty(Reg)) 00184 return true; 00185 00186 // BreakPHIEdge is true if all the uses are in the successor MBB being sunken 00187 // into and they are all PHI nodes. In this case, machine-sink must break 00188 // the critical edge first. e.g. 00189 // 00190 // BB#1: derived from LLVM BB %bb4.preheader 00191 // Predecessors according to CFG: BB#0 00192 // ... 00193 // %reg16385<def> = DEC64_32r %reg16437, %EFLAGS<imp-def,dead> 00194 // ... 00195 // JE_4 <BB#37>, %EFLAGS<imp-use> 00196 // Successors according to CFG: BB#37 BB#2 00197 // 00198 // BB#2: derived from LLVM BB %bb.nph 00199 // Predecessors according to CFG: BB#0 BB#1 00200 // %reg16386<def> = PHI %reg16434, <BB#0>, %reg16385, <BB#1> 00201 BreakPHIEdge = true; 00202 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { 00203 MachineInstr *UseInst = MO.getParent(); 00204 unsigned OpNo = &MO - &UseInst->getOperand(0); 00205 MachineBasicBlock *UseBlock = UseInst->getParent(); 00206 if (!(UseBlock == MBB && UseInst->isPHI() && 00207 UseInst->getOperand(OpNo+1).getMBB() == DefMBB)) { 00208 BreakPHIEdge = false; 00209 break; 00210 } 00211 } 00212 if (BreakPHIEdge) 00213 return true; 00214 00215 for (MachineOperand &MO : MRI->use_nodbg_operands(Reg)) { 00216 // Determine the block of the use. 00217 MachineInstr *UseInst = MO.getParent(); 00218 unsigned OpNo = &MO - &UseInst->getOperand(0); 00219 MachineBasicBlock *UseBlock = UseInst->getParent(); 00220 if (UseInst->isPHI()) { 00221 // PHI nodes use the operand in the predecessor block, not the block with 00222 // the PHI. 00223 UseBlock = UseInst->getOperand(OpNo+1).getMBB(); 00224 } else if (UseBlock == DefMBB) { 00225 LocalUse = true; 00226 return false; 00227 } 00228 00229 // Check that it dominates. 00230 if (!DT->dominates(MBB, UseBlock)) 00231 return false; 00232 } 00233 00234 return true; 00235 } 00236 00237 bool MachineSinking::runOnMachineFunction(MachineFunction &MF) { 00238 if (skipOptnoneFunction(*MF.getFunction())) 00239 return false; 00240 00241 DEBUG(dbgs() << "******** Machine Sinking ********\n"); 00242 00243 const TargetMachine &TM = MF.getTarget(); 00244 TII = TM.getSubtargetImpl()->getInstrInfo(); 00245 TRI = TM.getSubtargetImpl()->getRegisterInfo(); 00246 MRI = &MF.getRegInfo(); 00247 DT = &getAnalysis<MachineDominatorTree>(); 00248 PDT = &getAnalysis<MachinePostDominatorTree>(); 00249 LI = &getAnalysis<MachineLoopInfo>(); 00250 AA = &getAnalysis<AliasAnalysis>(); 00251 00252 bool EverMadeChange = false; 00253 00254 while (1) { 00255 bool MadeChange = false; 00256 00257 // Process all basic blocks. 00258 CEBCandidates.clear(); 00259 ToSplit.clear(); 00260 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); 00261 I != E; ++I) 00262 MadeChange |= ProcessBlock(*I); 00263 00264 // If we have anything we marked as toSplit, split it now. 00265 for (auto &Pair : ToSplit) { 00266 auto NewSucc = Pair.first->SplitCriticalEdge(Pair.second, this); 00267 if (NewSucc != nullptr) { 00268 DEBUG(dbgs() << " *** Splitting critical edge:" 00269 " BB#" << Pair.first->getNumber() 00270 << " -- BB#" << NewSucc->getNumber() 00271 << " -- BB#" << Pair.second->getNumber() << '\n'); 00272 MadeChange = true; 00273 ++NumSplit; 00274 } else 00275 DEBUG(dbgs() << " *** Not legal to break critical edge\n"); 00276 } 00277 // If this iteration over the code changed anything, keep iterating. 00278 if (!MadeChange) break; 00279 EverMadeChange = true; 00280 } 00281 return EverMadeChange; 00282 } 00283 00284 bool MachineSinking::ProcessBlock(MachineBasicBlock &MBB) { 00285 // Can't sink anything out of a block that has less than two successors. 00286 if (MBB.succ_size() <= 1 || MBB.empty()) return false; 00287 00288 // Don't bother sinking code out of unreachable blocks. In addition to being 00289 // unprofitable, it can also lead to infinite looping, because in an 00290 // unreachable loop there may be nowhere to stop. 00291 if (!DT->isReachableFromEntry(&MBB)) return false; 00292 00293 bool MadeChange = false; 00294 00295 // Walk the basic block bottom-up. Remember if we saw a store. 00296 MachineBasicBlock::iterator I = MBB.end(); 00297 --I; 00298 bool ProcessedBegin, SawStore = false; 00299 do { 00300 MachineInstr *MI = I; // The instruction to sink. 00301 00302 // Predecrement I (if it's not begin) so that it isn't invalidated by 00303 // sinking. 00304 ProcessedBegin = I == MBB.begin(); 00305 if (!ProcessedBegin) 00306 --I; 00307 00308 if (MI->isDebugValue()) 00309 continue; 00310 00311 bool Joined = PerformTrivialForwardCoalescing(MI, &MBB); 00312 if (Joined) { 00313 MadeChange = true; 00314 continue; 00315 } 00316 00317 if (SinkInstruction(MI, SawStore)) 00318 ++NumSunk, MadeChange = true; 00319 00320 // If we just processed the first instruction in the block, we're done. 00321 } while (!ProcessedBegin); 00322 00323 return MadeChange; 00324 } 00325 00326 bool MachineSinking::isWorthBreakingCriticalEdge(MachineInstr *MI, 00327 MachineBasicBlock *From, 00328 MachineBasicBlock *To) { 00329 // FIXME: Need much better heuristics. 00330 00331 // If the pass has already considered breaking this edge (during this pass 00332 // through the function), then let's go ahead and break it. This means 00333 // sinking multiple "cheap" instructions into the same block. 00334 if (!CEBCandidates.insert(std::make_pair(From, To))) 00335 return true; 00336 00337 if (!MI->isCopy() && !TII->isAsCheapAsAMove(MI)) 00338 return true; 00339 00340 // MI is cheap, we probably don't want to break the critical edge for it. 00341 // However, if this would allow some definitions of its source operands 00342 // to be sunk then it's probably worth it. 00343 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00344 const MachineOperand &MO = MI->getOperand(i); 00345 if (!MO.isReg() || !MO.isUse()) 00346 continue; 00347 unsigned Reg = MO.getReg(); 00348 if (Reg == 0) 00349 continue; 00350 00351 // We don't move live definitions of physical registers, 00352 // so sinking their uses won't enable any opportunities. 00353 if (TargetRegisterInfo::isPhysicalRegister(Reg)) 00354 continue; 00355 00356 // If this instruction is the only user of a virtual register, 00357 // check if breaking the edge will enable sinking 00358 // both this instruction and the defining instruction. 00359 if (MRI->hasOneNonDBGUse(Reg)) { 00360 // If the definition resides in same MBB, 00361 // claim it's likely we can sink these together. 00362 // If definition resides elsewhere, we aren't 00363 // blocking it from being sunk so don't break the edge. 00364 MachineInstr *DefMI = MRI->getVRegDef(Reg); 00365 if (DefMI->getParent() == MI->getParent()) 00366 return true; 00367 } 00368 } 00369 00370 return false; 00371 } 00372 00373 bool MachineSinking::PostponeSplitCriticalEdge(MachineInstr *MI, 00374 MachineBasicBlock *FromBB, 00375 MachineBasicBlock *ToBB, 00376 bool BreakPHIEdge) { 00377 if (!isWorthBreakingCriticalEdge(MI, FromBB, ToBB)) 00378 return false; 00379 00380 // Avoid breaking back edge. From == To means backedge for single BB loop. 00381 if (!SplitEdges || FromBB == ToBB) 00382 return false; 00383 00384 // Check for backedges of more "complex" loops. 00385 if (LI->getLoopFor(FromBB) == LI->getLoopFor(ToBB) && 00386 LI->isLoopHeader(ToBB)) 00387 return false; 00388 00389 // It's not always legal to break critical edges and sink the computation 00390 // to the edge. 00391 // 00392 // BB#1: 00393 // v1024 00394 // Beq BB#3 00395 // <fallthrough> 00396 // BB#2: 00397 // ... no uses of v1024 00398 // <fallthrough> 00399 // BB#3: 00400 // ... 00401 // = v1024 00402 // 00403 // If BB#1 -> BB#3 edge is broken and computation of v1024 is inserted: 00404 // 00405 // BB#1: 00406 // ... 00407 // Bne BB#2 00408 // BB#4: 00409 // v1024 = 00410 // B BB#3 00411 // BB#2: 00412 // ... no uses of v1024 00413 // <fallthrough> 00414 // BB#3: 00415 // ... 00416 // = v1024 00417 // 00418 // This is incorrect since v1024 is not computed along the BB#1->BB#2->BB#3 00419 // flow. We need to ensure the new basic block where the computation is 00420 // sunk to dominates all the uses. 00421 // It's only legal to break critical edge and sink the computation to the 00422 // new block if all the predecessors of "To", except for "From", are 00423 // not dominated by "From". Given SSA property, this means these 00424 // predecessors are dominated by "To". 00425 // 00426 // There is no need to do this check if all the uses are PHI nodes. PHI 00427 // sources are only defined on the specific predecessor edges. 00428 if (!BreakPHIEdge) { 00429 for (MachineBasicBlock::pred_iterator PI = ToBB->pred_begin(), 00430 E = ToBB->pred_end(); PI != E; ++PI) { 00431 if (*PI == FromBB) 00432 continue; 00433 if (!DT->dominates(ToBB, *PI)) 00434 return false; 00435 } 00436 } 00437 00438 ToSplit.insert(std::make_pair(FromBB, ToBB)); 00439 00440 return true; 00441 } 00442 00443 static bool AvoidsSinking(MachineInstr *MI, MachineRegisterInfo *MRI) { 00444 return MI->isInsertSubreg() || MI->isSubregToReg() || MI->isRegSequence(); 00445 } 00446 00447 /// collectDebgValues - Scan instructions following MI and collect any 00448 /// matching DBG_VALUEs. 00449 static void collectDebugValues(MachineInstr *MI, 00450 SmallVectorImpl<MachineInstr *> &DbgValues) { 00451 DbgValues.clear(); 00452 if (!MI->getOperand(0).isReg()) 00453 return; 00454 00455 MachineBasicBlock::iterator DI = MI; ++DI; 00456 for (MachineBasicBlock::iterator DE = MI->getParent()->end(); 00457 DI != DE; ++DI) { 00458 if (!DI->isDebugValue()) 00459 return; 00460 if (DI->getOperand(0).isReg() && 00461 DI->getOperand(0).getReg() == MI->getOperand(0).getReg()) 00462 DbgValues.push_back(DI); 00463 } 00464 } 00465 00466 /// isProfitableToSinkTo - Return true if it is profitable to sink MI. 00467 bool MachineSinking::isProfitableToSinkTo(unsigned Reg, MachineInstr *MI, 00468 MachineBasicBlock *MBB, 00469 MachineBasicBlock *SuccToSinkTo) { 00470 assert (MI && "Invalid MachineInstr!"); 00471 assert (SuccToSinkTo && "Invalid SinkTo Candidate BB"); 00472 00473 if (MBB == SuccToSinkTo) 00474 return false; 00475 00476 // It is profitable if SuccToSinkTo does not post dominate current block. 00477 if (!PDT->dominates(SuccToSinkTo, MBB)) 00478 return true; 00479 00480 // Check if only use in post dominated block is PHI instruction. 00481 bool NonPHIUse = false; 00482 for (MachineInstr &UseInst : MRI->use_nodbg_instructions(Reg)) { 00483 MachineBasicBlock *UseBlock = UseInst.getParent(); 00484 if (UseBlock == SuccToSinkTo && !UseInst.isPHI()) 00485 NonPHIUse = true; 00486 } 00487 if (!NonPHIUse) 00488 return true; 00489 00490 // If SuccToSinkTo post dominates then also it may be profitable if MI 00491 // can further profitably sinked into another block in next round. 00492 bool BreakPHIEdge = false; 00493 // FIXME - If finding successor is compile time expensive then catch results. 00494 if (MachineBasicBlock *MBB2 = FindSuccToSinkTo(MI, SuccToSinkTo, BreakPHIEdge)) 00495 return isProfitableToSinkTo(Reg, MI, SuccToSinkTo, MBB2); 00496 00497 // If SuccToSinkTo is final destination and it is a post dominator of current 00498 // block then it is not profitable to sink MI into SuccToSinkTo block. 00499 return false; 00500 } 00501 00502 /// FindSuccToSinkTo - Find a successor to sink this instruction to. 00503 MachineBasicBlock *MachineSinking::FindSuccToSinkTo(MachineInstr *MI, 00504 MachineBasicBlock *MBB, 00505 bool &BreakPHIEdge) { 00506 00507 assert (MI && "Invalid MachineInstr!"); 00508 assert (MBB && "Invalid MachineBasicBlock!"); 00509 00510 // Loop over all the operands of the specified instruction. If there is 00511 // anything we can't handle, bail out. 00512 00513 // SuccToSinkTo - This is the successor to sink this instruction to, once we 00514 // decide. 00515 MachineBasicBlock *SuccToSinkTo = nullptr; 00516 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00517 const MachineOperand &MO = MI->getOperand(i); 00518 if (!MO.isReg()) continue; // Ignore non-register operands. 00519 00520 unsigned Reg = MO.getReg(); 00521 if (Reg == 0) continue; 00522 00523 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 00524 if (MO.isUse()) { 00525 // If the physreg has no defs anywhere, it's just an ambient register 00526 // and we can freely move its uses. Alternatively, if it's allocatable, 00527 // it could get allocated to something with a def during allocation. 00528 if (!MRI->isConstantPhysReg(Reg, *MBB->getParent())) 00529 return nullptr; 00530 } else if (!MO.isDead()) { 00531 // A def that isn't dead. We can't move it. 00532 return nullptr; 00533 } 00534 } else { 00535 // Virtual register uses are always safe to sink. 00536 if (MO.isUse()) continue; 00537 00538 // If it's not safe to move defs of the register class, then abort. 00539 if (!TII->isSafeToMoveRegClassDefs(MRI->getRegClass(Reg))) 00540 return nullptr; 00541 00542 // FIXME: This picks a successor to sink into based on having one 00543 // successor that dominates all the uses. However, there are cases where 00544 // sinking can happen but where the sink point isn't a successor. For 00545 // example: 00546 // 00547 // x = computation 00548 // if () {} else {} 00549 // use x 00550 // 00551 // the instruction could be sunk over the whole diamond for the 00552 // if/then/else (or loop, etc), allowing it to be sunk into other blocks 00553 // after that. 00554 00555 // Virtual register defs can only be sunk if all their uses are in blocks 00556 // dominated by one of the successors. 00557 if (SuccToSinkTo) { 00558 // If a previous operand picked a block to sink to, then this operand 00559 // must be sinkable to the same block. 00560 bool LocalUse = false; 00561 if (!AllUsesDominatedByBlock(Reg, SuccToSinkTo, MBB, 00562 BreakPHIEdge, LocalUse)) 00563 return nullptr; 00564 00565 continue; 00566 } 00567 00568 // Otherwise, we should look at all the successors and decide which one 00569 // we should sink to. 00570 // We give successors with smaller loop depth higher priority. 00571 SmallVector<MachineBasicBlock*, 4> Succs(MBB->succ_begin(), MBB->succ_end()); 00572 // Sort Successors according to their loop depth. 00573 std::stable_sort( 00574 Succs.begin(), Succs.end(), 00575 [this](const MachineBasicBlock *LHS, const MachineBasicBlock *RHS) { 00576 return LI->getLoopDepth(LHS) < LI->getLoopDepth(RHS); 00577 }); 00578 for (SmallVectorImpl<MachineBasicBlock *>::iterator SI = Succs.begin(), 00579 E = Succs.end(); SI != E; ++SI) { 00580 MachineBasicBlock *SuccBlock = *SI; 00581 bool LocalUse = false; 00582 if (AllUsesDominatedByBlock(Reg, SuccBlock, MBB, 00583 BreakPHIEdge, LocalUse)) { 00584 SuccToSinkTo = SuccBlock; 00585 break; 00586 } 00587 if (LocalUse) 00588 // Def is used locally, it's never safe to move this def. 00589 return nullptr; 00590 } 00591 00592 // If we couldn't find a block to sink to, ignore this instruction. 00593 if (!SuccToSinkTo) 00594 return nullptr; 00595 if (!isProfitableToSinkTo(Reg, MI, MBB, SuccToSinkTo)) 00596 return nullptr; 00597 } 00598 } 00599 00600 // It is not possible to sink an instruction into its own block. This can 00601 // happen with loops. 00602 if (MBB == SuccToSinkTo) 00603 return nullptr; 00604 00605 // It's not safe to sink instructions to EH landing pad. Control flow into 00606 // landing pad is implicitly defined. 00607 if (SuccToSinkTo && SuccToSinkTo->isLandingPad()) 00608 return nullptr; 00609 00610 return SuccToSinkTo; 00611 } 00612 00613 /// SinkInstruction - Determine whether it is safe to sink the specified machine 00614 /// instruction out of its current block into a successor. 00615 bool MachineSinking::SinkInstruction(MachineInstr *MI, bool &SawStore) { 00616 // Don't sink insert_subreg, subreg_to_reg, reg_sequence. These are meant to 00617 // be close to the source to make it easier to coalesce. 00618 if (AvoidsSinking(MI, MRI)) 00619 return false; 00620 00621 // Check if it's safe to move the instruction. 00622 if (!MI->isSafeToMove(TII, AA, SawStore)) 00623 return false; 00624 00625 // FIXME: This should include support for sinking instructions within the 00626 // block they are currently in to shorten the live ranges. We often get 00627 // instructions sunk into the top of a large block, but it would be better to 00628 // also sink them down before their first use in the block. This xform has to 00629 // be careful not to *increase* register pressure though, e.g. sinking 00630 // "x = y + z" down if it kills y and z would increase the live ranges of y 00631 // and z and only shrink the live range of x. 00632 00633 bool BreakPHIEdge = false; 00634 MachineBasicBlock *ParentBlock = MI->getParent(); 00635 MachineBasicBlock *SuccToSinkTo = FindSuccToSinkTo(MI, ParentBlock, BreakPHIEdge); 00636 00637 // If there are no outputs, it must have side-effects. 00638 if (!SuccToSinkTo) 00639 return false; 00640 00641 00642 // If the instruction to move defines a dead physical register which is live 00643 // when leaving the basic block, don't move it because it could turn into a 00644 // "zombie" define of that preg. E.g., EFLAGS. (<rdar://problem/8030636>) 00645 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { 00646 const MachineOperand &MO = MI->getOperand(I); 00647 if (!MO.isReg()) continue; 00648 unsigned Reg = MO.getReg(); 00649 if (Reg == 0 || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; 00650 if (SuccToSinkTo->isLiveIn(Reg)) 00651 return false; 00652 } 00653 00654 DEBUG(dbgs() << "Sink instr " << *MI << "\tinto block " << *SuccToSinkTo); 00655 00656 // If the block has multiple predecessors, this is a critical edge. 00657 // Decide if we can sink along it or need to break the edge. 00658 if (SuccToSinkTo->pred_size() > 1) { 00659 // We cannot sink a load across a critical edge - there may be stores in 00660 // other code paths. 00661 bool TryBreak = false; 00662 bool store = true; 00663 if (!MI->isSafeToMove(TII, AA, store)) { 00664 DEBUG(dbgs() << " *** NOTE: Won't sink load along critical edge.\n"); 00665 TryBreak = true; 00666 } 00667 00668 // We don't want to sink across a critical edge if we don't dominate the 00669 // successor. We could be introducing calculations to new code paths. 00670 if (!TryBreak && !DT->dominates(ParentBlock, SuccToSinkTo)) { 00671 DEBUG(dbgs() << " *** NOTE: Critical edge found\n"); 00672 TryBreak = true; 00673 } 00674 00675 // Don't sink instructions into a loop. 00676 if (!TryBreak && LI->isLoopHeader(SuccToSinkTo)) { 00677 DEBUG(dbgs() << " *** NOTE: Loop header found\n"); 00678 TryBreak = true; 00679 } 00680 00681 // Otherwise we are OK with sinking along a critical edge. 00682 if (!TryBreak) 00683 DEBUG(dbgs() << "Sinking along critical edge.\n"); 00684 else { 00685 // Mark this edge as to be split. 00686 // If the edge can actually be split, the next iteration of the main loop 00687 // will sink MI in the newly created block. 00688 bool Status = 00689 PostponeSplitCriticalEdge(MI, ParentBlock, SuccToSinkTo, BreakPHIEdge); 00690 if (!Status) 00691 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to " 00692 "break critical edge\n"); 00693 // The instruction will not be sunk this time. 00694 return false; 00695 } 00696 } 00697 00698 if (BreakPHIEdge) { 00699 // BreakPHIEdge is true if all the uses are in the successor MBB being 00700 // sunken into and they are all PHI nodes. In this case, machine-sink must 00701 // break the critical edge first. 00702 bool Status = PostponeSplitCriticalEdge(MI, ParentBlock, 00703 SuccToSinkTo, BreakPHIEdge); 00704 if (!Status) 00705 DEBUG(dbgs() << " *** PUNTING: Not legal or profitable to " 00706 "break critical edge\n"); 00707 // The instruction will not be sunk this time. 00708 return false; 00709 } 00710 00711 // Determine where to insert into. Skip phi nodes. 00712 MachineBasicBlock::iterator InsertPos = SuccToSinkTo->begin(); 00713 while (InsertPos != SuccToSinkTo->end() && InsertPos->isPHI()) 00714 ++InsertPos; 00715 00716 // collect matching debug values. 00717 SmallVector<MachineInstr *, 2> DbgValuesToSink; 00718 collectDebugValues(MI, DbgValuesToSink); 00719 00720 // Move the instruction. 00721 SuccToSinkTo->splice(InsertPos, ParentBlock, MI, 00722 ++MachineBasicBlock::iterator(MI)); 00723 00724 // Move debug values. 00725 for (SmallVectorImpl<MachineInstr *>::iterator DBI = DbgValuesToSink.begin(), 00726 DBE = DbgValuesToSink.end(); DBI != DBE; ++DBI) { 00727 MachineInstr *DbgMI = *DBI; 00728 SuccToSinkTo->splice(InsertPos, ParentBlock, DbgMI, 00729 ++MachineBasicBlock::iterator(DbgMI)); 00730 } 00731 00732 // Conservatively, clear any kill flags, since it's possible that they are no 00733 // longer correct. 00734 MI->clearKillInfo(); 00735 00736 return true; 00737 }