1 //===- HexagonConstPropagation.cpp ----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #define DEBUG_TYPE "hcp"
11 #include "HexagonInstrInfo.h"
12 #include "HexagonRegisterInfo.h"
13 #include "HexagonSubtarget.h"
14 #include "llvm/ADT/APFloat.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/PostOrderIterator.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineOperand.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/TargetRegisterInfo.h"
28 #include "llvm/CodeGen/TargetSubtargetInfo.h"
29 #include "llvm/IR/Constants.h"
30 #include "llvm/IR/Type.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
52 // Properties of a value that are tracked by the propagation.
53 // A property that is marked as present (i.e. bit is set) dentes that the
54 // value is known (proven) to have this property. Not all combinations
55 // of bits make sense, for example Zero and NonZero are mutually exclusive,
56 // but on the other hand, Zero implies Finite. In this case, whenever
57 // the Zero property is present, Finite should also be present.
58 class ConstantProperties
{
68 NumericProperties
= (Zero
|NonZero
|Finite
|Infinity
|NaN
|SignedZero
),
71 SignProperties
= (PosOrZero
|NegOrZero
),
72 Everything
= (NumericProperties
|SignProperties
)
75 // For a given constant, deduce the set of trackable properties that this
77 static uint32_t deduce(const Constant
*C
);
80 // A representation of a register as it can appear in a MachineOperand,
81 // i.e. a pair register:subregister.
83 // FIXME: Use TargetInstrInfo::RegSubRegPair. Also duplicated in
84 // HexagonGenPredicate
85 struct RegisterSubReg
{
88 explicit RegisterSubReg(unsigned R
, unsigned SR
= 0) : Reg(R
), SubReg(SR
) {}
89 explicit RegisterSubReg(const MachineOperand
&MO
)
90 : Reg(MO
.getReg()), SubReg(MO
.getSubReg()) {}
92 void print(const TargetRegisterInfo
*TRI
= nullptr) const {
93 dbgs() << printReg(Reg
, TRI
, SubReg
);
96 bool operator== (const RegisterSubReg
&R
) const {
97 return (Reg
== R
.Reg
) && (SubReg
== R
.SubReg
);
101 // Lattice cell, based on that was described in the W-Z paper on constant
103 // Latice cell will be allowed to hold multiple constant values. While
104 // multiple values would normally indicate "bottom", we can still derive
105 // some useful information from them. For example, comparison X > 0
106 // could be folded if all the values in the cell associated with X are
110 enum { Normal
, Top
, Bottom
};
112 static const unsigned MaxCellSize
= 4;
116 unsigned IsSpecial
:1;
122 const Constant
*Value
;
123 const Constant
*Values
[MaxCellSize
];
126 LatticeCell() : Kind(Top
), Size(0), IsSpecial(false) {
127 for (unsigned i
= 0; i
< MaxCellSize
; ++i
)
131 bool meet(const LatticeCell
&L
);
132 bool add(const Constant
*C
);
133 bool add(uint32_t Property
);
134 uint32_t properties() const;
135 unsigned size() const { return Size
; }
137 LatticeCell
&operator= (const LatticeCell
&L
) {
139 // This memcpy also copies Properties (when L.Size == 0).
140 uint32_t N
= L
.IsSpecial
? sizeof L
.Properties
141 : L
.Size
*sizeof(const Constant
*);
142 memcpy(Values
, L
.Values
, N
);
145 IsSpecial
= L
.IsSpecial
;
150 bool isSingle() const { return size() == 1; }
151 bool isProperty() const { return IsSpecial
; }
152 bool isTop() const { return Kind
== Top
; }
153 bool isBottom() const { return Kind
== Bottom
; }
156 bool Changed
= (Kind
!= Bottom
);
163 void print(raw_ostream
&os
) const;
172 bool convertToProperty();
176 raw_ostream
&operator<< (raw_ostream
&os
, const LatticeCell
&L
) {
182 class MachineConstEvaluator
;
184 class MachineConstPropagator
{
186 MachineConstPropagator(MachineConstEvaluator
&E
) : MCE(E
) {
190 // Mapping: vreg -> cell
191 // The keys are registers _without_ subregisters. This won't allow
192 // definitions in the form of "vreg:subreg = ...". Such definitions
193 // would be questionable from the point of view of SSA, since the "vreg"
194 // could not be initialized in its entirety (specifically, an instruction
195 // defining the "other part" of "vreg" would also count as a definition
196 // of "vreg", which would violate the SSA).
197 // If a value of a pair vreg:subreg needs to be obtained, the cell for
198 // "vreg" needs to be looked up, and then the value of subregister "subreg"
199 // needs to be evaluated.
207 void clear() { Map
.clear(); }
209 bool has(unsigned R
) const {
210 // All non-virtual registers are considered "bottom".
211 if (!Register::isVirtualRegister(R
))
213 MapType::const_iterator F
= Map
.find(R
);
214 return F
!= Map
.end();
217 const LatticeCell
&get(unsigned R
) const {
218 if (!Register::isVirtualRegister(R
))
220 MapType::const_iterator F
= Map
.find(R
);
226 // Invalidates any const references.
227 void update(unsigned R
, const LatticeCell
&L
) {
231 void print(raw_ostream
&os
, const TargetRegisterInfo
&TRI
) const;
234 using MapType
= std::map
<unsigned, LatticeCell
>;
237 // To avoid creating "top" entries, return a const reference to
238 // this cell in "get". Also, have a "Bottom" cell to return from
239 // get when a value of a physical register is requested.
240 LatticeCell Top
, Bottom
;
243 using const_iterator
= MapType::const_iterator
;
245 const_iterator
begin() const { return Map
.begin(); }
246 const_iterator
end() const { return Map
.end(); }
249 bool run(MachineFunction
&MF
);
252 void visitPHI(const MachineInstr
&PN
);
253 void visitNonBranch(const MachineInstr
&MI
);
254 void visitBranchesFrom(const MachineInstr
&BrI
);
255 void visitUsesOf(unsigned R
);
256 bool computeBlockSuccessors(const MachineBasicBlock
*MB
,
257 SetVector
<const MachineBasicBlock
*> &Targets
);
258 void removeCFGEdge(MachineBasicBlock
*From
, MachineBasicBlock
*To
);
260 void propagate(MachineFunction
&MF
);
261 bool rewrite(MachineFunction
&MF
);
263 MachineRegisterInfo
*MRI
;
264 MachineConstEvaluator
&MCE
;
266 using CFGEdge
= std::pair
<unsigned, unsigned>;
267 using SetOfCFGEdge
= std::set
<CFGEdge
>;
268 using SetOfInstr
= std::set
<const MachineInstr
*>;
269 using QueueOfCFGEdge
= std::queue
<CFGEdge
>;
273 SetOfCFGEdge EdgeExec
;
274 SetOfInstr InstrExec
;
275 QueueOfCFGEdge FlowQ
;
278 // The "evaluator/rewriter" of machine instructions. This is an abstract
279 // base class that provides the interface that the propagator will use,
280 // as well as some helper functions that are target-independent.
281 class MachineConstEvaluator
{
283 MachineConstEvaluator(MachineFunction
&Fn
)
284 : TRI(*Fn
.getSubtarget().getRegisterInfo()),
285 MF(Fn
), CX(Fn
.getFunction().getContext()) {}
286 virtual ~MachineConstEvaluator() = default;
288 // The required interface:
289 // - A set of three "evaluate" functions. Each returns "true" if the
290 // computation succeeded, "false" otherwise.
291 // (1) Given an instruction MI, and the map with input values "Inputs",
292 // compute the set of output values "Outputs". An example of when
293 // the computation can "fail" is if MI is not an instruction that
294 // is recognized by the evaluator.
295 // (2) Given a register R (as reg:subreg), compute the cell that
296 // corresponds to the "subreg" part of the given register.
297 // (3) Given a branch instruction BrI, compute the set of target blocks.
298 // If the branch can fall-through, add null (0) to the list of
300 // - A function "rewrite", that given the cell map after propagation,
301 // could rewrite instruction MI in a more beneficial form. Return
302 // "true" if a change has been made, "false" otherwise.
303 using CellMap
= MachineConstPropagator::CellMap
;
304 virtual bool evaluate(const MachineInstr
&MI
, const CellMap
&Inputs
,
305 CellMap
&Outputs
) = 0;
306 virtual bool evaluate(const RegisterSubReg
&R
, const LatticeCell
&SrcC
,
307 LatticeCell
&Result
) = 0;
308 virtual bool evaluate(const MachineInstr
&BrI
, const CellMap
&Inputs
,
309 SetVector
<const MachineBasicBlock
*> &Targets
,
310 bool &CanFallThru
) = 0;
311 virtual bool rewrite(MachineInstr
&MI
, const CellMap
&Inputs
) = 0;
313 const TargetRegisterInfo
&TRI
;
324 L
= 0x04, // Less-than property.
325 G
= 0x08, // Greater-than property.
326 U
= 0x40, // Unsigned property.
337 static uint32_t negate(uint32_t Cmp
) {
342 assert((Cmp
& (L
|G
)) != (L
|G
));
349 bool getCell(const RegisterSubReg
&R
, const CellMap
&Inputs
, LatticeCell
&RC
);
350 bool constToInt(const Constant
*C
, APInt
&Val
) const;
351 bool constToFloat(const Constant
*C
, APFloat
&Val
) const;
352 const ConstantInt
*intToConst(const APInt
&Val
) const;
355 bool evaluateCMPrr(uint32_t Cmp
, const RegisterSubReg
&R1
, const RegisterSubReg
&R2
,
356 const CellMap
&Inputs
, bool &Result
);
357 bool evaluateCMPri(uint32_t Cmp
, const RegisterSubReg
&R1
, const APInt
&A2
,
358 const CellMap
&Inputs
, bool &Result
);
359 bool evaluateCMPrp(uint32_t Cmp
, const RegisterSubReg
&R1
, uint64_t Props2
,
360 const CellMap
&Inputs
, bool &Result
);
361 bool evaluateCMPii(uint32_t Cmp
, const APInt
&A1
, const APInt
&A2
,
363 bool evaluateCMPpi(uint32_t Cmp
, uint32_t Props
, const APInt
&A2
,
365 bool evaluateCMPpp(uint32_t Cmp
, uint32_t Props1
, uint32_t Props2
,
368 bool evaluateCOPY(const RegisterSubReg
&R1
, const CellMap
&Inputs
,
369 LatticeCell
&Result
);
371 // Logical operations.
372 bool evaluateANDrr(const RegisterSubReg
&R1
, const RegisterSubReg
&R2
,
373 const CellMap
&Inputs
, LatticeCell
&Result
);
374 bool evaluateANDri(const RegisterSubReg
&R1
, const APInt
&A2
,
375 const CellMap
&Inputs
, LatticeCell
&Result
);
376 bool evaluateANDii(const APInt
&A1
, const APInt
&A2
, APInt
&Result
);
377 bool evaluateORrr(const RegisterSubReg
&R1
, const RegisterSubReg
&R2
,
378 const CellMap
&Inputs
, LatticeCell
&Result
);
379 bool evaluateORri(const RegisterSubReg
&R1
, const APInt
&A2
,
380 const CellMap
&Inputs
, LatticeCell
&Result
);
381 bool evaluateORii(const APInt
&A1
, const APInt
&A2
, APInt
&Result
);
382 bool evaluateXORrr(const RegisterSubReg
&R1
, const RegisterSubReg
&R2
,
383 const CellMap
&Inputs
, LatticeCell
&Result
);
384 bool evaluateXORri(const RegisterSubReg
&R1
, const APInt
&A2
,
385 const CellMap
&Inputs
, LatticeCell
&Result
);
386 bool evaluateXORii(const APInt
&A1
, const APInt
&A2
, APInt
&Result
);
389 bool evaluateZEXTr(const RegisterSubReg
&R1
, unsigned Width
, unsigned Bits
,
390 const CellMap
&Inputs
, LatticeCell
&Result
);
391 bool evaluateZEXTi(const APInt
&A1
, unsigned Width
, unsigned Bits
,
393 bool evaluateSEXTr(const RegisterSubReg
&R1
, unsigned Width
, unsigned Bits
,
394 const CellMap
&Inputs
, LatticeCell
&Result
);
395 bool evaluateSEXTi(const APInt
&A1
, unsigned Width
, unsigned Bits
,
398 // Leading/trailing bits.
399 bool evaluateCLBr(const RegisterSubReg
&R1
, bool Zeros
, bool Ones
,
400 const CellMap
&Inputs
, LatticeCell
&Result
);
401 bool evaluateCLBi(const APInt
&A1
, bool Zeros
, bool Ones
, APInt
&Result
);
402 bool evaluateCTBr(const RegisterSubReg
&R1
, bool Zeros
, bool Ones
,
403 const CellMap
&Inputs
, LatticeCell
&Result
);
404 bool evaluateCTBi(const APInt
&A1
, bool Zeros
, bool Ones
, APInt
&Result
);
407 bool evaluateEXTRACTr(const RegisterSubReg
&R1
, unsigned Width
, unsigned Bits
,
408 unsigned Offset
, bool Signed
, const CellMap
&Inputs
,
409 LatticeCell
&Result
);
410 bool evaluateEXTRACTi(const APInt
&A1
, unsigned Bits
, unsigned Offset
,
411 bool Signed
, APInt
&Result
);
412 // Vector operations.
413 bool evaluateSplatr(const RegisterSubReg
&R1
, unsigned Bits
, unsigned Count
,
414 const CellMap
&Inputs
, LatticeCell
&Result
);
415 bool evaluateSplati(const APInt
&A1
, unsigned Bits
, unsigned Count
,
419 } // end anonymous namespace
421 uint32_t ConstantProperties::deduce(const Constant
*C
) {
422 if (isa
<ConstantInt
>(C
)) {
423 const ConstantInt
*CI
= cast
<ConstantInt
>(C
);
425 return Zero
| PosOrZero
| NegOrZero
| Finite
;
426 uint32_t Props
= (NonZero
| Finite
);
427 if (CI
->isNegative())
428 return Props
| NegOrZero
;
429 return Props
| PosOrZero
;
432 if (isa
<ConstantFP
>(C
)) {
433 const ConstantFP
*CF
= cast
<ConstantFP
>(C
);
434 uint32_t Props
= CF
->isNegative() ? (NegOrZero
|NonZero
)
437 return (Props
& ~NumericProperties
) | (Zero
|Finite
);
438 Props
= (Props
& ~NumericProperties
) | NonZero
;
440 return (Props
& ~NumericProperties
) | NaN
;
441 const APFloat
&Val
= CF
->getValueAPF();
442 if (Val
.isInfinity())
443 return (Props
& ~NumericProperties
) | Infinity
;
451 // Convert a cell from a set of specific values to a cell that tracks
453 bool LatticeCell::convertToProperty() {
456 // Corner case: converting a fresh (top) cell to "special".
457 // This can happen, when adding a property to a top cell.
458 uint32_t Everything
= ConstantProperties::Everything
;
459 uint32_t Ps
= !isTop() ? properties()
461 if (Ps
!= ConstantProperties::Unknown
) {
471 void LatticeCell::print(raw_ostream
&os
) const {
474 uint32_t Ps
= properties();
475 if (Ps
& ConstantProperties::Zero
)
477 if (Ps
& ConstantProperties::NonZero
)
479 if (Ps
& ConstantProperties::Finite
)
481 if (Ps
& ConstantProperties::Infinity
)
483 if (Ps
& ConstantProperties::NaN
)
485 if (Ps
& ConstantProperties::PosOrZero
)
487 if (Ps
& ConstantProperties::NegOrZero
)
496 } else if (isTop()) {
499 for (unsigned i
= 0; i
< size(); ++i
) {
500 const Constant
*C
= Values
[i
];
510 // "Meet" operation on two cells. This is the key of the propagation
512 bool LatticeCell::meet(const LatticeCell
&L
) {
513 bool Changed
= false;
515 Changed
= setBottom();
516 if (isBottom() || L
.isTop())
520 // L can be neither Top nor Bottom, so *this must have changed.
524 // Top/bottom cases covered. Need to integrate L's set into ours.
526 return add(L
.properties());
527 for (unsigned i
= 0; i
< L
.size(); ++i
) {
528 const Constant
*LC
= L
.Values
[i
];
534 // Add a new constant to the cell. This is actually where the cell update
535 // happens. If a cell has room for more constants, the new constant is added.
536 // Otherwise, the cell is converted to a "property" cell (i.e. a cell that
537 // will track properties of the associated values, and not the values
538 // themselves. Care is taken to handle special cases, like "bottom", etc.
539 bool LatticeCell::add(const Constant
*LC
) {
545 // Cell is not special. Try to add the constant here first,
548 while (Index
< Size
) {
549 const Constant
*C
= Values
[Index
];
550 // If the constant is already here, no change is needed.
555 if (Index
< MaxCellSize
) {
563 bool Changed
= false;
565 // This cell is special, or is not special, but is full. After this
566 // it will be special.
567 Changed
= convertToProperty();
568 uint32_t Ps
= properties();
569 uint32_t NewPs
= Ps
& ConstantProperties::deduce(LC
);
570 if (NewPs
== ConstantProperties::Unknown
) {
581 // Add a property to the cell. This will force the cell to become a property-
583 bool LatticeCell::add(uint32_t Property
) {
584 bool Changed
= convertToProperty();
585 uint32_t Ps
= properties();
586 if (Ps
== (Ps
& Property
))
588 Properties
= Property
& Ps
;
592 // Return the properties of the values in the cell. This is valid for any
593 // cell, and does not alter the cell itself.
594 uint32_t LatticeCell::properties() const {
597 assert(!isTop() && "Should not call this for a top cell");
599 return ConstantProperties::Unknown
;
601 assert(size() > 0 && "Empty cell");
602 uint32_t Ps
= ConstantProperties::deduce(Values
[0]);
603 for (unsigned i
= 1; i
< size(); ++i
) {
604 if (Ps
== ConstantProperties::Unknown
)
606 Ps
&= ConstantProperties::deduce(Values
[i
]);
612 void MachineConstPropagator::CellMap::print(raw_ostream
&os
,
613 const TargetRegisterInfo
&TRI
) const {
615 dbgs() << " " << printReg(I
.first
, &TRI
) << " -> " << I
.second
<< '\n';
619 void MachineConstPropagator::visitPHI(const MachineInstr
&PN
) {
620 const MachineBasicBlock
*MB
= PN
.getParent();
621 unsigned MBN
= MB
->getNumber();
622 LLVM_DEBUG(dbgs() << "Visiting FI(" << printMBBReference(*MB
) << "): " << PN
);
624 const MachineOperand
&MD
= PN
.getOperand(0);
625 RegisterSubReg
DefR(MD
);
626 assert(Register::isVirtualRegister(DefR
.Reg
));
628 bool Changed
= false;
630 // If the def has a sub-register, set the corresponding cell to "bottom".
633 const LatticeCell
&T
= Cells
.get(DefR
.Reg
);
634 Changed
= !T
.isBottom();
635 Cells
.update(DefR
.Reg
, Bottom
);
637 visitUsesOf(DefR
.Reg
);
641 LatticeCell DefC
= Cells
.get(DefR
.Reg
);
643 for (unsigned i
= 1, n
= PN
.getNumOperands(); i
< n
; i
+= 2) {
644 const MachineBasicBlock
*PB
= PN
.getOperand(i
+1).getMBB();
645 unsigned PBN
= PB
->getNumber();
646 if (!EdgeExec
.count(CFGEdge(PBN
, MBN
))) {
647 LLVM_DEBUG(dbgs() << " edge " << printMBBReference(*PB
) << "->"
648 << printMBBReference(*MB
) << " not executable\n");
651 const MachineOperand
&SO
= PN
.getOperand(i
);
652 RegisterSubReg
UseR(SO
);
653 // If the input is not a virtual register, we don't really know what
655 if (!Register::isVirtualRegister(UseR
.Reg
))
657 // If there is no cell for an input register, it means top.
658 if (!Cells
.has(UseR
.Reg
))
662 bool Eval
= MCE
.evaluate(UseR
, Cells
.get(UseR
.Reg
), SrcC
);
663 LLVM_DEBUG(dbgs() << " edge from " << printMBBReference(*PB
) << ": "
664 << printReg(UseR
.Reg
, &MCE
.TRI
, UseR
.SubReg
) << SrcC
666 Changed
|= Eval
? DefC
.meet(SrcC
)
668 Cells
.update(DefR
.Reg
, DefC
);
673 visitUsesOf(DefR
.Reg
);
676 void MachineConstPropagator::visitNonBranch(const MachineInstr
&MI
) {
677 LLVM_DEBUG(dbgs() << "Visiting MI(" << printMBBReference(*MI
.getParent())
680 bool Eval
= MCE
.evaluate(MI
, Cells
, Outputs
);
683 dbgs() << " outputs:";
684 for (auto &I
: Outputs
)
685 dbgs() << ' ' << I
.second
;
690 // Update outputs. If the value was not computed, set all the
691 // def cells to bottom.
692 for (const MachineOperand
&MO
: MI
.operands()) {
693 if (!MO
.isReg() || !MO
.isDef())
695 RegisterSubReg
DefR(MO
);
696 // Only track virtual registers.
697 if (!Register::isVirtualRegister(DefR
.Reg
))
699 bool Changed
= false;
700 // If the evaluation failed, set cells for all output registers to bottom.
702 const LatticeCell
&T
= Cells
.get(DefR
.Reg
);
703 Changed
= !T
.isBottom();
704 Cells
.update(DefR
.Reg
, Bottom
);
706 // Find the corresponding cell in the computed outputs.
707 // If it's not there, go on to the next def.
708 if (!Outputs
.has(DefR
.Reg
))
710 LatticeCell RC
= Cells
.get(DefR
.Reg
);
711 Changed
= RC
.meet(Outputs
.get(DefR
.Reg
));
712 Cells
.update(DefR
.Reg
, RC
);
715 visitUsesOf(DefR
.Reg
);
719 // Starting at a given branch, visit remaining branches in the block.
720 // Traverse over the subsequent branches for as long as the preceding one
721 // can fall through. Add all the possible targets to the flow work queue,
722 // including the potential fall-through to the layout-successor block.
723 void MachineConstPropagator::visitBranchesFrom(const MachineInstr
&BrI
) {
724 const MachineBasicBlock
&B
= *BrI
.getParent();
725 unsigned MBN
= B
.getNumber();
726 MachineBasicBlock::const_iterator It
= BrI
.getIterator();
727 MachineBasicBlock::const_iterator End
= B
.end();
729 SetVector
<const MachineBasicBlock
*> Targets
;
730 bool EvalOk
= true, FallsThru
= true;
732 const MachineInstr
&MI
= *It
;
733 InstrExec
.insert(&MI
);
734 LLVM_DEBUG(dbgs() << "Visiting " << (EvalOk
? "BR" : "br") << "("
735 << printMBBReference(B
) << "): " << MI
);
736 // Do not evaluate subsequent branches if the evaluation of any of the
737 // previous branches failed. Keep iterating over the branches only
738 // to mark them as executable.
739 EvalOk
= EvalOk
&& MCE
.evaluate(MI
, Cells
, Targets
, FallsThru
);
748 // Need to add all CFG successors that lead to EH landing pads.
749 // There won't be explicit branches to these blocks, but they must
751 for (const MachineBasicBlock
*SB
: B
.successors()) {
756 const MachineFunction
&MF
= *B
.getParent();
757 MachineFunction::const_iterator BI
= B
.getIterator();
758 MachineFunction::const_iterator Next
= std::next(BI
);
759 if (Next
!= MF
.end())
760 Targets
.insert(&*Next
);
763 // If the evaluation of the branches failed, make "Targets" to be the
764 // set of all successors of the block from the CFG.
765 // If the evaluation succeeded for all visited branches, then if the
766 // last one set "FallsThru", then add an edge to the layout successor
769 LLVM_DEBUG(dbgs() << " failed to evaluate a branch...adding all CFG "
771 for (const MachineBasicBlock
*SB
: B
.successors())
775 for (const MachineBasicBlock
*TB
: Targets
) {
776 unsigned TBN
= TB
->getNumber();
777 LLVM_DEBUG(dbgs() << " pushing edge " << printMBBReference(B
) << " -> "
778 << printMBBReference(*TB
) << "\n");
779 FlowQ
.push(CFGEdge(MBN
, TBN
));
783 void MachineConstPropagator::visitUsesOf(unsigned Reg
) {
784 LLVM_DEBUG(dbgs() << "Visiting uses of " << printReg(Reg
, &MCE
.TRI
)
785 << Cells
.get(Reg
) << '\n');
786 for (MachineInstr
&MI
: MRI
->use_nodbg_instructions(Reg
)) {
787 // Do not process non-executable instructions. They can become exceutable
788 // later (via a flow-edge in the work queue). In such case, the instruc-
789 // tion will be visited at that time.
790 if (!InstrExec
.count(&MI
))
794 else if (!MI
.isBranch())
797 visitBranchesFrom(MI
);
801 bool MachineConstPropagator::computeBlockSuccessors(const MachineBasicBlock
*MB
,
802 SetVector
<const MachineBasicBlock
*> &Targets
) {
803 MachineBasicBlock::const_iterator FirstBr
= MB
->end();
804 for (const MachineInstr
&MI
: *MB
) {
805 if (MI
.isDebugInstr())
808 FirstBr
= MI
.getIterator();
814 MachineBasicBlock::const_iterator End
= MB
->end();
817 for (MachineBasicBlock::const_iterator I
= FirstBr
; I
!= End
; ++I
) {
818 const MachineInstr
&MI
= *I
;
819 // Can there be debug instructions between branches?
820 if (MI
.isDebugInstr())
822 if (!InstrExec
.count(&MI
))
824 bool Eval
= MCE
.evaluate(MI
, Cells
, Targets
, DoNext
);
830 // If the last branch could fall-through, add block's layout successor.
832 MachineFunction::const_iterator BI
= MB
->getIterator();
833 MachineFunction::const_iterator NextI
= std::next(BI
);
834 if (NextI
!= MB
->getParent()->end())
835 Targets
.insert(&*NextI
);
838 // Add all the EH landing pads.
839 for (const MachineBasicBlock
*SB
: MB
->successors())
846 void MachineConstPropagator::removeCFGEdge(MachineBasicBlock
*From
,
847 MachineBasicBlock
*To
) {
848 // First, remove the CFG successor/predecessor information.
849 From
->removeSuccessor(To
);
850 // Remove all corresponding PHI operands in the To block.
851 for (auto I
= To
->begin(), E
= To
->getFirstNonPHI(); I
!= E
; ++I
) {
852 MachineInstr
*PN
= &*I
;
853 // reg0 = PHI reg1, bb2, reg3, bb4, ...
854 int N
= PN
->getNumOperands()-2;
856 if (PN
->getOperand(N
+1).getMBB() == From
) {
857 PN
->RemoveOperand(N
+1);
858 PN
->RemoveOperand(N
);
865 void MachineConstPropagator::propagate(MachineFunction
&MF
) {
866 MachineBasicBlock
*Entry
= GraphTraits
<MachineFunction
*>::getEntryNode(&MF
);
867 unsigned EntryNum
= Entry
->getNumber();
869 // Start with a fake edge, just to process the entry node.
870 FlowQ
.push(CFGEdge(EntryNum
, EntryNum
));
872 while (!FlowQ
.empty()) {
873 CFGEdge Edge
= FlowQ
.front();
877 dbgs() << "Picked edge "
878 << printMBBReference(*MF
.getBlockNumbered(Edge
.first
)) << "->"
879 << printMBBReference(*MF
.getBlockNumbered(Edge
.second
)) << '\n');
880 if (Edge
.first
!= EntryNum
)
881 if (EdgeExec
.count(Edge
))
883 EdgeExec
.insert(Edge
);
884 MachineBasicBlock
*SB
= MF
.getBlockNumbered(Edge
.second
);
886 // Process the block in three stages:
887 // - visit all PHI nodes,
888 // - visit all non-branch instructions,
889 // - visit block branches.
890 MachineBasicBlock::const_iterator It
= SB
->begin(), End
= SB
->end();
892 // Visit PHI nodes in the successor block.
893 while (It
!= End
&& It
->isPHI()) {
894 InstrExec
.insert(&*It
);
899 // If the successor block just became executable, visit all instructions.
900 // To see if this is the first time we're visiting it, check the first
901 // non-debug instruction to see if it is executable.
902 while (It
!= End
&& It
->isDebugInstr())
904 assert(It
== End
|| !It
->isPHI());
905 // If this block has been visited, go on to the next one.
906 if (It
!= End
&& InstrExec
.count(&*It
))
908 // For now, scan all non-branch instructions. Branches require different
910 while (It
!= End
&& !It
->isBranch()) {
911 if (!It
->isDebugInstr()) {
912 InstrExec
.insert(&*It
);
918 // Time to process the end of the block. This is different from
919 // processing regular (non-branch) instructions, because there can
920 // be multiple branches in a block, and they can cause the block to
923 visitBranchesFrom(*It
);
925 // If the block didn't have a branch, add all successor edges to the
926 // work queue. (There should really be only one successor in such case.)
927 unsigned SBN
= SB
->getNumber();
928 for (const MachineBasicBlock
*SSB
: SB
->successors())
929 FlowQ
.push(CFGEdge(SBN
, SSB
->getNumber()));
934 dbgs() << "Cells after propagation:\n";
935 Cells
.print(dbgs(), MCE
.TRI
);
936 dbgs() << "Dead CFG edges:\n";
937 for (const MachineBasicBlock
&B
: MF
) {
938 unsigned BN
= B
.getNumber();
939 for (const MachineBasicBlock
*SB
: B
.successors()) {
940 unsigned SN
= SB
->getNumber();
941 if (!EdgeExec
.count(CFGEdge(BN
, SN
)))
942 dbgs() << " " << printMBBReference(B
) << " -> "
943 << printMBBReference(*SB
) << '\n';
949 bool MachineConstPropagator::rewrite(MachineFunction
&MF
) {
950 bool Changed
= false;
951 // Rewrite all instructions based on the collected cell information.
953 // Traverse the instructions in a post-order, so that rewriting an
954 // instruction can make changes "downstream" in terms of control-flow
955 // without affecting the rewriting process. (We should not change
956 // instructions that have not yet been visited by the rewriter.)
957 // The reason for this is that the rewriter can introduce new vregs,
958 // and replace uses of old vregs (which had corresponding cells
959 // computed during propagation) with these new vregs (which at this
960 // point would not have any cells, and would appear to be "top").
961 // If an attempt was made to evaluate an instruction with a fresh
962 // "top" vreg, it would cause an error (abend) in the evaluator.
964 // Collect the post-order-traversal block ordering. The subsequent
965 // traversal/rewrite will update block successors, so it's safer
966 // if the visiting order it computed ahead of time.
967 std::vector
<MachineBasicBlock
*> POT
;
968 for (MachineBasicBlock
*B
: post_order(&MF
))
972 for (MachineBasicBlock
*B
: POT
) {
973 // Walk the block backwards (which usually begin with the branches).
974 // If any branch is rewritten, we may need to update the successor
975 // information for this block. Unless the block's successors can be
976 // precisely determined (which may not be the case for indirect
977 // branches), we cannot modify any branch.
979 // Compute the successor information.
980 SetVector
<const MachineBasicBlock
*> Targets
;
981 bool HaveTargets
= computeBlockSuccessors(B
, Targets
);
982 // Rewrite the executable instructions. Skip branches if we don't
983 // have block successor information.
984 for (auto I
= B
->rbegin(), E
= B
->rend(); I
!= E
; ++I
) {
985 MachineInstr
&MI
= *I
;
986 if (InstrExec
.count(&MI
)) {
987 if (MI
.isBranch() && !HaveTargets
)
989 Changed
|= MCE
.rewrite(MI
, Cells
);
992 // The rewriting could rewrite PHI nodes to non-PHI nodes, causing
993 // regular instructions to appear in between PHI nodes. Bring all
994 // the PHI nodes to the beginning of the block.
995 for (auto I
= B
->begin(), E
= B
->end(); I
!= E
; ++I
) {
998 // I is not PHI. Find the next PHI node P.
1006 // Splice P right before I.
1008 // Reset I to point at the just spliced PHI node.
1011 // Update the block successor information: remove unnecessary successors.
1013 SmallVector
<MachineBasicBlock
*,2> ToRemove
;
1014 for (MachineBasicBlock
*SB
: B
->successors()) {
1015 if (!Targets
.count(SB
))
1016 ToRemove
.push_back(const_cast<MachineBasicBlock
*>(SB
));
1019 for (unsigned i
= 0, n
= ToRemove
.size(); i
< n
; ++i
)
1020 removeCFGEdge(B
, ToRemove
[i
]);
1021 // If there are any blocks left in the computed targets, it means that
1022 // we think that the block could go somewhere, but the CFG does not.
1023 // This could legitimately happen in blocks that have non-returning
1024 // calls---we would think that the execution can continue, but the
1025 // CFG will not have a successor edge.
1028 // Need to do some final post-processing.
1029 // If a branch was not executable, it will not get rewritten, but should
1030 // be removed (or replaced with something equivalent to a A2_nop). We can't
1031 // erase instructions during rewriting, so this needs to be delayed until
1033 for (MachineBasicBlock
&B
: MF
) {
1034 MachineBasicBlock::iterator I
= B
.begin(), E
= B
.end();
1036 auto Next
= std::next(I
);
1037 if (I
->isBranch() && !InstrExec
.count(&*I
))
1045 // This is the constant propagation algorithm as described by Wegman-Zadeck.
1046 // Most of the terminology comes from there.
1047 bool MachineConstPropagator::run(MachineFunction
&MF
) {
1048 LLVM_DEBUG(MF
.print(dbgs() << "Starting MachineConstPropagator\n", nullptr));
1050 MRI
= &MF
.getRegInfo();
1055 assert(FlowQ
.empty());
1058 bool Changed
= rewrite(MF
);
1061 dbgs() << "End of MachineConstPropagator (Changed=" << Changed
<< ")\n";
1063 MF
.print(dbgs(), nullptr);
1068 // --------------------------------------------------------------------
1069 // Machine const evaluator.
1071 bool MachineConstEvaluator::getCell(const RegisterSubReg
&R
, const CellMap
&Inputs
,
1073 if (!Register::isVirtualRegister(R
.Reg
))
1075 const LatticeCell
&L
= Inputs
.get(R
.Reg
);
1078 return !RC
.isBottom();
1080 bool Eval
= evaluate(R
, L
, RC
);
1081 return Eval
&& !RC
.isBottom();
1084 bool MachineConstEvaluator::constToInt(const Constant
*C
,
1086 const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(C
);
1089 Val
= CI
->getValue();
1093 const ConstantInt
*MachineConstEvaluator::intToConst(const APInt
&Val
) const {
1094 return ConstantInt::get(CX
, Val
);
1097 bool MachineConstEvaluator::evaluateCMPrr(uint32_t Cmp
, const RegisterSubReg
&R1
,
1098 const RegisterSubReg
&R2
, const CellMap
&Inputs
, bool &Result
) {
1099 assert(Inputs
.has(R1
.Reg
) && Inputs
.has(R2
.Reg
));
1100 LatticeCell LS1
, LS2
;
1101 if (!getCell(R1
, Inputs
, LS1
) || !getCell(R2
, Inputs
, LS2
))
1104 bool IsProp1
= LS1
.isProperty();
1105 bool IsProp2
= LS2
.isProperty();
1107 uint32_t Prop1
= LS1
.properties();
1109 return evaluateCMPpp(Cmp
, Prop1
, LS2
.properties(), Result
);
1110 uint32_t NegCmp
= Comparison::negate(Cmp
);
1111 return evaluateCMPrp(NegCmp
, R2
, Prop1
, Inputs
, Result
);
1114 uint32_t Prop2
= LS2
.properties();
1115 return evaluateCMPrp(Cmp
, R1
, Prop2
, Inputs
, Result
);
1119 bool IsTrue
= true, IsFalse
= true;
1120 for (unsigned i
= 0; i
< LS2
.size(); ++i
) {
1122 bool Computed
= constToInt(LS2
.Values
[i
], A
) &&
1123 evaluateCMPri(Cmp
, R1
, A
, Inputs
, Res
);
1129 assert(!IsTrue
|| !IsFalse
);
1130 // The actual logical value of the comparison is same as IsTrue.
1132 // Return true if the result was proven to be true or proven to be false.
1133 return IsTrue
|| IsFalse
;
1136 bool MachineConstEvaluator::evaluateCMPri(uint32_t Cmp
, const RegisterSubReg
&R1
,
1137 const APInt
&A2
, const CellMap
&Inputs
, bool &Result
) {
1138 assert(Inputs
.has(R1
.Reg
));
1140 if (!getCell(R1
, Inputs
, LS
))
1142 if (LS
.isProperty())
1143 return evaluateCMPpi(Cmp
, LS
.properties(), A2
, Result
);
1146 bool IsTrue
= true, IsFalse
= true;
1147 for (unsigned i
= 0; i
< LS
.size(); ++i
) {
1149 bool Computed
= constToInt(LS
.Values
[i
], A
) &&
1150 evaluateCMPii(Cmp
, A
, A2
, Res
);
1156 assert(!IsTrue
|| !IsFalse
);
1157 // The actual logical value of the comparison is same as IsTrue.
1159 // Return true if the result was proven to be true or proven to be false.
1160 return IsTrue
|| IsFalse
;
1163 bool MachineConstEvaluator::evaluateCMPrp(uint32_t Cmp
, const RegisterSubReg
&R1
,
1164 uint64_t Props2
, const CellMap
&Inputs
, bool &Result
) {
1165 assert(Inputs
.has(R1
.Reg
));
1167 if (!getCell(R1
, Inputs
, LS
))
1169 if (LS
.isProperty())
1170 return evaluateCMPpp(Cmp
, LS
.properties(), Props2
, Result
);
1173 uint32_t NegCmp
= Comparison::negate(Cmp
);
1174 bool IsTrue
= true, IsFalse
= true;
1175 for (unsigned i
= 0; i
< LS
.size(); ++i
) {
1177 bool Computed
= constToInt(LS
.Values
[i
], A
) &&
1178 evaluateCMPpi(NegCmp
, Props2
, A
, Res
);
1184 assert(!IsTrue
|| !IsFalse
);
1186 return IsTrue
|| IsFalse
;
1189 bool MachineConstEvaluator::evaluateCMPii(uint32_t Cmp
, const APInt
&A1
,
1190 const APInt
&A2
, bool &Result
) {
1191 // NE is a special kind of comparison (not composed of smaller properties).
1192 if (Cmp
== Comparison::NE
) {
1193 Result
= !APInt::isSameValue(A1
, A2
);
1196 if (Cmp
== Comparison::EQ
) {
1197 Result
= APInt::isSameValue(A1
, A2
);
1200 if (Cmp
& Comparison::EQ
) {
1201 if (APInt::isSameValue(A1
, A2
))
1202 return (Result
= true);
1204 assert((Cmp
& (Comparison::L
| Comparison::G
)) && "Malformed comparison");
1207 unsigned W1
= A1
.getBitWidth();
1208 unsigned W2
= A2
.getBitWidth();
1209 unsigned MaxW
= (W1
>= W2
) ? W1
: W2
;
1210 if (Cmp
& Comparison::U
) {
1211 const APInt Zx1
= A1
.zextOrSelf(MaxW
);
1212 const APInt Zx2
= A2
.zextOrSelf(MaxW
);
1213 if (Cmp
& Comparison::L
)
1214 Result
= Zx1
.ult(Zx2
);
1215 else if (Cmp
& Comparison::G
)
1216 Result
= Zx2
.ult(Zx1
);
1220 // Signed comparison.
1221 const APInt Sx1
= A1
.sextOrSelf(MaxW
);
1222 const APInt Sx2
= A2
.sextOrSelf(MaxW
);
1223 if (Cmp
& Comparison::L
)
1224 Result
= Sx1
.slt(Sx2
);
1225 else if (Cmp
& Comparison::G
)
1226 Result
= Sx2
.slt(Sx1
);
1230 bool MachineConstEvaluator::evaluateCMPpi(uint32_t Cmp
, uint32_t Props
,
1231 const APInt
&A2
, bool &Result
) {
1232 if (Props
== ConstantProperties::Unknown
)
1235 // Should never see NaN here, but check for it for completeness.
1236 if (Props
& ConstantProperties::NaN
)
1238 // Infinity could theoretically be compared to a number, but the
1239 // presence of infinity here would be very suspicious. If we don't
1240 // know for sure that the number is finite, bail out.
1241 if (!(Props
& ConstantProperties::Finite
))
1244 // Let X be a number that has properties Props.
1246 if (Cmp
& Comparison::U
) {
1247 // In case of unsigned comparisons, we can only compare against 0.
1249 // Any x!=0 will be considered >0 in an unsigned comparison.
1250 if (Props
& ConstantProperties::Zero
)
1251 Result
= (Cmp
& Comparison::EQ
);
1252 else if (Props
& ConstantProperties::NonZero
)
1253 Result
= (Cmp
& Comparison::G
) || (Cmp
== Comparison::NE
);
1258 // A2 is not zero. The only handled case is if X = 0.
1259 if (Props
& ConstantProperties::Zero
) {
1260 Result
= (Cmp
& Comparison::L
) || (Cmp
== Comparison::NE
);
1266 // Signed comparisons are different.
1267 if (Props
& ConstantProperties::Zero
) {
1269 Result
= (Cmp
& Comparison::EQ
);
1271 Result
= (Cmp
== Comparison::NE
) ||
1272 ((Cmp
& Comparison::L
) && !A2
.isNegative()) ||
1273 ((Cmp
& Comparison::G
) && A2
.isNegative());
1276 if (Props
& ConstantProperties::PosOrZero
) {
1277 // X >= 0 and !(A2 < 0) => cannot compare
1278 if (!A2
.isNegative())
1280 // X >= 0 and A2 < 0
1281 Result
= (Cmp
& Comparison::G
) || (Cmp
== Comparison::NE
);
1284 if (Props
& ConstantProperties::NegOrZero
) {
1285 // X <= 0 and Src1 < 0 => cannot compare
1286 if (A2
== 0 || A2
.isNegative())
1288 // X <= 0 and A2 > 0
1289 Result
= (Cmp
& Comparison::L
) || (Cmp
== Comparison::NE
);
1296 bool MachineConstEvaluator::evaluateCMPpp(uint32_t Cmp
, uint32_t Props1
,
1297 uint32_t Props2
, bool &Result
) {
1298 using P
= ConstantProperties
;
1300 if ((Props1
& P::NaN
) && (Props2
& P::NaN
))
1302 if (!(Props1
& P::Finite
) || !(Props2
& P::Finite
))
1305 bool Zero1
= (Props1
& P::Zero
), Zero2
= (Props2
& P::Zero
);
1306 bool NonZero1
= (Props1
& P::NonZero
), NonZero2
= (Props2
& P::NonZero
);
1307 if (Zero1
&& Zero2
) {
1308 Result
= (Cmp
& Comparison::EQ
);
1311 if (Cmp
== Comparison::NE
) {
1312 if ((Zero1
&& NonZero2
) || (NonZero1
&& Zero2
))
1313 return (Result
= true);
1317 if (Cmp
& Comparison::U
) {
1318 // In unsigned comparisons, we can only compare against a known zero,
1319 // or a known non-zero.
1320 if (Zero1
&& NonZero2
) {
1321 Result
= (Cmp
& Comparison::L
);
1324 if (NonZero1
&& Zero2
) {
1325 Result
= (Cmp
& Comparison::G
);
1331 // Signed comparison. The comparison is not NE.
1332 bool Poz1
= (Props1
& P::PosOrZero
), Poz2
= (Props2
& P::PosOrZero
);
1333 bool Nez1
= (Props1
& P::NegOrZero
), Nez2
= (Props2
& P::NegOrZero
);
1335 if (NonZero1
|| NonZero2
) {
1336 Result
= (Cmp
& Comparison::L
);
1339 // Either (or both) could be zero. Can only say that X <= Y.
1340 if ((Cmp
& Comparison::EQ
) && (Cmp
& Comparison::L
))
1341 return (Result
= true);
1344 if (NonZero1
|| NonZero2
) {
1345 Result
= (Cmp
& Comparison::G
);
1348 // Either (or both) could be zero. Can only say that X >= Y.
1349 if ((Cmp
& Comparison::EQ
) && (Cmp
& Comparison::G
))
1350 return (Result
= true);
1356 bool MachineConstEvaluator::evaluateCOPY(const RegisterSubReg
&R1
,
1357 const CellMap
&Inputs
, LatticeCell
&Result
) {
1358 return getCell(R1
, Inputs
, Result
);
1361 bool MachineConstEvaluator::evaluateANDrr(const RegisterSubReg
&R1
,
1362 const RegisterSubReg
&R2
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1363 assert(Inputs
.has(R1
.Reg
) && Inputs
.has(R2
.Reg
));
1364 const LatticeCell
&L1
= Inputs
.get(R2
.Reg
);
1365 const LatticeCell
&L2
= Inputs
.get(R2
.Reg
);
1366 // If both sources are bottom, exit. Otherwise try to evaluate ANDri
1367 // with the non-bottom argument passed as the immediate. This is to
1368 // catch cases of ANDing with 0.
1369 if (L2
.isBottom()) {
1372 return evaluateANDrr(R2
, R1
, Inputs
, Result
);
1375 if (!evaluate(R2
, L2
, LS2
))
1377 if (LS2
.isBottom() || LS2
.isProperty())
1381 for (unsigned i
= 0; i
< LS2
.size(); ++i
) {
1383 bool Eval
= constToInt(LS2
.Values
[i
], A
) &&
1384 evaluateANDri(R1
, A
, Inputs
, RC
);
1389 return !Result
.isBottom();
1392 bool MachineConstEvaluator::evaluateANDri(const RegisterSubReg
&R1
,
1393 const APInt
&A2
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1394 assert(Inputs
.has(R1
.Reg
));
1396 return getCell(R1
, Inputs
, Result
);
1399 RC
.add(intToConst(A2
));
1400 // Overwrite Result.
1405 if (!getCell(R1
, Inputs
, LS1
))
1407 if (LS1
.isBottom() || LS1
.isProperty())
1411 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1412 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1413 evaluateANDii(A
, A2
, ResA
);
1416 const Constant
*C
= intToConst(ResA
);
1419 return !Result
.isBottom();
1422 bool MachineConstEvaluator::evaluateANDii(const APInt
&A1
,
1423 const APInt
&A2
, APInt
&Result
) {
1428 bool MachineConstEvaluator::evaluateORrr(const RegisterSubReg
&R1
,
1429 const RegisterSubReg
&R2
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1430 assert(Inputs
.has(R1
.Reg
) && Inputs
.has(R2
.Reg
));
1431 const LatticeCell
&L1
= Inputs
.get(R2
.Reg
);
1432 const LatticeCell
&L2
= Inputs
.get(R2
.Reg
);
1433 // If both sources are bottom, exit. Otherwise try to evaluate ORri
1434 // with the non-bottom argument passed as the immediate. This is to
1435 // catch cases of ORing with -1.
1436 if (L2
.isBottom()) {
1439 return evaluateORrr(R2
, R1
, Inputs
, Result
);
1442 if (!evaluate(R2
, L2
, LS2
))
1444 if (LS2
.isBottom() || LS2
.isProperty())
1448 for (unsigned i
= 0; i
< LS2
.size(); ++i
) {
1450 bool Eval
= constToInt(LS2
.Values
[i
], A
) &&
1451 evaluateORri(R1
, A
, Inputs
, RC
);
1456 return !Result
.isBottom();
1459 bool MachineConstEvaluator::evaluateORri(const RegisterSubReg
&R1
,
1460 const APInt
&A2
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1461 assert(Inputs
.has(R1
.Reg
));
1463 return getCell(R1
, Inputs
, Result
);
1466 RC
.add(intToConst(A2
));
1467 // Overwrite Result.
1472 if (!getCell(R1
, Inputs
, LS1
))
1474 if (LS1
.isBottom() || LS1
.isProperty())
1478 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1479 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1480 evaluateORii(A
, A2
, ResA
);
1483 const Constant
*C
= intToConst(ResA
);
1486 return !Result
.isBottom();
1489 bool MachineConstEvaluator::evaluateORii(const APInt
&A1
,
1490 const APInt
&A2
, APInt
&Result
) {
1495 bool MachineConstEvaluator::evaluateXORrr(const RegisterSubReg
&R1
,
1496 const RegisterSubReg
&R2
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1497 assert(Inputs
.has(R1
.Reg
) && Inputs
.has(R2
.Reg
));
1498 LatticeCell LS1
, LS2
;
1499 if (!getCell(R1
, Inputs
, LS1
) || !getCell(R2
, Inputs
, LS2
))
1501 if (LS1
.isProperty()) {
1502 if (LS1
.properties() & ConstantProperties::Zero
)
1503 return !(Result
= LS2
).isBottom();
1506 if (LS2
.isProperty()) {
1507 if (LS2
.properties() & ConstantProperties::Zero
)
1508 return !(Result
= LS1
).isBottom();
1513 for (unsigned i
= 0; i
< LS2
.size(); ++i
) {
1515 bool Eval
= constToInt(LS2
.Values
[i
], A
) &&
1516 evaluateXORri(R1
, A
, Inputs
, RC
);
1521 return !Result
.isBottom();
1524 bool MachineConstEvaluator::evaluateXORri(const RegisterSubReg
&R1
,
1525 const APInt
&A2
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1526 assert(Inputs
.has(R1
.Reg
));
1528 if (!getCell(R1
, Inputs
, LS1
))
1530 if (LS1
.isProperty()) {
1531 if (LS1
.properties() & ConstantProperties::Zero
) {
1532 const Constant
*C
= intToConst(A2
);
1534 return !Result
.isBottom();
1540 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1541 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1542 evaluateXORii(A
, A2
, XA
);
1545 const Constant
*C
= intToConst(XA
);
1548 return !Result
.isBottom();
1551 bool MachineConstEvaluator::evaluateXORii(const APInt
&A1
,
1552 const APInt
&A2
, APInt
&Result
) {
1557 bool MachineConstEvaluator::evaluateZEXTr(const RegisterSubReg
&R1
, unsigned Width
,
1558 unsigned Bits
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1559 assert(Inputs
.has(R1
.Reg
));
1561 if (!getCell(R1
, Inputs
, LS1
))
1563 if (LS1
.isProperty())
1567 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1568 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1569 evaluateZEXTi(A
, Width
, Bits
, XA
);
1572 const Constant
*C
= intToConst(XA
);
1578 bool MachineConstEvaluator::evaluateZEXTi(const APInt
&A1
, unsigned Width
,
1579 unsigned Bits
, APInt
&Result
) {
1580 unsigned BW
= A1
.getBitWidth();
1582 assert(Width
>= Bits
&& BW
>= Bits
);
1583 APInt Mask
= APInt::getLowBitsSet(Width
, Bits
);
1584 Result
= A1
.zextOrTrunc(Width
) & Mask
;
1588 bool MachineConstEvaluator::evaluateSEXTr(const RegisterSubReg
&R1
, unsigned Width
,
1589 unsigned Bits
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1590 assert(Inputs
.has(R1
.Reg
));
1592 if (!getCell(R1
, Inputs
, LS1
))
1594 if (LS1
.isBottom() || LS1
.isProperty())
1598 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1599 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1600 evaluateSEXTi(A
, Width
, Bits
, XA
);
1603 const Constant
*C
= intToConst(XA
);
1609 bool MachineConstEvaluator::evaluateSEXTi(const APInt
&A1
, unsigned Width
,
1610 unsigned Bits
, APInt
&Result
) {
1611 unsigned BW
= A1
.getBitWidth();
1612 assert(Width
>= Bits
&& BW
>= Bits
);
1613 // Special case to make things faster for smaller source widths.
1614 // Sign extension of 0 bits generates 0 as a result. This is consistent
1615 // with what the HW does.
1617 Result
= APInt(Width
, 0);
1620 // In C, shifts by 64 invoke undefined behavior: handle that case in APInt.
1621 if (BW
<= 64 && Bits
!= 0) {
1622 int64_t V
= A1
.getSExtValue();
1625 V
= static_cast<int8_t>(V
);
1628 V
= static_cast<int16_t>(V
);
1631 V
= static_cast<int32_t>(V
);
1634 // Shift left to lose all bits except lower "Bits" bits, then shift
1635 // the value back, replicating what was a sign bit after the first
1637 V
= (V
<< (64-Bits
)) >> (64-Bits
);
1640 // V is a 64-bit sign-extended value. Convert it to APInt of desired
1642 Result
= APInt(Width
, V
, true);
1645 // Slow case: the value doesn't fit in int64_t.
1647 Result
= A1
.trunc(Bits
).sext(Width
);
1649 Result
= A1
.sext(Width
);
1653 bool MachineConstEvaluator::evaluateCLBr(const RegisterSubReg
&R1
, bool Zeros
,
1654 bool Ones
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1655 assert(Inputs
.has(R1
.Reg
));
1657 if (!getCell(R1
, Inputs
, LS1
))
1659 if (LS1
.isBottom() || LS1
.isProperty())
1663 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1664 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1665 evaluateCLBi(A
, Zeros
, Ones
, CA
);
1668 const Constant
*C
= intToConst(CA
);
1674 bool MachineConstEvaluator::evaluateCLBi(const APInt
&A1
, bool Zeros
,
1675 bool Ones
, APInt
&Result
) {
1676 unsigned BW
= A1
.getBitWidth();
1677 if (!Zeros
&& !Ones
)
1680 if (Zeros
&& (Count
== 0))
1681 Count
= A1
.countLeadingZeros();
1682 if (Ones
&& (Count
== 0))
1683 Count
= A1
.countLeadingOnes();
1684 Result
= APInt(BW
, static_cast<uint64_t>(Count
), false);
1688 bool MachineConstEvaluator::evaluateCTBr(const RegisterSubReg
&R1
, bool Zeros
,
1689 bool Ones
, const CellMap
&Inputs
, LatticeCell
&Result
) {
1690 assert(Inputs
.has(R1
.Reg
));
1692 if (!getCell(R1
, Inputs
, LS1
))
1694 if (LS1
.isBottom() || LS1
.isProperty())
1698 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1699 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1700 evaluateCTBi(A
, Zeros
, Ones
, CA
);
1703 const Constant
*C
= intToConst(CA
);
1709 bool MachineConstEvaluator::evaluateCTBi(const APInt
&A1
, bool Zeros
,
1710 bool Ones
, APInt
&Result
) {
1711 unsigned BW
= A1
.getBitWidth();
1712 if (!Zeros
&& !Ones
)
1715 if (Zeros
&& (Count
== 0))
1716 Count
= A1
.countTrailingZeros();
1717 if (Ones
&& (Count
== 0))
1718 Count
= A1
.countTrailingOnes();
1719 Result
= APInt(BW
, static_cast<uint64_t>(Count
), false);
1723 bool MachineConstEvaluator::evaluateEXTRACTr(const RegisterSubReg
&R1
,
1724 unsigned Width
, unsigned Bits
, unsigned Offset
, bool Signed
,
1725 const CellMap
&Inputs
, LatticeCell
&Result
) {
1726 assert(Inputs
.has(R1
.Reg
));
1727 assert(Bits
+Offset
<= Width
);
1729 if (!getCell(R1
, Inputs
, LS1
))
1733 if (LS1
.isProperty()) {
1734 uint32_t Ps
= LS1
.properties();
1735 if (Ps
& ConstantProperties::Zero
) {
1736 const Constant
*C
= intToConst(APInt(Width
, 0, false));
1744 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1745 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1746 evaluateEXTRACTi(A
, Bits
, Offset
, Signed
, CA
);
1749 const Constant
*C
= intToConst(CA
);
1755 bool MachineConstEvaluator::evaluateEXTRACTi(const APInt
&A1
, unsigned Bits
,
1756 unsigned Offset
, bool Signed
, APInt
&Result
) {
1757 unsigned BW
= A1
.getBitWidth();
1758 assert(Bits
+Offset
<= BW
);
1759 // Extracting 0 bits generates 0 as a result (as indicated by the HW people).
1761 Result
= APInt(BW
, 0);
1765 int64_t V
= A1
.getZExtValue();
1766 V
<<= (64-Bits
-Offset
);
1770 V
= static_cast<uint64_t>(V
) >> (64-Bits
);
1771 Result
= APInt(BW
, V
, Signed
);
1775 Result
= A1
.shl(BW
-Bits
-Offset
).ashr(BW
-Bits
);
1777 Result
= A1
.shl(BW
-Bits
-Offset
).lshr(BW
-Bits
);
1781 bool MachineConstEvaluator::evaluateSplatr(const RegisterSubReg
&R1
,
1782 unsigned Bits
, unsigned Count
, const CellMap
&Inputs
,
1783 LatticeCell
&Result
) {
1784 assert(Inputs
.has(R1
.Reg
));
1786 if (!getCell(R1
, Inputs
, LS1
))
1788 if (LS1
.isBottom() || LS1
.isProperty())
1792 for (unsigned i
= 0; i
< LS1
.size(); ++i
) {
1793 bool Eval
= constToInt(LS1
.Values
[i
], A
) &&
1794 evaluateSplati(A
, Bits
, Count
, SA
);
1797 const Constant
*C
= intToConst(SA
);
1803 bool MachineConstEvaluator::evaluateSplati(const APInt
&A1
, unsigned Bits
,
1804 unsigned Count
, APInt
&Result
) {
1806 unsigned BW
= A1
.getBitWidth(), SW
= Count
*Bits
;
1807 APInt LoBits
= (Bits
< BW
) ? A1
.trunc(Bits
) : A1
.zextOrSelf(Bits
);
1809 LoBits
= LoBits
.zext(SW
);
1811 APInt
Res(SW
, 0, false);
1812 for (unsigned i
= 0; i
< Count
; ++i
) {
1820 // ----------------------------------------------------------------------
1821 // Hexagon-specific code.
1825 FunctionPass
*createHexagonConstPropagationPass();
1826 void initializeHexagonConstPropagationPass(PassRegistry
&Registry
);
1828 } // end namespace llvm
1832 class HexagonConstEvaluator
: public MachineConstEvaluator
{
1834 HexagonConstEvaluator(MachineFunction
&Fn
);
1836 bool evaluate(const MachineInstr
&MI
, const CellMap
&Inputs
,
1837 CellMap
&Outputs
) override
;
1838 bool evaluate(const RegisterSubReg
&R
, const LatticeCell
&SrcC
,
1839 LatticeCell
&Result
) override
;
1840 bool evaluate(const MachineInstr
&BrI
, const CellMap
&Inputs
,
1841 SetVector
<const MachineBasicBlock
*> &Targets
, bool &FallsThru
)
1843 bool rewrite(MachineInstr
&MI
, const CellMap
&Inputs
) override
;
1846 unsigned getRegBitWidth(unsigned Reg
) const;
1848 static uint32_t getCmp(unsigned Opc
);
1849 static APInt
getCmpImm(unsigned Opc
, unsigned OpX
,
1850 const MachineOperand
&MO
);
1851 void replaceWithNop(MachineInstr
&MI
);
1853 bool evaluateHexRSEQ32(RegisterSubReg RL
, RegisterSubReg RH
, const CellMap
&Inputs
,
1854 LatticeCell
&Result
);
1855 bool evaluateHexCompare(const MachineInstr
&MI
, const CellMap
&Inputs
,
1857 // This is suitable to be called for compare-and-jump instructions.
1858 bool evaluateHexCompare2(uint32_t Cmp
, const MachineOperand
&Src1
,
1859 const MachineOperand
&Src2
, const CellMap
&Inputs
, bool &Result
);
1860 bool evaluateHexLogical(const MachineInstr
&MI
, const CellMap
&Inputs
,
1862 bool evaluateHexCondMove(const MachineInstr
&MI
, const CellMap
&Inputs
,
1864 bool evaluateHexExt(const MachineInstr
&MI
, const CellMap
&Inputs
,
1866 bool evaluateHexVector1(const MachineInstr
&MI
, const CellMap
&Inputs
,
1868 bool evaluateHexVector2(const MachineInstr
&MI
, const CellMap
&Inputs
,
1871 void replaceAllRegUsesWith(unsigned FromReg
, unsigned ToReg
);
1872 bool rewriteHexBranch(MachineInstr
&BrI
, const CellMap
&Inputs
);
1873 bool rewriteHexConstDefs(MachineInstr
&MI
, const CellMap
&Inputs
,
1875 bool rewriteHexConstUses(MachineInstr
&MI
, const CellMap
&Inputs
);
1877 MachineRegisterInfo
*MRI
;
1878 const HexagonInstrInfo
&HII
;
1879 const HexagonRegisterInfo
&HRI
;
1882 class HexagonConstPropagation
: public MachineFunctionPass
{
1886 HexagonConstPropagation() : MachineFunctionPass(ID
) {}
1888 StringRef
getPassName() const override
{
1889 return "Hexagon Constant Propagation";
1892 bool runOnMachineFunction(MachineFunction
&MF
) override
{
1893 const Function
&F
= MF
.getFunction();
1894 if (skipFunction(F
))
1897 HexagonConstEvaluator
HCE(MF
);
1898 return MachineConstPropagator(HCE
).run(MF
);
1902 } // end anonymous namespace
1904 char HexagonConstPropagation::ID
= 0;
1906 INITIALIZE_PASS(HexagonConstPropagation
, "hexagon-constp",
1907 "Hexagon Constant Propagation", false, false)
1909 HexagonConstEvaluator::HexagonConstEvaluator(MachineFunction
&Fn
)
1910 : MachineConstEvaluator(Fn
),
1911 HII(*Fn
.getSubtarget
<HexagonSubtarget
>().getInstrInfo()),
1912 HRI(*Fn
.getSubtarget
<HexagonSubtarget
>().getRegisterInfo()) {
1913 MRI
= &Fn
.getRegInfo();
1916 bool HexagonConstEvaluator::evaluate(const MachineInstr
&MI
,
1917 const CellMap
&Inputs
, CellMap
&Outputs
) {
1920 if (MI
.getNumOperands() == 0 || !MI
.getOperand(0).isReg())
1922 const MachineOperand
&MD
= MI
.getOperand(0);
1926 unsigned Opc
= MI
.getOpcode();
1927 RegisterSubReg
DefR(MD
);
1928 assert(!DefR
.SubReg
);
1929 if (!Register::isVirtualRegister(DefR
.Reg
))
1934 RegisterSubReg
SrcR(MI
.getOperand(1));
1935 bool Eval
= evaluateCOPY(SrcR
, Inputs
, RC
);
1938 Outputs
.update(DefR
.Reg
, RC
);
1941 if (MI
.isRegSequence()) {
1942 unsigned Sub1
= MI
.getOperand(2).getImm();
1943 unsigned Sub2
= MI
.getOperand(4).getImm();
1944 const TargetRegisterClass
&DefRC
= *MRI
->getRegClass(DefR
.Reg
);
1945 unsigned SubLo
= HRI
.getHexagonSubRegIndex(DefRC
, Hexagon::ps_sub_lo
);
1946 unsigned SubHi
= HRI
.getHexagonSubRegIndex(DefRC
, Hexagon::ps_sub_hi
);
1947 if (Sub1
!= SubLo
&& Sub1
!= SubHi
)
1949 if (Sub2
!= SubLo
&& Sub2
!= SubHi
)
1951 assert(Sub1
!= Sub2
);
1952 bool LoIs1
= (Sub1
== SubLo
);
1953 const MachineOperand
&OpLo
= LoIs1
? MI
.getOperand(1) : MI
.getOperand(3);
1954 const MachineOperand
&OpHi
= LoIs1
? MI
.getOperand(3) : MI
.getOperand(1);
1956 RegisterSubReg
SrcRL(OpLo
), SrcRH(OpHi
);
1957 bool Eval
= evaluateHexRSEQ32(SrcRL
, SrcRH
, Inputs
, RC
);
1960 Outputs
.update(DefR
.Reg
, RC
);
1963 if (MI
.isCompare()) {
1964 bool Eval
= evaluateHexCompare(MI
, Inputs
, Outputs
);
1971 case Hexagon::A2_tfrsi
:
1972 case Hexagon::A2_tfrpi
:
1973 case Hexagon::CONST32
:
1974 case Hexagon::CONST64
:
1976 const MachineOperand
&VO
= MI
.getOperand(1);
1977 // The operand of CONST32 can be a blockaddress, e.g.
1978 // %0 = CONST32 <blockaddress(@eat, %l)>
1979 // Do this check for all instructions for safety.
1982 int64_t V
= MI
.getOperand(1).getImm();
1983 unsigned W
= getRegBitWidth(DefR
.Reg
);
1984 if (W
!= 32 && W
!= 64)
1986 IntegerType
*Ty
= (W
== 32) ? Type::getInt32Ty(CX
)
1987 : Type::getInt64Ty(CX
);
1988 const ConstantInt
*CI
= ConstantInt::get(Ty
, V
, true);
1989 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
1991 Outputs
.update(DefR
.Reg
, RC
);
1995 case Hexagon::PS_true
:
1996 case Hexagon::PS_false
:
1998 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
1999 bool NonZero
= (Opc
== Hexagon::PS_true
);
2000 uint32_t P
= NonZero
? ConstantProperties::NonZero
2001 : ConstantProperties::Zero
;
2003 Outputs
.update(DefR
.Reg
, RC
);
2007 case Hexagon::A2_and
:
2008 case Hexagon::A2_andir
:
2009 case Hexagon::A2_andp
:
2010 case Hexagon::A2_or
:
2011 case Hexagon::A2_orir
:
2012 case Hexagon::A2_orp
:
2013 case Hexagon::A2_xor
:
2014 case Hexagon::A2_xorp
:
2016 bool Eval
= evaluateHexLogical(MI
, Inputs
, Outputs
);
2022 case Hexagon::A2_combineii
: // combine(#s8Ext, #s8)
2023 case Hexagon::A4_combineii
: // combine(#s8, #u6Ext)
2025 if (!MI
.getOperand(1).isImm() || !MI
.getOperand(2).isImm())
2027 uint64_t Hi
= MI
.getOperand(1).getImm();
2028 uint64_t Lo
= MI
.getOperand(2).getImm();
2029 uint64_t Res
= (Hi
<< 32) | (Lo
& 0xFFFFFFFF);
2030 IntegerType
*Ty
= Type::getInt64Ty(CX
);
2031 const ConstantInt
*CI
= ConstantInt::get(Ty
, Res
, false);
2032 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2034 Outputs
.update(DefR
.Reg
, RC
);
2038 case Hexagon::S2_setbit_i
:
2040 int64_t B
= MI
.getOperand(2).getImm();
2041 assert(B
>=0 && B
< 32);
2042 APInt
A(32, (1ull << B
), false);
2043 RegisterSubReg
R(MI
.getOperand(1));
2044 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2045 bool Eval
= evaluateORri(R
, A
, Inputs
, RC
);
2048 Outputs
.update(DefR
.Reg
, RC
);
2052 case Hexagon::C2_mux
:
2053 case Hexagon::C2_muxir
:
2054 case Hexagon::C2_muxri
:
2055 case Hexagon::C2_muxii
:
2057 bool Eval
= evaluateHexCondMove(MI
, Inputs
, Outputs
);
2063 case Hexagon::A2_sxtb
:
2064 case Hexagon::A2_sxth
:
2065 case Hexagon::A2_sxtw
:
2066 case Hexagon::A2_zxtb
:
2067 case Hexagon::A2_zxth
:
2069 bool Eval
= evaluateHexExt(MI
, Inputs
, Outputs
);
2075 case Hexagon::S2_ct0
:
2076 case Hexagon::S2_ct0p
:
2077 case Hexagon::S2_ct1
:
2078 case Hexagon::S2_ct1p
:
2080 using namespace Hexagon
;
2082 bool Ones
= (Opc
== S2_ct1
) || (Opc
== S2_ct1p
);
2083 RegisterSubReg
R1(MI
.getOperand(1));
2084 assert(Inputs
.has(R1
.Reg
));
2086 bool Eval
= evaluateCTBr(R1
, !Ones
, Ones
, Inputs
, T
);
2089 // All of these instructions return a 32-bit value. The evaluate
2090 // will generate the same type as the operand, so truncate the
2091 // result if necessary.
2093 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2094 for (unsigned i
= 0; i
< T
.size(); ++i
) {
2095 const Constant
*CI
= T
.Values
[i
];
2096 if (constToInt(CI
, C
) && C
.getBitWidth() > 32)
2097 CI
= intToConst(C
.trunc(32));
2100 Outputs
.update(DefR
.Reg
, RC
);
2104 case Hexagon::S2_cl0
:
2105 case Hexagon::S2_cl0p
:
2106 case Hexagon::S2_cl1
:
2107 case Hexagon::S2_cl1p
:
2108 case Hexagon::S2_clb
:
2109 case Hexagon::S2_clbp
:
2111 using namespace Hexagon
;
2113 bool OnlyZeros
= (Opc
== S2_cl0
) || (Opc
== S2_cl0p
);
2114 bool OnlyOnes
= (Opc
== S2_cl1
) || (Opc
== S2_cl1p
);
2115 RegisterSubReg
R1(MI
.getOperand(1));
2116 assert(Inputs
.has(R1
.Reg
));
2118 bool Eval
= evaluateCLBr(R1
, !OnlyOnes
, !OnlyZeros
, Inputs
, T
);
2121 // All of these instructions return a 32-bit value. The evaluate
2122 // will generate the same type as the operand, so truncate the
2123 // result if necessary.
2125 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2126 for (unsigned i
= 0; i
< T
.size(); ++i
) {
2127 const Constant
*CI
= T
.Values
[i
];
2128 if (constToInt(CI
, C
) && C
.getBitWidth() > 32)
2129 CI
= intToConst(C
.trunc(32));
2132 Outputs
.update(DefR
.Reg
, RC
);
2136 case Hexagon::S4_extract
:
2137 case Hexagon::S4_extractp
:
2138 case Hexagon::S2_extractu
:
2139 case Hexagon::S2_extractup
:
2141 bool Signed
= (Opc
== Hexagon::S4_extract
) ||
2142 (Opc
== Hexagon::S4_extractp
);
2143 RegisterSubReg
R1(MI
.getOperand(1));
2144 unsigned BW
= getRegBitWidth(R1
.Reg
);
2145 unsigned Bits
= MI
.getOperand(2).getImm();
2146 unsigned Offset
= MI
.getOperand(3).getImm();
2147 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2149 APInt
Zero(BW
, 0, false);
2150 RC
.add(intToConst(Zero
));
2153 if (Offset
+Bits
> BW
) {
2154 // If the requested bitfield extends beyond the most significant bit,
2155 // the extra bits are treated as 0s. To emulate this behavior, reduce
2156 // the number of requested bits, and make the extract unsigned.
2160 bool Eval
= evaluateEXTRACTr(R1
, BW
, Bits
, Offset
, Signed
, Inputs
, RC
);
2163 Outputs
.update(DefR
.Reg
, RC
);
2167 case Hexagon::S2_vsplatrb
:
2168 case Hexagon::S2_vsplatrh
:
2172 // vrndwh, vrndwh:sat
2173 // vsathb, vsathub, vsatwuh
2175 // vtrunehb, vtrunohb
2178 bool Eval
= evaluateHexVector1(MI
, Inputs
, Outputs
);
2194 bool HexagonConstEvaluator::evaluate(const RegisterSubReg
&R
,
2195 const LatticeCell
&Input
, LatticeCell
&Result
) {
2200 const TargetRegisterClass
*RC
= MRI
->getRegClass(R
.Reg
);
2201 if (RC
!= &Hexagon::DoubleRegsRegClass
)
2203 if (R
.SubReg
!= Hexagon::isub_lo
&& R
.SubReg
!= Hexagon::isub_hi
)
2206 assert(!Input
.isTop());
2207 if (Input
.isBottom())
2210 using P
= ConstantProperties
;
2212 if (Input
.isProperty()) {
2213 uint32_t Ps
= Input
.properties();
2214 if (Ps
& (P::Zero
|P::NaN
)) {
2215 uint32_t Ns
= (Ps
& (P::Zero
|P::NaN
|P::SignProperties
));
2219 if (R
.SubReg
== Hexagon::isub_hi
) {
2220 uint32_t Ns
= (Ps
& P::SignProperties
);
2227 // The Input cell contains some known values. Pick the word corresponding
2228 // to the subregister.
2230 for (unsigned i
= 0; i
< Input
.size(); ++i
) {
2231 const Constant
*C
= Input
.Values
[i
];
2232 if (!constToInt(C
, A
))
2236 uint64_t U
= A
.getZExtValue();
2237 if (R
.SubReg
== Hexagon::isub_hi
)
2240 uint32_t U32
= Lo_32(U
);
2242 memcpy(&V32
, &U32
, sizeof V32
);
2243 IntegerType
*Ty
= Type::getInt32Ty(CX
);
2244 const ConstantInt
*C32
= ConstantInt::get(Ty
, static_cast<int64_t>(V32
));
2250 bool HexagonConstEvaluator::evaluate(const MachineInstr
&BrI
,
2251 const CellMap
&Inputs
, SetVector
<const MachineBasicBlock
*> &Targets
,
2253 // We need to evaluate one branch at a time. TII::analyzeBranch checks
2254 // all the branches in a basic block at once, so we cannot use it.
2255 unsigned Opc
= BrI
.getOpcode();
2256 bool SimpleBranch
= false;
2257 bool Negated
= false;
2259 case Hexagon::J2_jumpf
:
2260 case Hexagon::J2_jumpfnew
:
2261 case Hexagon::J2_jumpfnewpt
:
2264 case Hexagon::J2_jumpt
:
2265 case Hexagon::J2_jumptnew
:
2266 case Hexagon::J2_jumptnewpt
:
2267 // Simple branch: if([!]Pn) jump ...
2268 // i.e. Op0 = predicate, Op1 = branch target.
2269 SimpleBranch
= true;
2271 case Hexagon::J2_jump
:
2272 Targets
.insert(BrI
.getOperand(0).getMBB());
2277 // If the branch is of unknown type, assume that all successors are
2279 FallsThru
= !BrI
.isUnconditionalBranch();
2284 const MachineOperand
&MD
= BrI
.getOperand(0);
2285 RegisterSubReg
PR(MD
);
2286 // If the condition operand has a subregister, this is not something
2287 // we currently recognize.
2290 assert(Inputs
.has(PR
.Reg
));
2291 const LatticeCell
&PredC
= Inputs
.get(PR
.Reg
);
2292 if (PredC
.isBottom())
2295 uint32_t Props
= PredC
.properties();
2296 bool CTrue
= false, CFalse
= false;
2297 if (Props
& ConstantProperties::Zero
)
2299 else if (Props
& ConstantProperties::NonZero
)
2301 // If the condition is not known to be either, bail out.
2302 if (!CTrue
&& !CFalse
)
2305 const MachineBasicBlock
*BranchTarget
= BrI
.getOperand(1).getMBB();
2308 if ((!Negated
&& CTrue
) || (Negated
&& CFalse
))
2309 Targets
.insert(BranchTarget
);
2310 else if ((!Negated
&& CFalse
) || (Negated
&& CTrue
))
2319 bool HexagonConstEvaluator::rewrite(MachineInstr
&MI
, const CellMap
&Inputs
) {
2321 return rewriteHexBranch(MI
, Inputs
);
2323 unsigned Opc
= MI
.getOpcode();
2327 case Hexagon::A2_tfrsi
:
2328 case Hexagon::A2_tfrpi
:
2329 case Hexagon::CONST32
:
2330 case Hexagon::CONST64
:
2331 case Hexagon::PS_true
:
2332 case Hexagon::PS_false
:
2336 unsigned NumOp
= MI
.getNumOperands();
2340 bool AllDefs
, Changed
;
2341 Changed
= rewriteHexConstDefs(MI
, Inputs
, AllDefs
);
2342 // If not all defs have been rewritten (i.e. the instruction defines
2343 // a register that is not compile-time constant), then try to rewrite
2344 // register operands that are known to be constant with immediates.
2346 Changed
|= rewriteHexConstUses(MI
, Inputs
);
2351 unsigned HexagonConstEvaluator::getRegBitWidth(unsigned Reg
) const {
2352 const TargetRegisterClass
*RC
= MRI
->getRegClass(Reg
);
2353 if (Hexagon::IntRegsRegClass
.hasSubClassEq(RC
))
2355 if (Hexagon::DoubleRegsRegClass
.hasSubClassEq(RC
))
2357 if (Hexagon::PredRegsRegClass
.hasSubClassEq(RC
))
2359 llvm_unreachable("Invalid register");
2363 uint32_t HexagonConstEvaluator::getCmp(unsigned Opc
) {
2365 case Hexagon::C2_cmpeq
:
2366 case Hexagon::C2_cmpeqp
:
2367 case Hexagon::A4_cmpbeq
:
2368 case Hexagon::A4_cmpheq
:
2369 case Hexagon::A4_cmpbeqi
:
2370 case Hexagon::A4_cmpheqi
:
2371 case Hexagon::C2_cmpeqi
:
2372 case Hexagon::J4_cmpeqn1_t_jumpnv_nt
:
2373 case Hexagon::J4_cmpeqn1_t_jumpnv_t
:
2374 case Hexagon::J4_cmpeqi_t_jumpnv_nt
:
2375 case Hexagon::J4_cmpeqi_t_jumpnv_t
:
2376 case Hexagon::J4_cmpeq_t_jumpnv_nt
:
2377 case Hexagon::J4_cmpeq_t_jumpnv_t
:
2378 return Comparison::EQ
;
2380 case Hexagon::C4_cmpneq
:
2381 case Hexagon::C4_cmpneqi
:
2382 case Hexagon::J4_cmpeqn1_f_jumpnv_nt
:
2383 case Hexagon::J4_cmpeqn1_f_jumpnv_t
:
2384 case Hexagon::J4_cmpeqi_f_jumpnv_nt
:
2385 case Hexagon::J4_cmpeqi_f_jumpnv_t
:
2386 case Hexagon::J4_cmpeq_f_jumpnv_nt
:
2387 case Hexagon::J4_cmpeq_f_jumpnv_t
:
2388 return Comparison::NE
;
2390 case Hexagon::C2_cmpgt
:
2391 case Hexagon::C2_cmpgtp
:
2392 case Hexagon::A4_cmpbgt
:
2393 case Hexagon::A4_cmphgt
:
2394 case Hexagon::A4_cmpbgti
:
2395 case Hexagon::A4_cmphgti
:
2396 case Hexagon::C2_cmpgti
:
2397 case Hexagon::J4_cmpgtn1_t_jumpnv_nt
:
2398 case Hexagon::J4_cmpgtn1_t_jumpnv_t
:
2399 case Hexagon::J4_cmpgti_t_jumpnv_nt
:
2400 case Hexagon::J4_cmpgti_t_jumpnv_t
:
2401 case Hexagon::J4_cmpgt_t_jumpnv_nt
:
2402 case Hexagon::J4_cmpgt_t_jumpnv_t
:
2403 return Comparison::GTs
;
2405 case Hexagon::C4_cmplte
:
2406 case Hexagon::C4_cmpltei
:
2407 case Hexagon::J4_cmpgtn1_f_jumpnv_nt
:
2408 case Hexagon::J4_cmpgtn1_f_jumpnv_t
:
2409 case Hexagon::J4_cmpgti_f_jumpnv_nt
:
2410 case Hexagon::J4_cmpgti_f_jumpnv_t
:
2411 case Hexagon::J4_cmpgt_f_jumpnv_nt
:
2412 case Hexagon::J4_cmpgt_f_jumpnv_t
:
2413 return Comparison::LEs
;
2415 case Hexagon::C2_cmpgtu
:
2416 case Hexagon::C2_cmpgtup
:
2417 case Hexagon::A4_cmpbgtu
:
2418 case Hexagon::A4_cmpbgtui
:
2419 case Hexagon::A4_cmphgtu
:
2420 case Hexagon::A4_cmphgtui
:
2421 case Hexagon::C2_cmpgtui
:
2422 case Hexagon::J4_cmpgtui_t_jumpnv_nt
:
2423 case Hexagon::J4_cmpgtui_t_jumpnv_t
:
2424 case Hexagon::J4_cmpgtu_t_jumpnv_nt
:
2425 case Hexagon::J4_cmpgtu_t_jumpnv_t
:
2426 return Comparison::GTu
;
2428 case Hexagon::J4_cmpltu_f_jumpnv_nt
:
2429 case Hexagon::J4_cmpltu_f_jumpnv_t
:
2430 return Comparison::GEu
;
2432 case Hexagon::J4_cmpltu_t_jumpnv_nt
:
2433 case Hexagon::J4_cmpltu_t_jumpnv_t
:
2434 return Comparison::LTu
;
2436 case Hexagon::J4_cmplt_f_jumpnv_nt
:
2437 case Hexagon::J4_cmplt_f_jumpnv_t
:
2438 return Comparison::GEs
;
2440 case Hexagon::C4_cmplteu
:
2441 case Hexagon::C4_cmplteui
:
2442 case Hexagon::J4_cmpgtui_f_jumpnv_nt
:
2443 case Hexagon::J4_cmpgtui_f_jumpnv_t
:
2444 case Hexagon::J4_cmpgtu_f_jumpnv_nt
:
2445 case Hexagon::J4_cmpgtu_f_jumpnv_t
:
2446 return Comparison::LEu
;
2448 case Hexagon::J4_cmplt_t_jumpnv_nt
:
2449 case Hexagon::J4_cmplt_t_jumpnv_t
:
2450 return Comparison::LTs
;
2455 return Comparison::Unk
;
2458 APInt
HexagonConstEvaluator::getCmpImm(unsigned Opc
, unsigned OpX
,
2459 const MachineOperand
&MO
) {
2460 bool Signed
= false;
2462 case Hexagon::A4_cmpbgtui
: // u7
2463 case Hexagon::A4_cmphgtui
: // u7
2465 case Hexagon::A4_cmpheqi
: // s8
2466 case Hexagon::C4_cmpneqi
: // s8
2469 case Hexagon::A4_cmpbeqi
: // u8
2471 case Hexagon::C2_cmpgtui
: // u9
2472 case Hexagon::C4_cmplteui
: // u9
2474 case Hexagon::C2_cmpeqi
: // s10
2475 case Hexagon::C2_cmpgti
: // s10
2476 case Hexagon::C4_cmpltei
: // s10
2479 case Hexagon::J4_cmpeqi_f_jumpnv_nt
: // u5
2480 case Hexagon::J4_cmpeqi_f_jumpnv_t
: // u5
2481 case Hexagon::J4_cmpeqi_t_jumpnv_nt
: // u5
2482 case Hexagon::J4_cmpeqi_t_jumpnv_t
: // u5
2483 case Hexagon::J4_cmpgti_f_jumpnv_nt
: // u5
2484 case Hexagon::J4_cmpgti_f_jumpnv_t
: // u5
2485 case Hexagon::J4_cmpgti_t_jumpnv_nt
: // u5
2486 case Hexagon::J4_cmpgti_t_jumpnv_t
: // u5
2487 case Hexagon::J4_cmpgtui_f_jumpnv_nt
: // u5
2488 case Hexagon::J4_cmpgtui_f_jumpnv_t
: // u5
2489 case Hexagon::J4_cmpgtui_t_jumpnv_nt
: // u5
2490 case Hexagon::J4_cmpgtui_t_jumpnv_t
: // u5
2493 llvm_unreachable("Unhandled instruction");
2497 uint64_t Val
= MO
.getImm();
2498 return APInt(32, Val
, Signed
);
2501 void HexagonConstEvaluator::replaceWithNop(MachineInstr
&MI
) {
2502 MI
.setDesc(HII
.get(Hexagon::A2_nop
));
2503 while (MI
.getNumOperands() > 0)
2504 MI
.RemoveOperand(0);
2507 bool HexagonConstEvaluator::evaluateHexRSEQ32(RegisterSubReg RL
, RegisterSubReg RH
,
2508 const CellMap
&Inputs
, LatticeCell
&Result
) {
2509 assert(Inputs
.has(RL
.Reg
) && Inputs
.has(RH
.Reg
));
2510 LatticeCell LSL
, LSH
;
2511 if (!getCell(RL
, Inputs
, LSL
) || !getCell(RH
, Inputs
, LSH
))
2513 if (LSL
.isProperty() || LSH
.isProperty())
2516 unsigned LN
= LSL
.size(), HN
= LSH
.size();
2517 SmallVector
<APInt
,4> LoVs(LN
), HiVs(HN
);
2518 for (unsigned i
= 0; i
< LN
; ++i
) {
2519 bool Eval
= constToInt(LSL
.Values
[i
], LoVs
[i
]);
2522 assert(LoVs
[i
].getBitWidth() == 32);
2524 for (unsigned i
= 0; i
< HN
; ++i
) {
2525 bool Eval
= constToInt(LSH
.Values
[i
], HiVs
[i
]);
2528 assert(HiVs
[i
].getBitWidth() == 32);
2531 for (unsigned i
= 0; i
< HiVs
.size(); ++i
) {
2532 APInt HV
= HiVs
[i
].zextOrSelf(64) << 32;
2533 for (unsigned j
= 0; j
< LoVs
.size(); ++j
) {
2534 APInt LV
= LoVs
[j
].zextOrSelf(64);
2535 const Constant
*C
= intToConst(HV
| LV
);
2537 if (Result
.isBottom())
2541 return !Result
.isBottom();
2544 bool HexagonConstEvaluator::evaluateHexCompare(const MachineInstr
&MI
,
2545 const CellMap
&Inputs
, CellMap
&Outputs
) {
2546 unsigned Opc
= MI
.getOpcode();
2547 bool Classic
= false;
2549 case Hexagon::C2_cmpeq
:
2550 case Hexagon::C2_cmpeqp
:
2551 case Hexagon::C2_cmpgt
:
2552 case Hexagon::C2_cmpgtp
:
2553 case Hexagon::C2_cmpgtu
:
2554 case Hexagon::C2_cmpgtup
:
2555 case Hexagon::C2_cmpeqi
:
2556 case Hexagon::C2_cmpgti
:
2557 case Hexagon::C2_cmpgtui
:
2558 // Classic compare: Dst0 = CMP Src1, Src2
2562 // Not handling other compare instructions now.
2567 const MachineOperand
&Src1
= MI
.getOperand(1);
2568 const MachineOperand
&Src2
= MI
.getOperand(2);
2571 unsigned Opc
= MI
.getOpcode();
2572 bool Computed
= evaluateHexCompare2(Opc
, Src1
, Src2
, Inputs
, Result
);
2574 // Only create a zero/non-zero cell. At this time there isn't really
2575 // much need for specific values.
2576 RegisterSubReg
DefR(MI
.getOperand(0));
2577 LatticeCell L
= Outputs
.get(DefR
.Reg
);
2578 uint32_t P
= Result
? ConstantProperties::NonZero
2579 : ConstantProperties::Zero
;
2581 Outputs
.update(DefR
.Reg
, L
);
2589 bool HexagonConstEvaluator::evaluateHexCompare2(unsigned Opc
,
2590 const MachineOperand
&Src1
, const MachineOperand
&Src2
,
2591 const CellMap
&Inputs
, bool &Result
) {
2592 uint32_t Cmp
= getCmp(Opc
);
2593 bool Reg1
= Src1
.isReg(), Reg2
= Src2
.isReg();
2594 bool Imm1
= Src1
.isImm(), Imm2
= Src2
.isImm();
2596 RegisterSubReg
R1(Src1
);
2598 RegisterSubReg
R2(Src2
);
2599 return evaluateCMPrr(Cmp
, R1
, R2
, Inputs
, Result
);
2601 APInt A2
= getCmpImm(Opc
, 2, Src2
);
2602 return evaluateCMPri(Cmp
, R1
, A2
, Inputs
, Result
);
2605 APInt A1
= getCmpImm(Opc
, 1, Src1
);
2607 RegisterSubReg
R2(Src2
);
2608 uint32_t NegCmp
= Comparison::negate(Cmp
);
2609 return evaluateCMPri(NegCmp
, R2
, A1
, Inputs
, Result
);
2611 APInt A2
= getCmpImm(Opc
, 2, Src2
);
2612 return evaluateCMPii(Cmp
, A1
, A2
, Result
);
2615 // Unknown kind of comparison.
2619 bool HexagonConstEvaluator::evaluateHexLogical(const MachineInstr
&MI
,
2620 const CellMap
&Inputs
, CellMap
&Outputs
) {
2621 unsigned Opc
= MI
.getOpcode();
2622 if (MI
.getNumOperands() != 3)
2624 const MachineOperand
&Src1
= MI
.getOperand(1);
2625 const MachineOperand
&Src2
= MI
.getOperand(2);
2626 RegisterSubReg
R1(Src1
);
2632 case Hexagon::A2_and
:
2633 case Hexagon::A2_andp
:
2634 Eval
= evaluateANDrr(R1
, RegisterSubReg(Src2
), Inputs
, RC
);
2636 case Hexagon::A2_andir
: {
2639 APInt
A(32, Src2
.getImm(), true);
2640 Eval
= evaluateANDri(R1
, A
, Inputs
, RC
);
2643 case Hexagon::A2_or
:
2644 case Hexagon::A2_orp
:
2645 Eval
= evaluateORrr(R1
, RegisterSubReg(Src2
), Inputs
, RC
);
2647 case Hexagon::A2_orir
: {
2650 APInt
A(32, Src2
.getImm(), true);
2651 Eval
= evaluateORri(R1
, A
, Inputs
, RC
);
2654 case Hexagon::A2_xor
:
2655 case Hexagon::A2_xorp
:
2656 Eval
= evaluateXORrr(R1
, RegisterSubReg(Src2
), Inputs
, RC
);
2660 RegisterSubReg
DefR(MI
.getOperand(0));
2661 Outputs
.update(DefR
.Reg
, RC
);
2666 bool HexagonConstEvaluator::evaluateHexCondMove(const MachineInstr
&MI
,
2667 const CellMap
&Inputs
, CellMap
&Outputs
) {
2668 // Dst0 = Cond1 ? Src2 : Src3
2669 RegisterSubReg
CR(MI
.getOperand(1));
2670 assert(Inputs
.has(CR
.Reg
));
2672 if (!getCell(CR
, Inputs
, LS
))
2674 uint32_t Ps
= LS
.properties();
2676 if (Ps
& ConstantProperties::Zero
)
2678 else if (Ps
& ConstantProperties::NonZero
)
2683 const MachineOperand
&ValOp
= MI
.getOperand(TakeOp
);
2684 RegisterSubReg
DefR(MI
.getOperand(0));
2685 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2687 if (ValOp
.isImm()) {
2688 int64_t V
= ValOp
.getImm();
2689 unsigned W
= getRegBitWidth(DefR
.Reg
);
2690 APInt
A(W
, V
, true);
2691 const Constant
*C
= intToConst(A
);
2693 Outputs
.update(DefR
.Reg
, RC
);
2696 if (ValOp
.isReg()) {
2697 RegisterSubReg
R(ValOp
);
2698 const LatticeCell
&LR
= Inputs
.get(R
.Reg
);
2700 if (!evaluate(R
, LR
, LSR
))
2703 Outputs
.update(DefR
.Reg
, RC
);
2709 bool HexagonConstEvaluator::evaluateHexExt(const MachineInstr
&MI
,
2710 const CellMap
&Inputs
, CellMap
&Outputs
) {
2712 RegisterSubReg
R1(MI
.getOperand(1));
2713 assert(Inputs
.has(R1
.Reg
));
2715 unsigned Opc
= MI
.getOpcode();
2718 case Hexagon::A2_sxtb
:
2719 case Hexagon::A2_zxtb
:
2722 case Hexagon::A2_sxth
:
2723 case Hexagon::A2_zxth
:
2726 case Hexagon::A2_sxtw
:
2730 llvm_unreachable("Unhandled extension opcode");
2733 bool Signed
= false;
2735 case Hexagon::A2_sxtb
:
2736 case Hexagon::A2_sxth
:
2737 case Hexagon::A2_sxtw
:
2742 RegisterSubReg
DefR(MI
.getOperand(0));
2743 unsigned BW
= getRegBitWidth(DefR
.Reg
);
2744 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2745 bool Eval
= Signed
? evaluateSEXTr(R1
, BW
, Bits
, Inputs
, RC
)
2746 : evaluateZEXTr(R1
, BW
, Bits
, Inputs
, RC
);
2749 Outputs
.update(DefR
.Reg
, RC
);
2753 bool HexagonConstEvaluator::evaluateHexVector1(const MachineInstr
&MI
,
2754 const CellMap
&Inputs
, CellMap
&Outputs
) {
2756 RegisterSubReg
DefR(MI
.getOperand(0));
2757 RegisterSubReg
R1(MI
.getOperand(1));
2758 assert(Inputs
.has(R1
.Reg
));
2759 LatticeCell RC
= Outputs
.get(DefR
.Reg
);
2762 unsigned Opc
= MI
.getOpcode();
2764 case Hexagon::S2_vsplatrb
:
2765 // Rd = 4 times Rs:0..7
2766 Eval
= evaluateSplatr(R1
, 8, 4, Inputs
, RC
);
2768 case Hexagon::S2_vsplatrh
:
2769 // Rdd = 4 times Rs:0..15
2770 Eval
= evaluateSplatr(R1
, 16, 4, Inputs
, RC
);
2778 Outputs
.update(DefR
.Reg
, RC
);
2782 bool HexagonConstEvaluator::rewriteHexConstDefs(MachineInstr
&MI
,
2783 const CellMap
&Inputs
, bool &AllDefs
) {
2786 // Some diagnostics.
2787 // LLVM_DEBUG({...}) gets confused with all this code as an argument.
2789 bool Debugging
= DebugFlag
&& isCurrentDebugType(DEBUG_TYPE
);
2791 bool Const
= true, HasUse
= false;
2792 for (const MachineOperand
&MO
: MI
.operands()) {
2793 if (!MO
.isReg() || !MO
.isUse() || MO
.isImplicit())
2795 RegisterSubReg
R(MO
);
2796 if (!Register::isVirtualRegister(R
.Reg
))
2799 // PHIs can legitimately have "top" cells after propagation.
2800 if (!MI
.isPHI() && !Inputs
.has(R
.Reg
)) {
2801 dbgs() << "Top " << printReg(R
.Reg
, &HRI
, R
.SubReg
)
2802 << " in MI: " << MI
;
2805 const LatticeCell
&L
= Inputs
.get(R
.Reg
);
2806 Const
&= L
.isSingle();
2810 if (HasUse
&& Const
) {
2812 dbgs() << "CONST: " << MI
;
2813 for (const MachineOperand
&MO
: MI
.operands()) {
2814 if (!MO
.isReg() || !MO
.isUse() || MO
.isImplicit())
2816 Register R
= MO
.getReg();
2817 dbgs() << printReg(R
, &TRI
) << ": " << Inputs
.get(R
) << "\n";
2824 // Avoid generating TFRIs for register transfers---this will keep the
2825 // coalescing opportunities.
2829 // Collect all virtual register-def operands.
2830 SmallVector
<unsigned,2> DefRegs
;
2831 for (const MachineOperand
&MO
: MI
.operands()) {
2832 if (!MO
.isReg() || !MO
.isDef())
2834 Register R
= MO
.getReg();
2835 if (!Register::isVirtualRegister(R
))
2837 assert(!MO
.getSubReg());
2838 assert(Inputs
.has(R
));
2839 DefRegs
.push_back(R
);
2842 MachineBasicBlock
&B
= *MI
.getParent();
2843 const DebugLoc
&DL
= MI
.getDebugLoc();
2844 unsigned ChangedNum
= 0;
2846 SmallVector
<const MachineInstr
*,4> NewInstrs
;
2849 // For each defined register, if it is a constant, create an instruction
2851 // and replace all uses of the defined register with NewR.
2852 for (unsigned i
= 0, n
= DefRegs
.size(); i
< n
; ++i
) {
2853 unsigned R
= DefRegs
[i
];
2854 const LatticeCell
&L
= Inputs
.get(R
);
2857 const TargetRegisterClass
*RC
= MRI
->getRegClass(R
);
2858 MachineBasicBlock::iterator At
= MI
.getIterator();
2860 if (!L
.isSingle()) {
2861 // If this a zero/non-zero cell, we can fold a definition
2862 // of a predicate register.
2863 using P
= ConstantProperties
;
2865 uint64_t Ps
= L
.properties();
2866 if (!(Ps
& (P::Zero
|P::NonZero
)))
2868 const TargetRegisterClass
*PredRC
= &Hexagon::PredRegsRegClass
;
2871 const MCInstrDesc
*NewD
= (Ps
& P::Zero
) ?
2872 &HII
.get(Hexagon::PS_false
) :
2873 &HII
.get(Hexagon::PS_true
);
2874 Register NewR
= MRI
->createVirtualRegister(PredRC
);
2875 const MachineInstrBuilder
&MIB
= BuildMI(B
, At
, DL
, *NewD
, NewR
);
2878 NewInstrs
.push_back(&*MIB
);
2880 replaceAllRegUsesWith(R
, NewR
);
2882 // This cell has a single value.
2884 if (!constToInt(L
.Value
, A
) || !A
.isSignedIntN(64))
2886 const TargetRegisterClass
*NewRC
;
2887 const MCInstrDesc
*NewD
;
2889 unsigned W
= getRegBitWidth(R
);
2890 int64_t V
= A
.getSExtValue();
2891 assert(W
== 32 || W
== 64);
2893 NewRC
= &Hexagon::IntRegsRegClass
;
2895 NewRC
= &Hexagon::DoubleRegsRegClass
;
2896 Register NewR
= MRI
->createVirtualRegister(NewRC
);
2897 const MachineInstr
*NewMI
;
2900 NewD
= &HII
.get(Hexagon::A2_tfrsi
);
2901 NewMI
= BuildMI(B
, At
, DL
, *NewD
, NewR
)
2904 if (A
.isSignedIntN(8)) {
2905 NewD
= &HII
.get(Hexagon::A2_tfrpi
);
2906 NewMI
= BuildMI(B
, At
, DL
, *NewD
, NewR
)
2909 int32_t Hi
= V
>> 32;
2910 int32_t Lo
= V
& 0xFFFFFFFFLL
;
2911 if (isInt
<8>(Hi
) && isInt
<8>(Lo
)) {
2912 NewD
= &HII
.get(Hexagon::A2_combineii
);
2913 NewMI
= BuildMI(B
, At
, DL
, *NewD
, NewR
)
2917 NewD
= &HII
.get(Hexagon::CONST64
);
2918 NewMI
= BuildMI(B
, At
, DL
, *NewD
, NewR
)
2925 NewInstrs
.push_back(NewMI
);
2927 replaceAllRegUsesWith(R
, NewR
);
2933 if (!NewInstrs
.empty()) {
2934 MachineFunction
&MF
= *MI
.getParent()->getParent();
2935 dbgs() << "In function: " << MF
.getName() << "\n";
2936 dbgs() << "Rewrite: for " << MI
<< " created " << *NewInstrs
[0];
2937 for (unsigned i
= 1; i
< NewInstrs
.size(); ++i
)
2938 dbgs() << " " << *NewInstrs
[i
];
2942 AllDefs
= (ChangedNum
== DefRegs
.size());
2943 return ChangedNum
> 0;
2946 bool HexagonConstEvaluator::rewriteHexConstUses(MachineInstr
&MI
,
2947 const CellMap
&Inputs
) {
2948 bool Changed
= false;
2949 unsigned Opc
= MI
.getOpcode();
2950 MachineBasicBlock
&B
= *MI
.getParent();
2951 const DebugLoc
&DL
= MI
.getDebugLoc();
2952 MachineBasicBlock::iterator At
= MI
.getIterator();
2953 MachineInstr
*NewMI
= nullptr;
2956 case Hexagon::M2_maci
:
2957 // Convert DefR += mpyi(R2, R3)
2958 // to DefR += mpyi(R, #imm),
2959 // or DefR -= mpyi(R, #imm).
2961 RegisterSubReg
DefR(MI
.getOperand(0));
2962 assert(!DefR
.SubReg
);
2963 RegisterSubReg
R2(MI
.getOperand(2));
2964 RegisterSubReg
R3(MI
.getOperand(3));
2965 assert(Inputs
.has(R2
.Reg
) && Inputs
.has(R3
.Reg
));
2966 LatticeCell LS2
, LS3
;
2967 // It is enough to get one of the input cells, since we will only try
2968 // to replace one argument---whichever happens to be a single constant.
2969 bool HasC2
= getCell(R2
, Inputs
, LS2
), HasC3
= getCell(R3
, Inputs
, LS3
);
2970 if (!HasC2
&& !HasC3
)
2972 bool Zero
= ((HasC2
&& (LS2
.properties() & ConstantProperties::Zero
)) ||
2973 (HasC3
&& (LS3
.properties() & ConstantProperties::Zero
)));
2974 // If one of the operands is zero, eliminate the multiplication.
2976 // DefR == R1 (tied operands).
2977 MachineOperand
&Acc
= MI
.getOperand(1);
2978 RegisterSubReg
R1(Acc
);
2979 unsigned NewR
= R1
.Reg
;
2981 // Generate COPY. FIXME: Replace with the register:subregister.
2982 const TargetRegisterClass
*RC
= MRI
->getRegClass(DefR
.Reg
);
2983 NewR
= MRI
->createVirtualRegister(RC
);
2984 NewMI
= BuildMI(B
, At
, DL
, HII
.get(TargetOpcode::COPY
), NewR
)
2985 .addReg(R1
.Reg
, getRegState(Acc
), R1
.SubReg
);
2987 replaceAllRegUsesWith(DefR
.Reg
, NewR
);
2988 MRI
->clearKillFlags(NewR
);
2994 if (!LS3
.isSingle()) {
2995 if (!LS2
.isSingle())
2999 const LatticeCell
&LI
= Swap
? LS2
: LS3
;
3000 const MachineOperand
&OpR2
= Swap
? MI
.getOperand(3)
3002 // LI is single here.
3004 if (!constToInt(LI
.Value
, A
) || !A
.isSignedIntN(8))
3006 int64_t V
= A
.getSExtValue();
3007 const MCInstrDesc
&D
= (V
>= 0) ? HII
.get(Hexagon::M2_macsip
)
3008 : HII
.get(Hexagon::M2_macsin
);
3011 const TargetRegisterClass
*RC
= MRI
->getRegClass(DefR
.Reg
);
3012 Register NewR
= MRI
->createVirtualRegister(RC
);
3013 const MachineOperand
&Src1
= MI
.getOperand(1);
3014 NewMI
= BuildMI(B
, At
, DL
, D
, NewR
)
3015 .addReg(Src1
.getReg(), getRegState(Src1
), Src1
.getSubReg())
3016 .addReg(OpR2
.getReg(), getRegState(OpR2
), OpR2
.getSubReg())
3018 replaceAllRegUsesWith(DefR
.Reg
, NewR
);
3023 case Hexagon::A2_and
:
3025 RegisterSubReg
R1(MI
.getOperand(1));
3026 RegisterSubReg
R2(MI
.getOperand(2));
3027 assert(Inputs
.has(R1
.Reg
) && Inputs
.has(R2
.Reg
));
3028 LatticeCell LS1
, LS2
;
3029 unsigned CopyOf
= 0;
3030 // Check if any of the operands is -1 (i.e. all bits set).
3031 if (getCell(R1
, Inputs
, LS1
) && LS1
.isSingle()) {
3033 if (constToInt(LS1
.Value
, M1
) && !~M1
)
3036 else if (getCell(R2
, Inputs
, LS2
) && LS2
.isSingle()) {
3038 if (constToInt(LS2
.Value
, M1
) && !~M1
)
3043 MachineOperand
&SO
= MI
.getOperand(CopyOf
);
3044 RegisterSubReg
SR(SO
);
3045 RegisterSubReg
DefR(MI
.getOperand(0));
3046 unsigned NewR
= SR
.Reg
;
3048 const TargetRegisterClass
*RC
= MRI
->getRegClass(DefR
.Reg
);
3049 NewR
= MRI
->createVirtualRegister(RC
);
3050 NewMI
= BuildMI(B
, At
, DL
, HII
.get(TargetOpcode::COPY
), NewR
)
3051 .addReg(SR
.Reg
, getRegState(SO
), SR
.SubReg
);
3053 replaceAllRegUsesWith(DefR
.Reg
, NewR
);
3054 MRI
->clearKillFlags(NewR
);
3059 case Hexagon::A2_or
:
3061 RegisterSubReg
R1(MI
.getOperand(1));
3062 RegisterSubReg
R2(MI
.getOperand(2));
3063 assert(Inputs
.has(R1
.Reg
) && Inputs
.has(R2
.Reg
));
3064 LatticeCell LS1
, LS2
;
3065 unsigned CopyOf
= 0;
3067 using P
= ConstantProperties
;
3069 if (getCell(R1
, Inputs
, LS1
) && (LS1
.properties() & P::Zero
))
3071 else if (getCell(R2
, Inputs
, LS2
) && (LS2
.properties() & P::Zero
))
3075 MachineOperand
&SO
= MI
.getOperand(CopyOf
);
3076 RegisterSubReg
SR(SO
);
3077 RegisterSubReg
DefR(MI
.getOperand(0));
3078 unsigned NewR
= SR
.Reg
;
3080 const TargetRegisterClass
*RC
= MRI
->getRegClass(DefR
.Reg
);
3081 NewR
= MRI
->createVirtualRegister(RC
);
3082 NewMI
= BuildMI(B
, At
, DL
, HII
.get(TargetOpcode::COPY
), NewR
)
3083 .addReg(SR
.Reg
, getRegState(SO
), SR
.SubReg
);
3085 replaceAllRegUsesWith(DefR
.Reg
, NewR
);
3086 MRI
->clearKillFlags(NewR
);
3093 // clear all the kill flags of this new instruction.
3094 for (MachineOperand
&MO
: NewMI
->operands())
3095 if (MO
.isReg() && MO
.isUse())
3096 MO
.setIsKill(false);
3101 dbgs() << "Rewrite: for " << MI
;
3103 dbgs() << " created " << *NewMI
;
3105 dbgs() << " modified the instruction itself and created:" << *NewMI
;
3112 void HexagonConstEvaluator::replaceAllRegUsesWith(unsigned FromReg
,
3114 assert(Register::isVirtualRegister(FromReg
));
3115 assert(Register::isVirtualRegister(ToReg
));
3116 for (auto I
= MRI
->use_begin(FromReg
), E
= MRI
->use_end(); I
!= E
;) {
3117 MachineOperand
&O
= *I
;
3123 bool HexagonConstEvaluator::rewriteHexBranch(MachineInstr
&BrI
,
3124 const CellMap
&Inputs
) {
3125 MachineBasicBlock
&B
= *BrI
.getParent();
3126 unsigned NumOp
= BrI
.getNumOperands();
3131 SetVector
<const MachineBasicBlock
*> Targets
;
3132 bool Eval
= evaluate(BrI
, Inputs
, Targets
, FallsThru
);
3133 unsigned NumTargets
= Targets
.size();
3134 if (!Eval
|| NumTargets
> 1 || (NumTargets
== 1 && FallsThru
))
3136 if (BrI
.getOpcode() == Hexagon::J2_jump
)
3139 LLVM_DEBUG(dbgs() << "Rewrite(" << printMBBReference(B
) << "):" << BrI
);
3140 bool Rewritten
= false;
3141 if (NumTargets
> 0) {
3142 assert(!FallsThru
&& "This should have been checked before");
3143 // MIB.addMBB needs non-const pointer.
3144 MachineBasicBlock
*TargetB
= const_cast<MachineBasicBlock
*>(Targets
[0]);
3145 bool Moot
= B
.isLayoutSuccessor(TargetB
);
3147 // If we build a branch here, we must make sure that it won't be
3148 // erased as "non-executable". We can't mark any new instructions
3149 // as executable here, so we need to overwrite the BrI, which we
3150 // know is executable.
3151 const MCInstrDesc
&JD
= HII
.get(Hexagon::J2_jump
);
3152 auto NI
= BuildMI(B
, BrI
.getIterator(), BrI
.getDebugLoc(), JD
)
3155 while (BrI
.getNumOperands() > 0)
3156 BrI
.RemoveOperand(0);
3157 // This ensures that all implicit operands (e.g. implicit-def %r31, etc)
3158 // are present in the rewritten branch.
3159 for (auto &Op
: NI
->operands())
3161 NI
->eraseFromParent();
3166 // Do not erase instructions. A newly created instruction could get
3167 // the same address as an instruction marked as executable during the
3170 replaceWithNop(BrI
);
3174 FunctionPass
*llvm::createHexagonConstPropagationPass() {
3175 return new HexagonConstPropagation();