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" |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/BitVector.h" |
| 43 | #include "llvm/ADT/DenseMap.h" |
Bill Wendling | 48f7f23 | 2008-05-26 05:18:34 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/Statistic.h" |
| 46 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 49 | STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions"); |
| 50 | STATISTIC(NumCommuted , "Number of instructions commuted to coalesce"); |
| 51 | STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 52 | STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk"); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 53 | STATISTIC(NumReMats, "Number of instructions re-materialized"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 54 | |
| 55 | namespace { |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 56 | class VISIBILITY_HIDDEN TwoAddressInstructionPass |
| 57 | : public MachineFunctionPass { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 58 | const TargetInstrInfo *TII; |
| 59 | const TargetRegisterInfo *TRI; |
| 60 | MachineRegisterInfo *MRI; |
| 61 | LiveVariables *LV; |
| 62 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 63 | bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI, |
| 64 | unsigned Reg, |
| 65 | MachineBasicBlock::iterator OldPos); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 66 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 67 | bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC, |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 68 | MachineInstr *MI, MachineInstr *DefMI, |
| 69 | MachineBasicBlock *MBB, unsigned Loc, |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 70 | DenseMap<MachineInstr*, unsigned> &DistanceMap); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 71 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 72 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 73 | TwoAddressInstructionPass() : MachineFunctionPass((intptr_t)&ID) {} |
| 74 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 75 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 76 | AU.addPreserved<LiveVariables>(); |
| 77 | AU.addPreservedID(MachineLoopInfoID); |
| 78 | AU.addPreservedID(MachineDominatorsID); |
| 79 | AU.addPreservedID(PHIEliminationID); |
| 80 | MachineFunctionPass::getAnalysisUsage(AU); |
| 81 | } |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 82 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 83 | /// runOnMachineFunction - Pass entry point. |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 84 | bool runOnMachineFunction(MachineFunction&); |
| 85 | }; |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 86 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 87 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 88 | char TwoAddressInstructionPass::ID = 0; |
| 89 | static RegisterPass<TwoAddressInstructionPass> |
| 90 | X("twoaddressinstruction", "Two-Address instruction pass"); |
| 91 | |
Dan Gohman | 6ddba2b | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 92 | const PassInfo *const llvm::TwoAddressInstructionPassID = &X; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 93 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 94 | /// Sink3AddrInstruction - A two-address instruction has been converted to a |
| 95 | /// three-address instruction to avoid clobbering a register. Try to sink it |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 96 | /// past the instruction that would kill the above mentioned register to reduce |
| 97 | /// register pressure. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 98 | bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB, |
| 99 | MachineInstr *MI, unsigned SavedReg, |
| 100 | MachineBasicBlock::iterator OldPos) { |
| 101 | // Check if it's safe to move this instruction. |
| 102 | bool SeenStore = true; // Be conservative. |
| 103 | if (!MI->isSafeToMove(TII, SeenStore)) |
| 104 | return false; |
| 105 | |
| 106 | unsigned DefReg = 0; |
| 107 | SmallSet<unsigned, 4> UseRegs; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 108 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 109 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 110 | const MachineOperand &MO = MI->getOperand(i); |
| 111 | if (!MO.isRegister()) |
| 112 | continue; |
| 113 | unsigned MOReg = MO.getReg(); |
| 114 | if (!MOReg) |
| 115 | continue; |
| 116 | if (MO.isUse() && MOReg != SavedReg) |
| 117 | UseRegs.insert(MO.getReg()); |
| 118 | if (!MO.isDef()) |
| 119 | continue; |
| 120 | if (MO.isImplicit()) |
| 121 | // Don't try to move it if it implicitly defines a register. |
| 122 | return false; |
| 123 | if (DefReg) |
| 124 | // For now, don't move any instructions that define multiple registers. |
| 125 | return false; |
| 126 | DefReg = MO.getReg(); |
| 127 | } |
| 128 | |
| 129 | // Find the instruction that kills SavedReg. |
| 130 | MachineInstr *KillMI = NULL; |
| 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 |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 147 | // instruction is before or after another instruction. Then we can use |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 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 | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 153 | unsigned NumVisited = 0; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 154 | for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) { |
| 155 | MachineInstr *OtherMI = I; |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 156 | if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost. |
| 157 | return false; |
| 158 | ++NumVisited; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 159 | for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) { |
| 160 | MachineOperand &MO = OtherMI->getOperand(i); |
| 161 | if (!MO.isRegister()) |
| 162 | continue; |
| 163 | unsigned MOReg = MO.getReg(); |
| 164 | if (!MOReg) |
| 165 | continue; |
| 166 | if (DefReg == MOReg) |
| 167 | return false; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 168 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 169 | if (MO.isKill()) { |
| 170 | if (OtherMI == KillMI && MOReg == SavedReg) |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 171 | // Save the operand that kills the register. We want to unset the kill |
| 172 | // marker if we can sink MI past it. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 173 | KillMO = &MO; |
| 174 | else if (UseRegs.count(MOReg)) |
| 175 | // One of the uses is killed before the destination. |
| 176 | return false; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 181 | // Update kill and LV information. |
| 182 | KillMO->setIsKill(false); |
| 183 | KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI); |
| 184 | KillMO->setIsKill(true); |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 185 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 186 | if (LV) |
| 187 | LV->replaceKillInstruction(SavedReg, KillMI, MI); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 188 | |
| 189 | // Move instruction to its destination. |
| 190 | MBB->remove(MI); |
| 191 | MBB->insert(KillPos, MI); |
| 192 | |
| 193 | ++Num3AddrSunk; |
| 194 | return true; |
| 195 | } |
| 196 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 197 | /// isTwoAddrUse - Return true if the specified MI is using the specified |
| 198 | /// register as a two-address operand. |
| 199 | static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) { |
| 200 | const TargetInstrDesc &TID = UseMI->getDesc(); |
| 201 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { |
| 202 | MachineOperand &MO = UseMI->getOperand(i); |
Evan Cheng | 32a3ac7 | 2008-06-19 06:17:19 +0000 | [diff] [blame] | 203 | if (MO.isRegister() && MO.getReg() == Reg && |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 204 | (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1)) |
| 205 | // Earlier use is a two-address one. |
| 206 | return true; |
| 207 | } |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | /// isProfitableToReMat - Return true if the heuristics determines it is likely |
| 212 | /// to be profitable to re-materialize the definition of Reg rather than copy |
| 213 | /// the register. |
| 214 | bool |
| 215 | TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg, |
| 216 | const TargetRegisterClass *RC, |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 217 | MachineInstr *MI, MachineInstr *DefMI, |
| 218 | MachineBasicBlock *MBB, unsigned Loc, |
| 219 | DenseMap<MachineInstr*, unsigned> &DistanceMap){ |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 220 | bool OtherUse = false; |
| 221 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 222 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 223 | MachineOperand &UseMO = UI.getOperand(); |
| 224 | if (!UseMO.isUse()) |
| 225 | continue; |
| 226 | MachineInstr *UseMI = UseMO.getParent(); |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 227 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 228 | if (UseMBB == MBB) { |
| 229 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI); |
| 230 | if (DI != DistanceMap.end() && DI->second == Loc) |
| 231 | continue; // Current use. |
| 232 | OtherUse = true; |
| 233 | // There is at least one other use in the MBB that will clobber the |
| 234 | // register. |
| 235 | if (isTwoAddrUse(UseMI, Reg)) |
| 236 | return true; |
| 237 | } |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 238 | } |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 239 | |
| 240 | // If other uses in MBB are not two-address uses, then don't remat. |
| 241 | if (OtherUse) |
| 242 | return false; |
| 243 | |
| 244 | // No other uses in the same block, remat if it's defined in the same |
| 245 | // block so it does not unnecessarily extend the live range. |
| 246 | return MBB == DefMI->getParent(); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 249 | /// runOnMachineFunction - Reduce two-address instructions to two operands. |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 250 | /// |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 251 | bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 252 | DOUT << "Machine Function\n"; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 253 | const TargetMachine &TM = MF.getTarget(); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 254 | MRI = &MF.getRegInfo(); |
| 255 | TII = TM.getInstrInfo(); |
| 256 | TRI = TM.getRegisterInfo(); |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 257 | LV = getAnalysisToUpdate<LiveVariables>(); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 258 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 259 | bool MadeChange = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 260 | |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 261 | DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n"; |
| 262 | DOUT << "********** Function: " << MF.getFunction()->getName() << '\n'; |
Alkis Evlogimenos | 3a9986f | 2004-02-18 00:35:06 +0000 | [diff] [blame] | 263 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 264 | // ReMatRegs - Keep track of the registers whose def's are remat'ed. |
| 265 | BitVector ReMatRegs; |
| 266 | ReMatRegs.resize(MRI->getLastVirtReg()+1); |
| 267 | |
| 268 | // DistanceMap - Keep track the distance of a MI from the start of the |
| 269 | // current basic block. |
| 270 | DenseMap<MachineInstr*, unsigned> DistanceMap; |
Bill Wendling | 48f7f23 | 2008-05-26 05:18:34 +0000 | [diff] [blame] | 271 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 272 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 273 | mbbi != mbbe; ++mbbi) { |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 274 | unsigned Dist = 0; |
| 275 | DistanceMap.clear(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 276 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 277 | mi != me; ) { |
| 278 | MachineBasicBlock::iterator nmi = next(mi); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 279 | const TargetInstrDesc &TID = mi->getDesc(); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 280 | bool FirstTied = true; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 281 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 282 | DistanceMap.insert(std::make_pair(mi, ++Dist)); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 283 | for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) { |
| 284 | int ti = TID.getOperandConstraint(si, TOI::TIED_TO); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 285 | if (ti == -1) |
| 286 | continue; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 287 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 288 | if (FirstTied) { |
| 289 | ++NumTwoAddressInstrs; |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 290 | DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 291 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 292 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 293 | FirstTied = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 294 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 295 | assert(mi->getOperand(si).isRegister() && mi->getOperand(si).getReg() && |
| 296 | mi->getOperand(si).isUse() && "two address instruction invalid"); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 297 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 298 | // If the two operands are the same we just remove the use |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 299 | // and mark the def as def&use, otherwise we have to insert a copy. |
| 300 | if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) { |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 301 | // Rewrite: |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 302 | // a = b op c |
| 303 | // to: |
| 304 | // a = b |
| 305 | // a = a op c |
| 306 | unsigned regA = mi->getOperand(ti).getReg(); |
| 307 | unsigned regB = mi->getOperand(si).getReg(); |
| 308 | |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 309 | assert(TargetRegisterInfo::isVirtualRegister(regA) && |
| 310 | TargetRegisterInfo::isVirtualRegister(regB) && |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 311 | "cannot update physical register live information"); |
Chris Lattner | 6b50767 | 2004-01-31 21:21:43 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 313 | #ifndef NDEBUG |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 314 | // First, verify that we don't have a use of a in the instruction (a = |
| 315 | // b + a for example) because our transformation will not work. This |
| 316 | // should never occur because we are in SSA form. |
| 317 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) |
| 318 | assert((int)i == ti || |
| 319 | !mi->getOperand(i).isRegister() || |
| 320 | mi->getOperand(i).getReg() != regA); |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 321 | #endif |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 322 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 323 | // If this instruction is not the killing user of B, see if we can |
| 324 | // rearrange the code to make it so. Making it the killing user will |
| 325 | // allow us to coalesce A and B together, eliminating the copy we are |
| 326 | // about to insert. |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 327 | if (!mi->killsRegister(regB)) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 328 | // If this instruction is commutative, check to see if C dies. If |
| 329 | // so, swap the B and C operands. This makes the live ranges of A |
| 330 | // and C joinable. |
| 331 | // FIXME: This code also works for A := B op C instructions. |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 332 | if (TID.isCommutable() && mi->getNumOperands() >= 3) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 333 | assert(mi->getOperand(3-si).isRegister() && |
| 334 | "Not a proper commutative instruction!"); |
| 335 | unsigned regC = mi->getOperand(3-si).getReg(); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 336 | |
Evan Cheng | 6130f66 | 2008-03-05 00:59:57 +0000 | [diff] [blame] | 337 | if (mi->killsRegister(regC)) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 338 | DOUT << "2addr: COMMUTING : " << *mi; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 339 | MachineInstr *NewMI = TII->commuteInstruction(mi); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 340 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 341 | if (NewMI == 0) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 342 | DOUT << "2addr: COMMUTING FAILED!\n"; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 343 | } else { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 344 | DOUT << "2addr: COMMUTED TO: " << *NewMI; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 345 | // If the instruction changed to commute it, update livevar. |
| 346 | if (NewMI != mi) { |
Evan Cheng | be04dc1 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 347 | if (LV) |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 348 | // Update live variables |
Evan Cheng | be04dc1 | 2008-07-03 00:07:19 +0000 | [diff] [blame] | 349 | LV->replaceKillInstruction(regC, mi, NewMI); |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 350 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 351 | mbbi->insert(mi, NewMI); // Insert the new inst |
| 352 | mbbi->erase(mi); // Nuke the old inst. |
| 353 | mi = NewMI; |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 354 | DistanceMap.insert(std::make_pair(NewMI, Dist)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | ++NumCommuted; |
| 358 | regB = regC; |
| 359 | goto InstructionRearranged; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 360 | } |
Chris Lattner | c71d694 | 2005-01-19 07:08:42 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 362 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 363 | |
| 364 | // If this instruction is potentially convertible to a true |
| 365 | // three-address instruction, |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 366 | if (TID.isConvertibleTo3Addr()) { |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 367 | // FIXME: This assumes there are no more operands which are tied |
| 368 | // to another register. |
| 369 | #ifndef NDEBUG |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 370 | for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i) |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 371 | assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 372 | #endif |
| 373 | |
Owen Anderson | f660c17 | 2008-07-02 23:41:07 +0000 | [diff] [blame] | 374 | MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 375 | if (NewMI) { |
Bill Wendling | a09362e | 2006-11-28 22:48:48 +0000 | [diff] [blame] | 376 | DOUT << "2addr: CONVERTING 2-ADDR: " << *mi; |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 377 | DOUT << "2addr: TO 3-ADDR: " << *NewMI; |
Evan Cheng | 0099ae2 | 2008-03-13 07:56:58 +0000 | [diff] [blame] | 378 | bool Sunk = false; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 379 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 380 | if (NewMI->findRegisterUseOperand(regB, false, TRI)) |
Evan Cheng | 0099ae2 | 2008-03-13 07:56:58 +0000 | [diff] [blame] | 381 | // FIXME: Temporary workaround. If the new instruction doesn't |
| 382 | // uses regB, convertToThreeAddress must have created more |
| 383 | // then one instruction. |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 384 | Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 385 | |
| 386 | mbbi->erase(mi); // Nuke the old inst. |
| 387 | |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 388 | if (!Sunk) { |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 389 | DistanceMap.insert(std::make_pair(NewMI, Dist)); |
| 390 | mi = NewMI; |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 391 | nmi = next(mi); |
| 392 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 393 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 394 | ++NumConvertedTo3Addr; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 395 | break; // Done with this instruction. |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 396 | } |
Evan Cheng | b9d5e7c | 2007-10-20 04:01:47 +0000 | [diff] [blame] | 397 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 398 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 399 | |
| 400 | InstructionRearranged: |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 401 | const TargetRegisterClass* rc = MRI->getRegClass(regA); |
| 402 | MachineInstr *DefMI = MRI->getVRegDef(regB); |
| 403 | // If it's safe and profitable, remat the definition instead of |
| 404 | // copying it. |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 405 | if (DefMI && |
Evan Cheng | df3b993 | 2008-08-27 20:33:50 +0000 | [diff] [blame^] | 406 | DefMI->isSafeToReMat(TII, regB) && |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 407 | isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){ |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 408 | DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n"); |
| 409 | TII->reMaterialize(*mbbi, mi, regA, DefMI); |
| 410 | ReMatRegs.set(regB); |
| 411 | ++NumReMats; |
Bill Wendling | 48f7f23 | 2008-05-26 05:18:34 +0000 | [diff] [blame] | 412 | } else { |
| 413 | TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc); |
| 414 | } |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 415 | |
| 416 | MachineBasicBlock::iterator prevMi = prior(mi); |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 417 | DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM)); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 418 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 419 | // Update live variables for regB. |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 420 | if (LV) { |
| 421 | LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 422 | |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 423 | // regB is used in this BB. |
| 424 | varInfoB.UsedBlocks[mbbi->getNumber()] = true; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 425 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 426 | if (LV->removeVirtualRegisterKilled(regB, mi)) |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 427 | LV->addVirtualRegisterKilled(regB, prevMi); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 428 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 429 | if (LV->removeVirtualRegisterDead(regB, mi)) |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 430 | LV->addVirtualRegisterDead(regB, prevMi); |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 433 | // Replace all occurences of regB with regA. |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 434 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 435 | if (mi->getOperand(i).isRegister() && |
| 436 | mi->getOperand(i).getReg() == regB) |
| 437 | mi->getOperand(i).setReg(regA); |
| 438 | } |
Chris Lattner | cfa0f2e | 2005-01-02 02:34:12 +0000 | [diff] [blame] | 439 | } |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 440 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 441 | assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse()); |
| 442 | mi->getOperand(ti).setReg(mi->getOperand(si).getReg()); |
| 443 | MadeChange = true; |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 444 | |
Bill Wendling | bcd2498 | 2006-12-07 20:28:15 +0000 | [diff] [blame] | 445 | DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM)); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 446 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 447 | |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 448 | mi = nmi; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 452 | // Some remat'ed instructions are dead. |
| 453 | int VReg = ReMatRegs.find_first(); |
| 454 | while (VReg != -1) { |
| 455 | if (MRI->use_empty(VReg)) { |
| 456 | MachineInstr *DefMI = MRI->getVRegDef(VReg); |
| 457 | DefMI->eraseFromParent(); |
Bill Wendling | a16157a | 2008-05-26 05:49:49 +0000 | [diff] [blame] | 458 | } |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 459 | VReg = ReMatRegs.find_next(VReg); |
Bill Wendling | 48f7f23 | 2008-05-26 05:18:34 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 462 | return MadeChange; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 463 | } |