From: Justin Bogner Date: Fri, 25 Mar 2016 20:03:28 +0000 (+0000) Subject: CodeGen: Don't iterate over operands after we've erased an MI X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ec0e7d2582abe0169ebde8fb2105d998bdc79d4d;p=platform%2Fupstream%2Fllvm.git CodeGen: Don't iterate over operands after we've erased an MI This fixes a use-after-free introduced 3 years ago, in r182872 ;) The code more or less worked because the memory that CopyMI was pointing to happened to still be valid, but lots of tests would crash if you ran under ASAN with the recycling allocator changes from llvm.org/PR26808 llvm-svn: 264455 --- diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp index 18cbad5..42e5652 100644 --- a/llvm/lib/CodeGen/RegisterCoalescer.cpp +++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp @@ -979,6 +979,23 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP, } } + // CopyMI may have implicit operands, save them so that we can transfer them + // over to the newly materialized instruction after CopyMI is removed. + SmallVector ImplicitOps; + ImplicitOps.reserve(CopyMI->getNumOperands() - + CopyMI->getDesc().getNumOperands()); + for (unsigned I = CopyMI->getDesc().getNumOperands(), + E = CopyMI->getNumOperands(); + I != E; ++I) { + MachineOperand &MO = CopyMI->getOperand(I); + if (MO.isReg()) { + assert(MO.isImplicit() && "No explicit operands after implict operands."); + // Discard VReg implicit defs. + if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) + ImplicitOps.push_back(MO); + } + } + LIS->ReplaceMachineInstrInMaps(*CopyMI, *NewMI); CopyMI->eraseFromParent(); ErasedInstrs.insert(CopyMI); @@ -1082,19 +1099,9 @@ bool RegisterCoalescer::reMaterializeTrivialDef(const CoalescerPair &CP, if (NewMI->getOperand(0).getSubReg()) NewMI->getOperand(0).setIsUndef(); - // CopyMI may have implicit operands, transfer them over to the newly - // rematerialized instruction. And update implicit def interval valnos. - for (unsigned i = CopyMI->getDesc().getNumOperands(), - e = CopyMI->getNumOperands(); i != e; ++i) { - MachineOperand &MO = CopyMI->getOperand(i); - if (MO.isReg()) { - assert(MO.isImplicit() && "No explicit operands after implict operands."); - // Discard VReg implicit defs. - if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { - NewMI->addOperand(MO); - } - } - } + // Transfer over implicit operands to the rematerialized instruction. + for (MachineOperand &MO : ImplicitOps) + NewMI->addOperand(MO); SlotIndex NewMIIdx = LIS->getInstructionIndex(*NewMI); for (unsigned i = 0, e = NewMIImplDefs.size(); i != e; ++i) {