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