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" |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/LiveVariables.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 36 | #include "llvm/CodeGen/MachineInstr.h" |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/AliasAnalysis.h" |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 40 | #include "llvm/MC/MCInstrItineraries.h" |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetRegisterInfo.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 42 | #include "llvm/Target/TargetInstrInfo.h" |
| 43 | #include "llvm/Target/TargetMachine.h" |
Owen Anderson | 95dad83 | 2008-10-07 20:22:28 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetOptions.h" |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Debug.h" |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 46 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/BitVector.h" |
| 48 | #include "llvm/ADT/DenseMap.h" |
Dan Gohman | d68a076 | 2009-01-05 17:59:02 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/SmallSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/Statistic.h" |
| 51 | #include "llvm/ADT/STLExtras.h" |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 54 | STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions"); |
| 55 | STATISTIC(NumCommuted , "Number of instructions commuted to coalesce"); |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 56 | STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted"); |
Chris Lattner | cd3245a | 2006-12-19 22:41:21 +0000 | [diff] [blame] | 57 | STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 58 | STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk"); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 59 | STATISTIC(NumReSchedUps, "Number of instructions re-scheduled up"); |
| 60 | STATISTIC(NumReSchedDowns, "Number of instructions re-scheduled down"); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 61 | |
| 62 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 63 | class TwoAddressInstructionPass : public MachineFunctionPass { |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 64 | MachineFunction *MF; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 65 | const TargetInstrInfo *TII; |
| 66 | const TargetRegisterInfo *TRI; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 67 | const InstrItineraryData *InstrItins; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 68 | MachineRegisterInfo *MRI; |
| 69 | LiveVariables *LV; |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 70 | SlotIndexes *Indexes; |
| 71 | LiveIntervals *LIS; |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 72 | AliasAnalysis *AA; |
Evan Cheng | c3aa7c5 | 2011-11-16 18:44:48 +0000 | [diff] [blame] | 73 | CodeGenOpt::Level OptLevel; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 74 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 75 | // DistanceMap - Keep track the distance of a MI from the start of the |
| 76 | // current basic block. |
| 77 | DenseMap<MachineInstr*, unsigned> DistanceMap; |
| 78 | |
| 79 | // SrcRegMap - A map from virtual registers to physical registers which |
| 80 | // are likely targets to be coalesced to due to copies from physical |
| 81 | // registers to virtual registers. e.g. v1024 = move r0. |
| 82 | DenseMap<unsigned, unsigned> SrcRegMap; |
| 83 | |
| 84 | // DstRegMap - A map from virtual registers to physical registers which |
| 85 | // are likely targets to be coalesced to due to copies to physical |
| 86 | // registers from virtual registers. e.g. r1 = move v1024. |
| 87 | DenseMap<unsigned, unsigned> DstRegMap; |
| 88 | |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 89 | /// RegSequences - Keep track the list of REG_SEQUENCE instructions seen |
| 90 | /// during the initial walk of the machine function. |
| 91 | SmallVector<MachineInstr*, 16> RegSequences; |
| 92 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 93 | bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI, |
| 94 | unsigned Reg, |
| 95 | MachineBasicBlock::iterator OldPos); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 96 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 97 | bool NoUseAfterLastDef(unsigned Reg, MachineBasicBlock *MBB, unsigned Dist, |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 98 | unsigned &LastDef); |
| 99 | |
Evan Cheng | d99d68b | 2012-05-03 01:45:13 +0000 | [diff] [blame] | 100 | bool isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC, |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 101 | MachineInstr *MI, MachineBasicBlock *MBB, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 102 | unsigned Dist); |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 103 | |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 104 | bool CommuteInstruction(MachineBasicBlock::iterator &mi, |
| 105 | MachineFunction::iterator &mbbi, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 106 | unsigned RegB, unsigned RegC, unsigned Dist); |
| 107 | |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 108 | bool isProfitableToConv3Addr(unsigned RegA, unsigned RegB); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 109 | |
| 110 | bool ConvertInstTo3Addr(MachineBasicBlock::iterator &mi, |
| 111 | MachineBasicBlock::iterator &nmi, |
| 112 | MachineFunction::iterator &mbbi, |
Evan Cheng | 4d96c63 | 2011-02-10 02:20:55 +0000 | [diff] [blame] | 113 | unsigned RegA, unsigned RegB, unsigned Dist); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 114 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 115 | bool isDefTooClose(unsigned Reg, unsigned Dist, |
| 116 | MachineInstr *MI, MachineBasicBlock *MBB); |
| 117 | |
| 118 | bool RescheduleMIBelowKill(MachineBasicBlock *MBB, |
| 119 | MachineBasicBlock::iterator &mi, |
| 120 | MachineBasicBlock::iterator &nmi, |
| 121 | unsigned Reg); |
| 122 | bool RescheduleKillAboveMI(MachineBasicBlock *MBB, |
| 123 | MachineBasicBlock::iterator &mi, |
| 124 | MachineBasicBlock::iterator &nmi, |
| 125 | unsigned Reg); |
| 126 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 127 | bool TryInstructionTransform(MachineBasicBlock::iterator &mi, |
| 128 | MachineBasicBlock::iterator &nmi, |
| 129 | MachineFunction::iterator &mbbi, |
| 130 | unsigned SrcIdx, unsigned DstIdx, |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 131 | unsigned Dist, |
| 132 | SmallPtrSet<MachineInstr*, 8> &Processed); |
| 133 | |
| 134 | void ScanUses(unsigned DstReg, MachineBasicBlock *MBB, |
| 135 | SmallPtrSet<MachineInstr*, 8> &Processed); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 136 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 137 | void ProcessCopy(MachineInstr *MI, MachineBasicBlock *MBB, |
| 138 | SmallPtrSet<MachineInstr*, 8> &Processed); |
Evan Cheng | 3a3cce5 | 2009-08-07 00:28:58 +0000 | [diff] [blame] | 139 | |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 140 | typedef SmallVector<std::pair<unsigned, unsigned>, 4> TiedOpList; |
| 141 | typedef SmallDenseMap<unsigned, TiedOpList> TiedOperandMap; |
| 142 | bool collectTiedOperands(MachineInstr *MI, TiedOperandMap&); |
| 143 | |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 144 | void CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs, unsigned DstReg); |
| 145 | |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 146 | /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part |
| 147 | /// of the de-ssa process. This replaces sources of REG_SEQUENCE as |
| 148 | /// sub-register references of the register defined by REG_SEQUENCE. |
| 149 | bool EliminateRegSequences(); |
Evan Cheng | c6dcce3 | 2010-05-17 23:24:12 +0000 | [diff] [blame] | 150 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 151 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 152 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 153 | TwoAddressInstructionPass() : MachineFunctionPass(ID) { |
| 154 | initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry()); |
| 155 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 156 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 157 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 845012e | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 158 | AU.setPreservesCFG(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 159 | AU.addRequired<AliasAnalysis>(); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 160 | AU.addPreserved<LiveVariables>(); |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 161 | AU.addPreserved<SlotIndexes>(); |
| 162 | AU.addPreserved<LiveIntervals>(); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 163 | AU.addPreservedID(MachineLoopInfoID); |
| 164 | AU.addPreservedID(MachineDominatorsID); |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 165 | MachineFunctionPass::getAnalysisUsage(AU); |
| 166 | } |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 167 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 168 | /// runOnMachineFunction - Pass entry point. |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 169 | bool runOnMachineFunction(MachineFunction&); |
| 170 | }; |
Chris Lattner | d74ea2b | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 171 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 172 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 173 | char TwoAddressInstructionPass::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 174 | INITIALIZE_PASS_BEGIN(TwoAddressInstructionPass, "twoaddressinstruction", |
| 175 | "Two-Address instruction pass", false, false) |
| 176 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 177 | INITIALIZE_PASS_END(TwoAddressInstructionPass, "twoaddressinstruction", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 178 | "Two-Address instruction pass", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 179 | |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 180 | char &llvm::TwoAddressInstructionPassID = TwoAddressInstructionPass::ID; |
Alkis Evlogimenos | 4c08086 | 2003-12-18 22:40:24 +0000 | [diff] [blame] | 181 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 182 | /// Sink3AddrInstruction - A two-address instruction has been converted to a |
| 183 | /// three-address instruction to avoid clobbering a register. Try to sink it |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 184 | /// past the instruction that would kill the above mentioned register to reduce |
| 185 | /// register pressure. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 186 | bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB, |
| 187 | MachineInstr *MI, unsigned SavedReg, |
| 188 | MachineBasicBlock::iterator OldPos) { |
Eli Friedman | bde81d5 | 2011-09-23 22:41:57 +0000 | [diff] [blame] | 189 | // FIXME: Shouldn't we be trying to do this before we three-addressify the |
| 190 | // instruction? After this transformation is done, we no longer need |
| 191 | // the instruction to be in three-address form. |
| 192 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 193 | // Check if it's safe to move this instruction. |
| 194 | bool SeenStore = true; // Be conservative. |
Evan Cheng | ac1abde | 2010-03-02 19:03:01 +0000 | [diff] [blame] | 195 | if (!MI->isSafeToMove(TII, AA, SeenStore)) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 196 | return false; |
| 197 | |
| 198 | unsigned DefReg = 0; |
| 199 | SmallSet<unsigned, 4> UseRegs; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 200 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 201 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 202 | const MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 203 | if (!MO.isReg()) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 204 | continue; |
| 205 | unsigned MOReg = MO.getReg(); |
| 206 | if (!MOReg) |
| 207 | continue; |
| 208 | if (MO.isUse() && MOReg != SavedReg) |
| 209 | UseRegs.insert(MO.getReg()); |
| 210 | if (!MO.isDef()) |
| 211 | continue; |
| 212 | if (MO.isImplicit()) |
| 213 | // Don't try to move it if it implicitly defines a register. |
| 214 | return false; |
| 215 | if (DefReg) |
| 216 | // For now, don't move any instructions that define multiple registers. |
| 217 | return false; |
| 218 | DefReg = MO.getReg(); |
| 219 | } |
| 220 | |
| 221 | // Find the instruction that kills SavedReg. |
| 222 | MachineInstr *KillMI = NULL; |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 223 | for (MachineRegisterInfo::use_nodbg_iterator |
| 224 | UI = MRI->use_nodbg_begin(SavedReg), |
| 225 | UE = MRI->use_nodbg_end(); UI != UE; ++UI) { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 226 | MachineOperand &UseMO = UI.getOperand(); |
| 227 | if (!UseMO.isKill()) |
| 228 | continue; |
| 229 | KillMI = UseMO.getParent(); |
| 230 | break; |
| 231 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 232 | |
Eli Friedman | bde81d5 | 2011-09-23 22:41:57 +0000 | [diff] [blame] | 233 | // If we find the instruction that kills SavedReg, and it is in an |
| 234 | // appropriate location, we can try to sink the current instruction |
| 235 | // past it. |
| 236 | if (!KillMI || KillMI->getParent() != MBB || KillMI == MI || |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 237 | KillMI->isTerminator()) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 238 | return false; |
| 239 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 240 | // If any of the definitions are used by another instruction between the |
| 241 | // position and the kill use, then it's not safe to sink it. |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 242 | // |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 243 | // 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] | 244 | // instruction is before or after another instruction. Then we can use |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 245 | // MachineRegisterInfo def / use instead. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 246 | MachineOperand *KillMO = NULL; |
| 247 | MachineBasicBlock::iterator KillPos = KillMI; |
| 248 | ++KillPos; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 249 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 250 | unsigned NumVisited = 0; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 251 | for (MachineBasicBlock::iterator I = llvm::next(OldPos); I != KillPos; ++I) { |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 252 | MachineInstr *OtherMI = I; |
Dale Johannesen | 3bfef03 | 2010-02-11 18:22:31 +0000 | [diff] [blame] | 253 | // DBG_VALUE cannot be counted against the limit. |
| 254 | if (OtherMI->isDebugValue()) |
| 255 | continue; |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 256 | if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost. |
| 257 | return false; |
| 258 | ++NumVisited; |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 259 | for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) { |
| 260 | MachineOperand &MO = OtherMI->getOperand(i); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 261 | if (!MO.isReg()) |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 262 | continue; |
| 263 | unsigned MOReg = MO.getReg(); |
| 264 | if (!MOReg) |
| 265 | continue; |
| 266 | if (DefReg == MOReg) |
| 267 | return false; |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 268 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 269 | if (MO.isKill()) { |
| 270 | if (OtherMI == KillMI && MOReg == SavedReg) |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 271 | // Save the operand that kills the register. We want to unset the kill |
| 272 | // marker if we can sink MI past it. |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 273 | KillMO = &MO; |
| 274 | else if (UseRegs.count(MOReg)) |
| 275 | // One of the uses is killed before the destination. |
| 276 | return false; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 281 | // Update kill and LV information. |
| 282 | KillMO->setIsKill(false); |
| 283 | KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI); |
| 284 | KillMO->setIsKill(true); |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 285 | |
Evan Cheng | 9f1c831 | 2008-07-03 09:09:37 +0000 | [diff] [blame] | 286 | if (LV) |
| 287 | LV->replaceKillInstruction(SavedReg, KillMI, MI); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 288 | |
| 289 | // Move instruction to its destination. |
| 290 | MBB->remove(MI); |
| 291 | MBB->insert(KillPos, MI); |
| 292 | |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 293 | if (LIS) |
| 294 | LIS->handleMove(MI); |
| 295 | |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 296 | ++Num3AddrSunk; |
| 297 | return true; |
| 298 | } |
| 299 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 300 | /// NoUseAfterLastDef - Return true if there are no intervening uses between the |
| 301 | /// last instruction in the MBB that defines the specified register and the |
| 302 | /// two-address instruction which is being processed. It also returns the last |
| 303 | /// def location by reference |
| 304 | bool TwoAddressInstructionPass::NoUseAfterLastDef(unsigned Reg, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 305 | MachineBasicBlock *MBB, unsigned Dist, |
| 306 | unsigned &LastDef) { |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 307 | LastDef = 0; |
| 308 | unsigned LastUse = Dist; |
| 309 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg), |
| 310 | E = MRI->reg_end(); I != E; ++I) { |
| 311 | MachineOperand &MO = I.getOperand(); |
| 312 | MachineInstr *MI = MO.getParent(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 313 | if (MI->getParent() != MBB || MI->isDebugValue()) |
Dale Johannesen | d94998f | 2010-02-09 02:01:46 +0000 | [diff] [blame] | 314 | continue; |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 315 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); |
| 316 | if (DI == DistanceMap.end()) |
| 317 | continue; |
| 318 | if (MO.isUse() && DI->second < LastUse) |
| 319 | LastUse = DI->second; |
| 320 | if (MO.isDef() && DI->second > LastDef) |
| 321 | LastDef = DI->second; |
| 322 | } |
| 323 | |
| 324 | return !(LastUse > LastDef && LastUse < Dist); |
| 325 | } |
| 326 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 327 | /// isCopyToReg - Return true if the specified MI is a copy instruction or |
| 328 | /// a extract_subreg instruction. It also returns the source and destination |
| 329 | /// registers and whether they are physical registers by reference. |
| 330 | static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII, |
| 331 | unsigned &SrcReg, unsigned &DstReg, |
| 332 | bool &IsSrcPhys, bool &IsDstPhys) { |
| 333 | SrcReg = 0; |
| 334 | DstReg = 0; |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 335 | if (MI.isCopy()) { |
| 336 | DstReg = MI.getOperand(0).getReg(); |
| 337 | SrcReg = MI.getOperand(1).getReg(); |
| 338 | } else if (MI.isInsertSubreg() || MI.isSubregToReg()) { |
| 339 | DstReg = MI.getOperand(0).getReg(); |
| 340 | SrcReg = MI.getOperand(2).getReg(); |
| 341 | } else |
| 342 | return false; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 343 | |
Jakob Stoklund Olesen | 04c528a | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 344 | IsSrcPhys = TargetRegisterInfo::isPhysicalRegister(SrcReg); |
| 345 | IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
| 346 | return true; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 349 | /// isKilled - Test if the given register value, which is used by the given |
| 350 | /// instruction, is killed by the given instruction. This looks through |
| 351 | /// coalescable copies to see if the original value is potentially not killed. |
| 352 | /// |
| 353 | /// For example, in this code: |
| 354 | /// |
| 355 | /// %reg1034 = copy %reg1024 |
| 356 | /// %reg1035 = copy %reg1025<kill> |
| 357 | /// %reg1036 = add %reg1034<kill>, %reg1035<kill> |
| 358 | /// |
| 359 | /// %reg1034 is not considered to be killed, since it is copied from a |
| 360 | /// register which is not killed. Treating it as not killed lets the |
| 361 | /// normal heuristics commute the (two-address) add, which lets |
| 362 | /// coalescing eliminate the extra copy. |
| 363 | /// |
| 364 | static bool isKilled(MachineInstr &MI, unsigned Reg, |
| 365 | const MachineRegisterInfo *MRI, |
| 366 | const TargetInstrInfo *TII) { |
| 367 | MachineInstr *DefMI = &MI; |
| 368 | for (;;) { |
| 369 | if (!DefMI->killsRegister(Reg)) |
| 370 | return false; |
| 371 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 372 | return true; |
| 373 | MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg); |
| 374 | // If there are multiple defs, we can't do a simple analysis, so just |
| 375 | // go with what the kill flag says. |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 376 | if (llvm::next(Begin) != MRI->def_end()) |
Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 377 | return true; |
| 378 | DefMI = &*Begin; |
| 379 | bool IsSrcPhys, IsDstPhys; |
| 380 | unsigned SrcReg, DstReg; |
| 381 | // If the def is something other than a copy, then it isn't going to |
| 382 | // be coalesced, so follow the kill flag. |
| 383 | if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) |
| 384 | return true; |
| 385 | Reg = SrcReg; |
| 386 | } |
| 387 | } |
| 388 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 389 | /// isTwoAddrUse - Return true if the specified MI uses the specified register |
| 390 | /// as a two-address use. If so, return the destination register by reference. |
| 391 | static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 392 | const MCInstrDesc &MCID = MI.getDesc(); |
| 393 | unsigned NumOps = MI.isInlineAsm() |
| 394 | ? MI.getNumOperands() : MCID.getNumOperands(); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 395 | for (unsigned i = 0; i != NumOps; ++i) { |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 396 | const MachineOperand &MO = MI.getOperand(i); |
| 397 | if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg) |
| 398 | continue; |
Evan Cheng | a24752f | 2009-03-19 20:30:06 +0000 | [diff] [blame] | 399 | unsigned ti; |
| 400 | if (MI.isRegTiedToDefOperand(i, &ti)) { |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 401 | DstReg = MI.getOperand(ti).getReg(); |
| 402 | return true; |
| 403 | } |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | /// findOnlyInterestingUse - Given a register, if has a single in-basic block |
| 409 | /// use, return the use instruction if it's a copy or a two-address use. |
| 410 | static |
| 411 | MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB, |
| 412 | MachineRegisterInfo *MRI, |
| 413 | const TargetInstrInfo *TII, |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 414 | bool &IsCopy, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 415 | unsigned &DstReg, bool &IsDstPhys) { |
Evan Cheng | 1423c70 | 2010-03-03 21:18:38 +0000 | [diff] [blame] | 416 | if (!MRI->hasOneNonDBGUse(Reg)) |
| 417 | // None or more than one use. |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 418 | return 0; |
Evan Cheng | 1423c70 | 2010-03-03 21:18:38 +0000 | [diff] [blame] | 419 | MachineInstr &UseMI = *MRI->use_nodbg_begin(Reg); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 420 | if (UseMI.getParent() != MBB) |
| 421 | return 0; |
| 422 | unsigned SrcReg; |
| 423 | bool IsSrcPhys; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 424 | if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) { |
| 425 | IsCopy = true; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 426 | return &UseMI; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 427 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 428 | IsDstPhys = false; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 429 | if (isTwoAddrUse(UseMI, Reg, DstReg)) { |
| 430 | IsDstPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 431 | return &UseMI; |
Evan Cheng | 87d696a | 2009-04-14 00:32:25 +0000 | [diff] [blame] | 432 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /// getMappedReg - Return the physical register the specified virtual register |
| 437 | /// might be mapped to. |
| 438 | static unsigned |
| 439 | getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) { |
| 440 | while (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 441 | DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg); |
| 442 | if (SI == RegMap.end()) |
| 443 | return 0; |
| 444 | Reg = SI->second; |
| 445 | } |
| 446 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
| 447 | return Reg; |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | /// regsAreCompatible - Return true if the two registers are equal or aliased. |
| 452 | /// |
| 453 | static bool |
| 454 | regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) { |
| 455 | if (RegA == RegB) |
| 456 | return true; |
| 457 | if (!RegA || !RegB) |
| 458 | return false; |
| 459 | return TRI->regsOverlap(RegA, RegB); |
| 460 | } |
| 461 | |
| 462 | |
Manman Ren | d68e8cd | 2012-07-25 18:28:13 +0000 | [diff] [blame] | 463 | /// isProfitableToCommute - Return true if it's potentially profitable to commute |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 464 | /// the two-address instruction that's being processed. |
| 465 | bool |
Evan Cheng | d99d68b | 2012-05-03 01:45:13 +0000 | [diff] [blame] | 466 | TwoAddressInstructionPass::isProfitableToCommute(unsigned regA, unsigned regB, |
| 467 | unsigned regC, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 468 | MachineInstr *MI, MachineBasicBlock *MBB, |
| 469 | unsigned Dist) { |
Evan Cheng | c3aa7c5 | 2011-11-16 18:44:48 +0000 | [diff] [blame] | 470 | if (OptLevel == CodeGenOpt::None) |
| 471 | return false; |
| 472 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 473 | // Determine if it's profitable to commute this two address instruction. In |
| 474 | // general, we want no uses between this instruction and the definition of |
| 475 | // the two-address register. |
| 476 | // e.g. |
| 477 | // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1 |
| 478 | // %reg1029<def> = MOV8rr %reg1028 |
| 479 | // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead> |
| 480 | // insert => %reg1030<def> = MOV8rr %reg1028 |
| 481 | // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead> |
| 482 | // In this case, it might not be possible to coalesce the second MOV8rr |
| 483 | // instruction if the first one is coalesced. So it would be profitable to |
| 484 | // commute it: |
| 485 | // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1 |
| 486 | // %reg1029<def> = MOV8rr %reg1028 |
| 487 | // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead> |
| 488 | // insert => %reg1030<def> = MOV8rr %reg1029 |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 489 | // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead> |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 490 | |
| 491 | if (!MI->killsRegister(regC)) |
| 492 | return false; |
| 493 | |
| 494 | // Ok, we have something like: |
| 495 | // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead> |
| 496 | // let's see if it's worth commuting it. |
| 497 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 498 | // Look for situations like this: |
| 499 | // %reg1024<def> = MOV r1 |
| 500 | // %reg1025<def> = MOV r0 |
| 501 | // %reg1026<def> = ADD %reg1024, %reg1025 |
| 502 | // r0 = MOV %reg1026 |
| 503 | // Commute the ADD to hopefully eliminate an otherwise unavoidable copy. |
Evan Cheng | d99d68b | 2012-05-03 01:45:13 +0000 | [diff] [blame] | 504 | unsigned ToRegA = getMappedReg(regA, DstRegMap); |
| 505 | if (ToRegA) { |
| 506 | unsigned FromRegB = getMappedReg(regB, SrcRegMap); |
| 507 | unsigned FromRegC = getMappedReg(regC, SrcRegMap); |
| 508 | bool BComp = !FromRegB || regsAreCompatible(FromRegB, ToRegA, TRI); |
| 509 | bool CComp = !FromRegC || regsAreCompatible(FromRegC, ToRegA, TRI); |
| 510 | if (BComp != CComp) |
| 511 | return !BComp && CComp; |
| 512 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 513 | |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 514 | // If there is a use of regC between its last def (could be livein) and this |
| 515 | // instruction, then bail. |
| 516 | unsigned LastDefC = 0; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 517 | if (!NoUseAfterLastDef(regC, MBB, Dist, LastDefC)) |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 518 | return false; |
| 519 | |
| 520 | // If there is a use of regB between its last def (could be livein) and this |
| 521 | // instruction, then go ahead and make this transformation. |
| 522 | unsigned LastDefB = 0; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 523 | if (!NoUseAfterLastDef(regB, MBB, Dist, LastDefB)) |
Evan Cheng | d498c8f | 2009-01-25 03:53:59 +0000 | [diff] [blame] | 524 | return true; |
| 525 | |
| 526 | // Since there are no intervening uses for both registers, then commute |
| 527 | // if the def of regC is closer. Its live interval is shorter. |
| 528 | return LastDefB && LastDefC && LastDefC > LastDefB; |
| 529 | } |
| 530 | |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 531 | /// CommuteInstruction - Commute a two-address instruction and update the basic |
| 532 | /// block, distance map, and live variables if needed. Return true if it is |
| 533 | /// successful. |
| 534 | bool |
| 535 | TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi, |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 536 | MachineFunction::iterator &mbbi, |
| 537 | unsigned RegB, unsigned RegC, unsigned Dist) { |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 538 | MachineInstr *MI = mi; |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 539 | DEBUG(dbgs() << "2addr: COMMUTING : " << *MI); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 540 | MachineInstr *NewMI = TII->commuteInstruction(MI); |
| 541 | |
| 542 | if (NewMI == 0) { |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 543 | DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n"); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 544 | return false; |
| 545 | } |
| 546 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 547 | DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 548 | // If the instruction changed to commute it, update livevar. |
| 549 | if (NewMI != MI) { |
| 550 | if (LV) |
| 551 | // Update live variables |
| 552 | LV->replaceKillInstruction(RegC, MI, NewMI); |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 553 | if (Indexes) |
| 554 | Indexes->replaceMachineInstrInMaps(MI, NewMI); |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 555 | |
| 556 | mbbi->insert(mi, NewMI); // Insert the new inst |
| 557 | mbbi->erase(mi); // Nuke the old inst. |
| 558 | mi = NewMI; |
| 559 | DistanceMap.insert(std::make_pair(NewMI, Dist)); |
| 560 | } |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 561 | |
| 562 | // Update source register map. |
| 563 | unsigned FromRegC = getMappedReg(RegC, SrcRegMap); |
| 564 | if (FromRegC) { |
| 565 | unsigned RegA = MI->getOperand(0).getReg(); |
| 566 | SrcRegMap[RegA] = FromRegC; |
| 567 | } |
| 568 | |
Evan Cheng | 8191371 | 2009-01-23 23:27:33 +0000 | [diff] [blame] | 569 | return true; |
| 570 | } |
| 571 | |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 572 | /// isProfitableToConv3Addr - Return true if it is profitable to convert the |
| 573 | /// given 2-address instruction to a 3-address one. |
| 574 | bool |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 575 | TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA,unsigned RegB){ |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 576 | // Look for situations like this: |
| 577 | // %reg1024<def> = MOV r1 |
| 578 | // %reg1025<def> = MOV r0 |
| 579 | // %reg1026<def> = ADD %reg1024, %reg1025 |
| 580 | // r2 = MOV %reg1026 |
| 581 | // Turn ADD into a 3-address instruction to avoid a copy. |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 582 | unsigned FromRegB = getMappedReg(RegB, SrcRegMap); |
| 583 | if (!FromRegB) |
| 584 | return false; |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 585 | unsigned ToRegA = getMappedReg(RegA, DstRegMap); |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 586 | return (ToRegA && !regsAreCompatible(FromRegB, ToRegA, TRI)); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /// ConvertInstTo3Addr - Convert the specified two-address instruction into a |
| 590 | /// three address one. Return true if this transformation was successful. |
| 591 | bool |
| 592 | TwoAddressInstructionPass::ConvertInstTo3Addr(MachineBasicBlock::iterator &mi, |
| 593 | MachineBasicBlock::iterator &nmi, |
| 594 | MachineFunction::iterator &mbbi, |
Evan Cheng | 4d96c63 | 2011-02-10 02:20:55 +0000 | [diff] [blame] | 595 | unsigned RegA, unsigned RegB, |
| 596 | unsigned Dist) { |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 597 | MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV); |
| 598 | if (NewMI) { |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 599 | DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi); |
| 600 | DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 601 | bool Sunk = false; |
| 602 | |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 603 | if (Indexes) |
| 604 | Indexes->replaceMachineInstrInMaps(mi, NewMI); |
| 605 | |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 606 | if (NewMI->findRegisterUseOperand(RegB, false, TRI)) |
| 607 | // FIXME: Temporary workaround. If the new instruction doesn't |
| 608 | // uses RegB, convertToThreeAddress must have created more |
| 609 | // then one instruction. |
| 610 | Sunk = Sink3AddrInstruction(mbbi, NewMI, RegB, mi); |
| 611 | |
| 612 | mbbi->erase(mi); // Nuke the old inst. |
| 613 | |
| 614 | if (!Sunk) { |
| 615 | DistanceMap.insert(std::make_pair(NewMI, Dist)); |
| 616 | mi = NewMI; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 617 | nmi = llvm::next(mi); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 618 | } |
Evan Cheng | 4d96c63 | 2011-02-10 02:20:55 +0000 | [diff] [blame] | 619 | |
| 620 | // Update source and destination register maps. |
| 621 | SrcRegMap.erase(RegA); |
| 622 | DstRegMap.erase(RegB); |
Evan Cheng | e6f350d | 2009-03-30 21:34:07 +0000 | [diff] [blame] | 623 | return true; |
| 624 | } |
| 625 | |
| 626 | return false; |
| 627 | } |
| 628 | |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 629 | /// ScanUses - Scan forward recursively for only uses, update maps if the use |
| 630 | /// is a copy or a two-address instruction. |
| 631 | void |
| 632 | TwoAddressInstructionPass::ScanUses(unsigned DstReg, MachineBasicBlock *MBB, |
| 633 | SmallPtrSet<MachineInstr*, 8> &Processed) { |
| 634 | SmallVector<unsigned, 4> VirtRegPairs; |
| 635 | bool IsDstPhys; |
| 636 | bool IsCopy = false; |
| 637 | unsigned NewReg = 0; |
| 638 | unsigned Reg = DstReg; |
| 639 | while (MachineInstr *UseMI = findOnlyInterestingUse(Reg, MBB, MRI, TII,IsCopy, |
| 640 | NewReg, IsDstPhys)) { |
| 641 | if (IsCopy && !Processed.insert(UseMI)) |
| 642 | break; |
| 643 | |
| 644 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI); |
| 645 | if (DI != DistanceMap.end()) |
| 646 | // Earlier in the same MBB.Reached via a back edge. |
| 647 | break; |
| 648 | |
| 649 | if (IsDstPhys) { |
| 650 | VirtRegPairs.push_back(NewReg); |
| 651 | break; |
| 652 | } |
| 653 | bool isNew = SrcRegMap.insert(std::make_pair(NewReg, Reg)).second; |
| 654 | if (!isNew) |
| 655 | assert(SrcRegMap[NewReg] == Reg && "Can't map to two src registers!"); |
| 656 | VirtRegPairs.push_back(NewReg); |
| 657 | Reg = NewReg; |
| 658 | } |
| 659 | |
| 660 | if (!VirtRegPairs.empty()) { |
| 661 | unsigned ToReg = VirtRegPairs.back(); |
| 662 | VirtRegPairs.pop_back(); |
| 663 | while (!VirtRegPairs.empty()) { |
| 664 | unsigned FromReg = VirtRegPairs.back(); |
| 665 | VirtRegPairs.pop_back(); |
| 666 | bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second; |
| 667 | if (!isNew) |
| 668 | assert(DstRegMap[FromReg] == ToReg &&"Can't map to two dst registers!"); |
| 669 | ToReg = FromReg; |
| 670 | } |
| 671 | bool isNew = DstRegMap.insert(std::make_pair(DstReg, ToReg)).second; |
| 672 | if (!isNew) |
| 673 | assert(DstRegMap[DstReg] == ToReg && "Can't map to two dst registers!"); |
| 674 | } |
| 675 | } |
| 676 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 677 | /// ProcessCopy - If the specified instruction is not yet processed, process it |
| 678 | /// if it's a copy. For a copy instruction, we find the physical registers the |
| 679 | /// source and destination registers might be mapped to. These are kept in |
| 680 | /// point-to maps used to determine future optimizations. e.g. |
| 681 | /// v1024 = mov r0 |
| 682 | /// v1025 = mov r1 |
| 683 | /// v1026 = add v1024, v1025 |
| 684 | /// r1 = mov r1026 |
| 685 | /// If 'add' is a two-address instruction, v1024, v1026 are both potentially |
| 686 | /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is |
| 687 | /// potentially joined with r1 on the output side. It's worthwhile to commute |
| 688 | /// 'add' to eliminate a copy. |
| 689 | void TwoAddressInstructionPass::ProcessCopy(MachineInstr *MI, |
| 690 | MachineBasicBlock *MBB, |
| 691 | SmallPtrSet<MachineInstr*, 8> &Processed) { |
| 692 | if (Processed.count(MI)) |
| 693 | return; |
| 694 | |
| 695 | bool IsSrcPhys, IsDstPhys; |
| 696 | unsigned SrcReg, DstReg; |
| 697 | if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) |
| 698 | return; |
| 699 | |
| 700 | if (IsDstPhys && !IsSrcPhys) |
| 701 | DstRegMap.insert(std::make_pair(SrcReg, DstReg)); |
| 702 | else if (!IsDstPhys && IsSrcPhys) { |
Evan Cheng | 3005ed6 | 2009-04-13 20:04:24 +0000 | [diff] [blame] | 703 | bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second; |
| 704 | if (!isNew) |
| 705 | assert(SrcRegMap[DstReg] == SrcReg && |
| 706 | "Can't map to two src physical registers!"); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 707 | |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 708 | ScanUses(DstReg, MBB, Processed); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | Processed.insert(MI); |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 712 | return; |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 715 | /// RescheduleMIBelowKill - If there is one more local instruction that reads |
| 716 | /// 'Reg' and it kills 'Reg, consider moving the instruction below the kill |
| 717 | /// instruction in order to eliminate the need for the copy. |
| 718 | bool |
| 719 | TwoAddressInstructionPass::RescheduleMIBelowKill(MachineBasicBlock *MBB, |
| 720 | MachineBasicBlock::iterator &mi, |
| 721 | MachineBasicBlock::iterator &nmi, |
| 722 | unsigned Reg) { |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 723 | // Bail immediately if we don't have LV available. We use it to find kills |
| 724 | // efficiently. |
| 725 | if (!LV) |
| 726 | return false; |
| 727 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 728 | MachineInstr *MI = &*mi; |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 729 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 730 | if (DI == DistanceMap.end()) |
| 731 | // Must be created from unfolded load. Don't waste time trying this. |
| 732 | return false; |
| 733 | |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 734 | MachineInstr *KillMI = LV->getVarInfo(Reg).findKill(MBB); |
| 735 | if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike()) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 736 | // Don't mess with copies, they may be coalesced later. |
| 737 | return false; |
| 738 | |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 739 | if (KillMI->hasUnmodeledSideEffects() || KillMI->isCall() || |
| 740 | KillMI->isBranch() || KillMI->isTerminator()) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 741 | // Don't move pass calls, etc. |
| 742 | return false; |
| 743 | |
| 744 | unsigned DstReg; |
| 745 | if (isTwoAddrUse(*KillMI, Reg, DstReg)) |
| 746 | return false; |
| 747 | |
Evan Cheng | f178418 | 2011-11-15 06:26:51 +0000 | [diff] [blame] | 748 | bool SeenStore = true; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 749 | if (!MI->isSafeToMove(TII, AA, SeenStore)) |
| 750 | return false; |
| 751 | |
| 752 | if (TII->getInstrLatency(InstrItins, MI) > 1) |
| 753 | // FIXME: Needs more sophisticated heuristics. |
| 754 | return false; |
| 755 | |
| 756 | SmallSet<unsigned, 2> Uses; |
Evan Cheng | 9bad88a | 2011-11-16 03:47:42 +0000 | [diff] [blame] | 757 | SmallSet<unsigned, 2> Kills; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 758 | SmallSet<unsigned, 2> Defs; |
| 759 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 760 | const MachineOperand &MO = MI->getOperand(i); |
| 761 | if (!MO.isReg()) |
| 762 | continue; |
| 763 | unsigned MOReg = MO.getReg(); |
| 764 | if (!MOReg) |
| 765 | continue; |
| 766 | if (MO.isDef()) |
| 767 | Defs.insert(MOReg); |
Evan Cheng | 9bad88a | 2011-11-16 03:47:42 +0000 | [diff] [blame] | 768 | else { |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 769 | Uses.insert(MOReg); |
Evan Cheng | 9bad88a | 2011-11-16 03:47:42 +0000 | [diff] [blame] | 770 | if (MO.isKill() && MOReg != Reg) |
| 771 | Kills.insert(MOReg); |
| 772 | } |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // Move the copies connected to MI down as well. |
| 776 | MachineBasicBlock::iterator From = MI; |
| 777 | MachineBasicBlock::iterator To = llvm::next(From); |
| 778 | while (To->isCopy() && Defs.count(To->getOperand(1).getReg())) { |
| 779 | Defs.insert(To->getOperand(0).getReg()); |
| 780 | ++To; |
| 781 | } |
| 782 | |
| 783 | // Check if the reschedule will not break depedencies. |
| 784 | unsigned NumVisited = 0; |
| 785 | MachineBasicBlock::iterator KillPos = KillMI; |
| 786 | ++KillPos; |
| 787 | for (MachineBasicBlock::iterator I = To; I != KillPos; ++I) { |
| 788 | MachineInstr *OtherMI = I; |
| 789 | // DBG_VALUE cannot be counted against the limit. |
| 790 | if (OtherMI->isDebugValue()) |
| 791 | continue; |
| 792 | if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost. |
| 793 | return false; |
| 794 | ++NumVisited; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 795 | if (OtherMI->hasUnmodeledSideEffects() || OtherMI->isCall() || |
| 796 | OtherMI->isBranch() || OtherMI->isTerminator()) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 797 | // Don't move pass calls, etc. |
| 798 | return false; |
| 799 | for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) { |
| 800 | const MachineOperand &MO = OtherMI->getOperand(i); |
| 801 | if (!MO.isReg()) |
| 802 | continue; |
| 803 | unsigned MOReg = MO.getReg(); |
| 804 | if (!MOReg) |
| 805 | continue; |
| 806 | if (MO.isDef()) { |
| 807 | if (Uses.count(MOReg)) |
| 808 | // Physical register use would be clobbered. |
| 809 | return false; |
| 810 | if (!MO.isDead() && Defs.count(MOReg)) |
| 811 | // May clobber a physical register def. |
| 812 | // FIXME: This may be too conservative. It's ok if the instruction |
| 813 | // is sunken completely below the use. |
| 814 | return false; |
| 815 | } else { |
| 816 | if (Defs.count(MOReg)) |
| 817 | return false; |
Evan Cheng | 9bad88a | 2011-11-16 03:47:42 +0000 | [diff] [blame] | 818 | if (MOReg != Reg && |
| 819 | ((MO.isKill() && Uses.count(MOReg)) || Kills.count(MOReg))) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 820 | // Don't want to extend other live ranges and update kills. |
| 821 | return false; |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 822 | if (MOReg == Reg && !MO.isKill()) |
| 823 | // We can't schedule across a use of the register in question. |
| 824 | return false; |
| 825 | // Ensure that if this is register in question, its the kill we expect. |
| 826 | assert((MOReg != Reg || OtherMI == KillMI) && |
| 827 | "Found multiple kills of a register in a basic block"); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // Move debug info as well. |
Evan Cheng | 8aee7d8 | 2011-11-14 21:11:15 +0000 | [diff] [blame] | 833 | while (From != MBB->begin() && llvm::prior(From)->isDebugValue()) |
| 834 | --From; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 835 | |
| 836 | // Copies following MI may have been moved as well. |
| 837 | nmi = To; |
| 838 | MBB->splice(KillPos, MBB, From, To); |
| 839 | DistanceMap.erase(DI); |
| 840 | |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 841 | // Update live variables |
| 842 | LV->removeVirtualRegisterKilled(Reg, KillMI); |
| 843 | LV->addVirtualRegisterKilled(Reg, MI); |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 844 | if (LIS) |
| 845 | LIS->handleMove(MI); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 846 | |
Jakob Stoklund Olesen | a532bce | 2012-07-17 17:57:23 +0000 | [diff] [blame] | 847 | DEBUG(dbgs() << "\trescheduled below kill: " << *KillMI); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 848 | return true; |
| 849 | } |
| 850 | |
| 851 | /// isDefTooClose - Return true if the re-scheduling will put the given |
| 852 | /// instruction too close to the defs of its register dependencies. |
| 853 | bool TwoAddressInstructionPass::isDefTooClose(unsigned Reg, unsigned Dist, |
| 854 | MachineInstr *MI, |
| 855 | MachineBasicBlock *MBB) { |
| 856 | for (MachineRegisterInfo::def_iterator DI = MRI->def_begin(Reg), |
| 857 | DE = MRI->def_end(); DI != DE; ++DI) { |
| 858 | MachineInstr *DefMI = &*DI; |
| 859 | if (DefMI->getParent() != MBB || DefMI->isCopy() || DefMI->isCopyLike()) |
| 860 | continue; |
| 861 | if (DefMI == MI) |
| 862 | return true; // MI is defining something KillMI uses |
| 863 | DenseMap<MachineInstr*, unsigned>::iterator DDI = DistanceMap.find(DefMI); |
| 864 | if (DDI == DistanceMap.end()) |
| 865 | return true; // Below MI |
| 866 | unsigned DefDist = DDI->second; |
| 867 | assert(Dist > DefDist && "Visited def already?"); |
Andrew Trick | b7e0289 | 2012-06-05 21:11:27 +0000 | [diff] [blame] | 868 | if (TII->getInstrLatency(InstrItins, DefMI) > (Dist - DefDist)) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 869 | return true; |
| 870 | } |
| 871 | return false; |
| 872 | } |
| 873 | |
| 874 | /// RescheduleKillAboveMI - If there is one more local instruction that reads |
| 875 | /// 'Reg' and it kills 'Reg, consider moving the kill instruction above the |
| 876 | /// current two-address instruction in order to eliminate the need for the |
| 877 | /// copy. |
| 878 | bool |
| 879 | TwoAddressInstructionPass::RescheduleKillAboveMI(MachineBasicBlock *MBB, |
| 880 | MachineBasicBlock::iterator &mi, |
| 881 | MachineBasicBlock::iterator &nmi, |
| 882 | unsigned Reg) { |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 883 | // Bail immediately if we don't have LV available. We use it to find kills |
| 884 | // efficiently. |
| 885 | if (!LV) |
| 886 | return false; |
| 887 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 888 | MachineInstr *MI = &*mi; |
| 889 | DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); |
| 890 | if (DI == DistanceMap.end()) |
| 891 | // Must be created from unfolded load. Don't waste time trying this. |
| 892 | return false; |
| 893 | |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 894 | MachineInstr *KillMI = LV->getVarInfo(Reg).findKill(MBB); |
| 895 | if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike()) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 896 | // Don't mess with copies, they may be coalesced later. |
| 897 | return false; |
| 898 | |
| 899 | unsigned DstReg; |
| 900 | if (isTwoAddrUse(*KillMI, Reg, DstReg)) |
| 901 | return false; |
| 902 | |
Evan Cheng | f178418 | 2011-11-15 06:26:51 +0000 | [diff] [blame] | 903 | bool SeenStore = true; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 904 | if (!KillMI->isSafeToMove(TII, AA, SeenStore)) |
| 905 | return false; |
| 906 | |
| 907 | SmallSet<unsigned, 2> Uses; |
| 908 | SmallSet<unsigned, 2> Kills; |
| 909 | SmallSet<unsigned, 2> Defs; |
| 910 | SmallSet<unsigned, 2> LiveDefs; |
| 911 | for (unsigned i = 0, e = KillMI->getNumOperands(); i != e; ++i) { |
| 912 | const MachineOperand &MO = KillMI->getOperand(i); |
| 913 | if (!MO.isReg()) |
| 914 | continue; |
| 915 | unsigned MOReg = MO.getReg(); |
| 916 | if (MO.isUse()) { |
| 917 | if (!MOReg) |
| 918 | continue; |
| 919 | if (isDefTooClose(MOReg, DI->second, MI, MBB)) |
| 920 | return false; |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 921 | if (MOReg == Reg && !MO.isKill()) |
| 922 | return false; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 923 | Uses.insert(MOReg); |
| 924 | if (MO.isKill() && MOReg != Reg) |
| 925 | Kills.insert(MOReg); |
| 926 | } else if (TargetRegisterInfo::isPhysicalRegister(MOReg)) { |
| 927 | Defs.insert(MOReg); |
| 928 | if (!MO.isDead()) |
| 929 | LiveDefs.insert(MOReg); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | // Check if the reschedule will not break depedencies. |
| 934 | unsigned NumVisited = 0; |
| 935 | MachineBasicBlock::iterator KillPos = KillMI; |
| 936 | for (MachineBasicBlock::iterator I = mi; I != KillPos; ++I) { |
| 937 | MachineInstr *OtherMI = I; |
| 938 | // DBG_VALUE cannot be counted against the limit. |
| 939 | if (OtherMI->isDebugValue()) |
| 940 | continue; |
| 941 | if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost. |
| 942 | return false; |
| 943 | ++NumVisited; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 944 | if (OtherMI->hasUnmodeledSideEffects() || OtherMI->isCall() || |
| 945 | OtherMI->isBranch() || OtherMI->isTerminator()) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 946 | // Don't move pass calls, etc. |
| 947 | return false; |
Evan Cheng | ae7db7a | 2011-11-16 03:05:12 +0000 | [diff] [blame] | 948 | SmallVector<unsigned, 2> OtherDefs; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 949 | for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) { |
| 950 | const MachineOperand &MO = OtherMI->getOperand(i); |
| 951 | if (!MO.isReg()) |
| 952 | continue; |
| 953 | unsigned MOReg = MO.getReg(); |
| 954 | if (!MOReg) |
| 955 | continue; |
| 956 | if (MO.isUse()) { |
| 957 | if (Defs.count(MOReg)) |
| 958 | // Moving KillMI can clobber the physical register if the def has |
| 959 | // not been seen. |
| 960 | return false; |
| 961 | if (Kills.count(MOReg)) |
| 962 | // Don't want to extend other live ranges and update kills. |
| 963 | return false; |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 964 | if (OtherMI != MI && MOReg == Reg && !MO.isKill()) |
| 965 | // We can't schedule across a use of the register in question. |
| 966 | return false; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 967 | } else { |
Evan Cheng | ae7db7a | 2011-11-16 03:05:12 +0000 | [diff] [blame] | 968 | OtherDefs.push_back(MOReg); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
Evan Cheng | ae7db7a | 2011-11-16 03:05:12 +0000 | [diff] [blame] | 971 | |
| 972 | for (unsigned i = 0, e = OtherDefs.size(); i != e; ++i) { |
| 973 | unsigned MOReg = OtherDefs[i]; |
| 974 | if (Uses.count(MOReg)) |
| 975 | return false; |
| 976 | if (TargetRegisterInfo::isPhysicalRegister(MOReg) && |
| 977 | LiveDefs.count(MOReg)) |
| 978 | return false; |
| 979 | // Physical register def is seen. |
| 980 | Defs.erase(MOReg); |
| 981 | } |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | // Move the old kill above MI, don't forget to move debug info as well. |
| 985 | MachineBasicBlock::iterator InsertPos = mi; |
Evan Cheng | 8aee7d8 | 2011-11-14 21:11:15 +0000 | [diff] [blame] | 986 | while (InsertPos != MBB->begin() && llvm::prior(InsertPos)->isDebugValue()) |
| 987 | --InsertPos; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 988 | MachineBasicBlock::iterator From = KillMI; |
| 989 | MachineBasicBlock::iterator To = llvm::next(From); |
| 990 | while (llvm::prior(From)->isDebugValue()) |
| 991 | --From; |
| 992 | MBB->splice(InsertPos, MBB, From, To); |
| 993 | |
Evan Cheng | 2bee6a8 | 2011-11-16 03:33:08 +0000 | [diff] [blame] | 994 | nmi = llvm::prior(InsertPos); // Backtrack so we process the moved instr. |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 995 | DistanceMap.erase(DI); |
| 996 | |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 997 | // Update live variables |
| 998 | LV->removeVirtualRegisterKilled(Reg, KillMI); |
| 999 | LV->addVirtualRegisterKilled(Reg, MI); |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 1000 | if (LIS) |
| 1001 | LIS->handleMove(KillMI); |
Chandler Carruth | 7d532c8 | 2012-07-15 03:29:46 +0000 | [diff] [blame] | 1002 | |
Jakob Stoklund Olesen | a532bce | 2012-07-17 17:57:23 +0000 | [diff] [blame] | 1003 | DEBUG(dbgs() << "\trescheduled kill: " << *KillMI); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1004 | return true; |
| 1005 | } |
| 1006 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1007 | /// TryInstructionTransform - For the case where an instruction has a single |
| 1008 | /// pair of tied register operands, attempt some transformations that may |
| 1009 | /// either eliminate the tied operands or improve the opportunities for |
Lang Hames | f31ceaf | 2012-04-09 20:17:30 +0000 | [diff] [blame] | 1010 | /// coalescing away the register copy. Returns true if no copy needs to be |
| 1011 | /// inserted to untie mi's operands (either because they were untied, or |
| 1012 | /// because mi was rescheduled, and will be visited again later). |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1013 | bool TwoAddressInstructionPass:: |
| 1014 | TryInstructionTransform(MachineBasicBlock::iterator &mi, |
| 1015 | MachineBasicBlock::iterator &nmi, |
| 1016 | MachineFunction::iterator &mbbi, |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 1017 | unsigned SrcIdx, unsigned DstIdx, unsigned Dist, |
| 1018 | SmallPtrSet<MachineInstr*, 8> &Processed) { |
Evan Cheng | c3aa7c5 | 2011-11-16 18:44:48 +0000 | [diff] [blame] | 1019 | if (OptLevel == CodeGenOpt::None) |
| 1020 | return false; |
| 1021 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1022 | MachineInstr &MI = *mi; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1023 | unsigned regA = MI.getOperand(DstIdx).getReg(); |
| 1024 | unsigned regB = MI.getOperand(SrcIdx).getReg(); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1025 | |
| 1026 | assert(TargetRegisterInfo::isVirtualRegister(regB) && |
| 1027 | "cannot make instruction into two-address form"); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1028 | bool regBKilled = isKilled(MI, regB, MRI, TII); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1029 | |
Evan Cheng | d99d68b | 2012-05-03 01:45:13 +0000 | [diff] [blame] | 1030 | if (TargetRegisterInfo::isVirtualRegister(regA)) |
| 1031 | ScanUses(regA, &*mbbi, Processed); |
| 1032 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1033 | // Check if it is profitable to commute the operands. |
| 1034 | unsigned SrcOp1, SrcOp2; |
| 1035 | unsigned regC = 0; |
| 1036 | unsigned regCIdx = ~0U; |
| 1037 | bool TryCommute = false; |
| 1038 | bool AggressiveCommute = false; |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1039 | if (MI.isCommutable() && MI.getNumOperands() >= 3 && |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1040 | TII->findCommutedOpIndices(&MI, SrcOp1, SrcOp2)) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1041 | if (SrcIdx == SrcOp1) |
| 1042 | regCIdx = SrcOp2; |
| 1043 | else if (SrcIdx == SrcOp2) |
| 1044 | regCIdx = SrcOp1; |
| 1045 | |
| 1046 | if (regCIdx != ~0U) { |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1047 | regC = MI.getOperand(regCIdx).getReg(); |
| 1048 | if (!regBKilled && isKilled(MI, regC, MRI, TII)) |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1049 | // If C dies but B does not, swap the B and C operands. |
| 1050 | // This makes the live ranges of A and C joinable. |
| 1051 | TryCommute = true; |
Evan Cheng | d99d68b | 2012-05-03 01:45:13 +0000 | [diff] [blame] | 1052 | else if (isProfitableToCommute(regA, regB, regC, &MI, mbbi, Dist)) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1053 | TryCommute = true; |
| 1054 | AggressiveCommute = true; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | // If it's profitable to commute, try to do so. |
| 1060 | if (TryCommute && CommuteInstruction(mi, mbbi, regB, regC, Dist)) { |
| 1061 | ++NumCommuted; |
| 1062 | if (AggressiveCommute) |
| 1063 | ++NumAggrCommuted; |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1067 | // If there is one more use of regB later in the same MBB, consider |
| 1068 | // re-schedule this MI below it. |
| 1069 | if (RescheduleMIBelowKill(mbbi, mi, nmi, regB)) { |
| 1070 | ++NumReSchedDowns; |
Lang Hames | f31ceaf | 2012-04-09 20:17:30 +0000 | [diff] [blame] | 1071 | return true; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1074 | if (MI.isConvertibleTo3Addr()) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1075 | // This instruction is potentially convertible to a true |
| 1076 | // three-address instruction. Check if it is profitable. |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 1077 | if (!regBKilled || isProfitableToConv3Addr(regA, regB)) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1078 | // Try to convert it. |
Evan Cheng | 4d96c63 | 2011-02-10 02:20:55 +0000 | [diff] [blame] | 1079 | if (ConvertInstTo3Addr(mi, nmi, mbbi, regA, regB, Dist)) { |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1080 | ++NumConvertedTo3Addr; |
| 1081 | return true; // Done with this instruction. |
| 1082 | } |
| 1083 | } |
| 1084 | } |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1085 | |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1086 | // If there is one more use of regB later in the same MBB, consider |
| 1087 | // re-schedule it before this MI if it's legal. |
| 1088 | if (RescheduleKillAboveMI(mbbi, mi, nmi, regB)) { |
| 1089 | ++NumReSchedUps; |
Lang Hames | f31ceaf | 2012-04-09 20:17:30 +0000 | [diff] [blame] | 1090 | return true; |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1093 | // If this is an instruction with a load folded into it, try unfolding |
| 1094 | // the load, e.g. avoid this: |
| 1095 | // movq %rdx, %rcx |
| 1096 | // addq (%rax), %rcx |
| 1097 | // in favor of this: |
| 1098 | // movq (%rax), %rcx |
| 1099 | // addq %rdx, %rcx |
| 1100 | // because it's preferable to schedule a load than a register copy. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1101 | if (MI.mayLoad() && !regBKilled) { |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1102 | // Determine if a load can be unfolded. |
| 1103 | unsigned LoadRegIndex; |
| 1104 | unsigned NewOpc = |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1105 | TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1106 | /*UnfoldLoad=*/true, |
| 1107 | /*UnfoldStore=*/false, |
| 1108 | &LoadRegIndex); |
| 1109 | if (NewOpc != 0) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1110 | const MCInstrDesc &UnfoldMCID = TII->get(NewOpc); |
| 1111 | if (UnfoldMCID.getNumDefs() == 1) { |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1112 | // Unfold the load. |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1113 | DEBUG(dbgs() << "2addr: UNFOLDING: " << MI); |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1114 | const TargetRegisterClass *RC = |
Andrew Trick | f12f6df | 2012-05-03 01:14:37 +0000 | [diff] [blame] | 1115 | TRI->getAllocatableClass( |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1116 | TII->getRegClass(UnfoldMCID, LoadRegIndex, TRI, *MF)); |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1117 | unsigned Reg = MRI->createVirtualRegister(RC); |
| 1118 | SmallVector<MachineInstr *, 2> NewMIs; |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1119 | if (!TII->unfoldMemoryOperand(*MF, &MI, Reg, |
Evan Cheng | 98ec91e | 2010-07-02 20:36:18 +0000 | [diff] [blame] | 1120 | /*UnfoldLoad=*/true,/*UnfoldStore=*/false, |
| 1121 | NewMIs)) { |
| 1122 | DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n"); |
| 1123 | return false; |
| 1124 | } |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1125 | assert(NewMIs.size() == 2 && |
| 1126 | "Unfolded a load into multiple instructions!"); |
| 1127 | // The load was previously folded, so this is the only use. |
| 1128 | NewMIs[1]->addRegisterKilled(Reg, TRI); |
| 1129 | |
| 1130 | // Tentatively insert the instructions into the block so that they |
| 1131 | // look "normal" to the transformation logic. |
| 1132 | mbbi->insert(mi, NewMIs[0]); |
| 1133 | mbbi->insert(mi, NewMIs[1]); |
| 1134 | |
| 1135 | DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0] |
| 1136 | << "2addr: NEW INST: " << *NewMIs[1]); |
| 1137 | |
| 1138 | // Transform the instruction, now that it no longer has a load. |
| 1139 | unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA); |
| 1140 | unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB); |
| 1141 | MachineBasicBlock::iterator NewMI = NewMIs[1]; |
| 1142 | bool TransformSuccess = |
| 1143 | TryInstructionTransform(NewMI, mi, mbbi, |
Evan Cheng | f06e6c2 | 2011-03-02 01:08:17 +0000 | [diff] [blame] | 1144 | NewSrcIdx, NewDstIdx, Dist, Processed); |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1145 | if (TransformSuccess || |
| 1146 | NewMIs[1]->getOperand(NewSrcIdx).isKill()) { |
| 1147 | // Success, or at least we made an improvement. Keep the unfolded |
| 1148 | // instructions and discard the original. |
| 1149 | if (LV) { |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1150 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 1151 | MachineOperand &MO = MI.getOperand(i); |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 1152 | if (MO.isReg() && |
Dan Gohman | 7aa7bc7 | 2010-06-22 00:32:04 +0000 | [diff] [blame] | 1153 | TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 1154 | if (MO.isUse()) { |
Dan Gohman | cc1ca98 | 2010-06-22 02:07:21 +0000 | [diff] [blame] | 1155 | if (MO.isKill()) { |
| 1156 | if (NewMIs[0]->killsRegister(MO.getReg())) |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1157 | LV->replaceKillInstruction(MO.getReg(), &MI, NewMIs[0]); |
Dan Gohman | cc1ca98 | 2010-06-22 02:07:21 +0000 | [diff] [blame] | 1158 | else { |
| 1159 | assert(NewMIs[1]->killsRegister(MO.getReg()) && |
| 1160 | "Kill missing after load unfold!"); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1161 | LV->replaceKillInstruction(MO.getReg(), &MI, NewMIs[1]); |
Dan Gohman | cc1ca98 | 2010-06-22 02:07:21 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1164 | } else if (LV->removeVirtualRegisterDead(MO.getReg(), &MI)) { |
Dan Gohman | cc1ca98 | 2010-06-22 02:07:21 +0000 | [diff] [blame] | 1165 | if (NewMIs[1]->registerDefIsDead(MO.getReg())) |
| 1166 | LV->addVirtualRegisterDead(MO.getReg(), NewMIs[1]); |
| 1167 | else { |
| 1168 | assert(NewMIs[0]->registerDefIsDead(MO.getReg()) && |
| 1169 | "Dead flag missing after load unfold!"); |
| 1170 | LV->addVirtualRegisterDead(MO.getReg(), NewMIs[0]); |
| 1171 | } |
| 1172 | } |
Dan Gohman | 7aa7bc7 | 2010-06-22 00:32:04 +0000 | [diff] [blame] | 1173 | } |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1174 | } |
| 1175 | LV->addVirtualRegisterKilled(Reg, NewMIs[1]); |
| 1176 | } |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1177 | MI.eraseFromParent(); |
Dan Gohman | 584fedf | 2010-06-21 22:17:20 +0000 | [diff] [blame] | 1178 | mi = NewMIs[1]; |
| 1179 | if (TransformSuccess) |
| 1180 | return true; |
| 1181 | } else { |
| 1182 | // Transforming didn't eliminate the tie and didn't lead to an |
| 1183 | // improvement. Clean up the unfolded instructions and keep the |
| 1184 | // original. |
| 1185 | DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n"); |
| 1186 | NewMIs[0]->eraseFromParent(); |
| 1187 | NewMIs[1]->eraseFromParent(); |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1193 | return false; |
| 1194 | } |
| 1195 | |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1196 | // Collect tied operands of MI that need to be handled. |
| 1197 | // Rewrite trivial cases immediately. |
| 1198 | // Return true if any tied operands where found, including the trivial ones. |
| 1199 | bool TwoAddressInstructionPass:: |
| 1200 | collectTiedOperands(MachineInstr *MI, TiedOperandMap &TiedOperands) { |
| 1201 | const MCInstrDesc &MCID = MI->getDesc(); |
| 1202 | bool AnyOps = false; |
| 1203 | unsigned NumOps = MI->isInlineAsm() ? |
| 1204 | MI->getNumOperands() : MCID.getNumOperands(); |
| 1205 | |
| 1206 | for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) { |
| 1207 | unsigned DstIdx = 0; |
| 1208 | if (!MI->isRegTiedToDefOperand(SrcIdx, &DstIdx)) |
| 1209 | continue; |
| 1210 | AnyOps = true; |
| 1211 | |
| 1212 | assert(MI->getOperand(SrcIdx).isReg() && |
| 1213 | MI->getOperand(SrcIdx).getReg() && |
| 1214 | MI->getOperand(SrcIdx).isUse() && |
| 1215 | "two address instruction invalid"); |
| 1216 | |
| 1217 | unsigned RegB = MI->getOperand(SrcIdx).getReg(); |
| 1218 | |
| 1219 | // Deal with <undef> uses immediately - simply rewrite the src operand. |
| 1220 | if (MI->getOperand(SrcIdx).isUndef()) { |
| 1221 | unsigned DstReg = MI->getOperand(DstIdx).getReg(); |
| 1222 | // Constrain the DstReg register class if required. |
| 1223 | if (TargetRegisterInfo::isVirtualRegister(DstReg)) |
| 1224 | if (const TargetRegisterClass *RC = TII->getRegClass(MCID, SrcIdx, |
| 1225 | TRI, *MF)) |
| 1226 | MRI->constrainRegClass(DstReg, RC); |
| 1227 | MI->getOperand(SrcIdx).setReg(DstReg); |
| 1228 | DEBUG(dbgs() << "\t\trewrite undef:\t" << *MI); |
| 1229 | continue; |
| 1230 | } |
| 1231 | TiedOperands[RegB].push_back(std::make_pair(SrcIdx, DstIdx)); |
| 1232 | } |
| 1233 | return AnyOps; |
| 1234 | } |
| 1235 | |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 1236 | /// runOnMachineFunction - Reduce two-address instructions to two operands. |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1237 | /// |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1238 | bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) { |
| 1239 | MF = &Func; |
| 1240 | const TargetMachine &TM = MF->getTarget(); |
| 1241 | MRI = &MF->getRegInfo(); |
Evan Cheng | 875357d | 2008-03-13 06:37:55 +0000 | [diff] [blame] | 1242 | TII = TM.getInstrInfo(); |
| 1243 | TRI = TM.getRegisterInfo(); |
Evan Cheng | 2a4410d | 2011-11-14 19:48:55 +0000 | [diff] [blame] | 1244 | InstrItins = TM.getInstrItineraryData(); |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 1245 | Indexes = getAnalysisIfAvailable<SlotIndexes>(); |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 1246 | LV = getAnalysisIfAvailable<LiveVariables>(); |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 1247 | LIS = getAnalysisIfAvailable<LiveIntervals>(); |
Dan Gohman | a70dca1 | 2009-10-09 23:27:56 +0000 | [diff] [blame] | 1248 | AA = &getAnalysis<AliasAnalysis>(); |
Evan Cheng | c3aa7c5 | 2011-11-16 18:44:48 +0000 | [diff] [blame] | 1249 | OptLevel = TM.getOptLevel(); |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1250 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1251 | bool MadeChange = false; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1252 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 1253 | DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n"); |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 1254 | DEBUG(dbgs() << "********** Function: " |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1255 | << MF->getFunction()->getName() << '\n'); |
Alkis Evlogimenos | 3a9986f | 2004-02-18 00:35:06 +0000 | [diff] [blame] | 1256 | |
Jakob Stoklund Olesen | 73e7dce | 2011-07-29 22:51:22 +0000 | [diff] [blame] | 1257 | // This pass takes the function out of SSA form. |
| 1258 | MRI->leaveSSA(); |
| 1259 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 1260 | // ReMatRegs - Keep track of the registers whose def's are remat'ed. |
Jakob Stoklund Olesen | 00f93fc | 2011-01-09 03:45:44 +0000 | [diff] [blame] | 1261 | BitVector ReMatRegs(MRI->getNumVirtRegs()); |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 1262 | |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1263 | TiedOperandMap TiedOperands; |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1264 | |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 1265 | SmallPtrSet<MachineInstr*, 8> Processed; |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1266 | for (MachineFunction::iterator mbbi = MF->begin(), mbbe = MF->end(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1267 | mbbi != mbbe; ++mbbi) { |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 1268 | unsigned Dist = 0; |
| 1269 | DistanceMap.clear(); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 1270 | SrcRegMap.clear(); |
| 1271 | DstRegMap.clear(); |
| 1272 | Processed.clear(); |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1273 | for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end(); |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 1274 | mi != me; ) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1275 | MachineBasicBlock::iterator nmi = llvm::next(mi); |
Dale Johannesen | b8ff934 | 2010-02-10 21:47:48 +0000 | [diff] [blame] | 1276 | if (mi->isDebugValue()) { |
| 1277 | mi = nmi; |
| 1278 | continue; |
| 1279 | } |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 1280 | |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1281 | // Remember REG_SEQUENCE instructions, we'll deal with them later. |
| 1282 | if (mi->isRegSequence()) |
| 1283 | RegSequences.push_back(&*mi); |
| 1284 | |
Evan Cheng | 7543e58 | 2008-06-18 07:49:14 +0000 | [diff] [blame] | 1285 | DistanceMap.insert(std::make_pair(mi, ++Dist)); |
Evan Cheng | 870b807 | 2009-03-01 02:03:43 +0000 | [diff] [blame] | 1286 | |
| 1287 | ProcessCopy(&*mi, &*mbbi, Processed); |
| 1288 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1289 | // First scan through all the tied register uses in this instruction |
| 1290 | // and record a list of pairs of tied operands for each register. |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1291 | if (!collectTiedOperands(mi, TiedOperands)) { |
| 1292 | mi = nmi; |
| 1293 | continue; |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1294 | } |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1295 | |
Jakob Stoklund Olesen | 6ac8066 | 2012-08-03 23:25:45 +0000 | [diff] [blame^] | 1296 | ++NumTwoAddressInstrs; |
| 1297 | DEBUG(dbgs() << '\t' << *mi); |
| 1298 | |
Chandler Carruth | 32d75be | 2012-07-18 18:58:22 +0000 | [diff] [blame] | 1299 | // If the instruction has a single pair of tied operands, try some |
| 1300 | // transformations that may either eliminate the tied operands or |
| 1301 | // improve the opportunities for coalescing away the register copy. |
| 1302 | if (TiedOperands.size() == 1) { |
| 1303 | SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs |
| 1304 | = TiedOperands.begin()->second; |
| 1305 | if (TiedPairs.size() == 1) { |
| 1306 | unsigned SrcIdx = TiedPairs[0].first; |
| 1307 | unsigned DstIdx = TiedPairs[0].second; |
| 1308 | unsigned SrcReg = mi->getOperand(SrcIdx).getReg(); |
| 1309 | unsigned DstReg = mi->getOperand(DstIdx).getReg(); |
| 1310 | if (SrcReg != DstReg && |
| 1311 | TryInstructionTransform(mi, nmi, mbbi, SrcIdx, DstIdx, Dist, |
| 1312 | Processed)) { |
| 1313 | // The tied operands have been eliminated or shifted further down the |
| 1314 | // block to ease elimination. Continue processing with 'nmi'. |
| 1315 | TiedOperands.clear(); |
| 1316 | mi = nmi; |
| 1317 | continue; |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1322 | // Now iterate over the information collected above. |
| 1323 | for (TiedOperandMap::iterator OI = TiedOperands.begin(), |
| 1324 | OE = TiedOperands.end(); OI != OE; ++OI) { |
| 1325 | SmallVector<std::pair<unsigned, unsigned>, 4> &TiedPairs = OI->second; |
Evan Cheng | 360c2dd | 2006-11-01 23:06:55 +0000 | [diff] [blame] | 1326 | |
Cameron Zwarich | aaa5f14 | 2011-06-07 23:54:00 +0000 | [diff] [blame] | 1327 | bool IsEarlyClobber = false; |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1328 | bool RemovedKillFlag = false; |
| 1329 | bool AllUsesCopied = true; |
| 1330 | unsigned LastCopiedReg = 0; |
| 1331 | unsigned regB = OI->first; |
| 1332 | for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) { |
| 1333 | unsigned SrcIdx = TiedPairs[tpi].first; |
| 1334 | unsigned DstIdx = TiedPairs[tpi].second; |
Cameron Zwarich | aaa5f14 | 2011-06-07 23:54:00 +0000 | [diff] [blame] | 1335 | |
| 1336 | const MachineOperand &DstMO = mi->getOperand(DstIdx); |
| 1337 | unsigned regA = DstMO.getReg(); |
| 1338 | IsEarlyClobber |= DstMO.isEarlyClobber(); |
| 1339 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1340 | // Grab regB from the instruction because it may have changed if the |
| 1341 | // instruction was commuted. |
| 1342 | regB = mi->getOperand(SrcIdx).getReg(); |
| 1343 | |
| 1344 | if (regA == regB) { |
| 1345 | // The register is tied to multiple destinations (or else we would |
| 1346 | // not have continued this far), but this use of the register |
| 1347 | // already matches the tied destination. Leave it. |
| 1348 | AllUsesCopied = false; |
| 1349 | continue; |
| 1350 | } |
| 1351 | LastCopiedReg = regA; |
| 1352 | |
| 1353 | assert(TargetRegisterInfo::isVirtualRegister(regB) && |
| 1354 | "cannot make instruction into two-address form"); |
Chris Lattner | 6b50767 | 2004-01-31 21:21:43 +0000 | [diff] [blame] | 1355 | |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 1356 | #ifndef NDEBUG |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1357 | // First, verify that we don't have a use of "a" in the instruction |
| 1358 | // (a = b + a for example) because our transformation will not |
| 1359 | // work. This should never occur because we are in SSA form. |
| 1360 | for (unsigned i = 0; i != mi->getNumOperands(); ++i) |
| 1361 | assert(i == DstIdx || |
| 1362 | !mi->getOperand(i).isReg() || |
| 1363 | mi->getOperand(i).getReg() != regA); |
Chris Lattner | 1e31363 | 2004-07-21 23:17:57 +0000 | [diff] [blame] | 1364 | #endif |
Alkis Evlogimenos | 14be640 | 2004-02-04 22:17:40 +0000 | [diff] [blame] | 1365 | |
Manman Ren | d68e8cd | 2012-07-25 18:28:13 +0000 | [diff] [blame] | 1366 | // Emit a copy. |
| 1367 | BuildMI(*mbbi, mi, mi->getDebugLoc(), TII->get(TargetOpcode::COPY), |
| 1368 | regA).addReg(regB); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1369 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1370 | // Update DistanceMap. |
Evan Cheng | ad75364 | 2012-05-18 01:33:51 +0000 | [diff] [blame] | 1371 | MachineBasicBlock::iterator prevMI = prior(mi); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1372 | DistanceMap.insert(std::make_pair(prevMI, Dist)); |
| 1373 | DistanceMap[mi] = ++Dist; |
| 1374 | |
Jakob Stoklund Olesen | 5bfdedf | 2012-08-03 22:58:34 +0000 | [diff] [blame] | 1375 | SlotIndex CopyIdx; |
| 1376 | if (Indexes) |
| 1377 | CopyIdx = Indexes->insertMachineInstrInMaps(prevMI).getRegSlot(); |
| 1378 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 1379 | DEBUG(dbgs() << "\t\tprepend:\t" << *prevMI); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1380 | |
| 1381 | MachineOperand &MO = mi->getOperand(SrcIdx); |
| 1382 | assert(MO.isReg() && MO.getReg() == regB && MO.isUse() && |
| 1383 | "inconsistent operand info for 2-reg pass"); |
| 1384 | if (MO.isKill()) { |
| 1385 | MO.setIsKill(false); |
| 1386 | RemovedKillFlag = true; |
| 1387 | } |
Jakob Stoklund Olesen | 8e86929 | 2012-05-20 06:38:32 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Make sure regA is a legal regclass for the SrcIdx operand. |
| 1390 | if (TargetRegisterInfo::isVirtualRegister(regA) && |
| 1391 | TargetRegisterInfo::isVirtualRegister(regB)) |
| 1392 | MRI->constrainRegClass(regA, MRI->getRegClass(regB)); |
| 1393 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1394 | MO.setReg(regA); |
Evan Cheng | ad75364 | 2012-05-18 01:33:51 +0000 | [diff] [blame] | 1395 | |
Manman Ren | d68e8cd | 2012-07-25 18:28:13 +0000 | [diff] [blame] | 1396 | // Propagate SrcRegMap. |
| 1397 | SrcRegMap[regA] = regB; |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | if (AllUsesCopied) { |
Cameron Zwarich | aaa5f14 | 2011-06-07 23:54:00 +0000 | [diff] [blame] | 1401 | if (!IsEarlyClobber) { |
| 1402 | // Replace other (un-tied) uses of regB with LastCopiedReg. |
| 1403 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 1404 | MachineOperand &MO = mi->getOperand(i); |
| 1405 | if (MO.isReg() && MO.getReg() == regB && MO.isUse()) { |
| 1406 | if (MO.isKill()) { |
| 1407 | MO.setIsKill(false); |
| 1408 | RemovedKillFlag = true; |
| 1409 | } |
| 1410 | MO.setReg(LastCopiedReg); |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1411 | } |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | // Update live variables for regB. |
| 1416 | if (RemovedKillFlag && LV && LV->getVarInfo(regB).removeKill(mi)) |
| 1417 | LV->addVirtualRegisterKilled(regB, prior(mi)); |
| 1418 | |
| 1419 | } else if (RemovedKillFlag) { |
| 1420 | // Some tied uses of regB matched their destination registers, so |
| 1421 | // regB is still used in this instruction, but a kill flag was |
| 1422 | // removed from a different tied use of regB, so now we need to add |
| 1423 | // a kill flag to one of the remaining uses of regB. |
| 1424 | for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) { |
| 1425 | MachineOperand &MO = mi->getOperand(i); |
| 1426 | if (MO.isReg() && MO.getReg() == regB && MO.isUse()) { |
| 1427 | MO.setIsKill(true); |
| 1428 | break; |
Bob Wilson | 71124f6 | 2009-09-01 04:18:40 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
Bob Wilson | 4344979 | 2009-08-31 21:54:55 +0000 | [diff] [blame] | 1431 | } |
Evan Cheng | 68fc2da | 2010-06-09 19:26:01 +0000 | [diff] [blame] | 1432 | |
Chandler Carruth | 32d75be | 2012-07-18 18:58:22 +0000 | [diff] [blame] | 1433 | // We didn't change anything if there was a single tied pair, and that |
| 1434 | // pair didn't require copies. |
| 1435 | if (AllUsesCopied || TiedPairs.size() > 1) { |
| 1436 | MadeChange = true; |
Evan Cheng | 68fc2da | 2010-06-09 19:26:01 +0000 | [diff] [blame] | 1437 | |
Chandler Carruth | 32d75be | 2012-07-18 18:58:22 +0000 | [diff] [blame] | 1438 | // Schedule the source copy / remat inserted to form two-address |
| 1439 | // instruction. FIXME: Does it matter the distance map may not be |
| 1440 | // accurate after it's scheduled? |
| 1441 | TII->scheduleTwoAddrSource(prior(mi), mi, *TRI); |
| 1442 | } |
Bob Wilson | 4344979 | 2009-08-31 21:54:55 +0000 | [diff] [blame] | 1443 | |
David Greene | eb00b18 | 2010-01-05 01:24:21 +0000 | [diff] [blame] | 1444 | DEBUG(dbgs() << "\t\trewrite to:\t" << *mi); |
Jakob Stoklund Olesen | 351c881 | 2012-06-25 03:27:12 +0000 | [diff] [blame] | 1445 | } |
Bill Wendling | 637980e | 2008-05-10 00:12:52 +0000 | [diff] [blame] | 1446 | |
Jakob Stoklund Olesen | 351c881 | 2012-06-25 03:27:12 +0000 | [diff] [blame] | 1447 | // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form. |
| 1448 | if (mi->isInsertSubreg()) { |
| 1449 | // From %reg = INSERT_SUBREG %reg, %subreg, subidx |
| 1450 | // To %reg:subidx = COPY %subreg |
| 1451 | unsigned SubIdx = mi->getOperand(3).getImm(); |
| 1452 | mi->RemoveOperand(3); |
| 1453 | assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx"); |
| 1454 | mi->getOperand(0).setSubReg(SubIdx); |
| 1455 | mi->getOperand(0).setIsUndef(mi->getOperand(1).isUndef()); |
| 1456 | mi->RemoveOperand(1); |
| 1457 | mi->setDesc(TII->get(TargetOpcode::COPY)); |
| 1458 | DEBUG(dbgs() << "\t\tconvert to:\t" << *mi); |
Jakob Stoklund Olesen | ed2185e | 2010-07-06 23:26:25 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Bob Wilson | cc80df9 | 2009-09-03 20:58:42 +0000 | [diff] [blame] | 1461 | // Clear TiedOperands here instead of at the top of the loop |
| 1462 | // since most instructions do not have tied operands. |
| 1463 | TiedOperands.clear(); |
Evan Cheng | 7a963fa | 2008-03-27 01:27:25 +0000 | [diff] [blame] | 1464 | mi = nmi; |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 1468 | // Some remat'ed instructions are dead. |
Jakob Stoklund Olesen | 00f93fc | 2011-01-09 03:45:44 +0000 | [diff] [blame] | 1469 | for (int i = ReMatRegs.find_first(); i != -1; i = ReMatRegs.find_next(i)) { |
| 1470 | unsigned VReg = TargetRegisterInfo::index2VirtReg(i); |
Evan Cheng | f1250ee | 2010-03-23 20:36:12 +0000 | [diff] [blame] | 1471 | if (MRI->use_nodbg_empty(VReg)) { |
Evan Cheng | 601ca4b | 2008-06-25 01:16:38 +0000 | [diff] [blame] | 1472 | MachineInstr *DefMI = MRI->getVRegDef(VReg); |
| 1473 | DefMI->eraseFromParent(); |
Bill Wendling | a16157a | 2008-05-26 05:49:49 +0000 | [diff] [blame] | 1474 | } |
Bill Wendling | 48f7f23 | 2008-05-26 05:18:34 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1477 | // Eliminate REG_SEQUENCE instructions. Their whole purpose was to preseve |
| 1478 | // SSA form. It's now safe to de-SSA. |
| 1479 | MadeChange |= EliminateRegSequences(); |
| 1480 | |
Misha Brukman | 75fa4e4 | 2004-07-22 15:26:23 +0000 | [diff] [blame] | 1481 | return MadeChange; |
Alkis Evlogimenos | 71499de | 2003-12-18 13:06:04 +0000 | [diff] [blame] | 1482 | } |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1483 | |
| 1484 | static void UpdateRegSequenceSrcs(unsigned SrcReg, |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1485 | unsigned DstReg, unsigned SubIdx, |
Jakob Stoklund Olesen | 5a0d4fc | 2010-05-29 00:14:14 +0000 | [diff] [blame] | 1486 | MachineRegisterInfo *MRI, |
| 1487 | const TargetRegisterInfo &TRI) { |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1488 | for (MachineRegisterInfo::reg_iterator RI = MRI->reg_begin(SrcReg), |
Evan Cheng | 3ae56bc | 2010-05-12 01:27:49 +0000 | [diff] [blame] | 1489 | RE = MRI->reg_end(); RI != RE; ) { |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1490 | MachineOperand &MO = RI.getOperand(); |
| 1491 | ++RI; |
Jakob Stoklund Olesen | 5a0d4fc | 2010-05-29 00:14:14 +0000 | [diff] [blame] | 1492 | MO.substVirtReg(DstReg, SubIdx, TRI); |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
Jakob Stoklund Olesen | d36f5af | 2012-01-24 23:28:42 +0000 | [diff] [blame] | 1496 | // Find the first def of Reg, assuming they are all in the same basic block. |
| 1497 | static MachineInstr *findFirstDef(unsigned Reg, MachineRegisterInfo *MRI) { |
| 1498 | SmallPtrSet<MachineInstr*, 8> Defs; |
| 1499 | MachineInstr *First = 0; |
| 1500 | for (MachineRegisterInfo::def_iterator RI = MRI->def_begin(Reg); |
| 1501 | MachineInstr *MI = RI.skipInstruction(); Defs.insert(MI)) |
| 1502 | First = MI; |
| 1503 | if (!First) |
| 1504 | return 0; |
| 1505 | |
| 1506 | MachineBasicBlock *MBB = First->getParent(); |
| 1507 | MachineBasicBlock::iterator A = First, B = First; |
| 1508 | bool Moving; |
| 1509 | do { |
| 1510 | Moving = false; |
| 1511 | if (A != MBB->begin()) { |
| 1512 | Moving = true; |
| 1513 | --A; |
| 1514 | if (Defs.erase(A)) First = A; |
| 1515 | } |
| 1516 | if (B != MBB->end()) { |
| 1517 | Defs.erase(B); |
| 1518 | ++B; |
| 1519 | Moving = true; |
| 1520 | } |
| 1521 | } while (Moving && !Defs.empty()); |
| 1522 | assert(Defs.empty() && "Instructions outside basic block!"); |
| 1523 | return First; |
| 1524 | } |
| 1525 | |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1526 | /// CoalesceExtSubRegs - If a number of sources of the REG_SEQUENCE are |
| 1527 | /// EXTRACT_SUBREG from the same register and to the same virtual register |
| 1528 | /// with different sub-register indices, attempt to combine the |
| 1529 | /// EXTRACT_SUBREGs and pre-coalesce them. e.g. |
| 1530 | /// %reg1026<def> = VLDMQ %reg1025<kill>, 260, pred:14, pred:%reg0 |
| 1531 | /// %reg1029:6<def> = EXTRACT_SUBREG %reg1026, 6 |
| 1532 | /// %reg1029:5<def> = EXTRACT_SUBREG %reg1026<kill>, 5 |
| 1533 | /// Since D subregs 5, 6 can combine to a Q register, we can coalesce |
| 1534 | /// reg1026 to reg1029. |
| 1535 | void |
| 1536 | TwoAddressInstructionPass::CoalesceExtSubRegs(SmallVector<unsigned,4> &Srcs, |
| 1537 | unsigned DstReg) { |
| 1538 | SmallSet<unsigned, 4> Seen; |
| 1539 | for (unsigned i = 0, e = Srcs.size(); i != e; ++i) { |
| 1540 | unsigned SrcReg = Srcs[i]; |
| 1541 | if (!Seen.insert(SrcReg)) |
| 1542 | continue; |
| 1543 | |
Bob Wilson | 26bf8f9 | 2010-06-03 23:53:58 +0000 | [diff] [blame] | 1544 | // Check that the instructions are all in the same basic block. |
Manman Ren | 5f917cd | 2012-07-02 18:55:36 +0000 | [diff] [blame] | 1545 | MachineInstr *SrcDefMI = MRI->getUniqueVRegDef(SrcReg); |
| 1546 | MachineInstr *DstDefMI = MRI->getUniqueVRegDef(DstReg); |
| 1547 | if (!SrcDefMI || !DstDefMI || |
| 1548 | SrcDefMI->getParent() != DstDefMI->getParent()) |
Bob Wilson | 26bf8f9 | 2010-06-03 23:53:58 +0000 | [diff] [blame] | 1549 | continue; |
| 1550 | |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1551 | // If there are no other uses than copies which feed into |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1552 | // the reg_sequence, then we might be able to coalesce them. |
| 1553 | bool CanCoalesce = true; |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1554 | SmallVector<unsigned, 4> SrcSubIndices, DstSubIndices; |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1555 | for (MachineRegisterInfo::use_nodbg_iterator |
| 1556 | UI = MRI->use_nodbg_begin(SrcReg), |
| 1557 | UE = MRI->use_nodbg_end(); UI != UE; ++UI) { |
| 1558 | MachineInstr *UseMI = &*UI; |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1559 | if (!UseMI->isCopy() || UseMI->getOperand(0).getReg() != DstReg) { |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1560 | CanCoalesce = false; |
| 1561 | break; |
| 1562 | } |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1563 | SrcSubIndices.push_back(UseMI->getOperand(1).getSubReg()); |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1564 | DstSubIndices.push_back(UseMI->getOperand(0).getSubReg()); |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1567 | if (!CanCoalesce || SrcSubIndices.size() < 2) |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1568 | continue; |
| 1569 | |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1570 | // Check that the source subregisters can be combined. |
| 1571 | std::sort(SrcSubIndices.begin(), SrcSubIndices.end()); |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1572 | unsigned NewSrcSubIdx = 0; |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1573 | if (!TRI->canCombineSubRegIndices(MRI->getRegClass(SrcReg), SrcSubIndices, |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1574 | NewSrcSubIdx)) |
Bob Wilson | 26bf8f9 | 2010-06-03 23:53:58 +0000 | [diff] [blame] | 1575 | continue; |
| 1576 | |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1577 | // Check that the destination subregisters can also be combined. |
| 1578 | std::sort(DstSubIndices.begin(), DstSubIndices.end()); |
| 1579 | unsigned NewDstSubIdx = 0; |
| 1580 | if (!TRI->canCombineSubRegIndices(MRI->getRegClass(DstReg), DstSubIndices, |
| 1581 | NewDstSubIdx)) |
| 1582 | continue; |
| 1583 | |
| 1584 | // If neither source nor destination can be combined to the full register, |
| 1585 | // just give up. This could be improved if it ever matters. |
| 1586 | if (NewSrcSubIdx != 0 && NewDstSubIdx != 0) |
| 1587 | continue; |
| 1588 | |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1589 | // Now that we know that all the uses are extract_subregs and that those |
| 1590 | // subregs can somehow be combined, scan all the extract_subregs again to |
| 1591 | // make sure the subregs are in the right order and can be composed. |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1592 | MachineInstr *SomeMI = 0; |
| 1593 | CanCoalesce = true; |
| 1594 | for (MachineRegisterInfo::use_nodbg_iterator |
| 1595 | UI = MRI->use_nodbg_begin(SrcReg), |
| 1596 | UE = MRI->use_nodbg_end(); UI != UE; ++UI) { |
| 1597 | MachineInstr *UseMI = &*UI; |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1598 | assert(UseMI->isCopy()); |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1599 | unsigned DstSubIdx = UseMI->getOperand(0).getSubReg(); |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1600 | unsigned SrcSubIdx = UseMI->getOperand(1).getSubReg(); |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1601 | assert(DstSubIdx != 0 && "missing subreg from RegSequence elimination"); |
Bob Wilson | 4ffd22d | 2010-06-15 17:27:54 +0000 | [diff] [blame] | 1602 | if ((NewDstSubIdx == 0 && |
| 1603 | TRI->composeSubRegIndices(NewSrcSubIdx, DstSubIdx) != SrcSubIdx) || |
| 1604 | (NewSrcSubIdx == 0 && |
| 1605 | TRI->composeSubRegIndices(NewDstSubIdx, SrcSubIdx) != DstSubIdx)) { |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1606 | CanCoalesce = false; |
| 1607 | break; |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1608 | } |
Jakob Stoklund Olesen | defe12d | 2012-01-24 04:44:01 +0000 | [diff] [blame] | 1609 | // Keep track of one of the uses. Preferably the first one which has a |
| 1610 | // <def,undef> flag. |
| 1611 | if (!SomeMI || UseMI->getOperand(0).isUndef()) |
| 1612 | SomeMI = UseMI; |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1613 | } |
| 1614 | if (!CanCoalesce) |
| 1615 | continue; |
| 1616 | |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1617 | // Insert a copy to replace the original. |
Jakob Stoklund Olesen | 5c00e07 | 2010-07-08 16:40:15 +0000 | [diff] [blame] | 1618 | MachineInstr *CopyMI = BuildMI(*SomeMI->getParent(), SomeMI, |
| 1619 | SomeMI->getDebugLoc(), |
| 1620 | TII->get(TargetOpcode::COPY)) |
Jakob Stoklund Olesen | defe12d | 2012-01-24 04:44:01 +0000 | [diff] [blame] | 1621 | .addReg(DstReg, RegState::Define | |
| 1622 | getUndefRegState(SomeMI->getOperand(0).isUndef()), |
| 1623 | NewDstSubIdx) |
Jakob Stoklund Olesen | 5c00e07 | 2010-07-08 16:40:15 +0000 | [diff] [blame] | 1624 | .addReg(SrcReg, 0, NewSrcSubIdx); |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1625 | |
| 1626 | // Remove all the old extract instructions. |
| 1627 | for (MachineRegisterInfo::use_nodbg_iterator |
| 1628 | UI = MRI->use_nodbg_begin(SrcReg), |
| 1629 | UE = MRI->use_nodbg_end(); UI != UE; ) { |
| 1630 | MachineInstr *UseMI = &*UI; |
| 1631 | ++UI; |
| 1632 | if (UseMI == CopyMI) |
| 1633 | continue; |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1634 | assert(UseMI->isCopy()); |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1635 | // Move any kills to the new copy or extract instruction. |
| 1636 | if (UseMI->getOperand(1).isKill()) { |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1637 | CopyMI->getOperand(1).setIsKill(); |
Bob Wilson | 852a7e3 | 2010-06-15 05:56:31 +0000 | [diff] [blame] | 1638 | if (LV) |
| 1639 | // Update live variables |
| 1640 | LV->replaceKillInstruction(SrcReg, UseMI, &*CopyMI); |
| 1641 | } |
| 1642 | UseMI->eraseFromParent(); |
| 1643 | } |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
Evan Cheng | c6dcce3 | 2010-05-17 23:24:12 +0000 | [diff] [blame] | 1647 | static bool HasOtherRegSequenceUses(unsigned Reg, MachineInstr *RegSeq, |
| 1648 | MachineRegisterInfo *MRI) { |
| 1649 | for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg), |
| 1650 | UE = MRI->use_end(); UI != UE; ++UI) { |
| 1651 | MachineInstr *UseMI = &*UI; |
| 1652 | if (UseMI != RegSeq && UseMI->isRegSequence()) |
| 1653 | return true; |
| 1654 | } |
| 1655 | return false; |
| 1656 | } |
| 1657 | |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1658 | /// EliminateRegSequences - Eliminate REG_SEQUENCE instructions as part |
| 1659 | /// of the de-ssa process. This replaces sources of REG_SEQUENCE as |
| 1660 | /// sub-register references of the register defined by REG_SEQUENCE. e.g. |
| 1661 | /// |
| 1662 | /// %reg1029<def>, %reg1030<def> = VLD1q16 %reg1024<kill>, ... |
| 1663 | /// %reg1031<def> = REG_SEQUENCE %reg1029<kill>, 5, %reg1030<kill>, 6 |
| 1664 | /// => |
| 1665 | /// %reg1031:5<def>, %reg1031:6<def> = VLD1q16 %reg1024<kill>, ... |
| 1666 | bool TwoAddressInstructionPass::EliminateRegSequences() { |
| 1667 | if (RegSequences.empty()) |
| 1668 | return false; |
| 1669 | |
| 1670 | for (unsigned i = 0, e = RegSequences.size(); i != e; ++i) { |
| 1671 | MachineInstr *MI = RegSequences[i]; |
| 1672 | unsigned DstReg = MI->getOperand(0).getReg(); |
| 1673 | if (MI->getOperand(0).getSubReg() || |
| 1674 | TargetRegisterInfo::isPhysicalRegister(DstReg) || |
| 1675 | !(MI->getNumOperands() & 1)) { |
| 1676 | DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << *MI); |
| 1677 | llvm_unreachable(0); |
| 1678 | } |
Evan Cheng | 0bcccac | 2010-05-11 00:04:31 +0000 | [diff] [blame] | 1679 | |
Evan Cheng | 44bfdd3 | 2010-05-17 22:09:49 +0000 | [diff] [blame] | 1680 | bool IsImpDef = true; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 1681 | SmallVector<unsigned, 4> RealSrcs; |
Evan Cheng | 0bcccac | 2010-05-11 00:04:31 +0000 | [diff] [blame] | 1682 | SmallSet<unsigned, 4> Seen; |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1683 | for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) { |
Jakob Stoklund Olesen | 351c881 | 2012-06-25 03:27:12 +0000 | [diff] [blame] | 1684 | // Nothing needs to be inserted for <undef> operands. |
| 1685 | if (MI->getOperand(i).isUndef()) { |
| 1686 | MI->getOperand(i).setReg(0); |
| 1687 | continue; |
| 1688 | } |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1689 | unsigned SrcReg = MI->getOperand(i).getReg(); |
Pete Cooper | ef74ca6 | 2012-04-04 21:03:25 +0000 | [diff] [blame] | 1690 | unsigned SrcSubIdx = MI->getOperand(i).getSubReg(); |
Bob Wilson | 495de3b | 2010-12-17 01:21:12 +0000 | [diff] [blame] | 1691 | unsigned SubIdx = MI->getOperand(i+1).getImm(); |
Pete Cooper | cd7f02b | 2012-01-18 04:16:16 +0000 | [diff] [blame] | 1692 | // DefMI of NULL means the value does not have a vreg in this block |
| 1693 | // i.e., its a physical register or a subreg. |
| 1694 | // In either case we force a copy to be generated. |
| 1695 | MachineInstr *DefMI = NULL; |
| 1696 | if (!MI->getOperand(i).getSubReg() && |
| 1697 | !TargetRegisterInfo::isPhysicalRegister(SrcReg)) { |
Manman Ren | 5f917cd | 2012-07-02 18:55:36 +0000 | [diff] [blame] | 1698 | DefMI = MRI->getUniqueVRegDef(SrcReg); |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1699 | } |
Evan Cheng | 0bcccac | 2010-05-11 00:04:31 +0000 | [diff] [blame] | 1700 | |
Pete Cooper | cd7f02b | 2012-01-18 04:16:16 +0000 | [diff] [blame] | 1701 | if (DefMI && DefMI->isImplicitDef()) { |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 1702 | DefMI->eraseFromParent(); |
| 1703 | continue; |
| 1704 | } |
Evan Cheng | 44bfdd3 | 2010-05-17 22:09:49 +0000 | [diff] [blame] | 1705 | IsImpDef = false; |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 1706 | |
Jakob Stoklund Olesen | 0bc25f4 | 2010-07-08 16:40:22 +0000 | [diff] [blame] | 1707 | // Remember COPY sources. These might be candidate for coalescing. |
Pete Cooper | cd7f02b | 2012-01-18 04:16:16 +0000 | [diff] [blame] | 1708 | if (DefMI && DefMI->isCopy() && DefMI->getOperand(1).getSubReg()) |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 1709 | RealSrcs.push_back(DefMI->getOperand(1).getReg()); |
| 1710 | |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 1711 | bool isKill = MI->getOperand(i).isKill(); |
Pete Cooper | cd7f02b | 2012-01-18 04:16:16 +0000 | [diff] [blame] | 1712 | if (!DefMI || !Seen.insert(SrcReg) || |
| 1713 | MI->getParent() != DefMI->getParent() || |
Bob Wilson | 495de3b | 2010-12-17 01:21:12 +0000 | [diff] [blame] | 1714 | !isKill || HasOtherRegSequenceUses(SrcReg, MI, MRI) || |
| 1715 | !TRI->getMatchingSuperRegClass(MRI->getRegClass(DstReg), |
| 1716 | MRI->getRegClass(SrcReg), SubIdx)) { |
Evan Cheng | 054dbb8 | 2010-05-13 00:00:35 +0000 | [diff] [blame] | 1717 | // REG_SEQUENCE cannot have duplicated operands, add a copy. |
Jakob Stoklund Olesen | 3437352 | 2010-05-19 20:08:00 +0000 | [diff] [blame] | 1718 | // Also add an copy if the source is live-in the block. We don't want |
Evan Cheng | 054dbb8 | 2010-05-13 00:00:35 +0000 | [diff] [blame] | 1719 | // to end up with a partial-redef of a livein, e.g. |
| 1720 | // BB0: |
| 1721 | // reg1051:10<def> = |
| 1722 | // ... |
| 1723 | // BB1: |
| 1724 | // ... = reg1051:10 |
| 1725 | // BB2: |
| 1726 | // reg1051:9<def> = |
| 1727 | // LiveIntervalAnalysis won't like it. |
Jakob Stoklund Olesen | 3437352 | 2010-05-19 20:08:00 +0000 | [diff] [blame] | 1728 | // |
| 1729 | // If the REG_SEQUENCE doesn't kill its source, keeping live variables |
| 1730 | // correctly up to date becomes very difficult. Insert a copy. |
Jakob Stoklund Olesen | e4b9c4f | 2010-08-09 20:19:16 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Defer any kill flag to the last operand using SrcReg. Otherwise, we |
| 1733 | // might insert a COPY that uses SrcReg after is was killed. |
| 1734 | if (isKill) |
| 1735 | for (unsigned j = i + 2; j < e; j += 2) |
| 1736 | if (MI->getOperand(j).getReg() == SrcReg) { |
| 1737 | MI->getOperand(j).setIsKill(); |
| 1738 | isKill = false; |
| 1739 | break; |
| 1740 | } |
| 1741 | |
Evan Cheng | 054dbb8 | 2010-05-13 00:00:35 +0000 | [diff] [blame] | 1742 | MachineBasicBlock::iterator InsertLoc = MI; |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 1743 | MachineInstr *CopyMI = BuildMI(*MI->getParent(), InsertLoc, |
| 1744 | MI->getDebugLoc(), TII->get(TargetOpcode::COPY)) |
Bob Wilson | 495de3b | 2010-12-17 01:21:12 +0000 | [diff] [blame] | 1745 | .addReg(DstReg, RegState::Define, SubIdx) |
Pete Cooper | ef74ca6 | 2012-04-04 21:03:25 +0000 | [diff] [blame] | 1746 | .addReg(SrcReg, getKillRegState(isKill), SrcSubIdx); |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 1747 | MI->getOperand(i).setReg(0); |
Pete Cooper | cd7f02b | 2012-01-18 04:16:16 +0000 | [diff] [blame] | 1748 | if (LV && isKill && !TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 1749 | LV->replaceKillInstruction(SrcReg, MI, CopyMI); |
| 1750 | DEBUG(dbgs() << "Inserted: " << *CopyMI); |
Evan Cheng | 0bcccac | 2010-05-11 00:04:31 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | for (unsigned i = 1, e = MI->getNumOperands(); i < e; i += 2) { |
| 1755 | unsigned SrcReg = MI->getOperand(i).getReg(); |
Jakob Stoklund Olesen | 1e1098c | 2010-07-10 22:42:59 +0000 | [diff] [blame] | 1756 | if (!SrcReg) continue; |
Evan Cheng | 53c779b | 2010-05-17 20:57:12 +0000 | [diff] [blame] | 1757 | unsigned SubIdx = MI->getOperand(i+1).getImm(); |
Jakob Stoklund Olesen | 5a0d4fc | 2010-05-29 00:14:14 +0000 | [diff] [blame] | 1758 | UpdateRegSequenceSrcs(SrcReg, DstReg, SubIdx, MRI, *TRI); |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Jakob Stoklund Olesen | d36f5af | 2012-01-24 23:28:42 +0000 | [diff] [blame] | 1761 | // Set <def,undef> flags on the first DstReg def in the basic block. |
| 1762 | // It marks the beginning of the live range. All the other defs are |
| 1763 | // read-modify-write. |
| 1764 | if (MachineInstr *Def = findFirstDef(DstReg, MRI)) { |
| 1765 | for (unsigned i = 0, e = Def->getNumOperands(); i != e; ++i) { |
| 1766 | MachineOperand &MO = Def->getOperand(i); |
| 1767 | if (MO.isReg() && MO.isDef() && MO.getReg() == DstReg) |
| 1768 | MO.setIsUndef(); |
| 1769 | } |
| 1770 | // Make sure there is a full non-subreg imp-def operand on the |
| 1771 | // instruction. This shouldn't be necessary, but it seems that at least |
| 1772 | // RAFast requires it. |
| 1773 | Def->addRegisterDefined(DstReg, TRI); |
| 1774 | DEBUG(dbgs() << "First def: " << *Def); |
| 1775 | } |
| 1776 | |
Evan Cheng | 44bfdd3 | 2010-05-17 22:09:49 +0000 | [diff] [blame] | 1777 | if (IsImpDef) { |
| 1778 | DEBUG(dbgs() << "Turned: " << *MI << " into an IMPLICIT_DEF"); |
| 1779 | MI->setDesc(TII->get(TargetOpcode::IMPLICIT_DEF)); |
| 1780 | for (int j = MI->getNumOperands() - 1, ee = 0; j > ee; --j) |
Andrew Trick | 8247e0d | 2012-02-03 05:12:30 +0000 | [diff] [blame] | 1781 | MI->RemoveOperand(j); |
Evan Cheng | 44bfdd3 | 2010-05-17 22:09:49 +0000 | [diff] [blame] | 1782 | } else { |
| 1783 | DEBUG(dbgs() << "Eliminated: " << *MI); |
| 1784 | MI->eraseFromParent(); |
| 1785 | } |
Evan Cheng | b990a2f | 2010-05-14 23:21:14 +0000 | [diff] [blame] | 1786 | |
Jakob Stoklund Olesen | fe181f4 | 2010-06-18 23:10:20 +0000 | [diff] [blame] | 1787 | // Try coalescing some EXTRACT_SUBREG instructions. This can create |
| 1788 | // INSERT_SUBREG instructions that must have <undef> flags added by |
| 1789 | // LiveIntervalAnalysis, so only run it when LiveVariables is available. |
| 1790 | if (LV) |
| 1791 | CoalesceExtSubRegs(RealSrcs, DstReg); |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1792 | } |
| 1793 | |
Evan Cheng | fc6e6a9 | 2010-05-10 21:24:55 +0000 | [diff] [blame] | 1794 | RegSequences.clear(); |
Evan Cheng | 3d720fb | 2010-05-05 18:45:40 +0000 | [diff] [blame] | 1795 | return true; |
| 1796 | } |