[WebAssembly] Don't use range-based loop for a list that's being modified

The first instruction in a block is what the rend() iterator points to, so
if it moves, we need to re-evaluate rend() so that we continue to iterate
through the rest of the instructions.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256953 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman 2016-01-06 18:29:35 +00:00
parent 79d91e563f
commit c7e3f5ac69
2 changed files with 6 additions and 3 deletions

View File

@ -147,8 +147,10 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
// block boundaries, and the blocks aren't ordered so the block visitation
// order isn't significant, but we may want to change this in the future.
for (MachineBasicBlock &MBB : MF) {
for (MachineInstr &MI : reverse(MBB)) {
MachineInstr *Insert = &MI;
// Don't use a range-based for loop, because we modify the list as we're
// iterating over it and the end iterator may change.
for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
MachineInstr *Insert = &*MII;
// Don't nest anything inside a phi.
if (Insert->getOpcode() == TargetOpcode::PHI)
break;
@ -221,7 +223,7 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
Insert = Def;
}
if (AnyStackified)
ImposeStackOrdering(&MI);
ImposeStackOrdering(&*MII);
}
}

View File

@ -93,6 +93,7 @@ back:
; Test that a simple loop is handled as expected.
; CHECK-LABEL: test2:
; CHECK-NOT: local
; CHECK: block BB2_2{{$}}
; CHECK: br_if {{[^,]*}}, BB2_2{{$}}
; CHECK: BB2_1: