1 //===--- HexagonExpandCondsets.cpp ----------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // Replace mux instructions with the corresponding legal instructions.
11 // It is meant to work post-SSA, but still on virtual registers. It was
12 // originally placed between register coalescing and machine instruction
13 // scheduler.
14 // In this place in the optimization sequence, live interval analysis had
15 // been performed, and the live intervals should be preserved. A large part
16 // of the code deals with preserving the liveness information.
17 //
18 // Liveness tracking aside, the main functionality of this pass is divided
19 // into two steps. The first step is to replace an instruction
20 //   vreg0 = C2_mux vreg1, vreg2, vreg3
21 // with a pair of conditional transfers
22 //   vreg0 = A2_tfrt vreg1, vreg2
23 //   vreg0 = A2_tfrf vreg1, vreg3
24 // It is the intention that the execution of this pass could be terminated
25 // after this step, and the code generated would be functionally correct.
26 //
27 // If the uses of the source values vreg1 and vreg2 are kills, and their
28 // definitions are predicable, then in the second step, the conditional
29 // transfers will then be rewritten as predicated instructions. E.g.
30 //   vreg0 = A2_or vreg1, vreg2
31 //   vreg3 = A2_tfrt vreg99, vreg0<kill>
32 // will be rewritten as
33 //   vreg3 = A2_port vreg99, vreg1, vreg2
34 //
35 // This replacement has two variants: "up" and "down". Consider this case:
36 //   vreg0 = A2_or vreg1, vreg2
37 //   ... [intervening instructions] ...
38 //   vreg3 = A2_tfrt vreg99, vreg0<kill>
39 // variant "up":
40 //   vreg3 = A2_port vreg99, vreg1, vreg2
41 //   ... [intervening instructions, vreg0->vreg3] ...
42 //   [deleted]
43 // variant "down":
44 //   [deleted]
45 //   ... [intervening instructions] ...
46 //   vreg3 = A2_port vreg99, vreg1, vreg2
47 //
48 // Both, one or none of these variants may be valid, and checks are made
49 // to rule out inapplicable variants.
50 //
51 // As an additional optimization, before either of the two steps above is
52 // executed, the pass attempts to coalesce the target register with one of
53 // the source registers, e.g. given an instruction
54 //   vreg3 = C2_mux vreg0, vreg1, vreg2
55 // vreg3 will be coalesced with either vreg1 or vreg2. If this succeeds,
56 // the instruction would then be (for example)
57 //   vreg3 = C2_mux vreg0, vreg3, vreg2
58 // and, under certain circumstances, this could result in only one predicated
59 // instruction:
60 //   vreg3 = A2_tfrf vreg0, vreg2
61 //
62 
63 // Splitting a definition of a register into two predicated transfers
64 // creates a complication in liveness tracking. Live interval computation
65 // will see both instructions as actual definitions, and will mark the
66 // first one as dead. The definition is not actually dead, and this
67 // situation will need to be fixed. For example:
68 //   vreg1<def,dead> = A2_tfrt ...  ; marked as dead
69 //   vreg1<def> = A2_tfrf ...
70 //
71 // Since any of the individual predicated transfers may end up getting
72 // removed (in case it is an identity copy), some pre-existing def may
73 // be marked as dead after live interval recomputation:
74 //   vreg1<def,dead> = ...          ; marked as dead
75 //   ...
76 //   vreg1<def> = A2_tfrf ...       ; if A2_tfrt is removed
77 // This case happens if vreg1 was used as a source in A2_tfrt, which means
78 // that is it actually live at the A2_tfrf, and so the now dead definition
79 // of vreg1 will need to be updated to non-dead at some point.
80 //
81 // This issue could be remedied by adding implicit uses to the predicated
82 // transfers, but this will create a problem with subsequent predication,
83 // since the transfers will no longer be possible to reorder. To avoid
84 // that, the initial splitting will not add any implicit uses. These
85 // implicit uses will be added later, after predication. The extra price,
86 // however, is that finding the locations where the implicit uses need
87 // to be added, and updating the live ranges will be more involved.
88 
89 #define DEBUG_TYPE "expand-condsets"
90 
91 #include "HexagonTargetMachine.h"
92 #include "llvm/ADT/SetVector.h"
93 #include "llvm/CodeGen/Passes.h"
94 #include "llvm/CodeGen/LiveInterval.h"
95 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
96 #include "llvm/CodeGen/MachineDominators.h"
97 #include "llvm/CodeGen/MachineFunction.h"
98 #include "llvm/CodeGen/MachineInstrBuilder.h"
99 #include "llvm/CodeGen/MachineRegisterInfo.h"
100 #include "llvm/Target/TargetInstrInfo.h"
101 #include "llvm/Target/TargetMachine.h"
102 #include "llvm/Target/TargetRegisterInfo.h"
103 #include "llvm/Support/CommandLine.h"
104 #include "llvm/Support/Debug.h"
105 #include "llvm/Support/raw_ostream.h"
106 
107 #include <algorithm>
108 #include <iterator>
109 #include <set>
110 #include <utility>
111 
112 using namespace llvm;
113 
114 static cl::opt<unsigned> OptTfrLimit("expand-condsets-tfr-limit",
115   cl::init(~0U), cl::Hidden, cl::desc("Max number of mux expansions"));
116 static cl::opt<unsigned> OptCoaLimit("expand-condsets-coa-limit",
117   cl::init(~0U), cl::Hidden, cl::desc("Max number of segment coalescings"));
118 
119 namespace llvm {
120   void initializeHexagonExpandCondsetsPass(PassRegistry&);
121   FunctionPass *createHexagonExpandCondsets();
122 }
123 
124 namespace {
125   class HexagonExpandCondsets : public MachineFunctionPass {
126   public:
127     static char ID;
128     HexagonExpandCondsets() :
129         MachineFunctionPass(ID), HII(0), TRI(0), MRI(0),
130         LIS(0), CoaLimitActive(false),
131         TfrLimitActive(false), CoaCounter(0), TfrCounter(0) {
132       if (OptCoaLimit.getPosition())
133         CoaLimitActive = true, CoaLimit = OptCoaLimit;
134       if (OptTfrLimit.getPosition())
135         TfrLimitActive = true, TfrLimit = OptTfrLimit;
136       initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry());
137     }
138 
139     StringRef getPassName() const override { return "Hexagon Expand Condsets"; }
140     void getAnalysisUsage(AnalysisUsage &AU) const override {
141       AU.addRequired<LiveIntervals>();
142       AU.addPreserved<LiveIntervals>();
143       AU.addPreserved<SlotIndexes>();
144       AU.addRequired<MachineDominatorTree>();
145       AU.addPreserved<MachineDominatorTree>();
146       MachineFunctionPass::getAnalysisUsage(AU);
147     }
148     bool runOnMachineFunction(MachineFunction &MF) override;
149 
150   private:
151     const HexagonInstrInfo *HII;
152     const TargetRegisterInfo *TRI;
153     MachineDominatorTree *MDT;
154     MachineRegisterInfo *MRI;
155     LiveIntervals *LIS;
156 
157     bool CoaLimitActive, TfrLimitActive;
158     unsigned CoaLimit, TfrLimit, CoaCounter, TfrCounter;
159 
160     struct RegisterRef {
161       RegisterRef(const MachineOperand &Op) : Reg(Op.getReg()),
162           Sub(Op.getSubReg()) {}
163       RegisterRef(unsigned R = 0, unsigned S = 0) : Reg(R), Sub(S) {}
164       bool operator== (RegisterRef RR) const {
165         return Reg == RR.Reg && Sub == RR.Sub;
166       }
167       bool operator!= (RegisterRef RR) const { return !operator==(RR); }
168       bool operator< (RegisterRef RR) const {
169         return Reg < RR.Reg || (Reg == RR.Reg && Sub < RR.Sub);
170       }
171       unsigned Reg, Sub;
172     };
173 
174     typedef DenseMap<unsigned,unsigned> ReferenceMap;
175     enum { Sub_Low = 0x1, Sub_High = 0x2, Sub_None = (Sub_Low | Sub_High) };
176     enum { Exec_Then = 0x10, Exec_Else = 0x20 };
177     unsigned getMaskForSub(unsigned Sub);
178     bool isCondset(const MachineInstr &MI);
179     LaneBitmask getLaneMask(unsigned Reg, unsigned Sub);
180 
181     void addRefToMap(RegisterRef RR, ReferenceMap &Map, unsigned Exec);
182     bool isRefInMap(RegisterRef, ReferenceMap &Map, unsigned Exec);
183 
184     void updateDeadsInRange(unsigned Reg, LaneBitmask LM, LiveRange &Range);
185     void updateKillFlags(unsigned Reg);
186     void updateDeadFlags(unsigned Reg);
187     void recalculateLiveInterval(unsigned Reg);
188     void removeInstr(MachineInstr &MI);
189     void updateLiveness(std::set<unsigned> &RegSet, bool Recalc,
190         bool UpdateKills, bool UpdateDeads);
191 
192     unsigned getCondTfrOpcode(const MachineOperand &SO, bool Cond);
193     MachineInstr *genCondTfrFor(MachineOperand &SrcOp,
194         MachineBasicBlock::iterator At, unsigned DstR,
195         unsigned DstSR, const MachineOperand &PredOp, bool PredSense,
196         bool ReadUndef, bool ImpUse);
197     bool split(MachineInstr &MI, std::set<unsigned> &UpdRegs);
198     bool splitInBlock(MachineBasicBlock &B, std::set<unsigned> &UpdRegs);
199 
200     bool isPredicable(MachineInstr *MI);
201     MachineInstr *getReachingDefForPred(RegisterRef RD,
202         MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond);
203     bool canMoveOver(MachineInstr &MI, ReferenceMap &Defs, ReferenceMap &Uses);
204     bool canMoveMemTo(MachineInstr &MI, MachineInstr &ToI, bool IsDown);
205     void predicateAt(const MachineOperand &DefOp, MachineInstr &MI,
206                      MachineBasicBlock::iterator Where,
207                      const MachineOperand &PredOp, bool Cond,
208                      std::set<unsigned> &UpdRegs);
209     void renameInRange(RegisterRef RO, RegisterRef RN, unsigned PredR,
210         bool Cond, MachineBasicBlock::iterator First,
211         MachineBasicBlock::iterator Last);
212     bool predicate(MachineInstr &TfrI, bool Cond, std::set<unsigned> &UpdRegs);
213     bool predicateInBlock(MachineBasicBlock &B,
214         std::set<unsigned> &UpdRegs);
215 
216     bool isIntReg(RegisterRef RR, unsigned &BW);
217     bool isIntraBlocks(LiveInterval &LI);
218     bool coalesceRegisters(RegisterRef R1, RegisterRef R2);
219     bool coalesceSegments(MachineFunction &MF);
220   };
221 }
222 
223 char HexagonExpandCondsets::ID = 0;
224 
225 namespace llvm {
226   char &HexagonExpandCondsetsID = HexagonExpandCondsets::ID;
227 }
228 
229 INITIALIZE_PASS_BEGIN(HexagonExpandCondsets, "expand-condsets",
230   "Hexagon Expand Condsets", false, false)
231 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
232 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
233 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
234 INITIALIZE_PASS_END(HexagonExpandCondsets, "expand-condsets",
235   "Hexagon Expand Condsets", false, false)
236 
237 unsigned HexagonExpandCondsets::getMaskForSub(unsigned Sub) {
238   switch (Sub) {
239     case Hexagon::subreg_loreg:
240       return Sub_Low;
241     case Hexagon::subreg_hireg:
242       return Sub_High;
243     case Hexagon::NoSubRegister:
244       return Sub_None;
245   }
246   llvm_unreachable("Invalid subregister");
247 }
248 
249 bool HexagonExpandCondsets::isCondset(const MachineInstr &MI) {
250   unsigned Opc = MI.getOpcode();
251   switch (Opc) {
252     case Hexagon::C2_mux:
253     case Hexagon::C2_muxii:
254     case Hexagon::C2_muxir:
255     case Hexagon::C2_muxri:
256     case Hexagon::PS_pselect:
257         return true;
258       break;
259   }
260   return false;
261 }
262 
263 
264 LaneBitmask HexagonExpandCondsets::getLaneMask(unsigned Reg, unsigned Sub) {
265   assert(TargetRegisterInfo::isVirtualRegister(Reg));
266   return Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
267                   : MRI->getMaxLaneMaskForVReg(Reg);
268 }
269 
270 
271 void HexagonExpandCondsets::addRefToMap(RegisterRef RR, ReferenceMap &Map,
272       unsigned Exec) {
273   unsigned Mask = getMaskForSub(RR.Sub) | Exec;
274   ReferenceMap::iterator F = Map.find(RR.Reg);
275   if (F == Map.end())
276     Map.insert(std::make_pair(RR.Reg, Mask));
277   else
278     F->second |= Mask;
279 }
280 
281 
282 bool HexagonExpandCondsets::isRefInMap(RegisterRef RR, ReferenceMap &Map,
283       unsigned Exec) {
284   ReferenceMap::iterator F = Map.find(RR.Reg);
285   if (F == Map.end())
286     return false;
287   unsigned Mask = getMaskForSub(RR.Sub) | Exec;
288   if (Mask & F->second)
289     return true;
290   return false;
291 }
292 
293 
294 void HexagonExpandCondsets::updateKillFlags(unsigned Reg) {
295   auto KillAt = [this,Reg] (SlotIndex K, LaneBitmask LM) -> void {
296     // Set the <kill> flag on a use of Reg whose lane mask is contained in LM.
297     MachineInstr *MI = LIS->getInstructionFromIndex(K);
298     for (auto &Op : MI->operands()) {
299       if (!Op.isReg() || !Op.isUse() || Op.getReg() != Reg)
300         continue;
301       LaneBitmask SLM = getLaneMask(Reg, Op.getSubReg());
302       if ((SLM & LM) == SLM) {
303         // Only set the kill flag on the first encountered use of Reg in this
304         // instruction.
305         Op.setIsKill(true);
306         break;
307       }
308     }
309   };
310 
311   LiveInterval &LI = LIS->getInterval(Reg);
312   for (auto I = LI.begin(), E = LI.end(); I != E; ++I) {
313     if (!I->end.isRegister())
314       continue;
315     // Do not mark the end of the segment as <kill>, if the next segment
316     // starts with a predicated instruction.
317     auto NextI = std::next(I);
318     if (NextI != E && NextI->start.isRegister()) {
319       MachineInstr *DefI = LIS->getInstructionFromIndex(NextI->start);
320       if (HII->isPredicated(*DefI))
321         continue;
322     }
323     bool WholeReg = true;
324     if (LI.hasSubRanges()) {
325       auto EndsAtI = [I] (LiveInterval::SubRange &S) -> bool {
326         LiveRange::iterator F = S.find(I->end);
327         return F != S.end() && I->end == F->end;
328       };
329       // Check if all subranges end at I->end. If so, make sure to kill
330       // the whole register.
331       for (LiveInterval::SubRange &S : LI.subranges()) {
332         if (EndsAtI(S))
333           KillAt(I->end, S.LaneMask);
334         else
335           WholeReg = false;
336       }
337     }
338     if (WholeReg)
339       KillAt(I->end, MRI->getMaxLaneMaskForVReg(Reg));
340   }
341 }
342 
343 
344 void HexagonExpandCondsets::updateDeadsInRange(unsigned Reg, LaneBitmask LM,
345       LiveRange &Range) {
346   assert(TargetRegisterInfo::isVirtualRegister(Reg));
347   if (Range.empty())
348     return;
349 
350   auto IsRegDef = [this,Reg,LM] (MachineOperand &Op) -> bool {
351     if (!Op.isReg() || !Op.isDef())
352       return false;
353     unsigned DR = Op.getReg(), DSR = Op.getSubReg();
354     if (!TargetRegisterInfo::isVirtualRegister(DR) || DR != Reg)
355       return false;
356     LaneBitmask SLM = getLaneMask(DR, DSR);
357     return (SLM & LM) != 0;
358   };
359 
360   // The splitting step will create pairs of predicated definitions without
361   // any implicit uses (since implicit uses would interfere with predication).
362   // This can cause the reaching defs to become dead after live range
363   // recomputation, even though they are not really dead.
364   // We need to identify predicated defs that need implicit uses, and
365   // dead defs that are not really dead, and correct both problems.
366 
367   SetVector<MachineBasicBlock*> Defs;
368   auto Dominate = [this] (SetVector<MachineBasicBlock*> &Defs,
369                           MachineBasicBlock *Dest) -> bool {
370     for (MachineBasicBlock *D : Defs)
371       if (D != Dest && MDT->dominates(D, Dest))
372         return true;
373 
374     MachineBasicBlock *Entry = &Dest->getParent()->front();
375     SetVector<MachineBasicBlock*> Work(Dest->pred_begin(), Dest->pred_end());
376     for (unsigned i = 0; i < Work.size(); ++i) {
377       MachineBasicBlock *B = Work[i];
378       if (Defs.count(B))
379         continue;
380       if (B == Entry)
381         return false;
382       for (auto *P : B->predecessors())
383         Work.insert(P);
384     }
385     return true;
386   };
387 
388   // First, try to extend live range within individual basic blocks. This
389   // will leave us only with dead defs that do not reach any predicated
390   // defs in the same block.
391   SmallVector<SlotIndex,4> PredDefs;
392   for (auto &Seg : Range) {
393     if (!Seg.start.isRegister())
394       continue;
395     MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
396     Defs.insert(DefI->getParent());
397     if (HII->isPredicated(*DefI))
398       PredDefs.push_back(Seg.start);
399   }
400 
401   SmallVector<SlotIndex,8> Undefs;
402   LiveInterval &LI = LIS->getInterval(Reg);
403   LI.computeSubRangeUndefs(Undefs, LM, *MRI, *LIS->getSlotIndexes());
404 
405   for (auto &SI : PredDefs) {
406     MachineBasicBlock *BB = LIS->getMBBFromIndex(SI);
407     auto P = Range.extendInBlock(Undefs, LIS->getMBBStartIdx(BB), SI);
408     if (P.first != nullptr || P.second)
409       SI = SlotIndex();
410   }
411 
412   // Calculate reachability for those predicated defs that were not handled
413   // by the in-block extension.
414   SmallVector<SlotIndex,4> ExtTo;
415   for (auto &SI : PredDefs) {
416     if (!SI.isValid())
417       continue;
418     MachineBasicBlock *BB = LIS->getMBBFromIndex(SI);
419     if (BB->pred_empty())
420       continue;
421     // If the defs from this range reach SI via all predecessors, it is live.
422     if (Dominate(Defs, BB))
423       ExtTo.push_back(SI);
424   }
425   LIS->extendToIndices(Range, ExtTo, Undefs);
426 
427   // Remove <dead> flags from all defs that are not dead after live range
428   // extension, and collect all def operands. They will be used to generate
429   // the necessary implicit uses.
430   std::set<RegisterRef> DefRegs;
431   for (auto &Seg : Range) {
432     if (!Seg.start.isRegister())
433       continue;
434     MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
435     for (auto &Op : DefI->operands()) {
436       if (Seg.start.isDead() || !IsRegDef(Op))
437         continue;
438       DefRegs.insert(Op);
439       Op.setIsDead(false);
440     }
441   }
442 
443   // Finally, add implicit uses to each predicated def that is reached
444   // by other defs.
445   for (auto &Seg : Range) {
446     if (!Seg.start.isRegister() || !Range.liveAt(Seg.start.getPrevSlot()))
447       continue;
448     MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
449     if (!HII->isPredicated(*DefI))
450       continue;
451     MachineFunction &MF = *DefI->getParent()->getParent();
452     // Construct the set of all necessary implicit uses, based on the def
453     // operands in the instruction.
454     std::set<RegisterRef> ImpUses;
455     for (auto &Op : DefI->operands())
456       if (Op.isReg() && Op.isDef() && DefRegs.count(Op))
457         ImpUses.insert(Op);
458     for (RegisterRef R : ImpUses)
459       MachineInstrBuilder(MF, DefI).addReg(R.Reg, RegState::Implicit, R.Sub);
460   }
461 }
462 
463 
464 void HexagonExpandCondsets::updateDeadFlags(unsigned Reg) {
465   LiveInterval &LI = LIS->getInterval(Reg);
466   if (LI.hasSubRanges()) {
467     for (LiveInterval::SubRange &S : LI.subranges()) {
468       updateDeadsInRange(Reg, S.LaneMask, S);
469       LIS->shrinkToUses(S, Reg);
470     }
471     LI.clear();
472     LIS->constructMainRangeFromSubranges(LI);
473   } else {
474     updateDeadsInRange(Reg, MRI->getMaxLaneMaskForVReg(Reg), LI);
475   }
476 }
477 
478 
479 void HexagonExpandCondsets::recalculateLiveInterval(unsigned Reg) {
480   LIS->removeInterval(Reg);
481   LIS->createAndComputeVirtRegInterval(Reg);
482 }
483 
484 void HexagonExpandCondsets::removeInstr(MachineInstr &MI) {
485   LIS->RemoveMachineInstrFromMaps(MI);
486   MI.eraseFromParent();
487 }
488 
489 
490 void HexagonExpandCondsets::updateLiveness(std::set<unsigned> &RegSet,
491       bool Recalc, bool UpdateKills, bool UpdateDeads) {
492   UpdateKills |= UpdateDeads;
493   for (auto R : RegSet) {
494     if (Recalc)
495       recalculateLiveInterval(R);
496     if (UpdateKills)
497       MRI->clearKillFlags(R);
498     if (UpdateDeads)
499       updateDeadFlags(R);
500     // Fixing <dead> flags may extend live ranges, so reset <kill> flags
501     // after that.
502     if (UpdateKills)
503       updateKillFlags(R);
504     LIS->getInterval(R).verify();
505   }
506 }
507 
508 
509 /// Get the opcode for a conditional transfer of the value in SO (source
510 /// operand). The condition (true/false) is given in Cond.
511 unsigned HexagonExpandCondsets::getCondTfrOpcode(const MachineOperand &SO,
512       bool IfTrue) {
513   using namespace Hexagon;
514   if (SO.isReg()) {
515     unsigned PhysR;
516     RegisterRef RS = SO;
517     if (TargetRegisterInfo::isVirtualRegister(RS.Reg)) {
518       const TargetRegisterClass *VC = MRI->getRegClass(RS.Reg);
519       assert(VC->begin() != VC->end() && "Empty register class");
520       PhysR = *VC->begin();
521     } else {
522       assert(TargetRegisterInfo::isPhysicalRegister(RS.Reg));
523       PhysR = RS.Reg;
524     }
525     unsigned PhysS = (RS.Sub == 0) ? PhysR : TRI->getSubReg(PhysR, RS.Sub);
526     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysS);
527     switch (RC->getSize()) {
528       case 4:
529         return IfTrue ? A2_tfrt : A2_tfrf;
530       case 8:
531         return IfTrue ? A2_tfrpt : A2_tfrpf;
532     }
533     llvm_unreachable("Invalid register operand");
534   }
535   if (SO.isImm() || SO.isFPImm())
536     return IfTrue ? C2_cmoveit : C2_cmoveif;
537   llvm_unreachable("Unexpected source operand");
538 }
539 
540 
541 /// Generate a conditional transfer, copying the value SrcOp to the
542 /// destination register DstR:DstSR, and using the predicate register from
543 /// PredOp. The Cond argument specifies whether the predicate is to be
544 /// if(PredOp), or if(!PredOp).
545 MachineInstr *HexagonExpandCondsets::genCondTfrFor(MachineOperand &SrcOp,
546       MachineBasicBlock::iterator At,
547       unsigned DstR, unsigned DstSR, const MachineOperand &PredOp,
548       bool PredSense, bool ReadUndef, bool ImpUse) {
549   MachineInstr *MI = SrcOp.getParent();
550   MachineBasicBlock &B = *At->getParent();
551   const DebugLoc &DL = MI->getDebugLoc();
552 
553   // Don't avoid identity copies here (i.e. if the source and the destination
554   // are the same registers). It is actually better to generate them here,
555   // since this would cause the copy to potentially be predicated in the next
556   // step. The predication will remove such a copy if it is unable to
557   /// predicate.
558 
559   unsigned Opc = getCondTfrOpcode(SrcOp, PredSense);
560   unsigned State = RegState::Define | (ReadUndef ? RegState::Undef : 0);
561   MachineInstrBuilder MIB = BuildMI(B, At, DL, HII->get(Opc))
562         .addReg(DstR, State, DstSR)
563         .addOperand(PredOp)
564         .addOperand(SrcOp);
565 
566   // We don't want any kills yet.
567   MIB->clearKillInfo();
568   DEBUG(dbgs() << "created an initial copy: " << *MIB);
569   return &*MIB;
570 }
571 
572 
573 /// Replace a MUX instruction MI with a pair A2_tfrt/A2_tfrf. This function
574 /// performs all necessary changes to complete the replacement.
575 bool HexagonExpandCondsets::split(MachineInstr &MI,
576                                   std::set<unsigned> &UpdRegs) {
577   if (TfrLimitActive) {
578     if (TfrCounter >= TfrLimit)
579       return false;
580     TfrCounter++;
581   }
582   DEBUG(dbgs() << "\nsplitting BB#" << MI.getParent()->getNumber() << ": "
583                << MI);
584   MachineOperand &MD = MI.getOperand(0);  // Definition
585   MachineOperand &MP = MI.getOperand(1);  // Predicate register
586   assert(MD.isDef());
587   unsigned DR = MD.getReg(), DSR = MD.getSubReg();
588   bool ReadUndef = MD.isUndef();
589   MachineBasicBlock::iterator At = MI;
590 
591   // First, create the two invididual conditional transfers, and add each
592   // of them to the live intervals information. Do that first and then remove
593   // the old instruction from live intervals.
594   MachineInstr *TfrT =
595       genCondTfrFor(MI.getOperand(2), At, DR, DSR, MP, true, ReadUndef, false);
596   MachineInstr *TfrF =
597       genCondTfrFor(MI.getOperand(3), At, DR, DSR, MP, false, ReadUndef, true);
598   LIS->InsertMachineInstrInMaps(*TfrT);
599   LIS->InsertMachineInstrInMaps(*TfrF);
600 
601   // Will need to recalculate live intervals for all registers in MI.
602   for (auto &Op : MI.operands())
603     if (Op.isReg())
604       UpdRegs.insert(Op.getReg());
605 
606   removeInstr(MI);
607   return true;
608 }
609 
610 
611 /// Split all MUX instructions in the given block into pairs of conditional
612 /// transfers.
613 bool HexagonExpandCondsets::splitInBlock(MachineBasicBlock &B,
614       std::set<unsigned> &UpdRegs) {
615   bool Changed = false;
616   MachineBasicBlock::iterator I, E, NextI;
617   for (I = B.begin(), E = B.end(); I != E; I = NextI) {
618     NextI = std::next(I);
619     if (isCondset(*I))
620       Changed |= split(*I, UpdRegs);
621   }
622   return Changed;
623 }
624 
625 
626 bool HexagonExpandCondsets::isPredicable(MachineInstr *MI) {
627   if (HII->isPredicated(*MI) || !HII->isPredicable(*MI))
628     return false;
629   if (MI->hasUnmodeledSideEffects() || MI->mayStore())
630     return false;
631   // Reject instructions with multiple defs (e.g. post-increment loads).
632   bool HasDef = false;
633   for (auto &Op : MI->operands()) {
634     if (!Op.isReg() || !Op.isDef())
635       continue;
636     if (HasDef)
637       return false;
638     HasDef = true;
639   }
640   for (auto &Mo : MI->memoperands())
641     if (Mo->isVolatile())
642       return false;
643   return true;
644 }
645 
646 
647 /// Find the reaching definition for a predicated use of RD. The RD is used
648 /// under the conditions given by PredR and Cond, and this function will ignore
649 /// definitions that set RD under the opposite conditions.
650 MachineInstr *HexagonExpandCondsets::getReachingDefForPred(RegisterRef RD,
651       MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond) {
652   MachineBasicBlock &B = *UseIt->getParent();
653   MachineBasicBlock::iterator I = UseIt, S = B.begin();
654   if (I == S)
655     return 0;
656 
657   bool PredValid = true;
658   do {
659     --I;
660     MachineInstr *MI = &*I;
661     // Check if this instruction can be ignored, i.e. if it is predicated
662     // on the complementary condition.
663     if (PredValid && HII->isPredicated(*MI)) {
664       if (MI->readsRegister(PredR) && (Cond != HII->isPredicatedTrue(*MI)))
665         continue;
666     }
667 
668     // Check the defs. If the PredR is defined, invalidate it. If RD is
669     // defined, return the instruction or 0, depending on the circumstances.
670     for (auto &Op : MI->operands()) {
671       if (!Op.isReg() || !Op.isDef())
672         continue;
673       RegisterRef RR = Op;
674       if (RR.Reg == PredR) {
675         PredValid = false;
676         continue;
677       }
678       if (RR.Reg != RD.Reg)
679         continue;
680       // If the "Reg" part agrees, there is still the subregister to check.
681       // If we are looking for vreg1:loreg, we can skip vreg1:hireg, but
682       // not vreg1 (w/o subregisters).
683       if (RR.Sub == RD.Sub)
684         return MI;
685       if (RR.Sub == 0 || RD.Sub == 0)
686         return 0;
687       // We have different subregisters, so we can continue looking.
688     }
689   } while (I != S);
690 
691   return 0;
692 }
693 
694 
695 /// Check if the instruction MI can be safely moved over a set of instructions
696 /// whose side-effects (in terms of register defs and uses) are expressed in
697 /// the maps Defs and Uses. These maps reflect the conditional defs and uses
698 /// that depend on the same predicate register to allow moving instructions
699 /// over instructions predicated on the opposite condition.
700 bool HexagonExpandCondsets::canMoveOver(MachineInstr &MI, ReferenceMap &Defs,
701                                         ReferenceMap &Uses) {
702   // In order to be able to safely move MI over instructions that define
703   // "Defs" and use "Uses", no def operand from MI can be defined or used
704   // and no use operand can be defined.
705   for (auto &Op : MI.operands()) {
706     if (!Op.isReg())
707       continue;
708     RegisterRef RR = Op;
709     // For physical register we would need to check register aliases, etc.
710     // and we don't want to bother with that. It would be of little value
711     // before the actual register rewriting (from virtual to physical).
712     if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
713       return false;
714     // No redefs for any operand.
715     if (isRefInMap(RR, Defs, Exec_Then))
716       return false;
717     // For defs, there cannot be uses.
718     if (Op.isDef() && isRefInMap(RR, Uses, Exec_Then))
719       return false;
720   }
721   return true;
722 }
723 
724 
725 /// Check if the instruction accessing memory (TheI) can be moved to the
726 /// location ToI.
727 bool HexagonExpandCondsets::canMoveMemTo(MachineInstr &TheI, MachineInstr &ToI,
728                                          bool IsDown) {
729   bool IsLoad = TheI.mayLoad(), IsStore = TheI.mayStore();
730   if (!IsLoad && !IsStore)
731     return true;
732   if (HII->areMemAccessesTriviallyDisjoint(TheI, ToI))
733     return true;
734   if (TheI.hasUnmodeledSideEffects())
735     return false;
736 
737   MachineBasicBlock::iterator StartI = IsDown ? TheI : ToI;
738   MachineBasicBlock::iterator EndI = IsDown ? ToI : TheI;
739   bool Ordered = TheI.hasOrderedMemoryRef();
740 
741   // Search for aliased memory reference in (StartI, EndI).
742   for (MachineBasicBlock::iterator I = std::next(StartI); I != EndI; ++I) {
743     MachineInstr *MI = &*I;
744     if (MI->hasUnmodeledSideEffects())
745       return false;
746     bool L = MI->mayLoad(), S = MI->mayStore();
747     if (!L && !S)
748       continue;
749     if (Ordered && MI->hasOrderedMemoryRef())
750       return false;
751 
752     bool Conflict = (L && IsStore) || S;
753     if (Conflict)
754       return false;
755   }
756   return true;
757 }
758 
759 
760 /// Generate a predicated version of MI (where the condition is given via
761 /// PredR and Cond) at the point indicated by Where.
762 void HexagonExpandCondsets::predicateAt(const MachineOperand &DefOp,
763                                         MachineInstr &MI,
764                                         MachineBasicBlock::iterator Where,
765                                         const MachineOperand &PredOp, bool Cond,
766                                         std::set<unsigned> &UpdRegs) {
767   // The problem with updating live intervals is that we can move one def
768   // past another def. In particular, this can happen when moving an A2_tfrt
769   // over an A2_tfrf defining the same register. From the point of view of
770   // live intervals, these two instructions are two separate definitions,
771   // and each one starts another live segment. LiveIntervals's "handleMove"
772   // does not allow such moves, so we need to handle it ourselves. To avoid
773   // invalidating liveness data while we are using it, the move will be
774   // implemented in 4 steps: (1) add a clone of the instruction MI at the
775   // target location, (2) update liveness, (3) delete the old instruction,
776   // and (4) update liveness again.
777 
778   MachineBasicBlock &B = *MI.getParent();
779   DebugLoc DL = Where->getDebugLoc();  // "Where" points to an instruction.
780   unsigned Opc = MI.getOpcode();
781   unsigned PredOpc = HII->getCondOpcode(Opc, !Cond);
782   MachineInstrBuilder MB = BuildMI(B, Where, DL, HII->get(PredOpc));
783   unsigned Ox = 0, NP = MI.getNumOperands();
784   // Skip all defs from MI first.
785   while (Ox < NP) {
786     MachineOperand &MO = MI.getOperand(Ox);
787     if (!MO.isReg() || !MO.isDef())
788       break;
789     Ox++;
790   }
791   // Add the new def, then the predicate register, then the rest of the
792   // operands.
793   MB.addReg(DefOp.getReg(), getRegState(DefOp), DefOp.getSubReg());
794   MB.addReg(PredOp.getReg(), PredOp.isUndef() ? RegState::Undef : 0,
795             PredOp.getSubReg());
796   while (Ox < NP) {
797     MachineOperand &MO = MI.getOperand(Ox);
798     if (!MO.isReg() || !MO.isImplicit())
799       MB.addOperand(MO);
800     Ox++;
801   }
802 
803   MachineFunction &MF = *B.getParent();
804   MachineInstr::mmo_iterator I = MI.memoperands_begin();
805   unsigned NR = std::distance(I, MI.memoperands_end());
806   MachineInstr::mmo_iterator MemRefs = MF.allocateMemRefsArray(NR);
807   for (unsigned i = 0; i < NR; ++i)
808     MemRefs[i] = *I++;
809   MB.setMemRefs(MemRefs, MemRefs+NR);
810 
811   MachineInstr *NewI = MB;
812   NewI->clearKillInfo();
813   LIS->InsertMachineInstrInMaps(*NewI);
814 
815   for (auto &Op : NewI->operands())
816     if (Op.isReg())
817       UpdRegs.insert(Op.getReg());
818 }
819 
820 
821 /// In the range [First, Last], rename all references to the "old" register RO
822 /// to the "new" register RN, but only in instructions predicated on the given
823 /// condition.
824 void HexagonExpandCondsets::renameInRange(RegisterRef RO, RegisterRef RN,
825       unsigned PredR, bool Cond, MachineBasicBlock::iterator First,
826       MachineBasicBlock::iterator Last) {
827   MachineBasicBlock::iterator End = std::next(Last);
828   for (MachineBasicBlock::iterator I = First; I != End; ++I) {
829     MachineInstr *MI = &*I;
830     // Do not touch instructions that are not predicated, or are predicated
831     // on the opposite condition.
832     if (!HII->isPredicated(*MI))
833       continue;
834     if (!MI->readsRegister(PredR) || (Cond != HII->isPredicatedTrue(*MI)))
835       continue;
836 
837     for (auto &Op : MI->operands()) {
838       if (!Op.isReg() || RO != RegisterRef(Op))
839         continue;
840       Op.setReg(RN.Reg);
841       Op.setSubReg(RN.Sub);
842       // In practice, this isn't supposed to see any defs.
843       assert(!Op.isDef() && "Not expecting a def");
844     }
845   }
846 }
847 
848 
849 /// For a given conditional copy, predicate the definition of the source of
850 /// the copy under the given condition (using the same predicate register as
851 /// the copy).
852 bool HexagonExpandCondsets::predicate(MachineInstr &TfrI, bool Cond,
853                                       std::set<unsigned> &UpdRegs) {
854   // TfrI - A2_tfr[tf] Instruction (not A2_tfrsi).
855   unsigned Opc = TfrI.getOpcode();
856   (void)Opc;
857   assert(Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf);
858   DEBUG(dbgs() << "\nattempt to predicate if-" << (Cond ? "true" : "false")
859                << ": " << TfrI);
860 
861   MachineOperand &MD = TfrI.getOperand(0);
862   MachineOperand &MP = TfrI.getOperand(1);
863   MachineOperand &MS = TfrI.getOperand(2);
864   // The source operand should be a <kill>. This is not strictly necessary,
865   // but it makes things a lot simpler. Otherwise, we would need to rename
866   // some registers, which would complicate the transformation considerably.
867   if (!MS.isKill())
868     return false;
869   // Avoid predicating instructions that define a subregister if subregister
870   // liveness tracking is not enabled.
871   if (MD.getSubReg() && !MRI->shouldTrackSubRegLiveness(MD.getReg()))
872     return false;
873 
874   RegisterRef RT(MS);
875   unsigned PredR = MP.getReg();
876   MachineInstr *DefI = getReachingDefForPred(RT, TfrI, PredR, Cond);
877   if (!DefI || !isPredicable(DefI))
878     return false;
879 
880   DEBUG(dbgs() << "Source def: " << *DefI);
881 
882   // Collect the information about registers defined and used between the
883   // DefI and the TfrI.
884   // Map: reg -> bitmask of subregs
885   ReferenceMap Uses, Defs;
886   MachineBasicBlock::iterator DefIt = DefI, TfrIt = TfrI;
887 
888   // Check if the predicate register is valid between DefI and TfrI.
889   // If it is, we can then ignore instructions predicated on the negated
890   // conditions when collecting def and use information.
891   bool PredValid = true;
892   for (MachineBasicBlock::iterator I = std::next(DefIt); I != TfrIt; ++I) {
893     if (!I->modifiesRegister(PredR, 0))
894       continue;
895     PredValid = false;
896     break;
897   }
898 
899   for (MachineBasicBlock::iterator I = std::next(DefIt); I != TfrIt; ++I) {
900     MachineInstr *MI = &*I;
901     // If this instruction is predicated on the same register, it could
902     // potentially be ignored.
903     // By default assume that the instruction executes on the same condition
904     // as TfrI (Exec_Then), and also on the opposite one (Exec_Else).
905     unsigned Exec = Exec_Then | Exec_Else;
906     if (PredValid && HII->isPredicated(*MI) && MI->readsRegister(PredR))
907       Exec = (Cond == HII->isPredicatedTrue(*MI)) ? Exec_Then : Exec_Else;
908 
909     for (auto &Op : MI->operands()) {
910       if (!Op.isReg())
911         continue;
912       // We don't want to deal with physical registers. The reason is that
913       // they can be aliased with other physical registers. Aliased virtual
914       // registers must share the same register number, and can only differ
915       // in the subregisters, which we are keeping track of. Physical
916       // registers ters no longer have subregisters---their super- and
917       // subregisters are other physical registers, and we are not checking
918       // that.
919       RegisterRef RR = Op;
920       if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
921         return false;
922 
923       ReferenceMap &Map = Op.isDef() ? Defs : Uses;
924       addRefToMap(RR, Map, Exec);
925     }
926   }
927 
928   // The situation:
929   //   RT = DefI
930   //   ...
931   //   RD = TfrI ..., RT
932 
933   // If the register-in-the-middle (RT) is used or redefined between
934   // DefI and TfrI, we may not be able proceed with this transformation.
935   // We can ignore a def that will not execute together with TfrI, and a
936   // use that will. If there is such a use (that does execute together with
937   // TfrI), we will not be able to move DefI down. If there is a use that
938   // executed if TfrI's condition is false, then RT must be available
939   // unconditionally (cannot be predicated).
940   // Essentially, we need to be able to rename RT to RD in this segment.
941   if (isRefInMap(RT, Defs, Exec_Then) || isRefInMap(RT, Uses, Exec_Else))
942     return false;
943   RegisterRef RD = MD;
944   // If the predicate register is defined between DefI and TfrI, the only
945   // potential thing to do would be to move the DefI down to TfrI, and then
946   // predicate. The reaching def (DefI) must be movable down to the location
947   // of the TfrI.
948   // If the target register of the TfrI (RD) is not used or defined between
949   // DefI and TfrI, consider moving TfrI up to DefI.
950   bool CanUp =   canMoveOver(TfrI, Defs, Uses);
951   bool CanDown = canMoveOver(*DefI, Defs, Uses);
952   // The TfrI does not access memory, but DefI could. Check if it's safe
953   // to move DefI down to TfrI.
954   if (DefI->mayLoad() || DefI->mayStore())
955     if (!canMoveMemTo(*DefI, TfrI, true))
956       CanDown = false;
957 
958   DEBUG(dbgs() << "Can move up: " << (CanUp ? "yes" : "no")
959                << ", can move down: " << (CanDown ? "yes\n" : "no\n"));
960   MachineBasicBlock::iterator PastDefIt = std::next(DefIt);
961   if (CanUp)
962     predicateAt(MD, *DefI, PastDefIt, MP, Cond, UpdRegs);
963   else if (CanDown)
964     predicateAt(MD, *DefI, TfrIt, MP, Cond, UpdRegs);
965   else
966     return false;
967 
968   if (RT != RD) {
969     renameInRange(RT, RD, PredR, Cond, PastDefIt, TfrIt);
970     UpdRegs.insert(RT.Reg);
971   }
972 
973   removeInstr(TfrI);
974   removeInstr(*DefI);
975   return true;
976 }
977 
978 
979 /// Predicate all cases of conditional copies in the specified block.
980 bool HexagonExpandCondsets::predicateInBlock(MachineBasicBlock &B,
981       std::set<unsigned> &UpdRegs) {
982   bool Changed = false;
983   MachineBasicBlock::iterator I, E, NextI;
984   for (I = B.begin(), E = B.end(); I != E; I = NextI) {
985     NextI = std::next(I);
986     unsigned Opc = I->getOpcode();
987     if (Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf) {
988       bool Done = predicate(*I, (Opc == Hexagon::A2_tfrt), UpdRegs);
989       if (!Done) {
990         // If we didn't predicate I, we may need to remove it in case it is
991         // an "identity" copy, e.g.  vreg1 = A2_tfrt vreg2, vreg1.
992         if (RegisterRef(I->getOperand(0)) == RegisterRef(I->getOperand(2))) {
993           for (auto &Op : I->operands())
994             if (Op.isReg())
995               UpdRegs.insert(Op.getReg());
996           removeInstr(*I);
997         }
998       }
999       Changed |= Done;
1000     }
1001   }
1002   return Changed;
1003 }
1004 
1005 
1006 bool HexagonExpandCondsets::isIntReg(RegisterRef RR, unsigned &BW) {
1007   if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
1008     return false;
1009   const TargetRegisterClass *RC = MRI->getRegClass(RR.Reg);
1010   if (RC == &Hexagon::IntRegsRegClass) {
1011     BW = 32;
1012     return true;
1013   }
1014   if (RC == &Hexagon::DoubleRegsRegClass) {
1015     BW = (RR.Sub != 0) ? 32 : 64;
1016     return true;
1017   }
1018   return false;
1019 }
1020 
1021 
1022 bool HexagonExpandCondsets::isIntraBlocks(LiveInterval &LI) {
1023   for (LiveInterval::iterator I = LI.begin(), E = LI.end(); I != E; ++I) {
1024     LiveRange::Segment &LR = *I;
1025     // Range must start at a register...
1026     if (!LR.start.isRegister())
1027       return false;
1028     // ...and end in a register or in a dead slot.
1029     if (!LR.end.isRegister() && !LR.end.isDead())
1030       return false;
1031   }
1032   return true;
1033 }
1034 
1035 
1036 bool HexagonExpandCondsets::coalesceRegisters(RegisterRef R1, RegisterRef R2) {
1037   if (CoaLimitActive) {
1038     if (CoaCounter >= CoaLimit)
1039       return false;
1040     CoaCounter++;
1041   }
1042   unsigned BW1, BW2;
1043   if (!isIntReg(R1, BW1) || !isIntReg(R2, BW2) || BW1 != BW2)
1044     return false;
1045   if (MRI->isLiveIn(R1.Reg))
1046     return false;
1047   if (MRI->isLiveIn(R2.Reg))
1048     return false;
1049 
1050   LiveInterval &L1 = LIS->getInterval(R1.Reg);
1051   LiveInterval &L2 = LIS->getInterval(R2.Reg);
1052   if (L2.empty())
1053     return false;
1054   bool Overlap = L1.overlaps(L2);
1055 
1056   DEBUG(dbgs() << "compatible registers: ("
1057                << (Overlap ? "overlap" : "disjoint") << ")\n  "
1058                << PrintReg(R1.Reg, TRI, R1.Sub) << "  " << L1 << "\n  "
1059                << PrintReg(R2.Reg, TRI, R2.Sub) << "  " << L2 << "\n");
1060   if (R1.Sub || R2.Sub)
1061     return false;
1062   if (Overlap)
1063     return false;
1064 
1065   // Coalescing could have a negative impact on scheduling, so try to limit
1066   // to some reasonable extent. Only consider coalescing segments, when one
1067   // of them does not cross basic block boundaries.
1068   if (!isIntraBlocks(L1) && !isIntraBlocks(L2))
1069     return false;
1070 
1071   MRI->replaceRegWith(R2.Reg, R1.Reg);
1072 
1073   // Move all live segments from L2 to L1.
1074   typedef DenseMap<VNInfo*,VNInfo*> ValueInfoMap;
1075   ValueInfoMap VM;
1076   for (LiveInterval::iterator I = L2.begin(), E = L2.end(); I != E; ++I) {
1077     VNInfo *NewVN, *OldVN = I->valno;
1078     ValueInfoMap::iterator F = VM.find(OldVN);
1079     if (F == VM.end()) {
1080       NewVN = L1.getNextValue(I->valno->def, LIS->getVNInfoAllocator());
1081       VM.insert(std::make_pair(OldVN, NewVN));
1082     } else {
1083       NewVN = F->second;
1084     }
1085     L1.addSegment(LiveRange::Segment(I->start, I->end, NewVN));
1086   }
1087   while (L2.begin() != L2.end())
1088     L2.removeSegment(*L2.begin());
1089 
1090   updateKillFlags(R1.Reg);
1091   DEBUG(dbgs() << "coalesced: " << L1 << "\n");
1092   L1.verify();
1093 
1094   return true;
1095 }
1096 
1097 
1098 /// Attempt to coalesce one of the source registers to a MUX intruction with
1099 /// the destination register. This could lead to having only one predicated
1100 /// instruction in the end instead of two.
1101 bool HexagonExpandCondsets::coalesceSegments(MachineFunction &MF) {
1102   SmallVector<MachineInstr*,16> Condsets;
1103   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
1104     MachineBasicBlock &B = *I;
1105     for (MachineBasicBlock::iterator J = B.begin(), F = B.end(); J != F; ++J) {
1106       MachineInstr *MI = &*J;
1107       if (!isCondset(*MI))
1108         continue;
1109       MachineOperand &S1 = MI->getOperand(2), &S2 = MI->getOperand(3);
1110       if (!S1.isReg() && !S2.isReg())
1111         continue;
1112       Condsets.push_back(MI);
1113     }
1114   }
1115 
1116   bool Changed = false;
1117   for (unsigned i = 0, n = Condsets.size(); i < n; ++i) {
1118     MachineInstr *CI = Condsets[i];
1119     RegisterRef RD = CI->getOperand(0);
1120     RegisterRef RP = CI->getOperand(1);
1121     MachineOperand &S1 = CI->getOperand(2), &S2 = CI->getOperand(3);
1122     bool Done = false;
1123     // Consider this case:
1124     //   vreg1 = instr1 ...
1125     //   vreg2 = instr2 ...
1126     //   vreg0 = C2_mux ..., vreg1, vreg2
1127     // If vreg0 was coalesced with vreg1, we could end up with the following
1128     // code:
1129     //   vreg0 = instr1 ...
1130     //   vreg2 = instr2 ...
1131     //   vreg0 = A2_tfrf ..., vreg2
1132     // which will later become:
1133     //   vreg0 = instr1 ...
1134     //   vreg0 = instr2_cNotPt ...
1135     // i.e. there will be an unconditional definition (instr1) of vreg0
1136     // followed by a conditional one. The output dependency was there before
1137     // and it unavoidable, but if instr1 is predicable, we will no longer be
1138     // able to predicate it here.
1139     // To avoid this scenario, don't coalesce the destination register with
1140     // a source register that is defined by a predicable instruction.
1141     if (S1.isReg()) {
1142       RegisterRef RS = S1;
1143       MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, true);
1144       if (!RDef || !HII->isPredicable(*RDef))
1145         Done = coalesceRegisters(RD, RegisterRef(S1));
1146     }
1147     if (!Done && S2.isReg()) {
1148       RegisterRef RS = S2;
1149       MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, false);
1150       if (!RDef || !HII->isPredicable(*RDef))
1151         Done = coalesceRegisters(RD, RegisterRef(S2));
1152     }
1153     Changed |= Done;
1154   }
1155   return Changed;
1156 }
1157 
1158 
1159 bool HexagonExpandCondsets::runOnMachineFunction(MachineFunction &MF) {
1160   if (skipFunction(*MF.getFunction()))
1161     return false;
1162 
1163   HII = static_cast<const HexagonInstrInfo*>(MF.getSubtarget().getInstrInfo());
1164   TRI = MF.getSubtarget().getRegisterInfo();
1165   MDT = &getAnalysis<MachineDominatorTree>();
1166   LIS = &getAnalysis<LiveIntervals>();
1167   MRI = &MF.getRegInfo();
1168 
1169   DEBUG(LIS->print(dbgs() << "Before expand-condsets\n",
1170                    MF.getFunction()->getParent()));
1171 
1172   bool Changed = false;
1173   std::set<unsigned> SplitUpd, PredUpd;
1174 
1175   // Try to coalesce the target of a mux with one of its sources.
1176   // This could eliminate a register copy in some circumstances.
1177   Changed |= coalesceSegments(MF);
1178 
1179   // First, simply split all muxes into a pair of conditional transfers
1180   // and update the live intervals to reflect the new arrangement. The
1181   // goal is to update the kill flags, since predication will rely on
1182   // them.
1183   for (auto &B : MF)
1184     Changed |= splitInBlock(B, SplitUpd);
1185   updateLiveness(SplitUpd, true, true, false);
1186 
1187   // Traverse all blocks and collapse predicable instructions feeding
1188   // conditional transfers into predicated instructions.
1189   // Walk over all the instructions again, so we may catch pre-existing
1190   // cases that were not created in the previous step.
1191   for (auto &B : MF)
1192     Changed |= predicateInBlock(B, PredUpd);
1193 
1194   updateLiveness(PredUpd, true, true, true);
1195   // Remove from SplitUpd all registers contained in PredUpd to avoid
1196   // unnecessary liveness recalculation.
1197   std::set<unsigned> Diff;
1198   std::set_difference(SplitUpd.begin(), SplitUpd.end(),
1199                       PredUpd.begin(), PredUpd.end(),
1200                       std::inserter(Diff, Diff.begin()));
1201   updateLiveness(Diff, false, false, true);
1202 
1203   DEBUG({
1204     if (Changed)
1205       LIS->print(dbgs() << "After expand-condsets\n",
1206                  MF.getFunction()->getParent());
1207   });
1208 
1209   return Changed;
1210 }
1211 
1212 
1213 //===----------------------------------------------------------------------===//
1214 //                         Public Constructor Functions
1215 //===----------------------------------------------------------------------===//
1216 
1217 FunctionPass *llvm::createHexagonExpandCondsets() {
1218   return new HexagonExpandCondsets();
1219 }
1220