From c7e3f5ac6969e78e878997c13fe25c36ecb64e01 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Wed, 6 Jan 2016 18:29:35 +0000 Subject: [PATCH] [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 --- lib/Target/WebAssembly/WebAssemblyRegStackify.cpp | 8 +++++--- test/CodeGen/WebAssembly/cfg-stackify.ll | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp b/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp index 89ef5cdb2be..537c147e614 100644 --- a/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp +++ b/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp @@ -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); } } diff --git a/test/CodeGen/WebAssembly/cfg-stackify.ll b/test/CodeGen/WebAssembly/cfg-stackify.ll index 71f3551347b..d0250dc29e0 100644 --- a/test/CodeGen/WebAssembly/cfg-stackify.ll +++ b/test/CodeGen/WebAssembly/cfg-stackify.ll @@ -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: -- 2.34.1