10b57cec5SDimitry Andric //===- TwoAddressInstructionPass.cpp - Two-Address instruction pass -------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the TwoAddress instruction pass which is used 100b57cec5SDimitry Andric // by most register allocators. Two-Address instructions are rewritten 110b57cec5SDimitry Andric // from: 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric // A = B op C 140b57cec5SDimitry Andric // 150b57cec5SDimitry Andric // to: 160b57cec5SDimitry Andric // 170b57cec5SDimitry Andric // A = B 180b57cec5SDimitry Andric // A op= C 190b57cec5SDimitry Andric // 200b57cec5SDimitry Andric // Note that if a register allocator chooses to use this pass, that it 210b57cec5SDimitry Andric // has to be capable of handling the non-SSA nature of these rewritten 220b57cec5SDimitry Andric // virtual registers. 230b57cec5SDimitry Andric // 240b57cec5SDimitry Andric // It is also worth noting that the duplicate operand of the two 250b57cec5SDimitry Andric // address instruction is removed. 260b57cec5SDimitry Andric // 270b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 300b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 310b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 320b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 330b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 340b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 350b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h" 360b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 370b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 380b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h" 390b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 410b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 430b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 440b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 450b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 460b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 470b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 480b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 490b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 500b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 510b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 520b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h" 530b57cec5SDimitry Andric #include "llvm/MC/MCInstrItineraries.h" 540b57cec5SDimitry Andric #include "llvm/Pass.h" 550b57cec5SDimitry Andric #include "llvm/Support/CodeGen.h" 560b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 570b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 580b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 590b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 600b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 610b57cec5SDimitry Andric #include <cassert> 620b57cec5SDimitry Andric #include <iterator> 630b57cec5SDimitry Andric #include <utility> 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric using namespace llvm; 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric #define DEBUG_TYPE "twoaddressinstruction" 680b57cec5SDimitry Andric 690b57cec5SDimitry Andric STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions"); 700b57cec5SDimitry Andric STATISTIC(NumCommuted , "Number of instructions commuted to coalesce"); 710b57cec5SDimitry Andric STATISTIC(NumAggrCommuted , "Number of instructions aggressively commuted"); 720b57cec5SDimitry Andric STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address"); 730b57cec5SDimitry Andric STATISTIC(Num3AddrSunk, "Number of 3-address instructions sunk"); 740b57cec5SDimitry Andric STATISTIC(NumReSchedUps, "Number of instructions re-scheduled up"); 750b57cec5SDimitry Andric STATISTIC(NumReSchedDowns, "Number of instructions re-scheduled down"); 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric // Temporary flag to disable rescheduling. 780b57cec5SDimitry Andric static cl::opt<bool> 790b57cec5SDimitry Andric EnableRescheduling("twoaddr-reschedule", 800b57cec5SDimitry Andric cl::desc("Coalesce copies by rescheduling (default=true)"), 810b57cec5SDimitry Andric cl::init(true), cl::Hidden); 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // Limit the number of dataflow edges to traverse when evaluating the benefit 840b57cec5SDimitry Andric // of commuting operands. 850b57cec5SDimitry Andric static cl::opt<unsigned> MaxDataFlowEdge( 860b57cec5SDimitry Andric "dataflow-edge-limit", cl::Hidden, cl::init(3), 870b57cec5SDimitry Andric cl::desc("Maximum number of dataflow edges to traverse when evaluating " 880b57cec5SDimitry Andric "the benefit of commuting operands")); 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric namespace { 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric class TwoAddressInstructionPass : public MachineFunctionPass { 930b57cec5SDimitry Andric MachineFunction *MF; 940b57cec5SDimitry Andric const TargetInstrInfo *TII; 950b57cec5SDimitry Andric const TargetRegisterInfo *TRI; 960b57cec5SDimitry Andric const InstrItineraryData *InstrItins; 970b57cec5SDimitry Andric MachineRegisterInfo *MRI; 980b57cec5SDimitry Andric LiveVariables *LV; 990b57cec5SDimitry Andric LiveIntervals *LIS; 1000b57cec5SDimitry Andric AliasAnalysis *AA; 1010b57cec5SDimitry Andric CodeGenOpt::Level OptLevel; 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric // The current basic block being processed. 1040b57cec5SDimitry Andric MachineBasicBlock *MBB; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric // Keep track the distance of a MI from the start of the current basic block. 1070b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned> DistanceMap; 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric // Set of already processed instructions in the current block. 1100b57cec5SDimitry Andric SmallPtrSet<MachineInstr*, 8> Processed; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric // Set of instructions converted to three-address by target and then sunk 1130b57cec5SDimitry Andric // down current basic block. 1140b57cec5SDimitry Andric SmallPtrSet<MachineInstr*, 8> SunkInstrs; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric // A map from virtual registers to physical registers which are likely targets 1170b57cec5SDimitry Andric // to be coalesced to due to copies from physical registers to virtual 1180b57cec5SDimitry Andric // registers. e.g. v1024 = move r0. 1190b57cec5SDimitry Andric DenseMap<unsigned, unsigned> SrcRegMap; 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // A map from virtual registers to physical registers which are likely targets 1220b57cec5SDimitry Andric // to be coalesced to due to copies to physical registers from virtual 1230b57cec5SDimitry Andric // registers. e.g. r1 = move v1024. 1240b57cec5SDimitry Andric DenseMap<unsigned, unsigned> DstRegMap; 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric bool sink3AddrInstruction(MachineInstr *MI, unsigned Reg, 1270b57cec5SDimitry Andric MachineBasicBlock::iterator OldPos); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric bool isRevCopyChain(unsigned FromReg, unsigned ToReg, int Maxlen); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric bool noUseAfterLastDef(unsigned Reg, unsigned Dist, unsigned &LastDef); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric bool isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC, 1340b57cec5SDimitry Andric MachineInstr *MI, unsigned Dist); 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric bool commuteInstruction(MachineInstr *MI, unsigned DstIdx, 1370b57cec5SDimitry Andric unsigned RegBIdx, unsigned RegCIdx, unsigned Dist); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric bool isProfitableToConv3Addr(unsigned RegA, unsigned RegB); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric bool convertInstTo3Addr(MachineBasicBlock::iterator &mi, 1420b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 1430b57cec5SDimitry Andric unsigned RegA, unsigned RegB, unsigned Dist); 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric bool isDefTooClose(unsigned Reg, unsigned Dist, MachineInstr *MI); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric bool rescheduleMIBelowKill(MachineBasicBlock::iterator &mi, 1480b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 1490b57cec5SDimitry Andric unsigned Reg); 1500b57cec5SDimitry Andric bool rescheduleKillAboveMI(MachineBasicBlock::iterator &mi, 1510b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 1520b57cec5SDimitry Andric unsigned Reg); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric bool tryInstructionTransform(MachineBasicBlock::iterator &mi, 1550b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 1560b57cec5SDimitry Andric unsigned SrcIdx, unsigned DstIdx, 1570b57cec5SDimitry Andric unsigned Dist, bool shouldOnlyCommute); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric bool tryInstructionCommute(MachineInstr *MI, 1600b57cec5SDimitry Andric unsigned DstOpIdx, 1610b57cec5SDimitry Andric unsigned BaseOpIdx, 1620b57cec5SDimitry Andric bool BaseOpKilled, 1630b57cec5SDimitry Andric unsigned Dist); 1640b57cec5SDimitry Andric void scanUses(unsigned DstReg); 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric void processCopy(MachineInstr *MI); 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric using TiedPairList = SmallVector<std::pair<unsigned, unsigned>, 4>; 1690b57cec5SDimitry Andric using TiedOperandMap = SmallDenseMap<unsigned, TiedPairList>; 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric bool collectTiedOperands(MachineInstr *MI, TiedOperandMap&); 1720b57cec5SDimitry Andric void processTiedPairs(MachineInstr *MI, TiedPairList&, unsigned &Dist); 1730b57cec5SDimitry Andric void eliminateRegSequence(MachineBasicBlock::iterator&); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric public: 1760b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric TwoAddressInstructionPass() : MachineFunctionPass(ID) { 1790b57cec5SDimitry Andric initializeTwoAddressInstructionPassPass(*PassRegistry::getPassRegistry()); 1800b57cec5SDimitry Andric } 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1830b57cec5SDimitry Andric AU.setPreservesCFG(); 1840b57cec5SDimitry Andric AU.addUsedIfAvailable<AAResultsWrapperPass>(); 1850b57cec5SDimitry Andric AU.addUsedIfAvailable<LiveVariables>(); 1860b57cec5SDimitry Andric AU.addPreserved<LiveVariables>(); 1870b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>(); 1880b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>(); 1890b57cec5SDimitry Andric AU.addPreservedID(MachineLoopInfoID); 1900b57cec5SDimitry Andric AU.addPreservedID(MachineDominatorsID); 1910b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 1920b57cec5SDimitry Andric } 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric /// Pass entry point. 1950b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction&) override; 1960b57cec5SDimitry Andric }; 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric } // end anonymous namespace 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric char TwoAddressInstructionPass::ID = 0; 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric char &llvm::TwoAddressInstructionPassID = TwoAddressInstructionPass::ID; 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(TwoAddressInstructionPass, DEBUG_TYPE, 2050b57cec5SDimitry Andric "Two-Address instruction pass", false, false) 2060b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 2070b57cec5SDimitry Andric INITIALIZE_PASS_END(TwoAddressInstructionPass, DEBUG_TYPE, 2080b57cec5SDimitry Andric "Two-Address instruction pass", false, false) 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg, LiveIntervals *LIS); 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric /// A two-address instruction has been converted to a three-address instruction 2130b57cec5SDimitry Andric /// to avoid clobbering a register. Try to sink it past the instruction that 2140b57cec5SDimitry Andric /// would kill the above mentioned register to reduce register pressure. 2150b57cec5SDimitry Andric bool TwoAddressInstructionPass:: 2160b57cec5SDimitry Andric sink3AddrInstruction(MachineInstr *MI, unsigned SavedReg, 2170b57cec5SDimitry Andric MachineBasicBlock::iterator OldPos) { 2180b57cec5SDimitry Andric // FIXME: Shouldn't we be trying to do this before we three-addressify the 2190b57cec5SDimitry Andric // instruction? After this transformation is done, we no longer need 2200b57cec5SDimitry Andric // the instruction to be in three-address form. 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric // Check if it's safe to move this instruction. 2230b57cec5SDimitry Andric bool SeenStore = true; // Be conservative. 2240b57cec5SDimitry Andric if (!MI->isSafeToMove(AA, SeenStore)) 2250b57cec5SDimitry Andric return false; 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric unsigned DefReg = 0; 2280b57cec5SDimitry Andric SmallSet<unsigned, 4> UseRegs; 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands()) { 2310b57cec5SDimitry Andric if (!MO.isReg()) 2320b57cec5SDimitry Andric continue; 2338bcb0991SDimitry Andric Register MOReg = MO.getReg(); 2340b57cec5SDimitry Andric if (!MOReg) 2350b57cec5SDimitry Andric continue; 2360b57cec5SDimitry Andric if (MO.isUse() && MOReg != SavedReg) 2370b57cec5SDimitry Andric UseRegs.insert(MO.getReg()); 2380b57cec5SDimitry Andric if (!MO.isDef()) 2390b57cec5SDimitry Andric continue; 2400b57cec5SDimitry Andric if (MO.isImplicit()) 2410b57cec5SDimitry Andric // Don't try to move it if it implicitly defines a register. 2420b57cec5SDimitry Andric return false; 2430b57cec5SDimitry Andric if (DefReg) 2440b57cec5SDimitry Andric // For now, don't move any instructions that define multiple registers. 2450b57cec5SDimitry Andric return false; 2460b57cec5SDimitry Andric DefReg = MO.getReg(); 2470b57cec5SDimitry Andric } 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric // Find the instruction that kills SavedReg. 2500b57cec5SDimitry Andric MachineInstr *KillMI = nullptr; 2510b57cec5SDimitry Andric if (LIS) { 2520b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(SavedReg); 2530b57cec5SDimitry Andric assert(LI.end() != LI.begin() && 2540b57cec5SDimitry Andric "Reg should not have empty live interval."); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot(); 2570b57cec5SDimitry Andric LiveInterval::const_iterator I = LI.find(MBBEndIdx); 2580b57cec5SDimitry Andric if (I != LI.end() && I->start < MBBEndIdx) 2590b57cec5SDimitry Andric return false; 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric --I; 2620b57cec5SDimitry Andric KillMI = LIS->getInstructionFromIndex(I->end); 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric if (!KillMI) { 2650b57cec5SDimitry Andric for (MachineOperand &UseMO : MRI->use_nodbg_operands(SavedReg)) { 2660b57cec5SDimitry Andric if (!UseMO.isKill()) 2670b57cec5SDimitry Andric continue; 2680b57cec5SDimitry Andric KillMI = UseMO.getParent(); 2690b57cec5SDimitry Andric break; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric // If we find the instruction that kills SavedReg, and it is in an 2740b57cec5SDimitry Andric // appropriate location, we can try to sink the current instruction 2750b57cec5SDimitry Andric // past it. 2760b57cec5SDimitry Andric if (!KillMI || KillMI->getParent() != MBB || KillMI == MI || 2770b57cec5SDimitry Andric MachineBasicBlock::iterator(KillMI) == OldPos || KillMI->isTerminator()) 2780b57cec5SDimitry Andric return false; 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric // If any of the definitions are used by another instruction between the 2810b57cec5SDimitry Andric // position and the kill use, then it's not safe to sink it. 2820b57cec5SDimitry Andric // 2830b57cec5SDimitry Andric // FIXME: This can be sped up if there is an easy way to query whether an 2840b57cec5SDimitry Andric // instruction is before or after another instruction. Then we can use 2850b57cec5SDimitry Andric // MachineRegisterInfo def / use instead. 2860b57cec5SDimitry Andric MachineOperand *KillMO = nullptr; 2870b57cec5SDimitry Andric MachineBasicBlock::iterator KillPos = KillMI; 2880b57cec5SDimitry Andric ++KillPos; 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric unsigned NumVisited = 0; 2910b57cec5SDimitry Andric for (MachineInstr &OtherMI : make_range(std::next(OldPos), KillPos)) { 2920b57cec5SDimitry Andric // Debug instructions cannot be counted against the limit. 2930b57cec5SDimitry Andric if (OtherMI.isDebugInstr()) 2940b57cec5SDimitry Andric continue; 2950b57cec5SDimitry Andric if (NumVisited > 30) // FIXME: Arbitrary limit to reduce compile time cost. 2960b57cec5SDimitry Andric return false; 2970b57cec5SDimitry Andric ++NumVisited; 2980b57cec5SDimitry Andric for (unsigned i = 0, e = OtherMI.getNumOperands(); i != e; ++i) { 2990b57cec5SDimitry Andric MachineOperand &MO = OtherMI.getOperand(i); 3000b57cec5SDimitry Andric if (!MO.isReg()) 3010b57cec5SDimitry Andric continue; 3028bcb0991SDimitry Andric Register MOReg = MO.getReg(); 3030b57cec5SDimitry Andric if (!MOReg) 3040b57cec5SDimitry Andric continue; 3050b57cec5SDimitry Andric if (DefReg == MOReg) 3060b57cec5SDimitry Andric return false; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric if (MO.isKill() || (LIS && isPlainlyKilled(&OtherMI, MOReg, LIS))) { 3090b57cec5SDimitry Andric if (&OtherMI == KillMI && MOReg == SavedReg) 3100b57cec5SDimitry Andric // Save the operand that kills the register. We want to unset the kill 3110b57cec5SDimitry Andric // marker if we can sink MI past it. 3120b57cec5SDimitry Andric KillMO = &MO; 3130b57cec5SDimitry Andric else if (UseRegs.count(MOReg)) 3140b57cec5SDimitry Andric // One of the uses is killed before the destination. 3150b57cec5SDimitry Andric return false; 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric assert(KillMO && "Didn't find kill"); 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric if (!LIS) { 3220b57cec5SDimitry Andric // Update kill and LV information. 3230b57cec5SDimitry Andric KillMO->setIsKill(false); 3240b57cec5SDimitry Andric KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI); 3250b57cec5SDimitry Andric KillMO->setIsKill(true); 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric if (LV) 3280b57cec5SDimitry Andric LV->replaceKillInstruction(SavedReg, *KillMI, *MI); 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric // Move instruction to its destination. 3320b57cec5SDimitry Andric MBB->remove(MI); 3330b57cec5SDimitry Andric MBB->insert(KillPos, MI); 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric if (LIS) 3360b57cec5SDimitry Andric LIS->handleMove(*MI); 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric ++Num3AddrSunk; 3390b57cec5SDimitry Andric return true; 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric /// Return the MachineInstr* if it is the single def of the Reg in current BB. 3430b57cec5SDimitry Andric static MachineInstr *getSingleDef(unsigned Reg, MachineBasicBlock *BB, 3440b57cec5SDimitry Andric const MachineRegisterInfo *MRI) { 3450b57cec5SDimitry Andric MachineInstr *Ret = nullptr; 3460b57cec5SDimitry Andric for (MachineInstr &DefMI : MRI->def_instructions(Reg)) { 3470b57cec5SDimitry Andric if (DefMI.getParent() != BB || DefMI.isDebugValue()) 3480b57cec5SDimitry Andric continue; 3490b57cec5SDimitry Andric if (!Ret) 3500b57cec5SDimitry Andric Ret = &DefMI; 3510b57cec5SDimitry Andric else if (Ret != &DefMI) 3520b57cec5SDimitry Andric return nullptr; 3530b57cec5SDimitry Andric } 3540b57cec5SDimitry Andric return Ret; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric /// Check if there is a reversed copy chain from FromReg to ToReg: 3580b57cec5SDimitry Andric /// %Tmp1 = copy %Tmp2; 3590b57cec5SDimitry Andric /// %FromReg = copy %Tmp1; 3600b57cec5SDimitry Andric /// %ToReg = add %FromReg ... 3610b57cec5SDimitry Andric /// %Tmp2 = copy %ToReg; 3620b57cec5SDimitry Andric /// MaxLen specifies the maximum length of the copy chain the func 3630b57cec5SDimitry Andric /// can walk through. 3640b57cec5SDimitry Andric bool TwoAddressInstructionPass::isRevCopyChain(unsigned FromReg, unsigned ToReg, 3650b57cec5SDimitry Andric int Maxlen) { 3660b57cec5SDimitry Andric unsigned TmpReg = FromReg; 3670b57cec5SDimitry Andric for (int i = 0; i < Maxlen; i++) { 3680b57cec5SDimitry Andric MachineInstr *Def = getSingleDef(TmpReg, MBB, MRI); 3690b57cec5SDimitry Andric if (!Def || !Def->isCopy()) 3700b57cec5SDimitry Andric return false; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric TmpReg = Def->getOperand(1).getReg(); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric if (TmpReg == ToReg) 3750b57cec5SDimitry Andric return true; 3760b57cec5SDimitry Andric } 3770b57cec5SDimitry Andric return false; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric /// Return true if there are no intervening uses between the last instruction 3810b57cec5SDimitry Andric /// in the MBB that defines the specified register and the two-address 3820b57cec5SDimitry Andric /// instruction which is being processed. It also returns the last def location 3830b57cec5SDimitry Andric /// by reference. 3840b57cec5SDimitry Andric bool TwoAddressInstructionPass::noUseAfterLastDef(unsigned Reg, unsigned Dist, 3850b57cec5SDimitry Andric unsigned &LastDef) { 3860b57cec5SDimitry Andric LastDef = 0; 3870b57cec5SDimitry Andric unsigned LastUse = Dist; 3880b57cec5SDimitry Andric for (MachineOperand &MO : MRI->reg_operands(Reg)) { 3890b57cec5SDimitry Andric MachineInstr *MI = MO.getParent(); 3900b57cec5SDimitry Andric if (MI->getParent() != MBB || MI->isDebugValue()) 3910b57cec5SDimitry Andric continue; 3920b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); 3930b57cec5SDimitry Andric if (DI == DistanceMap.end()) 3940b57cec5SDimitry Andric continue; 3950b57cec5SDimitry Andric if (MO.isUse() && DI->second < LastUse) 3960b57cec5SDimitry Andric LastUse = DI->second; 3970b57cec5SDimitry Andric if (MO.isDef() && DI->second > LastDef) 3980b57cec5SDimitry Andric LastDef = DI->second; 3990b57cec5SDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric return !(LastUse > LastDef && LastUse < Dist); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric /// Return true if the specified MI is a copy instruction or an extract_subreg 4050b57cec5SDimitry Andric /// instruction. It also returns the source and destination registers and 4060b57cec5SDimitry Andric /// whether they are physical registers by reference. 4070b57cec5SDimitry Andric static bool isCopyToReg(MachineInstr &MI, const TargetInstrInfo *TII, 4080b57cec5SDimitry Andric unsigned &SrcReg, unsigned &DstReg, 4090b57cec5SDimitry Andric bool &IsSrcPhys, bool &IsDstPhys) { 4100b57cec5SDimitry Andric SrcReg = 0; 4110b57cec5SDimitry Andric DstReg = 0; 4120b57cec5SDimitry Andric if (MI.isCopy()) { 4130b57cec5SDimitry Andric DstReg = MI.getOperand(0).getReg(); 4140b57cec5SDimitry Andric SrcReg = MI.getOperand(1).getReg(); 4150b57cec5SDimitry Andric } else if (MI.isInsertSubreg() || MI.isSubregToReg()) { 4160b57cec5SDimitry Andric DstReg = MI.getOperand(0).getReg(); 4170b57cec5SDimitry Andric SrcReg = MI.getOperand(2).getReg(); 4180b57cec5SDimitry Andric } else 4190b57cec5SDimitry Andric return false; 4200b57cec5SDimitry Andric 4218bcb0991SDimitry Andric IsSrcPhys = Register::isPhysicalRegister(SrcReg); 4228bcb0991SDimitry Andric IsDstPhys = Register::isPhysicalRegister(DstReg); 4230b57cec5SDimitry Andric return true; 4240b57cec5SDimitry Andric } 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric /// Test if the given register value, which is used by the 4270b57cec5SDimitry Andric /// given instruction, is killed by the given instruction. 4280b57cec5SDimitry Andric static bool isPlainlyKilled(MachineInstr *MI, unsigned Reg, 4290b57cec5SDimitry Andric LiveIntervals *LIS) { 4308bcb0991SDimitry Andric if (LIS && Register::isVirtualRegister(Reg) && !LIS->isNotInMIMap(*MI)) { 4310b57cec5SDimitry Andric // FIXME: Sometimes tryInstructionTransform() will add instructions and 4320b57cec5SDimitry Andric // test whether they can be folded before keeping them. In this case it 4330b57cec5SDimitry Andric // sets a kill before recursively calling tryInstructionTransform() again. 4340b57cec5SDimitry Andric // If there is no interval available, we assume that this instruction is 4350b57cec5SDimitry Andric // one of those. A kill flag is manually inserted on the operand so the 4360b57cec5SDimitry Andric // check below will handle it. 4370b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 4380b57cec5SDimitry Andric // This is to match the kill flag version where undefs don't have kill 4390b57cec5SDimitry Andric // flags. 4400b57cec5SDimitry Andric if (!LI.hasAtLeastOneValue()) 4410b57cec5SDimitry Andric return false; 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric SlotIndex useIdx = LIS->getInstructionIndex(*MI); 4440b57cec5SDimitry Andric LiveInterval::const_iterator I = LI.find(useIdx); 4450b57cec5SDimitry Andric assert(I != LI.end() && "Reg must be live-in to use."); 4460b57cec5SDimitry Andric return !I->end.isBlock() && SlotIndex::isSameInstr(I->end, useIdx); 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric return MI->killsRegister(Reg); 4500b57cec5SDimitry Andric } 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric /// Test if the given register value, which is used by the given 4530b57cec5SDimitry Andric /// instruction, is killed by the given instruction. This looks through 4540b57cec5SDimitry Andric /// coalescable copies to see if the original value is potentially not killed. 4550b57cec5SDimitry Andric /// 4560b57cec5SDimitry Andric /// For example, in this code: 4570b57cec5SDimitry Andric /// 4580b57cec5SDimitry Andric /// %reg1034 = copy %reg1024 4590b57cec5SDimitry Andric /// %reg1035 = copy killed %reg1025 4600b57cec5SDimitry Andric /// %reg1036 = add killed %reg1034, killed %reg1035 4610b57cec5SDimitry Andric /// 4620b57cec5SDimitry Andric /// %reg1034 is not considered to be killed, since it is copied from a 4630b57cec5SDimitry Andric /// register which is not killed. Treating it as not killed lets the 4640b57cec5SDimitry Andric /// normal heuristics commute the (two-address) add, which lets 4650b57cec5SDimitry Andric /// coalescing eliminate the extra copy. 4660b57cec5SDimitry Andric /// 4670b57cec5SDimitry Andric /// If allowFalsePositives is true then likely kills are treated as kills even 4680b57cec5SDimitry Andric /// if it can't be proven that they are kills. 4690b57cec5SDimitry Andric static bool isKilled(MachineInstr &MI, unsigned Reg, 4700b57cec5SDimitry Andric const MachineRegisterInfo *MRI, 4710b57cec5SDimitry Andric const TargetInstrInfo *TII, 4720b57cec5SDimitry Andric LiveIntervals *LIS, 4730b57cec5SDimitry Andric bool allowFalsePositives) { 4740b57cec5SDimitry Andric MachineInstr *DefMI = &MI; 4750b57cec5SDimitry Andric while (true) { 4760b57cec5SDimitry Andric // All uses of physical registers are likely to be kills. 4778bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg) && 4780b57cec5SDimitry Andric (allowFalsePositives || MRI->hasOneUse(Reg))) 4790b57cec5SDimitry Andric return true; 4800b57cec5SDimitry Andric if (!isPlainlyKilled(DefMI, Reg, LIS)) 4810b57cec5SDimitry Andric return false; 4828bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) 4830b57cec5SDimitry Andric return true; 4840b57cec5SDimitry Andric MachineRegisterInfo::def_iterator Begin = MRI->def_begin(Reg); 4850b57cec5SDimitry Andric // If there are multiple defs, we can't do a simple analysis, so just 4860b57cec5SDimitry Andric // go with what the kill flag says. 4870b57cec5SDimitry Andric if (std::next(Begin) != MRI->def_end()) 4880b57cec5SDimitry Andric return true; 4890b57cec5SDimitry Andric DefMI = Begin->getParent(); 4900b57cec5SDimitry Andric bool IsSrcPhys, IsDstPhys; 4910b57cec5SDimitry Andric unsigned SrcReg, DstReg; 4920b57cec5SDimitry Andric // If the def is something other than a copy, then it isn't going to 4930b57cec5SDimitry Andric // be coalesced, so follow the kill flag. 4940b57cec5SDimitry Andric if (!isCopyToReg(*DefMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) 4950b57cec5SDimitry Andric return true; 4960b57cec5SDimitry Andric Reg = SrcReg; 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric } 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric /// Return true if the specified MI uses the specified register as a two-address 5010b57cec5SDimitry Andric /// use. If so, return the destination register by reference. 5020b57cec5SDimitry Andric static bool isTwoAddrUse(MachineInstr &MI, unsigned Reg, unsigned &DstReg) { 5030b57cec5SDimitry Andric for (unsigned i = 0, NumOps = MI.getNumOperands(); i != NumOps; ++i) { 5040b57cec5SDimitry Andric const MachineOperand &MO = MI.getOperand(i); 5050b57cec5SDimitry Andric if (!MO.isReg() || !MO.isUse() || MO.getReg() != Reg) 5060b57cec5SDimitry Andric continue; 5070b57cec5SDimitry Andric unsigned ti; 5080b57cec5SDimitry Andric if (MI.isRegTiedToDefOperand(i, &ti)) { 5090b57cec5SDimitry Andric DstReg = MI.getOperand(ti).getReg(); 5100b57cec5SDimitry Andric return true; 5110b57cec5SDimitry Andric } 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric return false; 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric /// Given a register, if has a single in-basic block use, return the use 5170b57cec5SDimitry Andric /// instruction if it's a copy or a two-address use. 5180b57cec5SDimitry Andric static 5190b57cec5SDimitry Andric MachineInstr *findOnlyInterestingUse(unsigned Reg, MachineBasicBlock *MBB, 5200b57cec5SDimitry Andric MachineRegisterInfo *MRI, 5210b57cec5SDimitry Andric const TargetInstrInfo *TII, 5220b57cec5SDimitry Andric bool &IsCopy, 5230b57cec5SDimitry Andric unsigned &DstReg, bool &IsDstPhys) { 5240b57cec5SDimitry Andric if (!MRI->hasOneNonDBGUse(Reg)) 5250b57cec5SDimitry Andric // None or more than one use. 5260b57cec5SDimitry Andric return nullptr; 5270b57cec5SDimitry Andric MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(Reg); 5280b57cec5SDimitry Andric if (UseMI.getParent() != MBB) 5290b57cec5SDimitry Andric return nullptr; 5300b57cec5SDimitry Andric unsigned SrcReg; 5310b57cec5SDimitry Andric bool IsSrcPhys; 5320b57cec5SDimitry Andric if (isCopyToReg(UseMI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) { 5330b57cec5SDimitry Andric IsCopy = true; 5340b57cec5SDimitry Andric return &UseMI; 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric IsDstPhys = false; 5370b57cec5SDimitry Andric if (isTwoAddrUse(UseMI, Reg, DstReg)) { 5388bcb0991SDimitry Andric IsDstPhys = Register::isPhysicalRegister(DstReg); 5390b57cec5SDimitry Andric return &UseMI; 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric return nullptr; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric /// Return the physical register the specified virtual register might be mapped 5450b57cec5SDimitry Andric /// to. 5460b57cec5SDimitry Andric static unsigned 5470b57cec5SDimitry Andric getMappedReg(unsigned Reg, DenseMap<unsigned, unsigned> &RegMap) { 5488bcb0991SDimitry Andric while (Register::isVirtualRegister(Reg)) { 5490b57cec5SDimitry Andric DenseMap<unsigned, unsigned>::iterator SI = RegMap.find(Reg); 5500b57cec5SDimitry Andric if (SI == RegMap.end()) 5510b57cec5SDimitry Andric return 0; 5520b57cec5SDimitry Andric Reg = SI->second; 5530b57cec5SDimitry Andric } 5548bcb0991SDimitry Andric if (Register::isPhysicalRegister(Reg)) 5550b57cec5SDimitry Andric return Reg; 5560b57cec5SDimitry Andric return 0; 5570b57cec5SDimitry Andric } 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric /// Return true if the two registers are equal or aliased. 5600b57cec5SDimitry Andric static bool 5610b57cec5SDimitry Andric regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) { 5620b57cec5SDimitry Andric if (RegA == RegB) 5630b57cec5SDimitry Andric return true; 5640b57cec5SDimitry Andric if (!RegA || !RegB) 5650b57cec5SDimitry Andric return false; 5660b57cec5SDimitry Andric return TRI->regsOverlap(RegA, RegB); 5670b57cec5SDimitry Andric } 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // Returns true if Reg is equal or aliased to at least one register in Set. 5700b57cec5SDimitry Andric static bool regOverlapsSet(const SmallVectorImpl<unsigned> &Set, unsigned Reg, 5710b57cec5SDimitry Andric const TargetRegisterInfo *TRI) { 5720b57cec5SDimitry Andric for (unsigned R : Set) 5730b57cec5SDimitry Andric if (TRI->regsOverlap(R, Reg)) 5740b57cec5SDimitry Andric return true; 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric return false; 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric /// Return true if it's potentially profitable to commute the two-address 5800b57cec5SDimitry Andric /// instruction that's being processed. 5810b57cec5SDimitry Andric bool 5820b57cec5SDimitry Andric TwoAddressInstructionPass:: 5830b57cec5SDimitry Andric isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC, 5840b57cec5SDimitry Andric MachineInstr *MI, unsigned Dist) { 5850b57cec5SDimitry Andric if (OptLevel == CodeGenOpt::None) 5860b57cec5SDimitry Andric return false; 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric // Determine if it's profitable to commute this two address instruction. In 5890b57cec5SDimitry Andric // general, we want no uses between this instruction and the definition of 5900b57cec5SDimitry Andric // the two-address register. 5910b57cec5SDimitry Andric // e.g. 5920b57cec5SDimitry Andric // %reg1028 = EXTRACT_SUBREG killed %reg1027, 1 5930b57cec5SDimitry Andric // %reg1029 = COPY %reg1028 5940b57cec5SDimitry Andric // %reg1029 = SHR8ri %reg1029, 7, implicit dead %eflags 5950b57cec5SDimitry Andric // insert => %reg1030 = COPY %reg1028 5960b57cec5SDimitry Andric // %reg1030 = ADD8rr killed %reg1028, killed %reg1029, implicit dead %eflags 5970b57cec5SDimitry Andric // In this case, it might not be possible to coalesce the second COPY 5980b57cec5SDimitry Andric // instruction if the first one is coalesced. So it would be profitable to 5990b57cec5SDimitry Andric // commute it: 6000b57cec5SDimitry Andric // %reg1028 = EXTRACT_SUBREG killed %reg1027, 1 6010b57cec5SDimitry Andric // %reg1029 = COPY %reg1028 6020b57cec5SDimitry Andric // %reg1029 = SHR8ri %reg1029, 7, implicit dead %eflags 6030b57cec5SDimitry Andric // insert => %reg1030 = COPY %reg1029 6040b57cec5SDimitry Andric // %reg1030 = ADD8rr killed %reg1029, killed %reg1028, implicit dead %eflags 6050b57cec5SDimitry Andric 6060b57cec5SDimitry Andric if (!isPlainlyKilled(MI, regC, LIS)) 6070b57cec5SDimitry Andric return false; 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric // Ok, we have something like: 6100b57cec5SDimitry Andric // %reg1030 = ADD8rr killed %reg1028, killed %reg1029, implicit dead %eflags 6110b57cec5SDimitry Andric // let's see if it's worth commuting it. 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric // Look for situations like this: 6140b57cec5SDimitry Andric // %reg1024 = MOV r1 6150b57cec5SDimitry Andric // %reg1025 = MOV r0 6160b57cec5SDimitry Andric // %reg1026 = ADD %reg1024, %reg1025 6170b57cec5SDimitry Andric // r0 = MOV %reg1026 6180b57cec5SDimitry Andric // Commute the ADD to hopefully eliminate an otherwise unavoidable copy. 6190b57cec5SDimitry Andric unsigned ToRegA = getMappedReg(regA, DstRegMap); 6200b57cec5SDimitry Andric if (ToRegA) { 6210b57cec5SDimitry Andric unsigned FromRegB = getMappedReg(regB, SrcRegMap); 6220b57cec5SDimitry Andric unsigned FromRegC = getMappedReg(regC, SrcRegMap); 6230b57cec5SDimitry Andric bool CompB = FromRegB && regsAreCompatible(FromRegB, ToRegA, TRI); 6240b57cec5SDimitry Andric bool CompC = FromRegC && regsAreCompatible(FromRegC, ToRegA, TRI); 6250b57cec5SDimitry Andric 6260b57cec5SDimitry Andric // Compute if any of the following are true: 6270b57cec5SDimitry Andric // -RegB is not tied to a register and RegC is compatible with RegA. 6280b57cec5SDimitry Andric // -RegB is tied to the wrong physical register, but RegC is. 6290b57cec5SDimitry Andric // -RegB is tied to the wrong physical register, and RegC isn't tied. 6300b57cec5SDimitry Andric if ((!FromRegB && CompC) || (FromRegB && !CompB && (!FromRegC || CompC))) 6310b57cec5SDimitry Andric return true; 6320b57cec5SDimitry Andric // Don't compute if any of the following are true: 6330b57cec5SDimitry Andric // -RegC is not tied to a register and RegB is compatible with RegA. 6340b57cec5SDimitry Andric // -RegC is tied to the wrong physical register, but RegB is. 6350b57cec5SDimitry Andric // -RegC is tied to the wrong physical register, and RegB isn't tied. 6360b57cec5SDimitry Andric if ((!FromRegC && CompB) || (FromRegC && !CompC && (!FromRegB || CompB))) 6370b57cec5SDimitry Andric return false; 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric // If there is a use of regC between its last def (could be livein) and this 6410b57cec5SDimitry Andric // instruction, then bail. 6420b57cec5SDimitry Andric unsigned LastDefC = 0; 6430b57cec5SDimitry Andric if (!noUseAfterLastDef(regC, Dist, LastDefC)) 6440b57cec5SDimitry Andric return false; 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric // If there is a use of regB between its last def (could be livein) and this 6470b57cec5SDimitry Andric // instruction, then go ahead and make this transformation. 6480b57cec5SDimitry Andric unsigned LastDefB = 0; 6490b57cec5SDimitry Andric if (!noUseAfterLastDef(regB, Dist, LastDefB)) 6500b57cec5SDimitry Andric return true; 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric // Look for situation like this: 6530b57cec5SDimitry Andric // %reg101 = MOV %reg100 6540b57cec5SDimitry Andric // %reg102 = ... 6550b57cec5SDimitry Andric // %reg103 = ADD %reg102, %reg101 6560b57cec5SDimitry Andric // ... = %reg103 ... 6570b57cec5SDimitry Andric // %reg100 = MOV %reg103 6580b57cec5SDimitry Andric // If there is a reversed copy chain from reg101 to reg103, commute the ADD 6590b57cec5SDimitry Andric // to eliminate an otherwise unavoidable copy. 6600b57cec5SDimitry Andric // FIXME: 6610b57cec5SDimitry Andric // We can extend the logic further: If an pair of operands in an insn has 6620b57cec5SDimitry Andric // been merged, the insn could be regarded as a virtual copy, and the virtual 6630b57cec5SDimitry Andric // copy could also be used to construct a copy chain. 6640b57cec5SDimitry Andric // To more generally minimize register copies, ideally the logic of two addr 6650b57cec5SDimitry Andric // instruction pass should be integrated with register allocation pass where 6660b57cec5SDimitry Andric // interference graph is available. 6670b57cec5SDimitry Andric if (isRevCopyChain(regC, regA, MaxDataFlowEdge)) 6680b57cec5SDimitry Andric return true; 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric if (isRevCopyChain(regB, regA, MaxDataFlowEdge)) 6710b57cec5SDimitry Andric return false; 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric // Since there are no intervening uses for both registers, then commute 6740b57cec5SDimitry Andric // if the def of regC is closer. Its live interval is shorter. 6750b57cec5SDimitry Andric return LastDefB && LastDefC && LastDefC > LastDefB; 6760b57cec5SDimitry Andric } 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric /// Commute a two-address instruction and update the basic block, distance map, 6790b57cec5SDimitry Andric /// and live variables if needed. Return true if it is successful. 6800b57cec5SDimitry Andric bool TwoAddressInstructionPass::commuteInstruction(MachineInstr *MI, 6810b57cec5SDimitry Andric unsigned DstIdx, 6820b57cec5SDimitry Andric unsigned RegBIdx, 6830b57cec5SDimitry Andric unsigned RegCIdx, 6840b57cec5SDimitry Andric unsigned Dist) { 6858bcb0991SDimitry Andric Register RegC = MI->getOperand(RegCIdx).getReg(); 6860b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: COMMUTING : " << *MI); 6870b57cec5SDimitry Andric MachineInstr *NewMI = TII->commuteInstruction(*MI, false, RegBIdx, RegCIdx); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric if (NewMI == nullptr) { 6900b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: COMMUTING FAILED!\n"); 6910b57cec5SDimitry Andric return false; 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: COMMUTED TO: " << *NewMI); 6950b57cec5SDimitry Andric assert(NewMI == MI && 6960b57cec5SDimitry Andric "TargetInstrInfo::commuteInstruction() should not return a new " 6970b57cec5SDimitry Andric "instruction unless it was requested."); 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric // Update source register map. 7000b57cec5SDimitry Andric unsigned FromRegC = getMappedReg(RegC, SrcRegMap); 7010b57cec5SDimitry Andric if (FromRegC) { 7028bcb0991SDimitry Andric Register RegA = MI->getOperand(DstIdx).getReg(); 7030b57cec5SDimitry Andric SrcRegMap[RegA] = FromRegC; 7040b57cec5SDimitry Andric } 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric return true; 7070b57cec5SDimitry Andric } 7080b57cec5SDimitry Andric 7090b57cec5SDimitry Andric /// Return true if it is profitable to convert the given 2-address instruction 7100b57cec5SDimitry Andric /// to a 3-address one. 7110b57cec5SDimitry Andric bool 7120b57cec5SDimitry Andric TwoAddressInstructionPass::isProfitableToConv3Addr(unsigned RegA,unsigned RegB){ 7130b57cec5SDimitry Andric // Look for situations like this: 7140b57cec5SDimitry Andric // %reg1024 = MOV r1 7150b57cec5SDimitry Andric // %reg1025 = MOV r0 7160b57cec5SDimitry Andric // %reg1026 = ADD %reg1024, %reg1025 7170b57cec5SDimitry Andric // r2 = MOV %reg1026 7180b57cec5SDimitry Andric // Turn ADD into a 3-address instruction to avoid a copy. 7190b57cec5SDimitry Andric unsigned FromRegB = getMappedReg(RegB, SrcRegMap); 7200b57cec5SDimitry Andric if (!FromRegB) 7210b57cec5SDimitry Andric return false; 7220b57cec5SDimitry Andric unsigned ToRegA = getMappedReg(RegA, DstRegMap); 7230b57cec5SDimitry Andric return (ToRegA && !regsAreCompatible(FromRegB, ToRegA, TRI)); 7240b57cec5SDimitry Andric } 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric /// Convert the specified two-address instruction into a three address one. 7270b57cec5SDimitry Andric /// Return true if this transformation was successful. 7280b57cec5SDimitry Andric bool 7290b57cec5SDimitry Andric TwoAddressInstructionPass::convertInstTo3Addr(MachineBasicBlock::iterator &mi, 7300b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 7310b57cec5SDimitry Andric unsigned RegA, unsigned RegB, 7320b57cec5SDimitry Andric unsigned Dist) { 7330b57cec5SDimitry Andric // FIXME: Why does convertToThreeAddress() need an iterator reference? 7340b57cec5SDimitry Andric MachineFunction::iterator MFI = MBB->getIterator(); 7350b57cec5SDimitry Andric MachineInstr *NewMI = TII->convertToThreeAddress(MFI, *mi, LV); 7360b57cec5SDimitry Andric assert(MBB->getIterator() == MFI && 7370b57cec5SDimitry Andric "convertToThreeAddress changed iterator reference"); 7380b57cec5SDimitry Andric if (!NewMI) 7390b57cec5SDimitry Andric return false; 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: CONVERTING 2-ADDR: " << *mi); 7420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: TO 3-ADDR: " << *NewMI); 7430b57cec5SDimitry Andric bool Sunk = false; 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric if (LIS) 7460b57cec5SDimitry Andric LIS->ReplaceMachineInstrInMaps(*mi, *NewMI); 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric if (NewMI->findRegisterUseOperand(RegB, false, TRI)) 7490b57cec5SDimitry Andric // FIXME: Temporary workaround. If the new instruction doesn't 7500b57cec5SDimitry Andric // uses RegB, convertToThreeAddress must have created more 7510b57cec5SDimitry Andric // then one instruction. 7520b57cec5SDimitry Andric Sunk = sink3AddrInstruction(NewMI, RegB, mi); 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric MBB->erase(mi); // Nuke the old inst. 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric if (!Sunk) { 7570b57cec5SDimitry Andric DistanceMap.insert(std::make_pair(NewMI, Dist)); 7580b57cec5SDimitry Andric mi = NewMI; 7590b57cec5SDimitry Andric nmi = std::next(mi); 7600b57cec5SDimitry Andric } 7610b57cec5SDimitry Andric else 7620b57cec5SDimitry Andric SunkInstrs.insert(NewMI); 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric // Update source and destination register maps. 7650b57cec5SDimitry Andric SrcRegMap.erase(RegA); 7660b57cec5SDimitry Andric DstRegMap.erase(RegB); 7670b57cec5SDimitry Andric return true; 7680b57cec5SDimitry Andric } 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric /// Scan forward recursively for only uses, update maps if the use is a copy or 7710b57cec5SDimitry Andric /// a two-address instruction. 7720b57cec5SDimitry Andric void 7730b57cec5SDimitry Andric TwoAddressInstructionPass::scanUses(unsigned DstReg) { 7740b57cec5SDimitry Andric SmallVector<unsigned, 4> VirtRegPairs; 7750b57cec5SDimitry Andric bool IsDstPhys; 7760b57cec5SDimitry Andric bool IsCopy = false; 7770b57cec5SDimitry Andric unsigned NewReg = 0; 7780b57cec5SDimitry Andric unsigned Reg = DstReg; 7790b57cec5SDimitry Andric while (MachineInstr *UseMI = findOnlyInterestingUse(Reg, MBB, MRI, TII,IsCopy, 7800b57cec5SDimitry Andric NewReg, IsDstPhys)) { 7810b57cec5SDimitry Andric if (IsCopy && !Processed.insert(UseMI).second) 7820b57cec5SDimitry Andric break; 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI); 7850b57cec5SDimitry Andric if (DI != DistanceMap.end()) 7860b57cec5SDimitry Andric // Earlier in the same MBB.Reached via a back edge. 7870b57cec5SDimitry Andric break; 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric if (IsDstPhys) { 7900b57cec5SDimitry Andric VirtRegPairs.push_back(NewReg); 7910b57cec5SDimitry Andric break; 7920b57cec5SDimitry Andric } 7930b57cec5SDimitry Andric bool isNew = SrcRegMap.insert(std::make_pair(NewReg, Reg)).second; 7940b57cec5SDimitry Andric if (!isNew) 7950b57cec5SDimitry Andric assert(SrcRegMap[NewReg] == Reg && "Can't map to two src registers!"); 7960b57cec5SDimitry Andric VirtRegPairs.push_back(NewReg); 7970b57cec5SDimitry Andric Reg = NewReg; 7980b57cec5SDimitry Andric } 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric if (!VirtRegPairs.empty()) { 8010b57cec5SDimitry Andric unsigned ToReg = VirtRegPairs.back(); 8020b57cec5SDimitry Andric VirtRegPairs.pop_back(); 8030b57cec5SDimitry Andric while (!VirtRegPairs.empty()) { 8040b57cec5SDimitry Andric unsigned FromReg = VirtRegPairs.back(); 8050b57cec5SDimitry Andric VirtRegPairs.pop_back(); 8060b57cec5SDimitry Andric bool isNew = DstRegMap.insert(std::make_pair(FromReg, ToReg)).second; 8070b57cec5SDimitry Andric if (!isNew) 8080b57cec5SDimitry Andric assert(DstRegMap[FromReg] == ToReg &&"Can't map to two dst registers!"); 8090b57cec5SDimitry Andric ToReg = FromReg; 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric bool isNew = DstRegMap.insert(std::make_pair(DstReg, ToReg)).second; 8120b57cec5SDimitry Andric if (!isNew) 8130b57cec5SDimitry Andric assert(DstRegMap[DstReg] == ToReg && "Can't map to two dst registers!"); 8140b57cec5SDimitry Andric } 8150b57cec5SDimitry Andric } 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric /// If the specified instruction is not yet processed, process it if it's a 8180b57cec5SDimitry Andric /// copy. For a copy instruction, we find the physical registers the 8190b57cec5SDimitry Andric /// source and destination registers might be mapped to. These are kept in 8200b57cec5SDimitry Andric /// point-to maps used to determine future optimizations. e.g. 8210b57cec5SDimitry Andric /// v1024 = mov r0 8220b57cec5SDimitry Andric /// v1025 = mov r1 8230b57cec5SDimitry Andric /// v1026 = add v1024, v1025 8240b57cec5SDimitry Andric /// r1 = mov r1026 8250b57cec5SDimitry Andric /// If 'add' is a two-address instruction, v1024, v1026 are both potentially 8260b57cec5SDimitry Andric /// coalesced to r0 (from the input side). v1025 is mapped to r1. v1026 is 8270b57cec5SDimitry Andric /// potentially joined with r1 on the output side. It's worthwhile to commute 8280b57cec5SDimitry Andric /// 'add' to eliminate a copy. 8290b57cec5SDimitry Andric void TwoAddressInstructionPass::processCopy(MachineInstr *MI) { 8300b57cec5SDimitry Andric if (Processed.count(MI)) 8310b57cec5SDimitry Andric return; 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric bool IsSrcPhys, IsDstPhys; 8340b57cec5SDimitry Andric unsigned SrcReg, DstReg; 8350b57cec5SDimitry Andric if (!isCopyToReg(*MI, TII, SrcReg, DstReg, IsSrcPhys, IsDstPhys)) 8360b57cec5SDimitry Andric return; 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric if (IsDstPhys && !IsSrcPhys) 8390b57cec5SDimitry Andric DstRegMap.insert(std::make_pair(SrcReg, DstReg)); 8400b57cec5SDimitry Andric else if (!IsDstPhys && IsSrcPhys) { 8410b57cec5SDimitry Andric bool isNew = SrcRegMap.insert(std::make_pair(DstReg, SrcReg)).second; 8420b57cec5SDimitry Andric if (!isNew) 8430b57cec5SDimitry Andric assert(SrcRegMap[DstReg] == SrcReg && 8440b57cec5SDimitry Andric "Can't map to two src physical registers!"); 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric scanUses(DstReg); 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric Processed.insert(MI); 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric /// If there is one more local instruction that reads 'Reg' and it kills 'Reg, 8530b57cec5SDimitry Andric /// consider moving the instruction below the kill instruction in order to 8540b57cec5SDimitry Andric /// eliminate the need for the copy. 8550b57cec5SDimitry Andric bool TwoAddressInstructionPass:: 8560b57cec5SDimitry Andric rescheduleMIBelowKill(MachineBasicBlock::iterator &mi, 8570b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 8580b57cec5SDimitry Andric unsigned Reg) { 8590b57cec5SDimitry Andric // Bail immediately if we don't have LV or LIS available. We use them to find 8600b57cec5SDimitry Andric // kills efficiently. 8610b57cec5SDimitry Andric if (!LV && !LIS) 8620b57cec5SDimitry Andric return false; 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric MachineInstr *MI = &*mi; 8650b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); 8660b57cec5SDimitry Andric if (DI == DistanceMap.end()) 8670b57cec5SDimitry Andric // Must be created from unfolded load. Don't waste time trying this. 8680b57cec5SDimitry Andric return false; 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric MachineInstr *KillMI = nullptr; 8710b57cec5SDimitry Andric if (LIS) { 8720b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 8730b57cec5SDimitry Andric assert(LI.end() != LI.begin() && 8740b57cec5SDimitry Andric "Reg should not have empty live interval."); 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot(); 8770b57cec5SDimitry Andric LiveInterval::const_iterator I = LI.find(MBBEndIdx); 8780b57cec5SDimitry Andric if (I != LI.end() && I->start < MBBEndIdx) 8790b57cec5SDimitry Andric return false; 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric --I; 8820b57cec5SDimitry Andric KillMI = LIS->getInstructionFromIndex(I->end); 8830b57cec5SDimitry Andric } else { 8840b57cec5SDimitry Andric KillMI = LV->getVarInfo(Reg).findKill(MBB); 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike()) 8870b57cec5SDimitry Andric // Don't mess with copies, they may be coalesced later. 8880b57cec5SDimitry Andric return false; 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric if (KillMI->hasUnmodeledSideEffects() || KillMI->isCall() || 8910b57cec5SDimitry Andric KillMI->isBranch() || KillMI->isTerminator()) 8920b57cec5SDimitry Andric // Don't move pass calls, etc. 8930b57cec5SDimitry Andric return false; 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric unsigned DstReg; 8960b57cec5SDimitry Andric if (isTwoAddrUse(*KillMI, Reg, DstReg)) 8970b57cec5SDimitry Andric return false; 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric bool SeenStore = true; 9000b57cec5SDimitry Andric if (!MI->isSafeToMove(AA, SeenStore)) 9010b57cec5SDimitry Andric return false; 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric if (TII->getInstrLatency(InstrItins, *MI) > 1) 9040b57cec5SDimitry Andric // FIXME: Needs more sophisticated heuristics. 9050b57cec5SDimitry Andric return false; 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric SmallVector<unsigned, 2> Uses; 9080b57cec5SDimitry Andric SmallVector<unsigned, 2> Kills; 9090b57cec5SDimitry Andric SmallVector<unsigned, 2> Defs; 9100b57cec5SDimitry Andric for (const MachineOperand &MO : MI->operands()) { 9110b57cec5SDimitry Andric if (!MO.isReg()) 9120b57cec5SDimitry Andric continue; 9138bcb0991SDimitry Andric Register MOReg = MO.getReg(); 9140b57cec5SDimitry Andric if (!MOReg) 9150b57cec5SDimitry Andric continue; 9160b57cec5SDimitry Andric if (MO.isDef()) 9170b57cec5SDimitry Andric Defs.push_back(MOReg); 9180b57cec5SDimitry Andric else { 9190b57cec5SDimitry Andric Uses.push_back(MOReg); 9200b57cec5SDimitry Andric if (MOReg != Reg && (MO.isKill() || 9210b57cec5SDimitry Andric (LIS && isPlainlyKilled(MI, MOReg, LIS)))) 9220b57cec5SDimitry Andric Kills.push_back(MOReg); 9230b57cec5SDimitry Andric } 9240b57cec5SDimitry Andric } 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric // Move the copies connected to MI down as well. 9270b57cec5SDimitry Andric MachineBasicBlock::iterator Begin = MI; 9280b57cec5SDimitry Andric MachineBasicBlock::iterator AfterMI = std::next(Begin); 9290b57cec5SDimitry Andric MachineBasicBlock::iterator End = AfterMI; 9300b57cec5SDimitry Andric while (End != MBB->end()) { 9310b57cec5SDimitry Andric End = skipDebugInstructionsForward(End, MBB->end()); 9320b57cec5SDimitry Andric if (End->isCopy() && regOverlapsSet(Defs, End->getOperand(1).getReg(), TRI)) 9330b57cec5SDimitry Andric Defs.push_back(End->getOperand(0).getReg()); 9340b57cec5SDimitry Andric else 9350b57cec5SDimitry Andric break; 9360b57cec5SDimitry Andric ++End; 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // Check if the reschedule will not break dependencies. 9400b57cec5SDimitry Andric unsigned NumVisited = 0; 9410b57cec5SDimitry Andric MachineBasicBlock::iterator KillPos = KillMI; 9420b57cec5SDimitry Andric ++KillPos; 9430b57cec5SDimitry Andric for (MachineInstr &OtherMI : make_range(End, KillPos)) { 9440b57cec5SDimitry Andric // Debug instructions cannot be counted against the limit. 9450b57cec5SDimitry Andric if (OtherMI.isDebugInstr()) 9460b57cec5SDimitry Andric continue; 9470b57cec5SDimitry Andric if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost. 9480b57cec5SDimitry Andric return false; 9490b57cec5SDimitry Andric ++NumVisited; 9500b57cec5SDimitry Andric if (OtherMI.hasUnmodeledSideEffects() || OtherMI.isCall() || 9510b57cec5SDimitry Andric OtherMI.isBranch() || OtherMI.isTerminator()) 9520b57cec5SDimitry Andric // Don't move pass calls, etc. 9530b57cec5SDimitry Andric return false; 9540b57cec5SDimitry Andric for (const MachineOperand &MO : OtherMI.operands()) { 9550b57cec5SDimitry Andric if (!MO.isReg()) 9560b57cec5SDimitry Andric continue; 9578bcb0991SDimitry Andric Register MOReg = MO.getReg(); 9580b57cec5SDimitry Andric if (!MOReg) 9590b57cec5SDimitry Andric continue; 9600b57cec5SDimitry Andric if (MO.isDef()) { 9610b57cec5SDimitry Andric if (regOverlapsSet(Uses, MOReg, TRI)) 9620b57cec5SDimitry Andric // Physical register use would be clobbered. 9630b57cec5SDimitry Andric return false; 9640b57cec5SDimitry Andric if (!MO.isDead() && regOverlapsSet(Defs, MOReg, TRI)) 9650b57cec5SDimitry Andric // May clobber a physical register def. 9660b57cec5SDimitry Andric // FIXME: This may be too conservative. It's ok if the instruction 9670b57cec5SDimitry Andric // is sunken completely below the use. 9680b57cec5SDimitry Andric return false; 9690b57cec5SDimitry Andric } else { 9700b57cec5SDimitry Andric if (regOverlapsSet(Defs, MOReg, TRI)) 9710b57cec5SDimitry Andric return false; 9720b57cec5SDimitry Andric bool isKill = 9730b57cec5SDimitry Andric MO.isKill() || (LIS && isPlainlyKilled(&OtherMI, MOReg, LIS)); 9740b57cec5SDimitry Andric if (MOReg != Reg && ((isKill && regOverlapsSet(Uses, MOReg, TRI)) || 9750b57cec5SDimitry Andric regOverlapsSet(Kills, MOReg, TRI))) 9760b57cec5SDimitry Andric // Don't want to extend other live ranges and update kills. 9770b57cec5SDimitry Andric return false; 9780b57cec5SDimitry Andric if (MOReg == Reg && !isKill) 9790b57cec5SDimitry Andric // We can't schedule across a use of the register in question. 9800b57cec5SDimitry Andric return false; 9810b57cec5SDimitry Andric // Ensure that if this is register in question, its the kill we expect. 9820b57cec5SDimitry Andric assert((MOReg != Reg || &OtherMI == KillMI) && 9830b57cec5SDimitry Andric "Found multiple kills of a register in a basic block"); 9840b57cec5SDimitry Andric } 9850b57cec5SDimitry Andric } 9860b57cec5SDimitry Andric } 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric // Move debug info as well. 9890b57cec5SDimitry Andric while (Begin != MBB->begin() && std::prev(Begin)->isDebugInstr()) 9900b57cec5SDimitry Andric --Begin; 9910b57cec5SDimitry Andric 9920b57cec5SDimitry Andric nmi = End; 9930b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos = KillPos; 9940b57cec5SDimitry Andric if (LIS) { 9950b57cec5SDimitry Andric // We have to move the copies first so that the MBB is still well-formed 9960b57cec5SDimitry Andric // when calling handleMove(). 9970b57cec5SDimitry Andric for (MachineBasicBlock::iterator MBBI = AfterMI; MBBI != End;) { 9980b57cec5SDimitry Andric auto CopyMI = MBBI++; 9990b57cec5SDimitry Andric MBB->splice(InsertPos, MBB, CopyMI); 10000b57cec5SDimitry Andric LIS->handleMove(*CopyMI); 10010b57cec5SDimitry Andric InsertPos = CopyMI; 10020b57cec5SDimitry Andric } 10030b57cec5SDimitry Andric End = std::next(MachineBasicBlock::iterator(MI)); 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric // Copies following MI may have been moved as well. 10070b57cec5SDimitry Andric MBB->splice(InsertPos, MBB, Begin, End); 10080b57cec5SDimitry Andric DistanceMap.erase(DI); 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric // Update live variables 10110b57cec5SDimitry Andric if (LIS) { 10120b57cec5SDimitry Andric LIS->handleMove(*MI); 10130b57cec5SDimitry Andric } else { 10140b57cec5SDimitry Andric LV->removeVirtualRegisterKilled(Reg, *KillMI); 10150b57cec5SDimitry Andric LV->addVirtualRegisterKilled(Reg, *MI); 10160b57cec5SDimitry Andric } 10170b57cec5SDimitry Andric 10180b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\trescheduled below kill: " << *KillMI); 10190b57cec5SDimitry Andric return true; 10200b57cec5SDimitry Andric } 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric /// Return true if the re-scheduling will put the given instruction too close 10230b57cec5SDimitry Andric /// to the defs of its register dependencies. 10240b57cec5SDimitry Andric bool TwoAddressInstructionPass::isDefTooClose(unsigned Reg, unsigned Dist, 10250b57cec5SDimitry Andric MachineInstr *MI) { 10260b57cec5SDimitry Andric for (MachineInstr &DefMI : MRI->def_instructions(Reg)) { 10270b57cec5SDimitry Andric if (DefMI.getParent() != MBB || DefMI.isCopy() || DefMI.isCopyLike()) 10280b57cec5SDimitry Andric continue; 10290b57cec5SDimitry Andric if (&DefMI == MI) 10300b57cec5SDimitry Andric return true; // MI is defining something KillMI uses 10310b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned>::iterator DDI = DistanceMap.find(&DefMI); 10320b57cec5SDimitry Andric if (DDI == DistanceMap.end()) 10330b57cec5SDimitry Andric return true; // Below MI 10340b57cec5SDimitry Andric unsigned DefDist = DDI->second; 10350b57cec5SDimitry Andric assert(Dist > DefDist && "Visited def already?"); 10360b57cec5SDimitry Andric if (TII->getInstrLatency(InstrItins, DefMI) > (Dist - DefDist)) 10370b57cec5SDimitry Andric return true; 10380b57cec5SDimitry Andric } 10390b57cec5SDimitry Andric return false; 10400b57cec5SDimitry Andric } 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric /// If there is one more local instruction that reads 'Reg' and it kills 'Reg, 10430b57cec5SDimitry Andric /// consider moving the kill instruction above the current two-address 10440b57cec5SDimitry Andric /// instruction in order to eliminate the need for the copy. 10450b57cec5SDimitry Andric bool TwoAddressInstructionPass:: 10460b57cec5SDimitry Andric rescheduleKillAboveMI(MachineBasicBlock::iterator &mi, 10470b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 10480b57cec5SDimitry Andric unsigned Reg) { 10490b57cec5SDimitry Andric // Bail immediately if we don't have LV or LIS available. We use them to find 10500b57cec5SDimitry Andric // kills efficiently. 10510b57cec5SDimitry Andric if (!LV && !LIS) 10520b57cec5SDimitry Andric return false; 10530b57cec5SDimitry Andric 10540b57cec5SDimitry Andric MachineInstr *MI = &*mi; 10550b57cec5SDimitry Andric DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI); 10560b57cec5SDimitry Andric if (DI == DistanceMap.end()) 10570b57cec5SDimitry Andric // Must be created from unfolded load. Don't waste time trying this. 10580b57cec5SDimitry Andric return false; 10590b57cec5SDimitry Andric 10600b57cec5SDimitry Andric MachineInstr *KillMI = nullptr; 10610b57cec5SDimitry Andric if (LIS) { 10620b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(Reg); 10630b57cec5SDimitry Andric assert(LI.end() != LI.begin() && 10640b57cec5SDimitry Andric "Reg should not have empty live interval."); 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric SlotIndex MBBEndIdx = LIS->getMBBEndIdx(MBB).getPrevSlot(); 10670b57cec5SDimitry Andric LiveInterval::const_iterator I = LI.find(MBBEndIdx); 10680b57cec5SDimitry Andric if (I != LI.end() && I->start < MBBEndIdx) 10690b57cec5SDimitry Andric return false; 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric --I; 10720b57cec5SDimitry Andric KillMI = LIS->getInstructionFromIndex(I->end); 10730b57cec5SDimitry Andric } else { 10740b57cec5SDimitry Andric KillMI = LV->getVarInfo(Reg).findKill(MBB); 10750b57cec5SDimitry Andric } 10760b57cec5SDimitry Andric if (!KillMI || MI == KillMI || KillMI->isCopy() || KillMI->isCopyLike()) 10770b57cec5SDimitry Andric // Don't mess with copies, they may be coalesced later. 10780b57cec5SDimitry Andric return false; 10790b57cec5SDimitry Andric 10800b57cec5SDimitry Andric unsigned DstReg; 10810b57cec5SDimitry Andric if (isTwoAddrUse(*KillMI, Reg, DstReg)) 10820b57cec5SDimitry Andric return false; 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric bool SeenStore = true; 10850b57cec5SDimitry Andric if (!KillMI->isSafeToMove(AA, SeenStore)) 10860b57cec5SDimitry Andric return false; 10870b57cec5SDimitry Andric 10880b57cec5SDimitry Andric SmallSet<unsigned, 2> Uses; 10890b57cec5SDimitry Andric SmallSet<unsigned, 2> Kills; 10900b57cec5SDimitry Andric SmallSet<unsigned, 2> Defs; 10910b57cec5SDimitry Andric SmallSet<unsigned, 2> LiveDefs; 10920b57cec5SDimitry Andric for (const MachineOperand &MO : KillMI->operands()) { 10930b57cec5SDimitry Andric if (!MO.isReg()) 10940b57cec5SDimitry Andric continue; 10958bcb0991SDimitry Andric Register MOReg = MO.getReg(); 10960b57cec5SDimitry Andric if (MO.isUse()) { 10970b57cec5SDimitry Andric if (!MOReg) 10980b57cec5SDimitry Andric continue; 10990b57cec5SDimitry Andric if (isDefTooClose(MOReg, DI->second, MI)) 11000b57cec5SDimitry Andric return false; 11010b57cec5SDimitry Andric bool isKill = MO.isKill() || (LIS && isPlainlyKilled(KillMI, MOReg, LIS)); 11020b57cec5SDimitry Andric if (MOReg == Reg && !isKill) 11030b57cec5SDimitry Andric return false; 11040b57cec5SDimitry Andric Uses.insert(MOReg); 11050b57cec5SDimitry Andric if (isKill && MOReg != Reg) 11060b57cec5SDimitry Andric Kills.insert(MOReg); 11078bcb0991SDimitry Andric } else if (Register::isPhysicalRegister(MOReg)) { 11080b57cec5SDimitry Andric Defs.insert(MOReg); 11090b57cec5SDimitry Andric if (!MO.isDead()) 11100b57cec5SDimitry Andric LiveDefs.insert(MOReg); 11110b57cec5SDimitry Andric } 11120b57cec5SDimitry Andric } 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric // Check if the reschedule will not break depedencies. 11150b57cec5SDimitry Andric unsigned NumVisited = 0; 11160b57cec5SDimitry Andric for (MachineInstr &OtherMI : 11170b57cec5SDimitry Andric make_range(mi, MachineBasicBlock::iterator(KillMI))) { 11180b57cec5SDimitry Andric // Debug instructions cannot be counted against the limit. 11190b57cec5SDimitry Andric if (OtherMI.isDebugInstr()) 11200b57cec5SDimitry Andric continue; 11210b57cec5SDimitry Andric if (NumVisited > 10) // FIXME: Arbitrary limit to reduce compile time cost. 11220b57cec5SDimitry Andric return false; 11230b57cec5SDimitry Andric ++NumVisited; 11240b57cec5SDimitry Andric if (OtherMI.hasUnmodeledSideEffects() || OtherMI.isCall() || 11250b57cec5SDimitry Andric OtherMI.isBranch() || OtherMI.isTerminator()) 11260b57cec5SDimitry Andric // Don't move pass calls, etc. 11270b57cec5SDimitry Andric return false; 11280b57cec5SDimitry Andric SmallVector<unsigned, 2> OtherDefs; 11290b57cec5SDimitry Andric for (const MachineOperand &MO : OtherMI.operands()) { 11300b57cec5SDimitry Andric if (!MO.isReg()) 11310b57cec5SDimitry Andric continue; 11328bcb0991SDimitry Andric Register MOReg = MO.getReg(); 11330b57cec5SDimitry Andric if (!MOReg) 11340b57cec5SDimitry Andric continue; 11350b57cec5SDimitry Andric if (MO.isUse()) { 11360b57cec5SDimitry Andric if (Defs.count(MOReg)) 11370b57cec5SDimitry Andric // Moving KillMI can clobber the physical register if the def has 11380b57cec5SDimitry Andric // not been seen. 11390b57cec5SDimitry Andric return false; 11400b57cec5SDimitry Andric if (Kills.count(MOReg)) 11410b57cec5SDimitry Andric // Don't want to extend other live ranges and update kills. 11420b57cec5SDimitry Andric return false; 11430b57cec5SDimitry Andric if (&OtherMI != MI && MOReg == Reg && 11440b57cec5SDimitry Andric !(MO.isKill() || (LIS && isPlainlyKilled(&OtherMI, MOReg, LIS)))) 11450b57cec5SDimitry Andric // We can't schedule across a use of the register in question. 11460b57cec5SDimitry Andric return false; 11470b57cec5SDimitry Andric } else { 11480b57cec5SDimitry Andric OtherDefs.push_back(MOReg); 11490b57cec5SDimitry Andric } 11500b57cec5SDimitry Andric } 11510b57cec5SDimitry Andric 11520b57cec5SDimitry Andric for (unsigned i = 0, e = OtherDefs.size(); i != e; ++i) { 11530b57cec5SDimitry Andric unsigned MOReg = OtherDefs[i]; 11540b57cec5SDimitry Andric if (Uses.count(MOReg)) 11550b57cec5SDimitry Andric return false; 11568bcb0991SDimitry Andric if (Register::isPhysicalRegister(MOReg) && LiveDefs.count(MOReg)) 11570b57cec5SDimitry Andric return false; 11580b57cec5SDimitry Andric // Physical register def is seen. 11590b57cec5SDimitry Andric Defs.erase(MOReg); 11600b57cec5SDimitry Andric } 11610b57cec5SDimitry Andric } 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric // Move the old kill above MI, don't forget to move debug info as well. 11640b57cec5SDimitry Andric MachineBasicBlock::iterator InsertPos = mi; 11650b57cec5SDimitry Andric while (InsertPos != MBB->begin() && std::prev(InsertPos)->isDebugInstr()) 11660b57cec5SDimitry Andric --InsertPos; 11670b57cec5SDimitry Andric MachineBasicBlock::iterator From = KillMI; 11680b57cec5SDimitry Andric MachineBasicBlock::iterator To = std::next(From); 11690b57cec5SDimitry Andric while (std::prev(From)->isDebugInstr()) 11700b57cec5SDimitry Andric --From; 11710b57cec5SDimitry Andric MBB->splice(InsertPos, MBB, From, To); 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric nmi = std::prev(InsertPos); // Backtrack so we process the moved instr. 11740b57cec5SDimitry Andric DistanceMap.erase(DI); 11750b57cec5SDimitry Andric 11760b57cec5SDimitry Andric // Update live variables 11770b57cec5SDimitry Andric if (LIS) { 11780b57cec5SDimitry Andric LIS->handleMove(*KillMI); 11790b57cec5SDimitry Andric } else { 11800b57cec5SDimitry Andric LV->removeVirtualRegisterKilled(Reg, *KillMI); 11810b57cec5SDimitry Andric LV->addVirtualRegisterKilled(Reg, *MI); 11820b57cec5SDimitry Andric } 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\trescheduled kill: " << *KillMI); 11850b57cec5SDimitry Andric return true; 11860b57cec5SDimitry Andric } 11870b57cec5SDimitry Andric 11880b57cec5SDimitry Andric /// Tries to commute the operand 'BaseOpIdx' and some other operand in the 11890b57cec5SDimitry Andric /// given machine instruction to improve opportunities for coalescing and 11900b57cec5SDimitry Andric /// elimination of a register to register copy. 11910b57cec5SDimitry Andric /// 11920b57cec5SDimitry Andric /// 'DstOpIdx' specifies the index of MI def operand. 11930b57cec5SDimitry Andric /// 'BaseOpKilled' specifies if the register associated with 'BaseOpIdx' 11940b57cec5SDimitry Andric /// operand is killed by the given instruction. 11950b57cec5SDimitry Andric /// The 'Dist' arguments provides the distance of MI from the start of the 11960b57cec5SDimitry Andric /// current basic block and it is used to determine if it is profitable 11970b57cec5SDimitry Andric /// to commute operands in the instruction. 11980b57cec5SDimitry Andric /// 11990b57cec5SDimitry Andric /// Returns true if the transformation happened. Otherwise, returns false. 12000b57cec5SDimitry Andric bool TwoAddressInstructionPass::tryInstructionCommute(MachineInstr *MI, 12010b57cec5SDimitry Andric unsigned DstOpIdx, 12020b57cec5SDimitry Andric unsigned BaseOpIdx, 12030b57cec5SDimitry Andric bool BaseOpKilled, 12040b57cec5SDimitry Andric unsigned Dist) { 12050b57cec5SDimitry Andric if (!MI->isCommutable()) 12060b57cec5SDimitry Andric return false; 12070b57cec5SDimitry Andric 12080b57cec5SDimitry Andric bool MadeChange = false; 12098bcb0991SDimitry Andric Register DstOpReg = MI->getOperand(DstOpIdx).getReg(); 12108bcb0991SDimitry Andric Register BaseOpReg = MI->getOperand(BaseOpIdx).getReg(); 12110b57cec5SDimitry Andric unsigned OpsNum = MI->getDesc().getNumOperands(); 12120b57cec5SDimitry Andric unsigned OtherOpIdx = MI->getDesc().getNumDefs(); 12130b57cec5SDimitry Andric for (; OtherOpIdx < OpsNum; OtherOpIdx++) { 12140b57cec5SDimitry Andric // The call of findCommutedOpIndices below only checks if BaseOpIdx 12150b57cec5SDimitry Andric // and OtherOpIdx are commutable, it does not really search for 12160b57cec5SDimitry Andric // other commutable operands and does not change the values of passed 12170b57cec5SDimitry Andric // variables. 12180b57cec5SDimitry Andric if (OtherOpIdx == BaseOpIdx || !MI->getOperand(OtherOpIdx).isReg() || 12190b57cec5SDimitry Andric !TII->findCommutedOpIndices(*MI, BaseOpIdx, OtherOpIdx)) 12200b57cec5SDimitry Andric continue; 12210b57cec5SDimitry Andric 12228bcb0991SDimitry Andric Register OtherOpReg = MI->getOperand(OtherOpIdx).getReg(); 12230b57cec5SDimitry Andric bool AggressiveCommute = false; 12240b57cec5SDimitry Andric 12250b57cec5SDimitry Andric // If OtherOp dies but BaseOp does not, swap the OtherOp and BaseOp 12260b57cec5SDimitry Andric // operands. This makes the live ranges of DstOp and OtherOp joinable. 12270b57cec5SDimitry Andric bool OtherOpKilled = isKilled(*MI, OtherOpReg, MRI, TII, LIS, false); 12280b57cec5SDimitry Andric bool DoCommute = !BaseOpKilled && OtherOpKilled; 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric if (!DoCommute && 12310b57cec5SDimitry Andric isProfitableToCommute(DstOpReg, BaseOpReg, OtherOpReg, MI, Dist)) { 12320b57cec5SDimitry Andric DoCommute = true; 12330b57cec5SDimitry Andric AggressiveCommute = true; 12340b57cec5SDimitry Andric } 12350b57cec5SDimitry Andric 12360b57cec5SDimitry Andric // If it's profitable to commute, try to do so. 12370b57cec5SDimitry Andric if (DoCommute && commuteInstruction(MI, DstOpIdx, BaseOpIdx, OtherOpIdx, 12380b57cec5SDimitry Andric Dist)) { 12390b57cec5SDimitry Andric MadeChange = true; 12400b57cec5SDimitry Andric ++NumCommuted; 12410b57cec5SDimitry Andric if (AggressiveCommute) { 12420b57cec5SDimitry Andric ++NumAggrCommuted; 12430b57cec5SDimitry Andric // There might be more than two commutable operands, update BaseOp and 12440b57cec5SDimitry Andric // continue scanning. 12450b57cec5SDimitry Andric // FIXME: This assumes that the new instruction's operands are in the 12460b57cec5SDimitry Andric // same positions and were simply swapped. 12470b57cec5SDimitry Andric BaseOpReg = OtherOpReg; 12480b57cec5SDimitry Andric BaseOpKilled = OtherOpKilled; 12490b57cec5SDimitry Andric // Resamples OpsNum in case the number of operands was reduced. This 12500b57cec5SDimitry Andric // happens with X86. 12510b57cec5SDimitry Andric OpsNum = MI->getDesc().getNumOperands(); 12520b57cec5SDimitry Andric continue; 12530b57cec5SDimitry Andric } 12540b57cec5SDimitry Andric // If this was a commute based on kill, we won't do better continuing. 12550b57cec5SDimitry Andric return MadeChange; 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric return MadeChange; 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric /// For the case where an instruction has a single pair of tied register 12620b57cec5SDimitry Andric /// operands, attempt some transformations that may either eliminate the tied 12630b57cec5SDimitry Andric /// operands or improve the opportunities for coalescing away the register copy. 12640b57cec5SDimitry Andric /// Returns true if no copy needs to be inserted to untie mi's operands 12650b57cec5SDimitry Andric /// (either because they were untied, or because mi was rescheduled, and will 12660b57cec5SDimitry Andric /// be visited again later). If the shouldOnlyCommute flag is true, only 12670b57cec5SDimitry Andric /// instruction commutation is attempted. 12680b57cec5SDimitry Andric bool TwoAddressInstructionPass:: 12690b57cec5SDimitry Andric tryInstructionTransform(MachineBasicBlock::iterator &mi, 12700b57cec5SDimitry Andric MachineBasicBlock::iterator &nmi, 12710b57cec5SDimitry Andric unsigned SrcIdx, unsigned DstIdx, 12720b57cec5SDimitry Andric unsigned Dist, bool shouldOnlyCommute) { 12730b57cec5SDimitry Andric if (OptLevel == CodeGenOpt::None) 12740b57cec5SDimitry Andric return false; 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric MachineInstr &MI = *mi; 12778bcb0991SDimitry Andric Register regA = MI.getOperand(DstIdx).getReg(); 12788bcb0991SDimitry Andric Register regB = MI.getOperand(SrcIdx).getReg(); 12790b57cec5SDimitry Andric 12808bcb0991SDimitry Andric assert(Register::isVirtualRegister(regB) && 12810b57cec5SDimitry Andric "cannot make instruction into two-address form"); 12820b57cec5SDimitry Andric bool regBKilled = isKilled(MI, regB, MRI, TII, LIS, true); 12830b57cec5SDimitry Andric 12848bcb0991SDimitry Andric if (Register::isVirtualRegister(regA)) 12850b57cec5SDimitry Andric scanUses(regA); 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric bool Commuted = tryInstructionCommute(&MI, DstIdx, SrcIdx, regBKilled, Dist); 12880b57cec5SDimitry Andric 12890b57cec5SDimitry Andric // If the instruction is convertible to 3 Addr, instead 1290*480093f4SDimitry Andric // of returning try 3 Addr transformation aggressively and 12910b57cec5SDimitry Andric // use this variable to check later. Because it might be better. 12920b57cec5SDimitry Andric // For example, we can just use `leal (%rsi,%rdi), %eax` and `ret` 12930b57cec5SDimitry Andric // instead of the following code. 12940b57cec5SDimitry Andric // addl %esi, %edi 12950b57cec5SDimitry Andric // movl %edi, %eax 12960b57cec5SDimitry Andric // ret 12970b57cec5SDimitry Andric if (Commuted && !MI.isConvertibleTo3Addr()) 12980b57cec5SDimitry Andric return false; 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric if (shouldOnlyCommute) 13010b57cec5SDimitry Andric return false; 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric // If there is one more use of regB later in the same MBB, consider 13040b57cec5SDimitry Andric // re-schedule this MI below it. 13050b57cec5SDimitry Andric if (!Commuted && EnableRescheduling && rescheduleMIBelowKill(mi, nmi, regB)) { 13060b57cec5SDimitry Andric ++NumReSchedDowns; 13070b57cec5SDimitry Andric return true; 13080b57cec5SDimitry Andric } 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric // If we commuted, regB may have changed so we should re-sample it to avoid 13110b57cec5SDimitry Andric // confusing the three address conversion below. 13120b57cec5SDimitry Andric if (Commuted) { 13130b57cec5SDimitry Andric regB = MI.getOperand(SrcIdx).getReg(); 13140b57cec5SDimitry Andric regBKilled = isKilled(MI, regB, MRI, TII, LIS, true); 13150b57cec5SDimitry Andric } 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric if (MI.isConvertibleTo3Addr()) { 13180b57cec5SDimitry Andric // This instruction is potentially convertible to a true 13190b57cec5SDimitry Andric // three-address instruction. Check if it is profitable. 13200b57cec5SDimitry Andric if (!regBKilled || isProfitableToConv3Addr(regA, regB)) { 13210b57cec5SDimitry Andric // Try to convert it. 13220b57cec5SDimitry Andric if (convertInstTo3Addr(mi, nmi, regA, regB, Dist)) { 13230b57cec5SDimitry Andric ++NumConvertedTo3Addr; 13240b57cec5SDimitry Andric return true; // Done with this instruction. 13250b57cec5SDimitry Andric } 13260b57cec5SDimitry Andric } 13270b57cec5SDimitry Andric } 13280b57cec5SDimitry Andric 13290b57cec5SDimitry Andric // Return if it is commuted but 3 addr conversion is failed. 13300b57cec5SDimitry Andric if (Commuted) 13310b57cec5SDimitry Andric return false; 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric // If there is one more use of regB later in the same MBB, consider 13340b57cec5SDimitry Andric // re-schedule it before this MI if it's legal. 13350b57cec5SDimitry Andric if (EnableRescheduling && rescheduleKillAboveMI(mi, nmi, regB)) { 13360b57cec5SDimitry Andric ++NumReSchedUps; 13370b57cec5SDimitry Andric return true; 13380b57cec5SDimitry Andric } 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric // If this is an instruction with a load folded into it, try unfolding 13410b57cec5SDimitry Andric // the load, e.g. avoid this: 13420b57cec5SDimitry Andric // movq %rdx, %rcx 13430b57cec5SDimitry Andric // addq (%rax), %rcx 13440b57cec5SDimitry Andric // in favor of this: 13450b57cec5SDimitry Andric // movq (%rax), %rcx 13460b57cec5SDimitry Andric // addq %rdx, %rcx 13470b57cec5SDimitry Andric // because it's preferable to schedule a load than a register copy. 13480b57cec5SDimitry Andric if (MI.mayLoad() && !regBKilled) { 13490b57cec5SDimitry Andric // Determine if a load can be unfolded. 13500b57cec5SDimitry Andric unsigned LoadRegIndex; 13510b57cec5SDimitry Andric unsigned NewOpc = 13520b57cec5SDimitry Andric TII->getOpcodeAfterMemoryUnfold(MI.getOpcode(), 13530b57cec5SDimitry Andric /*UnfoldLoad=*/true, 13540b57cec5SDimitry Andric /*UnfoldStore=*/false, 13550b57cec5SDimitry Andric &LoadRegIndex); 13560b57cec5SDimitry Andric if (NewOpc != 0) { 13570b57cec5SDimitry Andric const MCInstrDesc &UnfoldMCID = TII->get(NewOpc); 13580b57cec5SDimitry Andric if (UnfoldMCID.getNumDefs() == 1) { 13590b57cec5SDimitry Andric // Unfold the load. 13600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: UNFOLDING: " << MI); 13610b57cec5SDimitry Andric const TargetRegisterClass *RC = 13620b57cec5SDimitry Andric TRI->getAllocatableClass( 13630b57cec5SDimitry Andric TII->getRegClass(UnfoldMCID, LoadRegIndex, TRI, *MF)); 13648bcb0991SDimitry Andric Register Reg = MRI->createVirtualRegister(RC); 13650b57cec5SDimitry Andric SmallVector<MachineInstr *, 2> NewMIs; 13660b57cec5SDimitry Andric if (!TII->unfoldMemoryOperand(*MF, MI, Reg, 13670b57cec5SDimitry Andric /*UnfoldLoad=*/true, 13680b57cec5SDimitry Andric /*UnfoldStore=*/false, NewMIs)) { 13690b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n"); 13700b57cec5SDimitry Andric return false; 13710b57cec5SDimitry Andric } 13720b57cec5SDimitry Andric assert(NewMIs.size() == 2 && 13730b57cec5SDimitry Andric "Unfolded a load into multiple instructions!"); 13740b57cec5SDimitry Andric // The load was previously folded, so this is the only use. 13750b57cec5SDimitry Andric NewMIs[1]->addRegisterKilled(Reg, TRI); 13760b57cec5SDimitry Andric 13770b57cec5SDimitry Andric // Tentatively insert the instructions into the block so that they 13780b57cec5SDimitry Andric // look "normal" to the transformation logic. 13790b57cec5SDimitry Andric MBB->insert(mi, NewMIs[0]); 13800b57cec5SDimitry Andric MBB->insert(mi, NewMIs[1]); 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: NEW LOAD: " << *NewMIs[0] 13830b57cec5SDimitry Andric << "2addr: NEW INST: " << *NewMIs[1]); 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric // Transform the instruction, now that it no longer has a load. 13860b57cec5SDimitry Andric unsigned NewDstIdx = NewMIs[1]->findRegisterDefOperandIdx(regA); 13870b57cec5SDimitry Andric unsigned NewSrcIdx = NewMIs[1]->findRegisterUseOperandIdx(regB); 13880b57cec5SDimitry Andric MachineBasicBlock::iterator NewMI = NewMIs[1]; 13890b57cec5SDimitry Andric bool TransformResult = 13900b57cec5SDimitry Andric tryInstructionTransform(NewMI, mi, NewSrcIdx, NewDstIdx, Dist, true); 13910b57cec5SDimitry Andric (void)TransformResult; 13920b57cec5SDimitry Andric assert(!TransformResult && 13930b57cec5SDimitry Andric "tryInstructionTransform() should return false."); 13940b57cec5SDimitry Andric if (NewMIs[1]->getOperand(NewSrcIdx).isKill()) { 13950b57cec5SDimitry Andric // Success, or at least we made an improvement. Keep the unfolded 13960b57cec5SDimitry Andric // instructions and discard the original. 13970b57cec5SDimitry Andric if (LV) { 13980b57cec5SDimitry Andric for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 13990b57cec5SDimitry Andric MachineOperand &MO = MI.getOperand(i); 14008bcb0991SDimitry Andric if (MO.isReg() && Register::isVirtualRegister(MO.getReg())) { 14010b57cec5SDimitry Andric if (MO.isUse()) { 14020b57cec5SDimitry Andric if (MO.isKill()) { 14030b57cec5SDimitry Andric if (NewMIs[0]->killsRegister(MO.getReg())) 14040b57cec5SDimitry Andric LV->replaceKillInstruction(MO.getReg(), MI, *NewMIs[0]); 14050b57cec5SDimitry Andric else { 14060b57cec5SDimitry Andric assert(NewMIs[1]->killsRegister(MO.getReg()) && 14070b57cec5SDimitry Andric "Kill missing after load unfold!"); 14080b57cec5SDimitry Andric LV->replaceKillInstruction(MO.getReg(), MI, *NewMIs[1]); 14090b57cec5SDimitry Andric } 14100b57cec5SDimitry Andric } 14110b57cec5SDimitry Andric } else if (LV->removeVirtualRegisterDead(MO.getReg(), MI)) { 14120b57cec5SDimitry Andric if (NewMIs[1]->registerDefIsDead(MO.getReg())) 14130b57cec5SDimitry Andric LV->addVirtualRegisterDead(MO.getReg(), *NewMIs[1]); 14140b57cec5SDimitry Andric else { 14150b57cec5SDimitry Andric assert(NewMIs[0]->registerDefIsDead(MO.getReg()) && 14160b57cec5SDimitry Andric "Dead flag missing after load unfold!"); 14170b57cec5SDimitry Andric LV->addVirtualRegisterDead(MO.getReg(), *NewMIs[0]); 14180b57cec5SDimitry Andric } 14190b57cec5SDimitry Andric } 14200b57cec5SDimitry Andric } 14210b57cec5SDimitry Andric } 14220b57cec5SDimitry Andric LV->addVirtualRegisterKilled(Reg, *NewMIs[1]); 14230b57cec5SDimitry Andric } 14240b57cec5SDimitry Andric 14250b57cec5SDimitry Andric SmallVector<unsigned, 4> OrigRegs; 14260b57cec5SDimitry Andric if (LIS) { 14270b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 14280b57cec5SDimitry Andric if (MO.isReg()) 14290b57cec5SDimitry Andric OrigRegs.push_back(MO.getReg()); 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric MI.eraseFromParent(); 14340b57cec5SDimitry Andric 14350b57cec5SDimitry Andric // Update LiveIntervals. 14360b57cec5SDimitry Andric if (LIS) { 14370b57cec5SDimitry Andric MachineBasicBlock::iterator Begin(NewMIs[0]); 14380b57cec5SDimitry Andric MachineBasicBlock::iterator End(NewMIs[1]); 14390b57cec5SDimitry Andric LIS->repairIntervalsInRange(MBB, Begin, End, OrigRegs); 14400b57cec5SDimitry Andric } 14410b57cec5SDimitry Andric 14420b57cec5SDimitry Andric mi = NewMIs[1]; 14430b57cec5SDimitry Andric } else { 14440b57cec5SDimitry Andric // Transforming didn't eliminate the tie and didn't lead to an 14450b57cec5SDimitry Andric // improvement. Clean up the unfolded instructions and keep the 14460b57cec5SDimitry Andric // original. 14470b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "2addr: ABANDONING UNFOLD\n"); 14480b57cec5SDimitry Andric NewMIs[0]->eraseFromParent(); 14490b57cec5SDimitry Andric NewMIs[1]->eraseFromParent(); 14500b57cec5SDimitry Andric } 14510b57cec5SDimitry Andric } 14520b57cec5SDimitry Andric } 14530b57cec5SDimitry Andric } 14540b57cec5SDimitry Andric 14550b57cec5SDimitry Andric return false; 14560b57cec5SDimitry Andric } 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric // Collect tied operands of MI that need to be handled. 14590b57cec5SDimitry Andric // Rewrite trivial cases immediately. 14600b57cec5SDimitry Andric // Return true if any tied operands where found, including the trivial ones. 14610b57cec5SDimitry Andric bool TwoAddressInstructionPass:: 14620b57cec5SDimitry Andric collectTiedOperands(MachineInstr *MI, TiedOperandMap &TiedOperands) { 14630b57cec5SDimitry Andric const MCInstrDesc &MCID = MI->getDesc(); 14640b57cec5SDimitry Andric bool AnyOps = false; 14650b57cec5SDimitry Andric unsigned NumOps = MI->getNumOperands(); 14660b57cec5SDimitry Andric 14670b57cec5SDimitry Andric for (unsigned SrcIdx = 0; SrcIdx < NumOps; ++SrcIdx) { 14680b57cec5SDimitry Andric unsigned DstIdx = 0; 14690b57cec5SDimitry Andric if (!MI->isRegTiedToDefOperand(SrcIdx, &DstIdx)) 14700b57cec5SDimitry Andric continue; 14710b57cec5SDimitry Andric AnyOps = true; 14720b57cec5SDimitry Andric MachineOperand &SrcMO = MI->getOperand(SrcIdx); 14730b57cec5SDimitry Andric MachineOperand &DstMO = MI->getOperand(DstIdx); 14748bcb0991SDimitry Andric Register SrcReg = SrcMO.getReg(); 14758bcb0991SDimitry Andric Register DstReg = DstMO.getReg(); 14760b57cec5SDimitry Andric // Tied constraint already satisfied? 14770b57cec5SDimitry Andric if (SrcReg == DstReg) 14780b57cec5SDimitry Andric continue; 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric assert(SrcReg && SrcMO.isUse() && "two address instruction invalid"); 14810b57cec5SDimitry Andric 14820b57cec5SDimitry Andric // Deal with undef uses immediately - simply rewrite the src operand. 14830b57cec5SDimitry Andric if (SrcMO.isUndef() && !DstMO.getSubReg()) { 14840b57cec5SDimitry Andric // Constrain the DstReg register class if required. 14858bcb0991SDimitry Andric if (Register::isVirtualRegister(DstReg)) 14860b57cec5SDimitry Andric if (const TargetRegisterClass *RC = TII->getRegClass(MCID, SrcIdx, 14870b57cec5SDimitry Andric TRI, *MF)) 14880b57cec5SDimitry Andric MRI->constrainRegClass(DstReg, RC); 14890b57cec5SDimitry Andric SrcMO.setReg(DstReg); 14900b57cec5SDimitry Andric SrcMO.setSubReg(0); 14910b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\t\trewrite undef:\t" << *MI); 14920b57cec5SDimitry Andric continue; 14930b57cec5SDimitry Andric } 14940b57cec5SDimitry Andric TiedOperands[SrcReg].push_back(std::make_pair(SrcIdx, DstIdx)); 14950b57cec5SDimitry Andric } 14960b57cec5SDimitry Andric return AnyOps; 14970b57cec5SDimitry Andric } 14980b57cec5SDimitry Andric 14990b57cec5SDimitry Andric // Process a list of tied MI operands that all use the same source register. 15000b57cec5SDimitry Andric // The tied pairs are of the form (SrcIdx, DstIdx). 15010b57cec5SDimitry Andric void 15020b57cec5SDimitry Andric TwoAddressInstructionPass::processTiedPairs(MachineInstr *MI, 15030b57cec5SDimitry Andric TiedPairList &TiedPairs, 15040b57cec5SDimitry Andric unsigned &Dist) { 15050b57cec5SDimitry Andric bool IsEarlyClobber = false; 15060b57cec5SDimitry Andric for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) { 15070b57cec5SDimitry Andric const MachineOperand &DstMO = MI->getOperand(TiedPairs[tpi].second); 15080b57cec5SDimitry Andric IsEarlyClobber |= DstMO.isEarlyClobber(); 15090b57cec5SDimitry Andric } 15100b57cec5SDimitry Andric 15110b57cec5SDimitry Andric bool RemovedKillFlag = false; 15120b57cec5SDimitry Andric bool AllUsesCopied = true; 15130b57cec5SDimitry Andric unsigned LastCopiedReg = 0; 15140b57cec5SDimitry Andric SlotIndex LastCopyIdx; 15150b57cec5SDimitry Andric unsigned RegB = 0; 15160b57cec5SDimitry Andric unsigned SubRegB = 0; 15170b57cec5SDimitry Andric for (unsigned tpi = 0, tpe = TiedPairs.size(); tpi != tpe; ++tpi) { 15180b57cec5SDimitry Andric unsigned SrcIdx = TiedPairs[tpi].first; 15190b57cec5SDimitry Andric unsigned DstIdx = TiedPairs[tpi].second; 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric const MachineOperand &DstMO = MI->getOperand(DstIdx); 15228bcb0991SDimitry Andric Register RegA = DstMO.getReg(); 15230b57cec5SDimitry Andric 15240b57cec5SDimitry Andric // Grab RegB from the instruction because it may have changed if the 15250b57cec5SDimitry Andric // instruction was commuted. 15260b57cec5SDimitry Andric RegB = MI->getOperand(SrcIdx).getReg(); 15270b57cec5SDimitry Andric SubRegB = MI->getOperand(SrcIdx).getSubReg(); 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric if (RegA == RegB) { 15300b57cec5SDimitry Andric // The register is tied to multiple destinations (or else we would 15310b57cec5SDimitry Andric // not have continued this far), but this use of the register 15320b57cec5SDimitry Andric // already matches the tied destination. Leave it. 15330b57cec5SDimitry Andric AllUsesCopied = false; 15340b57cec5SDimitry Andric continue; 15350b57cec5SDimitry Andric } 15360b57cec5SDimitry Andric LastCopiedReg = RegA; 15370b57cec5SDimitry Andric 15388bcb0991SDimitry Andric assert(Register::isVirtualRegister(RegB) && 15390b57cec5SDimitry Andric "cannot make instruction into two-address form"); 15400b57cec5SDimitry Andric 15410b57cec5SDimitry Andric #ifndef NDEBUG 15420b57cec5SDimitry Andric // First, verify that we don't have a use of "a" in the instruction 15430b57cec5SDimitry Andric // (a = b + a for example) because our transformation will not 15440b57cec5SDimitry Andric // work. This should never occur because we are in SSA form. 15450b57cec5SDimitry Andric for (unsigned i = 0; i != MI->getNumOperands(); ++i) 15460b57cec5SDimitry Andric assert(i == DstIdx || 15470b57cec5SDimitry Andric !MI->getOperand(i).isReg() || 15480b57cec5SDimitry Andric MI->getOperand(i).getReg() != RegA); 15490b57cec5SDimitry Andric #endif 15500b57cec5SDimitry Andric 15510b57cec5SDimitry Andric // Emit a copy. 15520b57cec5SDimitry Andric MachineInstrBuilder MIB = BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), 15530b57cec5SDimitry Andric TII->get(TargetOpcode::COPY), RegA); 15540b57cec5SDimitry Andric // If this operand is folding a truncation, the truncation now moves to the 15550b57cec5SDimitry Andric // copy so that the register classes remain valid for the operands. 15560b57cec5SDimitry Andric MIB.addReg(RegB, 0, SubRegB); 15570b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClass(RegB); 15580b57cec5SDimitry Andric if (SubRegB) { 15598bcb0991SDimitry Andric if (Register::isVirtualRegister(RegA)) { 15600b57cec5SDimitry Andric assert(TRI->getMatchingSuperRegClass(RC, MRI->getRegClass(RegA), 15610b57cec5SDimitry Andric SubRegB) && 15620b57cec5SDimitry Andric "tied subregister must be a truncation"); 15630b57cec5SDimitry Andric // The superreg class will not be used to constrain the subreg class. 15640b57cec5SDimitry Andric RC = nullptr; 15658bcb0991SDimitry Andric } else { 15660b57cec5SDimitry Andric assert(TRI->getMatchingSuperReg(RegA, SubRegB, MRI->getRegClass(RegB)) 15670b57cec5SDimitry Andric && "tied subregister must be a truncation"); 15680b57cec5SDimitry Andric } 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric // Update DistanceMap. 15720b57cec5SDimitry Andric MachineBasicBlock::iterator PrevMI = MI; 15730b57cec5SDimitry Andric --PrevMI; 15740b57cec5SDimitry Andric DistanceMap.insert(std::make_pair(&*PrevMI, Dist)); 15750b57cec5SDimitry Andric DistanceMap[MI] = ++Dist; 15760b57cec5SDimitry Andric 15770b57cec5SDimitry Andric if (LIS) { 15780b57cec5SDimitry Andric LastCopyIdx = LIS->InsertMachineInstrInMaps(*PrevMI).getRegSlot(); 15790b57cec5SDimitry Andric 15808bcb0991SDimitry Andric if (Register::isVirtualRegister(RegA)) { 15810b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(RegA); 15820b57cec5SDimitry Andric VNInfo *VNI = LI.getNextValue(LastCopyIdx, LIS->getVNInfoAllocator()); 15830b57cec5SDimitry Andric SlotIndex endIdx = 15840b57cec5SDimitry Andric LIS->getInstructionIndex(*MI).getRegSlot(IsEarlyClobber); 15850b57cec5SDimitry Andric LI.addSegment(LiveInterval::Segment(LastCopyIdx, endIdx, VNI)); 15860b57cec5SDimitry Andric } 15870b57cec5SDimitry Andric } 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\t\tprepend:\t" << *MIB); 15900b57cec5SDimitry Andric 15910b57cec5SDimitry Andric MachineOperand &MO = MI->getOperand(SrcIdx); 15920b57cec5SDimitry Andric assert(MO.isReg() && MO.getReg() == RegB && MO.isUse() && 15930b57cec5SDimitry Andric "inconsistent operand info for 2-reg pass"); 15940b57cec5SDimitry Andric if (MO.isKill()) { 15950b57cec5SDimitry Andric MO.setIsKill(false); 15960b57cec5SDimitry Andric RemovedKillFlag = true; 15970b57cec5SDimitry Andric } 15980b57cec5SDimitry Andric 15990b57cec5SDimitry Andric // Make sure regA is a legal regclass for the SrcIdx operand. 16008bcb0991SDimitry Andric if (Register::isVirtualRegister(RegA) && Register::isVirtualRegister(RegB)) 16010b57cec5SDimitry Andric MRI->constrainRegClass(RegA, RC); 16020b57cec5SDimitry Andric MO.setReg(RegA); 16030b57cec5SDimitry Andric // The getMatchingSuper asserts guarantee that the register class projected 16040b57cec5SDimitry Andric // by SubRegB is compatible with RegA with no subregister. So regardless of 16050b57cec5SDimitry Andric // whether the dest oper writes a subreg, the source oper should not. 16060b57cec5SDimitry Andric MO.setSubReg(0); 16070b57cec5SDimitry Andric 16080b57cec5SDimitry Andric // Propagate SrcRegMap. 16090b57cec5SDimitry Andric SrcRegMap[RegA] = RegB; 16100b57cec5SDimitry Andric } 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric if (AllUsesCopied) { 16130b57cec5SDimitry Andric bool ReplacedAllUntiedUses = true; 16140b57cec5SDimitry Andric if (!IsEarlyClobber) { 16150b57cec5SDimitry Andric // Replace other (un-tied) uses of regB with LastCopiedReg. 16160b57cec5SDimitry Andric for (MachineOperand &MO : MI->operands()) { 16170b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) { 16180b57cec5SDimitry Andric if (MO.getSubReg() == SubRegB) { 16190b57cec5SDimitry Andric if (MO.isKill()) { 16200b57cec5SDimitry Andric MO.setIsKill(false); 16210b57cec5SDimitry Andric RemovedKillFlag = true; 16220b57cec5SDimitry Andric } 16230b57cec5SDimitry Andric MO.setReg(LastCopiedReg); 16240b57cec5SDimitry Andric MO.setSubReg(0); 16250b57cec5SDimitry Andric } else { 16260b57cec5SDimitry Andric ReplacedAllUntiedUses = false; 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric } 16290b57cec5SDimitry Andric } 16300b57cec5SDimitry Andric } 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andric // Update live variables for regB. 16330b57cec5SDimitry Andric if (RemovedKillFlag && ReplacedAllUntiedUses && 16340b57cec5SDimitry Andric LV && LV->getVarInfo(RegB).removeKill(*MI)) { 16350b57cec5SDimitry Andric MachineBasicBlock::iterator PrevMI = MI; 16360b57cec5SDimitry Andric --PrevMI; 16370b57cec5SDimitry Andric LV->addVirtualRegisterKilled(RegB, *PrevMI); 16380b57cec5SDimitry Andric } 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andric // Update LiveIntervals. 16410b57cec5SDimitry Andric if (LIS) { 16420b57cec5SDimitry Andric LiveInterval &LI = LIS->getInterval(RegB); 16430b57cec5SDimitry Andric SlotIndex MIIdx = LIS->getInstructionIndex(*MI); 16440b57cec5SDimitry Andric LiveInterval::const_iterator I = LI.find(MIIdx); 16450b57cec5SDimitry Andric assert(I != LI.end() && "RegB must be live-in to use."); 16460b57cec5SDimitry Andric 16470b57cec5SDimitry Andric SlotIndex UseIdx = MIIdx.getRegSlot(IsEarlyClobber); 16480b57cec5SDimitry Andric if (I->end == UseIdx) 16490b57cec5SDimitry Andric LI.removeSegment(LastCopyIdx, UseIdx); 16500b57cec5SDimitry Andric } 16510b57cec5SDimitry Andric } else if (RemovedKillFlag) { 16520b57cec5SDimitry Andric // Some tied uses of regB matched their destination registers, so 16530b57cec5SDimitry Andric // regB is still used in this instruction, but a kill flag was 16540b57cec5SDimitry Andric // removed from a different tied use of regB, so now we need to add 16550b57cec5SDimitry Andric // a kill flag to one of the remaining uses of regB. 16560b57cec5SDimitry Andric for (MachineOperand &MO : MI->operands()) { 16570b57cec5SDimitry Andric if (MO.isReg() && MO.getReg() == RegB && MO.isUse()) { 16580b57cec5SDimitry Andric MO.setIsKill(true); 16590b57cec5SDimitry Andric break; 16600b57cec5SDimitry Andric } 16610b57cec5SDimitry Andric } 16620b57cec5SDimitry Andric } 16630b57cec5SDimitry Andric } 16640b57cec5SDimitry Andric 16650b57cec5SDimitry Andric /// Reduce two-address instructions to two operands. 16660b57cec5SDimitry Andric bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &Func) { 16670b57cec5SDimitry Andric MF = &Func; 16680b57cec5SDimitry Andric const TargetMachine &TM = MF->getTarget(); 16690b57cec5SDimitry Andric MRI = &MF->getRegInfo(); 16700b57cec5SDimitry Andric TII = MF->getSubtarget().getInstrInfo(); 16710b57cec5SDimitry Andric TRI = MF->getSubtarget().getRegisterInfo(); 16720b57cec5SDimitry Andric InstrItins = MF->getSubtarget().getInstrItineraryData(); 16730b57cec5SDimitry Andric LV = getAnalysisIfAvailable<LiveVariables>(); 16740b57cec5SDimitry Andric LIS = getAnalysisIfAvailable<LiveIntervals>(); 16750b57cec5SDimitry Andric if (auto *AAPass = getAnalysisIfAvailable<AAResultsWrapperPass>()) 16760b57cec5SDimitry Andric AA = &AAPass->getAAResults(); 16770b57cec5SDimitry Andric else 16780b57cec5SDimitry Andric AA = nullptr; 16790b57cec5SDimitry Andric OptLevel = TM.getOptLevel(); 16800b57cec5SDimitry Andric // Disable optimizations if requested. We cannot skip the whole pass as some 16810b57cec5SDimitry Andric // fixups are necessary for correctness. 16820b57cec5SDimitry Andric if (skipFunction(Func.getFunction())) 16830b57cec5SDimitry Andric OptLevel = CodeGenOpt::None; 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric bool MadeChange = false; 16860b57cec5SDimitry Andric 16870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** REWRITING TWO-ADDR INSTRS **********\n"); 16880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "********** Function: " << MF->getName() << '\n'); 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric // This pass takes the function out of SSA form. 16910b57cec5SDimitry Andric MRI->leaveSSA(); 16920b57cec5SDimitry Andric 16930b57cec5SDimitry Andric TiedOperandMap TiedOperands; 16940b57cec5SDimitry Andric for (MachineFunction::iterator MBBI = MF->begin(), MBBE = MF->end(); 16950b57cec5SDimitry Andric MBBI != MBBE; ++MBBI) { 16960b57cec5SDimitry Andric MBB = &*MBBI; 16970b57cec5SDimitry Andric unsigned Dist = 0; 16980b57cec5SDimitry Andric DistanceMap.clear(); 16990b57cec5SDimitry Andric SrcRegMap.clear(); 17000b57cec5SDimitry Andric DstRegMap.clear(); 17010b57cec5SDimitry Andric Processed.clear(); 17020b57cec5SDimitry Andric SunkInstrs.clear(); 17030b57cec5SDimitry Andric for (MachineBasicBlock::iterator mi = MBB->begin(), me = MBB->end(); 17040b57cec5SDimitry Andric mi != me; ) { 17050b57cec5SDimitry Andric MachineBasicBlock::iterator nmi = std::next(mi); 17060b57cec5SDimitry Andric // Don't revisit an instruction previously converted by target. It may 17070b57cec5SDimitry Andric // contain undef register operands (%noreg), which are not handled. 17080b57cec5SDimitry Andric if (mi->isDebugInstr() || SunkInstrs.count(&*mi)) { 17090b57cec5SDimitry Andric mi = nmi; 17100b57cec5SDimitry Andric continue; 17110b57cec5SDimitry Andric } 17120b57cec5SDimitry Andric 17130b57cec5SDimitry Andric // Expand REG_SEQUENCE instructions. This will position mi at the first 17140b57cec5SDimitry Andric // expanded instruction. 17150b57cec5SDimitry Andric if (mi->isRegSequence()) 17160b57cec5SDimitry Andric eliminateRegSequence(mi); 17170b57cec5SDimitry Andric 17180b57cec5SDimitry Andric DistanceMap.insert(std::make_pair(&*mi, ++Dist)); 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric processCopy(&*mi); 17210b57cec5SDimitry Andric 17220b57cec5SDimitry Andric // First scan through all the tied register uses in this instruction 17230b57cec5SDimitry Andric // and record a list of pairs of tied operands for each register. 17240b57cec5SDimitry Andric if (!collectTiedOperands(&*mi, TiedOperands)) { 17250b57cec5SDimitry Andric mi = nmi; 17260b57cec5SDimitry Andric continue; 17270b57cec5SDimitry Andric } 17280b57cec5SDimitry Andric 17290b57cec5SDimitry Andric ++NumTwoAddressInstrs; 17300b57cec5SDimitry Andric MadeChange = true; 17310b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << '\t' << *mi); 17320b57cec5SDimitry Andric 17330b57cec5SDimitry Andric // If the instruction has a single pair of tied operands, try some 17340b57cec5SDimitry Andric // transformations that may either eliminate the tied operands or 17350b57cec5SDimitry Andric // improve the opportunities for coalescing away the register copy. 17360b57cec5SDimitry Andric if (TiedOperands.size() == 1) { 17370b57cec5SDimitry Andric SmallVectorImpl<std::pair<unsigned, unsigned>> &TiedPairs 17380b57cec5SDimitry Andric = TiedOperands.begin()->second; 17390b57cec5SDimitry Andric if (TiedPairs.size() == 1) { 17400b57cec5SDimitry Andric unsigned SrcIdx = TiedPairs[0].first; 17410b57cec5SDimitry Andric unsigned DstIdx = TiedPairs[0].second; 17428bcb0991SDimitry Andric Register SrcReg = mi->getOperand(SrcIdx).getReg(); 17438bcb0991SDimitry Andric Register DstReg = mi->getOperand(DstIdx).getReg(); 17440b57cec5SDimitry Andric if (SrcReg != DstReg && 17450b57cec5SDimitry Andric tryInstructionTransform(mi, nmi, SrcIdx, DstIdx, Dist, false)) { 17460b57cec5SDimitry Andric // The tied operands have been eliminated or shifted further down 17470b57cec5SDimitry Andric // the block to ease elimination. Continue processing with 'nmi'. 17480b57cec5SDimitry Andric TiedOperands.clear(); 17490b57cec5SDimitry Andric mi = nmi; 17500b57cec5SDimitry Andric continue; 17510b57cec5SDimitry Andric } 17520b57cec5SDimitry Andric } 17530b57cec5SDimitry Andric } 17540b57cec5SDimitry Andric 17550b57cec5SDimitry Andric // Now iterate over the information collected above. 17560b57cec5SDimitry Andric for (auto &TO : TiedOperands) { 17570b57cec5SDimitry Andric processTiedPairs(&*mi, TO.second, Dist); 17580b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\t\trewrite to:\t" << *mi); 17590b57cec5SDimitry Andric } 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric // Rewrite INSERT_SUBREG as COPY now that we no longer need SSA form. 17620b57cec5SDimitry Andric if (mi->isInsertSubreg()) { 17630b57cec5SDimitry Andric // From %reg = INSERT_SUBREG %reg, %subreg, subidx 17640b57cec5SDimitry Andric // To %reg:subidx = COPY %subreg 17650b57cec5SDimitry Andric unsigned SubIdx = mi->getOperand(3).getImm(); 17660b57cec5SDimitry Andric mi->RemoveOperand(3); 17670b57cec5SDimitry Andric assert(mi->getOperand(0).getSubReg() == 0 && "Unexpected subreg idx"); 17680b57cec5SDimitry Andric mi->getOperand(0).setSubReg(SubIdx); 17690b57cec5SDimitry Andric mi->getOperand(0).setIsUndef(mi->getOperand(1).isUndef()); 17700b57cec5SDimitry Andric mi->RemoveOperand(1); 17710b57cec5SDimitry Andric mi->setDesc(TII->get(TargetOpcode::COPY)); 17720b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\t\tconvert to:\t" << *mi); 17730b57cec5SDimitry Andric } 17740b57cec5SDimitry Andric 17750b57cec5SDimitry Andric // Clear TiedOperands here instead of at the top of the loop 17760b57cec5SDimitry Andric // since most instructions do not have tied operands. 17770b57cec5SDimitry Andric TiedOperands.clear(); 17780b57cec5SDimitry Andric mi = nmi; 17790b57cec5SDimitry Andric } 17800b57cec5SDimitry Andric } 17810b57cec5SDimitry Andric 17820b57cec5SDimitry Andric if (LIS) 17830b57cec5SDimitry Andric MF->verify(this, "After two-address instruction pass"); 17840b57cec5SDimitry Andric 17850b57cec5SDimitry Andric return MadeChange; 17860b57cec5SDimitry Andric } 17870b57cec5SDimitry Andric 17880b57cec5SDimitry Andric /// Eliminate a REG_SEQUENCE instruction as part of the de-ssa process. 17890b57cec5SDimitry Andric /// 17900b57cec5SDimitry Andric /// The instruction is turned into a sequence of sub-register copies: 17910b57cec5SDimitry Andric /// 17920b57cec5SDimitry Andric /// %dst = REG_SEQUENCE %v1, ssub0, %v2, ssub1 17930b57cec5SDimitry Andric /// 17940b57cec5SDimitry Andric /// Becomes: 17950b57cec5SDimitry Andric /// 17960b57cec5SDimitry Andric /// undef %dst:ssub0 = COPY %v1 17970b57cec5SDimitry Andric /// %dst:ssub1 = COPY %v2 17980b57cec5SDimitry Andric void TwoAddressInstructionPass:: 17990b57cec5SDimitry Andric eliminateRegSequence(MachineBasicBlock::iterator &MBBI) { 18000b57cec5SDimitry Andric MachineInstr &MI = *MBBI; 18018bcb0991SDimitry Andric Register DstReg = MI.getOperand(0).getReg(); 18028bcb0991SDimitry Andric if (MI.getOperand(0).getSubReg() || Register::isPhysicalRegister(DstReg) || 18030b57cec5SDimitry Andric !(MI.getNumOperands() & 1)) { 18040b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Illegal REG_SEQUENCE instruction:" << MI); 18050b57cec5SDimitry Andric llvm_unreachable(nullptr); 18060b57cec5SDimitry Andric } 18070b57cec5SDimitry Andric 18080b57cec5SDimitry Andric SmallVector<unsigned, 4> OrigRegs; 18090b57cec5SDimitry Andric if (LIS) { 18100b57cec5SDimitry Andric OrigRegs.push_back(MI.getOperand(0).getReg()); 18110b57cec5SDimitry Andric for (unsigned i = 1, e = MI.getNumOperands(); i < e; i += 2) 18120b57cec5SDimitry Andric OrigRegs.push_back(MI.getOperand(i).getReg()); 18130b57cec5SDimitry Andric } 18140b57cec5SDimitry Andric 18150b57cec5SDimitry Andric bool DefEmitted = false; 18160b57cec5SDimitry Andric for (unsigned i = 1, e = MI.getNumOperands(); i < e; i += 2) { 18170b57cec5SDimitry Andric MachineOperand &UseMO = MI.getOperand(i); 18188bcb0991SDimitry Andric Register SrcReg = UseMO.getReg(); 18190b57cec5SDimitry Andric unsigned SubIdx = MI.getOperand(i+1).getImm(); 18200b57cec5SDimitry Andric // Nothing needs to be inserted for undef operands. 18210b57cec5SDimitry Andric if (UseMO.isUndef()) 18220b57cec5SDimitry Andric continue; 18230b57cec5SDimitry Andric 18240b57cec5SDimitry Andric // Defer any kill flag to the last operand using SrcReg. Otherwise, we 18250b57cec5SDimitry Andric // might insert a COPY that uses SrcReg after is was killed. 18260b57cec5SDimitry Andric bool isKill = UseMO.isKill(); 18270b57cec5SDimitry Andric if (isKill) 18280b57cec5SDimitry Andric for (unsigned j = i + 2; j < e; j += 2) 18290b57cec5SDimitry Andric if (MI.getOperand(j).getReg() == SrcReg) { 18300b57cec5SDimitry Andric MI.getOperand(j).setIsKill(); 18310b57cec5SDimitry Andric UseMO.setIsKill(false); 18320b57cec5SDimitry Andric isKill = false; 18330b57cec5SDimitry Andric break; 18340b57cec5SDimitry Andric } 18350b57cec5SDimitry Andric 18360b57cec5SDimitry Andric // Insert the sub-register copy. 18370b57cec5SDimitry Andric MachineInstr *CopyMI = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), 18380b57cec5SDimitry Andric TII->get(TargetOpcode::COPY)) 18390b57cec5SDimitry Andric .addReg(DstReg, RegState::Define, SubIdx) 18400b57cec5SDimitry Andric .add(UseMO); 18410b57cec5SDimitry Andric 18420b57cec5SDimitry Andric // The first def needs an undef flag because there is no live register 18430b57cec5SDimitry Andric // before it. 18440b57cec5SDimitry Andric if (!DefEmitted) { 18450b57cec5SDimitry Andric CopyMI->getOperand(0).setIsUndef(true); 18460b57cec5SDimitry Andric // Return an iterator pointing to the first inserted instr. 18470b57cec5SDimitry Andric MBBI = CopyMI; 18480b57cec5SDimitry Andric } 18490b57cec5SDimitry Andric DefEmitted = true; 18500b57cec5SDimitry Andric 18510b57cec5SDimitry Andric // Update LiveVariables' kill info. 18528bcb0991SDimitry Andric if (LV && isKill && !Register::isPhysicalRegister(SrcReg)) 18530b57cec5SDimitry Andric LV->replaceKillInstruction(SrcReg, MI, *CopyMI); 18540b57cec5SDimitry Andric 18550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Inserted: " << *CopyMI); 18560b57cec5SDimitry Andric } 18570b57cec5SDimitry Andric 18580b57cec5SDimitry Andric MachineBasicBlock::iterator EndMBBI = 18590b57cec5SDimitry Andric std::next(MachineBasicBlock::iterator(MI)); 18600b57cec5SDimitry Andric 18610b57cec5SDimitry Andric if (!DefEmitted) { 18620b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Turned: " << MI << " into an IMPLICIT_DEF"); 18630b57cec5SDimitry Andric MI.setDesc(TII->get(TargetOpcode::IMPLICIT_DEF)); 18640b57cec5SDimitry Andric for (int j = MI.getNumOperands() - 1, ee = 0; j > ee; --j) 18650b57cec5SDimitry Andric MI.RemoveOperand(j); 18660b57cec5SDimitry Andric } else { 18670b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Eliminated: " << MI); 18680b57cec5SDimitry Andric MI.eraseFromParent(); 18690b57cec5SDimitry Andric } 18700b57cec5SDimitry Andric 18710b57cec5SDimitry Andric // Udpate LiveIntervals. 18720b57cec5SDimitry Andric if (LIS) 18730b57cec5SDimitry Andric LIS->repairIntervalsInRange(MBB, MBBI, EndMBBI, OrigRegs); 18740b57cec5SDimitry Andric } 1875