1 //===--- HexagonConstPropagation.cpp --------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "hcp"
12 #include "HexagonInstrInfo.h"
13 #include "HexagonRegisterInfo.h"
14 #include "HexagonSubtarget.h"
15 #include "llvm/ADT/APFloat.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/ADT/SetVector.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/CodeGen/MachineBasicBlock.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineOperand.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetRegisterInfo.h"
50 // Properties of a value that are tracked by the propagation.
51 // A property that is marked as present (i.e. bit is set) dentes that the
52 // value is known (proven) to have this property. Not all combinations
53 // of bits make sense, for example Zero and NonZero are mutually exclusive,
54 // but on the other hand, Zero implies Finite. In this case, whenever
55 // the Zero property is present, Finite should also be present.
56 class ConstantProperties {
66 NumericProperties = (Zero|NonZero|Finite|Infinity|NaN|SignedZero),
69 SignProperties = (PosOrZero|NegOrZero),
70 Everything = (NumericProperties|SignProperties)
73 // For a given constant, deduce the set of trackable properties that this
75 static uint32_t deduce(const Constant *C);
78 // A representation of a register as it can appear in a MachineOperand,
79 // i.e. a pair register:subregister.
83 explicit Register(unsigned R, unsigned SR = 0) : Reg(R), SubReg(SR) {}
84 explicit Register(const MachineOperand &MO)
85 : Reg(MO.getReg()), SubReg(MO.getSubReg()) {}
87 void print(const TargetRegisterInfo *TRI = nullptr) const {
88 dbgs() << PrintReg(Reg, TRI, SubReg);
91 bool operator== (const Register &R) const {
92 return (Reg == R.Reg) && (SubReg == R.SubReg);
96 // Lattice cell, based on that was described in the W-Z paper on constant
98 // Latice cell will be allowed to hold multiple constant values. While
99 // multiple values would normally indicate "bottom", we can still derive
100 // some useful information from them. For example, comparison X > 0
101 // could be folded if all the values in the cell associated with X are
105 enum { Normal, Top, Bottom };
107 static const unsigned MaxCellSize = 4;
111 unsigned IsSpecial:1;
117 const Constant *Value;
118 const Constant *Values[MaxCellSize];
121 LatticeCell() : Kind(Top), Size(0), IsSpecial(false) {
122 for (unsigned i = 0; i < MaxCellSize; ++i)
126 bool meet(const LatticeCell &L);
127 bool add(const Constant *C);
128 bool add(uint32_t Property);
129 uint32_t properties() const;
130 unsigned size() const { return Size; }
132 LatticeCell &operator= (const LatticeCell &L) {
134 // This memcpy also copies Properties (when L.Size == 0).
135 uint32_t N = L.IsSpecial ? sizeof L.Properties
136 : L.Size*sizeof(const Constant*);
137 memcpy(Values, L.Values, N);
140 IsSpecial = L.IsSpecial;
145 bool isSingle() const { return size() == 1; }
146 bool isProperty() const { return IsSpecial; }
147 bool isTop() const { return Kind == Top; }
148 bool isBottom() const { return Kind == Bottom; }
151 bool Changed = (Kind != Bottom);
158 void print(raw_ostream &os) const;
167 bool convertToProperty();
170 raw_ostream &operator<< (raw_ostream &os, const LatticeCell &L) {
175 class MachineConstEvaluator;
177 class MachineConstPropagator {
179 MachineConstPropagator(MachineConstEvaluator &E) : MCE(E) {
183 // Mapping: vreg -> cell
184 // The keys are registers _without_ subregisters. This won't allow
185 // definitions in the form of "vreg:subreg<def> = ...". Such definitions
186 // would be questionable from the point of view of SSA, since the "vreg"
187 // could not be initialized in its entirety (specifically, an instruction
188 // defining the "other part" of "vreg" would also count as a definition
189 // of "vreg", which would violate the SSA).
190 // If a value of a pair vreg:subreg needs to be obtained, the cell for
191 // "vreg" needs to be looked up, and then the value of subregister "subreg"
192 // needs to be evaluated.
200 void clear() { Map.clear(); }
202 bool has(unsigned R) const {
203 // All non-virtual registers are considered "bottom".
204 if (!TargetRegisterInfo::isVirtualRegister(R))
206 MapType::const_iterator F = Map.find(R);
207 return F != Map.end();
210 const LatticeCell &get(unsigned R) const {
211 if (!TargetRegisterInfo::isVirtualRegister(R))
213 MapType::const_iterator F = Map.find(R);
219 // Invalidates any const references.
220 void update(unsigned R, const LatticeCell &L) {
224 void print(raw_ostream &os, const TargetRegisterInfo &TRI) const;
227 typedef std::map<unsigned,LatticeCell> MapType;
229 // To avoid creating "top" entries, return a const reference to
230 // this cell in "get". Also, have a "Bottom" cell to return from
231 // get when a value of a physical register is requested.
232 LatticeCell Top, Bottom;
235 typedef MapType::const_iterator const_iterator;
236 const_iterator begin() const { return Map.begin(); }
237 const_iterator end() const { return Map.end(); }
240 bool run(MachineFunction &MF);
243 void visitPHI(const MachineInstr &PN);
244 void visitNonBranch(const MachineInstr &MI);
245 void visitBranchesFrom(const MachineInstr &BrI);
246 void visitUsesOf(unsigned R);
247 bool computeBlockSuccessors(const MachineBasicBlock *MB,
248 SetVector<const MachineBasicBlock*> &Targets);
249 void removeCFGEdge(MachineBasicBlock *From, MachineBasicBlock *To);
251 void propagate(MachineFunction &MF);
252 bool rewrite(MachineFunction &MF);
254 MachineRegisterInfo *MRI;
255 MachineConstEvaluator &MCE;
257 typedef std::pair<unsigned,unsigned> CFGEdge;
258 typedef std::set<CFGEdge> SetOfCFGEdge;
259 typedef std::set<const MachineInstr*> SetOfInstr;
260 typedef std::queue<CFGEdge> QueueOfCFGEdge;
264 SetOfCFGEdge EdgeExec;
265 SetOfInstr InstrExec;
266 QueueOfCFGEdge FlowQ;
269 // The "evaluator/rewriter" of machine instructions. This is an abstract
270 // base class that provides the interface that the propagator will use,
271 // as well as some helper functions that are target-independent.
272 class MachineConstEvaluator {
274 MachineConstEvaluator(MachineFunction &Fn)
275 : TRI(*Fn.getSubtarget().getRegisterInfo()),
276 MF(Fn), CX(Fn.getFunction()->getContext()) {}
277 virtual ~MachineConstEvaluator() = default;
279 // The required interface:
280 // - A set of three "evaluate" functions. Each returns "true" if the
281 // computation succeeded, "false" otherwise.
282 // (1) Given an instruction MI, and the map with input values "Inputs",
283 // compute the set of output values "Outputs". An example of when
284 // the computation can "fail" is if MI is not an instruction that
285 // is recognized by the evaluator.
286 // (2) Given a register R (as reg:subreg), compute the cell that
287 // corresponds to the "subreg" part of the given register.
288 // (3) Given a branch instruction BrI, compute the set of target blocks.
289 // If the branch can fall-through, add null (0) to the list of
291 // - A function "rewrite", that given the cell map after propagation,
292 // could rewrite instruction MI in a more beneficial form. Return
293 // "true" if a change has been made, "false" otherwise.
294 typedef MachineConstPropagator::CellMap CellMap;
295 virtual bool evaluate(const MachineInstr &MI, const CellMap &Inputs,
296 CellMap &Outputs) = 0;
297 virtual bool evaluate(const Register &R, const LatticeCell &SrcC,
298 LatticeCell &Result) = 0;
299 virtual bool evaluate(const MachineInstr &BrI, const CellMap &Inputs,
300 SetVector<const MachineBasicBlock*> &Targets,
301 bool &CanFallThru) = 0;
302 virtual bool rewrite(MachineInstr &MI, const CellMap &Inputs) = 0;
304 const TargetRegisterInfo &TRI;
315 L = 0x04, // Less-than property.
316 G = 0x08, // Greater-than property.
317 U = 0x40, // Unsigned property.
328 static uint32_t negate(uint32_t Cmp) {
333 assert((Cmp & (L|G)) != (L|G));
340 bool getCell(const Register &R, const CellMap &Inputs, LatticeCell &RC);
341 bool constToInt(const Constant *C, APInt &Val) const;
342 bool constToFloat(const Constant *C, APFloat &Val) const;
343 const ConstantInt *intToConst(const APInt &Val) const;
346 bool evaluateCMPrr(uint32_t Cmp, const Register &R1, const Register &R2,
347 const CellMap &Inputs, bool &Result);
348 bool evaluateCMPri(uint32_t Cmp, const Register &R1, const APInt &A2,
349 const CellMap &Inputs, bool &Result);
350 bool evaluateCMPrp(uint32_t Cmp, const Register &R1, uint64_t Props2,
351 const CellMap &Inputs, bool &Result);
352 bool evaluateCMPii(uint32_t Cmp, const APInt &A1, const APInt &A2,
354 bool evaluateCMPpi(uint32_t Cmp, uint32_t Props, const APInt &A2,
356 bool evaluateCMPpp(uint32_t Cmp, uint32_t Props1, uint32_t Props2,
359 bool evaluateCOPY(const Register &R1, const CellMap &Inputs,
360 LatticeCell &Result);
362 // Logical operations.
363 bool evaluateANDrr(const Register &R1, const Register &R2,
364 const CellMap &Inputs, LatticeCell &Result);
365 bool evaluateANDri(const Register &R1, const APInt &A2,
366 const CellMap &Inputs, LatticeCell &Result);
367 bool evaluateANDii(const APInt &A1, const APInt &A2, APInt &Result);
368 bool evaluateORrr(const Register &R1, const Register &R2,
369 const CellMap &Inputs, LatticeCell &Result);
370 bool evaluateORri(const Register &R1, const APInt &A2,
371 const CellMap &Inputs, LatticeCell &Result);
372 bool evaluateORii(const APInt &A1, const APInt &A2, APInt &Result);
373 bool evaluateXORrr(const Register &R1, const Register &R2,
374 const CellMap &Inputs, LatticeCell &Result);
375 bool evaluateXORri(const Register &R1, const APInt &A2,
376 const CellMap &Inputs, LatticeCell &Result);
377 bool evaluateXORii(const APInt &A1, const APInt &A2, APInt &Result);
380 bool evaluateZEXTr(const Register &R1, unsigned Width, unsigned Bits,
381 const CellMap &Inputs, LatticeCell &Result);
382 bool evaluateZEXTi(const APInt &A1, unsigned Width, unsigned Bits,
384 bool evaluateSEXTr(const Register &R1, unsigned Width, unsigned Bits,
385 const CellMap &Inputs, LatticeCell &Result);
386 bool evaluateSEXTi(const APInt &A1, unsigned Width, unsigned Bits,
389 // Leading/trailing bits.
390 bool evaluateCLBr(const Register &R1, bool Zeros, bool Ones,
391 const CellMap &Inputs, LatticeCell &Result);
392 bool evaluateCLBi(const APInt &A1, bool Zeros, bool Ones, APInt &Result);
393 bool evaluateCTBr(const Register &R1, bool Zeros, bool Ones,
394 const CellMap &Inputs, LatticeCell &Result);
395 bool evaluateCTBi(const APInt &A1, bool Zeros, bool Ones, APInt &Result);
398 bool evaluateEXTRACTr(const Register &R1, unsigned Width, unsigned Bits,
399 unsigned Offset, bool Signed, const CellMap &Inputs,
400 LatticeCell &Result);
401 bool evaluateEXTRACTi(const APInt &A1, unsigned Bits, unsigned Offset,
402 bool Signed, APInt &Result);
403 // Vector operations.
404 bool evaluateSplatr(const Register &R1, unsigned Bits, unsigned Count,
405 const CellMap &Inputs, LatticeCell &Result);
406 bool evaluateSplati(const APInt &A1, unsigned Bits, unsigned Count,
410 } // end anonymous namespace
412 uint32_t ConstantProperties::deduce(const Constant *C) {
413 if (isa<ConstantInt>(C)) {
414 const ConstantInt *CI = cast<ConstantInt>(C);
416 return Zero | PosOrZero | NegOrZero | Finite;
417 uint32_t Props = (NonZero | Finite);
418 if (CI->isNegative())
419 return Props | NegOrZero;
420 return Props | PosOrZero;
423 if (isa<ConstantFP>(C)) {
424 const ConstantFP *CF = cast<ConstantFP>(C);
425 uint32_t Props = CF->isNegative() ? (NegOrZero|NonZero)
428 return (Props & ~NumericProperties) | (Zero|Finite);
429 Props = (Props & ~NumericProperties) | NonZero;
431 return (Props & ~NumericProperties) | NaN;
432 const APFloat &Val = CF->getValueAPF();
433 if (Val.isInfinity())
434 return (Props & ~NumericProperties) | Infinity;
442 // Convert a cell from a set of specific values to a cell that tracks
444 bool LatticeCell::convertToProperty() {
447 // Corner case: converting a fresh (top) cell to "special".
448 // This can happen, when adding a property to a top cell.
449 uint32_t Everything = ConstantProperties::Everything;
450 uint32_t Ps = !isTop() ? properties()
452 if (Ps != ConstantProperties::Unknown) {
461 void LatticeCell::print(raw_ostream &os) const {
464 uint32_t Ps = properties();
465 if (Ps & ConstantProperties::Zero)
467 if (Ps & ConstantProperties::NonZero)
469 if (Ps & ConstantProperties::Finite)
471 if (Ps & ConstantProperties::Infinity)
473 if (Ps & ConstantProperties::NaN)
475 if (Ps & ConstantProperties::PosOrZero)
477 if (Ps & ConstantProperties::NegOrZero)
486 } else if (isTop()) {
489 for (unsigned i = 0; i < size(); ++i) {
490 const Constant *C = Values[i];
499 // "Meet" operation on two cells. This is the key of the propagation
501 bool LatticeCell::meet(const LatticeCell &L) {
502 bool Changed = false;
504 Changed = setBottom();
505 if (isBottom() || L.isTop())
509 // L can be neither Top nor Bottom, so *this must have changed.
513 // Top/bottom cases covered. Need to integrate L's set into ours.
515 return add(L.properties());
516 for (unsigned i = 0; i < L.size(); ++i) {
517 const Constant *LC = L.Values[i];
523 // Add a new constant to the cell. This is actually where the cell update
524 // happens. If a cell has room for more constants, the new constant is added.
525 // Otherwise, the cell is converted to a "property" cell (i.e. a cell that
526 // will track properties of the associated values, and not the values
527 // themselves. Care is taken to handle special cases, like "bottom", etc.
528 bool LatticeCell::add(const Constant *LC) {
534 // Cell is not special. Try to add the constant here first,
537 while (Index < Size) {
538 const Constant *C = Values[Index];
539 // If the constant is already here, no change is needed.
544 if (Index < MaxCellSize) {
552 bool Changed = false;
554 // This cell is special, or is not special, but is full. After this
555 // it will be special.
556 Changed = convertToProperty();
557 uint32_t Ps = properties();
558 uint32_t NewPs = Ps & ConstantProperties::deduce(LC);
559 if (NewPs == ConstantProperties::Unknown) {
570 // Add a property to the cell. This will force the cell to become a property-
572 bool LatticeCell::add(uint32_t Property) {
573 bool Changed = convertToProperty();
574 uint32_t Ps = properties();
575 if (Ps == (Ps & Property))
577 Properties = Property & Ps;
581 // Return the properties of the values in the cell. This is valid for any
582 // cell, and does not alter the cell itself.
583 uint32_t LatticeCell::properties() const {
586 assert(!isTop() && "Should not call this for a top cell");
588 return ConstantProperties::Unknown;
590 assert(size() > 0 && "Empty cell");
591 uint32_t Ps = ConstantProperties::deduce(Values[0]);
592 for (unsigned i = 1; i < size(); ++i) {
593 if (Ps == ConstantProperties::Unknown)
595 Ps &= ConstantProperties::deduce(Values[i]);
600 void MachineConstPropagator::CellMap::print(raw_ostream &os,
601 const TargetRegisterInfo &TRI) const {
603 dbgs() << " " << PrintReg(I.first, &TRI) << " -> " << I.second << '\n';
606 void MachineConstPropagator::visitPHI(const MachineInstr &PN) {
607 const MachineBasicBlock *MB = PN.getParent();
608 unsigned MBN = MB->getNumber();
609 DEBUG(dbgs() << "Visiting FI(BB#" << MBN << "): " << PN);
611 const MachineOperand &MD = PN.getOperand(0);
613 assert(TargetRegisterInfo::isVirtualRegister(DefR.Reg));
615 bool Changed = false;
617 // If the def has a sub-register, set the corresponding cell to "bottom".
620 const LatticeCell &T = Cells.get(DefR.Reg);
621 Changed = !T.isBottom();
622 Cells.update(DefR.Reg, Bottom);
624 visitUsesOf(DefR.Reg);
628 LatticeCell DefC = Cells.get(DefR.Reg);
630 for (unsigned i = 1, n = PN.getNumOperands(); i < n; i += 2) {
631 const MachineBasicBlock *PB = PN.getOperand(i+1).getMBB();
632 unsigned PBN = PB->getNumber();
633 if (!EdgeExec.count(CFGEdge(PBN, MBN))) {
634 DEBUG(dbgs() << " edge BB#" << PBN << "->BB#" << MBN
635 << " not executable\n");
638 const MachineOperand &SO = PN.getOperand(i);
640 // If the input is not a virtual register, we don't really know what
642 if (!TargetRegisterInfo::isVirtualRegister(UseR.Reg))
644 // If there is no cell for an input register, it means top.
645 if (!Cells.has(UseR.Reg))
649 bool Eval = MCE.evaluate(UseR, Cells.get(UseR.Reg), SrcC);
650 DEBUG(dbgs() << " edge from BB#" << PBN << ": "
651 << PrintReg(UseR.Reg, &MCE.TRI, UseR.SubReg)
653 Changed |= Eval ? DefC.meet(SrcC)
655 Cells.update(DefR.Reg, DefC);
660 visitUsesOf(DefR.Reg);
663 void MachineConstPropagator::visitNonBranch(const MachineInstr &MI) {
664 DEBUG(dbgs() << "Visiting MI(BB#" << MI.getParent()->getNumber()
667 bool Eval = MCE.evaluate(MI, Cells, Outputs);
670 dbgs() << " outputs:";
671 for (auto &I : Outputs)
672 dbgs() << ' ' << I.second;
677 // Update outputs. If the value was not computed, set all the
678 // def cells to bottom.
679 for (const MachineOperand &MO : MI.operands()) {
680 if (!MO.isReg() || !MO.isDef())
683 // Only track virtual registers.
684 if (!TargetRegisterInfo::isVirtualRegister(DefR.Reg))
686 bool Changed = false;
687 // If the evaluation failed, set cells for all output registers to bottom.
689 const LatticeCell &T = Cells.get(DefR.Reg);
690 Changed = !T.isBottom();
691 Cells.update(DefR.Reg, Bottom);
693 // Find the corresponding cell in the computed outputs.
694 // If it's not there, go on to the next def.
695 if (!Outputs.has(DefR.Reg))
697 LatticeCell RC = Cells.get(DefR.Reg);
698 Changed = RC.meet(Outputs.get(DefR.Reg));
699 Cells.update(DefR.Reg, RC);
702 visitUsesOf(DefR.Reg);
706 // \brief Starting at a given branch, visit remaining branches in the block.
707 // Traverse over the subsequent branches for as long as the preceding one
708 // can fall through. Add all the possible targets to the flow work queue,
709 // including the potential fall-through to the layout-successor block.
710 void MachineConstPropagator::visitBranchesFrom(const MachineInstr &BrI) {
711 const MachineBasicBlock &B = *BrI.getParent();
712 unsigned MBN = B.getNumber();
713 MachineBasicBlock::const_iterator It = BrI.getIterator();
714 MachineBasicBlock::const_iterator End = B.end();
716 SetVector<const MachineBasicBlock*> Targets;
717 bool EvalOk = true, FallsThru = true;
719 const MachineInstr &MI = *It;
720 InstrExec.insert(&MI);
721 DEBUG(dbgs() << "Visiting " << (EvalOk ? "BR" : "br") << "(BB#"
722 << MBN << "): " << MI);
723 // Do not evaluate subsequent branches if the evaluation of any of the
724 // previous branches failed. Keep iterating over the branches only
725 // to mark them as executable.
726 EvalOk = EvalOk && MCE.evaluate(MI, Cells, Targets, FallsThru);
735 // Need to add all CFG successors that lead to EH landing pads.
736 // There won't be explicit branches to these blocks, but they must
738 for (const MachineBasicBlock *SB : B.successors()) {
743 const MachineFunction &MF = *B.getParent();
744 MachineFunction::const_iterator BI = B.getIterator();
745 MachineFunction::const_iterator Next = std::next(BI);
746 if (Next != MF.end())
747 Targets.insert(&*Next);
750 // If the evaluation of the branches failed, make "Targets" to be the
751 // set of all successors of the block from the CFG.
752 // If the evaluation succeeded for all visited branches, then if the
753 // last one set "FallsThru", then add an edge to the layout successor
756 DEBUG(dbgs() << " failed to evaluate a branch...adding all CFG "
758 for (const MachineBasicBlock *SB : B.successors())
762 for (const MachineBasicBlock *TB : Targets) {
763 unsigned TBN = TB->getNumber();
764 DEBUG(dbgs() << " pushing edge BB#" << MBN << " -> BB#" << TBN << "\n");
765 FlowQ.push(CFGEdge(MBN, TBN));
769 void MachineConstPropagator::visitUsesOf(unsigned Reg) {
770 DEBUG(dbgs() << "Visiting uses of " << PrintReg(Reg, &MCE.TRI)
771 << Cells.get(Reg) << '\n');
772 for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) {
773 // Do not process non-executable instructions. They can become exceutable
774 // later (via a flow-edge in the work queue). In such case, the instruc-
775 // tion will be visited at that time.
776 if (!InstrExec.count(&MI))
780 else if (!MI.isBranch())
783 visitBranchesFrom(MI);
787 bool MachineConstPropagator::computeBlockSuccessors(const MachineBasicBlock *MB,
788 SetVector<const MachineBasicBlock*> &Targets) {
789 MachineBasicBlock::const_iterator FirstBr = MB->end();
790 for (const MachineInstr &MI : *MB) {
791 if (MI.isDebugValue())
794 FirstBr = MI.getIterator();
800 MachineBasicBlock::const_iterator End = MB->end();
803 for (MachineBasicBlock::const_iterator I = FirstBr; I != End; ++I) {
804 const MachineInstr &MI = *I;
805 // Can there be debug instructions between branches?
806 if (MI.isDebugValue())
808 if (!InstrExec.count(&MI))
810 bool Eval = MCE.evaluate(MI, Cells, Targets, DoNext);
816 // If the last branch could fall-through, add block's layout successor.
818 MachineFunction::const_iterator BI = MB->getIterator();
819 MachineFunction::const_iterator NextI = std::next(BI);
820 if (NextI != MB->getParent()->end())
821 Targets.insert(&*NextI);
824 // Add all the EH landing pads.
825 for (const MachineBasicBlock *SB : MB->successors())
832 void MachineConstPropagator::removeCFGEdge(MachineBasicBlock *From,
833 MachineBasicBlock *To) {
834 // First, remove the CFG successor/predecessor information.
835 From->removeSuccessor(To);
836 // Remove all corresponding PHI operands in the To block.
837 for (auto I = To->begin(), E = To->getFirstNonPHI(); I != E; ++I) {
838 MachineInstr *PN = &*I;
839 // reg0 = PHI reg1, bb2, reg3, bb4, ...
840 int N = PN->getNumOperands()-2;
842 if (PN->getOperand(N+1).getMBB() == From) {
843 PN->RemoveOperand(N+1);
844 PN->RemoveOperand(N);
851 void MachineConstPropagator::propagate(MachineFunction &MF) {
852 MachineBasicBlock *Entry = GraphTraits<MachineFunction*>::getEntryNode(&MF);
853 unsigned EntryNum = Entry->getNumber();
855 // Start with a fake edge, just to process the entry node.
856 FlowQ.push(CFGEdge(EntryNum, EntryNum));
858 while (!FlowQ.empty()) {
859 CFGEdge Edge = FlowQ.front();
862 DEBUG(dbgs() << "Picked edge BB#" << Edge.first << "->BB#"
863 << Edge.second << '\n');
864 if (Edge.first != EntryNum)
865 if (EdgeExec.count(Edge))
867 EdgeExec.insert(Edge);
868 MachineBasicBlock *SB = MF.getBlockNumbered(Edge.second);
870 // Process the block in three stages:
871 // - visit all PHI nodes,
872 // - visit all non-branch instructions,
873 // - visit block branches.
874 MachineBasicBlock::const_iterator It = SB->begin(), End = SB->end();
876 // Visit PHI nodes in the successor block.
877 while (It != End && It->isPHI()) {
878 InstrExec.insert(&*It);
883 // If the successor block just became executable, visit all instructions.
884 // To see if this is the first time we're visiting it, check the first
885 // non-debug instruction to see if it is executable.
886 while (It != End && It->isDebugValue())
888 assert(It == End || !It->isPHI());
889 // If this block has been visited, go on to the next one.
890 if (It != End && InstrExec.count(&*It))
892 // For now, scan all non-branch instructions. Branches require different
894 while (It != End && !It->isBranch()) {
895 if (!It->isDebugValue()) {
896 InstrExec.insert(&*It);
902 // Time to process the end of the block. This is different from
903 // processing regular (non-branch) instructions, because there can
904 // be multiple branches in a block, and they can cause the block to
907 visitBranchesFrom(*It);
909 // If the block didn't have a branch, add all successor edges to the
910 // work queue. (There should really be only one successor in such case.)
911 unsigned SBN = SB->getNumber();
912 for (const MachineBasicBlock *SSB : SB->successors())
913 FlowQ.push(CFGEdge(SBN, SSB->getNumber()));
918 dbgs() << "Cells after propagation:\n";
919 Cells.print(dbgs(), MCE.TRI);
920 dbgs() << "Dead CFG edges:\n";
921 for (const MachineBasicBlock &B : MF) {
922 unsigned BN = B.getNumber();
923 for (const MachineBasicBlock *SB : B.successors()) {
924 unsigned SN = SB->getNumber();
925 if (!EdgeExec.count(CFGEdge(BN, SN)))
926 dbgs() << " BB#" << BN << " -> BB#" << SN << '\n';
932 bool MachineConstPropagator::rewrite(MachineFunction &MF) {
933 bool Changed = false;
934 // Rewrite all instructions based on the collected cell information.
936 // Traverse the instructions in a post-order, so that rewriting an
937 // instruction can make changes "downstream" in terms of control-flow
938 // without affecting the rewriting process. (We should not change
939 // instructions that have not yet been visited by the rewriter.)
940 // The reason for this is that the rewriter can introduce new vregs,
941 // and replace uses of old vregs (which had corresponding cells
942 // computed during propagation) with these new vregs (which at this
943 // point would not have any cells, and would appear to be "top").
944 // If an attempt was made to evaluate an instruction with a fresh
945 // "top" vreg, it would cause an error (abend) in the evaluator.
947 // Collect the post-order-traversal block ordering. The subsequent
948 // traversal/rewrite will update block successors, so it's safer
949 // if the visiting order it computed ahead of time.
950 std::vector<MachineBasicBlock*> POT;
951 for (MachineBasicBlock *B : post_order(&MF))
955 for (MachineBasicBlock *B : POT) {
956 // Walk the block backwards (which usually begin with the branches).
957 // If any branch is rewritten, we may need to update the successor
958 // information for this block. Unless the block's successors can be
959 // precisely determined (which may not be the case for indirect
960 // branches), we cannot modify any branch.
962 // Compute the successor information.
963 SetVector<const MachineBasicBlock*> Targets;
964 bool HaveTargets = computeBlockSuccessors(B, Targets);
965 // Rewrite the executable instructions. Skip branches if we don't
966 // have block successor information.
967 for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) {
968 MachineInstr &MI = *I;
969 if (InstrExec.count(&MI)) {
970 if (MI.isBranch() && !HaveTargets)
972 Changed |= MCE.rewrite(MI, Cells);
975 // The rewriting could rewrite PHI nodes to non-PHI nodes, causing
976 // regular instructions to appear in between PHI nodes. Bring all
977 // the PHI nodes to the beginning of the block.
978 for (auto I = B->begin(), E = B->end(); I != E; ++I) {
981 // I is not PHI. Find the next PHI node P.
989 // Splice P right before I.
991 // Reset I to point at the just spliced PHI node.
994 // Update the block successor information: remove unnecessary successors.
996 SmallVector<MachineBasicBlock*,2> ToRemove;
997 for (MachineBasicBlock *SB : B->successors()) {
998 if (!Targets.count(SB))
999 ToRemove.push_back(const_cast<MachineBasicBlock*>(SB));
1002 for (unsigned i = 0, n = ToRemove.size(); i < n; ++i)
1003 removeCFGEdge(B, ToRemove[i]);
1004 // If there are any blocks left in the computed targets, it means that
1005 // we think that the block could go somewhere, but the CFG does not.
1006 // This could legitimately happen in blocks that have non-returning
1007 // calls---we would think that the execution can continue, but the
1008 // CFG will not have a successor edge.
1011 // Need to do some final post-processing.
1012 // If a branch was not executable, it will not get rewritten, but should
1013 // be removed (or replaced with something equivalent to a A2_nop). We can't
1014 // erase instructions during rewriting, so this needs to be delayed until
1016 for (MachineBasicBlock &B : MF) {
1017 MachineBasicBlock::iterator I = B.begin(), E = B.end();
1019 auto Next = std::next(I);
1020 if (I->isBranch() && !InstrExec.count(&*I))
1028 // This is the constant propagation algorithm as described by Wegman-Zadeck.
1029 // Most of the terminology comes from there.
1030 bool MachineConstPropagator::run(MachineFunction &MF) {
1031 DEBUG(MF.print(dbgs() << "Starting MachineConstPropagator\n", 0));
1033 MRI = &MF.getRegInfo();
1038 assert(FlowQ.empty());
1041 bool Changed = rewrite(MF);
1044 dbgs() << "End of MachineConstPropagator (Changed=" << Changed << ")\n";
1046 MF.print(dbgs(), 0);
1051 // --------------------------------------------------------------------
1052 // Machine const evaluator.
1054 bool MachineConstEvaluator::getCell(const Register &R, const CellMap &Inputs,
1056 if (!TargetRegisterInfo::isVirtualRegister(R.Reg))
1058 const LatticeCell &L = Inputs.get(R.Reg);
1061 return !RC.isBottom();
1063 bool Eval = evaluate(R, L, RC);
1064 return Eval && !RC.isBottom();
1067 bool MachineConstEvaluator::constToInt(const Constant *C,
1069 const ConstantInt *CI = dyn_cast<ConstantInt>(C);
1072 Val = CI->getValue();
1076 const ConstantInt *MachineConstEvaluator::intToConst(const APInt &Val) const {
1077 return ConstantInt::get(CX, Val);
1080 bool MachineConstEvaluator::evaluateCMPrr(uint32_t Cmp, const Register &R1,
1081 const Register &R2, const CellMap &Inputs, bool &Result) {
1082 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1083 LatticeCell LS1, LS2;
1084 if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2))
1087 bool IsProp1 = LS1.isProperty();
1088 bool IsProp2 = LS2.isProperty();
1090 uint32_t Prop1 = LS1.properties();
1092 return evaluateCMPpp(Cmp, Prop1, LS2.properties(), Result);
1093 uint32_t NegCmp = Comparison::negate(Cmp);
1094 return evaluateCMPrp(NegCmp, R2, Prop1, Inputs, Result);
1097 uint32_t Prop2 = LS2.properties();
1098 return evaluateCMPrp(Cmp, R1, Prop2, Inputs, Result);
1102 bool IsTrue = true, IsFalse = true;
1103 for (unsigned i = 0; i < LS2.size(); ++i) {
1105 bool Computed = constToInt(LS2.Values[i], A) &&
1106 evaluateCMPri(Cmp, R1, A, Inputs, Res);
1112 assert(!IsTrue || !IsFalse);
1113 // The actual logical value of the comparison is same as IsTrue.
1115 // Return true if the result was proven to be true or proven to be false.
1116 return IsTrue || IsFalse;
1119 bool MachineConstEvaluator::evaluateCMPri(uint32_t Cmp, const Register &R1,
1120 const APInt &A2, const CellMap &Inputs, bool &Result) {
1121 assert(Inputs.has(R1.Reg));
1123 if (!getCell(R1, Inputs, LS))
1125 if (LS.isProperty())
1126 return evaluateCMPpi(Cmp, LS.properties(), A2, Result);
1129 bool IsTrue = true, IsFalse = true;
1130 for (unsigned i = 0; i < LS.size(); ++i) {
1132 bool Computed = constToInt(LS.Values[i], A) &&
1133 evaluateCMPii(Cmp, A, A2, Res);
1139 assert(!IsTrue || !IsFalse);
1140 // The actual logical value of the comparison is same as IsTrue.
1142 // Return true if the result was proven to be true or proven to be false.
1143 return IsTrue || IsFalse;
1146 bool MachineConstEvaluator::evaluateCMPrp(uint32_t Cmp, const Register &R1,
1147 uint64_t Props2, const CellMap &Inputs, bool &Result) {
1148 assert(Inputs.has(R1.Reg));
1150 if (!getCell(R1, Inputs, LS))
1152 if (LS.isProperty())
1153 return evaluateCMPpp(Cmp, LS.properties(), Props2, Result);
1156 uint32_t NegCmp = Comparison::negate(Cmp);
1157 bool IsTrue = true, IsFalse = true;
1158 for (unsigned i = 0; i < LS.size(); ++i) {
1160 bool Computed = constToInt(LS.Values[i], A) &&
1161 evaluateCMPpi(NegCmp, Props2, A, Res);
1167 assert(!IsTrue || !IsFalse);
1169 return IsTrue || IsFalse;
1172 bool MachineConstEvaluator::evaluateCMPii(uint32_t Cmp, const APInt &A1,
1173 const APInt &A2, bool &Result) {
1174 // NE is a special kind of comparison (not composed of smaller properties).
1175 if (Cmp == Comparison::NE) {
1176 Result = !APInt::isSameValue(A1, A2);
1179 if (Cmp == Comparison::EQ) {
1180 Result = APInt::isSameValue(A1, A2);
1183 if (Cmp & Comparison::EQ) {
1184 if (APInt::isSameValue(A1, A2))
1185 return (Result = true);
1187 assert((Cmp & (Comparison::L | Comparison::G)) && "Malformed comparison");
1190 unsigned W1 = A1.getBitWidth();
1191 unsigned W2 = A2.getBitWidth();
1192 unsigned MaxW = (W1 >= W2) ? W1 : W2;
1193 if (Cmp & Comparison::U) {
1194 const APInt Zx1 = A1.zextOrSelf(MaxW);
1195 const APInt Zx2 = A2.zextOrSelf(MaxW);
1196 if (Cmp & Comparison::L)
1197 Result = Zx1.ult(Zx2);
1198 else if (Cmp & Comparison::G)
1199 Result = Zx2.ult(Zx1);
1203 // Signed comparison.
1204 const APInt Sx1 = A1.sextOrSelf(MaxW);
1205 const APInt Sx2 = A2.sextOrSelf(MaxW);
1206 if (Cmp & Comparison::L)
1207 Result = Sx1.slt(Sx2);
1208 else if (Cmp & Comparison::G)
1209 Result = Sx2.slt(Sx1);
1213 bool MachineConstEvaluator::evaluateCMPpi(uint32_t Cmp, uint32_t Props,
1214 const APInt &A2, bool &Result) {
1215 if (Props == ConstantProperties::Unknown)
1218 // Should never see NaN here, but check for it for completeness.
1219 if (Props & ConstantProperties::NaN)
1221 // Infinity could theoretically be compared to a number, but the
1222 // presence of infinity here would be very suspicious. If we don't
1223 // know for sure that the number is finite, bail out.
1224 if (!(Props & ConstantProperties::Finite))
1227 // Let X be a number that has properties Props.
1229 if (Cmp & Comparison::U) {
1230 // In case of unsigned comparisons, we can only compare against 0.
1232 // Any x!=0 will be considered >0 in an unsigned comparison.
1233 if (Props & ConstantProperties::Zero)
1234 Result = (Cmp & Comparison::EQ);
1235 else if (Props & ConstantProperties::NonZero)
1236 Result = (Cmp & Comparison::G) || (Cmp == Comparison::NE);
1241 // A2 is not zero. The only handled case is if X = 0.
1242 if (Props & ConstantProperties::Zero) {
1243 Result = (Cmp & Comparison::L) || (Cmp == Comparison::NE);
1249 // Signed comparisons are different.
1250 if (Props & ConstantProperties::Zero) {
1252 Result = (Cmp & Comparison::EQ);
1254 Result = (Cmp == Comparison::NE) ||
1255 ((Cmp & Comparison::L) && !A2.isNegative()) ||
1256 ((Cmp & Comparison::G) && A2.isNegative());
1259 if (Props & ConstantProperties::PosOrZero) {
1260 // X >= 0 and !(A2 < 0) => cannot compare
1261 if (!A2.isNegative())
1263 // X >= 0 and A2 < 0
1264 Result = (Cmp & Comparison::G) || (Cmp == Comparison::NE);
1267 if (Props & ConstantProperties::NegOrZero) {
1268 // X <= 0 and Src1 < 0 => cannot compare
1269 if (A2 == 0 || A2.isNegative())
1271 // X <= 0 and A2 > 0
1272 Result = (Cmp & Comparison::L) || (Cmp == Comparison::NE);
1279 bool MachineConstEvaluator::evaluateCMPpp(uint32_t Cmp, uint32_t Props1,
1280 uint32_t Props2, bool &Result) {
1281 typedef ConstantProperties P;
1282 if ((Props1 & P::NaN) && (Props2 & P::NaN))
1284 if (!(Props1 & P::Finite) || !(Props2 & P::Finite))
1287 bool Zero1 = (Props1 & P::Zero), Zero2 = (Props2 & P::Zero);
1288 bool NonZero1 = (Props1 & P::NonZero), NonZero2 = (Props2 & P::NonZero);
1289 if (Zero1 && Zero2) {
1290 Result = (Cmp & Comparison::EQ);
1293 if (Cmp == Comparison::NE) {
1294 if ((Zero1 && NonZero2) || (NonZero1 && Zero2))
1295 return (Result = true);
1299 if (Cmp & Comparison::U) {
1300 // In unsigned comparisons, we can only compare against a known zero,
1301 // or a known non-zero.
1302 if (Zero1 && NonZero2) {
1303 Result = (Cmp & Comparison::L);
1306 if (NonZero1 && Zero2) {
1307 Result = (Cmp & Comparison::G);
1313 // Signed comparison. The comparison is not NE.
1314 bool Poz1 = (Props1 & P::PosOrZero), Poz2 = (Props2 & P::PosOrZero);
1315 bool Nez1 = (Props1 & P::NegOrZero), Nez2 = (Props2 & P::NegOrZero);
1317 if (NonZero1 || NonZero2) {
1318 Result = (Cmp & Comparison::L);
1321 // Either (or both) could be zero. Can only say that X <= Y.
1322 if ((Cmp & Comparison::EQ) && (Cmp & Comparison::L))
1323 return (Result = true);
1326 if (NonZero1 || NonZero2) {
1327 Result = (Cmp & Comparison::G);
1330 // Either (or both) could be zero. Can only say that X >= Y.
1331 if ((Cmp & Comparison::EQ) && (Cmp & Comparison::G))
1332 return (Result = true);
1338 bool MachineConstEvaluator::evaluateCOPY(const Register &R1,
1339 const CellMap &Inputs, LatticeCell &Result) {
1340 return getCell(R1, Inputs, Result);
1343 bool MachineConstEvaluator::evaluateANDrr(const Register &R1,
1344 const Register &R2, const CellMap &Inputs, LatticeCell &Result) {
1345 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1346 const LatticeCell &L1 = Inputs.get(R2.Reg);
1347 const LatticeCell &L2 = Inputs.get(R2.Reg);
1348 // If both sources are bottom, exit. Otherwise try to evaluate ANDri
1349 // with the non-bottom argument passed as the immediate. This is to
1350 // catch cases of ANDing with 0.
1351 if (L2.isBottom()) {
1354 return evaluateANDrr(R2, R1, Inputs, Result);
1357 if (!evaluate(R2, L2, LS2))
1359 if (LS2.isBottom() || LS2.isProperty())
1363 for (unsigned i = 0; i < LS2.size(); ++i) {
1365 bool Eval = constToInt(LS2.Values[i], A) &&
1366 evaluateANDri(R1, A, Inputs, RC);
1371 return !Result.isBottom();
1374 bool MachineConstEvaluator::evaluateANDri(const Register &R1,
1375 const APInt &A2, const CellMap &Inputs, LatticeCell &Result) {
1376 assert(Inputs.has(R1.Reg));
1378 return getCell(R1, Inputs, Result);
1381 RC.add(intToConst(A2));
1382 // Overwrite Result.
1387 if (!getCell(R1, Inputs, LS1))
1389 if (LS1.isBottom() || LS1.isProperty())
1393 for (unsigned i = 0; i < LS1.size(); ++i) {
1394 bool Eval = constToInt(LS1.Values[i], A) &&
1395 evaluateANDii(A, A2, ResA);
1398 const Constant *C = intToConst(ResA);
1401 return !Result.isBottom();
1404 bool MachineConstEvaluator::evaluateANDii(const APInt &A1,
1405 const APInt &A2, APInt &Result) {
1410 bool MachineConstEvaluator::evaluateORrr(const Register &R1,
1411 const Register &R2, const CellMap &Inputs, LatticeCell &Result) {
1412 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1413 const LatticeCell &L1 = Inputs.get(R2.Reg);
1414 const LatticeCell &L2 = Inputs.get(R2.Reg);
1415 // If both sources are bottom, exit. Otherwise try to evaluate ORri
1416 // with the non-bottom argument passed as the immediate. This is to
1417 // catch cases of ORing with -1.
1418 if (L2.isBottom()) {
1421 return evaluateORrr(R2, R1, Inputs, Result);
1424 if (!evaluate(R2, L2, LS2))
1426 if (LS2.isBottom() || LS2.isProperty())
1430 for (unsigned i = 0; i < LS2.size(); ++i) {
1432 bool Eval = constToInt(LS2.Values[i], A) &&
1433 evaluateORri(R1, A, Inputs, RC);
1438 return !Result.isBottom();
1441 bool MachineConstEvaluator::evaluateORri(const Register &R1,
1442 const APInt &A2, const CellMap &Inputs, LatticeCell &Result) {
1443 assert(Inputs.has(R1.Reg));
1445 return getCell(R1, Inputs, Result);
1448 RC.add(intToConst(A2));
1449 // Overwrite Result.
1454 if (!getCell(R1, Inputs, LS1))
1456 if (LS1.isBottom() || LS1.isProperty())
1460 for (unsigned i = 0; i < LS1.size(); ++i) {
1461 bool Eval = constToInt(LS1.Values[i], A) &&
1462 evaluateORii(A, A2, ResA);
1465 const Constant *C = intToConst(ResA);
1468 return !Result.isBottom();
1471 bool MachineConstEvaluator::evaluateORii(const APInt &A1,
1472 const APInt &A2, APInt &Result) {
1477 bool MachineConstEvaluator::evaluateXORrr(const Register &R1,
1478 const Register &R2, const CellMap &Inputs, LatticeCell &Result) {
1479 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1480 LatticeCell LS1, LS2;
1481 if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2))
1483 if (LS1.isProperty()) {
1484 if (LS1.properties() & ConstantProperties::Zero)
1485 return !(Result = LS2).isBottom();
1488 if (LS2.isProperty()) {
1489 if (LS2.properties() & ConstantProperties::Zero)
1490 return !(Result = LS1).isBottom();
1495 for (unsigned i = 0; i < LS2.size(); ++i) {
1497 bool Eval = constToInt(LS2.Values[i], A) &&
1498 evaluateXORri(R1, A, Inputs, RC);
1503 return !Result.isBottom();
1506 bool MachineConstEvaluator::evaluateXORri(const Register &R1,
1507 const APInt &A2, const CellMap &Inputs, LatticeCell &Result) {
1508 assert(Inputs.has(R1.Reg));
1510 if (!getCell(R1, Inputs, LS1))
1512 if (LS1.isProperty()) {
1513 if (LS1.properties() & ConstantProperties::Zero) {
1514 const Constant *C = intToConst(A2);
1516 return !Result.isBottom();
1522 for (unsigned i = 0; i < LS1.size(); ++i) {
1523 bool Eval = constToInt(LS1.Values[i], A) &&
1524 evaluateXORii(A, A2, XA);
1527 const Constant *C = intToConst(XA);
1530 return !Result.isBottom();
1533 bool MachineConstEvaluator::evaluateXORii(const APInt &A1,
1534 const APInt &A2, APInt &Result) {
1539 bool MachineConstEvaluator::evaluateZEXTr(const Register &R1, unsigned Width,
1540 unsigned Bits, const CellMap &Inputs, LatticeCell &Result) {
1541 assert(Inputs.has(R1.Reg));
1543 if (!getCell(R1, Inputs, LS1))
1545 if (LS1.isProperty())
1549 for (unsigned i = 0; i < LS1.size(); ++i) {
1550 bool Eval = constToInt(LS1.Values[i], A) &&
1551 evaluateZEXTi(A, Width, Bits, XA);
1554 const Constant *C = intToConst(XA);
1560 bool MachineConstEvaluator::evaluateZEXTi(const APInt &A1, unsigned Width,
1561 unsigned Bits, APInt &Result) {
1562 unsigned BW = A1.getBitWidth();
1564 assert(Width >= Bits && BW >= Bits);
1565 APInt Mask = APInt::getLowBitsSet(Width, Bits);
1566 Result = A1.zextOrTrunc(Width) & Mask;
1570 bool MachineConstEvaluator::evaluateSEXTr(const Register &R1, unsigned Width,
1571 unsigned Bits, const CellMap &Inputs, LatticeCell &Result) {
1572 assert(Inputs.has(R1.Reg));
1574 if (!getCell(R1, Inputs, LS1))
1576 if (LS1.isBottom() || LS1.isProperty())
1580 for (unsigned i = 0; i < LS1.size(); ++i) {
1581 bool Eval = constToInt(LS1.Values[i], A) &&
1582 evaluateSEXTi(A, Width, Bits, XA);
1585 const Constant *C = intToConst(XA);
1591 bool MachineConstEvaluator::evaluateSEXTi(const APInt &A1, unsigned Width,
1592 unsigned Bits, APInt &Result) {
1593 unsigned BW = A1.getBitWidth();
1594 assert(Width >= Bits && BW >= Bits);
1595 // Special case to make things faster for smaller source widths.
1596 // Sign extension of 0 bits generates 0 as a result. This is consistent
1597 // with what the HW does.
1599 Result = APInt(Width, 0);
1602 // In C, shifts by 64 invoke undefined behavior: handle that case in APInt.
1603 if (BW <= 64 && Bits != 0) {
1604 int64_t V = A1.getSExtValue();
1607 V = static_cast<int8_t>(V);
1610 V = static_cast<int16_t>(V);
1613 V = static_cast<int32_t>(V);
1616 // Shift left to lose all bits except lower "Bits" bits, then shift
1617 // the value back, replicating what was a sign bit after the first
1619 V = (V << (64-Bits)) >> (64-Bits);
1622 // V is a 64-bit sign-extended value. Convert it to APInt of desired
1624 Result = APInt(Width, V, true);
1627 // Slow case: the value doesn't fit in int64_t.
1629 Result = A1.trunc(Bits).sext(Width);
1631 Result = A1.sext(Width);
1635 bool MachineConstEvaluator::evaluateCLBr(const Register &R1, bool Zeros,
1636 bool Ones, const CellMap &Inputs, LatticeCell &Result) {
1637 assert(Inputs.has(R1.Reg));
1639 if (!getCell(R1, Inputs, LS1))
1641 if (LS1.isBottom() || LS1.isProperty())
1645 for (unsigned i = 0; i < LS1.size(); ++i) {
1646 bool Eval = constToInt(LS1.Values[i], A) &&
1647 evaluateCLBi(A, Zeros, Ones, CA);
1650 const Constant *C = intToConst(CA);
1656 bool MachineConstEvaluator::evaluateCLBi(const APInt &A1, bool Zeros,
1657 bool Ones, APInt &Result) {
1658 unsigned BW = A1.getBitWidth();
1659 if (!Zeros && !Ones)
1662 if (Zeros && (Count == 0))
1663 Count = A1.countLeadingZeros();
1664 if (Ones && (Count == 0))
1665 Count = A1.countLeadingOnes();
1666 Result = APInt(BW, static_cast<uint64_t>(Count), false);
1670 bool MachineConstEvaluator::evaluateCTBr(const Register &R1, bool Zeros,
1671 bool Ones, const CellMap &Inputs, LatticeCell &Result) {
1672 assert(Inputs.has(R1.Reg));
1674 if (!getCell(R1, Inputs, LS1))
1676 if (LS1.isBottom() || LS1.isProperty())
1680 for (unsigned i = 0; i < LS1.size(); ++i) {
1681 bool Eval = constToInt(LS1.Values[i], A) &&
1682 evaluateCTBi(A, Zeros, Ones, CA);
1685 const Constant *C = intToConst(CA);
1691 bool MachineConstEvaluator::evaluateCTBi(const APInt &A1, bool Zeros,
1692 bool Ones, APInt &Result) {
1693 unsigned BW = A1.getBitWidth();
1694 if (!Zeros && !Ones)
1697 if (Zeros && (Count == 0))
1698 Count = A1.countTrailingZeros();
1699 if (Ones && (Count == 0))
1700 Count = A1.countTrailingOnes();
1701 Result = APInt(BW, static_cast<uint64_t>(Count), false);
1705 bool MachineConstEvaluator::evaluateEXTRACTr(const Register &R1,
1706 unsigned Width, unsigned Bits, unsigned Offset, bool Signed,
1707 const CellMap &Inputs, LatticeCell &Result) {
1708 assert(Inputs.has(R1.Reg));
1709 assert(Bits+Offset <= Width);
1711 if (!getCell(R1, Inputs, LS1))
1715 if (LS1.isProperty()) {
1716 uint32_t Ps = LS1.properties();
1717 if (Ps & ConstantProperties::Zero) {
1718 const Constant *C = intToConst(APInt(Width, 0, false));
1726 for (unsigned i = 0; i < LS1.size(); ++i) {
1727 bool Eval = constToInt(LS1.Values[i], A) &&
1728 evaluateEXTRACTi(A, Bits, Offset, Signed, CA);
1731 const Constant *C = intToConst(CA);
1737 bool MachineConstEvaluator::evaluateEXTRACTi(const APInt &A1, unsigned Bits,
1738 unsigned Offset, bool Signed, APInt &Result) {
1739 unsigned BW = A1.getBitWidth();
1740 assert(Bits+Offset <= BW);
1741 // Extracting 0 bits generates 0 as a result (as indicated by the HW people).
1743 Result = APInt(BW, 0);
1747 int64_t V = A1.getZExtValue();
1748 V <<= (64-Bits-Offset);
1752 V = static_cast<uint64_t>(V) >> (64-Bits);
1753 Result = APInt(BW, V, Signed);
1757 Result = A1.shl(BW-Bits-Offset).ashr(BW-Bits);
1759 Result = A1.shl(BW-Bits-Offset).lshr(BW-Bits);
1763 bool MachineConstEvaluator::evaluateSplatr(const Register &R1,
1764 unsigned Bits, unsigned Count, const CellMap &Inputs,
1765 LatticeCell &Result) {
1766 assert(Inputs.has(R1.Reg));
1768 if (!getCell(R1, Inputs, LS1))
1770 if (LS1.isBottom() || LS1.isProperty())
1774 for (unsigned i = 0; i < LS1.size(); ++i) {
1775 bool Eval = constToInt(LS1.Values[i], A) &&
1776 evaluateSplati(A, Bits, Count, SA);
1779 const Constant *C = intToConst(SA);
1785 bool MachineConstEvaluator::evaluateSplati(const APInt &A1, unsigned Bits,
1786 unsigned Count, APInt &Result) {
1788 unsigned BW = A1.getBitWidth(), SW = Count*Bits;
1789 APInt LoBits = (Bits < BW) ? A1.trunc(Bits) : A1.zextOrSelf(Bits);
1791 LoBits = LoBits.zext(SW);
1793 APInt Res(SW, 0, false);
1794 for (unsigned i = 0; i < Count; ++i) {
1802 // ----------------------------------------------------------------------
1803 // Hexagon-specific code.
1807 FunctionPass *createHexagonConstPropagationPass();
1808 void initializeHexagonConstPropagationPass(PassRegistry &Registry);
1810 } // end namespace llvm
1814 class HexagonConstEvaluator : public MachineConstEvaluator {
1816 HexagonConstEvaluator(MachineFunction &Fn);
1818 bool evaluate(const MachineInstr &MI, const CellMap &Inputs,
1819 CellMap &Outputs) override;
1820 bool evaluate(const Register &R, const LatticeCell &SrcC,
1821 LatticeCell &Result) override;
1822 bool evaluate(const MachineInstr &BrI, const CellMap &Inputs,
1823 SetVector<const MachineBasicBlock*> &Targets, bool &FallsThru)
1825 bool rewrite(MachineInstr &MI, const CellMap &Inputs) override;
1828 unsigned getRegBitWidth(unsigned Reg) const;
1830 static uint32_t getCmp(unsigned Opc);
1831 static APInt getCmpImm(unsigned Opc, unsigned OpX,
1832 const MachineOperand &MO);
1833 void replaceWithNop(MachineInstr &MI);
1835 bool evaluateHexRSEQ32(Register RL, Register RH, const CellMap &Inputs,
1836 LatticeCell &Result);
1837 bool evaluateHexCompare(const MachineInstr &MI, const CellMap &Inputs,
1839 // This is suitable to be called for compare-and-jump instructions.
1840 bool evaluateHexCompare2(uint32_t Cmp, const MachineOperand &Src1,
1841 const MachineOperand &Src2, const CellMap &Inputs, bool &Result);
1842 bool evaluateHexLogical(const MachineInstr &MI, const CellMap &Inputs,
1844 bool evaluateHexCondMove(const MachineInstr &MI, const CellMap &Inputs,
1846 bool evaluateHexExt(const MachineInstr &MI, const CellMap &Inputs,
1848 bool evaluateHexVector1(const MachineInstr &MI, const CellMap &Inputs,
1850 bool evaluateHexVector2(const MachineInstr &MI, const CellMap &Inputs,
1853 void replaceAllRegUsesWith(unsigned FromReg, unsigned ToReg);
1854 bool rewriteHexBranch(MachineInstr &BrI, const CellMap &Inputs);
1855 bool rewriteHexConstDefs(MachineInstr &MI, const CellMap &Inputs,
1857 bool rewriteHexConstUses(MachineInstr &MI, const CellMap &Inputs);
1859 MachineRegisterInfo *MRI;
1860 const HexagonInstrInfo &HII;
1861 const HexagonRegisterInfo &HRI;
1864 class HexagonConstPropagation : public MachineFunctionPass {
1868 HexagonConstPropagation() : MachineFunctionPass(ID) {
1869 PassRegistry &Registry = *PassRegistry::getPassRegistry();
1870 initializeHexagonConstPropagationPass(Registry);
1873 StringRef getPassName() const override {
1874 return "Hexagon Constant Propagation";
1877 bool runOnMachineFunction(MachineFunction &MF) override {
1878 const Function *F = MF.getFunction();
1881 if (skipFunction(*F))
1884 HexagonConstEvaluator HCE(MF);
1885 return MachineConstPropagator(HCE).run(MF);
1889 char HexagonConstPropagation::ID = 0;
1891 } // end anonymous namespace
1893 INITIALIZE_PASS(HexagonConstPropagation, "hcp", "Hexagon Constant Propagation",
1896 HexagonConstEvaluator::HexagonConstEvaluator(MachineFunction &Fn)
1897 : MachineConstEvaluator(Fn),
1898 HII(*Fn.getSubtarget<HexagonSubtarget>().getInstrInfo()),
1899 HRI(*Fn.getSubtarget<HexagonSubtarget>().getRegisterInfo()) {
1900 MRI = &Fn.getRegInfo();
1903 bool HexagonConstEvaluator::evaluate(const MachineInstr &MI,
1904 const CellMap &Inputs, CellMap &Outputs) {
1907 if (MI.getNumOperands() == 0 || !MI.getOperand(0).isReg())
1909 const MachineOperand &MD = MI.getOperand(0);
1913 unsigned Opc = MI.getOpcode();
1915 assert(!DefR.SubReg);
1916 if (!TargetRegisterInfo::isVirtualRegister(DefR.Reg))
1921 Register SrcR(MI.getOperand(1));
1922 bool Eval = evaluateCOPY(SrcR, Inputs, RC);
1925 Outputs.update(DefR.Reg, RC);
1928 if (MI.isRegSequence()) {
1929 unsigned Sub1 = MI.getOperand(2).getImm();
1930 unsigned Sub2 = MI.getOperand(4).getImm();
1931 const TargetRegisterClass *DefRC = MRI->getRegClass(DefR.Reg);
1932 unsigned SubLo = HRI.getHexagonSubRegIndex(DefRC, Hexagon::ps_sub_lo);
1933 unsigned SubHi = HRI.getHexagonSubRegIndex(DefRC, Hexagon::ps_sub_hi);
1934 if (Sub1 != SubLo && Sub1 != SubHi)
1936 if (Sub2 != SubLo && Sub2 != SubHi)
1938 assert(Sub1 != Sub2);
1939 bool LoIs1 = (Sub1 == SubLo);
1940 const MachineOperand &OpLo = LoIs1 ? MI.getOperand(1) : MI.getOperand(3);
1941 const MachineOperand &OpHi = LoIs1 ? MI.getOperand(3) : MI.getOperand(1);
1943 Register SrcRL(OpLo), SrcRH(OpHi);
1944 bool Eval = evaluateHexRSEQ32(SrcRL, SrcRH, Inputs, RC);
1947 Outputs.update(DefR.Reg, RC);
1950 if (MI.isCompare()) {
1951 bool Eval = evaluateHexCompare(MI, Inputs, Outputs);
1958 case Hexagon::A2_tfrsi:
1959 case Hexagon::A2_tfrpi:
1960 case Hexagon::CONST32:
1961 case Hexagon::CONST64:
1963 const MachineOperand &VO = MI.getOperand(1);
1964 // The operand of CONST32 can be a blockaddress, e.g.
1965 // %vreg0<def> = CONST32 <blockaddress(@eat, %L)>
1966 // Do this check for all instructions for safety.
1969 int64_t V = MI.getOperand(1).getImm();
1970 unsigned W = getRegBitWidth(DefR.Reg);
1971 if (W != 32 && W != 64)
1973 IntegerType *Ty = (W == 32) ? Type::getInt32Ty(CX)
1974 : Type::getInt64Ty(CX);
1975 const ConstantInt *CI = ConstantInt::get(Ty, V, true);
1976 LatticeCell RC = Outputs.get(DefR.Reg);
1978 Outputs.update(DefR.Reg, RC);
1982 case Hexagon::PS_true:
1983 case Hexagon::PS_false:
1985 LatticeCell RC = Outputs.get(DefR.Reg);
1986 bool NonZero = (Opc == Hexagon::PS_true);
1987 uint32_t P = NonZero ? ConstantProperties::NonZero
1988 : ConstantProperties::Zero;
1990 Outputs.update(DefR.Reg, RC);
1994 case Hexagon::A2_and:
1995 case Hexagon::A2_andir:
1996 case Hexagon::A2_andp:
1997 case Hexagon::A2_or:
1998 case Hexagon::A2_orir:
1999 case Hexagon::A2_orp:
2000 case Hexagon::A2_xor:
2001 case Hexagon::A2_xorp:
2003 bool Eval = evaluateHexLogical(MI, Inputs, Outputs);
2009 case Hexagon::A2_combineii: // combine(#s8Ext, #s8)
2010 case Hexagon::A4_combineii: // combine(#s8, #u6Ext)
2012 uint64_t Hi = MI.getOperand(1).getImm();
2013 uint64_t Lo = MI.getOperand(2).getImm();
2014 uint64_t Res = (Hi << 32) | (Lo & 0xFFFFFFFF);
2015 IntegerType *Ty = Type::getInt64Ty(CX);
2016 const ConstantInt *CI = ConstantInt::get(Ty, Res, false);
2017 LatticeCell RC = Outputs.get(DefR.Reg);
2019 Outputs.update(DefR.Reg, RC);
2023 case Hexagon::S2_setbit_i:
2025 int64_t B = MI.getOperand(2).getImm();
2026 assert(B >=0 && B < 32);
2027 APInt A(32, (1ull << B), false);
2028 Register R(MI.getOperand(1));
2029 LatticeCell RC = Outputs.get(DefR.Reg);
2030 bool Eval = evaluateORri(R, A, Inputs, RC);
2033 Outputs.update(DefR.Reg, RC);
2037 case Hexagon::C2_mux:
2038 case Hexagon::C2_muxir:
2039 case Hexagon::C2_muxri:
2040 case Hexagon::C2_muxii:
2042 bool Eval = evaluateHexCondMove(MI, Inputs, Outputs);
2048 case Hexagon::A2_sxtb:
2049 case Hexagon::A2_sxth:
2050 case Hexagon::A2_sxtw:
2051 case Hexagon::A2_zxtb:
2052 case Hexagon::A2_zxth:
2054 bool Eval = evaluateHexExt(MI, Inputs, Outputs);
2060 case Hexagon::S2_ct0:
2061 case Hexagon::S2_ct0p:
2062 case Hexagon::S2_ct1:
2063 case Hexagon::S2_ct1p:
2065 using namespace Hexagon;
2067 bool Ones = (Opc == S2_ct1) || (Opc == S2_ct1p);
2068 Register R1(MI.getOperand(1));
2069 assert(Inputs.has(R1.Reg));
2071 bool Eval = evaluateCTBr(R1, !Ones, Ones, Inputs, T);
2074 // All of these instructions return a 32-bit value. The evaluate
2075 // will generate the same type as the operand, so truncate the
2076 // result if necessary.
2078 LatticeCell RC = Outputs.get(DefR.Reg);
2079 for (unsigned i = 0; i < T.size(); ++i) {
2080 const Constant *CI = T.Values[i];
2081 if (constToInt(CI, C) && C.getBitWidth() > 32)
2082 CI = intToConst(C.trunc(32));
2085 Outputs.update(DefR.Reg, RC);
2089 case Hexagon::S2_cl0:
2090 case Hexagon::S2_cl0p:
2091 case Hexagon::S2_cl1:
2092 case Hexagon::S2_cl1p:
2093 case Hexagon::S2_clb:
2094 case Hexagon::S2_clbp:
2096 using namespace Hexagon;
2098 bool OnlyZeros = (Opc == S2_cl0) || (Opc == S2_cl0p);
2099 bool OnlyOnes = (Opc == S2_cl1) || (Opc == S2_cl1p);
2100 Register R1(MI.getOperand(1));
2101 assert(Inputs.has(R1.Reg));
2103 bool Eval = evaluateCLBr(R1, !OnlyOnes, !OnlyZeros, Inputs, T);
2106 // All of these instructions return a 32-bit value. The evaluate
2107 // will generate the same type as the operand, so truncate the
2108 // result if necessary.
2110 LatticeCell RC = Outputs.get(DefR.Reg);
2111 for (unsigned i = 0; i < T.size(); ++i) {
2112 const Constant *CI = T.Values[i];
2113 if (constToInt(CI, C) && C.getBitWidth() > 32)
2114 CI = intToConst(C.trunc(32));
2117 Outputs.update(DefR.Reg, RC);
2121 case Hexagon::S4_extract:
2122 case Hexagon::S4_extractp:
2123 case Hexagon::S2_extractu:
2124 case Hexagon::S2_extractup:
2126 bool Signed = (Opc == Hexagon::S4_extract) ||
2127 (Opc == Hexagon::S4_extractp);
2128 Register R1(MI.getOperand(1));
2129 unsigned BW = getRegBitWidth(R1.Reg);
2130 unsigned Bits = MI.getOperand(2).getImm();
2131 unsigned Offset = MI.getOperand(3).getImm();
2132 LatticeCell RC = Outputs.get(DefR.Reg);
2134 APInt Zero(BW, 0, false);
2135 RC.add(intToConst(Zero));
2138 if (Offset+Bits > BW) {
2139 // If the requested bitfield extends beyond the most significant bit,
2140 // the extra bits are treated as 0s. To emulate this behavior, reduce
2141 // the number of requested bits, and make the extract unsigned.
2145 bool Eval = evaluateEXTRACTr(R1, BW, Bits, Offset, Signed, Inputs, RC);
2148 Outputs.update(DefR.Reg, RC);
2152 case Hexagon::S2_vsplatrb:
2153 case Hexagon::S2_vsplatrh:
2157 // vrndwh, vrndwh:sat
2158 // vsathb, vsathub, vsatwuh
2160 // vtrunehb, vtrunohb
2163 bool Eval = evaluateHexVector1(MI, Inputs, Outputs);
2179 bool HexagonConstEvaluator::evaluate(const Register &R,
2180 const LatticeCell &Input, LatticeCell &Result) {
2185 const TargetRegisterClass *RC = MRI->getRegClass(R.Reg);
2186 if (RC != &Hexagon::DoubleRegsRegClass)
2188 if (R.SubReg != Hexagon::isub_lo && R.SubReg != Hexagon::isub_hi)
2191 assert(!Input.isTop());
2192 if (Input.isBottom())
2195 typedef ConstantProperties P;
2196 if (Input.isProperty()) {
2197 uint32_t Ps = Input.properties();
2198 if (Ps & (P::Zero|P::NaN)) {
2199 uint32_t Ns = (Ps & (P::Zero|P::NaN|P::SignProperties));
2203 if (R.SubReg == Hexagon::isub_hi) {
2204 uint32_t Ns = (Ps & P::SignProperties);
2211 // The Input cell contains some known values. Pick the word corresponding
2212 // to the subregister.
2214 for (unsigned i = 0; i < Input.size(); ++i) {
2215 const Constant *C = Input.Values[i];
2216 if (!constToInt(C, A))
2220 uint64_t U = A.getZExtValue();
2221 if (R.SubReg == Hexagon::isub_hi)
2224 uint32_t U32 = Lo_32(U);
2226 memcpy(&V32, &U32, sizeof V32);
2227 IntegerType *Ty = Type::getInt32Ty(CX);
2228 const ConstantInt *C32 = ConstantInt::get(Ty, static_cast<int64_t>(V32));
2234 bool HexagonConstEvaluator::evaluate(const MachineInstr &BrI,
2235 const CellMap &Inputs, SetVector<const MachineBasicBlock*> &Targets,
2237 // We need to evaluate one branch at a time. TII::analyzeBranch checks
2238 // all the branches in a basic block at once, so we cannot use it.
2239 unsigned Opc = BrI.getOpcode();
2240 bool SimpleBranch = false;
2241 bool Negated = false;
2243 case Hexagon::J2_jumpf:
2244 case Hexagon::J2_jumpfnew:
2245 case Hexagon::J2_jumpfnewpt:
2247 case Hexagon::J2_jumpt:
2248 case Hexagon::J2_jumptnew:
2249 case Hexagon::J2_jumptnewpt:
2250 // Simple branch: if([!]Pn) jump ...
2251 // i.e. Op0 = predicate, Op1 = branch target.
2252 SimpleBranch = true;
2254 case Hexagon::J2_jump:
2255 Targets.insert(BrI.getOperand(0).getMBB());
2260 // If the branch is of unknown type, assume that all successors are
2262 FallsThru = !BrI.isUnconditionalBranch();
2267 const MachineOperand &MD = BrI.getOperand(0);
2269 // If the condition operand has a subregister, this is not something
2270 // we currently recognize.
2273 assert(Inputs.has(PR.Reg));
2274 const LatticeCell &PredC = Inputs.get(PR.Reg);
2275 if (PredC.isBottom())
2278 uint32_t Props = PredC.properties();
2279 bool CTrue = false, CFalse = false;;
2280 if (Props & ConstantProperties::Zero)
2282 else if (Props & ConstantProperties::NonZero)
2284 // If the condition is not known to be either, bail out.
2285 if (!CTrue && !CFalse)
2288 const MachineBasicBlock *BranchTarget = BrI.getOperand(1).getMBB();
2291 if ((!Negated && CTrue) || (Negated && CFalse))
2292 Targets.insert(BranchTarget);
2293 else if ((!Negated && CFalse) || (Negated && CTrue))
2302 bool HexagonConstEvaluator::rewrite(MachineInstr &MI, const CellMap &Inputs) {
2304 return rewriteHexBranch(MI, Inputs);
2306 unsigned Opc = MI.getOpcode();
2310 case Hexagon::A2_tfrsi:
2311 case Hexagon::A2_tfrpi:
2312 case Hexagon::CONST32:
2313 case Hexagon::CONST64:
2314 case Hexagon::PS_true:
2315 case Hexagon::PS_false:
2319 unsigned NumOp = MI.getNumOperands();
2323 bool AllDefs, Changed;
2324 Changed = rewriteHexConstDefs(MI, Inputs, AllDefs);
2325 // If not all defs have been rewritten (i.e. the instruction defines
2326 // a register that is not compile-time constant), then try to rewrite
2327 // register operands that are known to be constant with immediates.
2329 Changed |= rewriteHexConstUses(MI, Inputs);
2334 unsigned HexagonConstEvaluator::getRegBitWidth(unsigned Reg) const {
2335 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
2336 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC))
2338 if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC))
2340 if (Hexagon::PredRegsRegClass.hasSubClassEq(RC))
2342 llvm_unreachable("Invalid register");
2346 uint32_t HexagonConstEvaluator::getCmp(unsigned Opc) {
2348 case Hexagon::C2_cmpeq:
2349 case Hexagon::C2_cmpeqp:
2350 case Hexagon::A4_cmpbeq:
2351 case Hexagon::A4_cmpheq:
2352 case Hexagon::A4_cmpbeqi:
2353 case Hexagon::A4_cmpheqi:
2354 case Hexagon::C2_cmpeqi:
2355 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
2356 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
2357 case Hexagon::J4_cmpeqi_t_jumpnv_nt:
2358 case Hexagon::J4_cmpeqi_t_jumpnv_t:
2359 case Hexagon::J4_cmpeq_t_jumpnv_nt:
2360 case Hexagon::J4_cmpeq_t_jumpnv_t:
2361 return Comparison::EQ;
2363 case Hexagon::C4_cmpneq:
2364 case Hexagon::C4_cmpneqi:
2365 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
2366 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
2367 case Hexagon::J4_cmpeqi_f_jumpnv_nt:
2368 case Hexagon::J4_cmpeqi_f_jumpnv_t:
2369 case Hexagon::J4_cmpeq_f_jumpnv_nt:
2370 case Hexagon::J4_cmpeq_f_jumpnv_t:
2371 return Comparison::NE;
2373 case Hexagon::C2_cmpgt:
2374 case Hexagon::C2_cmpgtp:
2375 case Hexagon::A4_cmpbgt:
2376 case Hexagon::A4_cmphgt:
2377 case Hexagon::A4_cmpbgti:
2378 case Hexagon::A4_cmphgti:
2379 case Hexagon::C2_cmpgti:
2380 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
2381 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
2382 case Hexagon::J4_cmpgti_t_jumpnv_nt:
2383 case Hexagon::J4_cmpgti_t_jumpnv_t:
2384 case Hexagon::J4_cmpgt_t_jumpnv_nt:
2385 case Hexagon::J4_cmpgt_t_jumpnv_t:
2386 return Comparison::GTs;
2388 case Hexagon::C4_cmplte:
2389 case Hexagon::C4_cmpltei:
2390 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
2391 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
2392 case Hexagon::J4_cmpgti_f_jumpnv_nt:
2393 case Hexagon::J4_cmpgti_f_jumpnv_t:
2394 case Hexagon::J4_cmpgt_f_jumpnv_nt:
2395 case Hexagon::J4_cmpgt_f_jumpnv_t:
2396 return Comparison::LEs;
2398 case Hexagon::C2_cmpgtu:
2399 case Hexagon::C2_cmpgtup:
2400 case Hexagon::A4_cmpbgtu:
2401 case Hexagon::A4_cmpbgtui:
2402 case Hexagon::A4_cmphgtu:
2403 case Hexagon::A4_cmphgtui:
2404 case Hexagon::C2_cmpgtui:
2405 case Hexagon::J4_cmpgtui_t_jumpnv_nt:
2406 case Hexagon::J4_cmpgtui_t_jumpnv_t:
2407 case Hexagon::J4_cmpgtu_t_jumpnv_nt:
2408 case Hexagon::J4_cmpgtu_t_jumpnv_t:
2409 return Comparison::GTu;
2411 case Hexagon::J4_cmpltu_f_jumpnv_nt:
2412 case Hexagon::J4_cmpltu_f_jumpnv_t:
2413 return Comparison::GEu;
2415 case Hexagon::J4_cmpltu_t_jumpnv_nt:
2416 case Hexagon::J4_cmpltu_t_jumpnv_t:
2417 return Comparison::LTu;
2419 case Hexagon::J4_cmplt_f_jumpnv_nt:
2420 case Hexagon::J4_cmplt_f_jumpnv_t:
2421 return Comparison::GEs;
2423 case Hexagon::C4_cmplteu:
2424 case Hexagon::C4_cmplteui:
2425 case Hexagon::J4_cmpgtui_f_jumpnv_nt:
2426 case Hexagon::J4_cmpgtui_f_jumpnv_t:
2427 case Hexagon::J4_cmpgtu_f_jumpnv_nt:
2428 case Hexagon::J4_cmpgtu_f_jumpnv_t:
2429 return Comparison::LEu;
2431 case Hexagon::J4_cmplt_t_jumpnv_nt:
2432 case Hexagon::J4_cmplt_t_jumpnv_t:
2433 return Comparison::LTs;
2438 return Comparison::Unk;
2441 APInt HexagonConstEvaluator::getCmpImm(unsigned Opc, unsigned OpX,
2442 const MachineOperand &MO) {
2443 bool Signed = false;
2445 case Hexagon::A4_cmpbgtui: // u7
2446 case Hexagon::A4_cmphgtui: // u7
2448 case Hexagon::A4_cmpheqi: // s8
2449 case Hexagon::C4_cmpneqi: // s8
2451 case Hexagon::A4_cmpbeqi: // u8
2453 case Hexagon::C2_cmpgtui: // u9
2454 case Hexagon::C4_cmplteui: // u9
2456 case Hexagon::C2_cmpeqi: // s10
2457 case Hexagon::C2_cmpgti: // s10
2458 case Hexagon::C4_cmpltei: // s10
2461 case Hexagon::J4_cmpeqi_f_jumpnv_nt: // u5
2462 case Hexagon::J4_cmpeqi_f_jumpnv_t: // u5
2463 case Hexagon::J4_cmpeqi_t_jumpnv_nt: // u5
2464 case Hexagon::J4_cmpeqi_t_jumpnv_t: // u5
2465 case Hexagon::J4_cmpgti_f_jumpnv_nt: // u5
2466 case Hexagon::J4_cmpgti_f_jumpnv_t: // u5
2467 case Hexagon::J4_cmpgti_t_jumpnv_nt: // u5
2468 case Hexagon::J4_cmpgti_t_jumpnv_t: // u5
2469 case Hexagon::J4_cmpgtui_f_jumpnv_nt: // u5
2470 case Hexagon::J4_cmpgtui_f_jumpnv_t: // u5
2471 case Hexagon::J4_cmpgtui_t_jumpnv_nt: // u5
2472 case Hexagon::J4_cmpgtui_t_jumpnv_t: // u5
2475 llvm_unreachable("Unhandled instruction");
2479 uint64_t Val = MO.getImm();
2480 return APInt(32, Val, Signed);
2483 void HexagonConstEvaluator::replaceWithNop(MachineInstr &MI) {
2484 MI.setDesc(HII.get(Hexagon::A2_nop));
2485 while (MI.getNumOperands() > 0)
2486 MI.RemoveOperand(0);
2489 bool HexagonConstEvaluator::evaluateHexRSEQ32(Register RL, Register RH,
2490 const CellMap &Inputs, LatticeCell &Result) {
2491 assert(Inputs.has(RL.Reg) && Inputs.has(RH.Reg));
2492 LatticeCell LSL, LSH;
2493 if (!getCell(RL, Inputs, LSL) || !getCell(RH, Inputs, LSH))
2495 if (LSL.isProperty() || LSH.isProperty())
2498 unsigned LN = LSL.size(), HN = LSH.size();
2499 SmallVector<APInt,4> LoVs(LN), HiVs(HN);
2500 for (unsigned i = 0; i < LN; ++i) {
2501 bool Eval = constToInt(LSL.Values[i], LoVs[i]);
2504 assert(LoVs[i].getBitWidth() == 32);
2506 for (unsigned i = 0; i < HN; ++i) {
2507 bool Eval = constToInt(LSH.Values[i], HiVs[i]);
2510 assert(HiVs[i].getBitWidth() == 32);
2513 for (unsigned i = 0; i < HiVs.size(); ++i) {
2514 APInt HV = HiVs[i].zextOrSelf(64) << 32;
2515 for (unsigned j = 0; j < LoVs.size(); ++j) {
2516 APInt LV = LoVs[j].zextOrSelf(64);
2517 const Constant *C = intToConst(HV | LV);
2519 if (Result.isBottom())
2523 return !Result.isBottom();
2526 bool HexagonConstEvaluator::evaluateHexCompare(const MachineInstr &MI,
2527 const CellMap &Inputs, CellMap &Outputs) {
2528 unsigned Opc = MI.getOpcode();
2529 bool Classic = false;
2531 case Hexagon::C2_cmpeq:
2532 case Hexagon::C2_cmpeqp:
2533 case Hexagon::C2_cmpgt:
2534 case Hexagon::C2_cmpgtp:
2535 case Hexagon::C2_cmpgtu:
2536 case Hexagon::C2_cmpgtup:
2537 case Hexagon::C2_cmpeqi:
2538 case Hexagon::C2_cmpgti:
2539 case Hexagon::C2_cmpgtui:
2540 // Classic compare: Dst0 = CMP Src1, Src2
2544 // Not handling other compare instructions now.
2549 const MachineOperand &Src1 = MI.getOperand(1);
2550 const MachineOperand &Src2 = MI.getOperand(2);
2553 unsigned Opc = MI.getOpcode();
2554 bool Computed = evaluateHexCompare2(Opc, Src1, Src2, Inputs, Result);
2556 // Only create a zero/non-zero cell. At this time there isn't really
2557 // much need for specific values.
2558 Register DefR(MI.getOperand(0));
2559 LatticeCell L = Outputs.get(DefR.Reg);
2560 uint32_t P = Result ? ConstantProperties::NonZero
2561 : ConstantProperties::Zero;
2563 Outputs.update(DefR.Reg, L);
2571 bool HexagonConstEvaluator::evaluateHexCompare2(unsigned Opc,
2572 const MachineOperand &Src1, const MachineOperand &Src2,
2573 const CellMap &Inputs, bool &Result) {
2574 uint32_t Cmp = getCmp(Opc);
2575 bool Reg1 = Src1.isReg(), Reg2 = Src2.isReg();
2576 bool Imm1 = Src1.isImm(), Imm2 = Src2.isImm();
2581 return evaluateCMPrr(Cmp, R1, R2, Inputs, Result);
2583 APInt A2 = getCmpImm(Opc, 2, Src2);
2584 return evaluateCMPri(Cmp, R1, A2, Inputs, Result);
2587 APInt A1 = getCmpImm(Opc, 1, Src1);
2590 uint32_t NegCmp = Comparison::negate(Cmp);
2591 return evaluateCMPri(NegCmp, R2, A1, Inputs, Result);
2593 APInt A2 = getCmpImm(Opc, 2, Src2);
2594 return evaluateCMPii(Cmp, A1, A2, Result);
2597 // Unknown kind of comparison.
2601 bool HexagonConstEvaluator::evaluateHexLogical(const MachineInstr &MI,
2602 const CellMap &Inputs, CellMap &Outputs) {
2603 unsigned Opc = MI.getOpcode();
2604 if (MI.getNumOperands() != 3)
2606 const MachineOperand &Src1 = MI.getOperand(1);
2607 const MachineOperand &Src2 = MI.getOperand(2);
2614 case Hexagon::A2_and:
2615 case Hexagon::A2_andp:
2616 Eval = evaluateANDrr(R1, Register(Src2), Inputs, RC);
2618 case Hexagon::A2_andir: {
2619 APInt A(32, Src2.getImm(), true);
2620 Eval = evaluateANDri(R1, A, Inputs, RC);
2623 case Hexagon::A2_or:
2624 case Hexagon::A2_orp:
2625 Eval = evaluateORrr(R1, Register(Src2), Inputs, RC);
2627 case Hexagon::A2_orir: {
2628 APInt A(32, Src2.getImm(), true);
2629 Eval = evaluateORri(R1, A, Inputs, RC);
2632 case Hexagon::A2_xor:
2633 case Hexagon::A2_xorp:
2634 Eval = evaluateXORrr(R1, Register(Src2), Inputs, RC);
2638 Register DefR(MI.getOperand(0));
2639 Outputs.update(DefR.Reg, RC);
2644 bool HexagonConstEvaluator::evaluateHexCondMove(const MachineInstr &MI,
2645 const CellMap &Inputs, CellMap &Outputs) {
2646 // Dst0 = Cond1 ? Src2 : Src3
2647 Register CR(MI.getOperand(1));
2648 assert(Inputs.has(CR.Reg));
2650 if (!getCell(CR, Inputs, LS))
2652 uint32_t Ps = LS.properties();
2654 if (Ps & ConstantProperties::Zero)
2656 else if (Ps & ConstantProperties::NonZero)
2661 const MachineOperand &ValOp = MI.getOperand(TakeOp);
2662 Register DefR(MI.getOperand(0));
2663 LatticeCell RC = Outputs.get(DefR.Reg);
2665 if (ValOp.isImm()) {
2666 int64_t V = ValOp.getImm();
2667 unsigned W = getRegBitWidth(DefR.Reg);
2668 APInt A(W, V, true);
2669 const Constant *C = intToConst(A);
2671 Outputs.update(DefR.Reg, RC);
2674 if (ValOp.isReg()) {
2676 const LatticeCell &LR = Inputs.get(R.Reg);
2678 if (!evaluate(R, LR, LSR))
2681 Outputs.update(DefR.Reg, RC);
2687 bool HexagonConstEvaluator::evaluateHexExt(const MachineInstr &MI,
2688 const CellMap &Inputs, CellMap &Outputs) {
2690 Register R1(MI.getOperand(1));
2691 assert(Inputs.has(R1.Reg));
2693 unsigned Opc = MI.getOpcode();
2696 case Hexagon::A2_sxtb:
2697 case Hexagon::A2_zxtb:
2700 case Hexagon::A2_sxth:
2701 case Hexagon::A2_zxth:
2704 case Hexagon::A2_sxtw:
2709 bool Signed = false;
2711 case Hexagon::A2_sxtb:
2712 case Hexagon::A2_sxth:
2713 case Hexagon::A2_sxtw:
2718 Register DefR(MI.getOperand(0));
2719 unsigned BW = getRegBitWidth(DefR.Reg);
2720 LatticeCell RC = Outputs.get(DefR.Reg);
2721 bool Eval = Signed ? evaluateSEXTr(R1, BW, Bits, Inputs, RC)
2722 : evaluateZEXTr(R1, BW, Bits, Inputs, RC);
2725 Outputs.update(DefR.Reg, RC);
2729 bool HexagonConstEvaluator::evaluateHexVector1(const MachineInstr &MI,
2730 const CellMap &Inputs, CellMap &Outputs) {
2732 Register DefR(MI.getOperand(0));
2733 Register R1(MI.getOperand(1));
2734 assert(Inputs.has(R1.Reg));
2735 LatticeCell RC = Outputs.get(DefR.Reg);
2738 unsigned Opc = MI.getOpcode();
2740 case Hexagon::S2_vsplatrb:
2741 // Rd = 4 times Rs:0..7
2742 Eval = evaluateSplatr(R1, 8, 4, Inputs, RC);
2744 case Hexagon::S2_vsplatrh:
2745 // Rdd = 4 times Rs:0..15
2746 Eval = evaluateSplatr(R1, 16, 4, Inputs, RC);
2754 Outputs.update(DefR.Reg, RC);
2758 bool HexagonConstEvaluator::rewriteHexConstDefs(MachineInstr &MI,
2759 const CellMap &Inputs, bool &AllDefs) {
2762 // Some diagnostics.
2763 // DEBUG({...}) gets confused with all this code as an argument.
2765 bool Debugging = DebugFlag && isCurrentDebugType(DEBUG_TYPE);
2767 bool Const = true, HasUse = false;
2768 for (const MachineOperand &MO : MI.operands()) {
2769 if (!MO.isReg() || !MO.isUse() || MO.isImplicit())
2772 if (!TargetRegisterInfo::isVirtualRegister(R.Reg))
2775 // PHIs can legitimately have "top" cells after propagation.
2776 if (!MI.isPHI() && !Inputs.has(R.Reg)) {
2777 dbgs() << "Top " << PrintReg(R.Reg, &HRI, R.SubReg)
2778 << " in MI: " << MI;
2781 const LatticeCell &L = Inputs.get(R.Reg);
2782 Const &= L.isSingle();
2786 if (HasUse && Const) {
2788 dbgs() << "CONST: " << MI;
2789 for (const MachineOperand &MO : MI.operands()) {
2790 if (!MO.isReg() || !MO.isUse() || MO.isImplicit())
2792 unsigned R = MO.getReg();
2793 dbgs() << PrintReg(R, &TRI) << ": " << Inputs.get(R) << "\n";
2800 // Avoid generating TFRIs for register transfers---this will keep the
2801 // coalescing opportunities.
2805 // Collect all virtual register-def operands.
2806 SmallVector<unsigned,2> DefRegs;
2807 for (const MachineOperand &MO : MI.operands()) {
2808 if (!MO.isReg() || !MO.isDef())
2810 unsigned R = MO.getReg();
2811 if (!TargetRegisterInfo::isVirtualRegister(R))
2813 assert(!MO.getSubReg());
2814 assert(Inputs.has(R));
2815 DefRegs.push_back(R);
2818 MachineBasicBlock &B = *MI.getParent();
2819 const DebugLoc &DL = MI.getDebugLoc();
2820 unsigned ChangedNum = 0;
2822 SmallVector<const MachineInstr*,4> NewInstrs;
2825 // For each defined register, if it is a constant, create an instruction
2827 // and replace all uses of the defined register with NewR.
2828 for (unsigned i = 0, n = DefRegs.size(); i < n; ++i) {
2829 unsigned R = DefRegs[i];
2830 const LatticeCell &L = Inputs.get(R);
2833 const TargetRegisterClass *RC = MRI->getRegClass(R);
2834 MachineBasicBlock::iterator At = MI.getIterator();
2836 if (!L.isSingle()) {
2837 // If this a zero/non-zero cell, we can fold a definition
2838 // of a predicate register.
2839 typedef ConstantProperties P;
2840 uint64_t Ps = L.properties();
2841 if (!(Ps & (P::Zero|P::NonZero)))
2843 const TargetRegisterClass *PredRC = &Hexagon::PredRegsRegClass;
2846 const MCInstrDesc *NewD = (Ps & P::Zero) ?
2847 &HII.get(Hexagon::PS_false) :
2848 &HII.get(Hexagon::PS_true);
2849 unsigned NewR = MRI->createVirtualRegister(PredRC);
2850 const MachineInstrBuilder &MIB = BuildMI(B, At, DL, *NewD, NewR);
2853 NewInstrs.push_back(&*MIB);
2855 replaceAllRegUsesWith(R, NewR);
2857 // This cell has a single value.
2859 if (!constToInt(L.Value, A) || !A.isSignedIntN(64))
2861 const TargetRegisterClass *NewRC;
2862 const MCInstrDesc *NewD;
2864 unsigned W = getRegBitWidth(R);
2865 int64_t V = A.getSExtValue();
2866 assert(W == 32 || W == 64);
2868 NewRC = &Hexagon::IntRegsRegClass;
2870 NewRC = &Hexagon::DoubleRegsRegClass;
2871 unsigned NewR = MRI->createVirtualRegister(NewRC);
2872 const MachineInstr *NewMI;
2875 NewD = &HII.get(Hexagon::A2_tfrsi);
2876 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2879 if (A.isSignedIntN(8)) {
2880 NewD = &HII.get(Hexagon::A2_tfrpi);
2881 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2884 int32_t Hi = V >> 32;
2885 int32_t Lo = V & 0xFFFFFFFFLL;
2886 if (isInt<8>(Hi) && isInt<8>(Lo)) {
2887 NewD = &HII.get(Hexagon::A2_combineii);
2888 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2892 NewD = &HII.get(Hexagon::CONST64);
2893 NewMI = BuildMI(B, At, DL, *NewD, NewR)
2900 NewInstrs.push_back(NewMI);
2902 replaceAllRegUsesWith(R, NewR);
2908 if (!NewInstrs.empty()) {
2909 MachineFunction &MF = *MI.getParent()->getParent();
2910 dbgs() << "In function: " << MF.getFunction()->getName() << "\n";
2911 dbgs() << "Rewrite: for " << MI << " created " << *NewInstrs[0];
2912 for (unsigned i = 1; i < NewInstrs.size(); ++i)
2913 dbgs() << " " << *NewInstrs[i];
2917 AllDefs = (ChangedNum == DefRegs.size());
2918 return ChangedNum > 0;
2921 bool HexagonConstEvaluator::rewriteHexConstUses(MachineInstr &MI,
2922 const CellMap &Inputs) {
2923 bool Changed = false;
2924 unsigned Opc = MI.getOpcode();
2925 MachineBasicBlock &B = *MI.getParent();
2926 const DebugLoc &DL = MI.getDebugLoc();
2927 MachineBasicBlock::iterator At = MI.getIterator();
2928 MachineInstr *NewMI = nullptr;
2931 case Hexagon::M2_maci:
2932 // Convert DefR += mpyi(R2, R3)
2933 // to DefR += mpyi(R, #imm),
2934 // or DefR -= mpyi(R, #imm).
2936 Register DefR(MI.getOperand(0));
2937 assert(!DefR.SubReg);
2938 Register R2(MI.getOperand(2));
2939 Register R3(MI.getOperand(3));
2940 assert(Inputs.has(R2.Reg) && Inputs.has(R3.Reg));
2941 LatticeCell LS2, LS3;
2942 // It is enough to get one of the input cells, since we will only try
2943 // to replace one argument---whichever happens to be a single constant.
2944 bool HasC2 = getCell(R2, Inputs, LS2), HasC3 = getCell(R3, Inputs, LS3);
2945 if (!HasC2 && !HasC3)
2947 bool Zero = ((HasC2 && (LS2.properties() & ConstantProperties::Zero)) ||
2948 (HasC3 && (LS3.properties() & ConstantProperties::Zero)));
2949 // If one of the operands is zero, eliminate the multiplication.
2951 // DefR == R1 (tied operands).
2952 MachineOperand &Acc = MI.getOperand(1);
2954 unsigned NewR = R1.Reg;
2956 // Generate COPY. FIXME: Replace with the register:subregister.
2957 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
2958 NewR = MRI->createVirtualRegister(RC);
2959 NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
2960 .addReg(R1.Reg, getRegState(Acc), R1.SubReg);
2962 replaceAllRegUsesWith(DefR.Reg, NewR);
2963 MRI->clearKillFlags(NewR);
2969 if (!LS3.isSingle()) {
2970 if (!LS2.isSingle())
2974 const LatticeCell &LI = Swap ? LS2 : LS3;
2975 const MachineOperand &OpR2 = Swap ? MI.getOperand(3)
2977 // LI is single here.
2979 if (!constToInt(LI.Value, A) || !A.isSignedIntN(8))
2981 int64_t V = A.getSExtValue();
2982 const MCInstrDesc &D = (V >= 0) ? HII.get(Hexagon::M2_macsip)
2983 : HII.get(Hexagon::M2_macsin);
2986 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
2987 unsigned NewR = MRI->createVirtualRegister(RC);
2988 const MachineOperand &Src1 = MI.getOperand(1);
2989 NewMI = BuildMI(B, At, DL, D, NewR)
2990 .addReg(Src1.getReg(), getRegState(Src1), Src1.getSubReg())
2991 .addReg(OpR2.getReg(), getRegState(OpR2), OpR2.getSubReg())
2993 replaceAllRegUsesWith(DefR.Reg, NewR);
2998 case Hexagon::A2_and:
3000 Register R1(MI.getOperand(1));
3001 Register R2(MI.getOperand(2));
3002 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3003 LatticeCell LS1, LS2;
3004 unsigned CopyOf = 0;
3005 // Check if any of the operands is -1 (i.e. all bits set).
3006 if (getCell(R1, Inputs, LS1) && LS1.isSingle()) {
3008 if (constToInt(LS1.Value, M1) && !~M1)
3011 else if (getCell(R2, Inputs, LS2) && LS2.isSingle()) {
3013 if (constToInt(LS2.Value, M1) && !~M1)
3018 MachineOperand &SO = MI.getOperand(CopyOf);
3020 Register DefR(MI.getOperand(0));
3021 unsigned NewR = SR.Reg;
3023 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3024 NewR = MRI->createVirtualRegister(RC);
3025 NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3026 .addReg(SR.Reg, getRegState(SO), SR.SubReg);
3028 replaceAllRegUsesWith(DefR.Reg, NewR);
3029 MRI->clearKillFlags(NewR);
3034 case Hexagon::A2_or:
3036 Register R1(MI.getOperand(1));
3037 Register R2(MI.getOperand(2));
3038 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3039 LatticeCell LS1, LS2;
3040 unsigned CopyOf = 0;
3041 typedef ConstantProperties P;
3042 if (getCell(R1, Inputs, LS1) && (LS1.properties() & P::Zero))
3044 else if (getCell(R2, Inputs, LS2) && (LS2.properties() & P::Zero))
3048 MachineOperand &SO = MI.getOperand(CopyOf);
3050 Register DefR(MI.getOperand(0));
3051 unsigned NewR = SR.Reg;
3053 const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg);
3054 NewR = MRI->createVirtualRegister(RC);
3055 NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
3056 .addReg(SR.Reg, getRegState(SO), SR.SubReg);
3058 replaceAllRegUsesWith(DefR.Reg, NewR);
3059 MRI->clearKillFlags(NewR);
3066 // clear all the kill flags of this new instruction.
3067 for (MachineOperand &MO : NewMI->operands())
3068 if (MO.isReg() && MO.isUse())
3069 MO.setIsKill(false);
3074 dbgs() << "Rewrite: for " << MI;
3076 dbgs() << " created " << *NewMI;
3078 dbgs() << " modified the instruction itself and created:" << *NewMI;
3085 void HexagonConstEvaluator::replaceAllRegUsesWith(unsigned FromReg,
3087 assert(TargetRegisterInfo::isVirtualRegister(FromReg));
3088 assert(TargetRegisterInfo::isVirtualRegister(ToReg));
3089 for (auto I = MRI->use_begin(FromReg), E = MRI->use_end(); I != E;) {
3090 MachineOperand &O = *I;
3096 bool HexagonConstEvaluator::rewriteHexBranch(MachineInstr &BrI,
3097 const CellMap &Inputs) {
3098 MachineBasicBlock &B = *BrI.getParent();
3099 unsigned NumOp = BrI.getNumOperands();
3104 SetVector<const MachineBasicBlock*> Targets;
3105 bool Eval = evaluate(BrI, Inputs, Targets, FallsThru);
3106 unsigned NumTargets = Targets.size();
3107 if (!Eval || NumTargets > 1 || (NumTargets == 1 && FallsThru))
3109 if (BrI.getOpcode() == Hexagon::J2_jump)
3112 DEBUG(dbgs() << "Rewrite(BB#" << B.getNumber() << "):" << BrI);
3113 bool Rewritten = false;
3114 if (NumTargets > 0) {
3115 assert(!FallsThru && "This should have been checked before");
3116 // MIB.addMBB needs non-const pointer.
3117 MachineBasicBlock *TargetB = const_cast<MachineBasicBlock*>(Targets[0]);
3118 bool Moot = B.isLayoutSuccessor(TargetB);
3120 // If we build a branch here, we must make sure that it won't be
3121 // erased as "non-executable". We can't mark any new instructions
3122 // as executable here, so we need to overwrite the BrI, which we
3123 // know is executable.
3124 const MCInstrDesc &JD = HII.get(Hexagon::J2_jump);
3125 auto NI = BuildMI(B, BrI.getIterator(), BrI.getDebugLoc(), JD)
3128 while (BrI.getNumOperands() > 0)
3129 BrI.RemoveOperand(0);
3130 // This ensures that all implicit operands (e.g. %R31<imp-def>, etc)
3131 // are present in the rewritten branch.
3132 for (auto &Op : NI->operands())
3134 NI->eraseFromParent();
3139 // Do not erase instructions. A newly created instruction could get
3140 // the same address as an instruction marked as executable during the
3143 replaceWithNop(BrI);
3147 FunctionPass *llvm::createHexagonConstPropagationPass() {
3148 return new HexagonConstPropagation();