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 | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/AliasAnalysis.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetInstrInfo.h" |
| 40 | #include "llvm/Target/TargetMachine.h" |
Owen Anderson | 95dad83 | 2008-10-07 20:22:28 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetOptions.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"); |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 52 | STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 53 | STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 54 | STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk"); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 55 | STATISTIC(NumReMats, "Number of instructions re-materialized"); |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 56 | STATISTIC(NumDeletes, "Number of dead instructions deleted"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 57 | |
| 58 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 59 | class TwoAddressInstructionPass : public MachineFunctionPass { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 60 | const TargetInstrInfo *TII; |
| 61 | const TargetRegisterInfo *TRI; |
| 62 | MachineRegisterInfo *MRI; |
| 63 | LiveVariables *LV; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 64 | AliasAnalysis *AA; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 65 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 66 | // DistanceMap - Keep track the distance of a MI from the start of the |
| 67 | // current basic block. |
| 68 | DenseMap<MachineInstr*, unsigned> DistanceMap; |
| 69 | |
| 70 | // SrcRegMap - A map from virtual registers to physical registers which |
| 71 | // are likely targets to be coalesced to due to copies from physical |
| 72 | // registers to virtual registers. e.g. v1024 = move r0. |
| 73 | DenseMap<unsigned, unsigned> SrcRegMap; |
| 74 | |
| 75 | // DstRegMap - A map from virtual registers to physical registers which |
| 76 | // are likely targets to be coalesced to due to copies to physical |
| 77 | // registers from virtual registers. e.g. r1 = move v1024. |
| 78 | DenseMap<unsigned, unsigned> DstRegMap; |
| 79 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 80 | bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI, |
| 81 | unsigned Reg, |
| 82 | MachineBasicBlock::iterator OldPos); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 83 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 84 | bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC, |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 85 | MachineInstr *MI, MachineInstr *DefMI, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 86 | MachineBasicBlock *MBB, unsigned Loc); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 87 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 88 | bool NoUseAfterLastDef(unsigned Reg, MachineBasicBlock *MBB, unsigned Dist, |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 89 | unsigned &LastDef); |
| 90 | |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 91 | MachineInstr *FindLastUseInMBB(unsigned Reg, MachineBasicBlock *MBB, |
| 92 | unsigned Dist); |
| 93 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 94 | bool isProfitableToCommute(unsigned regB, unsigned regC, |
| 95 | MachineInstr *MI, MachineBasicBlock *MBB, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 96 | unsigned Dist); |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 97 | |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 98 | bool CommuteInstruction(MachineBasicBlock::iterator &mi, |
| 99 | MachineFunction::iterator &mbbi, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 100 | unsigned RegB, unsigned RegC, unsigned Dist); |
| 101 | |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 102 | bool isProfitableToConv3Addr(unsigned RegA); |
| 103 | |
| 104 | bool ConvertInstTo3Addr(MachineBasicBlock::iterator &mi, |
| 105 | MachineBasicBlock::iterator &nmi, |
| 106 | MachineFunction::iterator &mbbi, |
| 107 | unsigned RegB, unsigned Dist); |
| 108 | |
Bob Wilson | 326f438 | 2009-09-01 22:51:08 +0000 | [diff] [blame] | 109 | typedef std::pair<std::pair<unsigned, bool>, MachineInstr*> NewKill; |
| 110 | bool canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills, |
| 111 | SmallVector<NewKill, 4> &NewKills, |
| 112 | MachineBasicBlock *MBB, unsigned Dist); |
| 113 | bool DeleteUnusedInstr(MachineBasicBlock::iterator &mi, |
| 114 | MachineBasicBlock::iterator &nmi, |
Jakob Stoklund Olesen | 0b25ae1 | 2009-11-18 21:33:35 +0000 | [diff] [blame] | 115 | MachineFunction::iterator &mbbi, unsigned Dist); |
Bob Wilson | 326f438 | 2009-09-01 22:51:08 +0000 | [diff] [blame] | 116 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 117 | bool TryInstructionTransform(MachineBasicBlock::iterator &mi, |
| 118 | MachineBasicBlock::iterator &nmi, |
| 119 | MachineFunction::iterator &mbbi, |
| 120 | unsigned SrcIdx, unsigned DstIdx, |
| 121 | unsigned Dist); |
| 122 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 123 | void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB, |
| 124 | SmallPtrSet<MachineInstr*, 8> &Processed); |
Evan Cheng | 3a3cce5 | 2009-08-07 00:28:58 +0000 | [diff] [blame] | 125 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 126 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 127 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 128 | TwoAddressInstructionPass() : MachineFunctionPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 129 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 130 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 131 | AU.setPreservesCFG(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 132 | AU.addRequired<AliasAnalysis>(); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 133 | AU.addPreserved<LiveVariables>(); |
| 134 | AU.addPreservedID(MachineLoopInfoID); |
| 135 | AU.addPreservedID(MachineDominatorsID); |
Owen Anderson | 95dad83 | 2008-10-07 20:22:28 +0000 | [diff] [blame] | 136 | if (StrongPHIElim) |
| 137 | AU.addPreservedID(StrongPHIEliminationID); |
| 138 | else |
| 139 | AU.addPreservedID(PHIEliminationID); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 140 | MachineFunctionPass::getAnalysisUsage(AU); |
| 141 | } |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 142 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 143 | /// runOnMachineFunction - Pass entry point. |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 144 | bool runOnMachineFunction(MachineFunction&); |
| 145 | }; |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 146 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 147 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 148 | char TwoAddressInstructionPass::ID = 0; |
| 149 | static RegisterPass<TwoAddressInstructionPass> |
| 150 | X("twoaddressinstruction", "Two-Address instruction pass"); |
| 151 | |
Dan Gohman | 6ddba2b | 2008-05-13 02:05:11 +0000 | [diff] [blame] | 152 | const PassInfo *const llvm::TwoAddressInstructionPassID = &X; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 153 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 154 | /// Sink3AddrInstruction - A two-address instruction has been converted to a |
| 155 | /// three-address instruction to avoid clobbering a register. Try to sink it |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 156 | /// past the instruction that would kill the above mentioned register to reduce |
| 157 | /// register pressure. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 158 | bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB, |
| 159 | MachineInstr *MI, unsigned SavedReg, |
| 160 | MachineBasicBlock::iterator OldPos) { |
| 161 | // Check if it's safe to move this instruction. |
| 162 | bool SeenStore = true; // Be conservative. |
Evan Cheng | ac1abde | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 163 | if (!MI->isSafeToMove(TII, AA, SeenStore)) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 164 | return false; |
| 165 | |
| 166 | unsigned DefReg = 0; |
| 167 | SmallSet<unsigned, 4> UseRegs; |
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 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 170 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 171 | if (!MO.isReg()) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 172 | continue; |
| 173 | unsigned MOReg = MO.getReg(); |
| 174 | if (!MOReg) |
| 175 | continue; |
| 176 | if (MO.isUse() && MOReg != SavedReg) |
| 177 | UseRegs.insert(MO.getReg()); |
| 178 | if (!MO.isDef()) |
| 179 | continue; |
| 180 | if (MO.isImplicit()) |
| 181 | // Don't try to move it if it implicitly defines a register. |
| 182 | return false; |
| 183 | if (DefReg) |
| 184 | // For now, don't move any instructions that define multiple registers. |
| 185 | return false; |
| 186 | DefReg = MO.getReg(); |
| 187 | } |
| 188 | |
| 189 | // Find the instruction that kills SavedReg. |
| 190 | MachineInstr *KillMI = NULL; |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 191 | for (MachineRegisterInfo::use_nodbg_iterator |
| 192 | UI = MRI->use_nodbg_begin(SavedReg), |
| 193 | UE = MRI->use_nodbg_end(); UI != UE; ++UI) { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 194 | MachineOperand &UseMO = UI.getOperand(); |
| 195 | if (!UseMO.isKill()) |
| 196 | continue; |
| 197 | KillMI = UseMO.getParent(); |
| 198 | break; |
| 199 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 200 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 201 | if (!KillMI || KillMI->getParent() != MBB || KillMI == MI) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 202 | return false; |
| 203 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 204 | // If any of the definitions are used by another instruction between the |
| 205 | // position and the kill use, then it's not safe to sink it. |
| 206 | // |
| 207 | // 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] | 208 | // instruction is before or after another instruction. Then we can use |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 209 | // MachineRegisterInfo def / use instead. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 210 | MachineOperand *KillMO = NULL; |
| 211 | MachineBasicBlock::iterator KillPos = KillMI; |
| 212 | ++KillPos; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 213 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 214 | unsigned NumVisited = 0; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 215 | for (MachineBasicBlock::iterator I = llvm::next(OldPos); I != KillPos; ++I) { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 216 | MachineInstr *OtherMI = I; |
Dale Johannesen | 3bfef03 | 2010-02-11 18:22:31 +0000 | [diff] [blame] | 217 | // DBG_VALUE cannot be counted against the limit. |
| 218 | if (OtherMI->isDebugValue()) |
| 219 | continue; |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 220 | if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost. |
| 221 | return false; |
| 222 | ++NumVisited; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 223 | for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) { |
| 224 | MachineOperand &MO = OtherMI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 225 | if (!MO.isReg()) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 226 | continue; |
| 227 | unsigned MOReg = MO.getReg(); |
| 228 | if (!MOReg) |
| 229 | continue; |
| 230 | if (DefReg == MOReg) |
| 231 | return false; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 232 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 233 | if (MO.isKill()) { |
| 234 | if (OtherMI == KillMI && MOReg == SavedReg) |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 235 | // Save the operand that kills the register. We want to unset the kill |
| 236 | // marker if we can sink MI past it. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 237 | KillMO = &MO; |
| 238 | else if (UseRegs.count(MOReg)) |
| 239 | // One of the uses is killed before the destination. |
| 240 | return false; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 245 | // Update kill and LV information. |
| 246 | KillMO->setIsKill(false); |
| 247 | KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI); |
| 248 | KillMO->setIsKill(true); |
Owen Anderson | 802af11 | 2008-07-02 21:28:58 +0000 | [diff] [blame] | 249 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 250 | if (LV) |
| 251 | LV->replaceKillInstruction(SavedReg, KillMI, MI); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 252 | |
| 253 | // Move instruction to its destination. |
| 254 | MBB->remove(MI); |
| 255 | MBB->insert(KillPos, MI); |
| 256 | |
| 257 | ++Num3AddrSunk; |
| 258 | return true; |
| 259 | } |
| 260 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 261 | /// isTwoAddrUse - Return true if the specified MI is using the specified |
| 262 | /// register as a two-address operand. |
| 263 | static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) { |
| 264 | const TargetInstrDesc &TID = UseMI->getDesc(); |
| 265 | for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) { |
| 266 | MachineOperand &MO = UseMI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 267 | if (MO.isReg() && MO.getReg() == Reg && |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 268 | (MO.isDef() || UseMI->isRegTiedToDefOperand(i))) |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 269 | // Earlier use is a two-address one. |
| 270 | return true; |
| 271 | } |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | /// isProfitableToReMat - Return true if the heuristics determines it is likely |
| 276 | /// to be profitable to re-materialize the definition of Reg rather than copy |
| 277 | /// the register. |
| 278 | bool |
| 279 | TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 280 | const TargetRegisterClass *RC, |
| 281 | MachineInstr *MI, MachineInstr *DefMI, |
| 282 | MachineBasicBlock *MBB, unsigned Loc) { |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 283 | bool OtherUse = false; |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 284 | for (MachineRegisterInfo::use_nodbg_iterator UI = MRI->use_nodbg_begin(Reg), |
| 285 | UE = MRI->use_nodbg_end(); UI != UE; ++UI) { |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 286 | MachineOperand &UseMO = UI.getOperand(); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 287 | MachineInstr *UseMI = UseMO.getParent(); |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 288 | MachineBasicBlock *UseMBB = UseMI->getParent(); |
| 289 | if (UseMBB == MBB) { |
| 290 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI); |
| 291 | if (DI != DistanceMap.end() && DI->second == Loc) |
| 292 | continue; // Current use. |
| 293 | OtherUse = true; |
| 294 | // There is at least one other use in the MBB that will clobber the |
| 295 | // register. |
| 296 | if (isTwoAddrUse(UseMI, Reg)) |
| 297 | return true; |
| 298 | } |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 299 | } |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 300 | |
| 301 | // If other uses in MBB are not two-address uses, then don't remat. |
| 302 | if (OtherUse) |
| 303 | return false; |
| 304 | |
| 305 | // No other uses in the same block, remat if it's defined in the same |
| 306 | // block so it does not unnecessarily extend the live range. |
| 307 | return MBB == DefMI->getParent(); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 310 | /// NoUseAfterLastDef - Return true if there are no intervening uses between the |
| 311 | /// last instruction in the MBB that defines the specified register and the |
| 312 | /// two-address instruction which is being processed. It also returns the last |
| 313 | /// def location by reference |
| 314 | bool TwoAddressInstructionPass::NoUseAfterLastDef(unsigned Reg, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 315 | MachineBasicBlock *MBB, unsigned Dist, |
| 316 | unsigned &LastDef) { |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 317 | LastDef = 0; |
| 318 | unsigned LastUse = Dist; |
| 319 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg), |
| 320 | E = MRI->reg_end(); I != E; ++I) { |
| 321 | MachineOperand &MO = I.getOperand(); |
| 322 | MachineInstr *MI = MO.getParent(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 323 | if (MI->getParent() != MBB || MI->isDebugValue()) |
Dale Johannesen | d94998f | 2010-02-09 02:01:46 +0000 | [diff] [blame] | 324 | continue; |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 325 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); |
| 326 | if (DI == DistanceMap.end()) |
| 327 | continue; |
| 328 | if (MO.isUse() && DI->second < LastUse) |
| 329 | LastUse = DI->second; |
| 330 | if (MO.isDef() && DI->second > LastDef) |
| 331 | LastDef = DI->second; |
| 332 | } |
| 333 | |
| 334 | return !(LastUse > LastDef && LastUse < Dist); |
| 335 | } |
| 336 | |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 337 | MachineInstr *TwoAddressInstructionPass::FindLastUseInMBB(unsigned Reg, |
| 338 | MachineBasicBlock *MBB, |
| 339 | unsigned Dist) { |
Lang Hames | a7c9dea | 2009-05-14 04:26:30 +0000 | [diff] [blame] | 340 | unsigned LastUseDist = 0; |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 341 | MachineInstr *LastUse = 0; |
| 342 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg), |
| 343 | E = MRI->reg_end(); I != E; ++I) { |
| 344 | MachineOperand &MO = I.getOperand(); |
| 345 | MachineInstr *MI = MO.getParent(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 346 | if (MI->getParent() != MBB || MI->isDebugValue()) |
Dale Johannesen | d94998f | 2010-02-09 02:01:46 +0000 | [diff] [blame] | 347 | continue; |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 348 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); |
| 349 | if (DI == DistanceMap.end()) |
| 350 | continue; |
Lang Hames | a7c9dea | 2009-05-14 04:26:30 +0000 | [diff] [blame] | 351 | if (DI->second >= Dist) |
| 352 | continue; |
| 353 | |
| 354 | if (MO.isUse() && DI->second > LastUseDist) { |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 355 | LastUse = DI->first; |
| 356 | LastUseDist = DI->second; |
| 357 | } |
| 358 | } |
| 359 | return LastUse; |
| 360 | } |
| 361 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 362 | /// isCopyToReg - Return true if the specified MI is a copy instruction or |
| 363 | /// a extract_subreg instruction. It also returns the source and destination |
| 364 | /// registers and whether they are physical registers by reference. |
| 365 | static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII, |
| 366 | unsigned &SrcReg, unsigned &DstReg, |
| 367 | bool &IsSrcPhys, bool &IsDstPhys) { |
| 368 | SrcReg = 0; |
| 369 | DstReg = 0; |
| 370 | unsigned SrcSubIdx, DstSubIdx; |
| 371 | if (!TII->isMoveInstr(MI, SrcReg, DstReg, SrcSubIdx, DstSubIdx)) { |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 372 | if (MI.isExtractSubreg()) { |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 373 | DstReg = MI.getOperand(0).getReg(); |
| 374 | SrcReg = MI.getOperand(1).getReg(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 375 | } else if (MI.isInsertSubreg()) { |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 376 | DstReg = MI.getOperand(0).getReg(); |
| 377 | SrcReg = MI.getOperand(2).getReg(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 378 | } else if (MI.isSubregToReg()) { |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 379 | DstReg = MI.getOperand(0).getReg(); |
| 380 | SrcReg = MI.getOperand(2).getReg(); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | if (DstReg) { |
| 385 | IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg); |
| 386 | IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
| 387 | return true; |
| 388 | } |
| 389 | return false; |
| 390 | } |
| 391 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 392 | /// isKilled - Test if the given register value, which is used by the given |
| 393 | /// instruction, is killed by the given instruction. This looks through |
| 394 | /// coalescable copies to see if the original value is potentially not killed. |
| 395 | /// |
| 396 | /// For example, in this code: |
| 397 | /// |
| 398 | /// %reg1034 = copy %reg1024 |
| 399 | /// %reg1035 = copy %reg1025<kill> |
| 400 | /// %reg1036 = add %reg1034<kill>, %reg1035<kill> |
| 401 | /// |
| 402 | /// %reg1034 is not considered to be killed, since it is copied from a |
| 403 | /// register which is not killed. Treating it as not killed lets the |
| 404 | /// normal heuristics commute the (two-address) add, which lets |
| 405 | /// coalescing eliminate the extra copy. |
| 406 | /// |
| 407 | static bool isKilled(MachineInstr &MI, unsigned Reg, |
| 408 | const MachineRegisterInfo *MRI, |
| 409 | const TargetInstrInfo *TII) { |
| 410 | MachineInstr *DefMI = &MI; |
| 411 | for (;;) { |
| 412 | if (!DefMI->killsRegister(Reg)) |
| 413 | return false; |
| 414 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 415 | return true; |
| 416 | MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg); |
| 417 | // If there are multiple defs, we can't do a simple analysis, so just |
| 418 | // go with what the kill flag says. |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 419 | if (llvm::next(Begin) != MRI->def_end()) |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 420 | return true; |
| 421 | DefMI = &*Begin; |
| 422 | bool IsSrcPhys, IsDstPhys; |
| 423 | unsigned SrcReg, DstReg; |
| 424 | // If the def is something other than a copy, then it isn't going to |
| 425 | // be coalesced, so follow the kill flag. |
| 426 | if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) |
| 427 | return true; |
| 428 | Reg = SrcReg; |
| 429 | } |
| 430 | } |
| 431 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 432 | /// isTwoAddrUse - Return true if the specified MI uses the specified register |
| 433 | /// as a two-address use. If so, return the destination register by reference. |
| 434 | static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) { |
| 435 | const TargetInstrDesc &TID = MI.getDesc(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 436 | unsigned NumOps = MI.isInlineAsm() ? MI.getNumOperands():TID.getNumOperands(); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 437 | for (unsigned i = 0; i != NumOps; ++i) { |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 438 | const MachineOperand &MO = MI.getOperand(i); |
| 439 | if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg) |
| 440 | continue; |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 441 | unsigned ti; |
| 442 | if (MI.isRegTiedToDefOperand(i, &ti)) { |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 443 | DstReg = MI.getOperand(ti).getReg(); |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | /// findOnlyInterestingUse - Given a register, if has a single in-basic block |
| 451 | /// use, return the use instruction if it's a copy or a two-address use. |
| 452 | static |
| 453 | MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB, |
| 454 | MachineRegisterInfo *MRI, |
| 455 | const TargetInstrInfo *TII, |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 456 | bool &IsCopy, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 457 | unsigned &DstReg, bool &IsDstPhys) { |
Evan Cheng | 1423c70 | 2010-03-03 21:18:38 +0000 | [diff] [blame] | 458 | if (!MRI->hasOneNonDBGUse(Reg)) |
| 459 | // None or more than one use. |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 460 | return 0; |
Evan Cheng | 1423c70 | 2010-03-03 21:18:38 +0000 | [diff] [blame] | 461 | MachineInstr &UseMI = *MRI->use_nodbg_begin(Reg); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 462 | if (UseMI.getParent() != MBB) |
| 463 | return 0; |
| 464 | unsigned SrcReg; |
| 465 | bool IsSrcPhys; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 466 | if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) { |
| 467 | IsCopy = true; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 468 | return &UseMI; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 469 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 470 | IsDstPhys = false; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 471 | if (isTwoAddrUse(UseMI, Reg, DstReg)) { |
| 472 | IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 473 | return &UseMI; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 474 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /// getMappedReg - Return the physical register the specified virtual register |
| 479 | /// might be mapped to. |
| 480 | static unsigned |
| 481 | getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) { |
| 482 | while (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 483 | DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg); |
| 484 | if (SI == RegMap.end()) |
| 485 | return 0; |
| 486 | Reg = SI->second; |
| 487 | } |
| 488 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 489 | return Reg; |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /// regsAreCompatible - Return true if the two registers are equal or aliased. |
| 494 | /// |
| 495 | static bool |
| 496 | regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) { |
| 497 | if (RegA == RegB) |
| 498 | return true; |
| 499 | if (!RegA || !RegB) |
| 500 | return false; |
| 501 | return TRI->regsOverlap(RegA, RegB); |
| 502 | } |
| 503 | |
| 504 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 505 | /// isProfitableToReMat - Return true if it's potentially profitable to commute |
| 506 | /// the two-address instruction that's being processed. |
| 507 | bool |
| 508 | TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 509 | MachineInstr *MI, MachineBasicBlock *MBB, |
| 510 | unsigned Dist) { |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 511 | // Determine if it's profitable to commute this two address instruction. In |
| 512 | // general, we want no uses between this instruction and the definition of |
| 513 | // the two-address register. |
| 514 | // e.g. |
| 515 | // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1 |
| 516 | // %reg1029<def> = MOV8rr %reg1028 |
| 517 | // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead> |
| 518 | // insert => %reg1030<def> = MOV8rr %reg1028 |
| 519 | // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead> |
| 520 | // In this case, it might not be possible to coalesce the second MOV8rr |
| 521 | // instruction if the first one is coalesced. So it would be profitable to |
| 522 | // commute it: |
| 523 | // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1 |
| 524 | // %reg1029<def> = MOV8rr %reg1028 |
| 525 | // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead> |
| 526 | // insert => %reg1030<def> = MOV8rr %reg1029 |
| 527 | // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead> |
| 528 | |
| 529 | if (!MI->killsRegister(regC)) |
| 530 | return false; |
| 531 | |
| 532 | // Ok, we have something like: |
| 533 | // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead> |
| 534 | // let's see if it's worth commuting it. |
| 535 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 536 | // Look for situations like this: |
| 537 | // %reg1024<def> = MOV r1 |
| 538 | // %reg1025<def> = MOV r0 |
| 539 | // %reg1026<def> = ADD %reg1024, %reg1025 |
| 540 | // r0 = MOV %reg1026 |
| 541 | // Commute the ADD to hopefully eliminate an otherwise unavoidable copy. |
| 542 | unsigned FromRegB = getMappedReg(regB, SrcRegMap); |
| 543 | unsigned FromRegC = getMappedReg(regC, SrcRegMap); |
| 544 | unsigned ToRegB = getMappedReg(regB, DstRegMap); |
| 545 | unsigned ToRegC = getMappedReg(regC, DstRegMap); |
| 546 | if (!regsAreCompatible(FromRegB, ToRegB, TRI) && |
| 547 | (regsAreCompatible(FromRegB, ToRegC, TRI) || |
| 548 | regsAreCompatible(FromRegC, ToRegB, TRI))) |
| 549 | return true; |
| 550 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 551 | // If there is a use of regC between its last def (could be livein) and this |
| 552 | // instruction, then bail. |
| 553 | unsigned LastDefC = 0; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 554 | if (!NoUseAfterLastDef(regC, MBB, Dist, LastDefC)) |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 555 | return false; |
| 556 | |
| 557 | // If there is a use of regB between its last def (could be livein) and this |
| 558 | // instruction, then go ahead and make this transformation. |
| 559 | unsigned LastDefB = 0; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 560 | if (!NoUseAfterLastDef(regB, MBB, Dist, LastDefB)) |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 561 | return true; |
| 562 | |
| 563 | // Since there are no intervening uses for both registers, then commute |
| 564 | // if the def of regC is closer. Its live interval is shorter. |
| 565 | return LastDefB && LastDefC && LastDefC > LastDefB; |
| 566 | } |
| 567 | |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 568 | /// CommuteInstruction - Commute a two-address instruction and update the basic |
| 569 | /// block, distance map, and live variables if needed. Return true if it is |
| 570 | /// successful. |
| 571 | bool |
| 572 | TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 573 | MachineFunction::iterator &mbbi, |
| 574 | unsigned RegB, unsigned RegC, unsigned Dist) { |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 575 | MachineInstr *MI = mi; |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 576 | DEBUG(dbgs() << "2addr: COMMUTING : " << *MI); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 577 | MachineInstr *NewMI = TII->commuteInstruction(MI); |
| 578 | |
| 579 | if (NewMI == 0) { |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 580 | DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n"); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 581 | return false; |
| 582 | } |
| 583 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 584 | DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 585 | // If the instruction changed to commute it, update livevar. |
| 586 | if (NewMI != MI) { |
| 587 | if (LV) |
| 588 | // Update live variables |
| 589 | LV->replaceKillInstruction(RegC, MI, NewMI); |
| 590 | |
| 591 | mbbi->insert(mi, NewMI); // Insert the new inst |
| 592 | mbbi->erase(mi); // Nuke the old inst. |
| 593 | mi = NewMI; |
| 594 | DistanceMap.insert(std::make_pair(NewMI, Dist)); |
| 595 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 596 | |
| 597 | // Update source register map. |
| 598 | unsigned FromRegC = getMappedReg(RegC, SrcRegMap); |
| 599 | if (FromRegC) { |
| 600 | unsigned RegA = MI->getOperand(0).getReg(); |
| 601 | SrcRegMap[RegA] = FromRegC; |
| 602 | } |
| 603 | |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 604 | return true; |
| 605 | } |
| 606 | |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 607 | /// isProfitableToConv3Addr - Return true if it is profitable to convert the |
| 608 | /// given 2-address instruction to a 3-address one. |
| 609 | bool |
| 610 | TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA) { |
| 611 | // Look for situations like this: |
| 612 | // %reg1024<def> = MOV r1 |
| 613 | // %reg1025<def> = MOV r0 |
| 614 | // %reg1026<def> = ADD %reg1024, %reg1025 |
| 615 | // r2 = MOV %reg1026 |
| 616 | // Turn ADD into a 3-address instruction to avoid a copy. |
| 617 | unsigned FromRegA = getMappedReg(RegA, SrcRegMap); |
| 618 | unsigned ToRegA = getMappedReg(RegA, DstRegMap); |
| 619 | return (FromRegA && ToRegA && !regsAreCompatible(FromRegA, ToRegA, TRI)); |
| 620 | } |
| 621 | |
| 622 | /// ConvertInstTo3Addr - Convert the specified two-address instruction into a |
| 623 | /// three address one. Return true if this transformation was successful. |
| 624 | bool |
| 625 | TwoAddressInstructionPass::ConvertInstTo3Addr(MachineBasicBlock::iterator &mi, |
| 626 | MachineBasicBlock::iterator &nmi, |
| 627 | MachineFunction::iterator &mbbi, |
| 628 | unsigned RegB, unsigned Dist) { |
| 629 | MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV); |
| 630 | if (NewMI) { |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 631 | DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi); |
| 632 | DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 633 | bool Sunk = false; |
| 634 | |
| 635 | if (NewMI->findRegisterUseOperand(RegB, false, TRI)) |
| 636 | // FIXME: Temporary workaround. If the new instruction doesn't |
| 637 | // uses RegB, convertToThreeAddress must have created more |
| 638 | // then one instruction. |
| 639 | Sunk = Sink3AddrInstruction(mbbi, NewMI, RegB, mi); |
| 640 | |
| 641 | mbbi->erase(mi); // Nuke the old inst. |
| 642 | |
| 643 | if (!Sunk) { |
| 644 | DistanceMap.insert(std::make_pair(NewMI, Dist)); |
| 645 | mi = NewMI; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 646 | nmi = llvm::next(mi); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 647 | } |
| 648 | return true; |
| 649 | } |
| 650 | |
| 651 | return false; |
| 652 | } |
| 653 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 654 | /// ProcessCopy - If the specified instruction is not yet processed, process it |
| 655 | /// if it's a copy. For a copy instruction, we find the physical registers the |
| 656 | /// source and destination registers might be mapped to. These are kept in |
| 657 | /// point-to maps used to determine future optimizations. e.g. |
| 658 | /// v1024 = mov r0 |
| 659 | /// v1025 = mov r1 |
| 660 | /// v1026 = add v1024, v1025 |
| 661 | /// r1 = mov r1026 |
| 662 | /// If 'add' is a two-address instruction, v1024, v1026 are both potentially |
| 663 | /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is |
| 664 | /// potentially joined with r1 on the output side. It's worthwhile to commute |
| 665 | /// 'add' to eliminate a copy. |
| 666 | void TwoAddressInstructionPass::ProcessCopy(MachineInstr *MI, |
| 667 | MachineBasicBlock *MBB, |
| 668 | SmallPtrSet<MachineInstr*, 8> &Processed) { |
| 669 | if (Processed.count(MI)) |
| 670 | return; |
| 671 | |
| 672 | bool IsSrcPhys, IsDstPhys; |
| 673 | unsigned SrcReg, DstReg; |
| 674 | if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) |
| 675 | return; |
| 676 | |
| 677 | if (IsDstPhys && !IsSrcPhys) |
| 678 | DstRegMap.insert(std::make_pair(SrcReg, DstReg)); |
| 679 | else if (!IsDstPhys && IsSrcPhys) { |
Evan Cheng | 3005ed6 | 2009-04-13 20:04:24 +0000 | [diff] [blame] | 680 | bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second; |
| 681 | if (!isNew) |
| 682 | assert(SrcRegMap[DstReg] == SrcReg && |
| 683 | "Can't map to two src physical registers!"); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 684 | |
| 685 | SmallVector<unsigned, 4> VirtRegPairs; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 686 | bool IsCopy = false; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 687 | unsigned NewReg = 0; |
| 688 | while (MachineInstr *UseMI = findOnlyInterestingUse(DstReg, MBB, MRI,TII, |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 689 | IsCopy, NewReg, IsDstPhys)) { |
| 690 | if (IsCopy) { |
| 691 | if (!Processed.insert(UseMI)) |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 692 | break; |
| 693 | } |
| 694 | |
| 695 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI); |
| 696 | if (DI != DistanceMap.end()) |
| 697 | // Earlier in the same MBB.Reached via a back edge. |
| 698 | break; |
| 699 | |
| 700 | if (IsDstPhys) { |
| 701 | VirtRegPairs.push_back(NewReg); |
| 702 | break; |
| 703 | } |
| 704 | bool isNew = SrcRegMap.insert(std::make_pair(NewReg, DstReg)).second; |
Evan Cheng | 3005ed6 | 2009-04-13 20:04:24 +0000 | [diff] [blame] | 705 | if (!isNew) |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 706 | assert(SrcRegMap[NewReg] == DstReg && |
| 707 | "Can't map to two src physical registers!"); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 708 | VirtRegPairs.push_back(NewReg); |
| 709 | DstReg = NewReg; |
| 710 | } |
| 711 | |
| 712 | if (!VirtRegPairs.empty()) { |
| 713 | unsigned ToReg = VirtRegPairs.back(); |
| 714 | VirtRegPairs.pop_back(); |
| 715 | while (!VirtRegPairs.empty()) { |
| 716 | unsigned FromReg = VirtRegPairs.back(); |
| 717 | VirtRegPairs.pop_back(); |
| 718 | bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second; |
Evan Cheng | 3005ed6 | 2009-04-13 20:04:24 +0000 | [diff] [blame] | 719 | if (!isNew) |
| 720 | assert(DstRegMap[FromReg] == ToReg && |
| 721 | "Can't map to two dst physical registers!"); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 722 | ToReg = FromReg; |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | Processed.insert(MI); |
| 728 | } |
| 729 | |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 730 | /// isSafeToDelete - If the specified instruction does not produce any side |
| 731 | /// effects and all of its defs are dead, then it's safe to delete. |
Jakob Stoklund Olesen | 0b25ae1 | 2009-11-18 21:33:35 +0000 | [diff] [blame] | 732 | static bool isSafeToDelete(MachineInstr *MI, |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 733 | const TargetInstrInfo *TII, |
| 734 | SmallVector<unsigned, 4> &Kills) { |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 735 | const TargetInstrDesc &TID = MI->getDesc(); |
| 736 | if (TID.mayStore() || TID.isCall()) |
| 737 | return false; |
| 738 | if (TID.isTerminator() || TID.hasUnmodeledSideEffects()) |
| 739 | return false; |
| 740 | |
| 741 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 742 | MachineOperand &MO = MI->getOperand(i); |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 743 | if (!MO.isReg()) |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 744 | continue; |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 745 | if (MO.isDef() && !MO.isDead()) |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 746 | return false; |
Jakob Stoklund Olesen | 0b25ae1 | 2009-11-18 21:33:35 +0000 | [diff] [blame] | 747 | if (MO.isUse() && MO.isKill()) |
Evan Cheng | e9ccb3a | 2009-04-28 02:12:36 +0000 | [diff] [blame] | 748 | Kills.push_back(MO.getReg()); |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 749 | } |
Evan Cheng | 28c7ce3 | 2009-02-21 03:14:25 +0000 | [diff] [blame] | 750 | return true; |
| 751 | } |
| 752 | |
Bob Wilson | 326f438 | 2009-09-01 22:51:08 +0000 | [diff] [blame] | 753 | /// canUpdateDeletedKills - Check if all the registers listed in Kills are |
| 754 | /// killed by instructions in MBB preceding the current instruction at |
| 755 | /// position Dist. If so, return true and record information about the |
| 756 | /// preceding kills in NewKills. |
| 757 | bool TwoAddressInstructionPass:: |
| 758 | canUpdateDeletedKills(SmallVector<unsigned, 4> &Kills, |
| 759 | SmallVector<NewKill, 4> &NewKills, |
| 760 | MachineBasicBlock *MBB, unsigned Dist) { |
| 761 | while (!Kills.empty()) { |
| 762 | unsigned Kill = Kills.back(); |
| 763 | Kills.pop_back(); |
| 764 | if (TargetRegisterInfo::isPhysicalRegister(Kill)) |
| 765 | return false; |
| 766 | |
| 767 | MachineInstr *LastKill = FindLastUseInMBB(Kill, MBB, Dist); |
| 768 | if (!LastKill) |
| 769 | return false; |
| 770 | |
| 771 | bool isModRef = LastKill->modifiesRegister(Kill); |
| 772 | NewKills.push_back(std::make_pair(std::make_pair(Kill, isModRef), |
| 773 | LastKill)); |
| 774 | } |
| 775 | return true; |
| 776 | } |
| 777 | |
| 778 | /// DeleteUnusedInstr - If an instruction with a tied register operand can |
| 779 | /// be safely deleted, just delete it. |
| 780 | bool |
| 781 | TwoAddressInstructionPass::DeleteUnusedInstr(MachineBasicBlock::iterator &mi, |
| 782 | MachineBasicBlock::iterator &nmi, |
| 783 | MachineFunction::iterator &mbbi, |
Bob Wilson | 326f438 | 2009-09-01 22:51:08 +0000 | [diff] [blame] | 784 | unsigned Dist) { |
| 785 | // Check if the instruction has no side effects and if all its defs are dead. |
| 786 | SmallVector<unsigned, 4> Kills; |
Jakob Stoklund Olesen | 0b25ae1 | 2009-11-18 21:33:35 +0000 | [diff] [blame] | 787 | if (!isSafeToDelete(mi, TII, Kills)) |
Bob Wilson | 326f438 | 2009-09-01 22:51:08 +0000 | [diff] [blame] | 788 | return false; |
| 789 | |
| 790 | // If this instruction kills some virtual registers, we need to |
| 791 | // update the kill information. If it's not possible to do so, |
| 792 | // then bail out. |
| 793 | SmallVector<NewKill, 4> NewKills; |
| 794 | if (!canUpdateDeletedKills(Kills, NewKills, &*mbbi, Dist)) |
| 795 | return false; |
| 796 | |
| 797 | if (LV) { |
| 798 | while (!NewKills.empty()) { |
| 799 | MachineInstr *NewKill = NewKills.back().second; |
| 800 | unsigned Kill = NewKills.back().first.first; |
| 801 | bool isDead = NewKills.back().first.second; |
| 802 | NewKills.pop_back(); |
| 803 | if (LV->removeVirtualRegisterKilled(Kill, mi)) { |
| 804 | if (isDead) |
| 805 | LV->addVirtualRegisterDead(Kill, NewKill); |
| 806 | else |
| 807 | LV->addVirtualRegisterKilled(Kill, NewKill); |
| 808 | } |
| 809 | } |
Bob Wilson | 326f438 | 2009-09-01 22:51:08 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | mbbi->erase(mi); // Nuke the old inst. |
| 813 | mi = nmi; |
| 814 | return true; |
| 815 | } |
| 816 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 817 | /// TryInstructionTransform - For the case where an instruction has a single |
| 818 | /// pair of tied register operands, attempt some transformations that may |
| 819 | /// either eliminate the tied operands or improve the opportunities for |
| 820 | /// coalescing away the register copy. Returns true if the tied operands |
| 821 | /// are eliminated altogether. |
| 822 | bool TwoAddressInstructionPass:: |
| 823 | TryInstructionTransform(MachineBasicBlock::iterator &mi, |
| 824 | MachineBasicBlock::iterator &nmi, |
| 825 | MachineFunction::iterator &mbbi, |
| 826 | unsigned SrcIdx, unsigned DstIdx, unsigned Dist) { |
| 827 | const TargetInstrDesc &TID = mi->getDesc(); |
| 828 | unsigned regA = mi->getOperand(DstIdx).getReg(); |
| 829 | unsigned regB = mi->getOperand(SrcIdx).getReg(); |
| 830 | |
| 831 | assert(TargetRegisterInfo::isVirtualRegister(regB) && |
| 832 | "cannot make instruction into two-address form"); |
| 833 | |
| 834 | // If regA is dead and the instruction can be deleted, just delete |
| 835 | // it so it doesn't clobber regB. |
| 836 | bool regBKilled = isKilled(*mi, regB, MRI, TII); |
| 837 | if (!regBKilled && mi->getOperand(DstIdx).isDead() && |
Jakob Stoklund Olesen | 0b25ae1 | 2009-11-18 21:33:35 +0000 | [diff] [blame] | 838 | DeleteUnusedInstr(mi, nmi, mbbi, Dist)) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 839 | ++NumDeletes; |
| 840 | return true; // Done with this instruction. |
| 841 | } |
| 842 | |
| 843 | // Check if it is profitable to commute the operands. |
| 844 | unsigned SrcOp1, SrcOp2; |
| 845 | unsigned regC = 0; |
| 846 | unsigned regCIdx = ~0U; |
| 847 | bool TryCommute = false; |
| 848 | bool AggressiveCommute = false; |
| 849 | if (TID.isCommutable() && mi->getNumOperands() >= 3 && |
| 850 | TII->findCommutedOpIndices(mi, SrcOp1, SrcOp2)) { |
| 851 | if (SrcIdx == SrcOp1) |
| 852 | regCIdx = SrcOp2; |
| 853 | else if (SrcIdx == SrcOp2) |
| 854 | regCIdx = SrcOp1; |
| 855 | |
| 856 | if (regCIdx != ~0U) { |
| 857 | regC = mi->getOperand(regCIdx).getReg(); |
| 858 | if (!regBKilled && isKilled(*mi, regC, MRI, TII)) |
| 859 | // If C dies but B does not, swap the B and C operands. |
| 860 | // This makes the live ranges of A and C joinable. |
| 861 | TryCommute = true; |
| 862 | else if (isProfitableToCommute(regB, regC, mi, mbbi, Dist)) { |
| 863 | TryCommute = true; |
| 864 | AggressiveCommute = true; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | // If it's profitable to commute, try to do so. |
| 870 | if (TryCommute && CommuteInstruction(mi, mbbi, regB, regC, Dist)) { |
| 871 | ++NumCommuted; |
| 872 | if (AggressiveCommute) |
| 873 | ++NumAggrCommuted; |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | if (TID.isConvertibleTo3Addr()) { |
| 878 | // This instruction is potentially convertible to a true |
| 879 | // three-address instruction. Check if it is profitable. |
| 880 | if (!regBKilled || isProfitableToConv3Addr(regA)) { |
| 881 | // Try to convert it. |
| 882 | if (ConvertInstTo3Addr(mi, nmi, mbbi, regB, Dist)) { |
| 883 | ++NumConvertedTo3Addr; |
| 884 | return true; // Done with this instruction. |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 891 | /// runOnMachineFunction - Reduce two-address instructions to two operands. |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 892 | /// |
Chris Lattner | 163c1e7 | 2004-01-31 21:14:04 +0000 | [diff] [blame] | 893 | bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) { |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 894 | DEBUG(dbgs() << "Machine Function\n"); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 895 | const TargetMachine &TM = MF.getTarget(); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 896 | MRI = &MF.getRegInfo(); |
| 897 | TII = TM.getInstrInfo(); |
| 898 | TRI = TM.getRegisterInfo(); |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 899 | LV = getAnalysisIfAvailable<LiveVariables>(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 900 | AA = &getAnalysis<AliasAnalysis>(); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 901 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 902 | bool MadeChange = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 903 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 904 | DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n"); |
| 905 | DEBUG(dbgs() << "********** Function: " |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 906 | << MF.getFunction()->getName() << '\n'); |
Alkis Evlogimenos | 3a9986f | 2004-02-18 00:35:06 +0000 | [diff] [blame] | 907 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 908 | // ReMatRegs - Keep track of the registers whose def's are remat'ed. |
| 909 | BitVector ReMatRegs; |
| 910 | ReMatRegs.resize(MRI->getLastVirtReg()+1); |
| 911 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 912 | typedef DenseMap<unsigned, SmallVector<std::pair<unsigned, unsigned>, 4> > |
| 913 | TiedOperandMap; |
| 914 | TiedOperandMap TiedOperands(4); |
| 915 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 916 | SmallPtrSet<MachineInstr*, 8> Processed; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 917 | for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end(); |
| 918 | mbbi != mbbe; ++mbbi) { |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 919 | unsigned Dist = 0; |
| 920 | DistanceMap.clear(); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 921 | SrcRegMap.clear(); |
| 922 | DstRegMap.clear(); |
| 923 | Processed.clear(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 924 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 925 | mi != me; ) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 926 | MachineBasicBlock::iterator nmi = llvm::next(mi); |
Dale Johannesen | b8ff934 | 2010-02-10 21:47:48 +0000 | [diff] [blame] | 927 | if (mi->isDebugValue()) { |
| 928 | mi = nmi; |
| 929 | continue; |
| 930 | } |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 932 | const TargetInstrDesc &TID = mi->getDesc(); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 933 | bool FirstTied = true; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 934 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 935 | DistanceMap.insert(std::make_pair(mi, ++Dist)); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 936 | |
| 937 | ProcessCopy(&*mi, &*mbbi, Processed); |
| 938 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 939 | // First scan through all the tied register uses in this instruction |
| 940 | // and record a list of pairs of tied operands for each register. |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 941 | unsigned NumOps = mi->isInlineAsm() |
Evan Cheng | fb11288 | 2009-03-23 08:01:15 +0000 | [diff] [blame] | 942 | ? mi->getNumOperands() : TID.getNumOperands(); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 943 | for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) { |
| 944 | unsigned DstIdx = 0; |
| 945 | if (!mi->isRegTiedToDefOperand(SrcIdx, &DstIdx)) |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 946 | continue; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 947 | |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 948 | if (FirstTied) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 949 | FirstTied = false; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 950 | ++NumTwoAddressInstrs; |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 951 | DEBUG(dbgs() << '\t' << *mi); |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 952 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 953 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 954 | assert(mi->getOperand(SrcIdx).isReg() && |
| 955 | mi->getOperand(SrcIdx).getReg() && |
| 956 | mi->getOperand(SrcIdx).isUse() && |
| 957 | "two address instruction invalid"); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 958 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 959 | unsigned regB = mi->getOperand(SrcIdx).getReg(); |
| 960 | TiedOperandMap::iterator OI = TiedOperands.find(regB); |
| 961 | if (OI == TiedOperands.end()) { |
| 962 | SmallVector<std::pair<unsigned, unsigned>, 4> TiedPair; |
| 963 | OI = TiedOperands.insert(std::make_pair(regB, TiedPair)).first; |
| 964 | } |
| 965 | OI->second.push_back(std::make_pair(SrcIdx, DstIdx)); |
| 966 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 967 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 968 | // Now iterate over the information collected above. |
| 969 | for (TiedOperandMap::iterator OI = TiedOperands.begin(), |
| 970 | OE = TiedOperands.end(); OI != OE; ++OI) { |
| 971 | SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs = OI->second; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 972 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 973 | // If the instruction has a single pair of tied operands, try some |
| 974 | // transformations that may either eliminate the tied operands or |
| 975 | // improve the opportunities for coalescing away the register copy. |
| 976 | if (TiedOperands.size() == 1 && TiedPairs.size() == 1) { |
| 977 | unsigned SrcIdx = TiedPairs[0].first; |
| 978 | unsigned DstIdx = TiedPairs[0].second; |
Bob Wilson | 4344979 | 2009-08-31 21:54:55 +0000 | [diff] [blame] | 979 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 980 | // If the registers are already equal, nothing needs to be done. |
| 981 | if (mi->getOperand(SrcIdx).getReg() == |
| 982 | mi->getOperand(DstIdx).getReg()) |
| 983 | break; // Done with this instruction. |
| 984 | |
| 985 | if (TryInstructionTransform(mi, nmi, mbbi, SrcIdx, DstIdx, Dist)) |
| 986 | break; // The tied operands have been eliminated. |
| 987 | } |
| 988 | |
| 989 | bool RemovedKillFlag = false; |
| 990 | bool AllUsesCopied = true; |
| 991 | unsigned LastCopiedReg = 0; |
| 992 | unsigned regB = OI->first; |
| 993 | for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) { |
| 994 | unsigned SrcIdx = TiedPairs[tpi].first; |
| 995 | unsigned DstIdx = TiedPairs[tpi].second; |
| 996 | unsigned regA = mi->getOperand(DstIdx).getReg(); |
| 997 | // Grab regB from the instruction because it may have changed if the |
| 998 | // instruction was commuted. |
| 999 | regB = mi->getOperand(SrcIdx).getReg(); |
| 1000 | |
| 1001 | if (regA == regB) { |
| 1002 | // The register is tied to multiple destinations (or else we would |
| 1003 | // not have continued this far), but this use of the register |
| 1004 | // already matches the tied destination. Leave it. |
| 1005 | AllUsesCopied = false; |
| 1006 | continue; |
| 1007 | } |
| 1008 | LastCopiedReg = regA; |
| 1009 | |
| 1010 | assert(TargetRegisterInfo::isVirtualRegister(regB) && |
| 1011 | "cannot make instruction into two-address form"); |
Chris Lattner | 6b50767 | 2004-01-31 21:21:43 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 1013 | #ifndef NDEBUG |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1014 | // First, verify that we don't have a use of "a" in the instruction |
| 1015 | // (a = b + a for example) because our transformation will not |
| 1016 | // work. This should never occur because we are in SSA form. |
| 1017 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) |
| 1018 | assert(i == DstIdx || |
| 1019 | !mi->getOperand(i).isReg() || |
| 1020 | mi->getOperand(i).getReg() != regA); |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 1021 | #endif |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 1022 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1023 | // Emit a copy or rematerialize the definition. |
| 1024 | const TargetRegisterClass *rc = MRI->getRegClass(regB); |
| 1025 | MachineInstr *DefMI = MRI->getVRegDef(regB); |
| 1026 | // If it's safe and profitable, remat the definition instead of |
| 1027 | // copying it. |
| 1028 | if (DefMI && |
| 1029 | DefMI->getDesc().isAsCheapAsAMove() && |
Evan Cheng | ac1abde | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 1030 | DefMI->isSafeToReMat(TII, AA, regB) && |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1031 | isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist)){ |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 1032 | DEBUG(dbgs() << "2addr: REMATTING : " << *DefMI << "\n"); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1033 | unsigned regASubIdx = mi->getOperand(DstIdx).getSubReg(); |
Evan Cheng | d57cdd5 | 2009-11-14 02:55:43 +0000 | [diff] [blame] | 1034 | TII->reMaterialize(*mbbi, mi, regA, regASubIdx, DefMI, TRI); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1035 | ReMatRegs.set(regB); |
| 1036 | ++NumReMats; |
Bob Wilson | 71124f6 | 2009-09-01 04:18:40 +0000 | [diff] [blame] | 1037 | } else { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1038 | bool Emitted = TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc); |
| 1039 | (void)Emitted; |
| 1040 | assert(Emitted && "Unable to issue a copy instruction!\n"); |
| 1041 | } |
| 1042 | |
| 1043 | MachineBasicBlock::iterator prevMI = prior(mi); |
| 1044 | // Update DistanceMap. |
| 1045 | DistanceMap.insert(std::make_pair(prevMI, Dist)); |
| 1046 | DistanceMap[mi] = ++Dist; |
| 1047 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 1048 | DEBUG(dbgs() << "\t\tprepend:\t" << *prevMI); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1049 | |
| 1050 | MachineOperand &MO = mi->getOperand(SrcIdx); |
| 1051 | assert(MO.isReg() && MO.getReg() == regB && MO.isUse() && |
| 1052 | "inconsistent operand info for 2-reg pass"); |
| 1053 | if (MO.isKill()) { |
| 1054 | MO.setIsKill(false); |
| 1055 | RemovedKillFlag = true; |
| 1056 | } |
| 1057 | MO.setReg(regA); |
| 1058 | } |
| 1059 | |
| 1060 | if (AllUsesCopied) { |
| 1061 | // Replace other (un-tied) uses of regB with LastCopiedReg. |
| 1062 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 1063 | MachineOperand &MO = mi->getOperand(i); |
| 1064 | if (MO.isReg() && MO.getReg() == regB && MO.isUse()) { |
| 1065 | if (MO.isKill()) { |
| 1066 | MO.setIsKill(false); |
| 1067 | RemovedKillFlag = true; |
| 1068 | } |
| 1069 | MO.setReg(LastCopiedReg); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | // Update live variables for regB. |
| 1074 | if (RemovedKillFlag && LV && LV->getVarInfo(regB).removeKill(mi)) |
| 1075 | LV->addVirtualRegisterKilled(regB, prior(mi)); |
| 1076 | |
| 1077 | } else if (RemovedKillFlag) { |
| 1078 | // Some tied uses of regB matched their destination registers, so |
| 1079 | // regB is still used in this instruction, but a kill flag was |
| 1080 | // removed from a different tied use of regB, so now we need to add |
| 1081 | // a kill flag to one of the remaining uses of regB. |
| 1082 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 1083 | MachineOperand &MO = mi->getOperand(i); |
| 1084 | if (MO.isReg() && MO.getReg() == regB && MO.isUse()) { |
| 1085 | MO.setIsKill(true); |
| 1086 | break; |
Bob Wilson | 71124f6 | 2009-09-01 04:18:40 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
Bob Wilson | 4344979 | 2009-08-31 21:54:55 +0000 | [diff] [blame] | 1089 | } |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1090 | |
Bob Wilson | 4344979 | 2009-08-31 21:54:55 +0000 | [diff] [blame] | 1091 | MadeChange = true; |
| 1092 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 1093 | DEBUG(dbgs() << "\t\trewrite to:\t" << *mi); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1094 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 1095 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1096 | // Clear TiedOperands here instead of at the top of the loop |
| 1097 | // since most instructions do not have tied operands. |
| 1098 | TiedOperands.clear(); |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 1099 | mi = nmi; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 1103 | // Some remat'ed instructions are dead. |
| 1104 | int VReg = ReMatRegs.find_first(); |
| 1105 | while (VReg != -1) { |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 1106 | if (MRI->use_nodbg_empty(VReg)) { |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 1107 | MachineInstr *DefMI = MRI->getVRegDef(VReg); |
| 1108 | DefMI->eraseFromParent(); |
Bill Wendling | a16157a | 2008-05-26 05:49:49 +0000 | [diff] [blame] | 1109 | } |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 1110 | VReg = ReMatRegs.find_next(VReg); |
Bill Wendling | 48f7f23 | 2008-05-26 05:18:34 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1113 | return MadeChange; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1114 | } |