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