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