<div dir="ltr"><div><div>I'm writing a pass that looks at the operands of certain non-commutable instructions and swaps a couple of them if certain conditions exist (a register bank conflict in the instruction). If the conflict exists, I build a new instruction which has the 2nd and 3rd operands swapped (using BuildMI).  Then I want to get rid of the original instruction. I had done some searching and found that eraseFromParent is used to do this so that's what I'm using in the code below:<br><br>     for (MachineFunction::iterator MFI = MF.begin(), E = MF.end();<br>       MFI != E; ++MFI) {<br>       // for each instr  <br>       MachineBasicBlock::instr_iterator II = MFI->instr_begin();<br>       while( II != MFI->instr_end()) {<br>          opcode = II->getOpcode();<br>          if(II->isCommutable() || isLoadRR(opcode)){<br>            MachineBasicBlock& MBB = *MFI;<br>            MachineInstr& MI = *II;<br>            DebugLoc DL = MI.getDebugLoc();<br>            <br>            MachineOperand& reg1 = MI.getOperand(0);<br>            MachineOperand& reg2 = MI.getOperand(1);<br>            MachineOperand& reg3 = MI.getOperand(2);<br>            if(reg1.isReg() && reg2.isReg() && reg3.isReg()){<br>              if((reg1.getReg()-8)%4 == (reg3.getReg()-8)%4){<br>                MachineBasicBlock::instr_iterator NII = std::next(II);<br>                //conflict if reg1 and reg3 are in same bank<br>                errs() <<  "Conflict: ";<br>                printOp(opcode);<br>                errs() <<  " has " << num_operands << " register operands:\n";<br>                errs() <<  " r1: " << (reg1.getReg()-8) << " r2: " << (reg2.getReg()-8)<br>                       <<  " r3: " << (reg3.getReg()-8) << "\n";<br>                //build the swapped version<br>                BuildMI(MBB, II, DL, TII.get(opcode),reg1.getReg()).addReg(reg3.getReg()).addReg(reg2.getReg());<br>                MI.eraseFromParent(); <br>                II = NII;<br>              }<br>            }<br>          }<br>          ++II;<br>       }<br>     }<br><br></div>Unfortunately, this leads to a segfault. Is this the proper way to do this or is there another suggested way of doing it?<br><br></div>Phil<br></div>