1*f4a2713aSLionel Sambuc //===- RegisterCoalescer.cpp - Generic Register Coalescing Interface -------==// 2*f4a2713aSLionel Sambuc // 3*f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4*f4a2713aSLionel Sambuc // 5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7*f4a2713aSLionel Sambuc // 8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9*f4a2713aSLionel Sambuc // 10*f4a2713aSLionel Sambuc // This file implements the generic RegisterCoalescer interface which 11*f4a2713aSLionel Sambuc // is used as the common interface used by all clients and 12*f4a2713aSLionel Sambuc // implementations of register coalescing. 13*f4a2713aSLionel Sambuc // 14*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 15*f4a2713aSLionel Sambuc 16*f4a2713aSLionel Sambuc #define DEBUG_TYPE "regalloc" 17*f4a2713aSLionel Sambuc #include "RegisterCoalescer.h" 18*f4a2713aSLionel Sambuc #include "llvm/ADT/OwningPtr.h" 19*f4a2713aSLionel Sambuc #include "llvm/ADT/STLExtras.h" 20*f4a2713aSLionel Sambuc #include "llvm/ADT/SmallSet.h" 21*f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h" 22*f4a2713aSLionel Sambuc #include "llvm/Analysis/AliasAnalysis.h" 23*f4a2713aSLionel Sambuc #include "llvm/CodeGen/LiveIntervalAnalysis.h" 24*f4a2713aSLionel Sambuc #include "llvm/CodeGen/LiveRangeEdit.h" 25*f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFrameInfo.h" 26*f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstr.h" 27*f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineLoopInfo.h" 28*f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineRegisterInfo.h" 29*f4a2713aSLionel Sambuc #include "llvm/CodeGen/Passes.h" 30*f4a2713aSLionel Sambuc #include "llvm/CodeGen/RegisterClassInfo.h" 31*f4a2713aSLionel Sambuc #include "llvm/CodeGen/VirtRegMap.h" 32*f4a2713aSLionel Sambuc #include "llvm/IR/Value.h" 33*f4a2713aSLionel Sambuc #include "llvm/Pass.h" 34*f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h" 35*f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h" 36*f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h" 37*f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h" 38*f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h" 39*f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h" 40*f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h" 41*f4a2713aSLionel Sambuc #include "llvm/Target/TargetSubtargetInfo.h" 42*f4a2713aSLionel Sambuc #include <algorithm> 43*f4a2713aSLionel Sambuc #include <cmath> 44*f4a2713aSLionel Sambuc using namespace llvm; 45*f4a2713aSLionel Sambuc 46*f4a2713aSLionel Sambuc STATISTIC(numJoins , "Number of interval joins performed"); 47*f4a2713aSLionel Sambuc STATISTIC(numCrossRCs , "Number of cross class joins performed"); 48*f4a2713aSLionel Sambuc STATISTIC(numCommutes , "Number of instruction commuting performed"); 49*f4a2713aSLionel Sambuc STATISTIC(numExtends , "Number of copies extended"); 50*f4a2713aSLionel Sambuc STATISTIC(NumReMats , "Number of instructions re-materialized"); 51*f4a2713aSLionel Sambuc STATISTIC(NumInflated , "Number of register classes inflated"); 52*f4a2713aSLionel Sambuc STATISTIC(NumLaneConflicts, "Number of dead lane conflicts tested"); 53*f4a2713aSLionel Sambuc STATISTIC(NumLaneResolves, "Number of dead lane conflicts resolved"); 54*f4a2713aSLionel Sambuc 55*f4a2713aSLionel Sambuc static cl::opt<bool> 56*f4a2713aSLionel Sambuc EnableJoining("join-liveintervals", 57*f4a2713aSLionel Sambuc cl::desc("Coalesce copies (default=true)"), 58*f4a2713aSLionel Sambuc cl::init(true)); 59*f4a2713aSLionel Sambuc 60*f4a2713aSLionel Sambuc // Temporary flag to test critical edge unsplitting. 61*f4a2713aSLionel Sambuc static cl::opt<bool> 62*f4a2713aSLionel Sambuc EnableJoinSplits("join-splitedges", 63*f4a2713aSLionel Sambuc cl::desc("Coalesce copies on split edges (default=subtarget)"), cl::Hidden); 64*f4a2713aSLionel Sambuc 65*f4a2713aSLionel Sambuc // Temporary flag to test global copy optimization. 66*f4a2713aSLionel Sambuc static cl::opt<cl::boolOrDefault> 67*f4a2713aSLionel Sambuc EnableGlobalCopies("join-globalcopies", 68*f4a2713aSLionel Sambuc cl::desc("Coalesce copies that span blocks (default=subtarget)"), 69*f4a2713aSLionel Sambuc cl::init(cl::BOU_UNSET), cl::Hidden); 70*f4a2713aSLionel Sambuc 71*f4a2713aSLionel Sambuc static cl::opt<bool> 72*f4a2713aSLionel Sambuc VerifyCoalescing("verify-coalescing", 73*f4a2713aSLionel Sambuc cl::desc("Verify machine instrs before and after register coalescing"), 74*f4a2713aSLionel Sambuc cl::Hidden); 75*f4a2713aSLionel Sambuc 76*f4a2713aSLionel Sambuc namespace { 77*f4a2713aSLionel Sambuc class RegisterCoalescer : public MachineFunctionPass, 78*f4a2713aSLionel Sambuc private LiveRangeEdit::Delegate { 79*f4a2713aSLionel Sambuc MachineFunction* MF; 80*f4a2713aSLionel Sambuc MachineRegisterInfo* MRI; 81*f4a2713aSLionel Sambuc const TargetMachine* TM; 82*f4a2713aSLionel Sambuc const TargetRegisterInfo* TRI; 83*f4a2713aSLionel Sambuc const TargetInstrInfo* TII; 84*f4a2713aSLionel Sambuc LiveIntervals *LIS; 85*f4a2713aSLionel Sambuc const MachineLoopInfo* Loops; 86*f4a2713aSLionel Sambuc AliasAnalysis *AA; 87*f4a2713aSLionel Sambuc RegisterClassInfo RegClassInfo; 88*f4a2713aSLionel Sambuc 89*f4a2713aSLionel Sambuc /// \brief True if the coalescer should aggressively coalesce global copies 90*f4a2713aSLionel Sambuc /// in favor of keeping local copies. 91*f4a2713aSLionel Sambuc bool JoinGlobalCopies; 92*f4a2713aSLionel Sambuc 93*f4a2713aSLionel Sambuc /// \brief True if the coalescer should aggressively coalesce fall-thru 94*f4a2713aSLionel Sambuc /// blocks exclusively containing copies. 95*f4a2713aSLionel Sambuc bool JoinSplitEdges; 96*f4a2713aSLionel Sambuc 97*f4a2713aSLionel Sambuc /// WorkList - Copy instructions yet to be coalesced. 98*f4a2713aSLionel Sambuc SmallVector<MachineInstr*, 8> WorkList; 99*f4a2713aSLionel Sambuc SmallVector<MachineInstr*, 8> LocalWorkList; 100*f4a2713aSLionel Sambuc 101*f4a2713aSLionel Sambuc /// ErasedInstrs - Set of instruction pointers that have been erased, and 102*f4a2713aSLionel Sambuc /// that may be present in WorkList. 103*f4a2713aSLionel Sambuc SmallPtrSet<MachineInstr*, 8> ErasedInstrs; 104*f4a2713aSLionel Sambuc 105*f4a2713aSLionel Sambuc /// Dead instructions that are about to be deleted. 106*f4a2713aSLionel Sambuc SmallVector<MachineInstr*, 8> DeadDefs; 107*f4a2713aSLionel Sambuc 108*f4a2713aSLionel Sambuc /// Virtual registers to be considered for register class inflation. 109*f4a2713aSLionel Sambuc SmallVector<unsigned, 8> InflateRegs; 110*f4a2713aSLionel Sambuc 111*f4a2713aSLionel Sambuc /// Recursively eliminate dead defs in DeadDefs. 112*f4a2713aSLionel Sambuc void eliminateDeadDefs(); 113*f4a2713aSLionel Sambuc 114*f4a2713aSLionel Sambuc /// LiveRangeEdit callback. 115*f4a2713aSLionel Sambuc void LRE_WillEraseInstruction(MachineInstr *MI); 116*f4a2713aSLionel Sambuc 117*f4a2713aSLionel Sambuc /// coalesceLocals - coalesce the LocalWorkList. 118*f4a2713aSLionel Sambuc void coalesceLocals(); 119*f4a2713aSLionel Sambuc 120*f4a2713aSLionel Sambuc /// joinAllIntervals - join compatible live intervals 121*f4a2713aSLionel Sambuc void joinAllIntervals(); 122*f4a2713aSLionel Sambuc 123*f4a2713aSLionel Sambuc /// copyCoalesceInMBB - Coalesce copies in the specified MBB, putting 124*f4a2713aSLionel Sambuc /// copies that cannot yet be coalesced into WorkList. 125*f4a2713aSLionel Sambuc void copyCoalesceInMBB(MachineBasicBlock *MBB); 126*f4a2713aSLionel Sambuc 127*f4a2713aSLionel Sambuc /// copyCoalesceWorkList - Try to coalesce all copies in CurrList. Return 128*f4a2713aSLionel Sambuc /// true if any progress was made. 129*f4a2713aSLionel Sambuc bool copyCoalesceWorkList(MutableArrayRef<MachineInstr*> CurrList); 130*f4a2713aSLionel Sambuc 131*f4a2713aSLionel Sambuc /// joinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, 132*f4a2713aSLionel Sambuc /// which are the src/dst of the copy instruction CopyMI. This returns 133*f4a2713aSLionel Sambuc /// true if the copy was successfully coalesced away. If it is not 134*f4a2713aSLionel Sambuc /// currently possible to coalesce this interval, but it may be possible if 135*f4a2713aSLionel Sambuc /// other things get coalesced, then it returns true by reference in 136*f4a2713aSLionel Sambuc /// 'Again'. 137*f4a2713aSLionel Sambuc bool joinCopy(MachineInstr *TheCopy, bool &Again); 138*f4a2713aSLionel Sambuc 139*f4a2713aSLionel Sambuc /// joinIntervals - Attempt to join these two intervals. On failure, this 140*f4a2713aSLionel Sambuc /// returns false. The output "SrcInt" will not have been modified, so we 141*f4a2713aSLionel Sambuc /// can use this information below to update aliases. 142*f4a2713aSLionel Sambuc bool joinIntervals(CoalescerPair &CP); 143*f4a2713aSLionel Sambuc 144*f4a2713aSLionel Sambuc /// Attempt joining two virtual registers. Return true on success. 145*f4a2713aSLionel Sambuc bool joinVirtRegs(CoalescerPair &CP); 146*f4a2713aSLionel Sambuc 147*f4a2713aSLionel Sambuc /// Attempt joining with a reserved physreg. 148*f4a2713aSLionel Sambuc bool joinReservedPhysReg(CoalescerPair &CP); 149*f4a2713aSLionel Sambuc 150*f4a2713aSLionel Sambuc /// adjustCopiesBackFrom - We found a non-trivially-coalescable copy. If 151*f4a2713aSLionel Sambuc /// the source value number is defined by a copy from the destination reg 152*f4a2713aSLionel Sambuc /// see if we can merge these two destination reg valno# into a single 153*f4a2713aSLionel Sambuc /// value number, eliminating a copy. 154*f4a2713aSLionel Sambuc bool adjustCopiesBackFrom(const CoalescerPair &CP, MachineInstr *CopyMI); 155*f4a2713aSLionel Sambuc 156*f4a2713aSLionel Sambuc /// hasOtherReachingDefs - Return true if there are definitions of IntB 157*f4a2713aSLionel Sambuc /// other than BValNo val# that can reach uses of AValno val# of IntA. 158*f4a2713aSLionel Sambuc bool hasOtherReachingDefs(LiveInterval &IntA, LiveInterval &IntB, 159*f4a2713aSLionel Sambuc VNInfo *AValNo, VNInfo *BValNo); 160*f4a2713aSLionel Sambuc 161*f4a2713aSLionel Sambuc /// removeCopyByCommutingDef - We found a non-trivially-coalescable copy. 162*f4a2713aSLionel Sambuc /// If the source value number is defined by a commutable instruction and 163*f4a2713aSLionel Sambuc /// its other operand is coalesced to the copy dest register, see if we 164*f4a2713aSLionel Sambuc /// can transform the copy into a noop by commuting the definition. 165*f4a2713aSLionel Sambuc bool removeCopyByCommutingDef(const CoalescerPair &CP,MachineInstr *CopyMI); 166*f4a2713aSLionel Sambuc 167*f4a2713aSLionel Sambuc /// reMaterializeTrivialDef - If the source of a copy is defined by a 168*f4a2713aSLionel Sambuc /// trivial computation, replace the copy by rematerialize the definition. 169*f4a2713aSLionel Sambuc bool reMaterializeTrivialDef(CoalescerPair &CP, MachineInstr *CopyMI, 170*f4a2713aSLionel Sambuc bool &IsDefCopy); 171*f4a2713aSLionel Sambuc 172*f4a2713aSLionel Sambuc /// canJoinPhys - Return true if a physreg copy should be joined. 173*f4a2713aSLionel Sambuc bool canJoinPhys(const CoalescerPair &CP); 174*f4a2713aSLionel Sambuc 175*f4a2713aSLionel Sambuc /// updateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and 176*f4a2713aSLionel Sambuc /// update the subregister number if it is not zero. If DstReg is a 177*f4a2713aSLionel Sambuc /// physical register and the existing subregister number of the def / use 178*f4a2713aSLionel Sambuc /// being updated is not zero, make sure to set it to the correct physical 179*f4a2713aSLionel Sambuc /// subregister. 180*f4a2713aSLionel Sambuc void updateRegDefsUses(unsigned SrcReg, unsigned DstReg, unsigned SubIdx); 181*f4a2713aSLionel Sambuc 182*f4a2713aSLionel Sambuc /// eliminateUndefCopy - Handle copies of undef values. 183*f4a2713aSLionel Sambuc bool eliminateUndefCopy(MachineInstr *CopyMI, const CoalescerPair &CP); 184*f4a2713aSLionel Sambuc 185*f4a2713aSLionel Sambuc public: 186*f4a2713aSLionel Sambuc static char ID; // Class identification, replacement for typeinfo 187*f4a2713aSLionel Sambuc RegisterCoalescer() : MachineFunctionPass(ID) { 188*f4a2713aSLionel Sambuc initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); 189*f4a2713aSLionel Sambuc } 190*f4a2713aSLionel Sambuc 191*f4a2713aSLionel Sambuc virtual void getAnalysisUsage(AnalysisUsage &AU) const; 192*f4a2713aSLionel Sambuc 193*f4a2713aSLionel Sambuc virtual void releaseMemory(); 194*f4a2713aSLionel Sambuc 195*f4a2713aSLionel Sambuc /// runOnMachineFunction - pass entry point 196*f4a2713aSLionel Sambuc virtual bool runOnMachineFunction(MachineFunction&); 197*f4a2713aSLionel Sambuc 198*f4a2713aSLionel Sambuc /// print - Implement the dump method. 199*f4a2713aSLionel Sambuc virtual void print(raw_ostream &O, const Module* = 0) const; 200*f4a2713aSLionel Sambuc }; 201*f4a2713aSLionel Sambuc } /// end anonymous namespace 202*f4a2713aSLionel Sambuc 203*f4a2713aSLionel Sambuc char &llvm::RegisterCoalescerID = RegisterCoalescer::ID; 204*f4a2713aSLionel Sambuc 205*f4a2713aSLionel Sambuc INITIALIZE_PASS_BEGIN(RegisterCoalescer, "simple-register-coalescing", 206*f4a2713aSLionel Sambuc "Simple Register Coalescing", false, false) 207*f4a2713aSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 208*f4a2713aSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 209*f4a2713aSLionel Sambuc INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) 210*f4a2713aSLionel Sambuc INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 211*f4a2713aSLionel Sambuc INITIALIZE_PASS_END(RegisterCoalescer, "simple-register-coalescing", 212*f4a2713aSLionel Sambuc "Simple Register Coalescing", false, false) 213*f4a2713aSLionel Sambuc 214*f4a2713aSLionel Sambuc char RegisterCoalescer::ID = 0; 215*f4a2713aSLionel Sambuc 216*f4a2713aSLionel Sambuc static bool isMoveInstr(const TargetRegisterInfo &tri, const MachineInstr *MI, 217*f4a2713aSLionel Sambuc unsigned &Src, unsigned &Dst, 218*f4a2713aSLionel Sambuc unsigned &SrcSub, unsigned &DstSub) { 219*f4a2713aSLionel Sambuc if (MI->isCopy()) { 220*f4a2713aSLionel Sambuc Dst = MI->getOperand(0).getReg(); 221*f4a2713aSLionel Sambuc DstSub = MI->getOperand(0).getSubReg(); 222*f4a2713aSLionel Sambuc Src = MI->getOperand(1).getReg(); 223*f4a2713aSLionel Sambuc SrcSub = MI->getOperand(1).getSubReg(); 224*f4a2713aSLionel Sambuc } else if (MI->isSubregToReg()) { 225*f4a2713aSLionel Sambuc Dst = MI->getOperand(0).getReg(); 226*f4a2713aSLionel Sambuc DstSub = tri.composeSubRegIndices(MI->getOperand(0).getSubReg(), 227*f4a2713aSLionel Sambuc MI->getOperand(3).getImm()); 228*f4a2713aSLionel Sambuc Src = MI->getOperand(2).getReg(); 229*f4a2713aSLionel Sambuc SrcSub = MI->getOperand(2).getSubReg(); 230*f4a2713aSLionel Sambuc } else 231*f4a2713aSLionel Sambuc return false; 232*f4a2713aSLionel Sambuc return true; 233*f4a2713aSLionel Sambuc } 234*f4a2713aSLionel Sambuc 235*f4a2713aSLionel Sambuc // Return true if this block should be vacated by the coalescer to eliminate 236*f4a2713aSLionel Sambuc // branches. The important cases to handle in the coalescer are critical edges 237*f4a2713aSLionel Sambuc // split during phi elimination which contain only copies. Simple blocks that 238*f4a2713aSLionel Sambuc // contain non-branches should also be vacated, but this can be handled by an 239*f4a2713aSLionel Sambuc // earlier pass similar to early if-conversion. 240*f4a2713aSLionel Sambuc static bool isSplitEdge(const MachineBasicBlock *MBB) { 241*f4a2713aSLionel Sambuc if (MBB->pred_size() != 1 || MBB->succ_size() != 1) 242*f4a2713aSLionel Sambuc return false; 243*f4a2713aSLionel Sambuc 244*f4a2713aSLionel Sambuc for (MachineBasicBlock::const_iterator MII = MBB->begin(), E = MBB->end(); 245*f4a2713aSLionel Sambuc MII != E; ++MII) { 246*f4a2713aSLionel Sambuc if (!MII->isCopyLike() && !MII->isUnconditionalBranch()) 247*f4a2713aSLionel Sambuc return false; 248*f4a2713aSLionel Sambuc } 249*f4a2713aSLionel Sambuc return true; 250*f4a2713aSLionel Sambuc } 251*f4a2713aSLionel Sambuc 252*f4a2713aSLionel Sambuc bool CoalescerPair::setRegisters(const MachineInstr *MI) { 253*f4a2713aSLionel Sambuc SrcReg = DstReg = 0; 254*f4a2713aSLionel Sambuc SrcIdx = DstIdx = 0; 255*f4a2713aSLionel Sambuc NewRC = 0; 256*f4a2713aSLionel Sambuc Flipped = CrossClass = false; 257*f4a2713aSLionel Sambuc 258*f4a2713aSLionel Sambuc unsigned Src, Dst, SrcSub, DstSub; 259*f4a2713aSLionel Sambuc if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub)) 260*f4a2713aSLionel Sambuc return false; 261*f4a2713aSLionel Sambuc Partial = SrcSub || DstSub; 262*f4a2713aSLionel Sambuc 263*f4a2713aSLionel Sambuc // If one register is a physreg, it must be Dst. 264*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Src)) { 265*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Dst)) 266*f4a2713aSLionel Sambuc return false; 267*f4a2713aSLionel Sambuc std::swap(Src, Dst); 268*f4a2713aSLionel Sambuc std::swap(SrcSub, DstSub); 269*f4a2713aSLionel Sambuc Flipped = true; 270*f4a2713aSLionel Sambuc } 271*f4a2713aSLionel Sambuc 272*f4a2713aSLionel Sambuc const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); 273*f4a2713aSLionel Sambuc 274*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(Dst)) { 275*f4a2713aSLionel Sambuc // Eliminate DstSub on a physreg. 276*f4a2713aSLionel Sambuc if (DstSub) { 277*f4a2713aSLionel Sambuc Dst = TRI.getSubReg(Dst, DstSub); 278*f4a2713aSLionel Sambuc if (!Dst) return false; 279*f4a2713aSLionel Sambuc DstSub = 0; 280*f4a2713aSLionel Sambuc } 281*f4a2713aSLionel Sambuc 282*f4a2713aSLionel Sambuc // Eliminate SrcSub by picking a corresponding Dst superregister. 283*f4a2713aSLionel Sambuc if (SrcSub) { 284*f4a2713aSLionel Sambuc Dst = TRI.getMatchingSuperReg(Dst, SrcSub, MRI.getRegClass(Src)); 285*f4a2713aSLionel Sambuc if (!Dst) return false; 286*f4a2713aSLionel Sambuc SrcSub = 0; 287*f4a2713aSLionel Sambuc } else if (!MRI.getRegClass(Src)->contains(Dst)) { 288*f4a2713aSLionel Sambuc return false; 289*f4a2713aSLionel Sambuc } 290*f4a2713aSLionel Sambuc } else { 291*f4a2713aSLionel Sambuc // Both registers are virtual. 292*f4a2713aSLionel Sambuc const TargetRegisterClass *SrcRC = MRI.getRegClass(Src); 293*f4a2713aSLionel Sambuc const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 294*f4a2713aSLionel Sambuc 295*f4a2713aSLionel Sambuc // Both registers have subreg indices. 296*f4a2713aSLionel Sambuc if (SrcSub && DstSub) { 297*f4a2713aSLionel Sambuc // Copies between different sub-registers are never coalescable. 298*f4a2713aSLionel Sambuc if (Src == Dst && SrcSub != DstSub) 299*f4a2713aSLionel Sambuc return false; 300*f4a2713aSLionel Sambuc 301*f4a2713aSLionel Sambuc NewRC = TRI.getCommonSuperRegClass(SrcRC, SrcSub, DstRC, DstSub, 302*f4a2713aSLionel Sambuc SrcIdx, DstIdx); 303*f4a2713aSLionel Sambuc if (!NewRC) 304*f4a2713aSLionel Sambuc return false; 305*f4a2713aSLionel Sambuc } else if (DstSub) { 306*f4a2713aSLionel Sambuc // SrcReg will be merged with a sub-register of DstReg. 307*f4a2713aSLionel Sambuc SrcIdx = DstSub; 308*f4a2713aSLionel Sambuc NewRC = TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSub); 309*f4a2713aSLionel Sambuc } else if (SrcSub) { 310*f4a2713aSLionel Sambuc // DstReg will be merged with a sub-register of SrcReg. 311*f4a2713aSLionel Sambuc DstIdx = SrcSub; 312*f4a2713aSLionel Sambuc NewRC = TRI.getMatchingSuperRegClass(SrcRC, DstRC, SrcSub); 313*f4a2713aSLionel Sambuc } else { 314*f4a2713aSLionel Sambuc // This is a straight copy without sub-registers. 315*f4a2713aSLionel Sambuc NewRC = TRI.getCommonSubClass(DstRC, SrcRC); 316*f4a2713aSLionel Sambuc } 317*f4a2713aSLionel Sambuc 318*f4a2713aSLionel Sambuc // The combined constraint may be impossible to satisfy. 319*f4a2713aSLionel Sambuc if (!NewRC) 320*f4a2713aSLionel Sambuc return false; 321*f4a2713aSLionel Sambuc 322*f4a2713aSLionel Sambuc // Prefer SrcReg to be a sub-register of DstReg. 323*f4a2713aSLionel Sambuc // FIXME: Coalescer should support subregs symmetrically. 324*f4a2713aSLionel Sambuc if (DstIdx && !SrcIdx) { 325*f4a2713aSLionel Sambuc std::swap(Src, Dst); 326*f4a2713aSLionel Sambuc std::swap(SrcIdx, DstIdx); 327*f4a2713aSLionel Sambuc Flipped = !Flipped; 328*f4a2713aSLionel Sambuc } 329*f4a2713aSLionel Sambuc 330*f4a2713aSLionel Sambuc CrossClass = NewRC != DstRC || NewRC != SrcRC; 331*f4a2713aSLionel Sambuc } 332*f4a2713aSLionel Sambuc // Check our invariants 333*f4a2713aSLionel Sambuc assert(TargetRegisterInfo::isVirtualRegister(Src) && "Src must be virtual"); 334*f4a2713aSLionel Sambuc assert(!(TargetRegisterInfo::isPhysicalRegister(Dst) && DstSub) && 335*f4a2713aSLionel Sambuc "Cannot have a physical SubIdx"); 336*f4a2713aSLionel Sambuc SrcReg = Src; 337*f4a2713aSLionel Sambuc DstReg = Dst; 338*f4a2713aSLionel Sambuc return true; 339*f4a2713aSLionel Sambuc } 340*f4a2713aSLionel Sambuc 341*f4a2713aSLionel Sambuc bool CoalescerPair::flip() { 342*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(DstReg)) 343*f4a2713aSLionel Sambuc return false; 344*f4a2713aSLionel Sambuc std::swap(SrcReg, DstReg); 345*f4a2713aSLionel Sambuc std::swap(SrcIdx, DstIdx); 346*f4a2713aSLionel Sambuc Flipped = !Flipped; 347*f4a2713aSLionel Sambuc return true; 348*f4a2713aSLionel Sambuc } 349*f4a2713aSLionel Sambuc 350*f4a2713aSLionel Sambuc bool CoalescerPair::isCoalescable(const MachineInstr *MI) const { 351*f4a2713aSLionel Sambuc if (!MI) 352*f4a2713aSLionel Sambuc return false; 353*f4a2713aSLionel Sambuc unsigned Src, Dst, SrcSub, DstSub; 354*f4a2713aSLionel Sambuc if (!isMoveInstr(TRI, MI, Src, Dst, SrcSub, DstSub)) 355*f4a2713aSLionel Sambuc return false; 356*f4a2713aSLionel Sambuc 357*f4a2713aSLionel Sambuc // Find the virtual register that is SrcReg. 358*f4a2713aSLionel Sambuc if (Dst == SrcReg) { 359*f4a2713aSLionel Sambuc std::swap(Src, Dst); 360*f4a2713aSLionel Sambuc std::swap(SrcSub, DstSub); 361*f4a2713aSLionel Sambuc } else if (Src != SrcReg) { 362*f4a2713aSLionel Sambuc return false; 363*f4a2713aSLionel Sambuc } 364*f4a2713aSLionel Sambuc 365*f4a2713aSLionel Sambuc // Now check that Dst matches DstReg. 366*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { 367*f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isPhysicalRegister(Dst)) 368*f4a2713aSLionel Sambuc return false; 369*f4a2713aSLionel Sambuc assert(!DstIdx && !SrcIdx && "Inconsistent CoalescerPair state."); 370*f4a2713aSLionel Sambuc // DstSub could be set for a physreg from INSERT_SUBREG. 371*f4a2713aSLionel Sambuc if (DstSub) 372*f4a2713aSLionel Sambuc Dst = TRI.getSubReg(Dst, DstSub); 373*f4a2713aSLionel Sambuc // Full copy of Src. 374*f4a2713aSLionel Sambuc if (!SrcSub) 375*f4a2713aSLionel Sambuc return DstReg == Dst; 376*f4a2713aSLionel Sambuc // This is a partial register copy. Check that the parts match. 377*f4a2713aSLionel Sambuc return TRI.getSubReg(DstReg, SrcSub) == Dst; 378*f4a2713aSLionel Sambuc } else { 379*f4a2713aSLionel Sambuc // DstReg is virtual. 380*f4a2713aSLionel Sambuc if (DstReg != Dst) 381*f4a2713aSLionel Sambuc return false; 382*f4a2713aSLionel Sambuc // Registers match, do the subregisters line up? 383*f4a2713aSLionel Sambuc return TRI.composeSubRegIndices(SrcIdx, SrcSub) == 384*f4a2713aSLionel Sambuc TRI.composeSubRegIndices(DstIdx, DstSub); 385*f4a2713aSLionel Sambuc } 386*f4a2713aSLionel Sambuc } 387*f4a2713aSLionel Sambuc 388*f4a2713aSLionel Sambuc void RegisterCoalescer::getAnalysisUsage(AnalysisUsage &AU) const { 389*f4a2713aSLionel Sambuc AU.setPreservesCFG(); 390*f4a2713aSLionel Sambuc AU.addRequired<AliasAnalysis>(); 391*f4a2713aSLionel Sambuc AU.addRequired<LiveIntervals>(); 392*f4a2713aSLionel Sambuc AU.addPreserved<LiveIntervals>(); 393*f4a2713aSLionel Sambuc AU.addPreserved<SlotIndexes>(); 394*f4a2713aSLionel Sambuc AU.addRequired<MachineLoopInfo>(); 395*f4a2713aSLionel Sambuc AU.addPreserved<MachineLoopInfo>(); 396*f4a2713aSLionel Sambuc AU.addPreservedID(MachineDominatorsID); 397*f4a2713aSLionel Sambuc MachineFunctionPass::getAnalysisUsage(AU); 398*f4a2713aSLionel Sambuc } 399*f4a2713aSLionel Sambuc 400*f4a2713aSLionel Sambuc void RegisterCoalescer::eliminateDeadDefs() { 401*f4a2713aSLionel Sambuc SmallVector<unsigned, 8> NewRegs; 402*f4a2713aSLionel Sambuc LiveRangeEdit(0, NewRegs, *MF, *LIS, 0, this).eliminateDeadDefs(DeadDefs); 403*f4a2713aSLionel Sambuc } 404*f4a2713aSLionel Sambuc 405*f4a2713aSLionel Sambuc // Callback from eliminateDeadDefs(). 406*f4a2713aSLionel Sambuc void RegisterCoalescer::LRE_WillEraseInstruction(MachineInstr *MI) { 407*f4a2713aSLionel Sambuc // MI may be in WorkList. Make sure we don't visit it. 408*f4a2713aSLionel Sambuc ErasedInstrs.insert(MI); 409*f4a2713aSLionel Sambuc } 410*f4a2713aSLionel Sambuc 411*f4a2713aSLionel Sambuc /// adjustCopiesBackFrom - We found a non-trivially-coalescable copy with IntA 412*f4a2713aSLionel Sambuc /// being the source and IntB being the dest, thus this defines a value number 413*f4a2713aSLionel Sambuc /// in IntB. If the source value number (in IntA) is defined by a copy from B, 414*f4a2713aSLionel Sambuc /// see if we can merge these two pieces of B into a single value number, 415*f4a2713aSLionel Sambuc /// eliminating a copy. For example: 416*f4a2713aSLionel Sambuc /// 417*f4a2713aSLionel Sambuc /// A3 = B0 418*f4a2713aSLionel Sambuc /// ... 419*f4a2713aSLionel Sambuc /// B1 = A3 <- this copy 420*f4a2713aSLionel Sambuc /// 421*f4a2713aSLionel Sambuc /// In this case, B0 can be extended to where the B1 copy lives, allowing the B1 422*f4a2713aSLionel Sambuc /// value number to be replaced with B0 (which simplifies the B liveinterval). 423*f4a2713aSLionel Sambuc /// 424*f4a2713aSLionel Sambuc /// This returns true if an interval was modified. 425*f4a2713aSLionel Sambuc /// 426*f4a2713aSLionel Sambuc bool RegisterCoalescer::adjustCopiesBackFrom(const CoalescerPair &CP, 427*f4a2713aSLionel Sambuc MachineInstr *CopyMI) { 428*f4a2713aSLionel Sambuc assert(!CP.isPartial() && "This doesn't work for partial copies."); 429*f4a2713aSLionel Sambuc assert(!CP.isPhys() && "This doesn't work for physreg copies."); 430*f4a2713aSLionel Sambuc 431*f4a2713aSLionel Sambuc LiveInterval &IntA = 432*f4a2713aSLionel Sambuc LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg()); 433*f4a2713aSLionel Sambuc LiveInterval &IntB = 434*f4a2713aSLionel Sambuc LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg()); 435*f4a2713aSLionel Sambuc SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot(); 436*f4a2713aSLionel Sambuc 437*f4a2713aSLionel Sambuc // BValNo is a value number in B that is defined by a copy from A. 'B1' in 438*f4a2713aSLionel Sambuc // the example above. 439*f4a2713aSLionel Sambuc LiveInterval::iterator BS = IntB.FindSegmentContaining(CopyIdx); 440*f4a2713aSLionel Sambuc if (BS == IntB.end()) return false; 441*f4a2713aSLionel Sambuc VNInfo *BValNo = BS->valno; 442*f4a2713aSLionel Sambuc 443*f4a2713aSLionel Sambuc // Get the location that B is defined at. Two options: either this value has 444*f4a2713aSLionel Sambuc // an unknown definition point or it is defined at CopyIdx. If unknown, we 445*f4a2713aSLionel Sambuc // can't process it. 446*f4a2713aSLionel Sambuc if (BValNo->def != CopyIdx) return false; 447*f4a2713aSLionel Sambuc 448*f4a2713aSLionel Sambuc // AValNo is the value number in A that defines the copy, A3 in the example. 449*f4a2713aSLionel Sambuc SlotIndex CopyUseIdx = CopyIdx.getRegSlot(true); 450*f4a2713aSLionel Sambuc LiveInterval::iterator AS = IntA.FindSegmentContaining(CopyUseIdx); 451*f4a2713aSLionel Sambuc // The live segment might not exist after fun with physreg coalescing. 452*f4a2713aSLionel Sambuc if (AS == IntA.end()) return false; 453*f4a2713aSLionel Sambuc VNInfo *AValNo = AS->valno; 454*f4a2713aSLionel Sambuc 455*f4a2713aSLionel Sambuc // If AValNo is defined as a copy from IntB, we can potentially process this. 456*f4a2713aSLionel Sambuc // Get the instruction that defines this value number. 457*f4a2713aSLionel Sambuc MachineInstr *ACopyMI = LIS->getInstructionFromIndex(AValNo->def); 458*f4a2713aSLionel Sambuc // Don't allow any partial copies, even if isCoalescable() allows them. 459*f4a2713aSLionel Sambuc if (!CP.isCoalescable(ACopyMI) || !ACopyMI->isFullCopy()) 460*f4a2713aSLionel Sambuc return false; 461*f4a2713aSLionel Sambuc 462*f4a2713aSLionel Sambuc // Get the Segment in IntB that this value number starts with. 463*f4a2713aSLionel Sambuc LiveInterval::iterator ValS = 464*f4a2713aSLionel Sambuc IntB.FindSegmentContaining(AValNo->def.getPrevSlot()); 465*f4a2713aSLionel Sambuc if (ValS == IntB.end()) 466*f4a2713aSLionel Sambuc return false; 467*f4a2713aSLionel Sambuc 468*f4a2713aSLionel Sambuc // Make sure that the end of the live segment is inside the same block as 469*f4a2713aSLionel Sambuc // CopyMI. 470*f4a2713aSLionel Sambuc MachineInstr *ValSEndInst = 471*f4a2713aSLionel Sambuc LIS->getInstructionFromIndex(ValS->end.getPrevSlot()); 472*f4a2713aSLionel Sambuc if (!ValSEndInst || ValSEndInst->getParent() != CopyMI->getParent()) 473*f4a2713aSLionel Sambuc return false; 474*f4a2713aSLionel Sambuc 475*f4a2713aSLionel Sambuc // Okay, we now know that ValS ends in the same block that the CopyMI 476*f4a2713aSLionel Sambuc // live-range starts. If there are no intervening live segments between them 477*f4a2713aSLionel Sambuc // in IntB, we can merge them. 478*f4a2713aSLionel Sambuc if (ValS+1 != BS) return false; 479*f4a2713aSLionel Sambuc 480*f4a2713aSLionel Sambuc DEBUG(dbgs() << "Extending: " << PrintReg(IntB.reg, TRI)); 481*f4a2713aSLionel Sambuc 482*f4a2713aSLionel Sambuc SlotIndex FillerStart = ValS->end, FillerEnd = BS->start; 483*f4a2713aSLionel Sambuc // We are about to delete CopyMI, so need to remove it as the 'instruction 484*f4a2713aSLionel Sambuc // that defines this value #'. Update the valnum with the new defining 485*f4a2713aSLionel Sambuc // instruction #. 486*f4a2713aSLionel Sambuc BValNo->def = FillerStart; 487*f4a2713aSLionel Sambuc 488*f4a2713aSLionel Sambuc // Okay, we can merge them. We need to insert a new liverange: 489*f4a2713aSLionel Sambuc // [ValS.end, BS.begin) of either value number, then we merge the 490*f4a2713aSLionel Sambuc // two value numbers. 491*f4a2713aSLionel Sambuc IntB.addSegment(LiveInterval::Segment(FillerStart, FillerEnd, BValNo)); 492*f4a2713aSLionel Sambuc 493*f4a2713aSLionel Sambuc // Okay, merge "B1" into the same value number as "B0". 494*f4a2713aSLionel Sambuc if (BValNo != ValS->valno) 495*f4a2713aSLionel Sambuc IntB.MergeValueNumberInto(BValNo, ValS->valno); 496*f4a2713aSLionel Sambuc DEBUG(dbgs() << " result = " << IntB << '\n'); 497*f4a2713aSLionel Sambuc 498*f4a2713aSLionel Sambuc // If the source instruction was killing the source register before the 499*f4a2713aSLionel Sambuc // merge, unset the isKill marker given the live range has been extended. 500*f4a2713aSLionel Sambuc int UIdx = ValSEndInst->findRegisterUseOperandIdx(IntB.reg, true); 501*f4a2713aSLionel Sambuc if (UIdx != -1) { 502*f4a2713aSLionel Sambuc ValSEndInst->getOperand(UIdx).setIsKill(false); 503*f4a2713aSLionel Sambuc } 504*f4a2713aSLionel Sambuc 505*f4a2713aSLionel Sambuc // Rewrite the copy. If the copy instruction was killing the destination 506*f4a2713aSLionel Sambuc // register before the merge, find the last use and trim the live range. That 507*f4a2713aSLionel Sambuc // will also add the isKill marker. 508*f4a2713aSLionel Sambuc CopyMI->substituteRegister(IntA.reg, IntB.reg, 0, *TRI); 509*f4a2713aSLionel Sambuc if (AS->end == CopyIdx) 510*f4a2713aSLionel Sambuc LIS->shrinkToUses(&IntA); 511*f4a2713aSLionel Sambuc 512*f4a2713aSLionel Sambuc ++numExtends; 513*f4a2713aSLionel Sambuc return true; 514*f4a2713aSLionel Sambuc } 515*f4a2713aSLionel Sambuc 516*f4a2713aSLionel Sambuc /// hasOtherReachingDefs - Return true if there are definitions of IntB 517*f4a2713aSLionel Sambuc /// other than BValNo val# that can reach uses of AValno val# of IntA. 518*f4a2713aSLionel Sambuc bool RegisterCoalescer::hasOtherReachingDefs(LiveInterval &IntA, 519*f4a2713aSLionel Sambuc LiveInterval &IntB, 520*f4a2713aSLionel Sambuc VNInfo *AValNo, 521*f4a2713aSLionel Sambuc VNInfo *BValNo) { 522*f4a2713aSLionel Sambuc // If AValNo has PHI kills, conservatively assume that IntB defs can reach 523*f4a2713aSLionel Sambuc // the PHI values. 524*f4a2713aSLionel Sambuc if (LIS->hasPHIKill(IntA, AValNo)) 525*f4a2713aSLionel Sambuc return true; 526*f4a2713aSLionel Sambuc 527*f4a2713aSLionel Sambuc for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end(); 528*f4a2713aSLionel Sambuc AI != AE; ++AI) { 529*f4a2713aSLionel Sambuc if (AI->valno != AValNo) continue; 530*f4a2713aSLionel Sambuc LiveInterval::iterator BI = 531*f4a2713aSLionel Sambuc std::upper_bound(IntB.begin(), IntB.end(), AI->start); 532*f4a2713aSLionel Sambuc if (BI != IntB.begin()) 533*f4a2713aSLionel Sambuc --BI; 534*f4a2713aSLionel Sambuc for (; BI != IntB.end() && AI->end >= BI->start; ++BI) { 535*f4a2713aSLionel Sambuc if (BI->valno == BValNo) 536*f4a2713aSLionel Sambuc continue; 537*f4a2713aSLionel Sambuc if (BI->start <= AI->start && BI->end > AI->start) 538*f4a2713aSLionel Sambuc return true; 539*f4a2713aSLionel Sambuc if (BI->start > AI->start && BI->start < AI->end) 540*f4a2713aSLionel Sambuc return true; 541*f4a2713aSLionel Sambuc } 542*f4a2713aSLionel Sambuc } 543*f4a2713aSLionel Sambuc return false; 544*f4a2713aSLionel Sambuc } 545*f4a2713aSLionel Sambuc 546*f4a2713aSLionel Sambuc /// removeCopyByCommutingDef - We found a non-trivially-coalescable copy with 547*f4a2713aSLionel Sambuc /// IntA being the source and IntB being the dest, thus this defines a value 548*f4a2713aSLionel Sambuc /// number in IntB. If the source value number (in IntA) is defined by a 549*f4a2713aSLionel Sambuc /// commutable instruction and its other operand is coalesced to the copy dest 550*f4a2713aSLionel Sambuc /// register, see if we can transform the copy into a noop by commuting the 551*f4a2713aSLionel Sambuc /// definition. For example, 552*f4a2713aSLionel Sambuc /// 553*f4a2713aSLionel Sambuc /// A3 = op A2 B0<kill> 554*f4a2713aSLionel Sambuc /// ... 555*f4a2713aSLionel Sambuc /// B1 = A3 <- this copy 556*f4a2713aSLionel Sambuc /// ... 557*f4a2713aSLionel Sambuc /// = op A3 <- more uses 558*f4a2713aSLionel Sambuc /// 559*f4a2713aSLionel Sambuc /// ==> 560*f4a2713aSLionel Sambuc /// 561*f4a2713aSLionel Sambuc /// B2 = op B0 A2<kill> 562*f4a2713aSLionel Sambuc /// ... 563*f4a2713aSLionel Sambuc /// B1 = B2 <- now an identify copy 564*f4a2713aSLionel Sambuc /// ... 565*f4a2713aSLionel Sambuc /// = op B2 <- more uses 566*f4a2713aSLionel Sambuc /// 567*f4a2713aSLionel Sambuc /// This returns true if an interval was modified. 568*f4a2713aSLionel Sambuc /// 569*f4a2713aSLionel Sambuc bool RegisterCoalescer::removeCopyByCommutingDef(const CoalescerPair &CP, 570*f4a2713aSLionel Sambuc MachineInstr *CopyMI) { 571*f4a2713aSLionel Sambuc assert (!CP.isPhys()); 572*f4a2713aSLionel Sambuc 573*f4a2713aSLionel Sambuc SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI).getRegSlot(); 574*f4a2713aSLionel Sambuc 575*f4a2713aSLionel Sambuc LiveInterval &IntA = 576*f4a2713aSLionel Sambuc LIS->getInterval(CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg()); 577*f4a2713aSLionel Sambuc LiveInterval &IntB = 578*f4a2713aSLionel Sambuc LIS->getInterval(CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg()); 579*f4a2713aSLionel Sambuc 580*f4a2713aSLionel Sambuc // BValNo is a value number in B that is defined by a copy from A. 'B1' in 581*f4a2713aSLionel Sambuc // the example above. 582*f4a2713aSLionel Sambuc VNInfo *BValNo = IntB.getVNInfoAt(CopyIdx); 583*f4a2713aSLionel Sambuc if (!BValNo || BValNo->def != CopyIdx) 584*f4a2713aSLionel Sambuc return false; 585*f4a2713aSLionel Sambuc 586*f4a2713aSLionel Sambuc // AValNo is the value number in A that defines the copy, A3 in the example. 587*f4a2713aSLionel Sambuc VNInfo *AValNo = IntA.getVNInfoAt(CopyIdx.getRegSlot(true)); 588*f4a2713aSLionel Sambuc assert(AValNo && "COPY source not live"); 589*f4a2713aSLionel Sambuc if (AValNo->isPHIDef() || AValNo->isUnused()) 590*f4a2713aSLionel Sambuc return false; 591*f4a2713aSLionel Sambuc MachineInstr *DefMI = LIS->getInstructionFromIndex(AValNo->def); 592*f4a2713aSLionel Sambuc if (!DefMI) 593*f4a2713aSLionel Sambuc return false; 594*f4a2713aSLionel Sambuc if (!DefMI->isCommutable()) 595*f4a2713aSLionel Sambuc return false; 596*f4a2713aSLionel Sambuc // If DefMI is a two-address instruction then commuting it will change the 597*f4a2713aSLionel Sambuc // destination register. 598*f4a2713aSLionel Sambuc int DefIdx = DefMI->findRegisterDefOperandIdx(IntA.reg); 599*f4a2713aSLionel Sambuc assert(DefIdx != -1); 600*f4a2713aSLionel Sambuc unsigned UseOpIdx; 601*f4a2713aSLionel Sambuc if (!DefMI->isRegTiedToUseOperand(DefIdx, &UseOpIdx)) 602*f4a2713aSLionel Sambuc return false; 603*f4a2713aSLionel Sambuc unsigned Op1, Op2, NewDstIdx; 604*f4a2713aSLionel Sambuc if (!TII->findCommutedOpIndices(DefMI, Op1, Op2)) 605*f4a2713aSLionel Sambuc return false; 606*f4a2713aSLionel Sambuc if (Op1 == UseOpIdx) 607*f4a2713aSLionel Sambuc NewDstIdx = Op2; 608*f4a2713aSLionel Sambuc else if (Op2 == UseOpIdx) 609*f4a2713aSLionel Sambuc NewDstIdx = Op1; 610*f4a2713aSLionel Sambuc else 611*f4a2713aSLionel Sambuc return false; 612*f4a2713aSLionel Sambuc 613*f4a2713aSLionel Sambuc MachineOperand &NewDstMO = DefMI->getOperand(NewDstIdx); 614*f4a2713aSLionel Sambuc unsigned NewReg = NewDstMO.getReg(); 615*f4a2713aSLionel Sambuc if (NewReg != IntB.reg || !IntB.Query(AValNo->def).isKill()) 616*f4a2713aSLionel Sambuc return false; 617*f4a2713aSLionel Sambuc 618*f4a2713aSLionel Sambuc // Make sure there are no other definitions of IntB that would reach the 619*f4a2713aSLionel Sambuc // uses which the new definition can reach. 620*f4a2713aSLionel Sambuc if (hasOtherReachingDefs(IntA, IntB, AValNo, BValNo)) 621*f4a2713aSLionel Sambuc return false; 622*f4a2713aSLionel Sambuc 623*f4a2713aSLionel Sambuc // If some of the uses of IntA.reg is already coalesced away, return false. 624*f4a2713aSLionel Sambuc // It's not possible to determine whether it's safe to perform the coalescing. 625*f4a2713aSLionel Sambuc for (MachineRegisterInfo::use_nodbg_iterator UI = 626*f4a2713aSLionel Sambuc MRI->use_nodbg_begin(IntA.reg), 627*f4a2713aSLionel Sambuc UE = MRI->use_nodbg_end(); UI != UE; ++UI) { 628*f4a2713aSLionel Sambuc MachineInstr *UseMI = &*UI; 629*f4a2713aSLionel Sambuc SlotIndex UseIdx = LIS->getInstructionIndex(UseMI); 630*f4a2713aSLionel Sambuc LiveInterval::iterator US = IntA.FindSegmentContaining(UseIdx); 631*f4a2713aSLionel Sambuc if (US == IntA.end() || US->valno != AValNo) 632*f4a2713aSLionel Sambuc continue; 633*f4a2713aSLionel Sambuc // If this use is tied to a def, we can't rewrite the register. 634*f4a2713aSLionel Sambuc if (UseMI->isRegTiedToDefOperand(UI.getOperandNo())) 635*f4a2713aSLionel Sambuc return false; 636*f4a2713aSLionel Sambuc } 637*f4a2713aSLionel Sambuc 638*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tremoveCopyByCommutingDef: " << AValNo->def << '\t' 639*f4a2713aSLionel Sambuc << *DefMI); 640*f4a2713aSLionel Sambuc 641*f4a2713aSLionel Sambuc // At this point we have decided that it is legal to do this 642*f4a2713aSLionel Sambuc // transformation. Start by commuting the instruction. 643*f4a2713aSLionel Sambuc MachineBasicBlock *MBB = DefMI->getParent(); 644*f4a2713aSLionel Sambuc MachineInstr *NewMI = TII->commuteInstruction(DefMI); 645*f4a2713aSLionel Sambuc if (!NewMI) 646*f4a2713aSLionel Sambuc return false; 647*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(IntA.reg) && 648*f4a2713aSLionel Sambuc TargetRegisterInfo::isVirtualRegister(IntB.reg) && 649*f4a2713aSLionel Sambuc !MRI->constrainRegClass(IntB.reg, MRI->getRegClass(IntA.reg))) 650*f4a2713aSLionel Sambuc return false; 651*f4a2713aSLionel Sambuc if (NewMI != DefMI) { 652*f4a2713aSLionel Sambuc LIS->ReplaceMachineInstrInMaps(DefMI, NewMI); 653*f4a2713aSLionel Sambuc MachineBasicBlock::iterator Pos = DefMI; 654*f4a2713aSLionel Sambuc MBB->insert(Pos, NewMI); 655*f4a2713aSLionel Sambuc MBB->erase(DefMI); 656*f4a2713aSLionel Sambuc } 657*f4a2713aSLionel Sambuc unsigned OpIdx = NewMI->findRegisterUseOperandIdx(IntA.reg, false); 658*f4a2713aSLionel Sambuc NewMI->getOperand(OpIdx).setIsKill(); 659*f4a2713aSLionel Sambuc 660*f4a2713aSLionel Sambuc // If ALR and BLR overlaps and end of BLR extends beyond end of ALR, e.g. 661*f4a2713aSLionel Sambuc // A = or A, B 662*f4a2713aSLionel Sambuc // ... 663*f4a2713aSLionel Sambuc // B = A 664*f4a2713aSLionel Sambuc // ... 665*f4a2713aSLionel Sambuc // C = A<kill> 666*f4a2713aSLionel Sambuc // ... 667*f4a2713aSLionel Sambuc // = B 668*f4a2713aSLionel Sambuc 669*f4a2713aSLionel Sambuc // Update uses of IntA of the specific Val# with IntB. 670*f4a2713aSLionel Sambuc for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(IntA.reg), 671*f4a2713aSLionel Sambuc UE = MRI->use_end(); UI != UE;) { 672*f4a2713aSLionel Sambuc MachineOperand &UseMO = UI.getOperand(); 673*f4a2713aSLionel Sambuc MachineInstr *UseMI = &*UI; 674*f4a2713aSLionel Sambuc ++UI; 675*f4a2713aSLionel Sambuc if (UseMI->isDebugValue()) { 676*f4a2713aSLionel Sambuc // FIXME These don't have an instruction index. Not clear we have enough 677*f4a2713aSLionel Sambuc // info to decide whether to do this replacement or not. For now do it. 678*f4a2713aSLionel Sambuc UseMO.setReg(NewReg); 679*f4a2713aSLionel Sambuc continue; 680*f4a2713aSLionel Sambuc } 681*f4a2713aSLionel Sambuc SlotIndex UseIdx = LIS->getInstructionIndex(UseMI).getRegSlot(true); 682*f4a2713aSLionel Sambuc LiveInterval::iterator US = IntA.FindSegmentContaining(UseIdx); 683*f4a2713aSLionel Sambuc if (US == IntA.end() || US->valno != AValNo) 684*f4a2713aSLionel Sambuc continue; 685*f4a2713aSLionel Sambuc // Kill flags are no longer accurate. They are recomputed after RA. 686*f4a2713aSLionel Sambuc UseMO.setIsKill(false); 687*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(NewReg)) 688*f4a2713aSLionel Sambuc UseMO.substPhysReg(NewReg, *TRI); 689*f4a2713aSLionel Sambuc else 690*f4a2713aSLionel Sambuc UseMO.setReg(NewReg); 691*f4a2713aSLionel Sambuc if (UseMI == CopyMI) 692*f4a2713aSLionel Sambuc continue; 693*f4a2713aSLionel Sambuc if (!UseMI->isCopy()) 694*f4a2713aSLionel Sambuc continue; 695*f4a2713aSLionel Sambuc if (UseMI->getOperand(0).getReg() != IntB.reg || 696*f4a2713aSLionel Sambuc UseMI->getOperand(0).getSubReg()) 697*f4a2713aSLionel Sambuc continue; 698*f4a2713aSLionel Sambuc 699*f4a2713aSLionel Sambuc // This copy will become a noop. If it's defining a new val#, merge it into 700*f4a2713aSLionel Sambuc // BValNo. 701*f4a2713aSLionel Sambuc SlotIndex DefIdx = UseIdx.getRegSlot(); 702*f4a2713aSLionel Sambuc VNInfo *DVNI = IntB.getVNInfoAt(DefIdx); 703*f4a2713aSLionel Sambuc if (!DVNI) 704*f4a2713aSLionel Sambuc continue; 705*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tnoop: " << DefIdx << '\t' << *UseMI); 706*f4a2713aSLionel Sambuc assert(DVNI->def == DefIdx); 707*f4a2713aSLionel Sambuc BValNo = IntB.MergeValueNumberInto(BValNo, DVNI); 708*f4a2713aSLionel Sambuc ErasedInstrs.insert(UseMI); 709*f4a2713aSLionel Sambuc LIS->RemoveMachineInstrFromMaps(UseMI); 710*f4a2713aSLionel Sambuc UseMI->eraseFromParent(); 711*f4a2713aSLionel Sambuc } 712*f4a2713aSLionel Sambuc 713*f4a2713aSLionel Sambuc // Extend BValNo by merging in IntA live segments of AValNo. Val# definition 714*f4a2713aSLionel Sambuc // is updated. 715*f4a2713aSLionel Sambuc VNInfo *ValNo = BValNo; 716*f4a2713aSLionel Sambuc ValNo->def = AValNo->def; 717*f4a2713aSLionel Sambuc for (LiveInterval::iterator AI = IntA.begin(), AE = IntA.end(); 718*f4a2713aSLionel Sambuc AI != AE; ++AI) { 719*f4a2713aSLionel Sambuc if (AI->valno != AValNo) continue; 720*f4a2713aSLionel Sambuc IntB.addSegment(LiveInterval::Segment(AI->start, AI->end, ValNo)); 721*f4a2713aSLionel Sambuc } 722*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\textended: " << IntB << '\n'); 723*f4a2713aSLionel Sambuc 724*f4a2713aSLionel Sambuc IntA.removeValNo(AValNo); 725*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\ttrimmed: " << IntA << '\n'); 726*f4a2713aSLionel Sambuc ++numCommutes; 727*f4a2713aSLionel Sambuc return true; 728*f4a2713aSLionel Sambuc } 729*f4a2713aSLionel Sambuc 730*f4a2713aSLionel Sambuc /// reMaterializeTrivialDef - If the source of a copy is defined by a trivial 731*f4a2713aSLionel Sambuc /// computation, replace the copy by rematerialize the definition. 732*f4a2713aSLionel Sambuc bool RegisterCoalescer::reMaterializeTrivialDef(CoalescerPair &CP, 733*f4a2713aSLionel Sambuc MachineInstr *CopyMI, 734*f4a2713aSLionel Sambuc bool &IsDefCopy) { 735*f4a2713aSLionel Sambuc IsDefCopy = false; 736*f4a2713aSLionel Sambuc unsigned SrcReg = CP.isFlipped() ? CP.getDstReg() : CP.getSrcReg(); 737*f4a2713aSLionel Sambuc unsigned SrcIdx = CP.isFlipped() ? CP.getDstIdx() : CP.getSrcIdx(); 738*f4a2713aSLionel Sambuc unsigned DstReg = CP.isFlipped() ? CP.getSrcReg() : CP.getDstReg(); 739*f4a2713aSLionel Sambuc unsigned DstIdx = CP.isFlipped() ? CP.getSrcIdx() : CP.getDstIdx(); 740*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(SrcReg)) 741*f4a2713aSLionel Sambuc return false; 742*f4a2713aSLionel Sambuc 743*f4a2713aSLionel Sambuc LiveInterval &SrcInt = LIS->getInterval(SrcReg); 744*f4a2713aSLionel Sambuc SlotIndex CopyIdx = LIS->getInstructionIndex(CopyMI); 745*f4a2713aSLionel Sambuc VNInfo *ValNo = SrcInt.Query(CopyIdx).valueIn(); 746*f4a2713aSLionel Sambuc assert(ValNo && "CopyMI input register not live"); 747*f4a2713aSLionel Sambuc if (ValNo->isPHIDef() || ValNo->isUnused()) 748*f4a2713aSLionel Sambuc return false; 749*f4a2713aSLionel Sambuc MachineInstr *DefMI = LIS->getInstructionFromIndex(ValNo->def); 750*f4a2713aSLionel Sambuc if (!DefMI) 751*f4a2713aSLionel Sambuc return false; 752*f4a2713aSLionel Sambuc if (DefMI->isCopyLike()) { 753*f4a2713aSLionel Sambuc IsDefCopy = true; 754*f4a2713aSLionel Sambuc return false; 755*f4a2713aSLionel Sambuc } 756*f4a2713aSLionel Sambuc if (!DefMI->isAsCheapAsAMove()) 757*f4a2713aSLionel Sambuc return false; 758*f4a2713aSLionel Sambuc if (!TII->isTriviallyReMaterializable(DefMI, AA)) 759*f4a2713aSLionel Sambuc return false; 760*f4a2713aSLionel Sambuc bool SawStore = false; 761*f4a2713aSLionel Sambuc if (!DefMI->isSafeToMove(TII, AA, SawStore)) 762*f4a2713aSLionel Sambuc return false; 763*f4a2713aSLionel Sambuc const MCInstrDesc &MCID = DefMI->getDesc(); 764*f4a2713aSLionel Sambuc if (MCID.getNumDefs() != 1) 765*f4a2713aSLionel Sambuc return false; 766*f4a2713aSLionel Sambuc // Only support subregister destinations when the def is read-undef. 767*f4a2713aSLionel Sambuc MachineOperand &DstOperand = CopyMI->getOperand(0); 768*f4a2713aSLionel Sambuc unsigned CopyDstReg = DstOperand.getReg(); 769*f4a2713aSLionel Sambuc if (DstOperand.getSubReg() && !DstOperand.isUndef()) 770*f4a2713aSLionel Sambuc return false; 771*f4a2713aSLionel Sambuc 772*f4a2713aSLionel Sambuc const TargetRegisterClass *DefRC = TII->getRegClass(MCID, 0, TRI, *MF); 773*f4a2713aSLionel Sambuc if (!DefMI->isImplicitDef()) { 774*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(DstReg)) { 775*f4a2713aSLionel Sambuc unsigned NewDstReg = DstReg; 776*f4a2713aSLionel Sambuc 777*f4a2713aSLionel Sambuc unsigned NewDstIdx = TRI->composeSubRegIndices(CP.getSrcIdx(), 778*f4a2713aSLionel Sambuc DefMI->getOperand(0).getSubReg()); 779*f4a2713aSLionel Sambuc if (NewDstIdx) 780*f4a2713aSLionel Sambuc NewDstReg = TRI->getSubReg(DstReg, NewDstIdx); 781*f4a2713aSLionel Sambuc 782*f4a2713aSLionel Sambuc // Finally, make sure that the physical subregister that will be 783*f4a2713aSLionel Sambuc // constructed later is permitted for the instruction. 784*f4a2713aSLionel Sambuc if (!DefRC->contains(NewDstReg)) 785*f4a2713aSLionel Sambuc return false; 786*f4a2713aSLionel Sambuc } else { 787*f4a2713aSLionel Sambuc // Theoretically, some stack frame reference could exist. Just make sure 788*f4a2713aSLionel Sambuc // it hasn't actually happened. 789*f4a2713aSLionel Sambuc assert(TargetRegisterInfo::isVirtualRegister(DstReg) && 790*f4a2713aSLionel Sambuc "Only expect to deal with virtual or physical registers"); 791*f4a2713aSLionel Sambuc } 792*f4a2713aSLionel Sambuc } 793*f4a2713aSLionel Sambuc 794*f4a2713aSLionel Sambuc MachineBasicBlock *MBB = CopyMI->getParent(); 795*f4a2713aSLionel Sambuc MachineBasicBlock::iterator MII = 796*f4a2713aSLionel Sambuc llvm::next(MachineBasicBlock::iterator(CopyMI)); 797*f4a2713aSLionel Sambuc TII->reMaterialize(*MBB, MII, DstReg, SrcIdx, DefMI, *TRI); 798*f4a2713aSLionel Sambuc MachineInstr *NewMI = prior(MII); 799*f4a2713aSLionel Sambuc 800*f4a2713aSLionel Sambuc LIS->ReplaceMachineInstrInMaps(CopyMI, NewMI); 801*f4a2713aSLionel Sambuc CopyMI->eraseFromParent(); 802*f4a2713aSLionel Sambuc ErasedInstrs.insert(CopyMI); 803*f4a2713aSLionel Sambuc 804*f4a2713aSLionel Sambuc // NewMI may have dead implicit defs (E.g. EFLAGS for MOV<bits>r0 on X86). 805*f4a2713aSLionel Sambuc // We need to remember these so we can add intervals once we insert 806*f4a2713aSLionel Sambuc // NewMI into SlotIndexes. 807*f4a2713aSLionel Sambuc SmallVector<unsigned, 4> NewMIImplDefs; 808*f4a2713aSLionel Sambuc for (unsigned i = NewMI->getDesc().getNumOperands(), 809*f4a2713aSLionel Sambuc e = NewMI->getNumOperands(); i != e; ++i) { 810*f4a2713aSLionel Sambuc MachineOperand &MO = NewMI->getOperand(i); 811*f4a2713aSLionel Sambuc if (MO.isReg()) { 812*f4a2713aSLionel Sambuc assert(MO.isDef() && MO.isImplicit() && MO.isDead() && 813*f4a2713aSLionel Sambuc TargetRegisterInfo::isPhysicalRegister(MO.getReg())); 814*f4a2713aSLionel Sambuc NewMIImplDefs.push_back(MO.getReg()); 815*f4a2713aSLionel Sambuc } 816*f4a2713aSLionel Sambuc } 817*f4a2713aSLionel Sambuc 818*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(DstReg)) { 819*f4a2713aSLionel Sambuc unsigned NewIdx = NewMI->getOperand(0).getSubReg(); 820*f4a2713aSLionel Sambuc const TargetRegisterClass *RCForInst; 821*f4a2713aSLionel Sambuc if (NewIdx) 822*f4a2713aSLionel Sambuc RCForInst = TRI->getMatchingSuperRegClass(MRI->getRegClass(DstReg), DefRC, 823*f4a2713aSLionel Sambuc NewIdx); 824*f4a2713aSLionel Sambuc 825*f4a2713aSLionel Sambuc if (MRI->constrainRegClass(DstReg, DefRC)) { 826*f4a2713aSLionel Sambuc // The materialized instruction is quite capable of setting DstReg 827*f4a2713aSLionel Sambuc // directly, but it may still have a now-trivial subregister index which 828*f4a2713aSLionel Sambuc // we should clear. 829*f4a2713aSLionel Sambuc NewMI->getOperand(0).setSubReg(0); 830*f4a2713aSLionel Sambuc } else if (NewIdx && RCForInst) { 831*f4a2713aSLionel Sambuc // The subreg index on NewMI is essential; we still have to make sure 832*f4a2713aSLionel Sambuc // DstReg:idx is in a class that NewMI can use. 833*f4a2713aSLionel Sambuc MRI->constrainRegClass(DstReg, RCForInst); 834*f4a2713aSLionel Sambuc } else { 835*f4a2713aSLionel Sambuc // DstReg is actually incompatible with NewMI, we have to move to a 836*f4a2713aSLionel Sambuc // super-reg's class. This could come from a sequence like: 837*f4a2713aSLionel Sambuc // GR32 = MOV32r0 838*f4a2713aSLionel Sambuc // GR8 = COPY GR32:sub_8 839*f4a2713aSLionel Sambuc MRI->setRegClass(DstReg, CP.getNewRC()); 840*f4a2713aSLionel Sambuc updateRegDefsUses(DstReg, DstReg, DstIdx); 841*f4a2713aSLionel Sambuc NewMI->getOperand(0).setSubReg( 842*f4a2713aSLionel Sambuc TRI->composeSubRegIndices(SrcIdx, DefMI->getOperand(0).getSubReg())); 843*f4a2713aSLionel Sambuc } 844*f4a2713aSLionel Sambuc } else if (NewMI->getOperand(0).getReg() != CopyDstReg) { 845*f4a2713aSLionel Sambuc // The New instruction may be defining a sub-register of what's actually 846*f4a2713aSLionel Sambuc // been asked for. If so it must implicitly define the whole thing. 847*f4a2713aSLionel Sambuc assert(TargetRegisterInfo::isPhysicalRegister(DstReg) && 848*f4a2713aSLionel Sambuc "Only expect virtual or physical registers in remat"); 849*f4a2713aSLionel Sambuc NewMI->getOperand(0).setIsDead(true); 850*f4a2713aSLionel Sambuc NewMI->addOperand(MachineOperand::CreateReg(CopyDstReg, 851*f4a2713aSLionel Sambuc true /*IsDef*/, 852*f4a2713aSLionel Sambuc true /*IsImp*/, 853*f4a2713aSLionel Sambuc false /*IsKill*/)); 854*f4a2713aSLionel Sambuc } 855*f4a2713aSLionel Sambuc 856*f4a2713aSLionel Sambuc if (NewMI->getOperand(0).getSubReg()) 857*f4a2713aSLionel Sambuc NewMI->getOperand(0).setIsUndef(); 858*f4a2713aSLionel Sambuc 859*f4a2713aSLionel Sambuc // CopyMI may have implicit operands, transfer them over to the newly 860*f4a2713aSLionel Sambuc // rematerialized instruction. And update implicit def interval valnos. 861*f4a2713aSLionel Sambuc for (unsigned i = CopyMI->getDesc().getNumOperands(), 862*f4a2713aSLionel Sambuc e = CopyMI->getNumOperands(); i != e; ++i) { 863*f4a2713aSLionel Sambuc MachineOperand &MO = CopyMI->getOperand(i); 864*f4a2713aSLionel Sambuc if (MO.isReg()) { 865*f4a2713aSLionel Sambuc assert(MO.isImplicit() && "No explicit operands after implict operands."); 866*f4a2713aSLionel Sambuc // Discard VReg implicit defs. 867*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { 868*f4a2713aSLionel Sambuc NewMI->addOperand(MO); 869*f4a2713aSLionel Sambuc } 870*f4a2713aSLionel Sambuc } 871*f4a2713aSLionel Sambuc } 872*f4a2713aSLionel Sambuc 873*f4a2713aSLionel Sambuc SlotIndex NewMIIdx = LIS->getInstructionIndex(NewMI); 874*f4a2713aSLionel Sambuc for (unsigned i = 0, e = NewMIImplDefs.size(); i != e; ++i) { 875*f4a2713aSLionel Sambuc unsigned Reg = NewMIImplDefs[i]; 876*f4a2713aSLionel Sambuc for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) 877*f4a2713aSLionel Sambuc if (LiveRange *LR = LIS->getCachedRegUnit(*Units)) 878*f4a2713aSLionel Sambuc LR->createDeadDef(NewMIIdx.getRegSlot(), LIS->getVNInfoAllocator()); 879*f4a2713aSLionel Sambuc } 880*f4a2713aSLionel Sambuc 881*f4a2713aSLionel Sambuc DEBUG(dbgs() << "Remat: " << *NewMI); 882*f4a2713aSLionel Sambuc ++NumReMats; 883*f4a2713aSLionel Sambuc 884*f4a2713aSLionel Sambuc // The source interval can become smaller because we removed a use. 885*f4a2713aSLionel Sambuc LIS->shrinkToUses(&SrcInt, &DeadDefs); 886*f4a2713aSLionel Sambuc if (!DeadDefs.empty()) 887*f4a2713aSLionel Sambuc eliminateDeadDefs(); 888*f4a2713aSLionel Sambuc 889*f4a2713aSLionel Sambuc return true; 890*f4a2713aSLionel Sambuc } 891*f4a2713aSLionel Sambuc 892*f4a2713aSLionel Sambuc /// eliminateUndefCopy - ProcessImpicitDefs may leave some copies of <undef> 893*f4a2713aSLionel Sambuc /// values, it only removes local variables. When we have a copy like: 894*f4a2713aSLionel Sambuc /// 895*f4a2713aSLionel Sambuc /// %vreg1 = COPY %vreg2<undef> 896*f4a2713aSLionel Sambuc /// 897*f4a2713aSLionel Sambuc /// We delete the copy and remove the corresponding value number from %vreg1. 898*f4a2713aSLionel Sambuc /// Any uses of that value number are marked as <undef>. 899*f4a2713aSLionel Sambuc bool RegisterCoalescer::eliminateUndefCopy(MachineInstr *CopyMI, 900*f4a2713aSLionel Sambuc const CoalescerPair &CP) { 901*f4a2713aSLionel Sambuc SlotIndex Idx = LIS->getInstructionIndex(CopyMI); 902*f4a2713aSLionel Sambuc LiveInterval *SrcInt = &LIS->getInterval(CP.getSrcReg()); 903*f4a2713aSLionel Sambuc if (SrcInt->liveAt(Idx)) 904*f4a2713aSLionel Sambuc return false; 905*f4a2713aSLionel Sambuc LiveInterval *DstInt = &LIS->getInterval(CP.getDstReg()); 906*f4a2713aSLionel Sambuc if (DstInt->liveAt(Idx)) 907*f4a2713aSLionel Sambuc return false; 908*f4a2713aSLionel Sambuc 909*f4a2713aSLionel Sambuc // No intervals are live-in to CopyMI - it is undef. 910*f4a2713aSLionel Sambuc if (CP.isFlipped()) 911*f4a2713aSLionel Sambuc DstInt = SrcInt; 912*f4a2713aSLionel Sambuc SrcInt = 0; 913*f4a2713aSLionel Sambuc 914*f4a2713aSLionel Sambuc VNInfo *DeadVNI = DstInt->getVNInfoAt(Idx.getRegSlot()); 915*f4a2713aSLionel Sambuc assert(DeadVNI && "No value defined in DstInt"); 916*f4a2713aSLionel Sambuc DstInt->removeValNo(DeadVNI); 917*f4a2713aSLionel Sambuc 918*f4a2713aSLionel Sambuc // Find new undef uses. 919*f4a2713aSLionel Sambuc for (MachineRegisterInfo::reg_nodbg_iterator 920*f4a2713aSLionel Sambuc I = MRI->reg_nodbg_begin(DstInt->reg), E = MRI->reg_nodbg_end(); 921*f4a2713aSLionel Sambuc I != E; ++I) { 922*f4a2713aSLionel Sambuc MachineOperand &MO = I.getOperand(); 923*f4a2713aSLionel Sambuc if (MO.isDef() || MO.isUndef()) 924*f4a2713aSLionel Sambuc continue; 925*f4a2713aSLionel Sambuc MachineInstr *MI = MO.getParent(); 926*f4a2713aSLionel Sambuc SlotIndex Idx = LIS->getInstructionIndex(MI); 927*f4a2713aSLionel Sambuc if (DstInt->liveAt(Idx)) 928*f4a2713aSLionel Sambuc continue; 929*f4a2713aSLionel Sambuc MO.setIsUndef(true); 930*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tnew undef: " << Idx << '\t' << *MI); 931*f4a2713aSLionel Sambuc } 932*f4a2713aSLionel Sambuc return true; 933*f4a2713aSLionel Sambuc } 934*f4a2713aSLionel Sambuc 935*f4a2713aSLionel Sambuc /// updateRegDefsUses - Replace all defs and uses of SrcReg to DstReg and 936*f4a2713aSLionel Sambuc /// update the subregister number if it is not zero. If DstReg is a 937*f4a2713aSLionel Sambuc /// physical register and the existing subregister number of the def / use 938*f4a2713aSLionel Sambuc /// being updated is not zero, make sure to set it to the correct physical 939*f4a2713aSLionel Sambuc /// subregister. 940*f4a2713aSLionel Sambuc void RegisterCoalescer::updateRegDefsUses(unsigned SrcReg, 941*f4a2713aSLionel Sambuc unsigned DstReg, 942*f4a2713aSLionel Sambuc unsigned SubIdx) { 943*f4a2713aSLionel Sambuc bool DstIsPhys = TargetRegisterInfo::isPhysicalRegister(DstReg); 944*f4a2713aSLionel Sambuc LiveInterval *DstInt = DstIsPhys ? 0 : &LIS->getInterval(DstReg); 945*f4a2713aSLionel Sambuc 946*f4a2713aSLionel Sambuc SmallPtrSet<MachineInstr*, 8> Visited; 947*f4a2713aSLionel Sambuc for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(SrcReg); 948*f4a2713aSLionel Sambuc MachineInstr *UseMI = I.skipInstruction();) { 949*f4a2713aSLionel Sambuc // Each instruction can only be rewritten once because sub-register 950*f4a2713aSLionel Sambuc // composition is not always idempotent. When SrcReg != DstReg, rewriting 951*f4a2713aSLionel Sambuc // the UseMI operands removes them from the SrcReg use-def chain, but when 952*f4a2713aSLionel Sambuc // SrcReg is DstReg we could encounter UseMI twice if it has multiple 953*f4a2713aSLionel Sambuc // operands mentioning the virtual register. 954*f4a2713aSLionel Sambuc if (SrcReg == DstReg && !Visited.insert(UseMI)) 955*f4a2713aSLionel Sambuc continue; 956*f4a2713aSLionel Sambuc 957*f4a2713aSLionel Sambuc SmallVector<unsigned,8> Ops; 958*f4a2713aSLionel Sambuc bool Reads, Writes; 959*f4a2713aSLionel Sambuc tie(Reads, Writes) = UseMI->readsWritesVirtualRegister(SrcReg, &Ops); 960*f4a2713aSLionel Sambuc 961*f4a2713aSLionel Sambuc // If SrcReg wasn't read, it may still be the case that DstReg is live-in 962*f4a2713aSLionel Sambuc // because SrcReg is a sub-register. 963*f4a2713aSLionel Sambuc if (DstInt && !Reads && SubIdx) 964*f4a2713aSLionel Sambuc Reads = DstInt->liveAt(LIS->getInstructionIndex(UseMI)); 965*f4a2713aSLionel Sambuc 966*f4a2713aSLionel Sambuc // Replace SrcReg with DstReg in all UseMI operands. 967*f4a2713aSLionel Sambuc for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 968*f4a2713aSLionel Sambuc MachineOperand &MO = UseMI->getOperand(Ops[i]); 969*f4a2713aSLionel Sambuc 970*f4a2713aSLionel Sambuc // Adjust <undef> flags in case of sub-register joins. We don't want to 971*f4a2713aSLionel Sambuc // turn a full def into a read-modify-write sub-register def and vice 972*f4a2713aSLionel Sambuc // versa. 973*f4a2713aSLionel Sambuc if (SubIdx && MO.isDef()) 974*f4a2713aSLionel Sambuc MO.setIsUndef(!Reads); 975*f4a2713aSLionel Sambuc 976*f4a2713aSLionel Sambuc if (DstIsPhys) 977*f4a2713aSLionel Sambuc MO.substPhysReg(DstReg, *TRI); 978*f4a2713aSLionel Sambuc else 979*f4a2713aSLionel Sambuc MO.substVirtReg(DstReg, SubIdx, *TRI); 980*f4a2713aSLionel Sambuc } 981*f4a2713aSLionel Sambuc 982*f4a2713aSLionel Sambuc DEBUG({ 983*f4a2713aSLionel Sambuc dbgs() << "\t\tupdated: "; 984*f4a2713aSLionel Sambuc if (!UseMI->isDebugValue()) 985*f4a2713aSLionel Sambuc dbgs() << LIS->getInstructionIndex(UseMI) << "\t"; 986*f4a2713aSLionel Sambuc dbgs() << *UseMI; 987*f4a2713aSLionel Sambuc }); 988*f4a2713aSLionel Sambuc } 989*f4a2713aSLionel Sambuc } 990*f4a2713aSLionel Sambuc 991*f4a2713aSLionel Sambuc /// canJoinPhys - Return true if a copy involving a physreg should be joined. 992*f4a2713aSLionel Sambuc bool RegisterCoalescer::canJoinPhys(const CoalescerPair &CP) { 993*f4a2713aSLionel Sambuc /// Always join simple intervals that are defined by a single copy from a 994*f4a2713aSLionel Sambuc /// reserved register. This doesn't increase register pressure, so it is 995*f4a2713aSLionel Sambuc /// always beneficial. 996*f4a2713aSLionel Sambuc if (!MRI->isReserved(CP.getDstReg())) { 997*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tCan only merge into reserved registers.\n"); 998*f4a2713aSLionel Sambuc return false; 999*f4a2713aSLionel Sambuc } 1000*f4a2713aSLionel Sambuc 1001*f4a2713aSLionel Sambuc LiveInterval &JoinVInt = LIS->getInterval(CP.getSrcReg()); 1002*f4a2713aSLionel Sambuc if (CP.isFlipped() && JoinVInt.containsOneValue()) 1003*f4a2713aSLionel Sambuc return true; 1004*f4a2713aSLionel Sambuc 1005*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tCannot join defs into reserved register.\n"); 1006*f4a2713aSLionel Sambuc return false; 1007*f4a2713aSLionel Sambuc } 1008*f4a2713aSLionel Sambuc 1009*f4a2713aSLionel Sambuc /// joinCopy - Attempt to join intervals corresponding to SrcReg/DstReg, 1010*f4a2713aSLionel Sambuc /// which are the src/dst of the copy instruction CopyMI. This returns true 1011*f4a2713aSLionel Sambuc /// if the copy was successfully coalesced away. If it is not currently 1012*f4a2713aSLionel Sambuc /// possible to coalesce this interval, but it may be possible if other 1013*f4a2713aSLionel Sambuc /// things get coalesced, then it returns true by reference in 'Again'. 1014*f4a2713aSLionel Sambuc bool RegisterCoalescer::joinCopy(MachineInstr *CopyMI, bool &Again) { 1015*f4a2713aSLionel Sambuc 1016*f4a2713aSLionel Sambuc Again = false; 1017*f4a2713aSLionel Sambuc DEBUG(dbgs() << LIS->getInstructionIndex(CopyMI) << '\t' << *CopyMI); 1018*f4a2713aSLionel Sambuc 1019*f4a2713aSLionel Sambuc CoalescerPair CP(*TRI); 1020*f4a2713aSLionel Sambuc if (!CP.setRegisters(CopyMI)) { 1021*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tNot coalescable.\n"); 1022*f4a2713aSLionel Sambuc return false; 1023*f4a2713aSLionel Sambuc } 1024*f4a2713aSLionel Sambuc 1025*f4a2713aSLionel Sambuc // Dead code elimination. This really should be handled by MachineDCE, but 1026*f4a2713aSLionel Sambuc // sometimes dead copies slip through, and we can't generate invalid live 1027*f4a2713aSLionel Sambuc // ranges. 1028*f4a2713aSLionel Sambuc if (!CP.isPhys() && CopyMI->allDefsAreDead()) { 1029*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tCopy is dead.\n"); 1030*f4a2713aSLionel Sambuc DeadDefs.push_back(CopyMI); 1031*f4a2713aSLionel Sambuc eliminateDeadDefs(); 1032*f4a2713aSLionel Sambuc return true; 1033*f4a2713aSLionel Sambuc } 1034*f4a2713aSLionel Sambuc 1035*f4a2713aSLionel Sambuc // Eliminate undefs. 1036*f4a2713aSLionel Sambuc if (!CP.isPhys() && eliminateUndefCopy(CopyMI, CP)) { 1037*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tEliminated copy of <undef> value.\n"); 1038*f4a2713aSLionel Sambuc LIS->RemoveMachineInstrFromMaps(CopyMI); 1039*f4a2713aSLionel Sambuc CopyMI->eraseFromParent(); 1040*f4a2713aSLionel Sambuc return false; // Not coalescable. 1041*f4a2713aSLionel Sambuc } 1042*f4a2713aSLionel Sambuc 1043*f4a2713aSLionel Sambuc // Coalesced copies are normally removed immediately, but transformations 1044*f4a2713aSLionel Sambuc // like removeCopyByCommutingDef() can inadvertently create identity copies. 1045*f4a2713aSLionel Sambuc // When that happens, just join the values and remove the copy. 1046*f4a2713aSLionel Sambuc if (CP.getSrcReg() == CP.getDstReg()) { 1047*f4a2713aSLionel Sambuc LiveInterval &LI = LIS->getInterval(CP.getSrcReg()); 1048*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tCopy already coalesced: " << LI << '\n'); 1049*f4a2713aSLionel Sambuc LiveQueryResult LRQ = LI.Query(LIS->getInstructionIndex(CopyMI)); 1050*f4a2713aSLionel Sambuc if (VNInfo *DefVNI = LRQ.valueDefined()) { 1051*f4a2713aSLionel Sambuc VNInfo *ReadVNI = LRQ.valueIn(); 1052*f4a2713aSLionel Sambuc assert(ReadVNI && "No value before copy and no <undef> flag."); 1053*f4a2713aSLionel Sambuc assert(ReadVNI != DefVNI && "Cannot read and define the same value."); 1054*f4a2713aSLionel Sambuc LI.MergeValueNumberInto(DefVNI, ReadVNI); 1055*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tMerged values: " << LI << '\n'); 1056*f4a2713aSLionel Sambuc } 1057*f4a2713aSLionel Sambuc LIS->RemoveMachineInstrFromMaps(CopyMI); 1058*f4a2713aSLionel Sambuc CopyMI->eraseFromParent(); 1059*f4a2713aSLionel Sambuc return true; 1060*f4a2713aSLionel Sambuc } 1061*f4a2713aSLionel Sambuc 1062*f4a2713aSLionel Sambuc // Enforce policies. 1063*f4a2713aSLionel Sambuc if (CP.isPhys()) { 1064*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tConsidering merging " << PrintReg(CP.getSrcReg(), TRI) 1065*f4a2713aSLionel Sambuc << " with " << PrintReg(CP.getDstReg(), TRI, CP.getSrcIdx()) 1066*f4a2713aSLionel Sambuc << '\n'); 1067*f4a2713aSLionel Sambuc if (!canJoinPhys(CP)) { 1068*f4a2713aSLionel Sambuc // Before giving up coalescing, if definition of source is defined by 1069*f4a2713aSLionel Sambuc // trivial computation, try rematerializing it. 1070*f4a2713aSLionel Sambuc bool IsDefCopy; 1071*f4a2713aSLionel Sambuc if (reMaterializeTrivialDef(CP, CopyMI, IsDefCopy)) 1072*f4a2713aSLionel Sambuc return true; 1073*f4a2713aSLionel Sambuc if (IsDefCopy) 1074*f4a2713aSLionel Sambuc Again = true; // May be possible to coalesce later. 1075*f4a2713aSLionel Sambuc return false; 1076*f4a2713aSLionel Sambuc } 1077*f4a2713aSLionel Sambuc } else { 1078*f4a2713aSLionel Sambuc DEBUG({ 1079*f4a2713aSLionel Sambuc dbgs() << "\tConsidering merging to " << CP.getNewRC()->getName() 1080*f4a2713aSLionel Sambuc << " with "; 1081*f4a2713aSLionel Sambuc if (CP.getDstIdx() && CP.getSrcIdx()) 1082*f4a2713aSLionel Sambuc dbgs() << PrintReg(CP.getDstReg()) << " in " 1083*f4a2713aSLionel Sambuc << TRI->getSubRegIndexName(CP.getDstIdx()) << " and " 1084*f4a2713aSLionel Sambuc << PrintReg(CP.getSrcReg()) << " in " 1085*f4a2713aSLionel Sambuc << TRI->getSubRegIndexName(CP.getSrcIdx()) << '\n'; 1086*f4a2713aSLionel Sambuc else 1087*f4a2713aSLionel Sambuc dbgs() << PrintReg(CP.getSrcReg(), TRI) << " in " 1088*f4a2713aSLionel Sambuc << PrintReg(CP.getDstReg(), TRI, CP.getSrcIdx()) << '\n'; 1089*f4a2713aSLionel Sambuc }); 1090*f4a2713aSLionel Sambuc 1091*f4a2713aSLionel Sambuc // When possible, let DstReg be the larger interval. 1092*f4a2713aSLionel Sambuc if (!CP.isPartial() && LIS->getInterval(CP.getSrcReg()).size() > 1093*f4a2713aSLionel Sambuc LIS->getInterval(CP.getDstReg()).size()) 1094*f4a2713aSLionel Sambuc CP.flip(); 1095*f4a2713aSLionel Sambuc } 1096*f4a2713aSLionel Sambuc 1097*f4a2713aSLionel Sambuc // Okay, attempt to join these two intervals. On failure, this returns false. 1098*f4a2713aSLionel Sambuc // Otherwise, if one of the intervals being joined is a physreg, this method 1099*f4a2713aSLionel Sambuc // always canonicalizes DstInt to be it. The output "SrcInt" will not have 1100*f4a2713aSLionel Sambuc // been modified, so we can use this information below to update aliases. 1101*f4a2713aSLionel Sambuc if (!joinIntervals(CP)) { 1102*f4a2713aSLionel Sambuc // Coalescing failed. 1103*f4a2713aSLionel Sambuc 1104*f4a2713aSLionel Sambuc // If definition of source is defined by trivial computation, try 1105*f4a2713aSLionel Sambuc // rematerializing it. 1106*f4a2713aSLionel Sambuc bool IsDefCopy; 1107*f4a2713aSLionel Sambuc if (reMaterializeTrivialDef(CP, CopyMI, IsDefCopy)) 1108*f4a2713aSLionel Sambuc return true; 1109*f4a2713aSLionel Sambuc 1110*f4a2713aSLionel Sambuc // If we can eliminate the copy without merging the live segments, do so 1111*f4a2713aSLionel Sambuc // now. 1112*f4a2713aSLionel Sambuc if (!CP.isPartial() && !CP.isPhys()) { 1113*f4a2713aSLionel Sambuc if (adjustCopiesBackFrom(CP, CopyMI) || 1114*f4a2713aSLionel Sambuc removeCopyByCommutingDef(CP, CopyMI)) { 1115*f4a2713aSLionel Sambuc LIS->RemoveMachineInstrFromMaps(CopyMI); 1116*f4a2713aSLionel Sambuc CopyMI->eraseFromParent(); 1117*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tTrivial!\n"); 1118*f4a2713aSLionel Sambuc return true; 1119*f4a2713aSLionel Sambuc } 1120*f4a2713aSLionel Sambuc } 1121*f4a2713aSLionel Sambuc 1122*f4a2713aSLionel Sambuc // Otherwise, we are unable to join the intervals. 1123*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\tInterference!\n"); 1124*f4a2713aSLionel Sambuc Again = true; // May be possible to coalesce later. 1125*f4a2713aSLionel Sambuc return false; 1126*f4a2713aSLionel Sambuc } 1127*f4a2713aSLionel Sambuc 1128*f4a2713aSLionel Sambuc // Coalescing to a virtual register that is of a sub-register class of the 1129*f4a2713aSLionel Sambuc // other. Make sure the resulting register is set to the right register class. 1130*f4a2713aSLionel Sambuc if (CP.isCrossClass()) { 1131*f4a2713aSLionel Sambuc ++numCrossRCs; 1132*f4a2713aSLionel Sambuc MRI->setRegClass(CP.getDstReg(), CP.getNewRC()); 1133*f4a2713aSLionel Sambuc } 1134*f4a2713aSLionel Sambuc 1135*f4a2713aSLionel Sambuc // Removing sub-register copies can ease the register class constraints. 1136*f4a2713aSLionel Sambuc // Make sure we attempt to inflate the register class of DstReg. 1137*f4a2713aSLionel Sambuc if (!CP.isPhys() && RegClassInfo.isProperSubClass(CP.getNewRC())) 1138*f4a2713aSLionel Sambuc InflateRegs.push_back(CP.getDstReg()); 1139*f4a2713aSLionel Sambuc 1140*f4a2713aSLionel Sambuc // CopyMI has been erased by joinIntervals at this point. Remove it from 1141*f4a2713aSLionel Sambuc // ErasedInstrs since copyCoalesceWorkList() won't add a successful join back 1142*f4a2713aSLionel Sambuc // to the work list. This keeps ErasedInstrs from growing needlessly. 1143*f4a2713aSLionel Sambuc ErasedInstrs.erase(CopyMI); 1144*f4a2713aSLionel Sambuc 1145*f4a2713aSLionel Sambuc // Rewrite all SrcReg operands to DstReg. 1146*f4a2713aSLionel Sambuc // Also update DstReg operands to include DstIdx if it is set. 1147*f4a2713aSLionel Sambuc if (CP.getDstIdx()) 1148*f4a2713aSLionel Sambuc updateRegDefsUses(CP.getDstReg(), CP.getDstReg(), CP.getDstIdx()); 1149*f4a2713aSLionel Sambuc updateRegDefsUses(CP.getSrcReg(), CP.getDstReg(), CP.getSrcIdx()); 1150*f4a2713aSLionel Sambuc 1151*f4a2713aSLionel Sambuc // SrcReg is guaranteed to be the register whose live interval that is 1152*f4a2713aSLionel Sambuc // being merged. 1153*f4a2713aSLionel Sambuc LIS->removeInterval(CP.getSrcReg()); 1154*f4a2713aSLionel Sambuc 1155*f4a2713aSLionel Sambuc // Update regalloc hint. 1156*f4a2713aSLionel Sambuc TRI->UpdateRegAllocHint(CP.getSrcReg(), CP.getDstReg(), *MF); 1157*f4a2713aSLionel Sambuc 1158*f4a2713aSLionel Sambuc DEBUG({ 1159*f4a2713aSLionel Sambuc dbgs() << "\tJoined. Result = "; 1160*f4a2713aSLionel Sambuc if (CP.isPhys()) 1161*f4a2713aSLionel Sambuc dbgs() << PrintReg(CP.getDstReg(), TRI); 1162*f4a2713aSLionel Sambuc else 1163*f4a2713aSLionel Sambuc dbgs() << LIS->getInterval(CP.getDstReg()); 1164*f4a2713aSLionel Sambuc dbgs() << '\n'; 1165*f4a2713aSLionel Sambuc }); 1166*f4a2713aSLionel Sambuc 1167*f4a2713aSLionel Sambuc ++numJoins; 1168*f4a2713aSLionel Sambuc return true; 1169*f4a2713aSLionel Sambuc } 1170*f4a2713aSLionel Sambuc 1171*f4a2713aSLionel Sambuc /// Attempt joining with a reserved physreg. 1172*f4a2713aSLionel Sambuc bool RegisterCoalescer::joinReservedPhysReg(CoalescerPair &CP) { 1173*f4a2713aSLionel Sambuc assert(CP.isPhys() && "Must be a physreg copy"); 1174*f4a2713aSLionel Sambuc assert(MRI->isReserved(CP.getDstReg()) && "Not a reserved register"); 1175*f4a2713aSLionel Sambuc LiveInterval &RHS = LIS->getInterval(CP.getSrcReg()); 1176*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tRHS = " << RHS << '\n'); 1177*f4a2713aSLionel Sambuc 1178*f4a2713aSLionel Sambuc assert(CP.isFlipped() && RHS.containsOneValue() && 1179*f4a2713aSLionel Sambuc "Invalid join with reserved register"); 1180*f4a2713aSLionel Sambuc 1181*f4a2713aSLionel Sambuc // Optimization for reserved registers like ESP. We can only merge with a 1182*f4a2713aSLionel Sambuc // reserved physreg if RHS has a single value that is a copy of CP.DstReg(). 1183*f4a2713aSLionel Sambuc // The live range of the reserved register will look like a set of dead defs 1184*f4a2713aSLionel Sambuc // - we don't properly track the live range of reserved registers. 1185*f4a2713aSLionel Sambuc 1186*f4a2713aSLionel Sambuc // Deny any overlapping intervals. This depends on all the reserved 1187*f4a2713aSLionel Sambuc // register live ranges to look like dead defs. 1188*f4a2713aSLionel Sambuc for (MCRegUnitIterator UI(CP.getDstReg(), TRI); UI.isValid(); ++UI) 1189*f4a2713aSLionel Sambuc if (RHS.overlaps(LIS->getRegUnit(*UI))) { 1190*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tInterference: " << PrintRegUnit(*UI, TRI) << '\n'); 1191*f4a2713aSLionel Sambuc return false; 1192*f4a2713aSLionel Sambuc } 1193*f4a2713aSLionel Sambuc 1194*f4a2713aSLionel Sambuc // Skip any value computations, we are not adding new values to the 1195*f4a2713aSLionel Sambuc // reserved register. Also skip merging the live ranges, the reserved 1196*f4a2713aSLionel Sambuc // register live range doesn't need to be accurate as long as all the 1197*f4a2713aSLionel Sambuc // defs are there. 1198*f4a2713aSLionel Sambuc 1199*f4a2713aSLionel Sambuc // Delete the identity copy. 1200*f4a2713aSLionel Sambuc MachineInstr *CopyMI = MRI->getVRegDef(RHS.reg); 1201*f4a2713aSLionel Sambuc LIS->RemoveMachineInstrFromMaps(CopyMI); 1202*f4a2713aSLionel Sambuc CopyMI->eraseFromParent(); 1203*f4a2713aSLionel Sambuc 1204*f4a2713aSLionel Sambuc // We don't track kills for reserved registers. 1205*f4a2713aSLionel Sambuc MRI->clearKillFlags(CP.getSrcReg()); 1206*f4a2713aSLionel Sambuc 1207*f4a2713aSLionel Sambuc return true; 1208*f4a2713aSLionel Sambuc } 1209*f4a2713aSLionel Sambuc 1210*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 1211*f4a2713aSLionel Sambuc // Interference checking and interval joining 1212*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 1213*f4a2713aSLionel Sambuc // 1214*f4a2713aSLionel Sambuc // In the easiest case, the two live ranges being joined are disjoint, and 1215*f4a2713aSLionel Sambuc // there is no interference to consider. It is quite common, though, to have 1216*f4a2713aSLionel Sambuc // overlapping live ranges, and we need to check if the interference can be 1217*f4a2713aSLionel Sambuc // resolved. 1218*f4a2713aSLionel Sambuc // 1219*f4a2713aSLionel Sambuc // The live range of a single SSA value forms a sub-tree of the dominator tree. 1220*f4a2713aSLionel Sambuc // This means that two SSA values overlap if and only if the def of one value 1221*f4a2713aSLionel Sambuc // is contained in the live range of the other value. As a special case, the 1222*f4a2713aSLionel Sambuc // overlapping values can be defined at the same index. 1223*f4a2713aSLionel Sambuc // 1224*f4a2713aSLionel Sambuc // The interference from an overlapping def can be resolved in these cases: 1225*f4a2713aSLionel Sambuc // 1226*f4a2713aSLionel Sambuc // 1. Coalescable copies. The value is defined by a copy that would become an 1227*f4a2713aSLionel Sambuc // identity copy after joining SrcReg and DstReg. The copy instruction will 1228*f4a2713aSLionel Sambuc // be removed, and the value will be merged with the source value. 1229*f4a2713aSLionel Sambuc // 1230*f4a2713aSLionel Sambuc // There can be several copies back and forth, causing many values to be 1231*f4a2713aSLionel Sambuc // merged into one. We compute a list of ultimate values in the joined live 1232*f4a2713aSLionel Sambuc // range as well as a mappings from the old value numbers. 1233*f4a2713aSLionel Sambuc // 1234*f4a2713aSLionel Sambuc // 2. IMPLICIT_DEF. This instruction is only inserted to ensure all PHI 1235*f4a2713aSLionel Sambuc // predecessors have a live out value. It doesn't cause real interference, 1236*f4a2713aSLionel Sambuc // and can be merged into the value it overlaps. Like a coalescable copy, it 1237*f4a2713aSLionel Sambuc // can be erased after joining. 1238*f4a2713aSLionel Sambuc // 1239*f4a2713aSLionel Sambuc // 3. Copy of external value. The overlapping def may be a copy of a value that 1240*f4a2713aSLionel Sambuc // is already in the other register. This is like a coalescable copy, but 1241*f4a2713aSLionel Sambuc // the live range of the source register must be trimmed after erasing the 1242*f4a2713aSLionel Sambuc // copy instruction: 1243*f4a2713aSLionel Sambuc // 1244*f4a2713aSLionel Sambuc // %src = COPY %ext 1245*f4a2713aSLionel Sambuc // %dst = COPY %ext <-- Remove this COPY, trim the live range of %ext. 1246*f4a2713aSLionel Sambuc // 1247*f4a2713aSLionel Sambuc // 4. Clobbering undefined lanes. Vector registers are sometimes built by 1248*f4a2713aSLionel Sambuc // defining one lane at a time: 1249*f4a2713aSLionel Sambuc // 1250*f4a2713aSLionel Sambuc // %dst:ssub0<def,read-undef> = FOO 1251*f4a2713aSLionel Sambuc // %src = BAR 1252*f4a2713aSLionel Sambuc // %dst:ssub1<def> = COPY %src 1253*f4a2713aSLionel Sambuc // 1254*f4a2713aSLionel Sambuc // The live range of %src overlaps the %dst value defined by FOO, but 1255*f4a2713aSLionel Sambuc // merging %src into %dst:ssub1 is only going to clobber the ssub1 lane 1256*f4a2713aSLionel Sambuc // which was undef anyway. 1257*f4a2713aSLionel Sambuc // 1258*f4a2713aSLionel Sambuc // The value mapping is more complicated in this case. The final live range 1259*f4a2713aSLionel Sambuc // will have different value numbers for both FOO and BAR, but there is no 1260*f4a2713aSLionel Sambuc // simple mapping from old to new values. It may even be necessary to add 1261*f4a2713aSLionel Sambuc // new PHI values. 1262*f4a2713aSLionel Sambuc // 1263*f4a2713aSLionel Sambuc // 5. Clobbering dead lanes. A def may clobber a lane of a vector register that 1264*f4a2713aSLionel Sambuc // is live, but never read. This can happen because we don't compute 1265*f4a2713aSLionel Sambuc // individual live ranges per lane. 1266*f4a2713aSLionel Sambuc // 1267*f4a2713aSLionel Sambuc // %dst<def> = FOO 1268*f4a2713aSLionel Sambuc // %src = BAR 1269*f4a2713aSLionel Sambuc // %dst:ssub1<def> = COPY %src 1270*f4a2713aSLionel Sambuc // 1271*f4a2713aSLionel Sambuc // This kind of interference is only resolved locally. If the clobbered 1272*f4a2713aSLionel Sambuc // lane value escapes the block, the join is aborted. 1273*f4a2713aSLionel Sambuc 1274*f4a2713aSLionel Sambuc namespace { 1275*f4a2713aSLionel Sambuc /// Track information about values in a single virtual register about to be 1276*f4a2713aSLionel Sambuc /// joined. Objects of this class are always created in pairs - one for each 1277*f4a2713aSLionel Sambuc /// side of the CoalescerPair. 1278*f4a2713aSLionel Sambuc class JoinVals { 1279*f4a2713aSLionel Sambuc LiveInterval &LI; 1280*f4a2713aSLionel Sambuc 1281*f4a2713aSLionel Sambuc // Location of this register in the final joined register. 1282*f4a2713aSLionel Sambuc // Either CP.DstIdx or CP.SrcIdx. 1283*f4a2713aSLionel Sambuc unsigned SubIdx; 1284*f4a2713aSLionel Sambuc 1285*f4a2713aSLionel Sambuc // Values that will be present in the final live range. 1286*f4a2713aSLionel Sambuc SmallVectorImpl<VNInfo*> &NewVNInfo; 1287*f4a2713aSLionel Sambuc 1288*f4a2713aSLionel Sambuc const CoalescerPair &CP; 1289*f4a2713aSLionel Sambuc LiveIntervals *LIS; 1290*f4a2713aSLionel Sambuc SlotIndexes *Indexes; 1291*f4a2713aSLionel Sambuc const TargetRegisterInfo *TRI; 1292*f4a2713aSLionel Sambuc 1293*f4a2713aSLionel Sambuc // Value number assignments. Maps value numbers in LI to entries in NewVNInfo. 1294*f4a2713aSLionel Sambuc // This is suitable for passing to LiveInterval::join(). 1295*f4a2713aSLionel Sambuc SmallVector<int, 8> Assignments; 1296*f4a2713aSLionel Sambuc 1297*f4a2713aSLionel Sambuc // Conflict resolution for overlapping values. 1298*f4a2713aSLionel Sambuc enum ConflictResolution { 1299*f4a2713aSLionel Sambuc // No overlap, simply keep this value. 1300*f4a2713aSLionel Sambuc CR_Keep, 1301*f4a2713aSLionel Sambuc 1302*f4a2713aSLionel Sambuc // Merge this value into OtherVNI and erase the defining instruction. 1303*f4a2713aSLionel Sambuc // Used for IMPLICIT_DEF, coalescable copies, and copies from external 1304*f4a2713aSLionel Sambuc // values. 1305*f4a2713aSLionel Sambuc CR_Erase, 1306*f4a2713aSLionel Sambuc 1307*f4a2713aSLionel Sambuc // Merge this value into OtherVNI but keep the defining instruction. 1308*f4a2713aSLionel Sambuc // This is for the special case where OtherVNI is defined by the same 1309*f4a2713aSLionel Sambuc // instruction. 1310*f4a2713aSLionel Sambuc CR_Merge, 1311*f4a2713aSLionel Sambuc 1312*f4a2713aSLionel Sambuc // Keep this value, and have it replace OtherVNI where possible. This 1313*f4a2713aSLionel Sambuc // complicates value mapping since OtherVNI maps to two different values 1314*f4a2713aSLionel Sambuc // before and after this def. 1315*f4a2713aSLionel Sambuc // Used when clobbering undefined or dead lanes. 1316*f4a2713aSLionel Sambuc CR_Replace, 1317*f4a2713aSLionel Sambuc 1318*f4a2713aSLionel Sambuc // Unresolved conflict. Visit later when all values have been mapped. 1319*f4a2713aSLionel Sambuc CR_Unresolved, 1320*f4a2713aSLionel Sambuc 1321*f4a2713aSLionel Sambuc // Unresolvable conflict. Abort the join. 1322*f4a2713aSLionel Sambuc CR_Impossible 1323*f4a2713aSLionel Sambuc }; 1324*f4a2713aSLionel Sambuc 1325*f4a2713aSLionel Sambuc // Per-value info for LI. The lane bit masks are all relative to the final 1326*f4a2713aSLionel Sambuc // joined register, so they can be compared directly between SrcReg and 1327*f4a2713aSLionel Sambuc // DstReg. 1328*f4a2713aSLionel Sambuc struct Val { 1329*f4a2713aSLionel Sambuc ConflictResolution Resolution; 1330*f4a2713aSLionel Sambuc 1331*f4a2713aSLionel Sambuc // Lanes written by this def, 0 for unanalyzed values. 1332*f4a2713aSLionel Sambuc unsigned WriteLanes; 1333*f4a2713aSLionel Sambuc 1334*f4a2713aSLionel Sambuc // Lanes with defined values in this register. Other lanes are undef and 1335*f4a2713aSLionel Sambuc // safe to clobber. 1336*f4a2713aSLionel Sambuc unsigned ValidLanes; 1337*f4a2713aSLionel Sambuc 1338*f4a2713aSLionel Sambuc // Value in LI being redefined by this def. 1339*f4a2713aSLionel Sambuc VNInfo *RedefVNI; 1340*f4a2713aSLionel Sambuc 1341*f4a2713aSLionel Sambuc // Value in the other live range that overlaps this def, if any. 1342*f4a2713aSLionel Sambuc VNInfo *OtherVNI; 1343*f4a2713aSLionel Sambuc 1344*f4a2713aSLionel Sambuc // Is this value an IMPLICIT_DEF that can be erased? 1345*f4a2713aSLionel Sambuc // 1346*f4a2713aSLionel Sambuc // IMPLICIT_DEF values should only exist at the end of a basic block that 1347*f4a2713aSLionel Sambuc // is a predecessor to a phi-value. These IMPLICIT_DEF instructions can be 1348*f4a2713aSLionel Sambuc // safely erased if they are overlapping a live value in the other live 1349*f4a2713aSLionel Sambuc // interval. 1350*f4a2713aSLionel Sambuc // 1351*f4a2713aSLionel Sambuc // Weird control flow graphs and incomplete PHI handling in 1352*f4a2713aSLionel Sambuc // ProcessImplicitDefs can very rarely create IMPLICIT_DEF values with 1353*f4a2713aSLionel Sambuc // longer live ranges. Such IMPLICIT_DEF values should be treated like 1354*f4a2713aSLionel Sambuc // normal values. 1355*f4a2713aSLionel Sambuc bool ErasableImplicitDef; 1356*f4a2713aSLionel Sambuc 1357*f4a2713aSLionel Sambuc // True when the live range of this value will be pruned because of an 1358*f4a2713aSLionel Sambuc // overlapping CR_Replace value in the other live range. 1359*f4a2713aSLionel Sambuc bool Pruned; 1360*f4a2713aSLionel Sambuc 1361*f4a2713aSLionel Sambuc // True once Pruned above has been computed. 1362*f4a2713aSLionel Sambuc bool PrunedComputed; 1363*f4a2713aSLionel Sambuc 1364*f4a2713aSLionel Sambuc Val() : Resolution(CR_Keep), WriteLanes(0), ValidLanes(0), 1365*f4a2713aSLionel Sambuc RedefVNI(0), OtherVNI(0), ErasableImplicitDef(false), 1366*f4a2713aSLionel Sambuc Pruned(false), PrunedComputed(false) {} 1367*f4a2713aSLionel Sambuc 1368*f4a2713aSLionel Sambuc bool isAnalyzed() const { return WriteLanes != 0; } 1369*f4a2713aSLionel Sambuc }; 1370*f4a2713aSLionel Sambuc 1371*f4a2713aSLionel Sambuc // One entry per value number in LI. 1372*f4a2713aSLionel Sambuc SmallVector<Val, 8> Vals; 1373*f4a2713aSLionel Sambuc 1374*f4a2713aSLionel Sambuc unsigned computeWriteLanes(const MachineInstr *DefMI, bool &Redef); 1375*f4a2713aSLionel Sambuc VNInfo *stripCopies(VNInfo *VNI); 1376*f4a2713aSLionel Sambuc ConflictResolution analyzeValue(unsigned ValNo, JoinVals &Other); 1377*f4a2713aSLionel Sambuc void computeAssignment(unsigned ValNo, JoinVals &Other); 1378*f4a2713aSLionel Sambuc bool taintExtent(unsigned, unsigned, JoinVals&, 1379*f4a2713aSLionel Sambuc SmallVectorImpl<std::pair<SlotIndex, unsigned> >&); 1380*f4a2713aSLionel Sambuc bool usesLanes(MachineInstr *MI, unsigned, unsigned, unsigned); 1381*f4a2713aSLionel Sambuc bool isPrunedValue(unsigned ValNo, JoinVals &Other); 1382*f4a2713aSLionel Sambuc 1383*f4a2713aSLionel Sambuc public: 1384*f4a2713aSLionel Sambuc JoinVals(LiveInterval &li, unsigned subIdx, 1385*f4a2713aSLionel Sambuc SmallVectorImpl<VNInfo*> &newVNInfo, 1386*f4a2713aSLionel Sambuc const CoalescerPair &cp, 1387*f4a2713aSLionel Sambuc LiveIntervals *lis, 1388*f4a2713aSLionel Sambuc const TargetRegisterInfo *tri) 1389*f4a2713aSLionel Sambuc : LI(li), SubIdx(subIdx), NewVNInfo(newVNInfo), CP(cp), LIS(lis), 1390*f4a2713aSLionel Sambuc Indexes(LIS->getSlotIndexes()), TRI(tri), 1391*f4a2713aSLionel Sambuc Assignments(LI.getNumValNums(), -1), Vals(LI.getNumValNums()) 1392*f4a2713aSLionel Sambuc {} 1393*f4a2713aSLionel Sambuc 1394*f4a2713aSLionel Sambuc /// Analyze defs in LI and compute a value mapping in NewVNInfo. 1395*f4a2713aSLionel Sambuc /// Returns false if any conflicts were impossible to resolve. 1396*f4a2713aSLionel Sambuc bool mapValues(JoinVals &Other); 1397*f4a2713aSLionel Sambuc 1398*f4a2713aSLionel Sambuc /// Try to resolve conflicts that require all values to be mapped. 1399*f4a2713aSLionel Sambuc /// Returns false if any conflicts were impossible to resolve. 1400*f4a2713aSLionel Sambuc bool resolveConflicts(JoinVals &Other); 1401*f4a2713aSLionel Sambuc 1402*f4a2713aSLionel Sambuc /// Prune the live range of values in Other.LI where they would conflict with 1403*f4a2713aSLionel Sambuc /// CR_Replace values in LI. Collect end points for restoring the live range 1404*f4a2713aSLionel Sambuc /// after joining. 1405*f4a2713aSLionel Sambuc void pruneValues(JoinVals &Other, SmallVectorImpl<SlotIndex> &EndPoints); 1406*f4a2713aSLionel Sambuc 1407*f4a2713aSLionel Sambuc /// Erase any machine instructions that have been coalesced away. 1408*f4a2713aSLionel Sambuc /// Add erased instructions to ErasedInstrs. 1409*f4a2713aSLionel Sambuc /// Add foreign virtual registers to ShrinkRegs if their live range ended at 1410*f4a2713aSLionel Sambuc /// the erased instrs. 1411*f4a2713aSLionel Sambuc void eraseInstrs(SmallPtrSet<MachineInstr*, 8> &ErasedInstrs, 1412*f4a2713aSLionel Sambuc SmallVectorImpl<unsigned> &ShrinkRegs); 1413*f4a2713aSLionel Sambuc 1414*f4a2713aSLionel Sambuc /// Get the value assignments suitable for passing to LiveInterval::join. 1415*f4a2713aSLionel Sambuc const int *getAssignments() const { return Assignments.data(); } 1416*f4a2713aSLionel Sambuc }; 1417*f4a2713aSLionel Sambuc } // end anonymous namespace 1418*f4a2713aSLionel Sambuc 1419*f4a2713aSLionel Sambuc /// Compute the bitmask of lanes actually written by DefMI. 1420*f4a2713aSLionel Sambuc /// Set Redef if there are any partial register definitions that depend on the 1421*f4a2713aSLionel Sambuc /// previous value of the register. 1422*f4a2713aSLionel Sambuc unsigned JoinVals::computeWriteLanes(const MachineInstr *DefMI, bool &Redef) { 1423*f4a2713aSLionel Sambuc unsigned L = 0; 1424*f4a2713aSLionel Sambuc for (ConstMIOperands MO(DefMI); MO.isValid(); ++MO) { 1425*f4a2713aSLionel Sambuc if (!MO->isReg() || MO->getReg() != LI.reg || !MO->isDef()) 1426*f4a2713aSLionel Sambuc continue; 1427*f4a2713aSLionel Sambuc L |= TRI->getSubRegIndexLaneMask( 1428*f4a2713aSLionel Sambuc TRI->composeSubRegIndices(SubIdx, MO->getSubReg())); 1429*f4a2713aSLionel Sambuc if (MO->readsReg()) 1430*f4a2713aSLionel Sambuc Redef = true; 1431*f4a2713aSLionel Sambuc } 1432*f4a2713aSLionel Sambuc return L; 1433*f4a2713aSLionel Sambuc } 1434*f4a2713aSLionel Sambuc 1435*f4a2713aSLionel Sambuc /// Find the ultimate value that VNI was copied from. 1436*f4a2713aSLionel Sambuc VNInfo *JoinVals::stripCopies(VNInfo *VNI) { 1437*f4a2713aSLionel Sambuc while (!VNI->isPHIDef()) { 1438*f4a2713aSLionel Sambuc MachineInstr *MI = Indexes->getInstructionFromIndex(VNI->def); 1439*f4a2713aSLionel Sambuc assert(MI && "No defining instruction"); 1440*f4a2713aSLionel Sambuc if (!MI->isFullCopy()) 1441*f4a2713aSLionel Sambuc break; 1442*f4a2713aSLionel Sambuc unsigned Reg = MI->getOperand(1).getReg(); 1443*f4a2713aSLionel Sambuc if (!TargetRegisterInfo::isVirtualRegister(Reg)) 1444*f4a2713aSLionel Sambuc break; 1445*f4a2713aSLionel Sambuc LiveQueryResult LRQ = LIS->getInterval(Reg).Query(VNI->def); 1446*f4a2713aSLionel Sambuc if (!LRQ.valueIn()) 1447*f4a2713aSLionel Sambuc break; 1448*f4a2713aSLionel Sambuc VNI = LRQ.valueIn(); 1449*f4a2713aSLionel Sambuc } 1450*f4a2713aSLionel Sambuc return VNI; 1451*f4a2713aSLionel Sambuc } 1452*f4a2713aSLionel Sambuc 1453*f4a2713aSLionel Sambuc /// Analyze ValNo in this live range, and set all fields of Vals[ValNo]. 1454*f4a2713aSLionel Sambuc /// Return a conflict resolution when possible, but leave the hard cases as 1455*f4a2713aSLionel Sambuc /// CR_Unresolved. 1456*f4a2713aSLionel Sambuc /// Recursively calls computeAssignment() on this and Other, guaranteeing that 1457*f4a2713aSLionel Sambuc /// both OtherVNI and RedefVNI have been analyzed and mapped before returning. 1458*f4a2713aSLionel Sambuc /// The recursion always goes upwards in the dominator tree, making loops 1459*f4a2713aSLionel Sambuc /// impossible. 1460*f4a2713aSLionel Sambuc JoinVals::ConflictResolution 1461*f4a2713aSLionel Sambuc JoinVals::analyzeValue(unsigned ValNo, JoinVals &Other) { 1462*f4a2713aSLionel Sambuc Val &V = Vals[ValNo]; 1463*f4a2713aSLionel Sambuc assert(!V.isAnalyzed() && "Value has already been analyzed!"); 1464*f4a2713aSLionel Sambuc VNInfo *VNI = LI.getValNumInfo(ValNo); 1465*f4a2713aSLionel Sambuc if (VNI->isUnused()) { 1466*f4a2713aSLionel Sambuc V.WriteLanes = ~0u; 1467*f4a2713aSLionel Sambuc return CR_Keep; 1468*f4a2713aSLionel Sambuc } 1469*f4a2713aSLionel Sambuc 1470*f4a2713aSLionel Sambuc // Get the instruction defining this value, compute the lanes written. 1471*f4a2713aSLionel Sambuc const MachineInstr *DefMI = 0; 1472*f4a2713aSLionel Sambuc if (VNI->isPHIDef()) { 1473*f4a2713aSLionel Sambuc // Conservatively assume that all lanes in a PHI are valid. 1474*f4a2713aSLionel Sambuc V.ValidLanes = V.WriteLanes = TRI->getSubRegIndexLaneMask(SubIdx); 1475*f4a2713aSLionel Sambuc } else { 1476*f4a2713aSLionel Sambuc DefMI = Indexes->getInstructionFromIndex(VNI->def); 1477*f4a2713aSLionel Sambuc bool Redef = false; 1478*f4a2713aSLionel Sambuc V.ValidLanes = V.WriteLanes = computeWriteLanes(DefMI, Redef); 1479*f4a2713aSLionel Sambuc 1480*f4a2713aSLionel Sambuc // If this is a read-modify-write instruction, there may be more valid 1481*f4a2713aSLionel Sambuc // lanes than the ones written by this instruction. 1482*f4a2713aSLionel Sambuc // This only covers partial redef operands. DefMI may have normal use 1483*f4a2713aSLionel Sambuc // operands reading the register. They don't contribute valid lanes. 1484*f4a2713aSLionel Sambuc // 1485*f4a2713aSLionel Sambuc // This adds ssub1 to the set of valid lanes in %src: 1486*f4a2713aSLionel Sambuc // 1487*f4a2713aSLionel Sambuc // %src:ssub1<def> = FOO 1488*f4a2713aSLionel Sambuc // 1489*f4a2713aSLionel Sambuc // This leaves only ssub1 valid, making any other lanes undef: 1490*f4a2713aSLionel Sambuc // 1491*f4a2713aSLionel Sambuc // %src:ssub1<def,read-undef> = FOO %src:ssub2 1492*f4a2713aSLionel Sambuc // 1493*f4a2713aSLionel Sambuc // The <read-undef> flag on the def operand means that old lane values are 1494*f4a2713aSLionel Sambuc // not important. 1495*f4a2713aSLionel Sambuc if (Redef) { 1496*f4a2713aSLionel Sambuc V.RedefVNI = LI.Query(VNI->def).valueIn(); 1497*f4a2713aSLionel Sambuc assert(V.RedefVNI && "Instruction is reading nonexistent value"); 1498*f4a2713aSLionel Sambuc computeAssignment(V.RedefVNI->id, Other); 1499*f4a2713aSLionel Sambuc V.ValidLanes |= Vals[V.RedefVNI->id].ValidLanes; 1500*f4a2713aSLionel Sambuc } 1501*f4a2713aSLionel Sambuc 1502*f4a2713aSLionel Sambuc // An IMPLICIT_DEF writes undef values. 1503*f4a2713aSLionel Sambuc if (DefMI->isImplicitDef()) { 1504*f4a2713aSLionel Sambuc // We normally expect IMPLICIT_DEF values to be live only until the end 1505*f4a2713aSLionel Sambuc // of their block. If the value is really live longer and gets pruned in 1506*f4a2713aSLionel Sambuc // another block, this flag is cleared again. 1507*f4a2713aSLionel Sambuc V.ErasableImplicitDef = true; 1508*f4a2713aSLionel Sambuc V.ValidLanes &= ~V.WriteLanes; 1509*f4a2713aSLionel Sambuc } 1510*f4a2713aSLionel Sambuc } 1511*f4a2713aSLionel Sambuc 1512*f4a2713aSLionel Sambuc // Find the value in Other that overlaps VNI->def, if any. 1513*f4a2713aSLionel Sambuc LiveQueryResult OtherLRQ = Other.LI.Query(VNI->def); 1514*f4a2713aSLionel Sambuc 1515*f4a2713aSLionel Sambuc // It is possible that both values are defined by the same instruction, or 1516*f4a2713aSLionel Sambuc // the values are PHIs defined in the same block. When that happens, the two 1517*f4a2713aSLionel Sambuc // values should be merged into one, but not into any preceding value. 1518*f4a2713aSLionel Sambuc // The first value defined or visited gets CR_Keep, the other gets CR_Merge. 1519*f4a2713aSLionel Sambuc if (VNInfo *OtherVNI = OtherLRQ.valueDefined()) { 1520*f4a2713aSLionel Sambuc assert(SlotIndex::isSameInstr(VNI->def, OtherVNI->def) && "Broken LRQ"); 1521*f4a2713aSLionel Sambuc 1522*f4a2713aSLionel Sambuc // One value stays, the other is merged. Keep the earlier one, or the first 1523*f4a2713aSLionel Sambuc // one we see. 1524*f4a2713aSLionel Sambuc if (OtherVNI->def < VNI->def) 1525*f4a2713aSLionel Sambuc Other.computeAssignment(OtherVNI->id, *this); 1526*f4a2713aSLionel Sambuc else if (VNI->def < OtherVNI->def && OtherLRQ.valueIn()) { 1527*f4a2713aSLionel Sambuc // This is an early-clobber def overlapping a live-in value in the other 1528*f4a2713aSLionel Sambuc // register. Not mergeable. 1529*f4a2713aSLionel Sambuc V.OtherVNI = OtherLRQ.valueIn(); 1530*f4a2713aSLionel Sambuc return CR_Impossible; 1531*f4a2713aSLionel Sambuc } 1532*f4a2713aSLionel Sambuc V.OtherVNI = OtherVNI; 1533*f4a2713aSLionel Sambuc Val &OtherV = Other.Vals[OtherVNI->id]; 1534*f4a2713aSLionel Sambuc // Keep this value, check for conflicts when analyzing OtherVNI. 1535*f4a2713aSLionel Sambuc if (!OtherV.isAnalyzed()) 1536*f4a2713aSLionel Sambuc return CR_Keep; 1537*f4a2713aSLionel Sambuc // Both sides have been analyzed now. 1538*f4a2713aSLionel Sambuc // Allow overlapping PHI values. Any real interference would show up in a 1539*f4a2713aSLionel Sambuc // predecessor, the PHI itself can't introduce any conflicts. 1540*f4a2713aSLionel Sambuc if (VNI->isPHIDef()) 1541*f4a2713aSLionel Sambuc return CR_Merge; 1542*f4a2713aSLionel Sambuc if (V.ValidLanes & OtherV.ValidLanes) 1543*f4a2713aSLionel Sambuc // Overlapping lanes can't be resolved. 1544*f4a2713aSLionel Sambuc return CR_Impossible; 1545*f4a2713aSLionel Sambuc else 1546*f4a2713aSLionel Sambuc return CR_Merge; 1547*f4a2713aSLionel Sambuc } 1548*f4a2713aSLionel Sambuc 1549*f4a2713aSLionel Sambuc // No simultaneous def. Is Other live at the def? 1550*f4a2713aSLionel Sambuc V.OtherVNI = OtherLRQ.valueIn(); 1551*f4a2713aSLionel Sambuc if (!V.OtherVNI) 1552*f4a2713aSLionel Sambuc // No overlap, no conflict. 1553*f4a2713aSLionel Sambuc return CR_Keep; 1554*f4a2713aSLionel Sambuc 1555*f4a2713aSLionel Sambuc assert(!SlotIndex::isSameInstr(VNI->def, V.OtherVNI->def) && "Broken LRQ"); 1556*f4a2713aSLionel Sambuc 1557*f4a2713aSLionel Sambuc // We have overlapping values, or possibly a kill of Other. 1558*f4a2713aSLionel Sambuc // Recursively compute assignments up the dominator tree. 1559*f4a2713aSLionel Sambuc Other.computeAssignment(V.OtherVNI->id, *this); 1560*f4a2713aSLionel Sambuc Val &OtherV = Other.Vals[V.OtherVNI->id]; 1561*f4a2713aSLionel Sambuc 1562*f4a2713aSLionel Sambuc // Check if OtherV is an IMPLICIT_DEF that extends beyond its basic block. 1563*f4a2713aSLionel Sambuc // This shouldn't normally happen, but ProcessImplicitDefs can leave such 1564*f4a2713aSLionel Sambuc // IMPLICIT_DEF instructions behind, and there is nothing wrong with it 1565*f4a2713aSLionel Sambuc // technically. 1566*f4a2713aSLionel Sambuc // 1567*f4a2713aSLionel Sambuc // WHen it happens, treat that IMPLICIT_DEF as a normal value, and don't try 1568*f4a2713aSLionel Sambuc // to erase the IMPLICIT_DEF instruction. 1569*f4a2713aSLionel Sambuc if (OtherV.ErasableImplicitDef && DefMI && 1570*f4a2713aSLionel Sambuc DefMI->getParent() != Indexes->getMBBFromIndex(V.OtherVNI->def)) { 1571*f4a2713aSLionel Sambuc DEBUG(dbgs() << "IMPLICIT_DEF defined at " << V.OtherVNI->def 1572*f4a2713aSLionel Sambuc << " extends into BB#" << DefMI->getParent()->getNumber() 1573*f4a2713aSLionel Sambuc << ", keeping it.\n"); 1574*f4a2713aSLionel Sambuc OtherV.ErasableImplicitDef = false; 1575*f4a2713aSLionel Sambuc } 1576*f4a2713aSLionel Sambuc 1577*f4a2713aSLionel Sambuc // Allow overlapping PHI values. Any real interference would show up in a 1578*f4a2713aSLionel Sambuc // predecessor, the PHI itself can't introduce any conflicts. 1579*f4a2713aSLionel Sambuc if (VNI->isPHIDef()) 1580*f4a2713aSLionel Sambuc return CR_Replace; 1581*f4a2713aSLionel Sambuc 1582*f4a2713aSLionel Sambuc // Check for simple erasable conflicts. 1583*f4a2713aSLionel Sambuc if (DefMI->isImplicitDef()) 1584*f4a2713aSLionel Sambuc return CR_Erase; 1585*f4a2713aSLionel Sambuc 1586*f4a2713aSLionel Sambuc // Include the non-conflict where DefMI is a coalescable copy that kills 1587*f4a2713aSLionel Sambuc // OtherVNI. We still want the copy erased and value numbers merged. 1588*f4a2713aSLionel Sambuc if (CP.isCoalescable(DefMI)) { 1589*f4a2713aSLionel Sambuc // Some of the lanes copied from OtherVNI may be undef, making them undef 1590*f4a2713aSLionel Sambuc // here too. 1591*f4a2713aSLionel Sambuc V.ValidLanes &= ~V.WriteLanes | OtherV.ValidLanes; 1592*f4a2713aSLionel Sambuc return CR_Erase; 1593*f4a2713aSLionel Sambuc } 1594*f4a2713aSLionel Sambuc 1595*f4a2713aSLionel Sambuc // This may not be a real conflict if DefMI simply kills Other and defines 1596*f4a2713aSLionel Sambuc // VNI. 1597*f4a2713aSLionel Sambuc if (OtherLRQ.isKill() && OtherLRQ.endPoint() <= VNI->def) 1598*f4a2713aSLionel Sambuc return CR_Keep; 1599*f4a2713aSLionel Sambuc 1600*f4a2713aSLionel Sambuc // Handle the case where VNI and OtherVNI can be proven to be identical: 1601*f4a2713aSLionel Sambuc // 1602*f4a2713aSLionel Sambuc // %other = COPY %ext 1603*f4a2713aSLionel Sambuc // %this = COPY %ext <-- Erase this copy 1604*f4a2713aSLionel Sambuc // 1605*f4a2713aSLionel Sambuc if (DefMI->isFullCopy() && !CP.isPartial() && 1606*f4a2713aSLionel Sambuc stripCopies(VNI) == stripCopies(V.OtherVNI)) 1607*f4a2713aSLionel Sambuc return CR_Erase; 1608*f4a2713aSLionel Sambuc 1609*f4a2713aSLionel Sambuc // If the lanes written by this instruction were all undef in OtherVNI, it is 1610*f4a2713aSLionel Sambuc // still safe to join the live ranges. This can't be done with a simple value 1611*f4a2713aSLionel Sambuc // mapping, though - OtherVNI will map to multiple values: 1612*f4a2713aSLionel Sambuc // 1613*f4a2713aSLionel Sambuc // 1 %dst:ssub0 = FOO <-- OtherVNI 1614*f4a2713aSLionel Sambuc // 2 %src = BAR <-- VNI 1615*f4a2713aSLionel Sambuc // 3 %dst:ssub1 = COPY %src<kill> <-- Eliminate this copy. 1616*f4a2713aSLionel Sambuc // 4 BAZ %dst<kill> 1617*f4a2713aSLionel Sambuc // 5 QUUX %src<kill> 1618*f4a2713aSLionel Sambuc // 1619*f4a2713aSLionel Sambuc // Here OtherVNI will map to itself in [1;2), but to VNI in [2;5). CR_Replace 1620*f4a2713aSLionel Sambuc // handles this complex value mapping. 1621*f4a2713aSLionel Sambuc if ((V.WriteLanes & OtherV.ValidLanes) == 0) 1622*f4a2713aSLionel Sambuc return CR_Replace; 1623*f4a2713aSLionel Sambuc 1624*f4a2713aSLionel Sambuc // If the other live range is killed by DefMI and the live ranges are still 1625*f4a2713aSLionel Sambuc // overlapping, it must be because we're looking at an early clobber def: 1626*f4a2713aSLionel Sambuc // 1627*f4a2713aSLionel Sambuc // %dst<def,early-clobber> = ASM %src<kill> 1628*f4a2713aSLionel Sambuc // 1629*f4a2713aSLionel Sambuc // In this case, it is illegal to merge the two live ranges since the early 1630*f4a2713aSLionel Sambuc // clobber def would clobber %src before it was read. 1631*f4a2713aSLionel Sambuc if (OtherLRQ.isKill()) { 1632*f4a2713aSLionel Sambuc // This case where the def doesn't overlap the kill is handled above. 1633*f4a2713aSLionel Sambuc assert(VNI->def.isEarlyClobber() && 1634*f4a2713aSLionel Sambuc "Only early clobber defs can overlap a kill"); 1635*f4a2713aSLionel Sambuc return CR_Impossible; 1636*f4a2713aSLionel Sambuc } 1637*f4a2713aSLionel Sambuc 1638*f4a2713aSLionel Sambuc // VNI is clobbering live lanes in OtherVNI, but there is still the 1639*f4a2713aSLionel Sambuc // possibility that no instructions actually read the clobbered lanes. 1640*f4a2713aSLionel Sambuc // If we're clobbering all the lanes in OtherVNI, at least one must be read. 1641*f4a2713aSLionel Sambuc // Otherwise Other.LI wouldn't be live here. 1642*f4a2713aSLionel Sambuc if ((TRI->getSubRegIndexLaneMask(Other.SubIdx) & ~V.WriteLanes) == 0) 1643*f4a2713aSLionel Sambuc return CR_Impossible; 1644*f4a2713aSLionel Sambuc 1645*f4a2713aSLionel Sambuc // We need to verify that no instructions are reading the clobbered lanes. To 1646*f4a2713aSLionel Sambuc // save compile time, we'll only check that locally. Don't allow the tainted 1647*f4a2713aSLionel Sambuc // value to escape the basic block. 1648*f4a2713aSLionel Sambuc MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def); 1649*f4a2713aSLionel Sambuc if (OtherLRQ.endPoint() >= Indexes->getMBBEndIdx(MBB)) 1650*f4a2713aSLionel Sambuc return CR_Impossible; 1651*f4a2713aSLionel Sambuc 1652*f4a2713aSLionel Sambuc // There are still some things that could go wrong besides clobbered lanes 1653*f4a2713aSLionel Sambuc // being read, for example OtherVNI may be only partially redefined in MBB, 1654*f4a2713aSLionel Sambuc // and some clobbered lanes could escape the block. Save this analysis for 1655*f4a2713aSLionel Sambuc // resolveConflicts() when all values have been mapped. We need to know 1656*f4a2713aSLionel Sambuc // RedefVNI and WriteLanes for any later defs in MBB, and we can't compute 1657*f4a2713aSLionel Sambuc // that now - the recursive analyzeValue() calls must go upwards in the 1658*f4a2713aSLionel Sambuc // dominator tree. 1659*f4a2713aSLionel Sambuc return CR_Unresolved; 1660*f4a2713aSLionel Sambuc } 1661*f4a2713aSLionel Sambuc 1662*f4a2713aSLionel Sambuc /// Compute the value assignment for ValNo in LI. 1663*f4a2713aSLionel Sambuc /// This may be called recursively by analyzeValue(), but never for a ValNo on 1664*f4a2713aSLionel Sambuc /// the stack. 1665*f4a2713aSLionel Sambuc void JoinVals::computeAssignment(unsigned ValNo, JoinVals &Other) { 1666*f4a2713aSLionel Sambuc Val &V = Vals[ValNo]; 1667*f4a2713aSLionel Sambuc if (V.isAnalyzed()) { 1668*f4a2713aSLionel Sambuc // Recursion should always move up the dominator tree, so ValNo is not 1669*f4a2713aSLionel Sambuc // supposed to reappear before it has been assigned. 1670*f4a2713aSLionel Sambuc assert(Assignments[ValNo] != -1 && "Bad recursion?"); 1671*f4a2713aSLionel Sambuc return; 1672*f4a2713aSLionel Sambuc } 1673*f4a2713aSLionel Sambuc switch ((V.Resolution = analyzeValue(ValNo, Other))) { 1674*f4a2713aSLionel Sambuc case CR_Erase: 1675*f4a2713aSLionel Sambuc case CR_Merge: 1676*f4a2713aSLionel Sambuc // Merge this ValNo into OtherVNI. 1677*f4a2713aSLionel Sambuc assert(V.OtherVNI && "OtherVNI not assigned, can't merge."); 1678*f4a2713aSLionel Sambuc assert(Other.Vals[V.OtherVNI->id].isAnalyzed() && "Missing recursion"); 1679*f4a2713aSLionel Sambuc Assignments[ValNo] = Other.Assignments[V.OtherVNI->id]; 1680*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tmerge " << PrintReg(LI.reg) << ':' << ValNo << '@' 1681*f4a2713aSLionel Sambuc << LI.getValNumInfo(ValNo)->def << " into " 1682*f4a2713aSLionel Sambuc << PrintReg(Other.LI.reg) << ':' << V.OtherVNI->id << '@' 1683*f4a2713aSLionel Sambuc << V.OtherVNI->def << " --> @" 1684*f4a2713aSLionel Sambuc << NewVNInfo[Assignments[ValNo]]->def << '\n'); 1685*f4a2713aSLionel Sambuc break; 1686*f4a2713aSLionel Sambuc case CR_Replace: 1687*f4a2713aSLionel Sambuc case CR_Unresolved: 1688*f4a2713aSLionel Sambuc // The other value is going to be pruned if this join is successful. 1689*f4a2713aSLionel Sambuc assert(V.OtherVNI && "OtherVNI not assigned, can't prune"); 1690*f4a2713aSLionel Sambuc Other.Vals[V.OtherVNI->id].Pruned = true; 1691*f4a2713aSLionel Sambuc // Fall through. 1692*f4a2713aSLionel Sambuc default: 1693*f4a2713aSLionel Sambuc // This value number needs to go in the final joined live range. 1694*f4a2713aSLionel Sambuc Assignments[ValNo] = NewVNInfo.size(); 1695*f4a2713aSLionel Sambuc NewVNInfo.push_back(LI.getValNumInfo(ValNo)); 1696*f4a2713aSLionel Sambuc break; 1697*f4a2713aSLionel Sambuc } 1698*f4a2713aSLionel Sambuc } 1699*f4a2713aSLionel Sambuc 1700*f4a2713aSLionel Sambuc bool JoinVals::mapValues(JoinVals &Other) { 1701*f4a2713aSLionel Sambuc for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) { 1702*f4a2713aSLionel Sambuc computeAssignment(i, Other); 1703*f4a2713aSLionel Sambuc if (Vals[i].Resolution == CR_Impossible) { 1704*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tinterference at " << PrintReg(LI.reg) << ':' << i 1705*f4a2713aSLionel Sambuc << '@' << LI.getValNumInfo(i)->def << '\n'); 1706*f4a2713aSLionel Sambuc return false; 1707*f4a2713aSLionel Sambuc } 1708*f4a2713aSLionel Sambuc } 1709*f4a2713aSLionel Sambuc return true; 1710*f4a2713aSLionel Sambuc } 1711*f4a2713aSLionel Sambuc 1712*f4a2713aSLionel Sambuc /// Assuming ValNo is going to clobber some valid lanes in Other.LI, compute 1713*f4a2713aSLionel Sambuc /// the extent of the tainted lanes in the block. 1714*f4a2713aSLionel Sambuc /// 1715*f4a2713aSLionel Sambuc /// Multiple values in Other.LI can be affected since partial redefinitions can 1716*f4a2713aSLionel Sambuc /// preserve previously tainted lanes. 1717*f4a2713aSLionel Sambuc /// 1718*f4a2713aSLionel Sambuc /// 1 %dst = VLOAD <-- Define all lanes in %dst 1719*f4a2713aSLionel Sambuc /// 2 %src = FOO <-- ValNo to be joined with %dst:ssub0 1720*f4a2713aSLionel Sambuc /// 3 %dst:ssub1 = BAR <-- Partial redef doesn't clear taint in ssub0 1721*f4a2713aSLionel Sambuc /// 4 %dst:ssub0 = COPY %src <-- Conflict resolved, ssub0 wasn't read 1722*f4a2713aSLionel Sambuc /// 1723*f4a2713aSLionel Sambuc /// For each ValNo in Other that is affected, add an (EndIndex, TaintedLanes) 1724*f4a2713aSLionel Sambuc /// entry to TaintedVals. 1725*f4a2713aSLionel Sambuc /// 1726*f4a2713aSLionel Sambuc /// Returns false if the tainted lanes extend beyond the basic block. 1727*f4a2713aSLionel Sambuc bool JoinVals:: 1728*f4a2713aSLionel Sambuc taintExtent(unsigned ValNo, unsigned TaintedLanes, JoinVals &Other, 1729*f4a2713aSLionel Sambuc SmallVectorImpl<std::pair<SlotIndex, unsigned> > &TaintExtent) { 1730*f4a2713aSLionel Sambuc VNInfo *VNI = LI.getValNumInfo(ValNo); 1731*f4a2713aSLionel Sambuc MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def); 1732*f4a2713aSLionel Sambuc SlotIndex MBBEnd = Indexes->getMBBEndIdx(MBB); 1733*f4a2713aSLionel Sambuc 1734*f4a2713aSLionel Sambuc // Scan Other.LI from VNI.def to MBBEnd. 1735*f4a2713aSLionel Sambuc LiveInterval::iterator OtherI = Other.LI.find(VNI->def); 1736*f4a2713aSLionel Sambuc assert(OtherI != Other.LI.end() && "No conflict?"); 1737*f4a2713aSLionel Sambuc do { 1738*f4a2713aSLionel Sambuc // OtherI is pointing to a tainted value. Abort the join if the tainted 1739*f4a2713aSLionel Sambuc // lanes escape the block. 1740*f4a2713aSLionel Sambuc SlotIndex End = OtherI->end; 1741*f4a2713aSLionel Sambuc if (End >= MBBEnd) { 1742*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\ttaints global " << PrintReg(Other.LI.reg) << ':' 1743*f4a2713aSLionel Sambuc << OtherI->valno->id << '@' << OtherI->start << '\n'); 1744*f4a2713aSLionel Sambuc return false; 1745*f4a2713aSLionel Sambuc } 1746*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\ttaints local " << PrintReg(Other.LI.reg) << ':' 1747*f4a2713aSLionel Sambuc << OtherI->valno->id << '@' << OtherI->start 1748*f4a2713aSLionel Sambuc << " to " << End << '\n'); 1749*f4a2713aSLionel Sambuc // A dead def is not a problem. 1750*f4a2713aSLionel Sambuc if (End.isDead()) 1751*f4a2713aSLionel Sambuc break; 1752*f4a2713aSLionel Sambuc TaintExtent.push_back(std::make_pair(End, TaintedLanes)); 1753*f4a2713aSLionel Sambuc 1754*f4a2713aSLionel Sambuc // Check for another def in the MBB. 1755*f4a2713aSLionel Sambuc if (++OtherI == Other.LI.end() || OtherI->start >= MBBEnd) 1756*f4a2713aSLionel Sambuc break; 1757*f4a2713aSLionel Sambuc 1758*f4a2713aSLionel Sambuc // Lanes written by the new def are no longer tainted. 1759*f4a2713aSLionel Sambuc const Val &OV = Other.Vals[OtherI->valno->id]; 1760*f4a2713aSLionel Sambuc TaintedLanes &= ~OV.WriteLanes; 1761*f4a2713aSLionel Sambuc if (!OV.RedefVNI) 1762*f4a2713aSLionel Sambuc break; 1763*f4a2713aSLionel Sambuc } while (TaintedLanes); 1764*f4a2713aSLionel Sambuc return true; 1765*f4a2713aSLionel Sambuc } 1766*f4a2713aSLionel Sambuc 1767*f4a2713aSLionel Sambuc /// Return true if MI uses any of the given Lanes from Reg. 1768*f4a2713aSLionel Sambuc /// This does not include partial redefinitions of Reg. 1769*f4a2713aSLionel Sambuc bool JoinVals::usesLanes(MachineInstr *MI, unsigned Reg, unsigned SubIdx, 1770*f4a2713aSLionel Sambuc unsigned Lanes) { 1771*f4a2713aSLionel Sambuc if (MI->isDebugValue()) 1772*f4a2713aSLionel Sambuc return false; 1773*f4a2713aSLionel Sambuc for (ConstMIOperands MO(MI); MO.isValid(); ++MO) { 1774*f4a2713aSLionel Sambuc if (!MO->isReg() || MO->isDef() || MO->getReg() != Reg) 1775*f4a2713aSLionel Sambuc continue; 1776*f4a2713aSLionel Sambuc if (!MO->readsReg()) 1777*f4a2713aSLionel Sambuc continue; 1778*f4a2713aSLionel Sambuc if (Lanes & TRI->getSubRegIndexLaneMask( 1779*f4a2713aSLionel Sambuc TRI->composeSubRegIndices(SubIdx, MO->getSubReg()))) 1780*f4a2713aSLionel Sambuc return true; 1781*f4a2713aSLionel Sambuc } 1782*f4a2713aSLionel Sambuc return false; 1783*f4a2713aSLionel Sambuc } 1784*f4a2713aSLionel Sambuc 1785*f4a2713aSLionel Sambuc bool JoinVals::resolveConflicts(JoinVals &Other) { 1786*f4a2713aSLionel Sambuc for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) { 1787*f4a2713aSLionel Sambuc Val &V = Vals[i]; 1788*f4a2713aSLionel Sambuc assert (V.Resolution != CR_Impossible && "Unresolvable conflict"); 1789*f4a2713aSLionel Sambuc if (V.Resolution != CR_Unresolved) 1790*f4a2713aSLionel Sambuc continue; 1791*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tconflict at " << PrintReg(LI.reg) << ':' << i 1792*f4a2713aSLionel Sambuc << '@' << LI.getValNumInfo(i)->def << '\n'); 1793*f4a2713aSLionel Sambuc ++NumLaneConflicts; 1794*f4a2713aSLionel Sambuc assert(V.OtherVNI && "Inconsistent conflict resolution."); 1795*f4a2713aSLionel Sambuc VNInfo *VNI = LI.getValNumInfo(i); 1796*f4a2713aSLionel Sambuc const Val &OtherV = Other.Vals[V.OtherVNI->id]; 1797*f4a2713aSLionel Sambuc 1798*f4a2713aSLionel Sambuc // VNI is known to clobber some lanes in OtherVNI. If we go ahead with the 1799*f4a2713aSLionel Sambuc // join, those lanes will be tainted with a wrong value. Get the extent of 1800*f4a2713aSLionel Sambuc // the tainted lanes. 1801*f4a2713aSLionel Sambuc unsigned TaintedLanes = V.WriteLanes & OtherV.ValidLanes; 1802*f4a2713aSLionel Sambuc SmallVector<std::pair<SlotIndex, unsigned>, 8> TaintExtent; 1803*f4a2713aSLionel Sambuc if (!taintExtent(i, TaintedLanes, Other, TaintExtent)) 1804*f4a2713aSLionel Sambuc // Tainted lanes would extend beyond the basic block. 1805*f4a2713aSLionel Sambuc return false; 1806*f4a2713aSLionel Sambuc 1807*f4a2713aSLionel Sambuc assert(!TaintExtent.empty() && "There should be at least one conflict."); 1808*f4a2713aSLionel Sambuc 1809*f4a2713aSLionel Sambuc // Now look at the instructions from VNI->def to TaintExtent (inclusive). 1810*f4a2713aSLionel Sambuc MachineBasicBlock *MBB = Indexes->getMBBFromIndex(VNI->def); 1811*f4a2713aSLionel Sambuc MachineBasicBlock::iterator MI = MBB->begin(); 1812*f4a2713aSLionel Sambuc if (!VNI->isPHIDef()) { 1813*f4a2713aSLionel Sambuc MI = Indexes->getInstructionFromIndex(VNI->def); 1814*f4a2713aSLionel Sambuc // No need to check the instruction defining VNI for reads. 1815*f4a2713aSLionel Sambuc ++MI; 1816*f4a2713aSLionel Sambuc } 1817*f4a2713aSLionel Sambuc assert(!SlotIndex::isSameInstr(VNI->def, TaintExtent.front().first) && 1818*f4a2713aSLionel Sambuc "Interference ends on VNI->def. Should have been handled earlier"); 1819*f4a2713aSLionel Sambuc MachineInstr *LastMI = 1820*f4a2713aSLionel Sambuc Indexes->getInstructionFromIndex(TaintExtent.front().first); 1821*f4a2713aSLionel Sambuc assert(LastMI && "Range must end at a proper instruction"); 1822*f4a2713aSLionel Sambuc unsigned TaintNum = 0; 1823*f4a2713aSLionel Sambuc for(;;) { 1824*f4a2713aSLionel Sambuc assert(MI != MBB->end() && "Bad LastMI"); 1825*f4a2713aSLionel Sambuc if (usesLanes(MI, Other.LI.reg, Other.SubIdx, TaintedLanes)) { 1826*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\ttainted lanes used by: " << *MI); 1827*f4a2713aSLionel Sambuc return false; 1828*f4a2713aSLionel Sambuc } 1829*f4a2713aSLionel Sambuc // LastMI is the last instruction to use the current value. 1830*f4a2713aSLionel Sambuc if (&*MI == LastMI) { 1831*f4a2713aSLionel Sambuc if (++TaintNum == TaintExtent.size()) 1832*f4a2713aSLionel Sambuc break; 1833*f4a2713aSLionel Sambuc LastMI = Indexes->getInstructionFromIndex(TaintExtent[TaintNum].first); 1834*f4a2713aSLionel Sambuc assert(LastMI && "Range must end at a proper instruction"); 1835*f4a2713aSLionel Sambuc TaintedLanes = TaintExtent[TaintNum].second; 1836*f4a2713aSLionel Sambuc } 1837*f4a2713aSLionel Sambuc ++MI; 1838*f4a2713aSLionel Sambuc } 1839*f4a2713aSLionel Sambuc 1840*f4a2713aSLionel Sambuc // The tainted lanes are unused. 1841*f4a2713aSLionel Sambuc V.Resolution = CR_Replace; 1842*f4a2713aSLionel Sambuc ++NumLaneResolves; 1843*f4a2713aSLionel Sambuc } 1844*f4a2713aSLionel Sambuc return true; 1845*f4a2713aSLionel Sambuc } 1846*f4a2713aSLionel Sambuc 1847*f4a2713aSLionel Sambuc // Determine if ValNo is a copy of a value number in LI or Other.LI that will 1848*f4a2713aSLionel Sambuc // be pruned: 1849*f4a2713aSLionel Sambuc // 1850*f4a2713aSLionel Sambuc // %dst = COPY %src 1851*f4a2713aSLionel Sambuc // %src = COPY %dst <-- This value to be pruned. 1852*f4a2713aSLionel Sambuc // %dst = COPY %src <-- This value is a copy of a pruned value. 1853*f4a2713aSLionel Sambuc // 1854*f4a2713aSLionel Sambuc bool JoinVals::isPrunedValue(unsigned ValNo, JoinVals &Other) { 1855*f4a2713aSLionel Sambuc Val &V = Vals[ValNo]; 1856*f4a2713aSLionel Sambuc if (V.Pruned || V.PrunedComputed) 1857*f4a2713aSLionel Sambuc return V.Pruned; 1858*f4a2713aSLionel Sambuc 1859*f4a2713aSLionel Sambuc if (V.Resolution != CR_Erase && V.Resolution != CR_Merge) 1860*f4a2713aSLionel Sambuc return V.Pruned; 1861*f4a2713aSLionel Sambuc 1862*f4a2713aSLionel Sambuc // Follow copies up the dominator tree and check if any intermediate value 1863*f4a2713aSLionel Sambuc // has been pruned. 1864*f4a2713aSLionel Sambuc V.PrunedComputed = true; 1865*f4a2713aSLionel Sambuc V.Pruned = Other.isPrunedValue(V.OtherVNI->id, *this); 1866*f4a2713aSLionel Sambuc return V.Pruned; 1867*f4a2713aSLionel Sambuc } 1868*f4a2713aSLionel Sambuc 1869*f4a2713aSLionel Sambuc void JoinVals::pruneValues(JoinVals &Other, 1870*f4a2713aSLionel Sambuc SmallVectorImpl<SlotIndex> &EndPoints) { 1871*f4a2713aSLionel Sambuc for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) { 1872*f4a2713aSLionel Sambuc SlotIndex Def = LI.getValNumInfo(i)->def; 1873*f4a2713aSLionel Sambuc switch (Vals[i].Resolution) { 1874*f4a2713aSLionel Sambuc case CR_Keep: 1875*f4a2713aSLionel Sambuc break; 1876*f4a2713aSLionel Sambuc case CR_Replace: { 1877*f4a2713aSLionel Sambuc // This value takes precedence over the value in Other.LI. 1878*f4a2713aSLionel Sambuc LIS->pruneValue(&Other.LI, Def, &EndPoints); 1879*f4a2713aSLionel Sambuc // Check if we're replacing an IMPLICIT_DEF value. The IMPLICIT_DEF 1880*f4a2713aSLionel Sambuc // instructions are only inserted to provide a live-out value for PHI 1881*f4a2713aSLionel Sambuc // predecessors, so the instruction should simply go away once its value 1882*f4a2713aSLionel Sambuc // has been replaced. 1883*f4a2713aSLionel Sambuc Val &OtherV = Other.Vals[Vals[i].OtherVNI->id]; 1884*f4a2713aSLionel Sambuc bool EraseImpDef = OtherV.ErasableImplicitDef && 1885*f4a2713aSLionel Sambuc OtherV.Resolution == CR_Keep; 1886*f4a2713aSLionel Sambuc if (!Def.isBlock()) { 1887*f4a2713aSLionel Sambuc // Remove <def,read-undef> flags. This def is now a partial redef. 1888*f4a2713aSLionel Sambuc // Also remove <def,dead> flags since the joined live range will 1889*f4a2713aSLionel Sambuc // continue past this instruction. 1890*f4a2713aSLionel Sambuc for (MIOperands MO(Indexes->getInstructionFromIndex(Def)); 1891*f4a2713aSLionel Sambuc MO.isValid(); ++MO) 1892*f4a2713aSLionel Sambuc if (MO->isReg() && MO->isDef() && MO->getReg() == LI.reg) { 1893*f4a2713aSLionel Sambuc MO->setIsUndef(EraseImpDef); 1894*f4a2713aSLionel Sambuc MO->setIsDead(false); 1895*f4a2713aSLionel Sambuc } 1896*f4a2713aSLionel Sambuc // This value will reach instructions below, but we need to make sure 1897*f4a2713aSLionel Sambuc // the live range also reaches the instruction at Def. 1898*f4a2713aSLionel Sambuc if (!EraseImpDef) 1899*f4a2713aSLionel Sambuc EndPoints.push_back(Def); 1900*f4a2713aSLionel Sambuc } 1901*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tpruned " << PrintReg(Other.LI.reg) << " at " << Def 1902*f4a2713aSLionel Sambuc << ": " << Other.LI << '\n'); 1903*f4a2713aSLionel Sambuc break; 1904*f4a2713aSLionel Sambuc } 1905*f4a2713aSLionel Sambuc case CR_Erase: 1906*f4a2713aSLionel Sambuc case CR_Merge: 1907*f4a2713aSLionel Sambuc if (isPrunedValue(i, Other)) { 1908*f4a2713aSLionel Sambuc // This value is ultimately a copy of a pruned value in LI or Other.LI. 1909*f4a2713aSLionel Sambuc // We can no longer trust the value mapping computed by 1910*f4a2713aSLionel Sambuc // computeAssignment(), the value that was originally copied could have 1911*f4a2713aSLionel Sambuc // been replaced. 1912*f4a2713aSLionel Sambuc LIS->pruneValue(&LI, Def, &EndPoints); 1913*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tpruned all of " << PrintReg(LI.reg) << " at " 1914*f4a2713aSLionel Sambuc << Def << ": " << LI << '\n'); 1915*f4a2713aSLionel Sambuc } 1916*f4a2713aSLionel Sambuc break; 1917*f4a2713aSLionel Sambuc case CR_Unresolved: 1918*f4a2713aSLionel Sambuc case CR_Impossible: 1919*f4a2713aSLionel Sambuc llvm_unreachable("Unresolved conflicts"); 1920*f4a2713aSLionel Sambuc } 1921*f4a2713aSLionel Sambuc } 1922*f4a2713aSLionel Sambuc } 1923*f4a2713aSLionel Sambuc 1924*f4a2713aSLionel Sambuc void JoinVals::eraseInstrs(SmallPtrSet<MachineInstr*, 8> &ErasedInstrs, 1925*f4a2713aSLionel Sambuc SmallVectorImpl<unsigned> &ShrinkRegs) { 1926*f4a2713aSLionel Sambuc for (unsigned i = 0, e = LI.getNumValNums(); i != e; ++i) { 1927*f4a2713aSLionel Sambuc // Get the def location before markUnused() below invalidates it. 1928*f4a2713aSLionel Sambuc SlotIndex Def = LI.getValNumInfo(i)->def; 1929*f4a2713aSLionel Sambuc switch (Vals[i].Resolution) { 1930*f4a2713aSLionel Sambuc case CR_Keep: 1931*f4a2713aSLionel Sambuc // If an IMPLICIT_DEF value is pruned, it doesn't serve a purpose any 1932*f4a2713aSLionel Sambuc // longer. The IMPLICIT_DEF instructions are only inserted by 1933*f4a2713aSLionel Sambuc // PHIElimination to guarantee that all PHI predecessors have a value. 1934*f4a2713aSLionel Sambuc if (!Vals[i].ErasableImplicitDef || !Vals[i].Pruned) 1935*f4a2713aSLionel Sambuc break; 1936*f4a2713aSLionel Sambuc // Remove value number i from LI. Note that this VNInfo is still present 1937*f4a2713aSLionel Sambuc // in NewVNInfo, so it will appear as an unused value number in the final 1938*f4a2713aSLionel Sambuc // joined interval. 1939*f4a2713aSLionel Sambuc LI.getValNumInfo(i)->markUnused(); 1940*f4a2713aSLionel Sambuc LI.removeValNo(LI.getValNumInfo(i)); 1941*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tremoved " << i << '@' << Def << ": " << LI << '\n'); 1942*f4a2713aSLionel Sambuc // FALL THROUGH. 1943*f4a2713aSLionel Sambuc 1944*f4a2713aSLionel Sambuc case CR_Erase: { 1945*f4a2713aSLionel Sambuc MachineInstr *MI = Indexes->getInstructionFromIndex(Def); 1946*f4a2713aSLionel Sambuc assert(MI && "No instruction to erase"); 1947*f4a2713aSLionel Sambuc if (MI->isCopy()) { 1948*f4a2713aSLionel Sambuc unsigned Reg = MI->getOperand(1).getReg(); 1949*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isVirtualRegister(Reg) && 1950*f4a2713aSLionel Sambuc Reg != CP.getSrcReg() && Reg != CP.getDstReg()) 1951*f4a2713aSLionel Sambuc ShrinkRegs.push_back(Reg); 1952*f4a2713aSLionel Sambuc } 1953*f4a2713aSLionel Sambuc ErasedInstrs.insert(MI); 1954*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\terased:\t" << Def << '\t' << *MI); 1955*f4a2713aSLionel Sambuc LIS->RemoveMachineInstrFromMaps(MI); 1956*f4a2713aSLionel Sambuc MI->eraseFromParent(); 1957*f4a2713aSLionel Sambuc break; 1958*f4a2713aSLionel Sambuc } 1959*f4a2713aSLionel Sambuc default: 1960*f4a2713aSLionel Sambuc break; 1961*f4a2713aSLionel Sambuc } 1962*f4a2713aSLionel Sambuc } 1963*f4a2713aSLionel Sambuc } 1964*f4a2713aSLionel Sambuc 1965*f4a2713aSLionel Sambuc bool RegisterCoalescer::joinVirtRegs(CoalescerPair &CP) { 1966*f4a2713aSLionel Sambuc SmallVector<VNInfo*, 16> NewVNInfo; 1967*f4a2713aSLionel Sambuc LiveInterval &RHS = LIS->getInterval(CP.getSrcReg()); 1968*f4a2713aSLionel Sambuc LiveInterval &LHS = LIS->getInterval(CP.getDstReg()); 1969*f4a2713aSLionel Sambuc JoinVals RHSVals(RHS, CP.getSrcIdx(), NewVNInfo, CP, LIS, TRI); 1970*f4a2713aSLionel Sambuc JoinVals LHSVals(LHS, CP.getDstIdx(), NewVNInfo, CP, LIS, TRI); 1971*f4a2713aSLionel Sambuc 1972*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\tRHS = " << RHS 1973*f4a2713aSLionel Sambuc << "\n\t\tLHS = " << LHS 1974*f4a2713aSLionel Sambuc << '\n'); 1975*f4a2713aSLionel Sambuc 1976*f4a2713aSLionel Sambuc // First compute NewVNInfo and the simple value mappings. 1977*f4a2713aSLionel Sambuc // Detect impossible conflicts early. 1978*f4a2713aSLionel Sambuc if (!LHSVals.mapValues(RHSVals) || !RHSVals.mapValues(LHSVals)) 1979*f4a2713aSLionel Sambuc return false; 1980*f4a2713aSLionel Sambuc 1981*f4a2713aSLionel Sambuc // Some conflicts can only be resolved after all values have been mapped. 1982*f4a2713aSLionel Sambuc if (!LHSVals.resolveConflicts(RHSVals) || !RHSVals.resolveConflicts(LHSVals)) 1983*f4a2713aSLionel Sambuc return false; 1984*f4a2713aSLionel Sambuc 1985*f4a2713aSLionel Sambuc // All clear, the live ranges can be merged. 1986*f4a2713aSLionel Sambuc 1987*f4a2713aSLionel Sambuc // The merging algorithm in LiveInterval::join() can't handle conflicting 1988*f4a2713aSLionel Sambuc // value mappings, so we need to remove any live ranges that overlap a 1989*f4a2713aSLionel Sambuc // CR_Replace resolution. Collect a set of end points that can be used to 1990*f4a2713aSLionel Sambuc // restore the live range after joining. 1991*f4a2713aSLionel Sambuc SmallVector<SlotIndex, 8> EndPoints; 1992*f4a2713aSLionel Sambuc LHSVals.pruneValues(RHSVals, EndPoints); 1993*f4a2713aSLionel Sambuc RHSVals.pruneValues(LHSVals, EndPoints); 1994*f4a2713aSLionel Sambuc 1995*f4a2713aSLionel Sambuc // Erase COPY and IMPLICIT_DEF instructions. This may cause some external 1996*f4a2713aSLionel Sambuc // registers to require trimming. 1997*f4a2713aSLionel Sambuc SmallVector<unsigned, 8> ShrinkRegs; 1998*f4a2713aSLionel Sambuc LHSVals.eraseInstrs(ErasedInstrs, ShrinkRegs); 1999*f4a2713aSLionel Sambuc RHSVals.eraseInstrs(ErasedInstrs, ShrinkRegs); 2000*f4a2713aSLionel Sambuc while (!ShrinkRegs.empty()) 2001*f4a2713aSLionel Sambuc LIS->shrinkToUses(&LIS->getInterval(ShrinkRegs.pop_back_val())); 2002*f4a2713aSLionel Sambuc 2003*f4a2713aSLionel Sambuc // Join RHS into LHS. 2004*f4a2713aSLionel Sambuc LHS.join(RHS, LHSVals.getAssignments(), RHSVals.getAssignments(), NewVNInfo); 2005*f4a2713aSLionel Sambuc 2006*f4a2713aSLionel Sambuc // Kill flags are going to be wrong if the live ranges were overlapping. 2007*f4a2713aSLionel Sambuc // Eventually, we should simply clear all kill flags when computing live 2008*f4a2713aSLionel Sambuc // ranges. They are reinserted after register allocation. 2009*f4a2713aSLionel Sambuc MRI->clearKillFlags(LHS.reg); 2010*f4a2713aSLionel Sambuc MRI->clearKillFlags(RHS.reg); 2011*f4a2713aSLionel Sambuc 2012*f4a2713aSLionel Sambuc if (EndPoints.empty()) 2013*f4a2713aSLionel Sambuc return true; 2014*f4a2713aSLionel Sambuc 2015*f4a2713aSLionel Sambuc // Recompute the parts of the live range we had to remove because of 2016*f4a2713aSLionel Sambuc // CR_Replace conflicts. 2017*f4a2713aSLionel Sambuc DEBUG(dbgs() << "\t\trestoring liveness to " << EndPoints.size() 2018*f4a2713aSLionel Sambuc << " points: " << LHS << '\n'); 2019*f4a2713aSLionel Sambuc LIS->extendToIndices(LHS, EndPoints); 2020*f4a2713aSLionel Sambuc return true; 2021*f4a2713aSLionel Sambuc } 2022*f4a2713aSLionel Sambuc 2023*f4a2713aSLionel Sambuc /// joinIntervals - Attempt to join these two intervals. On failure, this 2024*f4a2713aSLionel Sambuc /// returns false. 2025*f4a2713aSLionel Sambuc bool RegisterCoalescer::joinIntervals(CoalescerPair &CP) { 2026*f4a2713aSLionel Sambuc return CP.isPhys() ? joinReservedPhysReg(CP) : joinVirtRegs(CP); 2027*f4a2713aSLionel Sambuc } 2028*f4a2713aSLionel Sambuc 2029*f4a2713aSLionel Sambuc namespace { 2030*f4a2713aSLionel Sambuc // Information concerning MBB coalescing priority. 2031*f4a2713aSLionel Sambuc struct MBBPriorityInfo { 2032*f4a2713aSLionel Sambuc MachineBasicBlock *MBB; 2033*f4a2713aSLionel Sambuc unsigned Depth; 2034*f4a2713aSLionel Sambuc bool IsSplit; 2035*f4a2713aSLionel Sambuc 2036*f4a2713aSLionel Sambuc MBBPriorityInfo(MachineBasicBlock *mbb, unsigned depth, bool issplit) 2037*f4a2713aSLionel Sambuc : MBB(mbb), Depth(depth), IsSplit(issplit) {} 2038*f4a2713aSLionel Sambuc }; 2039*f4a2713aSLionel Sambuc } 2040*f4a2713aSLionel Sambuc 2041*f4a2713aSLionel Sambuc // C-style comparator that sorts first based on the loop depth of the basic 2042*f4a2713aSLionel Sambuc // block (the unsigned), and then on the MBB number. 2043*f4a2713aSLionel Sambuc // 2044*f4a2713aSLionel Sambuc // EnableGlobalCopies assumes that the primary sort key is loop depth. 2045*f4a2713aSLionel Sambuc static int compareMBBPriority(const MBBPriorityInfo *LHS, 2046*f4a2713aSLionel Sambuc const MBBPriorityInfo *RHS) { 2047*f4a2713aSLionel Sambuc // Deeper loops first 2048*f4a2713aSLionel Sambuc if (LHS->Depth != RHS->Depth) 2049*f4a2713aSLionel Sambuc return LHS->Depth > RHS->Depth ? -1 : 1; 2050*f4a2713aSLionel Sambuc 2051*f4a2713aSLionel Sambuc // Try to unsplit critical edges next. 2052*f4a2713aSLionel Sambuc if (LHS->IsSplit != RHS->IsSplit) 2053*f4a2713aSLionel Sambuc return LHS->IsSplit ? -1 : 1; 2054*f4a2713aSLionel Sambuc 2055*f4a2713aSLionel Sambuc // Prefer blocks that are more connected in the CFG. This takes care of 2056*f4a2713aSLionel Sambuc // the most difficult copies first while intervals are short. 2057*f4a2713aSLionel Sambuc unsigned cl = LHS->MBB->pred_size() + LHS->MBB->succ_size(); 2058*f4a2713aSLionel Sambuc unsigned cr = RHS->MBB->pred_size() + RHS->MBB->succ_size(); 2059*f4a2713aSLionel Sambuc if (cl != cr) 2060*f4a2713aSLionel Sambuc return cl > cr ? -1 : 1; 2061*f4a2713aSLionel Sambuc 2062*f4a2713aSLionel Sambuc // As a last resort, sort by block number. 2063*f4a2713aSLionel Sambuc return LHS->MBB->getNumber() < RHS->MBB->getNumber() ? -1 : 1; 2064*f4a2713aSLionel Sambuc } 2065*f4a2713aSLionel Sambuc 2066*f4a2713aSLionel Sambuc /// \returns true if the given copy uses or defines a local live range. 2067*f4a2713aSLionel Sambuc static bool isLocalCopy(MachineInstr *Copy, const LiveIntervals *LIS) { 2068*f4a2713aSLionel Sambuc if (!Copy->isCopy()) 2069*f4a2713aSLionel Sambuc return false; 2070*f4a2713aSLionel Sambuc 2071*f4a2713aSLionel Sambuc if (Copy->getOperand(1).isUndef()) 2072*f4a2713aSLionel Sambuc return false; 2073*f4a2713aSLionel Sambuc 2074*f4a2713aSLionel Sambuc unsigned SrcReg = Copy->getOperand(1).getReg(); 2075*f4a2713aSLionel Sambuc unsigned DstReg = Copy->getOperand(0).getReg(); 2076*f4a2713aSLionel Sambuc if (TargetRegisterInfo::isPhysicalRegister(SrcReg) 2077*f4a2713aSLionel Sambuc || TargetRegisterInfo::isPhysicalRegister(DstReg)) 2078*f4a2713aSLionel Sambuc return false; 2079*f4a2713aSLionel Sambuc 2080*f4a2713aSLionel Sambuc return LIS->intervalIsInOneMBB(LIS->getInterval(SrcReg)) 2081*f4a2713aSLionel Sambuc || LIS->intervalIsInOneMBB(LIS->getInterval(DstReg)); 2082*f4a2713aSLionel Sambuc } 2083*f4a2713aSLionel Sambuc 2084*f4a2713aSLionel Sambuc // Try joining WorkList copies starting from index From. 2085*f4a2713aSLionel Sambuc // Null out any successful joins. 2086*f4a2713aSLionel Sambuc bool RegisterCoalescer:: 2087*f4a2713aSLionel Sambuc copyCoalesceWorkList(MutableArrayRef<MachineInstr*> CurrList) { 2088*f4a2713aSLionel Sambuc bool Progress = false; 2089*f4a2713aSLionel Sambuc for (unsigned i = 0, e = CurrList.size(); i != e; ++i) { 2090*f4a2713aSLionel Sambuc if (!CurrList[i]) 2091*f4a2713aSLionel Sambuc continue; 2092*f4a2713aSLionel Sambuc // Skip instruction pointers that have already been erased, for example by 2093*f4a2713aSLionel Sambuc // dead code elimination. 2094*f4a2713aSLionel Sambuc if (ErasedInstrs.erase(CurrList[i])) { 2095*f4a2713aSLionel Sambuc CurrList[i] = 0; 2096*f4a2713aSLionel Sambuc continue; 2097*f4a2713aSLionel Sambuc } 2098*f4a2713aSLionel Sambuc bool Again = false; 2099*f4a2713aSLionel Sambuc bool Success = joinCopy(CurrList[i], Again); 2100*f4a2713aSLionel Sambuc Progress |= Success; 2101*f4a2713aSLionel Sambuc if (Success || !Again) 2102*f4a2713aSLionel Sambuc CurrList[i] = 0; 2103*f4a2713aSLionel Sambuc } 2104*f4a2713aSLionel Sambuc return Progress; 2105*f4a2713aSLionel Sambuc } 2106*f4a2713aSLionel Sambuc 2107*f4a2713aSLionel Sambuc void 2108*f4a2713aSLionel Sambuc RegisterCoalescer::copyCoalesceInMBB(MachineBasicBlock *MBB) { 2109*f4a2713aSLionel Sambuc DEBUG(dbgs() << MBB->getName() << ":\n"); 2110*f4a2713aSLionel Sambuc 2111*f4a2713aSLionel Sambuc // Collect all copy-like instructions in MBB. Don't start coalescing anything 2112*f4a2713aSLionel Sambuc // yet, it might invalidate the iterator. 2113*f4a2713aSLionel Sambuc const unsigned PrevSize = WorkList.size(); 2114*f4a2713aSLionel Sambuc if (JoinGlobalCopies) { 2115*f4a2713aSLionel Sambuc // Coalesce copies bottom-up to coalesce local defs before local uses. They 2116*f4a2713aSLionel Sambuc // are not inherently easier to resolve, but slightly preferable until we 2117*f4a2713aSLionel Sambuc // have local live range splitting. In particular this is required by 2118*f4a2713aSLionel Sambuc // cmp+jmp macro fusion. 2119*f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); 2120*f4a2713aSLionel Sambuc MII != E; ++MII) { 2121*f4a2713aSLionel Sambuc if (!MII->isCopyLike()) 2122*f4a2713aSLionel Sambuc continue; 2123*f4a2713aSLionel Sambuc if (isLocalCopy(&(*MII), LIS)) 2124*f4a2713aSLionel Sambuc LocalWorkList.push_back(&(*MII)); 2125*f4a2713aSLionel Sambuc else 2126*f4a2713aSLionel Sambuc WorkList.push_back(&(*MII)); 2127*f4a2713aSLionel Sambuc } 2128*f4a2713aSLionel Sambuc } 2129*f4a2713aSLionel Sambuc else { 2130*f4a2713aSLionel Sambuc for (MachineBasicBlock::iterator MII = MBB->begin(), E = MBB->end(); 2131*f4a2713aSLionel Sambuc MII != E; ++MII) 2132*f4a2713aSLionel Sambuc if (MII->isCopyLike()) 2133*f4a2713aSLionel Sambuc WorkList.push_back(MII); 2134*f4a2713aSLionel Sambuc } 2135*f4a2713aSLionel Sambuc // Try coalescing the collected copies immediately, and remove the nulls. 2136*f4a2713aSLionel Sambuc // This prevents the WorkList from getting too large since most copies are 2137*f4a2713aSLionel Sambuc // joinable on the first attempt. 2138*f4a2713aSLionel Sambuc MutableArrayRef<MachineInstr*> 2139*f4a2713aSLionel Sambuc CurrList(WorkList.begin() + PrevSize, WorkList.end()); 2140*f4a2713aSLionel Sambuc if (copyCoalesceWorkList(CurrList)) 2141*f4a2713aSLionel Sambuc WorkList.erase(std::remove(WorkList.begin() + PrevSize, WorkList.end(), 2142*f4a2713aSLionel Sambuc (MachineInstr*)0), WorkList.end()); 2143*f4a2713aSLionel Sambuc } 2144*f4a2713aSLionel Sambuc 2145*f4a2713aSLionel Sambuc void RegisterCoalescer::coalesceLocals() { 2146*f4a2713aSLionel Sambuc copyCoalesceWorkList(LocalWorkList); 2147*f4a2713aSLionel Sambuc for (unsigned j = 0, je = LocalWorkList.size(); j != je; ++j) { 2148*f4a2713aSLionel Sambuc if (LocalWorkList[j]) 2149*f4a2713aSLionel Sambuc WorkList.push_back(LocalWorkList[j]); 2150*f4a2713aSLionel Sambuc } 2151*f4a2713aSLionel Sambuc LocalWorkList.clear(); 2152*f4a2713aSLionel Sambuc } 2153*f4a2713aSLionel Sambuc 2154*f4a2713aSLionel Sambuc void RegisterCoalescer::joinAllIntervals() { 2155*f4a2713aSLionel Sambuc DEBUG(dbgs() << "********** JOINING INTERVALS ***********\n"); 2156*f4a2713aSLionel Sambuc assert(WorkList.empty() && LocalWorkList.empty() && "Old data still around."); 2157*f4a2713aSLionel Sambuc 2158*f4a2713aSLionel Sambuc std::vector<MBBPriorityInfo> MBBs; 2159*f4a2713aSLionel Sambuc MBBs.reserve(MF->size()); 2160*f4a2713aSLionel Sambuc for (MachineFunction::iterator I = MF->begin(), E = MF->end();I != E;++I){ 2161*f4a2713aSLionel Sambuc MachineBasicBlock *MBB = I; 2162*f4a2713aSLionel Sambuc MBBs.push_back(MBBPriorityInfo(MBB, Loops->getLoopDepth(MBB), 2163*f4a2713aSLionel Sambuc JoinSplitEdges && isSplitEdge(MBB))); 2164*f4a2713aSLionel Sambuc } 2165*f4a2713aSLionel Sambuc array_pod_sort(MBBs.begin(), MBBs.end(), compareMBBPriority); 2166*f4a2713aSLionel Sambuc 2167*f4a2713aSLionel Sambuc // Coalesce intervals in MBB priority order. 2168*f4a2713aSLionel Sambuc unsigned CurrDepth = UINT_MAX; 2169*f4a2713aSLionel Sambuc for (unsigned i = 0, e = MBBs.size(); i != e; ++i) { 2170*f4a2713aSLionel Sambuc // Try coalescing the collected local copies for deeper loops. 2171*f4a2713aSLionel Sambuc if (JoinGlobalCopies && MBBs[i].Depth < CurrDepth) { 2172*f4a2713aSLionel Sambuc coalesceLocals(); 2173*f4a2713aSLionel Sambuc CurrDepth = MBBs[i].Depth; 2174*f4a2713aSLionel Sambuc } 2175*f4a2713aSLionel Sambuc copyCoalesceInMBB(MBBs[i].MBB); 2176*f4a2713aSLionel Sambuc } 2177*f4a2713aSLionel Sambuc coalesceLocals(); 2178*f4a2713aSLionel Sambuc 2179*f4a2713aSLionel Sambuc // Joining intervals can allow other intervals to be joined. Iteratively join 2180*f4a2713aSLionel Sambuc // until we make no progress. 2181*f4a2713aSLionel Sambuc while (copyCoalesceWorkList(WorkList)) 2182*f4a2713aSLionel Sambuc /* empty */ ; 2183*f4a2713aSLionel Sambuc } 2184*f4a2713aSLionel Sambuc 2185*f4a2713aSLionel Sambuc void RegisterCoalescer::releaseMemory() { 2186*f4a2713aSLionel Sambuc ErasedInstrs.clear(); 2187*f4a2713aSLionel Sambuc WorkList.clear(); 2188*f4a2713aSLionel Sambuc DeadDefs.clear(); 2189*f4a2713aSLionel Sambuc InflateRegs.clear(); 2190*f4a2713aSLionel Sambuc } 2191*f4a2713aSLionel Sambuc 2192*f4a2713aSLionel Sambuc bool RegisterCoalescer::runOnMachineFunction(MachineFunction &fn) { 2193*f4a2713aSLionel Sambuc MF = &fn; 2194*f4a2713aSLionel Sambuc MRI = &fn.getRegInfo(); 2195*f4a2713aSLionel Sambuc TM = &fn.getTarget(); 2196*f4a2713aSLionel Sambuc TRI = TM->getRegisterInfo(); 2197*f4a2713aSLionel Sambuc TII = TM->getInstrInfo(); 2198*f4a2713aSLionel Sambuc LIS = &getAnalysis<LiveIntervals>(); 2199*f4a2713aSLionel Sambuc AA = &getAnalysis<AliasAnalysis>(); 2200*f4a2713aSLionel Sambuc Loops = &getAnalysis<MachineLoopInfo>(); 2201*f4a2713aSLionel Sambuc 2202*f4a2713aSLionel Sambuc const TargetSubtargetInfo &ST = TM->getSubtarget<TargetSubtargetInfo>(); 2203*f4a2713aSLionel Sambuc if (EnableGlobalCopies == cl::BOU_UNSET) 2204*f4a2713aSLionel Sambuc JoinGlobalCopies = ST.useMachineScheduler(); 2205*f4a2713aSLionel Sambuc else 2206*f4a2713aSLionel Sambuc JoinGlobalCopies = (EnableGlobalCopies == cl::BOU_TRUE); 2207*f4a2713aSLionel Sambuc 2208*f4a2713aSLionel Sambuc // The MachineScheduler does not currently require JoinSplitEdges. This will 2209*f4a2713aSLionel Sambuc // either be enabled unconditionally or replaced by a more general live range 2210*f4a2713aSLionel Sambuc // splitting optimization. 2211*f4a2713aSLionel Sambuc JoinSplitEdges = EnableJoinSplits; 2212*f4a2713aSLionel Sambuc 2213*f4a2713aSLionel Sambuc DEBUG(dbgs() << "********** SIMPLE REGISTER COALESCING **********\n" 2214*f4a2713aSLionel Sambuc << "********** Function: " << MF->getName() << '\n'); 2215*f4a2713aSLionel Sambuc 2216*f4a2713aSLionel Sambuc if (VerifyCoalescing) 2217*f4a2713aSLionel Sambuc MF->verify(this, "Before register coalescing"); 2218*f4a2713aSLionel Sambuc 2219*f4a2713aSLionel Sambuc RegClassInfo.runOnMachineFunction(fn); 2220*f4a2713aSLionel Sambuc 2221*f4a2713aSLionel Sambuc // Join (coalesce) intervals if requested. 2222*f4a2713aSLionel Sambuc if (EnableJoining) 2223*f4a2713aSLionel Sambuc joinAllIntervals(); 2224*f4a2713aSLionel Sambuc 2225*f4a2713aSLionel Sambuc // After deleting a lot of copies, register classes may be less constrained. 2226*f4a2713aSLionel Sambuc // Removing sub-register operands may allow GR32_ABCD -> GR32 and DPR_VFP2 -> 2227*f4a2713aSLionel Sambuc // DPR inflation. 2228*f4a2713aSLionel Sambuc array_pod_sort(InflateRegs.begin(), InflateRegs.end()); 2229*f4a2713aSLionel Sambuc InflateRegs.erase(std::unique(InflateRegs.begin(), InflateRegs.end()), 2230*f4a2713aSLionel Sambuc InflateRegs.end()); 2231*f4a2713aSLionel Sambuc DEBUG(dbgs() << "Trying to inflate " << InflateRegs.size() << " regs.\n"); 2232*f4a2713aSLionel Sambuc for (unsigned i = 0, e = InflateRegs.size(); i != e; ++i) { 2233*f4a2713aSLionel Sambuc unsigned Reg = InflateRegs[i]; 2234*f4a2713aSLionel Sambuc if (MRI->reg_nodbg_empty(Reg)) 2235*f4a2713aSLionel Sambuc continue; 2236*f4a2713aSLionel Sambuc if (MRI->recomputeRegClass(Reg, *TM)) { 2237*f4a2713aSLionel Sambuc DEBUG(dbgs() << PrintReg(Reg) << " inflated to " 2238*f4a2713aSLionel Sambuc << MRI->getRegClass(Reg)->getName() << '\n'); 2239*f4a2713aSLionel Sambuc ++NumInflated; 2240*f4a2713aSLionel Sambuc } 2241*f4a2713aSLionel Sambuc } 2242*f4a2713aSLionel Sambuc 2243*f4a2713aSLionel Sambuc DEBUG(dump()); 2244*f4a2713aSLionel Sambuc if (VerifyCoalescing) 2245*f4a2713aSLionel Sambuc MF->verify(this, "After register coalescing"); 2246*f4a2713aSLionel Sambuc return true; 2247*f4a2713aSLionel Sambuc } 2248*f4a2713aSLionel Sambuc 2249*f4a2713aSLionel Sambuc /// print - Implement the dump method. 2250*f4a2713aSLionel Sambuc void RegisterCoalescer::print(raw_ostream &O, const Module* m) const { 2251*f4a2713aSLionel Sambuc LIS->print(O, m); 2252*f4a2713aSLionel Sambuc } 2253