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