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