Index: include/llvm/CodeGen/MachineBasicBlock.h =================================================================== --- include/llvm/CodeGen/MachineBasicBlock.h +++ include/llvm/CodeGen/MachineBasicBlock.h @@ -805,6 +805,30 @@ MachineBasicBlock::iterator getInitial() { return I; } }; +/// Increment \p It until it points to a non-debug instruction or to \p End. +/// @param It Iterator to increment +/// @param End Iterator that points to end. Will be compared to It +/// @returns true if It == End, false otherwise. +template +inline bool skipDebugInstructionsForward(IterT &It, const IterT &End) { + while (It != End && It->isDebugValue()) + It++; + return It == End; +} + + +/// Decrement \p It until it points to a non-debug instruction or It == Begin. +/// @param It Iterator to decrement. +/// @param Begin Iterator that points to beginning. Will be compared to It +/// @returns true if It == Begin and It points to a debug value, false +// otherwise. +template +inline bool skipDebugInstructionsBackward(IterT &It, const IterT &Begin) { + while (It != Begin && It->isDebugValue()) + It--; + return It == Begin && It->isDebugValue(); +} + } // End llvm namespace #endif Index: lib/CodeGen/BranchFolding.cpp =================================================================== --- lib/CodeGen/BranchFolding.cpp +++ lib/CodeGen/BranchFolding.cpp @@ -1733,10 +1733,8 @@ // The terminator is probably a conditional branch, try not to separate the // branch from condition setting instruction. - MachineBasicBlock::iterator PI = Loc; - --PI; - while (PI != MBB->begin() && PI->isDebugValue()) - --PI; + MachineBasicBlock::iterator PI = std::prev(Loc); + skipDebugInstructionsBackward(PI, MBB->begin()); bool IsDef = false; for (const MachineOperand &MO : PI->operands()) { @@ -1830,18 +1828,11 @@ MachineBasicBlock::iterator FIE = FBB->end(); while (TIB != TIE && FIB != FIE) { // Skip dbg_value instructions. These do not count. - if (TIB->isDebugValue()) { - while (TIB != TIE && TIB->isDebugValue()) - ++TIB; - if (TIB == TIE) - break; - } - if (FIB->isDebugValue()) { - while (FIB != FIE && FIB->isDebugValue()) - ++FIB; - if (FIB == FIE) - break; - } + if (TIB->isDebugValue() && skipDebugInstructionsForward(TIB , TIE)) + break; + if (FIB->isDebugValue() && skipDebugInstructionsForward(FIB , TIE)) + break; + if (!TIB->isIdenticalTo(*FIB, MachineInstr::CheckKillDead)) break; Index: lib/CodeGen/IfConversion.cpp =================================================================== --- lib/CodeGen/IfConversion.cpp +++ lib/CodeGen/IfConversion.cpp @@ -588,18 +588,6 @@ return TExit && TExit == FalseBBI.BB; } -/// Increment \p It until it points to a non-debug instruction or to \p End. -/// @param It Iterator to increment -/// @param End Iterator that points to end. Will be compared to It -/// @returns true if It == End, false otherwise. -static inline bool skipDebugInstructionsForward( - MachineBasicBlock::iterator &It, - MachineBasicBlock::iterator &End) { - while (It != End && It->isDebugValue()) - It++; - return It == End; -} - /// Shrink the provided inclusive range by one instruction. /// If the range was one instruction (\p It == \p Begin), It is not modified, /// but \p Empty is set to true. @@ -613,21 +601,6 @@ It--; } -/// Decrement \p It until it points to a non-debug instruction or the range is -/// empty. -/// @param It Iterator to decrement. -/// @param Begin Iterator that points to beginning. Will be compared to It -/// @param Empty Set to true if the resulting range is Empty -/// @returns the value of Empty as a convenience. -static inline bool skipDebugInstructionsBackward( - MachineBasicBlock::iterator &Begin, - MachineBasicBlock::iterator &It, - bool &Empty) { - while (!Empty && It->isDebugValue()) - shrinkInclusiveRange(Begin, It, Empty); - return Empty; -} - /// Count duplicated instructions and move the iterators to show where they /// are. /// @param TIB True Iterator Begin @@ -718,9 +691,9 @@ // Count duplicate instructions at the ends of the blocks. while (!TEmpty && !FEmpty) { // Skip dbg_value instructions. These do not count. - if (skipDebugInstructionsBackward(TIB, TIE, TEmpty)) + if (TEmpty = skipDebugInstructionsBackward(TIE, TIB)) break; - if (skipDebugInstructionsBackward(FIB, FIE, FEmpty)) + if (FEmpty = skipDebugInstructionsBackward(FIE, FIB)) break; if (!TIE->isIdenticalTo(*FIE)) break; @@ -770,8 +743,8 @@ MachineBasicBlock::iterator E2 = std::prev(MBB2->end()); bool Empty1 = false, Empty2 = false; while (!Empty1 && !Empty2) { - skipDebugInstructionsBackward(B1, E1, Empty1); - skipDebugInstructionsBackward(B2, E2, Empty2); + Empty1 = skipDebugInstructionsBackward(E1, B1); + Empty2 = skipDebugInstructionsBackward(E2, B2); if (Empty1 && Empty2) break; Index: lib/CodeGen/MachineBasicBlock.cpp =================================================================== --- lib/CodeGen/MachineBasicBlock.cpp +++ lib/CodeGen/MachineBasicBlock.cpp @@ -191,9 +191,8 @@ MachineBasicBlock::iterator MachineBasicBlock::getFirstNonDebugInstr() { // Skip over begin-of-block dbg_value instructions. - iterator I = begin(), E = end(); - while (I != E && I->isDebugValue()) - ++I; + iterator I = begin(); + skipDebugInstructionsForward(I, end()); return I; } @@ -1140,17 +1139,10 @@ /// instructions. Return UnknownLoc if there is none. DebugLoc MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { - DebugLoc DL; - instr_iterator E = instr_end(); - if (MBBI == E) - return DL; - // Skip debug declarations, we don't want a DebugLoc from them. - while (MBBI != E && MBBI->isDebugValue()) - MBBI++; - if (MBBI != E) - DL = MBBI->getDebugLoc(); - return DL; + if (!skipDebugInstructionsForward(MBBI, instr_end())) + return MBBI->getDebugLoc(); + return {}; } /// Return probability of the edge from this block to MBB. Index: lib/CodeGen/MachineCSE.cpp =================================================================== --- lib/CodeGen/MachineCSE.cpp +++ lib/CodeGen/MachineCSE.cpp @@ -177,8 +177,7 @@ unsigned LookAheadLeft = LookAheadLimit; while (LookAheadLeft) { // Skip over dbg_value's. - while (I != E && I->isDebugValue()) - ++I; + skipDebugInstructionsForward(I, E); if (I == E) // Reached end of block, register is obviously dead. Index: lib/CodeGen/RegisterPressure.cpp =================================================================== --- lib/CodeGen/RegisterPressure.cpp +++ lib/CodeGen/RegisterPressure.cpp @@ -267,9 +267,7 @@ SlotIndex RegPressureTracker::getCurrSlot() const { MachineBasicBlock::const_iterator IdxPos = CurrPos; - while (IdxPos != MBB->end() && IdxPos->isDebugValue()) - ++IdxPos; - if (IdxPos == MBB->end()) + if (skipDebugInstructionsForward(IdxPos, MBB->end())) return LIS->getMBBEndIdx(MBB); return LIS->getInstructionIndex(*IdxPos).getRegSlot(); } @@ -813,9 +811,8 @@ static_cast(P).openTop(CurrPos); // Find the previous instruction. - do - --CurrPos; - while (CurrPos != MBB->begin() && CurrPos->isDebugValue()); + CurrPos--; + skipDebugInstructionsBackward(CurrPos, MBB->begin()); SlotIndex SlotIdx; if (RequireIntervals) @@ -891,9 +888,8 @@ bumpDeadDefs(RegOpers.DeadDefs); // Find the next instruction. - do - ++CurrPos; - while (CurrPos != MBB->end() && CurrPos->isDebugValue()); + ++CurrPos; + skipDebugInstructionsForward(CurrPos, MBB->end()); } void RegPressureTracker::advance() {