blob: 8a2444a255fb457fb3fee1898b428824ff272279 [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();
Krzysztof Parzyszek6400dec2016-07-28 20:25:21 +00001595 (void)BW;
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +00001596 assert(Width >= Bits && BW >= Bits);
1597 APInt Mask = APInt::getLowBitsSet(Width, Bits);
1598 Result = A1.zextOrTrunc(Width) & Mask;
1599 return true;
1600}
1601
1602
1603bool MachineConstEvaluator::evaluateSEXTr(const Register &R1, unsigned Width,
1604 unsigned Bits, const CellMap &Inputs, LatticeCell &Result) {
1605 assert(Inputs.has(R1.Reg));
1606 LatticeCell LS1;
1607 if (!getCell(R1, Inputs, LS1))
1608 return false;
1609 if (LS1.isBottom() || LS1.isProperty())
1610 return false;
1611
1612 APInt A, XA;
1613 for (unsigned i = 0; i < LS1.size(); ++i) {
1614 bool Eval = constToInt(LS1.Values[i], A) &&
1615 evaluateSEXTi(A, Width, Bits, XA);
1616 if (!Eval)
1617 return false;
1618 const Constant *C = intToConst(XA);
1619 Result.add(C);
1620 }
1621 return true;
1622}
1623
1624
1625bool MachineConstEvaluator::evaluateSEXTi(const APInt &A1, unsigned Width,
1626 unsigned Bits, APInt &Result) {
1627 unsigned BW = A1.getBitWidth();
1628 assert(Width >= Bits && BW >= Bits);
1629 // Special case to make things faster for smaller source widths.
1630 // Sign extension of 0 bits generates 0 as a result. This is consistent
1631 // with what the HW does.
1632 if (Bits == 0) {
1633 Result = APInt(Width, 0);
1634 return true;
1635 }
1636 // In C, shifts by 64 invoke undefined behavior: handle that case in APInt.
1637 if (BW <= 64 && Bits != 0) {
1638 int64_t V = A1.getSExtValue();
1639 switch (Bits) {
1640 case 8:
1641 V = static_cast<int8_t>(V);
1642 break;
1643 case 16:
1644 V = static_cast<int16_t>(V);
1645 break;
1646 case 32:
1647 V = static_cast<int32_t>(V);
1648 break;
1649 default:
1650 // Shift left to lose all bits except lower "Bits" bits, then shift
1651 // the value back, replicating what was a sign bit after the first
1652 // shift.
1653 V = (V << (64-Bits)) >> (64-Bits);
1654 break;
1655 }
1656 // V is a 64-bit sign-extended value. Convert it to APInt of desired
1657 // width.
1658 Result = APInt(Width, V, true);
1659 return true;
1660 }
1661 // Slow case: the value doesn't fit in int64_t.
1662 if (Bits < BW)
1663 Result = A1.trunc(Bits).sext(Width);
1664 else // Bits == BW
1665 Result = A1.sext(Width);
1666 return true;
1667}
1668
1669
1670bool MachineConstEvaluator::evaluateCLBr(const Register &R1, bool Zeros,
1671 bool Ones, const CellMap &Inputs, LatticeCell &Result) {
1672 assert(Inputs.has(R1.Reg));
1673 LatticeCell LS1;
1674 if (!getCell(R1, Inputs, LS1))
1675 return false;
1676 if (LS1.isBottom() || LS1.isProperty())
1677 return false;
1678
1679 APInt A, CA;
1680 for (unsigned i = 0; i < LS1.size(); ++i) {
1681 bool Eval = constToInt(LS1.Values[i], A) &&
1682 evaluateCLBi(A, Zeros, Ones, CA);
1683 if (!Eval)
1684 return false;
1685 const Constant *C = intToConst(CA);
1686 Result.add(C);
1687 }
1688 return true;
1689}
1690
1691
1692bool MachineConstEvaluator::evaluateCLBi(const APInt &A1, bool Zeros,
1693 bool Ones, APInt &Result) {
1694 unsigned BW = A1.getBitWidth();
1695 if (!Zeros && !Ones)
1696 return false;
1697 unsigned Count = 0;
1698 if (Zeros && (Count == 0))
1699 Count = A1.countLeadingZeros();
1700 if (Ones && (Count == 0))
1701 Count = A1.countLeadingOnes();
1702 Result = APInt(BW, static_cast<uint64_t>(Count), false);
1703 return true;
1704}
1705
1706
1707bool MachineConstEvaluator::evaluateCTBr(const Register &R1, bool Zeros,
1708 bool Ones, const CellMap &Inputs, LatticeCell &Result) {
1709 assert(Inputs.has(R1.Reg));
1710 LatticeCell LS1;
1711 if (!getCell(R1, Inputs, LS1))
1712 return false;
1713 if (LS1.isBottom() || LS1.isProperty())
1714 return false;
1715
1716 APInt A, CA;
1717 for (unsigned i = 0; i < LS1.size(); ++i) {
1718 bool Eval = constToInt(LS1.Values[i], A) &&
1719 evaluateCTBi(A, Zeros, Ones, CA);
1720 if (!Eval)
1721 return false;
1722 const Constant *C = intToConst(CA);
1723 Result.add(C);
1724 }
1725 return true;
1726}
1727
1728
1729bool MachineConstEvaluator::evaluateCTBi(const APInt &A1, bool Zeros,
1730 bool Ones, APInt &Result) {
1731 unsigned BW = A1.getBitWidth();
1732 if (!Zeros && !Ones)
1733 return false;
1734 unsigned Count = 0;
1735 if (Zeros && (Count == 0))
1736 Count = A1.countTrailingZeros();
1737 if (Ones && (Count == 0))
1738 Count = A1.countTrailingOnes();
1739 Result = APInt(BW, static_cast<uint64_t>(Count), false);
1740 return true;
1741}
1742
1743
1744bool MachineConstEvaluator::evaluateEXTRACTr(const Register &R1,
1745 unsigned Width, unsigned Bits, unsigned Offset, bool Signed,
1746 const CellMap &Inputs, LatticeCell &Result) {
1747 assert(Inputs.has(R1.Reg));
1748 assert(Bits+Offset <= Width);
1749 LatticeCell LS1;
1750 if (!getCell(R1, Inputs, LS1))
1751 return false;
1752 if (LS1.isBottom())
1753 return false;
1754 if (LS1.isProperty()) {
1755 uint32_t Ps = LS1.properties();
1756 if (Ps & ConstantProperties::Zero) {
1757 const Constant *C = intToConst(APInt(Width, 0, false));
1758 Result.add(C);
1759 return true;
1760 }
1761 return false;
1762 }
1763
1764 APInt A, CA;
1765 for (unsigned i = 0; i < LS1.size(); ++i) {
1766 bool Eval = constToInt(LS1.Values[i], A) &&
1767 evaluateEXTRACTi(A, Bits, Offset, Signed, CA);
1768 if (!Eval)
1769 return false;
1770 const Constant *C = intToConst(CA);
1771 Result.add(C);
1772 }
1773 return true;
1774}
1775
1776
1777bool MachineConstEvaluator::evaluateEXTRACTi(const APInt &A1, unsigned Bits,
1778 unsigned Offset, bool Signed, APInt &Result) {
1779 unsigned BW = A1.getBitWidth();
1780 assert(Bits+Offset <= BW);
1781 // Extracting 0 bits generates 0 as a result (as indicated by the HW people).
1782 if (Bits == 0) {
1783 Result = APInt(BW, 0);
1784 return true;
1785 }
1786 if (BW <= 64) {
1787 int64_t V = A1.getZExtValue();
1788 V <<= (64-Bits-Offset);
1789 if (Signed)
1790 V >>= (64-Bits);
1791 else
1792 V = static_cast<uint64_t>(V) >> (64-Bits);
1793 Result = APInt(BW, V, Signed);
1794 return true;
1795 }
1796 if (Signed)
1797 Result = A1.shl(BW-Bits-Offset).ashr(BW-Bits);
1798 else
1799 Result = A1.shl(BW-Bits-Offset).lshr(BW-Bits);
1800 return true;
1801}
1802
1803
1804bool MachineConstEvaluator::evaluateSplatr(const Register &R1,
1805 unsigned Bits, unsigned Count, const CellMap &Inputs,
1806 LatticeCell &Result) {
1807 assert(Inputs.has(R1.Reg));
1808 LatticeCell LS1;
1809 if (!getCell(R1, Inputs, LS1))
1810 return false;
1811 if (LS1.isBottom() || LS1.isProperty())
1812 return false;
1813
1814 APInt A, SA;
1815 for (unsigned i = 0; i < LS1.size(); ++i) {
1816 bool Eval = constToInt(LS1.Values[i], A) &&
1817 evaluateSplati(A, Bits, Count, SA);
1818 if (!Eval)
1819 return false;
1820 const Constant *C = intToConst(SA);
1821 Result.add(C);
1822 }
1823 return true;
1824}
1825
1826
1827bool MachineConstEvaluator::evaluateSplati(const APInt &A1, unsigned Bits,
1828 unsigned Count, APInt &Result) {
1829 assert(Count > 0);
1830 unsigned BW = A1.getBitWidth(), SW = Count*Bits;
1831 APInt LoBits = (Bits < BW) ? A1.trunc(Bits) : A1.zextOrSelf(Bits);
1832 if (Count > 1)
1833 LoBits = LoBits.zext(SW);
1834
1835 APInt Res(SW, 0, false);
1836 for (unsigned i = 0; i < Count; ++i) {
1837 Res <<= Bits;
1838 Res |= LoBits;
1839 }
1840 Result = Res;
1841 return true;
1842}
1843
1844
1845// ----------------------------------------------------------------------
1846// Hexagon-specific code.
1847
1848namespace llvm {
1849 FunctionPass *createHexagonConstPropagationPass();
1850 void initializeHexagonConstPropagationPass(PassRegistry &Registry);
1851}
1852
1853namespace {
1854 class HexagonConstEvaluator : public MachineConstEvaluator {
1855 public:
1856 HexagonConstEvaluator(MachineFunction &Fn);
1857
1858 bool evaluate(const MachineInstr &MI, const CellMap &Inputs,
1859 CellMap &Outputs) override;
1860 bool evaluate(const Register &R, const LatticeCell &SrcC,
1861 LatticeCell &Result) override;
1862 bool evaluate(const MachineInstr &BrI, const CellMap &Inputs,
1863 SetVector<const MachineBasicBlock*> &Targets, bool &FallsThru)
1864 override;
1865 bool rewrite(MachineInstr &MI, const CellMap &Inputs) override;
1866
1867
1868 private:
1869 unsigned getRegBitWidth(unsigned Reg) const;
1870
1871 static uint32_t getCmp(unsigned Opc);
1872 static APInt getCmpImm(unsigned Opc, unsigned OpX,
1873 const MachineOperand &MO);
1874 void replaceWithNop(MachineInstr &MI);
1875
1876 bool evaluateHexRSEQ32(Register RL, Register RH, const CellMap &Inputs,
1877 LatticeCell &Result);
1878 bool evaluateHexCompare(const MachineInstr &MI, const CellMap &Inputs,
1879 CellMap &Outputs);
1880 // This is suitable to be called for compare-and-jump instructions.
1881 bool evaluateHexCompare2(uint32_t Cmp, const MachineOperand &Src1,
1882 const MachineOperand &Src2, const CellMap &Inputs, bool &Result);
1883 bool evaluateHexLogical(const MachineInstr &MI, const CellMap &Inputs,
1884 CellMap &Outputs);
1885 bool evaluateHexCondMove(const MachineInstr &MI, const CellMap &Inputs,
1886 CellMap &Outputs);
1887 bool evaluateHexExt(const MachineInstr &MI, const CellMap &Inputs,
1888 CellMap &Outputs);
1889 bool evaluateHexVector1(const MachineInstr &MI, const CellMap &Inputs,
1890 CellMap &Outputs);
1891 bool evaluateHexVector2(const MachineInstr &MI, const CellMap &Inputs,
1892 CellMap &Outputs);
1893
1894 void replaceAllRegUsesWith(unsigned FromReg, unsigned ToReg);
1895 bool rewriteHexBranch(MachineInstr &BrI, const CellMap &Inputs);
1896 bool rewriteHexConstDefs(MachineInstr &MI, const CellMap &Inputs,
1897 bool &AllDefs);
1898 bool rewriteHexConstUses(MachineInstr &MI, const CellMap &Inputs);
1899
1900 MachineRegisterInfo *MRI;
1901 const HexagonInstrInfo &HII;
1902 const HexagonRegisterInfo &HRI;
1903 };
1904
1905
1906 class HexagonConstPropagation : public MachineFunctionPass {
1907 public:
1908 static char ID;
1909 HexagonConstPropagation() : MachineFunctionPass(ID) {
1910 PassRegistry &Registry = *PassRegistry::getPassRegistry();
1911 initializeHexagonConstPropagationPass(Registry);
1912 }
1913 const char *getPassName() const override {
1914 return "Hexagon Constant Propagation";
1915 }
1916 bool runOnMachineFunction(MachineFunction &MF) override {
1917 const Function *F = MF.getFunction();
1918 if (!F)
1919 return false;
1920 if (skipFunction(*F))
1921 return false;
1922
1923 HexagonConstEvaluator HCE(MF);
1924 return MachineConstPropagator(HCE).run(MF);
1925 }
1926 };
1927
1928 char HexagonConstPropagation::ID = 0;
1929}
1930
1931INITIALIZE_PASS(HexagonConstPropagation, "hcp", "Hexagon Constant Propagation",
1932 false, false)
1933
1934
1935HexagonConstEvaluator::HexagonConstEvaluator(MachineFunction &Fn)
1936 : MachineConstEvaluator(Fn),
1937 HII(*Fn.getSubtarget<HexagonSubtarget>().getInstrInfo()),
1938 HRI(*Fn.getSubtarget<HexagonSubtarget>().getRegisterInfo()) {
1939 MRI = &Fn.getRegInfo();
1940}
1941
1942
1943bool HexagonConstEvaluator::evaluate(const MachineInstr &MI,
1944 const CellMap &Inputs, CellMap &Outputs) {
1945 if (MI.isCall())
1946 return false;
1947 if (MI.getNumOperands() == 0 || !MI.getOperand(0).isReg())
1948 return false;
1949 const MachineOperand &MD = MI.getOperand(0);
1950 if (!MD.isDef())
1951 return false;
1952
1953 unsigned Opc = MI.getOpcode();
1954 Register DefR(MD);
1955 assert(!DefR.SubReg);
1956 if (!TargetRegisterInfo::isVirtualRegister(DefR.Reg))
1957 return false;
1958
1959 if (MI.isCopy()) {
1960 LatticeCell RC;
1961 Register SrcR(MI.getOperand(1));
1962 bool Eval = evaluateCOPY(SrcR, Inputs, RC);
1963 if (!Eval)
1964 return false;
1965 Outputs.update(DefR.Reg, RC);
1966 return true;
1967 }
1968 if (MI.isRegSequence()) {
1969 unsigned Sub1 = MI.getOperand(2).getImm();
1970 unsigned Sub2 = MI.getOperand(4).getImm();
1971 if (Sub1 != Hexagon::subreg_loreg && Sub1 != Hexagon::subreg_hireg)
1972 return false;
1973 if (Sub2 != Hexagon::subreg_loreg && Sub2 != Hexagon::subreg_hireg)
1974 return false;
1975 assert(Sub1 != Sub2);
1976 bool LoIs1 = (Sub1 == Hexagon::subreg_loreg);
1977 const MachineOperand &OpLo = LoIs1 ? MI.getOperand(1) : MI.getOperand(3);
1978 const MachineOperand &OpHi = LoIs1 ? MI.getOperand(3) : MI.getOperand(1);
1979 LatticeCell RC;
1980 Register SrcRL(OpLo), SrcRH(OpHi);
1981 bool Eval = evaluateHexRSEQ32(SrcRL, SrcRH, Inputs, RC);
1982 if (!Eval)
1983 return false;
1984 Outputs.update(DefR.Reg, RC);
1985 return true;
1986 }
1987 if (MI.isCompare()) {
1988 bool Eval = evaluateHexCompare(MI, Inputs, Outputs);
1989 return Eval;
1990 }
1991
1992 switch (Opc) {
1993 default:
1994 return false;
1995 case Hexagon::A2_tfrsi:
1996 case Hexagon::CONST32:
1997 case Hexagon::A2_tfrpi:
1998 case Hexagon::CONST32_Int_Real:
1999 case Hexagon::CONST64_Int_Real:
2000 {
2001 const MachineOperand &VO = MI.getOperand(1);
2002 // The operand of CONST32_Int_Real can be a blockaddress, e.g.
2003 // %vreg0<def> = CONST32_Int_Real <blockaddress(@eat, %L)>
2004 // Do this check for all instructions for safety.
2005 if (!VO.isImm())
2006 return false;
2007 int64_t V = MI.getOperand(1).getImm();
2008 unsigned W = getRegBitWidth(DefR.Reg);
2009 if (W != 32 && W != 64)
2010 return false;
2011 IntegerType *Ty = (W == 32) ? Type::getInt32Ty(CX)
2012 : Type::getInt64Ty(CX);
2013 const ConstantInt *CI = ConstantInt::get(Ty, V, true);
2014 LatticeCell RC = Outputs.get(DefR.Reg);
2015 RC.add(CI);
2016 Outputs.update(DefR.Reg, RC);
2017 break;
2018 }
2019
2020 case Hexagon::TFR_PdTrue:
2021 case Hexagon::TFR_PdFalse:
2022 {
2023 LatticeCell RC = Outputs.get(DefR.Reg);
2024 bool NonZero = (Opc == Hexagon::TFR_PdTrue);
2025 uint32_t P = NonZero ? ConstantProperties::NonZero
2026 : ConstantProperties::Zero;
2027 RC.add(P);
2028 Outputs.update(DefR.Reg, RC);
2029 break;
2030 }
2031
2032 case Hexagon::A2_and:
2033 case Hexagon::A2_andir:
2034 case Hexagon::A2_andp:
2035 case Hexagon::A2_or:
2036 case Hexagon::A2_orir:
2037 case Hexagon::A2_orp:
2038 case Hexagon::A2_xor:
2039 case Hexagon::A2_xorp:
2040 {
2041 bool Eval = evaluateHexLogical(MI, Inputs, Outputs);
2042 if (!Eval)
2043 return false;
2044 break;
2045 }
2046
2047 case Hexagon::A2_combineii: // combine(#s8Ext, #s8)
2048 case Hexagon::A4_combineii: // combine(#s8, #u6Ext)
2049 {
Benjamin Kramerafff73c2016-07-30 13:25:37 +00002050 uint64_t Hi = MI.getOperand(1).getImm();
2051 uint64_t Lo = MI.getOperand(2).getImm();
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +00002052 uint64_t Res = (Hi << 32) | (Lo & 0xFFFFFFFF);
2053 IntegerType *Ty = Type::getInt64Ty(CX);
2054 const ConstantInt *CI = ConstantInt::get(Ty, Res, false);
2055 LatticeCell RC = Outputs.get(DefR.Reg);
2056 RC.add(CI);
2057 Outputs.update(DefR.Reg, RC);
2058 break;
2059 }
2060
2061 case Hexagon::S2_setbit_i:
2062 {
2063 int64_t B = MI.getOperand(2).getImm();
2064 assert(B >=0 && B < 32);
Simon Pilgrim0aaf6ba2016-07-29 10:03:39 +00002065 APInt A(32, (1ull << B), false);
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +00002066 Register R(MI.getOperand(1));
2067 LatticeCell RC = Outputs.get(DefR.Reg);
2068 bool Eval = evaluateORri(R, A, Inputs, RC);
2069 if (!Eval)
2070 return false;
2071 Outputs.update(DefR.Reg, RC);
2072 break;
2073 }
2074
2075 case Hexagon::C2_mux:
2076 case Hexagon::C2_muxir:
2077 case Hexagon::C2_muxri:
2078 case Hexagon::C2_muxii:
2079 {
2080 bool Eval = evaluateHexCondMove(MI, Inputs, Outputs);
2081 if (!Eval)
2082 return false;
2083 break;
2084 }
2085
2086 case Hexagon::A2_sxtb:
2087 case Hexagon::A2_sxth:
2088 case Hexagon::A2_sxtw:
2089 case Hexagon::A2_zxtb:
2090 case Hexagon::A2_zxth:
2091 {
2092 bool Eval = evaluateHexExt(MI, Inputs, Outputs);
2093 if (!Eval)
2094 return false;
2095 break;
2096 }
2097
2098 case Hexagon::S2_ct0:
2099 case Hexagon::S2_ct0p:
2100 case Hexagon::S2_ct1:
2101 case Hexagon::S2_ct1p:
2102 {
2103 using namespace Hexagon;
2104 bool Ones = (Opc == S2_ct1) || (Opc == S2_ct1p);
2105 Register R1(MI.getOperand(1));
2106 assert(Inputs.has(R1.Reg));
2107 LatticeCell T;
2108 bool Eval = evaluateCTBr(R1, !Ones, Ones, Inputs, T);
2109 if (!Eval)
2110 return false;
2111 // All of these instructions return a 32-bit value. The evaluate
2112 // will generate the same type as the operand, so truncate the
2113 // result if necessary.
2114 APInt C;
2115 LatticeCell RC = Outputs.get(DefR.Reg);
2116 for (unsigned i = 0; i < T.size(); ++i) {
2117 const Constant *CI = T.Values[i];
2118 if (constToInt(CI, C) && C.getBitWidth() > 32)
2119 CI = intToConst(C.trunc(32));
2120 RC.add(CI);
2121 }
2122 Outputs.update(DefR.Reg, RC);
2123 break;
2124 }
2125
2126 case Hexagon::S2_cl0:
2127 case Hexagon::S2_cl0p:
2128 case Hexagon::S2_cl1:
2129 case Hexagon::S2_cl1p:
2130 case Hexagon::S2_clb:
2131 case Hexagon::S2_clbp:
2132 {
2133 using namespace Hexagon;
2134 bool OnlyZeros = (Opc == S2_cl0) || (Opc == S2_cl0p);
2135 bool OnlyOnes = (Opc == S2_cl1) || (Opc == S2_cl1p);
2136 Register R1(MI.getOperand(1));
2137 assert(Inputs.has(R1.Reg));
2138 LatticeCell T;
2139 bool Eval = evaluateCLBr(R1, !OnlyOnes, !OnlyZeros, Inputs, T);
2140 if (!Eval)
2141 return false;
2142 // All of these instructions return a 32-bit value. The evaluate
2143 // will generate the same type as the operand, so truncate the
2144 // result if necessary.
2145 APInt C;
2146 LatticeCell RC = Outputs.get(DefR.Reg);
2147 for (unsigned i = 0; i < T.size(); ++i) {
2148 const Constant *CI = T.Values[i];
2149 if (constToInt(CI, C) && C.getBitWidth() > 32)
2150 CI = intToConst(C.trunc(32));
2151 RC.add(CI);
2152 }
2153 Outputs.update(DefR.Reg, RC);
2154 break;
2155 }
2156
2157 case Hexagon::S4_extract:
2158 case Hexagon::S4_extractp:
2159 case Hexagon::S2_extractu:
2160 case Hexagon::S2_extractup:
2161 {
2162 bool Signed = (Opc == Hexagon::S4_extract) ||
2163 (Opc == Hexagon::S4_extractp);
2164 Register R1(MI.getOperand(1));
2165 unsigned BW = getRegBitWidth(R1.Reg);
2166 unsigned Bits = MI.getOperand(2).getImm();
2167 unsigned Offset = MI.getOperand(3).getImm();
2168 LatticeCell RC = Outputs.get(DefR.Reg);
2169 if (Offset >= BW) {
2170 APInt Zero(BW, 0, false);
2171 RC.add(intToConst(Zero));
2172 break;
2173 }
2174 if (Offset+Bits > BW) {
2175 // If the requested bitfield extends beyond the most significant bit,
2176 // the extra bits are treated as 0s. To emulate this behavior, reduce
2177 // the number of requested bits, and make the extract unsigned.
2178 Bits = BW-Offset;
2179 Signed = false;
2180 }
2181 bool Eval = evaluateEXTRACTr(R1, BW, Bits, Offset, Signed, Inputs, RC);
2182 if (!Eval)
2183 return false;
2184 Outputs.update(DefR.Reg, RC);
2185 break;
2186 }
2187
2188 case Hexagon::S2_vsplatrb:
2189 case Hexagon::S2_vsplatrh:
2190 // vabsh, vabsh:sat
2191 // vabsw, vabsw:sat
2192 // vconj:sat
2193 // vrndwh, vrndwh:sat
2194 // vsathb, vsathub, vsatwuh
2195 // vsxtbh, vsxthw
2196 // vtrunehb, vtrunohb
2197 // vzxtbh, vzxthw
2198 {
2199 bool Eval = evaluateHexVector1(MI, Inputs, Outputs);
2200 if (!Eval)
2201 return false;
2202 break;
2203 }
2204
2205 // TODO:
2206 // A2_vaddh
2207 // A2_vaddhs
2208 // A2_vaddw
2209 // A2_vaddws
2210 }
2211
2212 return true;
2213}
2214
2215
2216bool HexagonConstEvaluator::evaluate(const Register &R,
2217 const LatticeCell &Input, LatticeCell &Result) {
2218 if (!R.SubReg) {
2219 Result = Input;
2220 return true;
2221 }
2222 // Predicate registers do not have subregisters.
2223 const TargetRegisterClass *RC = MRI->getRegClass(R.Reg);
2224 if (RC == &Hexagon::PredRegsRegClass)
2225 return false;
2226 if (R.SubReg != Hexagon::subreg_loreg && R.SubReg != Hexagon::subreg_hireg)
2227 return false;
2228
2229 assert(!Input.isTop());
2230 if (Input.isBottom())
2231 return false;
2232
2233 typedef ConstantProperties P;
2234 if (Input.isProperty()) {
2235 uint32_t Ps = Input.properties();
2236 if (Ps & (P::Zero|P::NaN)) {
2237 uint32_t Ns = (Ps & (P::Zero|P::NaN|P::SignProperties));
2238 Result.add(Ns);
2239 return true;
2240 }
2241 if (R.SubReg == Hexagon::subreg_hireg) {
2242 uint32_t Ns = (Ps & P::SignProperties);
2243 Result.add(Ns);
2244 return true;
2245 }
2246 return false;
2247 }
2248
2249 // The Input cell contains some known values. Pick the word corresponding
2250 // to the subregister.
2251 APInt A;
2252 for (unsigned i = 0; i < Input.size(); ++i) {
2253 const Constant *C = Input.Values[i];
2254 if (!constToInt(C, A))
2255 return false;
2256 if (!A.isIntN(64))
2257 return false;
2258 uint64_t U = A.getZExtValue();
2259 if (R.SubReg == Hexagon::subreg_hireg)
2260 U >>= 32;
2261 U &= 0xFFFFFFFFULL;
2262 uint32_t U32 = Lo_32(U);
2263 int32_t V32;
2264 memcpy(&V32, &U32, sizeof V32);
2265 IntegerType *Ty = Type::getInt32Ty(CX);
2266 const ConstantInt *C32 = ConstantInt::get(Ty, static_cast<int64_t>(V32));
2267 Result.add(C32);
2268 }
2269 return true;
2270}
2271
2272
2273bool HexagonConstEvaluator::evaluate(const MachineInstr &BrI,
2274 const CellMap &Inputs, SetVector<const MachineBasicBlock*> &Targets,
2275 bool &FallsThru) {
2276 // We need to evaluate one branch at a time. TII::analyzeBranch checks
2277 // all the branches in a basic block at once, so we cannot use it.
2278 unsigned Opc = BrI.getOpcode();
2279 bool SimpleBranch = false;
2280 bool Negated = false;
2281 switch (Opc) {
2282 case Hexagon::J2_jumpf:
2283 case Hexagon::J2_jumpfnew:
2284 case Hexagon::J2_jumpfnewpt:
2285 Negated = true;
2286 case Hexagon::J2_jumpt:
2287 case Hexagon::J2_jumptnew:
2288 case Hexagon::J2_jumptnewpt:
2289 // Simple branch: if([!]Pn) jump ...
2290 // i.e. Op0 = predicate, Op1 = branch target.
2291 SimpleBranch = true;
2292 break;
2293 case Hexagon::J2_jump:
2294 Targets.insert(BrI.getOperand(0).getMBB());
2295 FallsThru = false;
2296 return true;
2297 default:
2298Undetermined:
2299 // If the branch is of unknown type, assume that all successors are
2300 // executable.
2301 FallsThru = !BrI.isUnconditionalBranch();
2302 return false;
2303 }
2304
2305 if (SimpleBranch) {
2306 const MachineOperand &MD = BrI.getOperand(0);
2307 Register PR(MD);
2308 // If the condition operand has a subregister, this is not something
2309 // we currently recognize.
2310 if (PR.SubReg)
2311 goto Undetermined;
2312 assert(Inputs.has(PR.Reg));
2313 const LatticeCell &PredC = Inputs.get(PR.Reg);
2314 if (PredC.isBottom())
2315 goto Undetermined;
2316
2317 uint32_t Props = PredC.properties();
2318 bool CTrue = false, CFalse = false;;
2319 if (Props & ConstantProperties::Zero)
2320 CFalse = true;
2321 else if (Props & ConstantProperties::NonZero)
2322 CTrue = true;
2323 // If the condition is not known to be either, bail out.
2324 if (!CTrue && !CFalse)
2325 goto Undetermined;
2326
2327 const MachineBasicBlock *BranchTarget = BrI.getOperand(1).getMBB();
2328
2329 FallsThru = false;
2330 if ((!Negated && CTrue) || (Negated && CFalse))
2331 Targets.insert(BranchTarget);
2332 else if ((!Negated && CFalse) || (Negated && CTrue))
2333 FallsThru = true;
2334 else
2335 goto Undetermined;
2336 }
2337
2338 return true;
2339}
2340
2341
2342bool HexagonConstEvaluator::rewrite(MachineInstr &MI, const CellMap &Inputs) {
2343 if (MI.isBranch())
2344 return rewriteHexBranch(MI, Inputs);
2345
2346 unsigned Opc = MI.getOpcode();
2347 switch (Opc) {
2348 default:
2349 break;
2350 case Hexagon::A2_tfrsi:
2351 case Hexagon::CONST32:
2352 case Hexagon::A2_tfrpi:
2353 case Hexagon::CONST32_Int_Real:
2354 case Hexagon::CONST64_Int_Real:
2355 case Hexagon::TFR_PdTrue:
2356 case Hexagon::TFR_PdFalse:
2357 return false;
2358 }
2359
2360 unsigned NumOp = MI.getNumOperands();
2361 if (NumOp == 0)
2362 return false;
2363
2364 bool AllDefs, Changed;
2365 Changed = rewriteHexConstDefs(MI, Inputs, AllDefs);
2366 // If not all defs have been rewritten (i.e. the instruction defines
2367 // a register that is not compile-time constant), then try to rewrite
2368 // register operands that are known to be constant with immediates.
2369 if (!AllDefs)
2370 Changed |= rewriteHexConstUses(MI, Inputs);
2371
2372 return Changed;
2373}
2374
2375
2376unsigned HexagonConstEvaluator::getRegBitWidth(unsigned Reg) const {
2377 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
2378 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC))
2379 return 32;
2380 if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC))
2381 return 64;
2382 if (Hexagon::PredRegsRegClass.hasSubClassEq(RC))
2383 return 8;
2384 llvm_unreachable("Invalid register");
2385 return 0;
2386}
2387
2388
2389uint32_t HexagonConstEvaluator::getCmp(unsigned Opc) {
2390 switch (Opc) {
2391 case Hexagon::C2_cmpeq:
2392 case Hexagon::C2_cmpeqp:
2393 case Hexagon::A4_cmpbeq:
2394 case Hexagon::A4_cmpheq:
2395 case Hexagon::A4_cmpbeqi:
2396 case Hexagon::A4_cmpheqi:
2397 case Hexagon::C2_cmpeqi:
2398 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
2399 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
2400 case Hexagon::J4_cmpeqi_t_jumpnv_nt:
2401 case Hexagon::J4_cmpeqi_t_jumpnv_t:
2402 case Hexagon::J4_cmpeq_t_jumpnv_nt:
2403 case Hexagon::J4_cmpeq_t_jumpnv_t:
2404 return Comparison::EQ;
2405
2406 case Hexagon::C4_cmpneq:
2407 case Hexagon::C4_cmpneqi:
2408 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
2409 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
2410 case Hexagon::J4_cmpeqi_f_jumpnv_nt:
2411 case Hexagon::J4_cmpeqi_f_jumpnv_t:
2412 case Hexagon::J4_cmpeq_f_jumpnv_nt:
2413 case Hexagon::J4_cmpeq_f_jumpnv_t:
2414 return Comparison::NE;
2415
2416 case Hexagon::C2_cmpgt:
2417 case Hexagon::C2_cmpgtp:
2418 case Hexagon::A4_cmpbgt:
2419 case Hexagon::A4_cmphgt:
2420 case Hexagon::A4_cmpbgti:
2421 case Hexagon::A4_cmphgti:
2422 case Hexagon::C2_cmpgti:
2423 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
2424 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
2425 case Hexagon::J4_cmpgti_t_jumpnv_nt:
2426 case Hexagon::J4_cmpgti_t_jumpnv_t:
2427 case Hexagon::J4_cmpgt_t_jumpnv_nt:
2428 case Hexagon::J4_cmpgt_t_jumpnv_t:
2429 return Comparison::GTs;
2430
2431 case Hexagon::C4_cmplte:
2432 case Hexagon::C4_cmpltei:
2433 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
2434 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
2435 case Hexagon::J4_cmpgti_f_jumpnv_nt:
2436 case Hexagon::J4_cmpgti_f_jumpnv_t:
2437 case Hexagon::J4_cmpgt_f_jumpnv_nt:
2438 case Hexagon::J4_cmpgt_f_jumpnv_t:
2439 return Comparison::LEs;
2440
2441 case Hexagon::C2_cmpgtu:
2442 case Hexagon::C2_cmpgtup:
2443 case Hexagon::A4_cmpbgtu:
2444 case Hexagon::A4_cmpbgtui:
2445 case Hexagon::A4_cmphgtu:
2446 case Hexagon::A4_cmphgtui:
2447 case Hexagon::C2_cmpgtui:
2448 case Hexagon::J4_cmpgtui_t_jumpnv_nt:
2449 case Hexagon::J4_cmpgtui_t_jumpnv_t:
2450 case Hexagon::J4_cmpgtu_t_jumpnv_nt:
2451 case Hexagon::J4_cmpgtu_t_jumpnv_t:
2452 return Comparison::GTu;
2453
2454 case Hexagon::J4_cmpltu_f_jumpnv_nt:
2455 case Hexagon::J4_cmpltu_f_jumpnv_t:
2456 return Comparison::GEu;
2457
2458 case Hexagon::J4_cmpltu_t_jumpnv_nt:
2459 case Hexagon::J4_cmpltu_t_jumpnv_t:
2460 return Comparison::LTu;
2461
2462 case Hexagon::J4_cmplt_f_jumpnv_nt:
2463 case Hexagon::J4_cmplt_f_jumpnv_t:
2464 return Comparison::GEs;
2465
2466 case Hexagon::C4_cmplteu:
2467 case Hexagon::C4_cmplteui:
2468 case Hexagon::J4_cmpgtui_f_jumpnv_nt:
2469 case Hexagon::J4_cmpgtui_f_jumpnv_t:
2470 case Hexagon::J4_cmpgtu_f_jumpnv_nt:
2471 case Hexagon::J4_cmpgtu_f_jumpnv_t:
2472 return Comparison::LEu;
2473
2474 case Hexagon::J4_cmplt_t_jumpnv_nt:
2475 case Hexagon::J4_cmplt_t_jumpnv_t:
2476 return Comparison::LTs;
2477
2478 default:
2479 break;
2480 }
2481 return Comparison::Unk;
2482}
2483
2484
2485APInt HexagonConstEvaluator::getCmpImm(unsigned Opc, unsigned OpX,
2486 const MachineOperand &MO) {
2487 bool Signed = false;
2488 switch (Opc) {
2489 case Hexagon::A4_cmpbgtui: // u7
2490 case Hexagon::A4_cmphgtui: // u7
2491 break;
2492 case Hexagon::A4_cmpheqi: // s8
2493 case Hexagon::C4_cmpneqi: // s8
2494 Signed = true;
2495 case Hexagon::A4_cmpbeqi: // u8
2496 break;
2497 case Hexagon::C2_cmpgtui: // u9
2498 case Hexagon::C4_cmplteui: // u9
2499 break;
2500 case Hexagon::C2_cmpeqi: // s10
2501 case Hexagon::C2_cmpgti: // s10
2502 case Hexagon::C4_cmpltei: // s10
2503 Signed = true;
2504 break;
2505 case Hexagon::J4_cmpeqi_f_jumpnv_nt: // u5
2506 case Hexagon::J4_cmpeqi_f_jumpnv_t: // u5
2507 case Hexagon::J4_cmpeqi_t_jumpnv_nt: // u5
2508 case Hexagon::J4_cmpeqi_t_jumpnv_t: // u5
2509 case Hexagon::J4_cmpgti_f_jumpnv_nt: // u5
2510 case Hexagon::J4_cmpgti_f_jumpnv_t: // u5
2511 case Hexagon::J4_cmpgti_t_jumpnv_nt: // u5
2512 case Hexagon::J4_cmpgti_t_jumpnv_t: // u5
2513 case Hexagon::J4_cmpgtui_f_jumpnv_nt: // u5
2514 case Hexagon::J4_cmpgtui_f_jumpnv_t: // u5
2515 case Hexagon::J4_cmpgtui_t_jumpnv_nt: // u5
2516 case Hexagon::J4_cmpgtui_t_jumpnv_t: // u5
2517 break;
2518 default:
2519 llvm_unreachable("Unhandled instruction");
2520 break;
2521 }
2522
2523 uint64_t Val = MO.getImm();
2524 return APInt(32, Val, Signed);
2525}
2526
2527
2528void HexagonConstEvaluator::replaceWithNop(MachineInstr &MI) {
2529 MI.setDesc(HII.get(Hexagon::A2_nop));
2530 while (MI.getNumOperands() > 0)
2531 MI.RemoveOperand(0);
2532}
2533
2534
2535bool HexagonConstEvaluator::evaluateHexRSEQ32(Register RL, Register RH,
2536 const CellMap &Inputs, LatticeCell &Result) {
2537 assert(Inputs.has(RL.Reg) && Inputs.has(RH.Reg));
2538 LatticeCell LSL, LSH;
2539 if (!getCell(RL, Inputs, LSL) || !getCell(RH, Inputs, LSH))
2540 return false;
2541 if (LSL.isProperty() || LSH.isProperty())
2542 return false;
2543
2544 unsigned LN = LSL.size(), HN = LSH.size();
2545 SmallVector<APInt,4> LoVs(LN), HiVs(HN);
2546 for (unsigned i = 0; i < LN; ++i) {
2547 bool Eval = constToInt(LSL.Values[i], LoVs[i]);
2548 if (!Eval)
2549 return false;
2550 assert(LoVs[i].getBitWidth() == 32);
2551 }
2552 for (unsigned i = 0; i < HN; ++i) {
2553 bool Eval = constToInt(LSH.Values[i], HiVs[i]);
2554 if (!Eval)
2555 return false;
2556 assert(HiVs[i].getBitWidth() == 32);
2557 }
2558
2559 for (unsigned i = 0; i < HiVs.size(); ++i) {
2560 APInt HV = HiVs[i].zextOrSelf(64) << 32;
2561 for (unsigned j = 0; j < LoVs.size(); ++j) {
2562 APInt LV = LoVs[j].zextOrSelf(64);
2563 const Constant *C = intToConst(HV | LV);
2564 Result.add(C);
2565 if (Result.isBottom())
2566 return false;
2567 }
2568 }
2569 return !Result.isBottom();
2570}
2571
2572
2573bool HexagonConstEvaluator::evaluateHexCompare(const MachineInstr &MI,
2574 const CellMap &Inputs, CellMap &Outputs) {
2575 unsigned Opc = MI.getOpcode();
2576 bool Classic = false;
2577 switch (Opc) {
2578 case Hexagon::C2_cmpeq:
2579 case Hexagon::C2_cmpeqp:
2580 case Hexagon::C2_cmpgt:
2581 case Hexagon::C2_cmpgtp:
2582 case Hexagon::C2_cmpgtu:
2583 case Hexagon::C2_cmpgtup:
2584 case Hexagon::C2_cmpeqi:
2585 case Hexagon::C2_cmpgti:
2586 case Hexagon::C2_cmpgtui:
2587 // Classic compare: Dst0 = CMP Src1, Src2
2588 Classic = true;
2589 break;
2590 default:
2591 // Not handling other compare instructions now.
2592 return false;
2593 }
2594
2595 if (Classic) {
2596 const MachineOperand &Src1 = MI.getOperand(1);
2597 const MachineOperand &Src2 = MI.getOperand(2);
2598
2599 bool Result;
2600 unsigned Opc = MI.getOpcode();
2601 bool Computed = evaluateHexCompare2(Opc, Src1, Src2, Inputs, Result);
2602 if (Computed) {
2603 // Only create a zero/non-zero cell. At this time there isn't really
2604 // much need for specific values.
2605 Register DefR(MI.getOperand(0));
2606 LatticeCell L = Outputs.get(DefR.Reg);
2607 uint32_t P = Result ? ConstantProperties::NonZero
2608 : ConstantProperties::Zero;
2609 L.add(P);
2610 Outputs.update(DefR.Reg, L);
2611 return true;
2612 }
2613 }
2614
2615 return false;
2616}
2617
2618
2619bool HexagonConstEvaluator::evaluateHexCompare2(unsigned Opc,
2620 const MachineOperand &Src1, const MachineOperand &Src2,
2621 const CellMap &Inputs, bool &Result) {
2622 uint32_t Cmp = getCmp(Opc);
2623 bool Reg1 = Src1.isReg(), Reg2 = Src2.isReg();
2624 bool Imm1 = Src1.isImm(), Imm2 = Src2.isImm();
2625 if (Reg1) {
2626 Register R1(Src1);
2627 if (Reg2) {
2628 Register R2(Src2);
2629 return evaluateCMPrr(Cmp, R1, R2, Inputs, Result);
2630 } else if (Imm2) {
2631 APInt A2 = getCmpImm(Opc, 2, Src2);
2632 return evaluateCMPri(Cmp, R1, A2, Inputs, Result);
2633 }
2634 } else if (Imm1) {
2635 APInt A1 = getCmpImm(Opc, 1, Src1);
2636 if (Reg2) {
2637 Register R2(Src2);
2638 uint32_t NegCmp = Comparison::negate(Cmp);
2639 return evaluateCMPri(NegCmp, R2, A1, Inputs, Result);
2640 } else if (Imm2) {
2641 APInt A2 = getCmpImm(Opc, 2, Src2);
2642 return evaluateCMPii(Cmp, A1, A2, Result);
2643 }
2644 }
2645 // Unknown kind of comparison.
2646 return false;
2647}
2648
2649
2650bool HexagonConstEvaluator::evaluateHexLogical(const MachineInstr &MI,
2651 const CellMap &Inputs, CellMap &Outputs) {
2652 unsigned Opc = MI.getOpcode();
2653 if (MI.getNumOperands() != 3)
2654 return false;
2655 const MachineOperand &Src1 = MI.getOperand(1);
2656 const MachineOperand &Src2 = MI.getOperand(2);
2657 Register R1(Src1);
2658 bool Eval = false;
2659 LatticeCell RC;
2660 switch (Opc) {
2661 default:
2662 return false;
2663 case Hexagon::A2_and:
2664 case Hexagon::A2_andp:
2665 Eval = evaluateANDrr(R1, Register(Src2), Inputs, RC);
2666 break;
2667 case Hexagon::A2_andir: {
2668 APInt A(32, Src2.getImm(), true);
2669 Eval = evaluateANDri(R1, A, Inputs, RC);
2670 break;
2671 }
2672 case Hexagon::A2_or:
2673 case Hexagon::A2_orp:
2674 Eval = evaluateORrr(R1, Register(Src2), Inputs, RC);
2675 break;
2676 case Hexagon::A2_orir: {
2677 APInt A(32, Src2.getImm(), true);
2678 Eval = evaluateORri(R1, A, Inputs, RC);
2679 break;
2680 }
2681 case Hexagon::A2_xor:
2682 case Hexagon::A2_xorp:
2683 Eval = evaluateXORrr(R1, Register(Src2), Inputs, RC);
2684 break;
2685 }
2686 if (Eval) {
2687 Register DefR(MI.getOperand(0));
2688 Outputs.update(DefR.Reg, RC);
2689 }
2690 return Eval;
2691}
2692
2693
2694bool HexagonConstEvaluator::evaluateHexCondMove(const MachineInstr &MI,
2695 const CellMap &Inputs, CellMap &Outputs) {
2696 // Dst0 = Cond1 ? Src2 : Src3
2697 Register CR(MI.getOperand(1));
2698 assert(Inputs.has(CR.Reg));
2699 LatticeCell LS;
2700 if (!getCell(CR, Inputs, LS))
2701 return false;
2702 uint32_t Ps = LS.properties();
2703 unsigned TakeOp;
2704 if (Ps & ConstantProperties::Zero)
2705 TakeOp = 3;
2706 else if (Ps & ConstantProperties::NonZero)
2707 TakeOp = 2;
2708 else
2709 return false;
2710
2711 const MachineOperand &ValOp = MI.getOperand(TakeOp);
2712 Register DefR(MI.getOperand(0));
2713 LatticeCell RC = Outputs.get(DefR.Reg);
2714
2715 if (ValOp.isImm()) {
2716 int64_t V = ValOp.getImm();
2717 unsigned W = getRegBitWidth(DefR.Reg);
2718 APInt A(W, V, true);
2719 const Constant *C = intToConst(A);
2720 RC.add(C);
2721 Outputs.update(DefR.Reg, RC);
2722 return true;
2723 }
2724 if (ValOp.isReg()) {
2725 Register R(ValOp);
2726 const LatticeCell &LR = Inputs.get(R.Reg);
2727 LatticeCell LSR;
2728 if (!evaluate(R, LR, LSR))
2729 return false;
2730 RC.meet(LSR);
2731 Outputs.update(DefR.Reg, RC);
2732 return true;
2733 }
2734 return false;
2735}
2736
2737
2738bool HexagonConstEvaluator::evaluateHexExt(const MachineInstr &MI,
2739 const CellMap &Inputs, CellMap &Outputs) {
2740 // Dst0 = ext R1
2741 Register R1(MI.getOperand(1));
2742 assert(Inputs.has(R1.Reg));
2743
2744 unsigned Opc = MI.getOpcode();
2745 unsigned Bits;
2746 switch (Opc) {
2747 case Hexagon::A2_sxtb:
2748 case Hexagon::A2_zxtb:
2749 Bits = 8;
2750 break;
2751 case Hexagon::A2_sxth:
2752 case Hexagon::A2_zxth:
2753 Bits = 16;
2754 break;
2755 case Hexagon::A2_sxtw:
2756 Bits = 32;
2757 break;
2758 }
2759
2760 bool Signed = false;
2761 switch (Opc) {
2762 case Hexagon::A2_sxtb:
2763 case Hexagon::A2_sxth:
2764 case Hexagon::A2_sxtw:
2765 Signed = true;
2766 break;
2767 }
2768
2769 Register DefR(MI.getOperand(0));
2770 unsigned BW = getRegBitWidth(DefR.Reg);
2771 LatticeCell RC = Outputs.get(DefR.Reg);
2772 bool Eval = Signed ? evaluateSEXTr(R1, BW, Bits, Inputs, RC)
2773 : evaluateZEXTr(R1, BW, Bits, Inputs, RC);
2774 if (!Eval)
2775 return false;
2776 Outputs.update(DefR.Reg, RC);
2777 return true;
2778}
2779
2780
2781bool HexagonConstEvaluator::evaluateHexVector1(const MachineInstr &MI,
2782 const CellMap &Inputs, CellMap &Outputs) {
2783 // DefR = op R1
2784 Register DefR(MI.getOperand(0));
2785 Register R1(MI.getOperand(1));
2786 assert(Inputs.has(R1.Reg));
2787 LatticeCell RC = Outputs.get(DefR.Reg);
2788 bool Eval;
2789
2790 unsigned Opc = MI.getOpcode();
2791 switch (Opc) {
2792 case Hexagon::S2_vsplatrb:
2793 // Rd = 4 times Rs:0..7
2794 Eval = evaluateSplatr(R1, 8, 4, Inputs, RC);
2795 break;
2796 case Hexagon::S2_vsplatrh:
2797 // Rdd = 4 times Rs:0..15
2798 Eval = evaluateSplatr(R1, 16, 4, Inputs, RC);
2799 break;
2800 default:
2801 return false;
2802 }
2803
2804 if (!Eval)
2805 return false;
2806 Outputs.update(DefR.Reg, RC);
2807 return true;
2808}
2809
2810
2811bool HexagonConstEvaluator::rewriteHexConstDefs(MachineInstr &MI,
2812 const CellMap &Inputs, bool &AllDefs) {
2813 AllDefs = false;
2814
2815 // Some diagnostics.
2816 // DEBUG({...}) gets confused with all this code as an argument.
2817#ifndef NDEBUG
2818 bool Debugging = llvm::DebugFlag &&
2819 llvm::isCurrentDebugType(DEBUG_TYPE);
2820 if (Debugging) {
2821 bool Const = true, HasUse = false;
2822 for (const MachineOperand &MO : MI.operands()) {
2823 if (!MO.isReg() || !MO.isUse() || MO.isImplicit())
2824 continue;
2825 Register R(MO);
2826 if (!TargetRegisterInfo::isVirtualRegister(R.Reg))
2827 continue;
2828 HasUse = true;
2829 // PHIs can legitimately have "top" cells after propagation.
2830 if (!MI.isPHI() && !Inputs.has(R.Reg)) {
2831 dbgs() << "Top " << PrintReg(R.Reg, &HRI, R.SubReg)
2832 << " in MI: " << MI;
2833 continue;
2834 }
2835 const LatticeCell &L = Inputs.get(R.Reg);
2836 Const &= L.isSingle();
2837 if (!Const)
2838 break;
2839 }
2840 if (HasUse && Const) {
2841 if (!MI.isCopy()) {
2842 dbgs() << "CONST: " << MI;
2843 for (const MachineOperand &MO : MI.operands()) {
2844 if (!MO.isReg() || !MO.isUse() || MO.isImplicit())
2845 continue;
2846 unsigned R = MO.getReg();
2847 dbgs() << PrintReg(R, &TRI) << ": " << Inputs.get(R) << "\n";
2848 }
2849 }
2850 }
2851 }
2852#endif
2853
2854 // Avoid generating TFRIs for register transfers---this will keep the
2855 // coalescing opportunities.
2856 if (MI.isCopy())
2857 return false;
2858
2859 // Collect all virtual register-def operands.
2860 SmallVector<unsigned,2> DefRegs;
2861 for (const MachineOperand &MO : MI.operands()) {
2862 if (!MO.isReg() || !MO.isDef())
2863 continue;
2864 unsigned R = MO.getReg();
2865 if (!TargetRegisterInfo::isVirtualRegister(R))
2866 continue;
2867 assert(!MO.getSubReg());
2868 assert(Inputs.has(R));
2869 DefRegs.push_back(R);
2870 }
2871
2872 MachineBasicBlock &B = *MI.getParent();
2873 const DebugLoc &DL = MI.getDebugLoc();
2874 unsigned ChangedNum = 0;
2875#ifndef NDEBUG
2876 SmallVector<const MachineInstr*,4> NewInstrs;
2877#endif
2878
2879 // For each defined register, if it is a constant, create an instruction
2880 // NewR = const
2881 // and replace all uses of the defined register with NewR.
2882 for (unsigned i = 0, n = DefRegs.size(); i < n; ++i) {
2883 unsigned R = DefRegs[i];
2884 const LatticeCell &L = Inputs.get(R);
2885 if (L.isBottom())
2886 continue;
2887 const TargetRegisterClass *RC = MRI->getRegClass(R);
2888 MachineBasicBlock::iterator At = MI.getIterator();
2889
2890 if (!L.isSingle()) {
2891 // If this a zero/non-zero cell, we can fold a definition
2892 // of a predicate register.
2893 typedef ConstantProperties P;
2894 uint64_t Ps = L.properties();
2895 if (!(Ps & (P::Zero|P::NonZero)))
2896 continue;
2897 const TargetRegisterClass *PredRC = &Hexagon::PredRegsRegClass;
2898 if (RC != PredRC)
2899 continue;
2900 const MCInstrDesc *NewD = (Ps & P::Zero) ?
2901 &HII.get(Hexagon::TFR_PdFalse) :
2902 &HII.get(Hexagon::TFR_PdTrue);
2903 unsigned NewR = MRI->createVirtualRegister(PredRC);
2904 const MachineInstrBuilder &MIB = BuildMI(B, At, DL, *NewD, NewR);
2905 (void)MIB;
2906#ifndef NDEBUG
2907 NewInstrs.push_back(&*MIB);
2908#endif
2909 replaceAllRegUsesWith(R, NewR);
2910 } else {
2911 // This cell has a single value.
2912 APInt A;
2913 if (!constToInt(L.Value, A) || !A.isSignedIntN(64))
2914 continue;
2915 const TargetRegisterClass *NewRC;
2916 const MCInstrDesc *NewD;
2917
2918 unsigned W = getRegBitWidth(R);
2919 int64_t V = A.getSExtValue();
2920 assert(W == 32 || W == 64);
2921 if (W == 32)
2922 NewRC = &Hexagon::IntRegsRegClass;
2923 else
2924 NewRC = &Hexagon::DoubleRegsRegClass;
2925 unsigned NewR = MRI->createVirtualRegister(NewRC);
2926 const MachineInstr *NewMI;
2927
2928 if (W == 32) {
2929 NewD = &HII.get(Hexagon::A2_tfrsi);
2930 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2931 .addImm(V);
2932 } else {
2933 if (A.isSignedIntN(8)) {
2934 NewD = &HII.get(Hexagon::A2_tfrpi);
2935 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2936 .addImm(V);
2937 } else {
2938 int32_t Hi = V >> 32;
2939 int32_t Lo = V & 0xFFFFFFFFLL;
2940 if (isInt<8>(Hi) && isInt<8>(Lo)) {
2941 NewD = &HII.get(Hexagon::A2_combineii);
2942 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2943 .addImm(Hi)
2944 .addImm(Lo);
2945 } else {
2946 NewD = &HII.get(Hexagon::CONST64_Int_Real);
2947 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2948 .addImm(V);
2949 }
2950 }
2951 }
2952 (void)NewMI;
2953#ifndef NDEBUG
2954 NewInstrs.push_back(NewMI);
2955#endif
2956 replaceAllRegUsesWith(R, NewR);
2957 }
2958 ChangedNum++;
2959 }
2960
2961 DEBUG({
2962 if (!NewInstrs.empty()) {
2963 MachineFunction &MF = *MI.getParent()->getParent();
2964 dbgs() << "In function: " << MF.getFunction()->getName() << "\n";
2965 dbgs() << "Rewrite: for " << MI << " created " << *NewInstrs[0];
2966 for (unsigned i = 1; i < NewInstrs.size(); ++i)
2967 dbgs() << " " << *NewInstrs[i];
2968 }
2969 });
2970
2971 AllDefs = (ChangedNum == DefRegs.size());
2972 return ChangedNum > 0;
2973}
2974
2975
2976bool HexagonConstEvaluator::rewriteHexConstUses(MachineInstr &MI,
2977 const CellMap &Inputs) {
2978 bool Changed = false;
2979 unsigned Opc = MI.getOpcode();
2980 MachineBasicBlock &B = *MI.getParent();
2981 const DebugLoc &DL = MI.getDebugLoc();
2982 MachineBasicBlock::iterator At = MI.getIterator();
2983 MachineInstr *NewMI = NULL;
2984
2985 switch (Opc) {
2986 case Hexagon::M2_maci:
2987 // Convert DefR += mpyi(R2, R3)
2988 // to DefR += mpyi(R, #imm),
2989 // or DefR -= mpyi(R, #imm).
2990 {
2991 Register DefR(MI.getOperand(0));
2992 assert(!DefR.SubReg);
2993 Register R2(MI.getOperand(2));
2994 Register R3(MI.getOperand(3));
2995 assert(Inputs.has(R2.Reg) && Inputs.has(R3.Reg));
2996 LatticeCell LS2, LS3;
2997 // It is enough to get one of the input cells, since we will only try
2998 // to replace one argument---whichever happens to be a single constant.
2999 bool HasC2 = getCell(R2, Inputs, LS2), HasC3 = getCell(R3, Inputs, LS3);
3000 if (!HasC2 && !HasC3)
3001 return false;
3002 bool Zero = ((HasC2 && (LS2.properties() & ConstantProperties::Zero)) ||
3003 (HasC3 && (LS3.properties() & ConstantProperties::Zero)));
3004 // If one of the operands is zero, eliminate the multiplication.
3005 if (Zero) {
3006 // DefR == R1 (tied operands).
3007 MachineOperand &Acc = MI.getOperand(1);
3008 Register R1(Acc);
3009 unsigned NewR = R1.Reg;
3010 if (R1.SubReg) {
3011 // Generate COPY. FIXME: Replace with the register:subregister.
3012 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3013 NewR = MRI->createVirtualRegister(RC);
3014 NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3015 .addReg(R1.Reg, getRegState(Acc), R1.SubReg);
3016 }
3017 replaceAllRegUsesWith(DefR.Reg, NewR);
3018 MRI->clearKillFlags(NewR);
3019 Changed = true;
3020 break;
3021 }
3022
3023 bool Swap = false;
3024 if (!LS3.isSingle()) {
3025 if (!LS2.isSingle())
3026 return false;
3027 Swap = true;
3028 }
3029 const LatticeCell &LI = Swap ? LS2 : LS3;
3030 const MachineOperand &OpR2 = Swap ? MI.getOperand(3)
3031 : MI.getOperand(2);
3032 // LI is single here.
3033 APInt A;
3034 if (!constToInt(LI.Value, A) || !A.isSignedIntN(8))
3035 return false;
3036 int64_t V = A.getSExtValue();
3037 const MCInstrDesc &D = (V >= 0) ? HII.get(Hexagon::M2_macsip)
3038 : HII.get(Hexagon::M2_macsin);
3039 if (V < 0)
3040 V = -V;
3041 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3042 unsigned NewR = MRI->createVirtualRegister(RC);
3043 const MachineOperand &Src1 = MI.getOperand(1);
3044 NewMI = BuildMI(B, At, DL, D, NewR)
3045 .addReg(Src1.getReg(), getRegState(Src1), Src1.getSubReg())
3046 .addReg(OpR2.getReg(), getRegState(OpR2), OpR2.getSubReg())
3047 .addImm(V);
3048 replaceAllRegUsesWith(DefR.Reg, NewR);
3049 Changed = true;
3050 break;
3051 }
3052
3053 case Hexagon::A2_and:
3054 {
3055 Register R1(MI.getOperand(1));
3056 Register R2(MI.getOperand(2));
3057 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3058 LatticeCell LS1, LS2;
3059 unsigned CopyOf = 0;
3060 // Check if any of the operands is -1 (i.e. all bits set).
3061 if (getCell(R1, Inputs, LS1) && LS1.isSingle()) {
3062 APInt M1;
3063 if (constToInt(LS1.Value, M1) && !~M1)
3064 CopyOf = 2;
3065 }
3066 else if (getCell(R2, Inputs, LS2) && LS2.isSingle()) {
3067 APInt M1;
3068 if (constToInt(LS2.Value, M1) && !~M1)
3069 CopyOf = 1;
3070 }
3071 if (!CopyOf)
3072 return false;
3073 MachineOperand &SO = MI.getOperand(CopyOf);
3074 Register SR(SO);
3075 Register DefR(MI.getOperand(0));
3076 unsigned NewR = SR.Reg;
3077 if (SR.SubReg) {
3078 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3079 NewR = MRI->createVirtualRegister(RC);
3080 NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3081 .addReg(SR.Reg, getRegState(SO), SR.SubReg);
3082 }
3083 replaceAllRegUsesWith(DefR.Reg, NewR);
3084 MRI->clearKillFlags(NewR);
3085 Changed = true;
3086 }
3087 break;
3088
3089 case Hexagon::A2_or:
3090 {
3091 Register R1(MI.getOperand(1));
3092 Register R2(MI.getOperand(2));
3093 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3094 LatticeCell LS1, LS2;
3095 unsigned CopyOf = 0;
3096 typedef ConstantProperties P;
3097 if (getCell(R1, Inputs, LS1) && (LS1.properties() & P::Zero))
3098 CopyOf = 2;
3099 else if (getCell(R2, Inputs, LS2) && (LS2.properties() & P::Zero))
3100 CopyOf = 1;
3101 if (!CopyOf)
3102 return false;
3103 MachineOperand &SO = MI.getOperand(CopyOf);
3104 Register SR(SO);
3105 Register DefR(MI.getOperand(0));
3106 unsigned NewR = SR.Reg;
3107 if (SR.SubReg) {
3108 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3109 NewR = MRI->createVirtualRegister(RC);
3110 NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3111 .addReg(SR.Reg, getRegState(SO), SR.SubReg);
3112 }
3113 replaceAllRegUsesWith(DefR.Reg, NewR);
3114 MRI->clearKillFlags(NewR);
3115 Changed = true;
3116 }
3117 break;
3118 }
3119
3120 if (NewMI) {
3121 // clear all the kill flags of this new instruction.
3122 for (MachineOperand &MO : NewMI->operands())
3123 if (MO.isReg() && MO.isUse())
3124 MO.setIsKill(false);
3125 }
3126
3127 DEBUG({
3128 if (NewMI) {
3129 dbgs() << "Rewrite: for " << MI;
3130 if (NewMI != &MI)
3131 dbgs() << " created " << *NewMI;
3132 else
3133 dbgs() << " modified the instruction itself and created:" << *NewMI;
3134 }
3135 });
3136
3137 return Changed;
3138}
3139
3140
3141void HexagonConstEvaluator::replaceAllRegUsesWith(unsigned FromReg,
3142 unsigned ToReg) {
3143 assert(TargetRegisterInfo::isVirtualRegister(FromReg));
3144 assert(TargetRegisterInfo::isVirtualRegister(ToReg));
3145 for (auto I = MRI->use_begin(FromReg), E = MRI->use_end(); I != E;) {
3146 MachineOperand &O = *I;
3147 ++I;
3148 O.setReg(ToReg);
3149 }
3150}
3151
3152
3153bool HexagonConstEvaluator::rewriteHexBranch(MachineInstr &BrI,
3154 const CellMap &Inputs) {
3155 MachineBasicBlock &B = *BrI.getParent();
3156 unsigned NumOp = BrI.getNumOperands();
3157 if (!NumOp)
3158 return false;
3159
3160 bool FallsThru;
3161 SetVector<const MachineBasicBlock*> Targets;
3162 bool Eval = evaluate(BrI, Inputs, Targets, FallsThru);
3163 unsigned NumTargets = Targets.size();
3164 if (!Eval || NumTargets > 1 || (NumTargets == 1 && FallsThru))
3165 return false;
3166 if (BrI.getOpcode() == Hexagon::J2_jump)
3167 return false;
3168
3169 DEBUG(dbgs() << "Rewrite(BB#" << B.getNumber() << "):" << BrI);
3170 bool Rewritten = false;
3171 if (NumTargets > 0) {
3172 assert(!FallsThru && "This should have been checked before");
3173 // MIB.addMBB needs non-const pointer.
3174 MachineBasicBlock *TargetB = const_cast<MachineBasicBlock*>(Targets[0]);
3175 bool Moot = B.isLayoutSuccessor(TargetB);
3176 if (!Moot) {
3177 // If we build a branch here, we must make sure that it won't be
3178 // erased as "non-executable". We can't mark any new instructions
3179 // as executable here, so we need to overwrite the BrI, which we
3180 // know is executable.
3181 const MCInstrDesc &JD = HII.get(Hexagon::J2_jump);
3182 auto NI = BuildMI(B, BrI.getIterator(), BrI.getDebugLoc(), JD)
3183 .addMBB(TargetB);
3184 BrI.setDesc(JD);
3185 while (BrI.getNumOperands() > 0)
3186 BrI.RemoveOperand(0);
3187 // This ensures that all implicit operands (e.g. %R31<imp-def>, etc)
3188 // are present in the rewritten branch.
3189 for (auto &Op : NI->operands())
3190 BrI.addOperand(Op);
3191 NI->eraseFromParent();
3192 Rewritten = true;
3193 }
3194 }
3195
3196 // Do not erase instructions. A newly created instruction could get
3197 // the same address as an instruction marked as executable during the
3198 // propagation.
3199 if (!Rewritten)
3200 replaceWithNop(BrI);
3201 return true;
3202}
3203
3204
3205// --------------------------------------------------------------------
3206FunctionPass *llvm::createHexagonConstPropagationPass() {
3207 return new HexagonConstPropagation();
3208}
3209