1 //===--- HexagonConstPropagation.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 #define DEBUG_TYPE "hcp"
11 
12 #include "HexagonInstrInfo.h"
13 #include "HexagonRegisterInfo.h"
14 #include "HexagonSubtarget.h"
15 
16 #include "llvm/ADT/PostOrderIterator.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 
28 #include <map>
29 #include <queue>
30 #include <set>
31 
32 using namespace llvm;
33 
34 namespace {
35   class LatticeCell;
36 
37   // Properties of a value that are tracked by the propagation.
38   // A property that is marked as present (i.e. bit is set) dentes that the
39   // value is known (proven) to have this property. Not all combinations
40   // of bits make sense, for example Zero and NonZero are mutually exclusive,
41   // but on the other hand, Zero implies Finite. In this case, whenever
42   // the Zero property is present, Finite should also be present.
43   class ConstantProperties {
44   public:
45     enum {
46       Unknown   = 0x0000,
47       Zero      = 0x0001,
48       NonZero   = 0x0002,
49       Finite    = 0x0004,
50       Infinity  = 0x0008,
51       NaN       = 0x0010,
52       SignedZero = 0x0020,
53       NumericProperties = (Zero|NonZero|Finite|Infinity|NaN|SignedZero),
54       PosOrZero       = 0x0100,
55       NegOrZero       = 0x0200,
56       SignProperties  = (PosOrZero|NegOrZero),
57       Everything      = (NumericProperties|SignProperties)
58     };
59 
60     // For a given constant, deduce the set of trackable properties that this
61     // constant has.
62     static uint32_t deduce(const Constant *C);
63   };
64 
65 
66   // A representation of a register as it can appear in a MachineOperand,
67   // i.e. a pair register:subregister.
68   struct Register {
69     unsigned Reg, SubReg;
70     explicit Register(unsigned R, unsigned SR = 0) : Reg(R), SubReg(SR) {}
71     explicit Register(const MachineOperand &MO)
72       : Reg(MO.getReg()), SubReg(MO.getSubReg()) {}
73     void print(const TargetRegisterInfo *TRI = 0) const {
74       dbgs() << PrintReg(Reg, TRI, SubReg);
75     }
76     bool operator== (const Register &R) const {
77       return (Reg == R.Reg) && (SubReg == R.SubReg);
78     }
79   };
80 
81 
82   // Lattice cell, based on that was described in the W-Z paper on constant
83   // propagation.
84   // Latice cell will be allowed to hold multiple constant values. While
85   // multiple values would normally indicate "bottom", we can still derive
86   // some useful information from them. For example, comparison X > 0
87   // could be folded if all the values in the cell associated with X are
88   // positive.
89   class LatticeCell {
90   private:
91     enum { Normal, Top, Bottom };
92     static const unsigned MaxCellSize = 4;
93     unsigned Kind:2;
94     unsigned Size:3;
95     unsigned IsSpecial:1;
96     unsigned :0;
97 
98   public:
99     union {
100       uint32_t Properties;
101       const Constant *Value;
102       const Constant *Values[MaxCellSize];
103     };
104 
105     LatticeCell() : Kind(Top), Size(0), IsSpecial(false) {
106       for (unsigned i = 0; i < MaxCellSize; ++i)
107         Values[i] = 0;
108     }
109 
110     bool meet(const LatticeCell &L);
111     bool add(const Constant *C);
112     bool add(uint32_t Property);
113     uint32_t properties() const;
114     unsigned size() const { return Size; }
115 
116     LatticeCell &operator= (const LatticeCell &L) {
117       if (this != &L) {
118         // This memcpy also copies Properties (when L.Size == 0).
119         uint32_t N = L.IsSpecial ? sizeof L.Properties
120                                  : L.Size*sizeof(const Constant*);
121         memcpy(Values, L.Values, N);
122         Kind = L.Kind;
123         Size = L.Size;
124         IsSpecial = L.IsSpecial;
125       }
126       return *this;
127     }
128 
129     bool isSingle() const { return size() == 1; }
130     bool isProperty() const { return IsSpecial; }
131     bool isTop() const { return Kind == Top; }
132     bool isBottom() const { return Kind == Bottom; }
133     bool setBottom() {
134       bool Changed = (Kind != Bottom);
135       Kind = Bottom;
136       Size = 0;
137       IsSpecial = false;
138       return Changed;
139     }
140     void print(raw_ostream &os) const;
141 
142   private:
143     void setProperty() {
144       IsSpecial = true;
145       Size = 0;
146       Kind = Normal;
147     }
148     bool convertToProperty();
149   };
150 
151   raw_ostream &operator<< (raw_ostream &os, const LatticeCell &L) {
152     L.print(os);
153     return os;
154   }
155 
156   class MachineConstEvaluator;
157 
158   class MachineConstPropagator {
159   public:
160     MachineConstPropagator(MachineConstEvaluator &E) : MCE(E) {
161       Bottom.setBottom();
162     }
163 
164     // Mapping: vreg -> cell
165     // The keys are registers _without_ subregisters. This won't allow
166     // definitions in the form of "vreg:subreg<def> = ...". Such definitions
167     // would be questionable from the point of view of SSA, since the "vreg"
168     // could not be initialized in its entirety (specifically, an instruction
169     // defining the "other part" of "vreg" would also count as a definition
170     // of "vreg", which would violate the SSA).
171     // If a value of a pair vreg:subreg needs to be obtained, the cell for
172     // "vreg" needs to be looked up, and then the value of subregister "subreg"
173     // needs to be evaluated.
174     class CellMap {
175     public:
176       CellMap() {
177         assert(Top.isTop());
178         Bottom.setBottom();
179       }
180       void clear() { Map.clear(); }
181       bool has(unsigned R) const {
182         // All non-virtual registers are considered "bottom".
183         if (!TargetRegisterInfo::isVirtualRegister(R))
184           return true;
185         MapType::const_iterator F = Map.find(R);
186         return F != Map.end();
187       }
188       const LatticeCell &get(unsigned R) const {
189         if (!TargetRegisterInfo::isVirtualRegister(R))
190           return Bottom;
191         MapType::const_iterator F = Map.find(R);
192         if (F != Map.end())
193           return F->second;
194         return Top;
195       }
196       // Invalidates any const references.
197       void update(unsigned R, const LatticeCell &L) {
198         Map[R] = L;
199       }
200       void print(raw_ostream &os, const TargetRegisterInfo &TRI) const;
201     private:
202       typedef std::map<unsigned,LatticeCell> MapType;
203       MapType Map;
204       // To avoid creating "top" entries, return a const reference to
205       // this cell in "get". Also, have a "Bottom" cell to return from
206       // get when a value of a physical register is requested.
207       LatticeCell Top, Bottom;
208     public:
209       typedef MapType::const_iterator const_iterator;
210       const_iterator begin() const { return Map.begin(); }
211       const_iterator end() const { return Map.end(); }
212     };
213 
214     bool run(MachineFunction &MF);
215 
216   private:
217     void visitPHI(const MachineInstr &PN);
218     void visitNonBranch(const MachineInstr &MI);
219     void visitBranchesFrom(const MachineInstr &BrI);
220     void visitUsesOf(unsigned R);
221     bool computeBlockSuccessors(const MachineBasicBlock *MB,
222           SetVector<const MachineBasicBlock*> &Targets);
223     void removeCFGEdge(MachineBasicBlock *From, MachineBasicBlock *To);
224 
225     void propagate(MachineFunction &MF);
226     bool rewrite(MachineFunction &MF);
227 
228     MachineRegisterInfo      *MRI;
229     MachineConstEvaluator    &MCE;
230 
231     typedef std::pair<unsigned,unsigned> CFGEdge;
232     typedef std::set<CFGEdge> SetOfCFGEdge;
233     typedef std::set<const MachineInstr*> SetOfInstr;
234     typedef std::queue<CFGEdge> QueueOfCFGEdge;
235 
236     LatticeCell     Bottom;
237     CellMap         Cells;
238     SetOfCFGEdge    EdgeExec;
239     SetOfInstr      InstrExec;
240     QueueOfCFGEdge  FlowQ;
241   };
242 
243 
244   // The "evaluator/rewriter" of machine instructions. This is an abstract
245   // base class that provides the interface that the propagator will use,
246   // as well as some helper functions that are target-independent.
247   class MachineConstEvaluator {
248   public:
249     MachineConstEvaluator(MachineFunction &Fn)
250       : TRI(*Fn.getSubtarget().getRegisterInfo()),
251         MF(Fn), CX(Fn.getFunction()->getContext()) {}
252     virtual ~MachineConstEvaluator() {}
253 
254     // The required interface:
255     // - A set of three "evaluate" functions. Each returns "true" if the
256     //       computation succeeded, "false" otherwise.
257     //   (1) Given an instruction MI, and the map with input values "Inputs",
258     //       compute the set of output values "Outputs". An example of when
259     //       the computation can "fail" is if MI is not an instruction that
260     //       is recognized by the evaluator.
261     //   (2) Given a register R (as reg:subreg), compute the cell that
262     //       corresponds to the "subreg" part of the given register.
263     //   (3) Given a branch instruction BrI, compute the set of target blocks.
264     //       If the branch can fall-through, add null (0) to the list of
265     //       possible targets.
266     // - A function "rewrite", that given the cell map after propagation,
267     //   could rewrite instruction MI in a more beneficial form. Return
268     //   "true" if a change has been made, "false" otherwise.
269     typedef MachineConstPropagator::CellMap CellMap;
270     virtual bool evaluate(const MachineInstr &MI, const CellMap &Inputs,
271                           CellMap &Outputs) = 0;
272     virtual bool evaluate(const Register &R, const LatticeCell &SrcC,
273                           LatticeCell &Result) = 0;
274     virtual bool evaluate(const MachineInstr &BrI, const CellMap &Inputs,
275                           SetVector<const MachineBasicBlock*> &Targets,
276                           bool &CanFallThru) = 0;
277     virtual bool rewrite(MachineInstr &MI, const CellMap &Inputs) = 0;
278 
279     const TargetRegisterInfo &TRI;
280 
281   protected:
282     MachineFunction &MF;
283     LLVMContext     &CX;
284 
285     struct Comparison {
286       enum {
287         Unk = 0x00,
288         EQ  = 0x01,
289         NE  = 0x02,
290         L   = 0x04, // Less-than property.
291         G   = 0x08, // Greater-than property.
292         U   = 0x40, // Unsigned property.
293         LTs = L,
294         LEs = L | EQ,
295         GTs = G,
296         GEs = G | EQ,
297         LTu = L      | U,
298         LEu = L | EQ | U,
299         GTu = G      | U,
300         GEu = G | EQ | U
301       };
302       static uint32_t negate(uint32_t Cmp) {
303         if (Cmp == EQ)
304           return NE;
305         if (Cmp == NE)
306           return EQ;
307         assert((Cmp & (L|G)) != (L|G));
308         return Cmp ^ (L|G);
309       }
310     };
311 
312     // Helper functions.
313 
314     bool getCell(const Register &R, const CellMap &Inputs, LatticeCell &RC);
315     bool constToInt(const Constant *C, APInt &Val) const;
316     bool constToFloat(const Constant *C, APFloat &Val) const;
317     const ConstantInt *intToConst(const APInt &Val) const;
318 
319     // Compares.
320     bool evaluateCMPrr(uint32_t Cmp, const Register &R1, const Register &R2,
321           const CellMap &Inputs, bool &Result);
322     bool evaluateCMPri(uint32_t Cmp, const Register &R1, const APInt &A2,
323           const CellMap &Inputs, bool &Result);
324     bool evaluateCMPrp(uint32_t Cmp, const Register &R1, uint64_t Props2,
325           const CellMap &Inputs, bool &Result);
326     bool evaluateCMPii(uint32_t Cmp, const APInt &A1, const APInt &A2,
327           bool &Result);
328     bool evaluateCMPpi(uint32_t Cmp, uint32_t Props, const APInt &A2,
329           bool &Result);
330     bool evaluateCMPpp(uint32_t Cmp, uint32_t Props1, uint32_t Props2,
331           bool &Result);
332 
333     bool evaluateCOPY(const Register &R1, const CellMap &Inputs,
334           LatticeCell &Result);
335 
336     // Logical operations.
337     bool evaluateANDrr(const Register &R1, const Register &R2,
338           const CellMap &Inputs, LatticeCell &Result);
339     bool evaluateANDri(const Register &R1, const APInt &A2,
340           const CellMap &Inputs, LatticeCell &Result);
341     bool evaluateANDii(const APInt &A1, const APInt &A2, APInt &Result);
342     bool evaluateORrr(const Register &R1, const Register &R2,
343           const CellMap &Inputs, LatticeCell &Result);
344     bool evaluateORri(const Register &R1, const APInt &A2,
345           const CellMap &Inputs, LatticeCell &Result);
346     bool evaluateORii(const APInt &A1, const APInt &A2, APInt &Result);
347     bool evaluateXORrr(const Register &R1, const Register &R2,
348           const CellMap &Inputs, LatticeCell &Result);
349     bool evaluateXORri(const Register &R1, const APInt &A2,
350           const CellMap &Inputs, LatticeCell &Result);
351     bool evaluateXORii(const APInt &A1, const APInt &A2, APInt &Result);
352 
353     // Extensions.
354     bool evaluateZEXTr(const Register &R1, unsigned Width, unsigned Bits,
355           const CellMap &Inputs, LatticeCell &Result);
356     bool evaluateZEXTi(const APInt &A1, unsigned Width, unsigned Bits,
357           APInt &Result);
358     bool evaluateSEXTr(const Register &R1, unsigned Width, unsigned Bits,
359           const CellMap &Inputs, LatticeCell &Result);
360     bool evaluateSEXTi(const APInt &A1, unsigned Width, unsigned Bits,
361           APInt &Result);
362 
363     // Leading/trailing bits.
364     bool evaluateCLBr(const Register &R1, bool Zeros, bool Ones,
365           const CellMap &Inputs, LatticeCell &Result);
366     bool evaluateCLBi(const APInt &A1, bool Zeros, bool Ones, APInt &Result);
367     bool evaluateCTBr(const Register &R1, bool Zeros, bool Ones,
368           const CellMap &Inputs, LatticeCell &Result);
369     bool evaluateCTBi(const APInt &A1, bool Zeros, bool Ones, APInt &Result);
370 
371     // Bitfield extract.
372     bool evaluateEXTRACTr(const Register &R1, unsigned Width, unsigned Bits,
373           unsigned Offset, bool Signed, const CellMap &Inputs,
374           LatticeCell &Result);
375     bool evaluateEXTRACTi(const APInt &A1, unsigned Bits, unsigned Offset,
376           bool Signed, APInt &Result);
377     // Vector operations.
378     bool evaluateSplatr(const Register &R1, unsigned Bits, unsigned Count,
379           const CellMap &Inputs, LatticeCell &Result);
380     bool evaluateSplati(const APInt &A1, unsigned Bits, unsigned Count,
381           APInt &Result);
382   };
383 
384 }
385 
386 uint32_t ConstantProperties::deduce(const Constant *C) {
387   if (isa<ConstantInt>(C)) {
388     const ConstantInt *CI = cast<ConstantInt>(C);
389     if (CI->isZero())
390       return Zero | PosOrZero | NegOrZero | Finite;
391     uint32_t Props = (NonZero | Finite);
392     if (CI->isNegative())
393       return Props | NegOrZero;
394     return Props | PosOrZero;
395   }
396 
397   if (isa<ConstantFP>(C)) {
398     const ConstantFP *CF = cast<ConstantFP>(C);
399     uint32_t Props = CF->isNegative() ? (NegOrZero|NonZero)
400                                       : PosOrZero;
401     if (CF->isZero())
402       return (Props & ~NumericProperties) | (Zero|Finite);
403     Props = (Props & ~NumericProperties) | NonZero;
404     if (CF->isNaN())
405       return (Props & ~NumericProperties) | NaN;
406     const APFloat &Val = CF->getValueAPF();
407     if (Val.isInfinity())
408       return (Props & ~NumericProperties) | Infinity;
409     Props |= Finite;
410     return Props;
411   }
412 
413   return Unknown;
414 }
415 
416 
417 // Convert a cell from a set of specific values to a cell that tracks
418 // properties.
419 bool LatticeCell::convertToProperty() {
420   if (isProperty())
421     return false;
422   // Corner case: converting a fresh (top) cell to "special".
423   // This can happen, when adding a property to a top cell.
424   uint32_t Everything = ConstantProperties::Everything;
425   uint32_t Ps = !isTop() ? properties()
426                          : Everything;
427   if (Ps != ConstantProperties::Unknown) {
428     Properties = Ps;
429     setProperty();
430   } else {
431     setBottom();
432   }
433   return true;
434 }
435 
436 
437 void LatticeCell::print(raw_ostream &os) const {
438   if (isProperty()) {
439     os << "{ ";
440     uint32_t Ps = properties();
441     if (Ps & ConstantProperties::Zero)
442       os << "zero ";
443     if (Ps & ConstantProperties::NonZero)
444       os << "nonzero ";
445     if (Ps & ConstantProperties::Finite)
446       os << "finite ";
447     if (Ps & ConstantProperties::Infinity)
448       os << "infinity ";
449     if (Ps & ConstantProperties::NaN)
450       os << "nan ";
451     if (Ps & ConstantProperties::PosOrZero)
452       os << "poz ";
453     if (Ps & ConstantProperties::NegOrZero)
454       os << "nez ";
455     os << '}';
456     return;
457   }
458 
459   os << "{ ";
460   if (isBottom()) {
461     os << "bottom";
462   } else if (isTop()) {
463     os << "top";
464   } else {
465     for (unsigned i = 0; i < size(); ++i) {
466       const Constant *C = Values[i];
467       if (i != 0)
468         os << ", ";
469       C->print(os);
470     }
471   }
472   os << " }";
473 }
474 
475 
476 // "Meet" operation on two cells. This is the key of the propagation
477 // algorithm.
478 bool LatticeCell::meet(const LatticeCell &L) {
479   bool Changed = false;
480   if (L.isBottom())
481     Changed = setBottom();
482   if (isBottom() || L.isTop())
483     return Changed;
484   if (isTop()) {
485     *this = L;
486     // L can be neither Top nor Bottom, so *this must have changed.
487     return true;
488   }
489 
490   // Top/bottom cases covered. Need to integrate L's set into ours.
491   if (L.isProperty())
492     return add(L.properties());
493   for (unsigned i = 0; i < L.size(); ++i) {
494     const Constant *LC = L.Values[i];
495     Changed |= add(LC);
496   }
497   return Changed;
498 }
499 
500 
501 // Add a new constant to the cell. This is actually where the cell update
502 // happens. If a cell has room for more constants, the new constant is added.
503 // Otherwise, the cell is converted to a "property" cell (i.e. a cell that
504 // will track properties of the associated values, and not the values
505 // themselves. Care is taken to handle special cases, like "bottom", etc.
506 bool LatticeCell::add(const Constant *LC) {
507   assert(LC);
508   if (isBottom())
509     return false;
510 
511   if (!isProperty()) {
512     // Cell is not special. Try to add the constant here first,
513     // if there is room.
514     unsigned Index = 0;
515     while (Index < Size) {
516       const Constant *C = Values[Index];
517       // If the constant is already here, no change is needed.
518       if (C == LC)
519         return false;
520       Index++;
521     }
522     if (Index < MaxCellSize) {
523       Values[Index] = LC;
524       Kind = Normal;
525       Size++;
526       return true;
527     }
528   }
529 
530   bool Changed = false;
531 
532   // This cell is special, or is not special, but is full. After this
533   // it will be special.
534   Changed = convertToProperty();
535   uint32_t Ps = properties();
536   uint32_t NewPs = Ps & ConstantProperties::deduce(LC);
537   if (NewPs == ConstantProperties::Unknown) {
538     setBottom();
539     return true;
540   }
541   if (Ps != NewPs) {
542     Properties = NewPs;
543     Changed = true;
544   }
545   return Changed;
546 }
547 
548 
549 // Add a property to the cell. This will force the cell to become a property-
550 // tracking cell.
551 bool LatticeCell::add(uint32_t Property) {
552   bool Changed = convertToProperty();
553   uint32_t Ps = properties();
554   if (Ps == (Ps & Property))
555     return Changed;
556   Properties = Property & Ps;
557   return true;
558 }
559 
560 
561 // Return the properties of the values in the cell. This is valid for any
562 // cell, and does not alter the cell itself.
563 uint32_t LatticeCell::properties() const {
564   if (isProperty())
565     return Properties;
566   assert(!isTop() && "Should not call this for a top cell");
567   if (isBottom())
568     return ConstantProperties::Unknown;
569 
570   assert(size() > 0 && "Empty cell");
571   uint32_t Ps = ConstantProperties::deduce(Values[0]);
572   for (unsigned i = 1; i < size(); ++i) {
573     if (Ps == ConstantProperties::Unknown)
574       break;
575     Ps &= ConstantProperties::deduce(Values[i]);
576   }
577   return Ps;
578 }
579 
580 
581 void MachineConstPropagator::CellMap::print(raw_ostream &os,
582       const TargetRegisterInfo &TRI) const {
583   for (auto &I : Map)
584     dbgs() << "  " << PrintReg(I.first, &TRI) << " -> " << I.second << '\n';
585 }
586 
587 
588 void MachineConstPropagator::visitPHI(const MachineInstr &PN) {
589   const MachineBasicBlock *MB = PN.getParent();
590   unsigned MBN = MB->getNumber();
591   DEBUG(dbgs() << "Visiting FI(BB#" << MBN << "): " << PN);
592 
593   const MachineOperand &MD = PN.getOperand(0);
594   Register DefR(MD);
595   assert(TargetRegisterInfo::isVirtualRegister(DefR.Reg));
596 
597   bool Changed = false;
598 
599   // If the def has a sub-register, set the corresponding cell to "bottom".
600   if (DefR.SubReg) {
601 Bottomize:
602     const LatticeCell &T = Cells.get(DefR.Reg);
603     Changed = !T.isBottom();
604     Cells.update(DefR.Reg, Bottom);
605     if (Changed)
606       visitUsesOf(DefR.Reg);
607     return;
608   }
609 
610   LatticeCell DefC = Cells.get(DefR.Reg);
611 
612   for (unsigned i = 1, n = PN.getNumOperands(); i < n; i += 2) {
613     const MachineBasicBlock *PB = PN.getOperand(i+1).getMBB();
614     unsigned PBN = PB->getNumber();
615     if (!EdgeExec.count(CFGEdge(PBN, MBN))) {
616       DEBUG(dbgs() << "  edge BB#" << PBN << "->BB#" << MBN
617                    << " not executable\n");
618       continue;
619     }
620     const MachineOperand &SO = PN.getOperand(i);
621     Register UseR(SO);
622     // If the input is not a virtual register, we don't really know what
623     // value it holds.
624     if (!TargetRegisterInfo::isVirtualRegister(UseR.Reg))
625       goto Bottomize;
626     // If there is no cell for an input register, it means top.
627     if (!Cells.has(UseR.Reg))
628       continue;
629 
630     LatticeCell SrcC;
631     bool Eval = MCE.evaluate(UseR, Cells.get(UseR.Reg), SrcC);
632     DEBUG(dbgs() << "  edge from BB#" << PBN << ": "
633                  << PrintReg(UseR.Reg, &MCE.TRI, UseR.SubReg)
634                  << SrcC << '\n');
635     Changed |= Eval ? DefC.meet(SrcC)
636                     : DefC.setBottom();
637     Cells.update(DefR.Reg, DefC);
638     if (DefC.isBottom())
639       break;
640   }
641   if (Changed)
642     visitUsesOf(DefR.Reg);
643 }
644 
645 
646 void MachineConstPropagator::visitNonBranch(const MachineInstr &MI) {
647   DEBUG(dbgs() << "Visiting MI(BB#" << MI.getParent()->getNumber()
648                << "): " << MI);
649   CellMap Outputs;
650   bool Eval = MCE.evaluate(MI, Cells, Outputs);
651   DEBUG({
652     if (Eval) {
653       dbgs() << "  outputs:";
654       for (auto &I : Outputs)
655         dbgs() << ' ' << I.second;
656       dbgs() << '\n';
657     }
658   });
659 
660   // Update outputs. If the value was not computed, set all the
661   // def cells to bottom.
662   for (const MachineOperand &MO : MI.operands()) {
663     if (!MO.isReg() || !MO.isDef())
664       continue;
665     Register DefR(MO);
666     // Only track virtual registers.
667     if (!TargetRegisterInfo::isVirtualRegister(DefR.Reg))
668       continue;
669     bool Changed = false;
670     // If the evaluation failed, set cells for all output registers to bottom.
671     if (!Eval) {
672       const LatticeCell &T = Cells.get(DefR.Reg);
673       Changed = !T.isBottom();
674       Cells.update(DefR.Reg, Bottom);
675     } else {
676       // Find the corresponding cell in the computed outputs.
677       // If it's not there, go on to the next def.
678       if (!Outputs.has(DefR.Reg))
679         continue;
680       LatticeCell RC = Cells.get(DefR.Reg);
681       Changed = RC.meet(Outputs.get(DefR.Reg));
682       Cells.update(DefR.Reg, RC);
683     }
684     if (Changed)
685       visitUsesOf(DefR.Reg);
686   }
687 }
688 
689 
690 // \brief Starting at a given branch, visit remaining branches in the block.
691 // Traverse over the subsequent branches for as long as the preceding one
692 // can fall through. Add all the possible targets to the flow work queue,
693 // including the potential fall-through to the layout-successor block.
694 void MachineConstPropagator::visitBranchesFrom(const MachineInstr &BrI) {
695   const MachineBasicBlock &B = *BrI.getParent();
696   unsigned MBN = B.getNumber();
697   MachineBasicBlock::const_iterator It = BrI.getIterator();
698   MachineBasicBlock::const_iterator End = B.end();
699 
700   SetVector<const MachineBasicBlock*> Targets;
701   bool EvalOk = true, FallsThru = true;
702   while (It != End) {
703     const MachineInstr &MI = *It;
704     InstrExec.insert(&MI);
705     DEBUG(dbgs() << "Visiting " << (EvalOk ? "BR" : "br") << "(BB#"
706                  << MBN << "): " << MI);
707     // Do not evaluate subsequent branches if the evaluation of any of the
708     // previous branches failed. Keep iterating over the branches only
709     // to mark them as executable.
710     EvalOk = EvalOk && MCE.evaluate(MI, Cells, Targets, FallsThru);
711     if (!EvalOk)
712       FallsThru = true;
713     if (!FallsThru)
714       break;
715     ++It;
716   }
717 
718   if (EvalOk) {
719     // Need to add all CFG successors that lead to EH landing pads.
720     // There won't be explicit branches to these blocks, but they must
721     // be processed.
722     for (const MachineBasicBlock *SB : B.successors()) {
723       if (SB->isEHPad())
724         Targets.insert(SB);
725     }
726     if (FallsThru) {
727       const MachineFunction &MF = *B.getParent();
728       MachineFunction::const_iterator BI = B.getIterator();
729       MachineFunction::const_iterator Next = std::next(BI);
730       if (Next != MF.end())
731         Targets.insert(&*Next);
732     }
733   } else {
734     // If the evaluation of the branches failed, make "Targets" to be the
735     // set of all successors of the block from the CFG.
736     // If the evaluation succeeded for all visited branches, then if the
737     // last one set "FallsThru", then add an edge to the layout successor
738     // to the targets.
739     Targets.clear();
740     DEBUG(dbgs() << "  failed to evaluate a branch...adding all CFG "
741                     "successors\n");
742     for (const MachineBasicBlock *SB : B.successors())
743       Targets.insert(SB);
744   }
745 
746   for (const MachineBasicBlock *TB : Targets) {
747     unsigned TBN = TB->getNumber();
748     DEBUG(dbgs() << "  pushing edge BB#" << MBN << " -> BB#" << TBN << "\n");
749     FlowQ.push(CFGEdge(MBN, TBN));
750   }
751 }
752 
753 
754 void MachineConstPropagator::visitUsesOf(unsigned Reg) {
755   DEBUG(dbgs() << "Visiting uses of " << PrintReg(Reg, &MCE.TRI)
756                << Cells.get(Reg) << '\n');
757   for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
758     // Do not process non-executable instructions. They can become exceutable
759     // later (via a flow-edge in the work queue). In such case, the instruc-
760     // tion will be visited at that time.
761     if (!InstrExec.count(&MI))
762       continue;
763     if (MI.isPHI())
764       visitPHI(MI);
765     else if (!MI.isBranch())
766       visitNonBranch(MI);
767     else
768       visitBranchesFrom(MI);
769   }
770 }
771 
772 bool MachineConstPropagator::computeBlockSuccessors(const MachineBasicBlock *MB,
773       SetVector<const MachineBasicBlock*> &Targets) {
774   MachineBasicBlock::const_iterator FirstBr = MB->end();
775   for (const MachineInstr &MI : *MB) {
776     if (MI.isDebugValue())
777       continue;
778     if (MI.isBranch()) {
779       FirstBr = MI.getIterator();
780       break;
781     }
782   }
783 
784   Targets.clear();
785   MachineBasicBlock::const_iterator End = MB->end();
786 
787   bool DoNext = true;
788   for (MachineBasicBlock::const_iterator I = FirstBr; I != End; ++I) {
789     const MachineInstr &MI = *I;
790     // Can there be debug instructions between branches?
791     if (MI.isDebugValue())
792       continue;
793     if (!InstrExec.count(&MI))
794       continue;
795     bool Eval = MCE.evaluate(MI, Cells, Targets, DoNext);
796     if (!Eval)
797       return false;
798     if (!DoNext)
799       break;
800   }
801   // If the last branch could fall-through, add block's layout successor.
802   if (DoNext) {
803     MachineFunction::const_iterator BI = MB->getIterator();
804     MachineFunction::const_iterator NextI = std::next(BI);
805     if (NextI != MB->getParent()->end())
806       Targets.insert(&*NextI);
807   }
808 
809   // Add all the EH landing pads.
810   for (const MachineBasicBlock *SB : MB->successors())
811     if (SB->isEHPad())
812       Targets.insert(SB);
813 
814   return true;
815 }
816 
817 
818 void MachineConstPropagator::removeCFGEdge(MachineBasicBlock *From,
819       MachineBasicBlock *To) {
820   // First, remove the CFG successor/predecessor information.
821   From->removeSuccessor(To);
822   // Remove all corresponding PHI operands in the To block.
823   for (auto I = To->begin(), E = To->getFirstNonPHI(); I != E; ++I) {
824     MachineInstr *PN = &*I;
825     // reg0 = PHI reg1, bb2, reg3, bb4, ...
826     int N = PN->getNumOperands()-2;
827     while (N > 0) {
828       if (PN->getOperand(N+1).getMBB() == From) {
829         PN->RemoveOperand(N+1);
830         PN->RemoveOperand(N);
831       }
832       N -= 2;
833     }
834   }
835 }
836 
837 
838 void MachineConstPropagator::propagate(MachineFunction &MF) {
839   MachineBasicBlock *Entry = GraphTraits<MachineFunction*>::getEntryNode(&MF);
840   unsigned EntryNum = Entry->getNumber();
841 
842   // Start with a fake edge, just to process the entry node.
843   FlowQ.push(CFGEdge(EntryNum, EntryNum));
844 
845   while (!FlowQ.empty()) {
846     CFGEdge Edge = FlowQ.front();
847     FlowQ.pop();
848 
849     DEBUG(dbgs() << "Picked edge BB#" << Edge.first << "->BB#"
850                  << Edge.second << '\n');
851     if (Edge.first != EntryNum)
852       if (EdgeExec.count(Edge))
853         continue;
854     EdgeExec.insert(Edge);
855     MachineBasicBlock *SB = MF.getBlockNumbered(Edge.second);
856 
857     // Process the block in three stages:
858     // - visit all PHI nodes,
859     // - visit all non-branch instructions,
860     // - visit block branches.
861     MachineBasicBlock::const_iterator It = SB->begin(), End = SB->end();
862 
863     // Visit PHI nodes in the successor block.
864     while (It != End && It->isPHI()) {
865       InstrExec.insert(&*It);
866       visitPHI(*It);
867       ++It;
868     }
869 
870     // If the successor block just became executable, visit all instructions.
871     // To see if this is the first time we're visiting it, check the first
872     // non-debug instruction to see if it is executable.
873     while (It != End && It->isDebugValue())
874       ++It;
875     assert(It == End || !It->isPHI());
876     // If this block has been visited, go on to the next one.
877     if (It != End && InstrExec.count(&*It))
878       continue;
879     // For now, scan all non-branch instructions. Branches require different
880     // processing.
881     while (It != End && !It->isBranch()) {
882       if (!It->isDebugValue()) {
883         InstrExec.insert(&*It);
884         visitNonBranch(*It);
885       }
886       ++It;
887     }
888 
889     // Time to process the end of the block. This is different from
890     // processing regular (non-branch) instructions, because there can
891     // be multiple branches in a block, and they can cause the block to
892     // terminate early.
893     if (It != End) {
894       visitBranchesFrom(*It);
895     } else {
896       // If the block didn't have a branch, add all successor edges to the
897       // work queue. (There should really be only one successor in such case.)
898       unsigned SBN = SB->getNumber();
899       for (const MachineBasicBlock *SSB : SB->successors())
900         FlowQ.push(CFGEdge(SBN, SSB->getNumber()));
901     }
902   } // while (FlowQ)
903 
904   DEBUG({
905     dbgs() << "Cells after propagation:\n";
906     Cells.print(dbgs(), MCE.TRI);
907     dbgs() << "Dead CFG edges:\n";
908     for (const MachineBasicBlock &B : MF) {
909       unsigned BN = B.getNumber();
910       for (const MachineBasicBlock *SB : B.successors()) {
911         unsigned SN = SB->getNumber();
912         if (!EdgeExec.count(CFGEdge(BN, SN)))
913           dbgs() << "  BB#" << BN << " -> BB#" << SN << '\n';
914       }
915     }
916   });
917 }
918 
919 
920 bool MachineConstPropagator::rewrite(MachineFunction &MF) {
921   bool Changed = false;
922   // Rewrite all instructions based on the collected cell information.
923   //
924   // Traverse the instructions in a post-order, so that rewriting an
925   // instruction can make changes "downstream" in terms of control-flow
926   // without affecting the rewriting process. (We should not change
927   // instructions that have not yet been visited by the rewriter.)
928   // The reason for this is that the rewriter can introduce new vregs,
929   // and replace uses of old vregs (which had corresponding cells
930   // computed during propagation) with these new vregs (which at this
931   // point would not have any cells, and would appear to be "top").
932   // If an attempt was made to evaluate an instruction with a fresh
933   // "top" vreg, it would cause an error (abend) in the evaluator.
934 
935   // Collect the post-order-traversal block ordering. The subsequent
936   // traversal/rewrite will update block successors, so it's safer
937   // if the visiting order it computed ahead of time.
938   std::vector<MachineBasicBlock*> POT;
939   for (MachineBasicBlock *B : post_order(&MF))
940     if (!B->empty())
941       POT.push_back(B);
942 
943   for (MachineBasicBlock *B : POT) {
944     // Walk the block backwards (which usually begin with the branches).
945     // If any branch is rewritten, we may need to update the successor
946     // information for this block. Unless the block's successors can be
947     // precisely determined (which may not be the case for indirect
948     // branches), we cannot modify any branch.
949 
950     // Compute the successor information.
951     SetVector<const MachineBasicBlock*> Targets;
952     bool HaveTargets = computeBlockSuccessors(B, Targets);
953     // Rewrite the executable instructions. Skip branches if we don't
954     // have block successor information.
955     for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
956       MachineInstr &MI = *I;
957       if (InstrExec.count(&MI)) {
958         if (MI.isBranch() && !HaveTargets)
959           continue;
960         Changed |= MCE.rewrite(MI, Cells);
961       }
962     }
963     // The rewriting could rewrite PHI nodes to non-PHI nodes, causing
964     // regular instructions to appear in between PHI nodes. Bring all
965     // the PHI nodes to the beginning of the block.
966     for (auto I = B->begin(), E = B->end(); I != E; ++I) {
967       if (I->isPHI())
968         continue;
969       // I is not PHI. Find the next PHI node P.
970       auto P = I;
971       while (++P != E)
972         if (P->isPHI())
973           break;
974       // Not found.
975       if (P == E)
976         break;
977       // Splice P right before I.
978       B->splice(I, B, P);
979       // Reset I to point at the just spliced PHI node.
980       --I;
981     }
982     // Update the block successor information: remove unnecessary successors.
983     if (HaveTargets) {
984       SmallVector<MachineBasicBlock*,2> ToRemove;
985       for (MachineBasicBlock *SB : B->successors()) {
986         if (!Targets.count(SB))
987           ToRemove.push_back(const_cast<MachineBasicBlock*>(SB));
988         Targets.remove(SB);
989       }
990       for (unsigned i = 0, n = ToRemove.size(); i < n; ++i)
991         removeCFGEdge(B, ToRemove[i]);
992       // If there are any blocks left in the computed targets, it means that
993       // we think that the block could go somewhere, but the CFG does not.
994       // This could legitimately happen in blocks that have non-returning
995       // calls---we would think that the execution can continue, but the
996       // CFG will not have a successor edge.
997     }
998   }
999   // Need to do some final post-processing.
1000   // If a branch was not executable, it will not get rewritten, but should
1001   // be removed (or replaced with something equivalent to a A2_nop). We can't
1002   // erase instructions during rewriting, so this needs to be delayed until
1003   // now.
1004   for (MachineBasicBlock &B : MF) {
1005     MachineBasicBlock::iterator I = B.begin(), E = B.end();
1006     while (I != E) {
1007       auto Next = std::next(I);
1008       if (I->isBranch() && !InstrExec.count(&*I))
1009         B.erase(I);
1010       I = Next;
1011     }
1012   }
1013   return Changed;
1014 }
1015 
1016 
1017 // This is the constant propagation algorithm as described by Wegman-Zadeck.
1018 // Most of the terminology comes from there.
1019 bool MachineConstPropagator::run(MachineFunction &MF) {
1020   DEBUG(MF.print(dbgs() << "Starting MachineConstPropagator\n", 0));
1021 
1022   MRI = &MF.getRegInfo();
1023 
1024   Cells.clear();
1025   EdgeExec.clear();
1026   InstrExec.clear();
1027   assert(FlowQ.empty());
1028 
1029   propagate(MF);
1030   bool Changed = rewrite(MF);
1031 
1032   DEBUG({
1033     dbgs() << "End of MachineConstPropagator (Changed=" << Changed << ")\n";
1034     if (Changed)
1035       MF.print(dbgs(), 0);
1036   });
1037   return Changed;
1038 }
1039 
1040 
1041 // --------------------------------------------------------------------
1042 // Machine const evaluator.
1043 
1044 bool MachineConstEvaluator::getCell(const Register &R, const CellMap &Inputs,
1045       LatticeCell &RC) {
1046   if (!TargetRegisterInfo::isVirtualRegister(R.Reg))
1047     return false;
1048   const LatticeCell &L = Inputs.get(R.Reg);
1049   if (!R.SubReg) {
1050     RC = L;
1051     return !RC.isBottom();
1052   }
1053   bool Eval = evaluate(R, L, RC);
1054   return Eval && !RC.isBottom();
1055 }
1056 
1057 
1058 bool MachineConstEvaluator::constToInt(const Constant *C,
1059       APInt &Val) const {
1060   const ConstantInt *CI = dyn_cast<ConstantInt>(C);
1061   if (!CI)
1062     return false;
1063   Val = CI->getValue();
1064   return true;
1065 }
1066 
1067 
1068 const ConstantInt *MachineConstEvaluator::intToConst(const APInt &Val) const {
1069   return ConstantInt::get(CX, Val);
1070 }
1071 
1072 
1073 bool MachineConstEvaluator::evaluateCMPrr(uint32_t Cmp, const Register &R1,
1074       const Register &R2, const CellMap &Inputs, bool &Result) {
1075   assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1076   LatticeCell LS1, LS2;
1077   if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2))
1078     return false;
1079 
1080   bool IsProp1 = LS1.isProperty();
1081   bool IsProp2 = LS2.isProperty();
1082   if (IsProp1) {
1083     uint32_t Prop1 = LS1.properties();
1084     if (IsProp2)
1085       return evaluateCMPpp(Cmp, Prop1, LS2.properties(), Result);
1086     uint32_t NegCmp = Comparison::negate(Cmp);
1087     return evaluateCMPrp(NegCmp, R2, Prop1, Inputs, Result);
1088   }
1089   if (IsProp2) {
1090     uint32_t Prop2 = LS2.properties();
1091     return evaluateCMPrp(Cmp, R1, Prop2, Inputs, Result);
1092   }
1093 
1094   APInt A;
1095   bool IsTrue = true, IsFalse = true;
1096   for (unsigned i = 0; i < LS2.size(); ++i) {
1097     bool Res;
1098     bool Computed = constToInt(LS2.Values[i], A) &&
1099                     evaluateCMPri(Cmp, R1, A, Inputs, Res);
1100     if (!Computed)
1101       return false;
1102     IsTrue &= Res;
1103     IsFalse &= !Res;
1104   }
1105   assert(!IsTrue || !IsFalse);
1106   // The actual logical value of the comparison is same as IsTrue.
1107   Result = IsTrue;
1108   // Return true if the result was proven to be true or proven to be false.
1109   return IsTrue || IsFalse;
1110 }
1111 
1112 
1113 bool MachineConstEvaluator::evaluateCMPri(uint32_t Cmp, const Register &R1,
1114       const APInt &A2, const CellMap &Inputs, bool &Result) {
1115   assert(Inputs.has(R1.Reg));
1116   LatticeCell LS;
1117   if (!getCell(R1, Inputs, LS))
1118     return false;
1119   if (LS.isProperty())
1120     return evaluateCMPpi(Cmp, LS.properties(), A2, Result);
1121 
1122   APInt A;
1123   bool IsTrue = true, IsFalse = true;
1124   for (unsigned i = 0; i < LS.size(); ++i) {
1125     bool Res;
1126     bool Computed = constToInt(LS.Values[i], A) &&
1127                     evaluateCMPii(Cmp, A, A2, Res);
1128     if (!Computed)
1129       return false;
1130     IsTrue &= Res;
1131     IsFalse &= !Res;
1132   }
1133   assert(!IsTrue || !IsFalse);
1134   // The actual logical value of the comparison is same as IsTrue.
1135   Result = IsTrue;
1136   // Return true if the result was proven to be true or proven to be false.
1137   return IsTrue || IsFalse;
1138 }
1139 
1140 
1141 bool MachineConstEvaluator::evaluateCMPrp(uint32_t Cmp, const Register &R1,
1142       uint64_t Props2, const CellMap &Inputs, bool &Result) {
1143   assert(Inputs.has(R1.Reg));
1144   LatticeCell LS;
1145   if (!getCell(R1, Inputs, LS))
1146     return false;
1147   if (LS.isProperty())
1148     return evaluateCMPpp(Cmp, LS.properties(), Props2, Result);
1149 
1150   APInt A;
1151   uint32_t NegCmp = Comparison::negate(Cmp);
1152   bool IsTrue = true, IsFalse = true;
1153   for (unsigned i = 0; i < LS.size(); ++i) {
1154     bool Res;
1155     bool Computed = constToInt(LS.Values[i], A) &&
1156                     evaluateCMPpi(NegCmp, Props2, A, Res);
1157     if (!Computed)
1158       return false;
1159     IsTrue &= Res;
1160     IsFalse &= !Res;
1161   }
1162   assert(!IsTrue || !IsFalse);
1163   Result = IsTrue;
1164   return IsTrue || IsFalse;
1165 }
1166 
1167 
1168 bool MachineConstEvaluator::evaluateCMPii(uint32_t Cmp, const APInt &A1,
1169       const APInt &A2, bool &Result) {
1170   // NE is a special kind of comparison (not composed of smaller properties).
1171   if (Cmp == Comparison::NE) {
1172     Result = !APInt::isSameValue(A1, A2);
1173     return true;
1174   }
1175   if (Cmp == Comparison::EQ) {
1176     Result = APInt::isSameValue(A1, A2);
1177     return true;
1178   }
1179   if (Cmp & Comparison::EQ) {
1180     if (APInt::isSameValue(A1, A2))
1181       return (Result = true);
1182   }
1183   assert((Cmp & (Comparison::L | Comparison::G)) && "Malformed comparison");
1184   Result = false;
1185 
1186   unsigned W1 = A1.getBitWidth();
1187   unsigned W2 = A2.getBitWidth();
1188   unsigned MaxW = (W1 >= W2) ? W1 : W2;
1189   if (Cmp & Comparison::U) {
1190     const APInt Zx1 = A1.zextOrSelf(MaxW);
1191     const APInt Zx2 = A2.zextOrSelf(MaxW);
1192     if (Cmp & Comparison::L)
1193       Result = Zx1.ult(Zx2);
1194     else if (Cmp & Comparison::G)
1195       Result = Zx2.ult(Zx1);
1196     return true;
1197   }
1198 
1199   // Signed comparison.
1200   const APInt Sx1 = A1.sextOrSelf(MaxW);
1201   const APInt Sx2 = A2.sextOrSelf(MaxW);
1202   if (Cmp & Comparison::L)
1203     Result = Sx1.slt(Sx2);
1204   else if (Cmp & Comparison::G)
1205     Result = Sx2.slt(Sx1);
1206   return true;
1207 }
1208 
1209 
1210 bool MachineConstEvaluator::evaluateCMPpi(uint32_t Cmp, uint32_t Props,
1211       const APInt &A2, bool &Result) {
1212   if (Props == ConstantProperties::Unknown)
1213     return false;
1214 
1215   // Should never see NaN here, but check for it for completeness.
1216   if (Props & ConstantProperties::NaN)
1217     return false;
1218   // Infinity could theoretically be compared to a number, but the
1219   // presence of infinity here would be very suspicious. If we don't
1220   // know for sure that the number is finite, bail out.
1221   if (!(Props & ConstantProperties::Finite))
1222     return false;
1223 
1224   // Let X be a number that has properties Props.
1225 
1226   if (Cmp & Comparison::U) {
1227     // In case of unsigned comparisons, we can only compare against 0.
1228     if (A2 == 0) {
1229       // Any x!=0 will be considered >0 in an unsigned comparison.
1230       if (Props & ConstantProperties::Zero)
1231         Result = (Cmp & Comparison::EQ);
1232       else if (Props & ConstantProperties::NonZero)
1233         Result = (Cmp & Comparison::G) || (Cmp == Comparison::NE);
1234       else
1235         return false;
1236       return true;
1237     }
1238     // A2 is not zero. The only handled case is if X = 0.
1239     if (Props & ConstantProperties::Zero) {
1240       Result = (Cmp & Comparison::L) || (Cmp == Comparison::NE);
1241       return true;
1242     }
1243     return false;
1244   }
1245 
1246   // Signed comparisons are different.
1247   if (Props & ConstantProperties::Zero) {
1248     if (A2 == 0)
1249       Result = (Cmp & Comparison::EQ);
1250     else
1251       Result = (Cmp == Comparison::NE) ||
1252                ((Cmp & Comparison::L) && !A2.isNegative()) ||
1253                ((Cmp & Comparison::G) &&  A2.isNegative());
1254     return true;
1255   }
1256   if (Props & ConstantProperties::PosOrZero) {
1257     // X >= 0 and !(A2 < 0) => cannot compare
1258     if (!A2.isNegative())
1259       return false;
1260     // X >= 0 and A2 < 0
1261     Result = (Cmp & Comparison::G) || (Cmp == Comparison::NE);
1262     return true;
1263   }
1264   if (Props & ConstantProperties::NegOrZero) {
1265     // X <= 0 and Src1 < 0 => cannot compare
1266     if (A2 == 0 || A2.isNegative())
1267       return false;
1268     // X <= 0 and A2 > 0
1269     Result = (Cmp & Comparison::L) || (Cmp == Comparison::NE);
1270     return true;
1271   }
1272 
1273   return false;
1274 }
1275 
1276 
1277 bool MachineConstEvaluator::evaluateCMPpp(uint32_t Cmp, uint32_t Props1,
1278       uint32_t Props2, bool &Result) {
1279   typedef ConstantProperties P;
1280   if ((Props1 & P::NaN) && (Props2 & P::NaN))
1281     return false;
1282   if (!(Props1 & P::Finite) || !(Props2 & P::Finite))
1283     return false;
1284 
1285   bool Zero1 = (Props1 & P::Zero), Zero2 = (Props2 & P::Zero);
1286   bool NonZero1 = (Props1 & P::NonZero), NonZero2 = (Props2 & P::NonZero);
1287   if (Zero1 && Zero2) {
1288     Result = (Cmp & Comparison::EQ);
1289     return true;
1290   }
1291   if (Cmp == Comparison::NE) {
1292     if ((Zero1 && NonZero2) || (NonZero1 && Zero2))
1293       return (Result = true);
1294     return false;
1295   }
1296 
1297   if (Cmp & Comparison::U) {
1298     // In unsigned comparisons, we can only compare against a known zero,
1299     // or a known non-zero.
1300     if (Zero1 && NonZero2) {
1301       Result = (Cmp & Comparison::L);
1302       return true;
1303     }
1304     if (NonZero1 && Zero2) {
1305       Result = (Cmp & Comparison::G);
1306       return true;
1307     }
1308     return false;
1309   }
1310 
1311   // Signed comparison. The comparison is not NE.
1312   bool Poz1 = (Props1 & P::PosOrZero), Poz2 = (Props2 & P::PosOrZero);
1313   bool Nez1 = (Props1 & P::NegOrZero), Nez2 = (Props2 & P::NegOrZero);
1314   if (Nez1 && Poz2) {
1315     if (NonZero1 || NonZero2) {
1316       Result = (Cmp & Comparison::L);
1317       return true;
1318     }
1319     // Either (or both) could be zero. Can only say that X <= Y.
1320     if ((Cmp & Comparison::EQ) && (Cmp & Comparison::L))
1321       return (Result = true);
1322   }
1323   if (Poz1 && Nez2) {
1324     if (NonZero1 || NonZero2) {
1325       Result = (Cmp & Comparison::G);
1326       return true;
1327     }
1328     // Either (or both) could be zero. Can only say that X >= Y.
1329     if ((Cmp & Comparison::EQ) && (Cmp & Comparison::G))
1330       return (Result = true);
1331   }
1332 
1333   return false;
1334 }
1335 
1336 
1337 bool MachineConstEvaluator::evaluateCOPY(const Register &R1,
1338       const CellMap &Inputs, LatticeCell &Result) {
1339   return getCell(R1, Inputs, Result);
1340 }
1341 
1342 
1343 bool MachineConstEvaluator::evaluateANDrr(const Register &R1,
1344       const Register &R2, const CellMap &Inputs, LatticeCell &Result) {
1345   assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1346   const LatticeCell &L1 = Inputs.get(R2.Reg);
1347   const LatticeCell &L2 = Inputs.get(R2.Reg);
1348   // If both sources are bottom, exit. Otherwise try to evaluate ANDri
1349   // with the non-bottom argument passed as the immediate. This is to
1350   // catch cases of ANDing with 0.
1351   if (L2.isBottom()) {
1352     if (L1.isBottom())
1353       return false;
1354     return evaluateANDrr(R2, R1, Inputs, Result);
1355   }
1356   LatticeCell LS2;
1357   if (!evaluate(R2, L2, LS2))
1358     return false;
1359   if (LS2.isBottom() || LS2.isProperty())
1360     return false;
1361 
1362   APInt A;
1363   for (unsigned i = 0; i < LS2.size(); ++i) {
1364     LatticeCell RC;
1365     bool Eval = constToInt(LS2.Values[i], A) &&
1366                 evaluateANDri(R1, A, Inputs, RC);
1367     if (!Eval)
1368       return false;
1369     Result.meet(RC);
1370   }
1371   return !Result.isBottom();
1372 }
1373 
1374 
1375 bool MachineConstEvaluator::evaluateANDri(const Register &R1,
1376       const APInt &A2, const CellMap &Inputs, LatticeCell &Result) {
1377   assert(Inputs.has(R1.Reg));
1378   if (A2 == -1)
1379     return getCell(R1, Inputs, Result);
1380   if (A2 == 0) {
1381     LatticeCell RC;
1382     RC.add(intToConst(A2));
1383     // Overwrite Result.
1384     Result = RC;
1385     return true;
1386   }
1387   LatticeCell LS1;
1388   if (!getCell(R1, Inputs, LS1))
1389     return false;
1390   if (LS1.isBottom() || LS1.isProperty())
1391     return false;
1392 
1393   APInt A, ResA;
1394   for (unsigned i = 0; i < LS1.size(); ++i) {
1395     bool Eval = constToInt(LS1.Values[i], A) &&
1396                 evaluateANDii(A, A2, ResA);
1397     if (!Eval)
1398       return false;
1399     const Constant *C = intToConst(ResA);
1400     Result.add(C);
1401   }
1402   return !Result.isBottom();
1403 }
1404 
1405 
1406 bool MachineConstEvaluator::evaluateANDii(const APInt &A1,
1407       const APInt &A2, APInt &Result) {
1408   Result = A1 & A2;
1409   return true;
1410 }
1411 
1412 
1413 bool MachineConstEvaluator::evaluateORrr(const Register &R1,
1414       const Register &R2, const CellMap &Inputs, LatticeCell &Result) {
1415   assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1416   const LatticeCell &L1 = Inputs.get(R2.Reg);
1417   const LatticeCell &L2 = Inputs.get(R2.Reg);
1418   // If both sources are bottom, exit. Otherwise try to evaluate ORri
1419   // with the non-bottom argument passed as the immediate. This is to
1420   // catch cases of ORing with -1.
1421   if (L2.isBottom()) {
1422     if (L1.isBottom())
1423       return false;
1424     return evaluateORrr(R2, R1, Inputs, Result);
1425   }
1426   LatticeCell LS2;
1427   if (!evaluate(R2, L2, LS2))
1428     return false;
1429   if (LS2.isBottom() || LS2.isProperty())
1430     return false;
1431 
1432   APInt A;
1433   for (unsigned i = 0; i < LS2.size(); ++i) {
1434     LatticeCell RC;
1435     bool Eval = constToInt(LS2.Values[i], A) &&
1436                 evaluateORri(R1, A, Inputs, RC);
1437     if (!Eval)
1438       return false;
1439     Result.meet(RC);
1440   }
1441   return !Result.isBottom();
1442 }
1443 
1444 
1445 bool MachineConstEvaluator::evaluateORri(const Register &R1,
1446       const APInt &A2, const CellMap &Inputs, LatticeCell &Result) {
1447   assert(Inputs.has(R1.Reg));
1448   if (A2 == 0)
1449     return getCell(R1, Inputs, Result);
1450   if (A2 == -1) {
1451     LatticeCell RC;
1452     RC.add(intToConst(A2));
1453     // Overwrite Result.
1454     Result = RC;
1455     return true;
1456   }
1457   LatticeCell LS1;
1458   if (!getCell(R1, Inputs, LS1))
1459     return false;
1460   if (LS1.isBottom() || LS1.isProperty())
1461     return false;
1462 
1463   APInt A, ResA;
1464   for (unsigned i = 0; i < LS1.size(); ++i) {
1465     bool Eval = constToInt(LS1.Values[i], A) &&
1466                 evaluateORii(A, A2, ResA);
1467     if (!Eval)
1468       return false;
1469     const Constant *C = intToConst(ResA);
1470     Result.add(C);
1471   }
1472   return !Result.isBottom();
1473 }
1474 
1475 
1476 bool MachineConstEvaluator::evaluateORii(const APInt &A1,
1477       const APInt &A2, APInt &Result) {
1478   Result = A1 | A2;
1479   return true;
1480 }
1481 
1482 
1483 bool MachineConstEvaluator::evaluateXORrr(const Register &R1,
1484       const Register &R2, const CellMap &Inputs, LatticeCell &Result) {
1485   assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1486   LatticeCell LS1, LS2;
1487   if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2))
1488     return false;
1489   if (LS1.isProperty()) {
1490     if (LS1.properties() & ConstantProperties::Zero)
1491       return !(Result = LS2).isBottom();
1492     return false;
1493   }
1494   if (LS2.isProperty()) {
1495     if (LS2.properties() & ConstantProperties::Zero)
1496       return !(Result = LS1).isBottom();
1497     return false;
1498   }
1499 
1500   APInt A;
1501   for (unsigned i = 0; i < LS2.size(); ++i) {
1502     LatticeCell RC;
1503     bool Eval = constToInt(LS2.Values[i], A) &&
1504                 evaluateXORri(R1, A, Inputs, RC);
1505     if (!Eval)
1506       return false;
1507     Result.meet(RC);
1508   }
1509   return !Result.isBottom();
1510 }
1511 
1512 
1513 bool MachineConstEvaluator::evaluateXORri(const Register &R1,
1514       const APInt &A2, const CellMap &Inputs, LatticeCell &Result) {
1515   assert(Inputs.has(R1.Reg));
1516   LatticeCell LS1;
1517   if (!getCell(R1, Inputs, LS1))
1518     return false;
1519   if (LS1.isProperty()) {
1520     if (LS1.properties() & ConstantProperties::Zero) {
1521       const Constant *C = intToConst(A2);
1522       Result.add(C);
1523       return !Result.isBottom();
1524     }
1525     return false;
1526   }
1527 
1528   APInt A, XA;
1529   for (unsigned i = 0; i < LS1.size(); ++i) {
1530     bool Eval = constToInt(LS1.Values[i], A) &&
1531                 evaluateXORii(A, A2, XA);
1532     if (!Eval)
1533       return false;
1534     const Constant *C = intToConst(XA);
1535     Result.add(C);
1536   }
1537   return !Result.isBottom();
1538 }
1539 
1540 
1541 bool MachineConstEvaluator::evaluateXORii(const APInt &A1,
1542       const APInt &A2, APInt &Result) {
1543   Result = A1 ^ A2;
1544   return true;
1545 }
1546 
1547 
1548 bool MachineConstEvaluator::evaluateZEXTr(const Register &R1, unsigned Width,
1549       unsigned Bits, const CellMap &Inputs, LatticeCell &Result) {
1550   assert(Inputs.has(R1.Reg));
1551   LatticeCell LS1;
1552   if (!getCell(R1, Inputs, LS1))
1553     return false;
1554   if (LS1.isProperty())
1555     return false;
1556 
1557   APInt A, XA;
1558   for (unsigned i = 0; i < LS1.size(); ++i) {
1559     bool Eval = constToInt(LS1.Values[i], A) &&
1560                 evaluateZEXTi(A, Width, Bits, XA);
1561     if (!Eval)
1562       return false;
1563     const Constant *C = intToConst(XA);
1564     Result.add(C);
1565   }
1566   return true;
1567 }
1568 
1569 
1570 bool MachineConstEvaluator::evaluateZEXTi(const APInt &A1, unsigned Width,
1571       unsigned Bits, APInt &Result) {
1572   unsigned BW = A1.getBitWidth();
1573   (void)BW;
1574   assert(Width >= Bits && BW >= Bits);
1575   APInt Mask = APInt::getLowBitsSet(Width, Bits);
1576   Result = A1.zextOrTrunc(Width) & Mask;
1577   return true;
1578 }
1579 
1580 
1581 bool MachineConstEvaluator::evaluateSEXTr(const Register &R1, unsigned Width,
1582       unsigned Bits, const CellMap &Inputs, LatticeCell &Result) {
1583   assert(Inputs.has(R1.Reg));
1584   LatticeCell LS1;
1585   if (!getCell(R1, Inputs, LS1))
1586     return false;
1587   if (LS1.isBottom() || LS1.isProperty())
1588     return false;
1589 
1590   APInt A, XA;
1591   for (unsigned i = 0; i < LS1.size(); ++i) {
1592     bool Eval = constToInt(LS1.Values[i], A) &&
1593                 evaluateSEXTi(A, Width, Bits, XA);
1594     if (!Eval)
1595       return false;
1596     const Constant *C = intToConst(XA);
1597     Result.add(C);
1598   }
1599   return true;
1600 }
1601 
1602 
1603 bool MachineConstEvaluator::evaluateSEXTi(const APInt &A1, unsigned Width,
1604       unsigned Bits, APInt &Result) {
1605   unsigned BW = A1.getBitWidth();
1606   assert(Width >= Bits && BW >= Bits);
1607   // Special case to make things faster for smaller source widths.
1608   // Sign extension of 0 bits generates 0 as a result. This is consistent
1609   // with what the HW does.
1610   if (Bits == 0) {
1611     Result = APInt(Width, 0);
1612     return true;
1613   }
1614   // In C, shifts by 64 invoke undefined behavior: handle that case in APInt.
1615   if (BW <= 64 && Bits != 0) {
1616     int64_t V = A1.getSExtValue();
1617     switch (Bits) {
1618       case 8:
1619         V = static_cast<int8_t>(V);
1620         break;
1621       case 16:
1622         V = static_cast<int16_t>(V);
1623         break;
1624       case 32:
1625         V = static_cast<int32_t>(V);
1626         break;
1627       default:
1628         // Shift left to lose all bits except lower "Bits" bits, then shift
1629         // the value back, replicating what was a sign bit after the first
1630         // shift.
1631         V = (V << (64-Bits)) >> (64-Bits);
1632         break;
1633     }
1634     // V is a 64-bit sign-extended value. Convert it to APInt of desired
1635     // width.
1636     Result = APInt(Width, V, true);
1637     return true;
1638   }
1639   // Slow case: the value doesn't fit in int64_t.
1640   if (Bits < BW)
1641     Result = A1.trunc(Bits).sext(Width);
1642   else // Bits == BW
1643     Result = A1.sext(Width);
1644   return true;
1645 }
1646 
1647 
1648 bool MachineConstEvaluator::evaluateCLBr(const Register &R1, bool Zeros,
1649       bool Ones, const CellMap &Inputs, LatticeCell &Result) {
1650   assert(Inputs.has(R1.Reg));
1651   LatticeCell LS1;
1652   if (!getCell(R1, Inputs, LS1))
1653     return false;
1654   if (LS1.isBottom() || LS1.isProperty())
1655     return false;
1656 
1657   APInt A, CA;
1658   for (unsigned i = 0; i < LS1.size(); ++i) {
1659     bool Eval = constToInt(LS1.Values[i], A) &&
1660                 evaluateCLBi(A, Zeros, Ones, CA);
1661     if (!Eval)
1662       return false;
1663     const Constant *C = intToConst(CA);
1664     Result.add(C);
1665   }
1666   return true;
1667 }
1668 
1669 
1670 bool MachineConstEvaluator::evaluateCLBi(const APInt &A1, bool Zeros,
1671       bool Ones, APInt &Result) {
1672   unsigned BW = A1.getBitWidth();
1673   if (!Zeros && !Ones)
1674     return false;
1675   unsigned Count = 0;
1676   if (Zeros && (Count == 0))
1677     Count = A1.countLeadingZeros();
1678   if (Ones && (Count == 0))
1679     Count = A1.countLeadingOnes();
1680   Result = APInt(BW, static_cast<uint64_t>(Count), false);
1681   return true;
1682 }
1683 
1684 
1685 bool MachineConstEvaluator::evaluateCTBr(const Register &R1, bool Zeros,
1686       bool Ones, const CellMap &Inputs, LatticeCell &Result) {
1687   assert(Inputs.has(R1.Reg));
1688   LatticeCell LS1;
1689   if (!getCell(R1, Inputs, LS1))
1690     return false;
1691   if (LS1.isBottom() || LS1.isProperty())
1692     return false;
1693 
1694   APInt A, CA;
1695   for (unsigned i = 0; i < LS1.size(); ++i) {
1696     bool Eval = constToInt(LS1.Values[i], A) &&
1697                 evaluateCTBi(A, Zeros, Ones, CA);
1698     if (!Eval)
1699       return false;
1700     const Constant *C = intToConst(CA);
1701     Result.add(C);
1702   }
1703   return true;
1704 }
1705 
1706 
1707 bool MachineConstEvaluator::evaluateCTBi(const APInt &A1, bool Zeros,
1708       bool Ones, APInt &Result) {
1709   unsigned BW = A1.getBitWidth();
1710   if (!Zeros && !Ones)
1711     return false;
1712   unsigned Count = 0;
1713   if (Zeros && (Count == 0))
1714     Count = A1.countTrailingZeros();
1715   if (Ones && (Count == 0))
1716     Count = A1.countTrailingOnes();
1717   Result = APInt(BW, static_cast<uint64_t>(Count), false);
1718   return true;
1719 }
1720 
1721 
1722 bool MachineConstEvaluator::evaluateEXTRACTr(const Register &R1,
1723       unsigned Width, unsigned Bits, unsigned Offset, bool Signed,
1724       const CellMap &Inputs, LatticeCell &Result) {
1725   assert(Inputs.has(R1.Reg));
1726   assert(Bits+Offset <= Width);
1727   LatticeCell LS1;
1728   if (!getCell(R1, Inputs, LS1))
1729     return false;
1730   if (LS1.isBottom())
1731     return false;
1732   if (LS1.isProperty()) {
1733     uint32_t Ps = LS1.properties();
1734     if (Ps & ConstantProperties::Zero) {
1735       const Constant *C = intToConst(APInt(Width, 0, false));
1736       Result.add(C);
1737       return true;
1738     }
1739     return false;
1740   }
1741 
1742   APInt A, CA;
1743   for (unsigned i = 0; i < LS1.size(); ++i) {
1744     bool Eval = constToInt(LS1.Values[i], A) &&
1745                 evaluateEXTRACTi(A, Bits, Offset, Signed, CA);
1746     if (!Eval)
1747       return false;
1748     const Constant *C = intToConst(CA);
1749     Result.add(C);
1750   }
1751   return true;
1752 }
1753 
1754 
1755 bool MachineConstEvaluator::evaluateEXTRACTi(const APInt &A1, unsigned Bits,
1756       unsigned Offset, bool Signed, APInt &Result) {
1757   unsigned BW = A1.getBitWidth();
1758   assert(Bits+Offset <= BW);
1759   // Extracting 0 bits generates 0 as a result (as indicated by the HW people).
1760   if (Bits == 0) {
1761     Result = APInt(BW, 0);
1762     return true;
1763   }
1764   if (BW <= 64) {
1765     int64_t V = A1.getZExtValue();
1766     V <<= (64-Bits-Offset);
1767     if (Signed)
1768       V >>= (64-Bits);
1769     else
1770       V = static_cast<uint64_t>(V) >> (64-Bits);
1771     Result = APInt(BW, V, Signed);
1772     return true;
1773   }
1774   if (Signed)
1775     Result = A1.shl(BW-Bits-Offset).ashr(BW-Bits);
1776   else
1777     Result = A1.shl(BW-Bits-Offset).lshr(BW-Bits);
1778   return true;
1779 }
1780 
1781 
1782 bool MachineConstEvaluator::evaluateSplatr(const Register &R1,
1783       unsigned Bits, unsigned Count, const CellMap &Inputs,
1784       LatticeCell &Result) {
1785   assert(Inputs.has(R1.Reg));
1786   LatticeCell LS1;
1787   if (!getCell(R1, Inputs, LS1))
1788     return false;
1789   if (LS1.isBottom() || LS1.isProperty())
1790     return false;
1791 
1792   APInt A, SA;
1793   for (unsigned i = 0; i < LS1.size(); ++i) {
1794     bool Eval = constToInt(LS1.Values[i], A) &&
1795                 evaluateSplati(A, Bits, Count, SA);
1796     if (!Eval)
1797       return false;
1798     const Constant *C = intToConst(SA);
1799     Result.add(C);
1800   }
1801   return true;
1802 }
1803 
1804 
1805 bool MachineConstEvaluator::evaluateSplati(const APInt &A1, unsigned Bits,
1806       unsigned Count, APInt &Result) {
1807   assert(Count > 0);
1808   unsigned BW = A1.getBitWidth(), SW = Count*Bits;
1809   APInt LoBits = (Bits < BW) ? A1.trunc(Bits) : A1.zextOrSelf(Bits);
1810   if (Count > 1)
1811     LoBits = LoBits.zext(SW);
1812 
1813   APInt Res(SW, 0, false);
1814   for (unsigned i = 0; i < Count; ++i) {
1815     Res <<= Bits;
1816     Res |= LoBits;
1817   }
1818   Result = Res;
1819   return true;
1820 }
1821 
1822 
1823 // ----------------------------------------------------------------------
1824 // Hexagon-specific code.
1825 
1826 namespace llvm {
1827   FunctionPass *createHexagonConstPropagationPass();
1828   void initializeHexagonConstPropagationPass(PassRegistry &Registry);
1829 }
1830 
1831 namespace {
1832   class HexagonConstEvaluator : public MachineConstEvaluator {
1833   public:
1834     HexagonConstEvaluator(MachineFunction &Fn);
1835 
1836     bool evaluate(const MachineInstr &MI, const CellMap &Inputs,
1837           CellMap &Outputs) override;
1838     bool evaluate(const Register &R, const LatticeCell &SrcC,
1839           LatticeCell &Result) override;
1840     bool evaluate(const MachineInstr &BrI, const CellMap &Inputs,
1841           SetVector<const MachineBasicBlock*> &Targets, bool &FallsThru)
1842           override;
1843     bool rewrite(MachineInstr &MI, const CellMap &Inputs) override;
1844 
1845 
1846   private:
1847     unsigned getRegBitWidth(unsigned Reg) const;
1848 
1849     static uint32_t getCmp(unsigned Opc);
1850     static APInt getCmpImm(unsigned Opc, unsigned OpX,
1851           const MachineOperand &MO);
1852     void replaceWithNop(MachineInstr &MI);
1853 
1854     bool evaluateHexRSEQ32(Register RL, Register RH, const CellMap &Inputs,
1855           LatticeCell &Result);
1856     bool evaluateHexCompare(const MachineInstr &MI, const CellMap &Inputs,
1857           CellMap &Outputs);
1858     // This is suitable to be called for compare-and-jump instructions.
1859     bool evaluateHexCompare2(uint32_t Cmp, const MachineOperand &Src1,
1860           const MachineOperand &Src2, const CellMap &Inputs, bool &Result);
1861     bool evaluateHexLogical(const MachineInstr &MI, const CellMap &Inputs,
1862           CellMap &Outputs);
1863     bool evaluateHexCondMove(const MachineInstr &MI, const CellMap &Inputs,
1864           CellMap &Outputs);
1865     bool evaluateHexExt(const MachineInstr &MI, const CellMap &Inputs,
1866           CellMap &Outputs);
1867     bool evaluateHexVector1(const MachineInstr &MI, const CellMap &Inputs,
1868           CellMap &Outputs);
1869     bool evaluateHexVector2(const MachineInstr &MI, const CellMap &Inputs,
1870           CellMap &Outputs);
1871 
1872     void replaceAllRegUsesWith(unsigned FromReg, unsigned ToReg);
1873     bool rewriteHexBranch(MachineInstr &BrI, const CellMap &Inputs);
1874     bool rewriteHexConstDefs(MachineInstr &MI, const CellMap &Inputs,
1875           bool &AllDefs);
1876     bool rewriteHexConstUses(MachineInstr &MI, const CellMap &Inputs);
1877 
1878     MachineRegisterInfo *MRI;
1879     const HexagonInstrInfo &HII;
1880     const HexagonRegisterInfo &HRI;
1881   };
1882 
1883 
1884   class HexagonConstPropagation : public MachineFunctionPass {
1885   public:
1886     static char ID;
1887     HexagonConstPropagation() : MachineFunctionPass(ID) {
1888       PassRegistry &Registry = *PassRegistry::getPassRegistry();
1889       initializeHexagonConstPropagationPass(Registry);
1890     }
1891     StringRef getPassName() const override {
1892       return "Hexagon Constant Propagation";
1893     }
1894     bool runOnMachineFunction(MachineFunction &MF) override {
1895       const Function *F = MF.getFunction();
1896       if (!F)
1897         return false;
1898       if (skipFunction(*F))
1899         return false;
1900 
1901       HexagonConstEvaluator HCE(MF);
1902       return MachineConstPropagator(HCE).run(MF);
1903     }
1904   };
1905 
1906   char HexagonConstPropagation::ID = 0;
1907 }
1908 
1909 INITIALIZE_PASS(HexagonConstPropagation, "hcp", "Hexagon Constant Propagation",
1910                 false, false)
1911 
1912 
1913 HexagonConstEvaluator::HexagonConstEvaluator(MachineFunction &Fn)
1914   : MachineConstEvaluator(Fn),
1915     HII(*Fn.getSubtarget<HexagonSubtarget>().getInstrInfo()),
1916     HRI(*Fn.getSubtarget<HexagonSubtarget>().getRegisterInfo()) {
1917   MRI = &Fn.getRegInfo();
1918 }
1919 
1920 
1921 bool HexagonConstEvaluator::evaluate(const MachineInstr &MI,
1922       const CellMap &Inputs, CellMap &Outputs) {
1923   if (MI.isCall())
1924     return false;
1925   if (MI.getNumOperands() == 0 || !MI.getOperand(0).isReg())
1926     return false;
1927   const MachineOperand &MD = MI.getOperand(0);
1928   if (!MD.isDef())
1929     return false;
1930 
1931   unsigned Opc = MI.getOpcode();
1932   Register DefR(MD);
1933   assert(!DefR.SubReg);
1934   if (!TargetRegisterInfo::isVirtualRegister(DefR.Reg))
1935     return false;
1936 
1937   if (MI.isCopy()) {
1938     LatticeCell RC;
1939     Register SrcR(MI.getOperand(1));
1940     bool Eval = evaluateCOPY(SrcR, Inputs, RC);
1941     if (!Eval)
1942       return false;
1943     Outputs.update(DefR.Reg, RC);
1944     return true;
1945   }
1946   if (MI.isRegSequence()) {
1947     unsigned Sub1 = MI.getOperand(2).getImm();
1948     unsigned Sub2 = MI.getOperand(4).getImm();
1949     const TargetRegisterClass *DefRC = MRI->getRegClass(DefR.Reg);
1950     unsigned SubLo = HRI.getHexagonSubRegIndex(DefRC, Hexagon::ps_sub_lo);
1951     unsigned SubHi = HRI.getHexagonSubRegIndex(DefRC, Hexagon::ps_sub_hi);
1952     if (Sub1 != SubLo && Sub1 != SubHi)
1953       return false;
1954     if (Sub2 != SubLo && Sub2 != SubHi)
1955       return false;
1956     assert(Sub1 != Sub2);
1957     bool LoIs1 = (Sub1 == SubLo);
1958     const MachineOperand &OpLo = LoIs1 ? MI.getOperand(1) : MI.getOperand(3);
1959     const MachineOperand &OpHi = LoIs1 ? MI.getOperand(3) : MI.getOperand(1);
1960     LatticeCell RC;
1961     Register SrcRL(OpLo), SrcRH(OpHi);
1962     bool Eval = evaluateHexRSEQ32(SrcRL, SrcRH, Inputs, RC);
1963     if (!Eval)
1964       return false;
1965     Outputs.update(DefR.Reg, RC);
1966     return true;
1967   }
1968   if (MI.isCompare()) {
1969     bool Eval = evaluateHexCompare(MI, Inputs, Outputs);
1970     return Eval;
1971   }
1972 
1973   switch (Opc) {
1974     default:
1975       return false;
1976     case Hexagon::A2_tfrsi:
1977     case Hexagon::A2_tfrpi:
1978     case Hexagon::CONST32:
1979     case Hexagon::CONST64:
1980     {
1981       const MachineOperand &VO = MI.getOperand(1);
1982       // The operand of CONST32 can be a blockaddress, e.g.
1983       //   %vreg0<def> = CONST32 <blockaddress(@eat, %L)>
1984       // Do this check for all instructions for safety.
1985       if (!VO.isImm())
1986         return false;
1987       int64_t V = MI.getOperand(1).getImm();
1988       unsigned W = getRegBitWidth(DefR.Reg);
1989       if (W != 32 && W != 64)
1990         return false;
1991       IntegerType *Ty = (W == 32) ? Type::getInt32Ty(CX)
1992                                   : Type::getInt64Ty(CX);
1993       const ConstantInt *CI = ConstantInt::get(Ty, V, true);
1994       LatticeCell RC = Outputs.get(DefR.Reg);
1995       RC.add(CI);
1996       Outputs.update(DefR.Reg, RC);
1997       break;
1998     }
1999 
2000     case Hexagon::PS_true:
2001     case Hexagon::PS_false:
2002     {
2003       LatticeCell RC = Outputs.get(DefR.Reg);
2004       bool NonZero = (Opc == Hexagon::PS_true);
2005       uint32_t P = NonZero ? ConstantProperties::NonZero
2006                            : ConstantProperties::Zero;
2007       RC.add(P);
2008       Outputs.update(DefR.Reg, RC);
2009       break;
2010     }
2011 
2012     case Hexagon::A2_and:
2013     case Hexagon::A2_andir:
2014     case Hexagon::A2_andp:
2015     case Hexagon::A2_or:
2016     case Hexagon::A2_orir:
2017     case Hexagon::A2_orp:
2018     case Hexagon::A2_xor:
2019     case Hexagon::A2_xorp:
2020     {
2021       bool Eval = evaluateHexLogical(MI, Inputs, Outputs);
2022       if (!Eval)
2023         return false;
2024       break;
2025     }
2026 
2027     case Hexagon::A2_combineii:  // combine(#s8Ext, #s8)
2028     case Hexagon::A4_combineii:  // combine(#s8, #u6Ext)
2029     {
2030       uint64_t Hi = MI.getOperand(1).getImm();
2031       uint64_t Lo = MI.getOperand(2).getImm();
2032       uint64_t Res = (Hi << 32) | (Lo & 0xFFFFFFFF);
2033       IntegerType *Ty = Type::getInt64Ty(CX);
2034       const ConstantInt *CI = ConstantInt::get(Ty, Res, false);
2035       LatticeCell RC = Outputs.get(DefR.Reg);
2036       RC.add(CI);
2037       Outputs.update(DefR.Reg, RC);
2038       break;
2039     }
2040 
2041     case Hexagon::S2_setbit_i:
2042     {
2043       int64_t B = MI.getOperand(2).getImm();
2044       assert(B >=0 && B < 32);
2045       APInt A(32, (1ull << B), false);
2046       Register R(MI.getOperand(1));
2047       LatticeCell RC = Outputs.get(DefR.Reg);
2048       bool Eval = evaluateORri(R, A, Inputs, RC);
2049       if (!Eval)
2050         return false;
2051       Outputs.update(DefR.Reg, RC);
2052       break;
2053     }
2054 
2055     case Hexagon::C2_mux:
2056     case Hexagon::C2_muxir:
2057     case Hexagon::C2_muxri:
2058     case Hexagon::C2_muxii:
2059     {
2060       bool Eval = evaluateHexCondMove(MI, Inputs, Outputs);
2061       if (!Eval)
2062         return false;
2063       break;
2064     }
2065 
2066     case Hexagon::A2_sxtb:
2067     case Hexagon::A2_sxth:
2068     case Hexagon::A2_sxtw:
2069     case Hexagon::A2_zxtb:
2070     case Hexagon::A2_zxth:
2071     {
2072       bool Eval = evaluateHexExt(MI, Inputs, Outputs);
2073       if (!Eval)
2074         return false;
2075       break;
2076     }
2077 
2078     case Hexagon::S2_ct0:
2079     case Hexagon::S2_ct0p:
2080     case Hexagon::S2_ct1:
2081     case Hexagon::S2_ct1p:
2082     {
2083       using namespace Hexagon;
2084       bool Ones = (Opc == S2_ct1) || (Opc == S2_ct1p);
2085       Register R1(MI.getOperand(1));
2086       assert(Inputs.has(R1.Reg));
2087       LatticeCell T;
2088       bool Eval = evaluateCTBr(R1, !Ones, Ones, Inputs, T);
2089       if (!Eval)
2090         return false;
2091       // All of these instructions return a 32-bit value. The evaluate
2092       // will generate the same type as the operand, so truncate the
2093       // result if necessary.
2094       APInt C;
2095       LatticeCell RC = Outputs.get(DefR.Reg);
2096       for (unsigned i = 0; i < T.size(); ++i) {
2097         const Constant *CI = T.Values[i];
2098         if (constToInt(CI, C) && C.getBitWidth() > 32)
2099           CI = intToConst(C.trunc(32));
2100         RC.add(CI);
2101       }
2102       Outputs.update(DefR.Reg, RC);
2103       break;
2104     }
2105 
2106     case Hexagon::S2_cl0:
2107     case Hexagon::S2_cl0p:
2108     case Hexagon::S2_cl1:
2109     case Hexagon::S2_cl1p:
2110     case Hexagon::S2_clb:
2111     case Hexagon::S2_clbp:
2112     {
2113       using namespace Hexagon;
2114       bool OnlyZeros = (Opc == S2_cl0) || (Opc == S2_cl0p);
2115       bool OnlyOnes =  (Opc == S2_cl1) || (Opc == S2_cl1p);
2116       Register R1(MI.getOperand(1));
2117       assert(Inputs.has(R1.Reg));
2118       LatticeCell T;
2119       bool Eval = evaluateCLBr(R1, !OnlyOnes, !OnlyZeros, Inputs, T);
2120       if (!Eval)
2121         return false;
2122       // All of these instructions return a 32-bit value. The evaluate
2123       // will generate the same type as the operand, so truncate the
2124       // result if necessary.
2125       APInt C;
2126       LatticeCell RC = Outputs.get(DefR.Reg);
2127       for (unsigned i = 0; i < T.size(); ++i) {
2128         const Constant *CI = T.Values[i];
2129         if (constToInt(CI, C) && C.getBitWidth() > 32)
2130           CI = intToConst(C.trunc(32));
2131         RC.add(CI);
2132       }
2133       Outputs.update(DefR.Reg, RC);
2134       break;
2135     }
2136 
2137     case Hexagon::S4_extract:
2138     case Hexagon::S4_extractp:
2139     case Hexagon::S2_extractu:
2140     case Hexagon::S2_extractup:
2141     {
2142       bool Signed = (Opc == Hexagon::S4_extract) ||
2143                     (Opc == Hexagon::S4_extractp);
2144       Register R1(MI.getOperand(1));
2145       unsigned BW = getRegBitWidth(R1.Reg);
2146       unsigned Bits = MI.getOperand(2).getImm();
2147       unsigned Offset = MI.getOperand(3).getImm();
2148       LatticeCell RC = Outputs.get(DefR.Reg);
2149       if (Offset >= BW) {
2150         APInt Zero(BW, 0, false);
2151         RC.add(intToConst(Zero));
2152         break;
2153       }
2154       if (Offset+Bits > BW) {
2155         // If the requested bitfield extends beyond the most significant bit,
2156         // the extra bits are treated as 0s. To emulate this behavior, reduce
2157         // the number of requested bits, and make the extract unsigned.
2158         Bits = BW-Offset;
2159         Signed = false;
2160       }
2161       bool Eval = evaluateEXTRACTr(R1, BW, Bits, Offset, Signed, Inputs, RC);
2162       if (!Eval)
2163         return false;
2164       Outputs.update(DefR.Reg, RC);
2165       break;
2166     }
2167 
2168     case Hexagon::S2_vsplatrb:
2169     case Hexagon::S2_vsplatrh:
2170     // vabsh, vabsh:sat
2171     // vabsw, vabsw:sat
2172     // vconj:sat
2173     // vrndwh, vrndwh:sat
2174     // vsathb, vsathub, vsatwuh
2175     // vsxtbh, vsxthw
2176     // vtrunehb, vtrunohb
2177     // vzxtbh, vzxthw
2178     {
2179       bool Eval = evaluateHexVector1(MI, Inputs, Outputs);
2180       if (!Eval)
2181         return false;
2182       break;
2183     }
2184 
2185     // TODO:
2186     // A2_vaddh
2187     // A2_vaddhs
2188     // A2_vaddw
2189     // A2_vaddws
2190   }
2191 
2192   return true;
2193 }
2194 
2195 
2196 bool HexagonConstEvaluator::evaluate(const Register &R,
2197       const LatticeCell &Input, LatticeCell &Result) {
2198   if (!R.SubReg) {
2199     Result = Input;
2200     return true;
2201   }
2202   const TargetRegisterClass *RC = MRI->getRegClass(R.Reg);
2203   if (RC != &Hexagon::DoubleRegsRegClass)
2204     return false;
2205   if (R.SubReg != Hexagon::isub_lo && R.SubReg != Hexagon::isub_hi)
2206     return false;
2207 
2208   assert(!Input.isTop());
2209   if (Input.isBottom())
2210     return false;
2211 
2212   typedef ConstantProperties P;
2213   if (Input.isProperty()) {
2214     uint32_t Ps = Input.properties();
2215     if (Ps & (P::Zero|P::NaN)) {
2216       uint32_t Ns = (Ps & (P::Zero|P::NaN|P::SignProperties));
2217       Result.add(Ns);
2218       return true;
2219     }
2220     if (R.SubReg == Hexagon::isub_hi) {
2221       uint32_t Ns = (Ps & P::SignProperties);
2222       Result.add(Ns);
2223       return true;
2224     }
2225     return false;
2226   }
2227 
2228   // The Input cell contains some known values. Pick the word corresponding
2229   // to the subregister.
2230   APInt A;
2231   for (unsigned i = 0; i < Input.size(); ++i) {
2232     const Constant *C = Input.Values[i];
2233     if (!constToInt(C, A))
2234       return false;
2235     if (!A.isIntN(64))
2236       return false;
2237     uint64_t U = A.getZExtValue();
2238     if (R.SubReg == Hexagon::isub_hi)
2239       U >>= 32;
2240     U &= 0xFFFFFFFFULL;
2241     uint32_t U32 = Lo_32(U);
2242     int32_t V32;
2243     memcpy(&V32, &U32, sizeof V32);
2244     IntegerType *Ty = Type::getInt32Ty(CX);
2245     const ConstantInt *C32 = ConstantInt::get(Ty, static_cast<int64_t>(V32));
2246     Result.add(C32);
2247   }
2248   return true;
2249 }
2250 
2251 
2252 bool HexagonConstEvaluator::evaluate(const MachineInstr &BrI,
2253       const CellMap &Inputs, SetVector<const MachineBasicBlock*> &Targets,
2254       bool &FallsThru) {
2255   // We need to evaluate one branch at a time. TII::analyzeBranch checks
2256   // all the branches in a basic block at once, so we cannot use it.
2257   unsigned Opc = BrI.getOpcode();
2258   bool SimpleBranch = false;
2259   bool Negated = false;
2260   switch (Opc) {
2261     case Hexagon::J2_jumpf:
2262     case Hexagon::J2_jumpfnew:
2263     case Hexagon::J2_jumpfnewpt:
2264       Negated = true;
2265     case Hexagon::J2_jumpt:
2266     case Hexagon::J2_jumptnew:
2267     case Hexagon::J2_jumptnewpt:
2268       // Simple branch:  if([!]Pn) jump ...
2269       // i.e. Op0 = predicate, Op1 = branch target.
2270       SimpleBranch = true;
2271       break;
2272     case Hexagon::J2_jump:
2273       Targets.insert(BrI.getOperand(0).getMBB());
2274       FallsThru = false;
2275       return true;
2276     default:
2277 Undetermined:
2278       // If the branch is of unknown type, assume that all successors are
2279       // executable.
2280       FallsThru = !BrI.isUnconditionalBranch();
2281       return false;
2282   }
2283 
2284   if (SimpleBranch) {
2285     const MachineOperand &MD = BrI.getOperand(0);
2286     Register PR(MD);
2287     // If the condition operand has a subregister, this is not something
2288     // we currently recognize.
2289     if (PR.SubReg)
2290       goto Undetermined;
2291     assert(Inputs.has(PR.Reg));
2292     const LatticeCell &PredC = Inputs.get(PR.Reg);
2293     if (PredC.isBottom())
2294       goto Undetermined;
2295 
2296     uint32_t Props = PredC.properties();
2297     bool CTrue = false, CFalse = false;;
2298     if (Props & ConstantProperties::Zero)
2299       CFalse = true;
2300     else if (Props & ConstantProperties::NonZero)
2301       CTrue = true;
2302     // If the condition is not known to be either, bail out.
2303     if (!CTrue && !CFalse)
2304       goto Undetermined;
2305 
2306     const MachineBasicBlock *BranchTarget = BrI.getOperand(1).getMBB();
2307 
2308     FallsThru = false;
2309     if ((!Negated && CTrue) || (Negated && CFalse))
2310       Targets.insert(BranchTarget);
2311     else if ((!Negated && CFalse) || (Negated && CTrue))
2312       FallsThru = true;
2313     else
2314       goto Undetermined;
2315   }
2316 
2317   return true;
2318 }
2319 
2320 
2321 bool HexagonConstEvaluator::rewrite(MachineInstr &MI, const CellMap &Inputs) {
2322   if (MI.isBranch())
2323     return rewriteHexBranch(MI, Inputs);
2324 
2325   unsigned Opc = MI.getOpcode();
2326   switch (Opc) {
2327     default:
2328       break;
2329     case Hexagon::A2_tfrsi:
2330     case Hexagon::A2_tfrpi:
2331     case Hexagon::CONST32:
2332     case Hexagon::CONST64:
2333     case Hexagon::PS_true:
2334     case Hexagon::PS_false:
2335       return false;
2336   }
2337 
2338   unsigned NumOp = MI.getNumOperands();
2339   if (NumOp == 0)
2340     return false;
2341 
2342   bool AllDefs, Changed;
2343   Changed = rewriteHexConstDefs(MI, Inputs, AllDefs);
2344   // If not all defs have been rewritten (i.e. the instruction defines
2345   // a register that is not compile-time constant), then try to rewrite
2346   // register operands that are known to be constant with immediates.
2347   if (!AllDefs)
2348     Changed |= rewriteHexConstUses(MI, Inputs);
2349 
2350   return Changed;
2351 }
2352 
2353 
2354 unsigned HexagonConstEvaluator::getRegBitWidth(unsigned Reg) const {
2355   const TargetRegisterClass *RC = MRI->getRegClass(Reg);
2356   if (Hexagon::IntRegsRegClass.hasSubClassEq(RC))
2357     return 32;
2358   if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC))
2359     return 64;
2360   if (Hexagon::PredRegsRegClass.hasSubClassEq(RC))
2361     return 8;
2362   llvm_unreachable("Invalid register");
2363   return 0;
2364 }
2365 
2366 
2367 uint32_t HexagonConstEvaluator::getCmp(unsigned Opc) {
2368   switch (Opc) {
2369     case Hexagon::C2_cmpeq:
2370     case Hexagon::C2_cmpeqp:
2371     case Hexagon::A4_cmpbeq:
2372     case Hexagon::A4_cmpheq:
2373     case Hexagon::A4_cmpbeqi:
2374     case Hexagon::A4_cmpheqi:
2375     case Hexagon::C2_cmpeqi:
2376     case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
2377     case Hexagon::J4_cmpeqn1_t_jumpnv_t:
2378     case Hexagon::J4_cmpeqi_t_jumpnv_nt:
2379     case Hexagon::J4_cmpeqi_t_jumpnv_t:
2380     case Hexagon::J4_cmpeq_t_jumpnv_nt:
2381     case Hexagon::J4_cmpeq_t_jumpnv_t:
2382       return Comparison::EQ;
2383 
2384     case Hexagon::C4_cmpneq:
2385     case Hexagon::C4_cmpneqi:
2386     case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
2387     case Hexagon::J4_cmpeqn1_f_jumpnv_t:
2388     case Hexagon::J4_cmpeqi_f_jumpnv_nt:
2389     case Hexagon::J4_cmpeqi_f_jumpnv_t:
2390     case Hexagon::J4_cmpeq_f_jumpnv_nt:
2391     case Hexagon::J4_cmpeq_f_jumpnv_t:
2392       return Comparison::NE;
2393 
2394     case Hexagon::C2_cmpgt:
2395     case Hexagon::C2_cmpgtp:
2396     case Hexagon::A4_cmpbgt:
2397     case Hexagon::A4_cmphgt:
2398     case Hexagon::A4_cmpbgti:
2399     case Hexagon::A4_cmphgti:
2400     case Hexagon::C2_cmpgti:
2401     case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
2402     case Hexagon::J4_cmpgtn1_t_jumpnv_t:
2403     case Hexagon::J4_cmpgti_t_jumpnv_nt:
2404     case Hexagon::J4_cmpgti_t_jumpnv_t:
2405     case Hexagon::J4_cmpgt_t_jumpnv_nt:
2406     case Hexagon::J4_cmpgt_t_jumpnv_t:
2407       return Comparison::GTs;
2408 
2409     case Hexagon::C4_cmplte:
2410     case Hexagon::C4_cmpltei:
2411     case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
2412     case Hexagon::J4_cmpgtn1_f_jumpnv_t:
2413     case Hexagon::J4_cmpgti_f_jumpnv_nt:
2414     case Hexagon::J4_cmpgti_f_jumpnv_t:
2415     case Hexagon::J4_cmpgt_f_jumpnv_nt:
2416     case Hexagon::J4_cmpgt_f_jumpnv_t:
2417       return Comparison::LEs;
2418 
2419     case Hexagon::C2_cmpgtu:
2420     case Hexagon::C2_cmpgtup:
2421     case Hexagon::A4_cmpbgtu:
2422     case Hexagon::A4_cmpbgtui:
2423     case Hexagon::A4_cmphgtu:
2424     case Hexagon::A4_cmphgtui:
2425     case Hexagon::C2_cmpgtui:
2426     case Hexagon::J4_cmpgtui_t_jumpnv_nt:
2427     case Hexagon::J4_cmpgtui_t_jumpnv_t:
2428     case Hexagon::J4_cmpgtu_t_jumpnv_nt:
2429     case Hexagon::J4_cmpgtu_t_jumpnv_t:
2430       return Comparison::GTu;
2431 
2432     case Hexagon::J4_cmpltu_f_jumpnv_nt:
2433     case Hexagon::J4_cmpltu_f_jumpnv_t:
2434       return Comparison::GEu;
2435 
2436     case Hexagon::J4_cmpltu_t_jumpnv_nt:
2437     case Hexagon::J4_cmpltu_t_jumpnv_t:
2438       return Comparison::LTu;
2439 
2440     case Hexagon::J4_cmplt_f_jumpnv_nt:
2441     case Hexagon::J4_cmplt_f_jumpnv_t:
2442       return Comparison::GEs;
2443 
2444     case Hexagon::C4_cmplteu:
2445     case Hexagon::C4_cmplteui:
2446     case Hexagon::J4_cmpgtui_f_jumpnv_nt:
2447     case Hexagon::J4_cmpgtui_f_jumpnv_t:
2448     case Hexagon::J4_cmpgtu_f_jumpnv_nt:
2449     case Hexagon::J4_cmpgtu_f_jumpnv_t:
2450       return Comparison::LEu;
2451 
2452     case Hexagon::J4_cmplt_t_jumpnv_nt:
2453     case Hexagon::J4_cmplt_t_jumpnv_t:
2454       return Comparison::LTs;
2455 
2456     default:
2457       break;
2458   }
2459   return Comparison::Unk;
2460 }
2461 
2462 
2463 APInt HexagonConstEvaluator::getCmpImm(unsigned Opc, unsigned OpX,
2464       const MachineOperand &MO) {
2465   bool Signed = false;
2466   switch (Opc) {
2467     case Hexagon::A4_cmpbgtui:   // u7
2468     case Hexagon::A4_cmphgtui:   // u7
2469       break;
2470     case Hexagon::A4_cmpheqi:    // s8
2471     case Hexagon::C4_cmpneqi:   // s8
2472       Signed = true;
2473     case Hexagon::A4_cmpbeqi:    // u8
2474       break;
2475     case Hexagon::C2_cmpgtui:      // u9
2476     case Hexagon::C4_cmplteui:  // u9
2477       break;
2478     case Hexagon::C2_cmpeqi:       // s10
2479     case Hexagon::C2_cmpgti:       // s10
2480     case Hexagon::C4_cmpltei:   // s10
2481       Signed = true;
2482       break;
2483     case Hexagon::J4_cmpeqi_f_jumpnv_nt:   // u5
2484     case Hexagon::J4_cmpeqi_f_jumpnv_t:    // u5
2485     case Hexagon::J4_cmpeqi_t_jumpnv_nt:   // u5
2486     case Hexagon::J4_cmpeqi_t_jumpnv_t:    // u5
2487     case Hexagon::J4_cmpgti_f_jumpnv_nt:   // u5
2488     case Hexagon::J4_cmpgti_f_jumpnv_t:    // u5
2489     case Hexagon::J4_cmpgti_t_jumpnv_nt:   // u5
2490     case Hexagon::J4_cmpgti_t_jumpnv_t:    // u5
2491     case Hexagon::J4_cmpgtui_f_jumpnv_nt:  // u5
2492     case Hexagon::J4_cmpgtui_f_jumpnv_t:   // u5
2493     case Hexagon::J4_cmpgtui_t_jumpnv_nt:  // u5
2494     case Hexagon::J4_cmpgtui_t_jumpnv_t:   // u5
2495       break;
2496     default:
2497       llvm_unreachable("Unhandled instruction");
2498       break;
2499   }
2500 
2501   uint64_t Val = MO.getImm();
2502   return APInt(32, Val, Signed);
2503 }
2504 
2505 
2506 void HexagonConstEvaluator::replaceWithNop(MachineInstr &MI) {
2507   MI.setDesc(HII.get(Hexagon::A2_nop));
2508   while (MI.getNumOperands() > 0)
2509     MI.RemoveOperand(0);
2510 }
2511 
2512 
2513 bool HexagonConstEvaluator::evaluateHexRSEQ32(Register RL, Register RH,
2514       const CellMap &Inputs, LatticeCell &Result) {
2515   assert(Inputs.has(RL.Reg) && Inputs.has(RH.Reg));
2516   LatticeCell LSL, LSH;
2517   if (!getCell(RL, Inputs, LSL) || !getCell(RH, Inputs, LSH))
2518     return false;
2519   if (LSL.isProperty() || LSH.isProperty())
2520     return false;
2521 
2522   unsigned LN = LSL.size(), HN = LSH.size();
2523   SmallVector<APInt,4> LoVs(LN), HiVs(HN);
2524   for (unsigned i = 0; i < LN; ++i) {
2525     bool Eval = constToInt(LSL.Values[i], LoVs[i]);
2526     if (!Eval)
2527       return false;
2528     assert(LoVs[i].getBitWidth() == 32);
2529   }
2530   for (unsigned i = 0; i < HN; ++i) {
2531     bool Eval = constToInt(LSH.Values[i], HiVs[i]);
2532     if (!Eval)
2533       return false;
2534     assert(HiVs[i].getBitWidth() == 32);
2535   }
2536 
2537   for (unsigned i = 0; i < HiVs.size(); ++i) {
2538     APInt HV = HiVs[i].zextOrSelf(64) << 32;
2539     for (unsigned j = 0; j < LoVs.size(); ++j) {
2540       APInt LV = LoVs[j].zextOrSelf(64);
2541       const Constant *C = intToConst(HV | LV);
2542       Result.add(C);
2543       if (Result.isBottom())
2544         return false;
2545     }
2546   }
2547   return !Result.isBottom();
2548 }
2549 
2550 
2551 bool HexagonConstEvaluator::evaluateHexCompare(const MachineInstr &MI,
2552       const CellMap &Inputs, CellMap &Outputs) {
2553   unsigned Opc = MI.getOpcode();
2554   bool Classic = false;
2555   switch (Opc) {
2556     case Hexagon::C2_cmpeq:
2557     case Hexagon::C2_cmpeqp:
2558     case Hexagon::C2_cmpgt:
2559     case Hexagon::C2_cmpgtp:
2560     case Hexagon::C2_cmpgtu:
2561     case Hexagon::C2_cmpgtup:
2562     case Hexagon::C2_cmpeqi:
2563     case Hexagon::C2_cmpgti:
2564     case Hexagon::C2_cmpgtui:
2565       // Classic compare:  Dst0 = CMP Src1, Src2
2566       Classic = true;
2567       break;
2568     default:
2569       // Not handling other compare instructions now.
2570       return false;
2571   }
2572 
2573   if (Classic) {
2574     const MachineOperand &Src1 = MI.getOperand(1);
2575     const MachineOperand &Src2 = MI.getOperand(2);
2576 
2577     bool Result;
2578     unsigned Opc = MI.getOpcode();
2579     bool Computed = evaluateHexCompare2(Opc, Src1, Src2, Inputs, Result);
2580     if (Computed) {
2581       // Only create a zero/non-zero cell. At this time there isn't really
2582       // much need for specific values.
2583       Register DefR(MI.getOperand(0));
2584       LatticeCell L = Outputs.get(DefR.Reg);
2585       uint32_t P = Result ? ConstantProperties::NonZero
2586                           : ConstantProperties::Zero;
2587       L.add(P);
2588       Outputs.update(DefR.Reg, L);
2589       return true;
2590     }
2591   }
2592 
2593   return false;
2594 }
2595 
2596 
2597 bool HexagonConstEvaluator::evaluateHexCompare2(unsigned Opc,
2598       const MachineOperand &Src1, const MachineOperand &Src2,
2599       const CellMap &Inputs, bool &Result) {
2600   uint32_t Cmp = getCmp(Opc);
2601   bool Reg1 = Src1.isReg(), Reg2 = Src2.isReg();
2602   bool Imm1 = Src1.isImm(), Imm2 = Src2.isImm();
2603   if (Reg1) {
2604     Register R1(Src1);
2605     if (Reg2) {
2606       Register R2(Src2);
2607       return evaluateCMPrr(Cmp, R1, R2, Inputs, Result);
2608     } else if (Imm2) {
2609       APInt A2 = getCmpImm(Opc, 2, Src2);
2610       return evaluateCMPri(Cmp, R1, A2, Inputs, Result);
2611     }
2612   } else if (Imm1) {
2613     APInt A1 = getCmpImm(Opc, 1, Src1);
2614     if (Reg2) {
2615       Register R2(Src2);
2616       uint32_t NegCmp = Comparison::negate(Cmp);
2617       return evaluateCMPri(NegCmp, R2, A1, Inputs, Result);
2618     } else if (Imm2) {
2619       APInt A2 = getCmpImm(Opc, 2, Src2);
2620       return evaluateCMPii(Cmp, A1, A2, Result);
2621     }
2622   }
2623   // Unknown kind of comparison.
2624   return false;
2625 }
2626 
2627 
2628 bool HexagonConstEvaluator::evaluateHexLogical(const MachineInstr &MI,
2629       const CellMap &Inputs, CellMap &Outputs) {
2630   unsigned Opc = MI.getOpcode();
2631   if (MI.getNumOperands() != 3)
2632     return false;
2633   const MachineOperand &Src1 = MI.getOperand(1);
2634   const MachineOperand &Src2 = MI.getOperand(2);
2635   Register R1(Src1);
2636   bool Eval = false;
2637   LatticeCell RC;
2638   switch (Opc) {
2639     default:
2640       return false;
2641     case Hexagon::A2_and:
2642     case Hexagon::A2_andp:
2643       Eval = evaluateANDrr(R1, Register(Src2), Inputs, RC);
2644       break;
2645     case Hexagon::A2_andir: {
2646       APInt A(32, Src2.getImm(), true);
2647       Eval = evaluateANDri(R1, A, Inputs, RC);
2648       break;
2649     }
2650     case Hexagon::A2_or:
2651     case Hexagon::A2_orp:
2652       Eval = evaluateORrr(R1, Register(Src2), Inputs, RC);
2653       break;
2654     case Hexagon::A2_orir: {
2655       APInt A(32, Src2.getImm(), true);
2656       Eval = evaluateORri(R1, A, Inputs, RC);
2657       break;
2658     }
2659     case Hexagon::A2_xor:
2660     case Hexagon::A2_xorp:
2661       Eval = evaluateXORrr(R1, Register(Src2), Inputs, RC);
2662       break;
2663   }
2664   if (Eval) {
2665     Register DefR(MI.getOperand(0));
2666     Outputs.update(DefR.Reg, RC);
2667   }
2668   return Eval;
2669 }
2670 
2671 
2672 bool HexagonConstEvaluator::evaluateHexCondMove(const MachineInstr &MI,
2673       const CellMap &Inputs, CellMap &Outputs) {
2674   // Dst0 = Cond1 ? Src2 : Src3
2675   Register CR(MI.getOperand(1));
2676   assert(Inputs.has(CR.Reg));
2677   LatticeCell LS;
2678   if (!getCell(CR, Inputs, LS))
2679     return false;
2680   uint32_t Ps = LS.properties();
2681   unsigned TakeOp;
2682   if (Ps & ConstantProperties::Zero)
2683     TakeOp = 3;
2684   else if (Ps & ConstantProperties::NonZero)
2685     TakeOp = 2;
2686   else
2687     return false;
2688 
2689   const MachineOperand &ValOp = MI.getOperand(TakeOp);
2690   Register DefR(MI.getOperand(0));
2691   LatticeCell RC = Outputs.get(DefR.Reg);
2692 
2693   if (ValOp.isImm()) {
2694     int64_t V = ValOp.getImm();
2695     unsigned W = getRegBitWidth(DefR.Reg);
2696     APInt A(W, V, true);
2697     const Constant *C = intToConst(A);
2698     RC.add(C);
2699     Outputs.update(DefR.Reg, RC);
2700     return true;
2701   }
2702   if (ValOp.isReg()) {
2703     Register R(ValOp);
2704     const LatticeCell &LR = Inputs.get(R.Reg);
2705     LatticeCell LSR;
2706     if (!evaluate(R, LR, LSR))
2707       return false;
2708     RC.meet(LSR);
2709     Outputs.update(DefR.Reg, RC);
2710     return true;
2711   }
2712   return false;
2713 }
2714 
2715 
2716 bool HexagonConstEvaluator::evaluateHexExt(const MachineInstr &MI,
2717       const CellMap &Inputs, CellMap &Outputs) {
2718   // Dst0 = ext R1
2719   Register R1(MI.getOperand(1));
2720   assert(Inputs.has(R1.Reg));
2721 
2722   unsigned Opc = MI.getOpcode();
2723   unsigned Bits;
2724   switch (Opc) {
2725     case Hexagon::A2_sxtb:
2726     case Hexagon::A2_zxtb:
2727       Bits = 8;
2728       break;
2729     case Hexagon::A2_sxth:
2730     case Hexagon::A2_zxth:
2731       Bits = 16;
2732       break;
2733     case Hexagon::A2_sxtw:
2734       Bits = 32;
2735       break;
2736   }
2737 
2738   bool Signed = false;
2739   switch (Opc) {
2740     case Hexagon::A2_sxtb:
2741     case Hexagon::A2_sxth:
2742     case Hexagon::A2_sxtw:
2743       Signed = true;
2744       break;
2745   }
2746 
2747   Register DefR(MI.getOperand(0));
2748   unsigned BW = getRegBitWidth(DefR.Reg);
2749   LatticeCell RC = Outputs.get(DefR.Reg);
2750   bool Eval = Signed ? evaluateSEXTr(R1, BW, Bits, Inputs, RC)
2751                      : evaluateZEXTr(R1, BW, Bits, Inputs, RC);
2752   if (!Eval)
2753     return false;
2754   Outputs.update(DefR.Reg, RC);
2755   return true;
2756 }
2757 
2758 
2759 bool HexagonConstEvaluator::evaluateHexVector1(const MachineInstr &MI,
2760       const CellMap &Inputs, CellMap &Outputs) {
2761   // DefR = op R1
2762   Register DefR(MI.getOperand(0));
2763   Register R1(MI.getOperand(1));
2764   assert(Inputs.has(R1.Reg));
2765   LatticeCell RC = Outputs.get(DefR.Reg);
2766   bool Eval;
2767 
2768   unsigned Opc = MI.getOpcode();
2769   switch (Opc) {
2770     case Hexagon::S2_vsplatrb:
2771       // Rd = 4 times Rs:0..7
2772       Eval = evaluateSplatr(R1, 8, 4, Inputs, RC);
2773       break;
2774     case Hexagon::S2_vsplatrh:
2775       // Rdd = 4 times Rs:0..15
2776       Eval = evaluateSplatr(R1, 16, 4, Inputs, RC);
2777       break;
2778     default:
2779       return false;
2780   }
2781 
2782   if (!Eval)
2783     return false;
2784   Outputs.update(DefR.Reg, RC);
2785   return true;
2786 }
2787 
2788 
2789 bool HexagonConstEvaluator::rewriteHexConstDefs(MachineInstr &MI,
2790       const CellMap &Inputs, bool &AllDefs) {
2791   AllDefs = false;
2792 
2793   // Some diagnostics.
2794   // DEBUG({...}) gets confused with all this code as an argument.
2795 #ifndef NDEBUG
2796   bool Debugging = llvm::DebugFlag &&
2797                    llvm::isCurrentDebugType(DEBUG_TYPE);
2798   if (Debugging) {
2799     bool Const = true, HasUse = false;
2800     for (const MachineOperand &MO : MI.operands()) {
2801       if (!MO.isReg() || !MO.isUse() || MO.isImplicit())
2802         continue;
2803       Register R(MO);
2804       if (!TargetRegisterInfo::isVirtualRegister(R.Reg))
2805         continue;
2806       HasUse = true;
2807       // PHIs can legitimately have "top" cells after propagation.
2808       if (!MI.isPHI() && !Inputs.has(R.Reg)) {
2809         dbgs() << "Top " << PrintReg(R.Reg, &HRI, R.SubReg)
2810                << " in MI: " << MI;
2811         continue;
2812       }
2813       const LatticeCell &L = Inputs.get(R.Reg);
2814       Const &= L.isSingle();
2815       if (!Const)
2816         break;
2817     }
2818     if (HasUse && Const) {
2819       if (!MI.isCopy()) {
2820         dbgs() << "CONST: " << MI;
2821         for (const MachineOperand &MO : MI.operands()) {
2822           if (!MO.isReg() || !MO.isUse() || MO.isImplicit())
2823             continue;
2824           unsigned R = MO.getReg();
2825           dbgs() << PrintReg(R, &TRI) << ": " << Inputs.get(R) << "\n";
2826         }
2827       }
2828     }
2829   }
2830 #endif
2831 
2832   // Avoid generating TFRIs for register transfers---this will keep the
2833   // coalescing opportunities.
2834   if (MI.isCopy())
2835     return false;
2836 
2837   // Collect all virtual register-def operands.
2838   SmallVector<unsigned,2> DefRegs;
2839   for (const MachineOperand &MO : MI.operands()) {
2840     if (!MO.isReg() || !MO.isDef())
2841       continue;
2842     unsigned R = MO.getReg();
2843     if (!TargetRegisterInfo::isVirtualRegister(R))
2844       continue;
2845     assert(!MO.getSubReg());
2846     assert(Inputs.has(R));
2847     DefRegs.push_back(R);
2848   }
2849 
2850   MachineBasicBlock &B = *MI.getParent();
2851   const DebugLoc &DL = MI.getDebugLoc();
2852   unsigned ChangedNum = 0;
2853 #ifndef NDEBUG
2854   SmallVector<const MachineInstr*,4> NewInstrs;
2855 #endif
2856 
2857   // For each defined register, if it is a constant, create an instruction
2858   //   NewR = const
2859   // and replace all uses of the defined register with NewR.
2860   for (unsigned i = 0, n = DefRegs.size(); i < n; ++i) {
2861     unsigned R = DefRegs[i];
2862     const LatticeCell &L = Inputs.get(R);
2863     if (L.isBottom())
2864       continue;
2865     const TargetRegisterClass *RC = MRI->getRegClass(R);
2866     MachineBasicBlock::iterator At = MI.getIterator();
2867 
2868     if (!L.isSingle()) {
2869       // If this a zero/non-zero cell, we can fold a definition
2870       // of a predicate register.
2871       typedef ConstantProperties P;
2872       uint64_t Ps = L.properties();
2873       if (!(Ps & (P::Zero|P::NonZero)))
2874         continue;
2875       const TargetRegisterClass *PredRC = &Hexagon::PredRegsRegClass;
2876       if (RC != PredRC)
2877         continue;
2878       const MCInstrDesc *NewD = (Ps & P::Zero) ?
2879         &HII.get(Hexagon::PS_false) :
2880         &HII.get(Hexagon::PS_true);
2881       unsigned NewR = MRI->createVirtualRegister(PredRC);
2882       const MachineInstrBuilder &MIB = BuildMI(B, At, DL, *NewD, NewR);
2883       (void)MIB;
2884 #ifndef NDEBUG
2885       NewInstrs.push_back(&*MIB);
2886 #endif
2887       replaceAllRegUsesWith(R, NewR);
2888     } else {
2889       // This cell has a single value.
2890       APInt A;
2891       if (!constToInt(L.Value, A) || !A.isSignedIntN(64))
2892         continue;
2893       const TargetRegisterClass *NewRC;
2894       const MCInstrDesc *NewD;
2895 
2896       unsigned W = getRegBitWidth(R);
2897       int64_t V = A.getSExtValue();
2898       assert(W == 32 || W == 64);
2899       if (W == 32)
2900         NewRC = &Hexagon::IntRegsRegClass;
2901       else
2902         NewRC = &Hexagon::DoubleRegsRegClass;
2903       unsigned NewR = MRI->createVirtualRegister(NewRC);
2904       const MachineInstr *NewMI;
2905 
2906       if (W == 32) {
2907         NewD = &HII.get(Hexagon::A2_tfrsi);
2908         NewMI = BuildMI(B, At, DL, *NewD, NewR)
2909                   .addImm(V);
2910       } else {
2911         if (A.isSignedIntN(8)) {
2912           NewD = &HII.get(Hexagon::A2_tfrpi);
2913           NewMI = BuildMI(B, At, DL, *NewD, NewR)
2914                     .addImm(V);
2915         } else {
2916           int32_t Hi = V >> 32;
2917           int32_t Lo = V & 0xFFFFFFFFLL;
2918           if (isInt<8>(Hi) && isInt<8>(Lo)) {
2919             NewD = &HII.get(Hexagon::A2_combineii);
2920             NewMI = BuildMI(B, At, DL, *NewD, NewR)
2921                       .addImm(Hi)
2922                       .addImm(Lo);
2923           } else {
2924             NewD = &HII.get(Hexagon::CONST64);
2925             NewMI = BuildMI(B, At, DL, *NewD, NewR)
2926                       .addImm(V);
2927           }
2928         }
2929       }
2930       (void)NewMI;
2931 #ifndef NDEBUG
2932       NewInstrs.push_back(NewMI);
2933 #endif
2934       replaceAllRegUsesWith(R, NewR);
2935     }
2936     ChangedNum++;
2937   }
2938 
2939   DEBUG({
2940     if (!NewInstrs.empty()) {
2941       MachineFunction &MF = *MI.getParent()->getParent();
2942       dbgs() << "In function: " << MF.getFunction()->getName() << "\n";
2943       dbgs() << "Rewrite: for " << MI << "  created " << *NewInstrs[0];
2944       for (unsigned i = 1; i < NewInstrs.size(); ++i)
2945         dbgs() << "          " << *NewInstrs[i];
2946     }
2947   });
2948 
2949   AllDefs = (ChangedNum == DefRegs.size());
2950   return ChangedNum > 0;
2951 }
2952 
2953 
2954 bool HexagonConstEvaluator::rewriteHexConstUses(MachineInstr &MI,
2955       const CellMap &Inputs) {
2956   bool Changed = false;
2957   unsigned Opc = MI.getOpcode();
2958   MachineBasicBlock &B = *MI.getParent();
2959   const DebugLoc &DL = MI.getDebugLoc();
2960   MachineBasicBlock::iterator At = MI.getIterator();
2961   MachineInstr *NewMI = NULL;
2962 
2963   switch (Opc) {
2964     case Hexagon::M2_maci:
2965     // Convert DefR += mpyi(R2, R3)
2966     //   to   DefR += mpyi(R, #imm),
2967     //   or   DefR -= mpyi(R, #imm).
2968     {
2969       Register DefR(MI.getOperand(0));
2970       assert(!DefR.SubReg);
2971       Register R2(MI.getOperand(2));
2972       Register R3(MI.getOperand(3));
2973       assert(Inputs.has(R2.Reg) && Inputs.has(R3.Reg));
2974       LatticeCell LS2, LS3;
2975       // It is enough to get one of the input cells, since we will only try
2976       // to replace one argument---whichever happens to be a single constant.
2977       bool HasC2 = getCell(R2, Inputs, LS2), HasC3 = getCell(R3, Inputs, LS3);
2978       if (!HasC2 && !HasC3)
2979         return false;
2980       bool Zero = ((HasC2 && (LS2.properties() & ConstantProperties::Zero)) ||
2981                    (HasC3 && (LS3.properties() & ConstantProperties::Zero)));
2982       // If one of the operands is zero, eliminate the multiplication.
2983       if (Zero) {
2984         // DefR == R1 (tied operands).
2985         MachineOperand &Acc = MI.getOperand(1);
2986         Register R1(Acc);
2987         unsigned NewR = R1.Reg;
2988         if (R1.SubReg) {
2989           // Generate COPY. FIXME: Replace with the register:subregister.
2990           const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
2991           NewR = MRI->createVirtualRegister(RC);
2992           NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
2993                     .addReg(R1.Reg, getRegState(Acc), R1.SubReg);
2994         }
2995         replaceAllRegUsesWith(DefR.Reg, NewR);
2996         MRI->clearKillFlags(NewR);
2997         Changed = true;
2998         break;
2999       }
3000 
3001       bool Swap = false;
3002       if (!LS3.isSingle()) {
3003         if (!LS2.isSingle())
3004           return false;
3005         Swap = true;
3006       }
3007       const LatticeCell &LI = Swap ? LS2 : LS3;
3008       const MachineOperand &OpR2 = Swap ? MI.getOperand(3)
3009                                         : MI.getOperand(2);
3010       // LI is single here.
3011       APInt A;
3012       if (!constToInt(LI.Value, A) || !A.isSignedIntN(8))
3013         return false;
3014       int64_t V = A.getSExtValue();
3015       const MCInstrDesc &D = (V >= 0) ? HII.get(Hexagon::M2_macsip)
3016                                       : HII.get(Hexagon::M2_macsin);
3017       if (V < 0)
3018         V = -V;
3019       const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3020       unsigned NewR = MRI->createVirtualRegister(RC);
3021       const MachineOperand &Src1 = MI.getOperand(1);
3022       NewMI = BuildMI(B, At, DL, D, NewR)
3023                 .addReg(Src1.getReg(), getRegState(Src1), Src1.getSubReg())
3024                 .addReg(OpR2.getReg(), getRegState(OpR2), OpR2.getSubReg())
3025                 .addImm(V);
3026       replaceAllRegUsesWith(DefR.Reg, NewR);
3027       Changed = true;
3028       break;
3029     }
3030 
3031     case Hexagon::A2_and:
3032     {
3033       Register R1(MI.getOperand(1));
3034       Register R2(MI.getOperand(2));
3035       assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3036       LatticeCell LS1, LS2;
3037       unsigned CopyOf = 0;
3038       // Check if any of the operands is -1 (i.e. all bits set).
3039       if (getCell(R1, Inputs, LS1) && LS1.isSingle()) {
3040         APInt M1;
3041         if (constToInt(LS1.Value, M1) && !~M1)
3042           CopyOf = 2;
3043       }
3044       else if (getCell(R2, Inputs, LS2) && LS2.isSingle()) {
3045         APInt M1;
3046         if (constToInt(LS2.Value, M1) && !~M1)
3047           CopyOf = 1;
3048       }
3049       if (!CopyOf)
3050         return false;
3051       MachineOperand &SO = MI.getOperand(CopyOf);
3052       Register SR(SO);
3053       Register DefR(MI.getOperand(0));
3054       unsigned NewR = SR.Reg;
3055       if (SR.SubReg) {
3056         const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3057         NewR = MRI->createVirtualRegister(RC);
3058         NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3059                   .addReg(SR.Reg, getRegState(SO), SR.SubReg);
3060       }
3061       replaceAllRegUsesWith(DefR.Reg, NewR);
3062       MRI->clearKillFlags(NewR);
3063       Changed = true;
3064     }
3065     break;
3066 
3067     case Hexagon::A2_or:
3068     {
3069       Register R1(MI.getOperand(1));
3070       Register R2(MI.getOperand(2));
3071       assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3072       LatticeCell LS1, LS2;
3073       unsigned CopyOf = 0;
3074       typedef ConstantProperties P;
3075       if (getCell(R1, Inputs, LS1) && (LS1.properties() & P::Zero))
3076         CopyOf = 2;
3077       else if (getCell(R2, Inputs, LS2) && (LS2.properties() & P::Zero))
3078         CopyOf = 1;
3079       if (!CopyOf)
3080         return false;
3081       MachineOperand &SO = MI.getOperand(CopyOf);
3082       Register SR(SO);
3083       Register DefR(MI.getOperand(0));
3084       unsigned NewR = SR.Reg;
3085       if (SR.SubReg) {
3086         const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3087         NewR = MRI->createVirtualRegister(RC);
3088         NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3089                   .addReg(SR.Reg, getRegState(SO), SR.SubReg);
3090       }
3091       replaceAllRegUsesWith(DefR.Reg, NewR);
3092       MRI->clearKillFlags(NewR);
3093       Changed = true;
3094     }
3095     break;
3096   }
3097 
3098   if (NewMI) {
3099     // clear all the kill flags of this new instruction.
3100     for (MachineOperand &MO : NewMI->operands())
3101       if (MO.isReg() && MO.isUse())
3102         MO.setIsKill(false);
3103   }
3104 
3105   DEBUG({
3106     if (NewMI) {
3107       dbgs() << "Rewrite: for " << MI;
3108       if (NewMI != &MI)
3109         dbgs() << "  created " << *NewMI;
3110       else
3111         dbgs() << "  modified the instruction itself and created:" << *NewMI;
3112     }
3113   });
3114 
3115   return Changed;
3116 }
3117 
3118 
3119 void HexagonConstEvaluator::replaceAllRegUsesWith(unsigned FromReg,
3120       unsigned ToReg) {
3121   assert(TargetRegisterInfo::isVirtualRegister(FromReg));
3122   assert(TargetRegisterInfo::isVirtualRegister(ToReg));
3123   for (auto I = MRI->use_begin(FromReg), E = MRI->use_end(); I != E;) {
3124     MachineOperand &O = *I;
3125     ++I;
3126     O.setReg(ToReg);
3127   }
3128 }
3129 
3130 
3131 bool HexagonConstEvaluator::rewriteHexBranch(MachineInstr &BrI,
3132       const CellMap &Inputs) {
3133   MachineBasicBlock &B = *BrI.getParent();
3134   unsigned NumOp = BrI.getNumOperands();
3135   if (!NumOp)
3136     return false;
3137 
3138   bool FallsThru;
3139   SetVector<const MachineBasicBlock*> Targets;
3140   bool Eval = evaluate(BrI, Inputs, Targets, FallsThru);
3141   unsigned NumTargets = Targets.size();
3142   if (!Eval || NumTargets > 1 || (NumTargets == 1 && FallsThru))
3143     return false;
3144   if (BrI.getOpcode() == Hexagon::J2_jump)
3145     return false;
3146 
3147   DEBUG(dbgs() << "Rewrite(BB#" << B.getNumber() << "):" << BrI);
3148   bool Rewritten = false;
3149   if (NumTargets > 0) {
3150     assert(!FallsThru && "This should have been checked before");
3151     // MIB.addMBB needs non-const pointer.
3152     MachineBasicBlock *TargetB = const_cast<MachineBasicBlock*>(Targets[0]);
3153     bool Moot = B.isLayoutSuccessor(TargetB);
3154     if (!Moot) {
3155       // If we build a branch here, we must make sure that it won't be
3156       // erased as "non-executable". We can't mark any new instructions
3157       // as executable here, so we need to overwrite the BrI, which we
3158       // know is executable.
3159       const MCInstrDesc &JD = HII.get(Hexagon::J2_jump);
3160       auto NI = BuildMI(B, BrI.getIterator(), BrI.getDebugLoc(), JD)
3161                   .addMBB(TargetB);
3162       BrI.setDesc(JD);
3163       while (BrI.getNumOperands() > 0)
3164         BrI.RemoveOperand(0);
3165       // This ensures that all implicit operands (e.g. %R31<imp-def>, etc)
3166       // are present in the rewritten branch.
3167       for (auto &Op : NI->operands())
3168         BrI.addOperand(Op);
3169       NI->eraseFromParent();
3170       Rewritten = true;
3171     }
3172   }
3173 
3174   // Do not erase instructions. A newly created instruction could get
3175   // the same address as an instruction marked as executable during the
3176   // propagation.
3177   if (!Rewritten)
3178     replaceWithNop(BrI);
3179   return true;
3180 }
3181 
3182 
3183 // --------------------------------------------------------------------
3184 FunctionPass *llvm::createHexagonConstPropagationPass() {
3185   return new HexagonConstPropagation();
3186 }
3187 
3188