From cf75ab597514effb6f8e915f05d74f45debb89ec Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Mon, 14 Feb 2011 21:50:37 +0000 Subject: [PATCH] Fix PR8854. Track inserted copies to avoid read before write. Sorry, it's hard to reduce a sensible small test case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@125523 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/PeepholeOptimizer.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/CodeGen/PeepholeOptimizer.cpp b/lib/CodeGen/PeepholeOptimizer.cpp index 5f6ee306f48..3474f755b8e 100644 --- a/lib/CodeGen/PeepholeOptimizer.cpp +++ b/lib/CodeGen/PeepholeOptimizer.cpp @@ -331,25 +331,37 @@ bool PeepholeOptimizer::runOnMachineFunction(MachineFunction &MF) { ImmDefRegs.clear(); ImmDefMIs.clear(); + MachineBasicBlock::iterator PMII = I->begin(); for (MachineBasicBlock::iterator MII = I->begin(), MIE = I->end(); MII != MIE; ) { - MachineInstr *MI = &*MII++; + MachineInstr *MI = &*MII; LocalMIs.insert(MI); if (MI->isLabel() || MI->isPHI() || MI->isImplicitDef() || MI->isKill() || MI->isInlineAsm() || MI->isDebugValue() || - MI->hasUnmodeledSideEffects()) + MI->hasUnmodeledSideEffects()) { + ++MII; continue; + } if (MI->getDesc().isCompare()) { - Changed |= OptimizeCmpInstr(MI, MBB); - } else if (isMoveImmediate(MI, ImmDefRegs, ImmDefMIs)) { + if (OptimizeCmpInstr(MI, MBB)) { + // MI is deleted. + Changed = true; + MII = llvm::next(PMII); + continue; + } + } + + if (isMoveImmediate(MI, ImmDefRegs, ImmDefMIs)) { SeenMoveImm = true; } else { Changed |= OptimizeExtInstr(MI, MBB, LocalMIs); if (SeenMoveImm) Changed |= FoldImmediate(MI, MBB, ImmDefRegs, ImmDefMIs); } + PMII = MII; + ++MII; } }