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