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