- // Go bottom up if RefsInMBB is empty and the end of the mbb isn't beyond
- // the last index in the live range.
- if (RefsInMBB.empty() && LastIdx >= EndIdx) {
- MachineBasicBlock::iterator MII = MBB->getFirstTerminator();
- MachineBasicBlock::iterator EndPt = MI;
-
- if (MII == EndPt) return Pt;
-
- --MII;
- do {
- unsigned Index = LIs->getInstructionIndex(MII);
- unsigned Gap = LIs->findGapBeforeInstr(Index);
-
- // We can't insert a restore between the barrier (a call) and its
- // corresponding call frame teardown.
- if (MII->getOpcode() == TRI->getCallFrameDestroyOpcode()) {
- bool reachedBarrier = false;
- while (MII->getOpcode() != TRI->getCallFrameSetupOpcode()) {
- --MII;
- if (MII == EndPt) {
- reachedBarrier = true;
- break;
- }
- }
-
- if (reachedBarrier) break;
- else continue;
- } else if (Gap) {
- Pt = MII;
- RestoreIndex = Gap;
- break;
- }
-
- --MII;
- } while (MII != EndPt);
- } else {
- MachineBasicBlock::iterator MII = MI;
- MII = ++MII;
-
- // FIXME: Limit the number of instructions to examine to reduce
- // compile time?
- while (MII != MBB->getFirstTerminator()) {
- unsigned Index = LIs->getInstructionIndex(MII);
- if (Index > LastIdx)
- break;
- unsigned Gap = LIs->findGapBeforeInstr(Index);
+ // We start at the call, so walk forward until we find the call frame teardown
+ // since we can't insert restores before that. Bail if we encounter a use
+ // during this time.
+ MachineBasicBlock::iterator MII = MI;
+ if (MII == EndPt) return Pt;
+
+ while (MII != EndPt && !RefsInMBB.count(MII) &&
+ MII->getOpcode() != TRI->getCallFrameDestroyOpcode())
+ ++MII;
+ if (MII == EndPt || RefsInMBB.count(MII)) return Pt;
+ ++MII;
+
+ // FIXME: Limit the number of instructions to examine to reduce
+ // compile time?
+ while (MII != EndPt) {
+ SlotIndex Index = LIs->getInstructionIndex(MII);
+ if (Index > LastIdx)
+ break;