- if (MBB.succ_size() <= 1) return false;
-
- // Walk the basic block bottom-up
- for (MachineBasicBlock::iterator I = MBB.end(); I != MBB.begin(); ){
- MachineBasicBlock::iterator LastIt = I;
- if (SinkInstruction(--I)) {
- I = LastIt;
- ++NumSunk;
- }
- }
+ if (MBB.succ_size() <= 1 || MBB.empty()) return false;
+
+ bool MadeChange = false;
+
+ // Walk the basic block bottom-up. Remember if we saw a store.
+ MachineBasicBlock::iterator I = MBB.end();
+ --I;
+ bool ProcessedBegin, SawStore = false;
+ do {
+ MachineInstr *MI = I; // The instruction to sink.
+
+ // Predecrement I (if it's not begin) so that it isn't invalidated by
+ // sinking.
+ ProcessedBegin = I == MBB.begin();
+ if (!ProcessedBegin)
+ --I;
+
+ if (SinkInstruction(MI, SawStore))
+ ++NumSunk, MadeChange = true;
+
+ // If we just processed the first instruction in the block, we're done.
+ } while (!ProcessedBegin);