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