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