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