From: Duncan P. N. Exon Smith Date: Thu, 30 Jun 2016 00:10:17 +0000 (+0000) Subject: CodeGen: Add an explicit BuildMI overload for MachineInstr& X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e1a95d05f2c28cc618506f27a5efea1a4d1990d8;p=llvm CodeGen: Add an explicit BuildMI overload for MachineInstr& Add an explicit overload to BuildMI for MachineInstr& to deal with insertions inside of instruction bundles. - Use it to re-implement MachineInstr* to give it coverage. - Document how the overload for MachineBasicBlock::instr_iterator differs from that for MachineBasicBlock::iterator (the previous (implicit) overload for MachineInstr&). - Add a comment explaining why the MachineInstr& and MachineInstr* overloads don't universally forward to the MachineBasicBlock::instr_iterator overload. Thanks to Justin for noticing the API quirk. While this doesn't fix any known bugs -- all uses of BuildMI with a MachineInstr& were previously using MachineBasicBlock::iterator -- it protects against future bugs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274193 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/CodeGen/MachineInstrBuilder.h b/include/llvm/CodeGen/MachineInstrBuilder.h index 233472bcda9..da0afc32035 100644 --- a/include/llvm/CodeGen/MachineInstrBuilder.h +++ b/include/llvm/CodeGen/MachineInstrBuilder.h @@ -265,6 +265,12 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); } +/// This version of the builder inserts the newly-built instruction before +/// the given position in the given MachineBasicBlock, and sets up the first +/// operand as a destination virtual register. +/// +/// If \c I is inside a bundle, then the newly inserted \a MachineInstr is +/// added to the same bundle. inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineBasicBlock::instr_iterator I, const DebugLoc &DL, const MCInstrDesc &MCID, @@ -275,16 +281,20 @@ inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define); } -inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I, +inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I, const DebugLoc &DL, const MCInstrDesc &MCID, unsigned DestReg) { - if (I->isInsideBundle()) { - MachineBasicBlock::instr_iterator MII(I); - return BuildMI(BB, MII, DL, MCID, DestReg); - } + // Calling the overload for instr_iterator is always correct. However, the + // definition is not available in headers, so inline the check. + if (I.isInsideBundle()) + return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg); + return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg); +} - MachineBasicBlock::iterator MII = I; - return BuildMI(BB, MII, DL, MCID, DestReg); +inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I, + const DebugLoc &DL, const MCInstrDesc &MCID, + unsigned DestReg) { + return BuildMI(BB, *I, DL, MCID, DestReg); } /// This version of the builder inserts the newly-built instruction before the