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