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