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