Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1 | //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Alkis Evlogimenos | 50c047d | 2004-01-04 23:09:24 +0000 | [diff] [blame] | 10 | // This file implements the TwoAddress instruction pass which is used |
| 11 | // by most register allocators. Two-Address instructions are rewritten |
| 12 | // from: |
| 13 | // |
| 14 | // A = B op C |
| 15 | // |
| 16 | // to: |
| 17 | // |
| 18 | // A = B |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 19 | // A op= C |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 20 | // |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 21 | // Note that if a register allocator chooses to use this pass, that it |
| 22 | // has to be capable of handling the non-SSA nature of these rewritten |
| 23 | // virtual registers. |
| 24 | // |
| 25 | // It is also worth noting that the duplicate operand of the two |
| 26 | // address instruction is removed. |
Chris Lattner | bd91c1c | 2004-01-31 21:07:15 +0000 | [diff] [blame] | 27 | // |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | #define DEBUG_TYPE "twoaddrinstr" |
Chris Lattner | bd91c1c | 2004-01-31 21:07:15 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 32 | #include "llvm/Function.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/LiveVariables.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 35 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetInstrInfo.h" |
| 39 | #include "llvm/Target/TargetMachine.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Compiler.h" |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/Statistic.h" |
| 43 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 46 | STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions"); |
| 47 | STATISTIC(NumCommuted , "Number of instructions commuted to coalesce"); |
| 48 | STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 49 | STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk"); |
| 50 | |
| 51 | namespace { |
Chris Lattner | f8c68f6 | 2006-06-28 22:17:39 +0000 | [diff] [blame] | 52 | struct VISIBILITY_HIDDEN TwoAddressInstructionPass |
| 53 | : public MachineFunctionPass { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 54 | const TargetInstrInfo *TII; |
| 55 | const TargetRegisterInfo *TRI; |
| 56 | MachineRegisterInfo *MRI; |
| 57 | LiveVariables *LV; |
| 58 | |
| 59 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 60 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 61 | TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {} |
| 62 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 63 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 64 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 65 | /// runOnMachineFunction - pass entry point |
| 66 | bool runOnMachineFunction(MachineFunction&); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI, |
| 70 | unsigned Reg, |
| 71 | MachineBasicBlock::iterator OldPos); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 72 | }; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 73 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 74 | char TwoAddressInstructionPass::ID = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 75 | RegisterPass<TwoAddressInstructionPass> |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 76 | X("twoaddressinstruction", "Two-Address instruction pass"); |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 77 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 78 | |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 79 | const PassInfo *llvm::TwoAddressInstructionPassID = X.getPassInfo(); |
| 80 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 81 | void TwoAddressInstructionPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 82 | AU.addRequired<LiveVariables>(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 83 | AU.addPreserved<LiveVariables>(); |
Bill Wendling | 67d65bb | 2008-01-04 20:54:55 +0000 | [diff] [blame] | 84 | AU.addPreservedID(MachineLoopInfoID); |
| 85 | AU.addPreservedID(MachineDominatorsID); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 86 | AU.addPreservedID(PHIEliminationID); |
| 87 | MachineFunctionPass::getAnalysisUsage(AU); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 90 | /// Sink3AddrInstruction - A two-address instruction has been converted to a |
| 91 | /// three-address instruction to avoid clobbering a register. Try to sink it |
| 92 | /// past the instruction that would kill the above mentioned register to |
| 93 | /// reduce register pressure. |
| 94 | bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB, |
| 95 | MachineInstr *MI, unsigned SavedReg, |
| 96 | MachineBasicBlock::iterator OldPos) { |
| 97 | // Check if it's safe to move this instruction. |
| 98 | bool SeenStore = true; // Be conservative. |
| 99 | if (!MI->isSafeToMove(TII, SeenStore)) |
| 100 | return false; |
| 101 | |
| 102 | unsigned DefReg = 0; |
| 103 | SmallSet<unsigned, 4> UseRegs; |
| 104 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 105 | const MachineOperand &MO = MI->getOperand(i); |
| 106 | if (!MO.isRegister()) |
| 107 | continue; |
| 108 | unsigned MOReg = MO.getReg(); |
| 109 | if (!MOReg) |
| 110 | continue; |
| 111 | if (MO.isUse() && MOReg != SavedReg) |
| 112 | UseRegs.insert(MO.getReg()); |
| 113 | if (!MO.isDef()) |
| 114 | continue; |
| 115 | if (MO.isImplicit()) |
| 116 | // Don't try to move it if it implicitly defines a register. |
| 117 | return false; |
| 118 | if (DefReg) |
| 119 | // For now, don't move any instructions that define multiple registers. |
| 120 | return false; |
| 121 | DefReg = MO.getReg(); |
| 122 | } |
| 123 | |
| 124 | // Find the instruction that kills SavedReg. |
| 125 | MachineInstr *KillMI = NULL; |
| 126 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg), |
| 127 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 128 | MachineOperand &UseMO = UI.getOperand(); |
| 129 | if (!UseMO.isKill()) |
| 130 | continue; |
| 131 | KillMI = UseMO.getParent(); |
| 132 | break; |
| 133 | } |
| 134 | if (!KillMI || KillMI->getParent() != MBB) |
| 135 | return false; |
| 136 | |
| 137 | // If any of the definitions are used by another instruction between |
| 138 | // the position and the kill use, then it's not safe to sink it. |
| 139 | // FIXME: This can be sped up if there is an easy way to query whether |
| 140 | // an instruction if before or after another instruction. Then we can |
| 141 | // use MachineRegisterInfo def / use instead. |
| 142 | MachineOperand *KillMO = NULL; |
| 143 | MachineBasicBlock::iterator KillPos = KillMI; |
| 144 | ++KillPos; |
| 145 | for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) { |
| 146 | MachineInstr *OtherMI = I; |
| 147 | for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) { |
| 148 | MachineOperand &MO = OtherMI->getOperand(i); |
| 149 | if (!MO.isRegister()) |
| 150 | continue; |
| 151 | unsigned MOReg = MO.getReg(); |
| 152 | if (!MOReg) |
| 153 | continue; |
| 154 | if (DefReg == MOReg) |
| 155 | return false; |
| 156 | if (MO.isKill()) { |
| 157 | if (OtherMI == KillMI && MOReg == SavedReg) |
| 158 | // Save the operand that kills the register. We want unset the kill |
| 159 | // marker is we can sink MI past it. |
| 160 | KillMO = &MO; |
| 161 | else if (UseRegs.count(MOReg)) |
| 162 | // One of the uses is killed before the destination. |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 168 | // Update kill and LV information. |
| 169 | KillMO->setIsKill(false); |
| 170 | KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI); |
| 171 | KillMO->setIsKill(true); |
| 172 | LiveVariables::VarInfo& VarInfo = LV->getVarInfo(SavedReg); |
| 173 | VarInfo.removeKill(KillMI); |
| 174 | VarInfo.Kills.push_back(MI); |
| 175 | |
| 176 | // Move instruction to its destination. |
| 177 | MBB->remove(MI); |
| 178 | MBB->insert(KillPos, MI); |
| 179 | |
| 180 | ++Num3AddrSunk; |
| 181 | return true; |
| 182 | } |
| 183 | |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 184 | /// runOnMachineFunction - Reduce two-address instructions to two |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 185 | /// operands. |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 186 | /// |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 187 | bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 188 | DOUT << "Machine Function\n"; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 189 | const TargetMachine &TM = MF.getTarget(); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 190 | MRI = &MF.getRegInfo(); |
| 191 | TII = TM.getInstrInfo(); |
| 192 | TRI = TM.getRegisterInfo(); |
| 193 | LV = &getAnalysis<LiveVariables>(); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 194 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 195 | bool MadeChange = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 196 | |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 197 | DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n"; |
| 198 | DOUT << "********** Function: " << MF.getFunction()->getName() << '\n'; |
Alkis Evlogimenos | 3a9986f | 2004-02-18 00:35:06 +0000 | [diff] [blame] | 199 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 200 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 201 | mbbi != mbbe; ++mbbi) { |
| 202 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
| 203 | mi != me; ++mi) { |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 204 | const TargetInstrDesc &TID = mi->getDesc(); |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 205 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 206 | bool FirstTied = true; |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 207 | for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) { |
| 208 | int ti = TID.getOperandConstraint(si, TOI::TIED_TO); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 209 | if (ti == -1) |
| 210 | continue; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 211 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 212 | if (FirstTied) { |
| 213 | ++NumTwoAddressInstrs; |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 214 | DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 215 | } |
| 216 | FirstTied = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 217 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 218 | assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() && |
| 219 | mi->getOperand(si).isUse() && "two address instruction invalid"); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 220 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 221 | // if the two operands are the same we just remove the use |
| 222 | // and mark the def as def&use, otherwise we have to insert a copy. |
| 223 | if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) { |
| 224 | // rewrite: |
| 225 | // a = b op c |
| 226 | // to: |
| 227 | // a = b |
| 228 | // a = a op c |
| 229 | unsigned regA = mi->getOperand(ti).getReg(); |
| 230 | unsigned regB = mi->getOperand(si).getReg(); |
| 231 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 232 | assert(TargetRegisterInfo::isVirtualRegister(regA) && |
| 233 | TargetRegisterInfo::isVirtualRegister(regB) && |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 234 | "cannot update physical register live information"); |
Chris Lattner | 6b50767 | 2004-01-31 21:21:43 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 236 | #ifndef NDEBUG |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 237 | // First, verify that we don't have a use of a in the instruction (a = |
| 238 | // b + a for example) because our transformation will not work. This |
| 239 | // should never occur because we are in SSA form. |
| 240 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) |
| 241 | assert((int)i == ti || |
| 242 | !mi->getOperand(i).isRegister() || |
| 243 | mi->getOperand(i).getReg() != regA); |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 244 | #endif |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 245 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 246 | // If this instruction is not the killing user of B, see if we can |
| 247 | // rearrange the code to make it so. Making it the killing user will |
| 248 | // allow us to coalesce A and B together, eliminating the copy we are |
| 249 | // about to insert. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 250 | if (!mi->killsRegister(regB)) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 251 | // If this instruction is commutative, check to see if C dies. If |
| 252 | // so, swap the B and C operands. This makes the live ranges of A |
| 253 | // and C joinable. |
| 254 | // FIXME: This code also works for A := B op C instructions. |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 255 | if (TID.isCommutable() && mi->getNumOperands() >= 3) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 256 | assert(mi->getOperand(3-si).isRegister() && |
| 257 | "Not a proper commutative instruction!"); |
| 258 | unsigned regC = mi->getOperand(3-si).getReg(); |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 259 | if (mi->killsRegister(regC)) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 260 | DOUT << "2addr: COMMUTING : " << *mi; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 261 | MachineInstr *NewMI = TII->commuteInstruction(mi); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 262 | if (NewMI == 0) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 263 | DOUT << "2addr: COMMUTING FAILED!\n"; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 264 | } else { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 265 | DOUT << "2addr: COMMUTED TO: " << *NewMI; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 266 | // If the instruction changed to commute it, update livevar. |
| 267 | if (NewMI != mi) { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 268 | LV->instructionChanged(mi, NewMI); // Update live variables |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 269 | mbbi->insert(mi, NewMI); // Insert the new inst |
| 270 | mbbi->erase(mi); // Nuke the old inst. |
| 271 | mi = NewMI; |
| 272 | } |
| 273 | |
| 274 | ++NumCommuted; |
| 275 | regB = regC; |
| 276 | goto InstructionRearranged; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 277 | } |
Chris Lattner | c71d694 | 2005-01-19 07:08:42 +0000 | [diff] [blame] | 278 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 279 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 280 | |
| 281 | // If this instruction is potentially convertible to a true |
| 282 | // three-address instruction, |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 283 | if (TID.isConvertibleTo3Addr()) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 284 | // FIXME: This assumes there are no more operands which are tied |
| 285 | // to another register. |
| 286 | #ifndef NDEBUG |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 287 | for (unsigned i = si+1, e = TID.getNumOperands(); i < e; ++i) |
| 288 | assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 289 | #endif |
| 290 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 291 | if (MachineInstr *New=TII->convertToThreeAddress(mbbi, mi, *LV)) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 292 | DOUT << "2addr: CONVERTING 2-ADDR: " << *mi; |
| 293 | DOUT << "2addr: TO 3-ADDR: " << *New; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 294 | bool Sunk = Sink3AddrInstruction(mbbi, New, regB, mi); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 295 | mbbi->erase(mi); // Nuke the old inst. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 296 | if (!Sunk) mi = New; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 297 | ++NumConvertedTo3Addr; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 298 | // Done with this instruction. |
| 299 | break; |
| 300 | } |
Evan Cheng | b9d5e7c | 2007-10-20 04:01:47 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 302 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 303 | |
| 304 | InstructionRearranged: |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 305 | const TargetRegisterClass* rc = MF.getRegInfo().getRegClass(regA); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 306 | TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 307 | |
| 308 | MachineBasicBlock::iterator prevMi = prior(mi); |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 309 | DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 310 | |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 311 | // update live variables for regB |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 312 | LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB); |
Owen Anderson | a018540 | 2007-11-08 01:20:48 +0000 | [diff] [blame] | 313 | // regB is used in this BB. |
| 314 | varInfoB.UsedBlocks[mbbi->getNumber()] = true; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 315 | if (LV->removeVirtualRegisterKilled(regB, mbbi, mi)) |
| 316 | LV->addVirtualRegisterKilled(regB, prevMi); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 317 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 318 | if (LV->removeVirtualRegisterDead(regB, mbbi, mi)) |
| 319 | LV->addVirtualRegisterDead(regB, prevMi); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 320 | |
| 321 | // replace all occurences of regB with regA |
| 322 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 323 | if (mi->getOperand(i).isRegister() && |
| 324 | mi->getOperand(i).getReg() == regB) |
| 325 | mi->getOperand(i).setReg(regA); |
| 326 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 327 | } |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 328 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 329 | assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse()); |
| 330 | mi->getOperand(ti).setReg(mi->getOperand(si).getReg()); |
| 331 | MadeChange = true; |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 332 | |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 333 | DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM)); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 334 | } |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | return MadeChange; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 339 | } |