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