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