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