9 #define DEBUG_TYPE "hcp" 58 class ConstantProperties {
68 NumericProperties = (Zero|NonZero|Finite|Infinity|NaN|SignedZero),
71 SignProperties = (PosOrZero|NegOrZero),
72 Everything = (NumericProperties|SignProperties)
85 struct RegisterSubReg {
88 explicit RegisterSubReg(
unsigned R,
unsigned SR = 0) :
Reg(R),
SubReg(SR) {}
96 bool operator== (
const RegisterSubReg &R)
const {
97 return (Reg == R.Reg) && (SubReg == R.SubReg);
110 enum { Normal, Top, Bottom };
112 static const unsigned MaxCellSize = 4;
116 unsigned IsSpecial:1;
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);
135 unsigned size()
const {
return Size; }
137 LatticeCell(
const LatticeCell &L) {
140 L.IsSpecial ?
sizeof L.Properties : L.Size *
sizeof(
const Constant *);
141 memcpy(Values, L.Values, N);
144 IsSpecial = L.IsSpecial;
147 LatticeCell &operator=(
const LatticeCell &L) {
150 uint32_t N = L.IsSpecial ?
sizeof L.Properties
151 : L.Size *
sizeof(
const Constant *);
152 memcpy(Values, L.Values, N);
155 IsSpecial = L.IsSpecial;
160 bool isSingle()
const {
return size() == 1; }
161 bool isProperty()
const {
return IsSpecial; }
162 bool isTop()
const {
return Kind == Top; }
163 bool isBottom()
const {
return Kind == Bottom; }
166 bool Changed = (
Kind != Bottom);
182 bool convertToProperty();
192 class MachineConstEvaluator;
194 class MachineConstPropagator {
196 MachineConstPropagator(MachineConstEvaluator &
E) : MCE(E) {
217 void clear() { Map.clear(); }
219 bool has(
unsigned R)
const {
223 MapType::const_iterator
F = Map.find(R);
224 return F != Map.end();
227 const LatticeCell &
get(
unsigned R)
const {
230 MapType::const_iterator
F = Map.find(R);
237 void update(
unsigned R,
const LatticeCell &L) {
244 using MapType = std::map<unsigned, LatticeCell>;
250 LatticeCell Top, Bottom;
253 using const_iterator = MapType::const_iterator;
255 const_iterator
begin()
const {
return Map.
begin(); }
256 const_iterator
end()
const {
return Map.
end(); }
265 void visitUsesOf(
unsigned R);
274 MachineConstEvaluator &MCE;
276 using CFGEdge = std::pair<unsigned, unsigned>;
277 using SetOfCFGEdge = std::set<CFGEdge>;
278 using SetOfInstr = std::set<const MachineInstr *>;
279 using QueueOfCFGEdge = std::queue<CFGEdge>;
283 SetOfCFGEdge EdgeExec;
284 SetOfInstr InstrExec;
285 QueueOfCFGEdge FlowQ;
291 class MachineConstEvaluator {
296 virtual ~MachineConstEvaluator() =
default;
313 using CellMap = MachineConstPropagator::CellMap;
314 virtual bool evaluate(
const MachineInstr &
MI,
const CellMap &Inputs,
315 CellMap &Outputs) = 0;
316 virtual bool evaluate(
const RegisterSubReg &R,
const LatticeCell &SrcC,
317 LatticeCell &Result) = 0;
318 virtual bool evaluate(
const MachineInstr &BrI,
const CellMap &Inputs,
320 bool &CanFallThru) = 0;
359 bool getCell(
const RegisterSubReg &R,
const CellMap &Inputs, LatticeCell &RC);
365 bool evaluateCMPrr(
uint32_t Cmp,
const RegisterSubReg &R1,
const RegisterSubReg &
R2,
366 const CellMap &Inputs,
bool &Result);
367 bool evaluateCMPri(
uint32_t Cmp,
const RegisterSubReg &R1,
const APInt &A2,
368 const CellMap &Inputs,
bool &Result);
369 bool evaluateCMPrp(
uint32_t Cmp,
const RegisterSubReg &R1, uint64_t Props2,
370 const CellMap &Inputs,
bool &Result);
378 bool evaluateCOPY(
const RegisterSubReg &R1,
const CellMap &Inputs,
379 LatticeCell &Result);
382 bool evaluateANDrr(
const RegisterSubReg &R1,
const RegisterSubReg &R2,
383 const CellMap &Inputs, LatticeCell &Result);
384 bool evaluateANDri(
const RegisterSubReg &R1,
const APInt &A2,
385 const CellMap &Inputs, LatticeCell &Result);
387 bool evaluateORrr(
const RegisterSubReg &R1,
const RegisterSubReg &R2,
388 const CellMap &Inputs, LatticeCell &Result);
389 bool evaluateORri(
const RegisterSubReg &R1,
const APInt &A2,
390 const CellMap &Inputs, LatticeCell &Result);
392 bool evaluateXORrr(
const RegisterSubReg &R1,
const RegisterSubReg &R2,
393 const CellMap &Inputs, LatticeCell &Result);
394 bool evaluateXORri(
const RegisterSubReg &R1,
const APInt &A2,
395 const CellMap &Inputs, LatticeCell &Result);
399 bool evaluateZEXTr(
const RegisterSubReg &R1,
unsigned Width,
unsigned Bits,
400 const CellMap &Inputs, LatticeCell &Result);
401 bool evaluateZEXTi(
const APInt &A1,
unsigned Width,
unsigned Bits,
403 bool evaluateSEXTr(
const RegisterSubReg &R1,
unsigned Width,
unsigned Bits,
404 const CellMap &Inputs, LatticeCell &Result);
405 bool evaluateSEXTi(
const APInt &A1,
unsigned Width,
unsigned Bits,
409 bool evaluateCLBr(
const RegisterSubReg &R1,
bool Zeros,
bool Ones,
410 const CellMap &Inputs, LatticeCell &Result);
411 bool evaluateCLBi(
const APInt &A1,
bool Zeros,
bool Ones,
APInt &Result);
412 bool evaluateCTBr(
const RegisterSubReg &R1,
bool Zeros,
bool Ones,
413 const CellMap &Inputs, LatticeCell &Result);
414 bool evaluateCTBi(
const APInt &A1,
bool Zeros,
bool Ones,
APInt &Result);
417 bool evaluateEXTRACTr(
const RegisterSubReg &R1,
unsigned Width,
unsigned Bits,
419 LatticeCell &Result);
420 bool evaluateEXTRACTi(
const APInt &A1,
unsigned Bits,
unsigned Offset,
421 bool Signed,
APInt &Result);
423 bool evaluateSplatr(
const RegisterSubReg &R1,
unsigned Bits,
unsigned Count,
424 const CellMap &Inputs, LatticeCell &Result);
425 bool evaluateSplati(
const APInt &A1,
unsigned Bits,
unsigned Count,
432 if (isa<ConstantInt>(C)) {
435 return Zero | PosOrZero | NegOrZero | Finite;
436 uint32_t Props = (NonZero | Finite);
438 return Props | NegOrZero;
439 return Props | PosOrZero;
442 if (isa<ConstantFP>(C)) {
447 return (Props & ~NumericProperties) | (Zero|Finite);
448 Props = (Props & ~NumericProperties) | NonZero;
450 return (Props & ~NumericProperties) | NaN;
453 return (Props & ~NumericProperties) | Infinity;
463 bool LatticeCell::convertToProperty() {
468 uint32_t Everything = ConstantProperties::Everything;
469 uint32_t Ps = !isTop() ? properties()
485 if (Ps & ConstantProperties::Zero)
487 if (Ps & ConstantProperties::NonZero)
489 if (Ps & ConstantProperties::Finite)
491 if (Ps & ConstantProperties::Infinity)
493 if (Ps & ConstantProperties::NaN)
495 if (Ps & ConstantProperties::PosOrZero)
497 if (Ps & ConstantProperties::NegOrZero)
506 }
else if (isTop()) {
509 for (
unsigned i = 0; i <
size(); ++i) {
522 bool LatticeCell::meet(
const LatticeCell &L) {
523 bool Changed =
false;
525 Changed = setBottom();
526 if (isBottom() || L.isTop())
536 return add(L.properties());
537 for (
unsigned i = 0; i < L.size(); ++i) {
558 while (Index <
Size) {
565 if (Index < MaxCellSize) {
573 bool Changed =
false;
577 Changed = convertToProperty();
579 uint32_t NewPs = Ps & ConstantProperties::deduce(LC);
594 bool Changed = convertToProperty();
596 if (Ps == (Ps & Property))
598 Properties = Property & Ps;
604 uint32_t LatticeCell::properties()
const {
607 assert(!isTop() &&
"Should not call this for a top cell");
612 uint32_t Ps = ConstantProperties::deduce(Values[0]);
613 for (
unsigned i = 1; i <
size(); ++i) {
616 Ps &= ConstantProperties::deduce(Values[i]);
625 dbgs() <<
" " <<
printReg(
I.first, &TRI) <<
" -> " <<
I.second <<
'\n';
629 void MachineConstPropagator::visitPHI(
const MachineInstr &PN) {
635 RegisterSubReg DefR(MD);
638 bool Changed =
false;
643 const LatticeCell &
T = Cells.get(DefR.Reg);
644 Changed = !T.isBottom();
645 Cells.update(DefR.Reg, Bottom);
647 visitUsesOf(DefR.Reg);
651 LatticeCell DefC = Cells.get(DefR.Reg);
656 if (!EdgeExec.count(CFGEdge(PBN, MBN))) {
662 RegisterSubReg UseR(SO);
668 if (!Cells.has(UseR.Reg))
672 bool Eval = MCE.evaluate(UseR, Cells.get(UseR.Reg), SrcC);
674 <<
printReg(UseR.Reg, &MCE.TRI, UseR.SubReg) << SrcC
676 Changed |= Eval ? DefC.meet(SrcC)
678 Cells.update(DefR.Reg, DefC);
683 visitUsesOf(DefR.Reg);
686 void MachineConstPropagator::visitNonBranch(
const MachineInstr &
MI) {
690 bool Eval = MCE.evaluate(MI, Cells, Outputs);
693 dbgs() <<
" outputs:";
694 for (
auto &
I : Outputs)
695 dbgs() <<
' ' <<
I.second;
703 if (!MO.isReg() || !MO.isDef())
705 RegisterSubReg DefR(MO);
709 bool Changed =
false;
712 const LatticeCell &
T = Cells.get(DefR.Reg);
713 Changed = !T.isBottom();
714 Cells.update(DefR.Reg, Bottom);
718 if (!Outputs.has(DefR.Reg))
720 LatticeCell RC = Cells.get(DefR.Reg);
721 Changed = RC.meet(Outputs.get(DefR.Reg));
722 Cells.update(DefR.Reg, RC);
725 visitUsesOf(DefR.Reg);
733 void MachineConstPropagator::visitBranchesFrom(
const MachineInstr &BrI) {
740 bool EvalOk =
true, FallsThru =
true;
743 InstrExec.insert(&MI);
749 EvalOk = EvalOk && MCE.evaluate(MI, Cells, Targets, FallsThru);
769 if (Next != MF.
end())
779 LLVM_DEBUG(
dbgs() <<
" failed to evaluate a branch...adding all CFG " 786 unsigned TBN =
TB->getNumber();
789 FlowQ.push(CFGEdge(MBN, TBN));
793 void MachineConstPropagator::visitUsesOf(
unsigned Reg) {
795 << Cells.get(Reg) <<
'\n');
800 if (!InstrExec.count(&MI))
807 visitBranchesFrom(MI);
832 if (!InstrExec.count(&MI))
834 bool Eval = MCE.evaluate(MI, Cells, Targets, DoNext);
844 if (NextI != MB->getParent()->end())
880 FlowQ.push(CFGEdge(EntryNum, EntryNum));
882 while (!FlowQ.empty()) {
883 CFGEdge Edge = FlowQ.front();
887 dbgs() <<
"Picked edge " 890 if (Edge.first != EntryNum)
891 if (EdgeExec.count(Edge))
893 EdgeExec.insert(Edge);
903 while (It != End && It->isPHI()) {
904 InstrExec.insert(&*It);
912 while (It != End && It->isDebugInstr())
914 assert(It == End || !It->isPHI());
916 if (It != End && InstrExec.count(&*It))
920 while (It != End && !It->isBranch()) {
921 if (!It->isDebugInstr()) {
922 InstrExec.insert(&*It);
933 visitBranchesFrom(*It);
939 FlowQ.push(CFGEdge(SBN, SSB->getNumber()));
944 dbgs() <<
"Cells after propagation:\n";
945 Cells.print(
dbgs(), MCE.TRI);
946 dbgs() <<
"Dead CFG edges:\n";
948 unsigned BN =
B.getNumber();
950 unsigned SN = SB->getNumber();
951 if (!EdgeExec.count(CFGEdge(BN, SN)))
960 bool Changed =
false;
977 std::vector<MachineBasicBlock*> POT;
991 bool HaveTargets = computeBlockSuccessors(
B, Targets);
994 for (
auto I =
B->rbegin(),
E =
B->rend();
I !=
E; ++
I) {
996 if (InstrExec.count(&MI)) {
999 Changed |= MCE.rewrite(MI, Cells);
1005 for (
auto I =
B->begin(),
E =
B->end();
I !=
E; ++
I) {
1025 if (!Targets.
count(SB))
1026 ToRemove.
push_back(const_cast<MachineBasicBlock*>(SB));
1029 for (
unsigned i = 0, n = ToRemove.
size(); i < n; ++i)
1030 removeCFGEdge(
B, ToRemove[i]);
1046 auto Next = std::next(I);
1047 if (I->isBranch() && !InstrExec.count(&*I))
1071 dbgs() <<
"End of MachineConstPropagator (Changed=" << Changed <<
")\n";
1081 bool MachineConstEvaluator::getCell(
const RegisterSubReg &R,
const CellMap &Inputs,
1085 const LatticeCell &L = Inputs.get(R.Reg);
1088 return !RC.isBottom();
1090 bool Eval = evaluate(R, L, RC);
1091 return Eval && !RC.isBottom();
1094 bool MachineConstEvaluator::constToInt(
const Constant *C,
1103 const ConstantInt *MachineConstEvaluator::intToConst(
const APInt &Val)
const {
1107 bool MachineConstEvaluator::evaluateCMPrr(
uint32_t Cmp,
const RegisterSubReg &R1,
1108 const RegisterSubReg &
R2,
const CellMap &Inputs,
bool &Result) {
1109 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1110 LatticeCell LS1, LS2;
1111 if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2))
1114 bool IsProp1 = LS1.isProperty();
1115 bool IsProp2 = LS2.isProperty();
1119 return evaluateCMPpp(Cmp, Prop1, LS2.properties(), Result);
1120 uint32_t NegCmp = Comparison::negate(Cmp);
1121 return evaluateCMPrp(NegCmp, R2, Prop1, Inputs, Result);
1125 return evaluateCMPrp(Cmp, R1, Prop2, Inputs, Result);
1129 bool IsTrue =
true, IsFalse =
true;
1130 for (
unsigned i = 0; i < LS2.size(); ++i) {
1132 bool Computed = constToInt(LS2.Values[i], A) &&
1133 evaluateCMPri(Cmp, R1, A, Inputs, Res);
1139 assert(!IsTrue || !IsFalse);
1143 return IsTrue || IsFalse;
1146 bool MachineConstEvaluator::evaluateCMPri(
uint32_t Cmp,
const RegisterSubReg &R1,
1147 const APInt &A2,
const CellMap &Inputs,
bool &Result) {
1148 assert(Inputs.has(R1.Reg));
1150 if (!getCell(R1, Inputs, LS))
1152 if (LS.isProperty())
1153 return evaluateCMPpi(Cmp, LS.properties(), A2, Result);
1156 bool IsTrue =
true, IsFalse =
true;
1157 for (
unsigned i = 0; i < LS.size(); ++i) {
1159 bool Computed = constToInt(LS.Values[i], A) &&
1160 evaluateCMPii(Cmp, A, A2, Res);
1166 assert(!IsTrue || !IsFalse);
1170 return IsTrue || IsFalse;
1173 bool MachineConstEvaluator::evaluateCMPrp(
uint32_t Cmp,
const RegisterSubReg &R1,
1174 uint64_t Props2,
const CellMap &Inputs,
bool &Result) {
1175 assert(Inputs.has(R1.Reg));
1177 if (!getCell(R1, Inputs, LS))
1179 if (LS.isProperty())
1180 return evaluateCMPpp(Cmp, LS.properties(), Props2, Result);
1183 uint32_t NegCmp = Comparison::negate(Cmp);
1184 bool IsTrue =
true, IsFalse =
true;
1185 for (
unsigned i = 0; i < LS.size(); ++i) {
1187 bool Computed = constToInt(LS.Values[i], A) &&
1188 evaluateCMPpi(NegCmp, Props2, A, Res);
1194 assert(!IsTrue || !IsFalse);
1196 return IsTrue || IsFalse;
1199 bool MachineConstEvaluator::evaluateCMPii(
uint32_t Cmp,
const APInt &A1,
1200 const APInt &A2,
bool &Result) {
1212 return (Result =
true);
1219 unsigned MaxW = (W1 >= W2) ? W1 : W2;
1220 if (Cmp & Comparison::U) {
1223 if (Cmp & Comparison::L)
1224 Result = Zx1.
ult(Zx2);
1226 Result = Zx2.
ult(Zx1);
1233 if (Cmp & Comparison::L)
1234 Result = Sx1.
slt(Sx2);
1236 Result = Sx2.
slt(Sx1);
1241 const APInt &A2,
bool &Result) {
1246 if (Props & ConstantProperties::NaN)
1251 if (!(Props & ConstantProperties::Finite))
1256 if (Cmp & Comparison::U) {
1260 if (Props & ConstantProperties::Zero)
1262 else if (Props & ConstantProperties::NonZero)
1269 if (Props & ConstantProperties::Zero) {
1277 if (Props & ConstantProperties::Zero) {
1282 ((Cmp & Comparison::L) && !A2.
isNegative()) ||
1286 if (Props & ConstantProperties::PosOrZero) {
1294 if (Props & ConstantProperties::NegOrZero) {
1308 using P = ConstantProperties;
1310 if ((Props1 & P::NaN) && (Props2 & P::NaN))
1312 if (!(Props1 & P::Finite) || !(Props2 & P::Finite))
1315 bool Zero1 = (Props1 & P::Zero), Zero2 = (Props2 & P::Zero);
1316 bool NonZero1 = (Props1 & P::NonZero), NonZero2 = (Props2 & P::NonZero);
1317 if (Zero1 && Zero2) {
1322 if ((Zero1 && NonZero2) || (NonZero1 && Zero2))
1323 return (Result =
true);
1327 if (Cmp & Comparison::U) {
1330 if (Zero1 && NonZero2) {
1331 Result = (Cmp & Comparison::L);
1334 if (NonZero1 && Zero2) {
1342 bool Poz1 = (Props1 & P::PosOrZero), Poz2 = (Props2 & P::PosOrZero);
1343 bool Nez1 = (Props1 & P::NegOrZero), Nez2 = (Props2 & P::NegOrZero);
1345 if (NonZero1 || NonZero2) {
1346 Result = (Cmp & Comparison::L);
1351 return (Result =
true);
1354 if (NonZero1 || NonZero2) {
1360 return (Result =
true);
1366 bool MachineConstEvaluator::evaluateCOPY(
const RegisterSubReg &R1,
1367 const CellMap &Inputs, LatticeCell &Result) {
1368 return getCell(R1, Inputs, Result);
1371 bool MachineConstEvaluator::evaluateANDrr(
const RegisterSubReg &R1,
1372 const RegisterSubReg &R2,
const CellMap &Inputs, LatticeCell &Result) {
1373 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1374 const LatticeCell &L1 = Inputs.get(R2.Reg);
1375 const LatticeCell &L2 = Inputs.get(R2.Reg);
1379 if (L2.isBottom()) {
1382 return evaluateANDrr(R2, R1, Inputs, Result);
1385 if (!evaluate(R2, L2, LS2))
1387 if (LS2.isBottom() || LS2.isProperty())
1391 for (
unsigned i = 0; i < LS2.size(); ++i) {
1393 bool Eval = constToInt(LS2.Values[i], A) &&
1394 evaluateANDri(R1, A, Inputs, RC);
1399 return !Result.isBottom();
1402 bool MachineConstEvaluator::evaluateANDri(
const RegisterSubReg &R1,
1403 const APInt &A2,
const CellMap &Inputs, LatticeCell &Result) {
1404 assert(Inputs.has(R1.Reg));
1406 return getCell(R1, Inputs, Result);
1409 RC.add(intToConst(A2));
1415 if (!getCell(R1, Inputs, LS1))
1417 if (LS1.isBottom() || LS1.isProperty())
1421 for (
unsigned i = 0; i < LS1.size(); ++i) {
1422 bool Eval = constToInt(LS1.Values[i], A) &&
1423 evaluateANDii(A, A2, ResA);
1426 const Constant *C = intToConst(ResA);
1429 return !Result.isBottom();
1432 bool MachineConstEvaluator::evaluateANDii(
const APInt &A1,
1438 bool MachineConstEvaluator::evaluateORrr(
const RegisterSubReg &R1,
1439 const RegisterSubReg &R2,
const CellMap &Inputs, LatticeCell &Result) {
1440 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1441 const LatticeCell &L1 = Inputs.get(R2.Reg);
1442 const LatticeCell &L2 = Inputs.get(R2.Reg);
1446 if (L2.isBottom()) {
1449 return evaluateORrr(R2, R1, Inputs, Result);
1452 if (!evaluate(R2, L2, LS2))
1454 if (LS2.isBottom() || LS2.isProperty())
1458 for (
unsigned i = 0; i < LS2.size(); ++i) {
1460 bool Eval = constToInt(LS2.Values[i], A) &&
1461 evaluateORri(R1, A, Inputs, RC);
1466 return !Result.isBottom();
1469 bool MachineConstEvaluator::evaluateORri(
const RegisterSubReg &R1,
1470 const APInt &A2,
const CellMap &Inputs, LatticeCell &Result) {
1471 assert(Inputs.has(R1.Reg));
1473 return getCell(R1, Inputs, Result);
1476 RC.add(intToConst(A2));
1482 if (!getCell(R1, Inputs, LS1))
1484 if (LS1.isBottom() || LS1.isProperty())
1488 for (
unsigned i = 0; i < LS1.size(); ++i) {
1489 bool Eval = constToInt(LS1.Values[i], A) &&
1490 evaluateORii(A, A2, ResA);
1493 const Constant *C = intToConst(ResA);
1496 return !Result.isBottom();
1499 bool MachineConstEvaluator::evaluateORii(
const APInt &A1,
1505 bool MachineConstEvaluator::evaluateXORrr(
const RegisterSubReg &R1,
1506 const RegisterSubReg &R2,
const CellMap &Inputs, LatticeCell &Result) {
1507 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
1508 LatticeCell LS1, LS2;
1509 if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2))
1511 if (LS1.isProperty()) {
1512 if (LS1.properties() & ConstantProperties::Zero)
1513 return !(Result = LS2).isBottom();
1516 if (LS2.isProperty()) {
1517 if (LS2.properties() & ConstantProperties::Zero)
1518 return !(Result = LS1).isBottom();
1523 for (
unsigned i = 0; i < LS2.size(); ++i) {
1525 bool Eval = constToInt(LS2.Values[i], A) &&
1526 evaluateXORri(R1, A, Inputs, RC);
1531 return !Result.isBottom();
1534 bool MachineConstEvaluator::evaluateXORri(
const RegisterSubReg &R1,
1535 const APInt &A2,
const CellMap &Inputs, LatticeCell &Result) {
1536 assert(Inputs.has(R1.Reg));
1538 if (!getCell(R1, Inputs, LS1))
1540 if (LS1.isProperty()) {
1541 if (LS1.properties() & ConstantProperties::Zero) {
1542 const Constant *C = intToConst(A2);
1544 return !Result.isBottom();
1550 for (
unsigned i = 0; i < LS1.size(); ++i) {
1551 bool Eval = constToInt(LS1.Values[i], A) &&
1552 evaluateXORii(A, A2, XA);
1555 const Constant *C = intToConst(XA);
1558 return !Result.isBottom();
1561 bool MachineConstEvaluator::evaluateXORii(
const APInt &A1,
1567 bool MachineConstEvaluator::evaluateZEXTr(
const RegisterSubReg &R1,
unsigned Width,
1568 unsigned Bits,
const CellMap &Inputs, LatticeCell &Result) {
1569 assert(Inputs.has(R1.Reg));
1571 if (!getCell(R1, Inputs, LS1))
1573 if (LS1.isProperty())
1577 for (
unsigned i = 0; i < LS1.size(); ++i) {
1578 bool Eval = constToInt(LS1.Values[i], A) &&
1579 evaluateZEXTi(A, Width, Bits, XA);
1582 const Constant *C = intToConst(XA);
1588 bool MachineConstEvaluator::evaluateZEXTi(
const APInt &A1,
unsigned Width,
1589 unsigned Bits,
APInt &Result) {
1592 assert(Width >= Bits && BW >= Bits);
1598 bool MachineConstEvaluator::evaluateSEXTr(
const RegisterSubReg &R1,
unsigned Width,
1599 unsigned Bits,
const CellMap &Inputs, LatticeCell &Result) {
1600 assert(Inputs.has(R1.Reg));
1602 if (!getCell(R1, Inputs, LS1))
1604 if (LS1.isBottom() || LS1.isProperty())
1608 for (
unsigned i = 0; i < LS1.size(); ++i) {
1609 bool Eval = constToInt(LS1.Values[i], A) &&
1610 evaluateSEXTi(A, Width, Bits, XA);
1613 const Constant *C = intToConst(XA);
1619 bool MachineConstEvaluator::evaluateSEXTi(
const APInt &A1,
unsigned Width,
1620 unsigned Bits,
APInt &Result) {
1622 assert(Width >= Bits && BW >= Bits);
1627 Result =
APInt(Width, 0);
1631 if (BW <= 64 && Bits != 0) {
1635 V =
static_cast<int8_t
>(V);
1638 V =
static_cast<int16_t
>(V);
1641 V =
static_cast<int32_t
>(V);
1652 Result =
APInt(Width, V,
true);
1659 Result = A1.
sext(Width);
1663 bool MachineConstEvaluator::evaluateCLBr(
const RegisterSubReg &R1,
bool Zeros,
1664 bool Ones,
const CellMap &Inputs, LatticeCell &Result) {
1665 assert(Inputs.has(R1.Reg));
1667 if (!getCell(R1, Inputs, LS1))
1669 if (LS1.isBottom() || LS1.isProperty())
1673 for (
unsigned i = 0; i < LS1.size(); ++i) {
1674 bool Eval = constToInt(LS1.Values[i], A) &&
1675 evaluateCLBi(A, Zeros, Ones, CA);
1678 const Constant *C = intToConst(CA);
1684 bool MachineConstEvaluator::evaluateCLBi(
const APInt &A1,
bool Zeros,
1685 bool Ones,
APInt &Result) {
1687 if (!Zeros && !Ones)
1690 if (Zeros && (Count == 0))
1692 if (Ones && (Count == 0))
1694 Result =
APInt(BW, static_cast<uint64_t>(Count),
false);
1698 bool MachineConstEvaluator::evaluateCTBr(
const RegisterSubReg &R1,
bool Zeros,
1699 bool Ones,
const CellMap &Inputs, LatticeCell &Result) {
1700 assert(Inputs.has(R1.Reg));
1702 if (!getCell(R1, Inputs, LS1))
1704 if (LS1.isBottom() || LS1.isProperty())
1708 for (
unsigned i = 0; i < LS1.size(); ++i) {
1709 bool Eval = constToInt(LS1.Values[i], A) &&
1710 evaluateCTBi(A, Zeros, Ones, CA);
1713 const Constant *C = intToConst(CA);
1719 bool MachineConstEvaluator::evaluateCTBi(
const APInt &A1,
bool Zeros,
1720 bool Ones,
APInt &Result) {
1722 if (!Zeros && !Ones)
1725 if (Zeros && (Count == 0))
1727 if (Ones && (Count == 0))
1729 Result =
APInt(BW, static_cast<uint64_t>(Count),
false);
1733 bool MachineConstEvaluator::evaluateEXTRACTr(
const RegisterSubReg &R1,
1734 unsigned Width,
unsigned Bits,
unsigned Offset,
bool Signed,
1735 const CellMap &Inputs, LatticeCell &Result) {
1736 assert(Inputs.has(R1.Reg));
1737 assert(Bits+Offset <= Width);
1739 if (!getCell(R1, Inputs, LS1))
1743 if (LS1.isProperty()) {
1745 if (Ps & ConstantProperties::Zero) {
1754 for (
unsigned i = 0; i < LS1.size(); ++i) {
1755 bool Eval = constToInt(LS1.Values[i], A) &&
1756 evaluateEXTRACTi(A, Bits, Offset, Signed, CA);
1759 const Constant *C = intToConst(CA);
1765 bool MachineConstEvaluator::evaluateEXTRACTi(
const APInt &A1,
unsigned Bits,
1766 unsigned Offset,
bool Signed,
APInt &Result) {
1768 assert(Bits+Offset <= BW);
1771 Result =
APInt(BW, 0);
1780 V =
static_cast<uint64_t
>(V) >> (64-Bits);
1781 Result =
APInt(BW, V, Signed);
1785 Result = A1.
shl(BW-Bits-Offset).
ashr(BW-Bits);
1787 Result = A1.
shl(BW-Bits-Offset).
lshr(BW-Bits);
1791 bool MachineConstEvaluator::evaluateSplatr(
const RegisterSubReg &R1,
1792 unsigned Bits,
unsigned Count,
const CellMap &Inputs,
1793 LatticeCell &Result) {
1794 assert(Inputs.has(R1.Reg));
1796 if (!getCell(R1, Inputs, LS1))
1798 if (LS1.isBottom() || LS1.isProperty())
1802 for (
unsigned i = 0; i < LS1.size(); ++i) {
1803 bool Eval = constToInt(LS1.Values[i], A) &&
1804 evaluateSplati(A, Bits, Count, SA);
1807 const Constant *C = intToConst(SA);
1813 bool MachineConstEvaluator::evaluateSplati(
const APInt &A1,
unsigned Bits,
1814 unsigned Count,
APInt &Result) {
1819 LoBits = LoBits.
zext(SW);
1821 APInt Res(SW, 0,
false);
1822 for (
unsigned i = 0; i < Count; ++i) {
1842 class HexagonConstEvaluator :
public MachineConstEvaluator {
1846 bool evaluate(
const MachineInstr &MI,
const CellMap &Inputs,
1847 CellMap &Outputs)
override;
1848 bool evaluate(
const RegisterSubReg &R,
const LatticeCell &SrcC,
1849 LatticeCell &Result)
override;
1850 bool evaluate(
const MachineInstr &BrI,
const CellMap &Inputs,
1859 static APInt getCmpImm(
unsigned Opc,
unsigned OpX,
1863 bool evaluateHexRSEQ32(RegisterSubReg RL, RegisterSubReg RH,
const CellMap &Inputs,
1864 LatticeCell &Result);
1865 bool evaluateHexCompare(
const MachineInstr &MI,
const CellMap &Inputs,
1869 const MachineOperand &Src2,
const CellMap &Inputs,
bool &Result);
1870 bool evaluateHexLogical(
const MachineInstr &MI,
const CellMap &Inputs,
1872 bool evaluateHexCondMove(
const MachineInstr &MI,
const CellMap &Inputs,
1874 bool evaluateHexExt(
const MachineInstr &MI,
const CellMap &Inputs,
1876 bool evaluateHexVector1(
const MachineInstr &MI,
const CellMap &Inputs,
1878 bool evaluateHexVector2(
const MachineInstr &MI,
const CellMap &Inputs,
1881 void replaceAllRegUsesWith(
unsigned FromReg,
unsigned ToReg);
1882 bool rewriteHexBranch(
MachineInstr &BrI,
const CellMap &Inputs);
1883 bool rewriteHexConstDefs(
MachineInstr &MI,
const CellMap &Inputs,
1885 bool rewriteHexConstUses(
MachineInstr &MI,
const CellMap &Inputs);
1898 StringRef getPassName()
const override {
1899 return "Hexagon Constant Propagation";
1904 if (skipFunction(F))
1907 HexagonConstEvaluator HCE(MF);
1908 return MachineConstPropagator(HCE).run(MF);
1917 "Hexagon Constant Propagation",
false,
false)
1920 : MachineConstEvaluator(Fn),
1923 MRI = &Fn.getRegInfo();
1926 bool HexagonConstEvaluator::evaluate(
const MachineInstr &MI,
1927 const CellMap &Inputs, CellMap &Outputs) {
1937 RegisterSubReg DefR(MD);
1945 bool Eval = evaluateCOPY(SrcR, Inputs, RC);
1948 Outputs.update(DefR.Reg, RC);
1957 if (Sub1 != SubLo && Sub1 != SubHi)
1959 if (Sub2 != SubLo && Sub2 != SubHi)
1962 bool LoIs1 = (Sub1 == SubLo);
1966 RegisterSubReg SrcRL(OpLo), SrcRH(OpHi);
1967 bool Eval = evaluateHexRSEQ32(SrcRL, SrcRH, Inputs, RC);
1970 Outputs.update(DefR.Reg, RC);
1974 bool Eval = evaluateHexCompare(MI, Inputs, Outputs);
1981 case Hexagon::A2_tfrsi:
1982 case Hexagon::A2_tfrpi:
1984 case Hexagon::CONST64:
1994 if (W != 32 && W != 64)
1999 LatticeCell RC = Outputs.get(DefR.Reg);
2001 Outputs.update(DefR.Reg, RC);
2005 case Hexagon::PS_true:
2006 case Hexagon::PS_false:
2008 LatticeCell RC = Outputs.get(DefR.Reg);
2009 bool NonZero = (Opc == Hexagon::PS_true);
2010 uint32_t P = NonZero ? ConstantProperties::NonZero
2011 : ConstantProperties::Zero;
2013 Outputs.update(DefR.Reg, RC);
2017 case Hexagon::A2_and:
2018 case Hexagon::A2_andir:
2019 case Hexagon::A2_andp:
2020 case Hexagon::A2_or:
2021 case Hexagon::A2_orir:
2022 case Hexagon::A2_orp:
2023 case Hexagon::A2_xor:
2024 case Hexagon::A2_xorp:
2026 bool Eval = evaluateHexLogical(MI, Inputs, Outputs);
2032 case Hexagon::A2_combineii:
2033 case Hexagon::A4_combineii:
2039 uint64_t Res = (Hi << 32) | (Lo & 0xFFFFFFFF);
2042 LatticeCell RC = Outputs.get(DefR.Reg);
2044 Outputs.update(DefR.Reg, RC);
2048 case Hexagon::S2_setbit_i:
2052 APInt A(32, (1ull << B),
false);
2054 LatticeCell RC = Outputs.get(DefR.Reg);
2055 bool Eval = evaluateORri(R, A, Inputs, RC);
2058 Outputs.update(DefR.Reg, RC);
2062 case Hexagon::C2_mux:
2063 case Hexagon::C2_muxir:
2064 case Hexagon::C2_muxri:
2065 case Hexagon::C2_muxii:
2067 bool Eval = evaluateHexCondMove(MI, Inputs, Outputs);
2073 case Hexagon::A2_sxtb:
2074 case Hexagon::A2_sxth:
2075 case Hexagon::A2_sxtw:
2076 case Hexagon::A2_zxtb:
2077 case Hexagon::A2_zxth:
2079 bool Eval = evaluateHexExt(MI, Inputs, Outputs);
2085 case Hexagon::S2_ct0:
2086 case Hexagon::S2_ct0p:
2087 case Hexagon::S2_ct1:
2088 case Hexagon::S2_ct1p:
2090 using namespace Hexagon;
2092 bool Ones = (Opc == S2_ct1) || (Opc == S2_ct1p);
2094 assert(Inputs.has(R1.Reg));
2096 bool Eval = evaluateCTBr(R1, !Ones, Ones, Inputs, T);
2103 LatticeCell RC = Outputs.get(DefR.Reg);
2104 for (
unsigned i = 0; i < T.size(); ++i) {
2107 CI = intToConst(C.
trunc(32));
2110 Outputs.update(DefR.Reg, RC);
2114 case Hexagon::S2_cl0:
2115 case Hexagon::S2_cl0p:
2116 case Hexagon::S2_cl1:
2117 case Hexagon::S2_cl1p:
2118 case Hexagon::S2_clb:
2119 case Hexagon::S2_clbp:
2121 using namespace Hexagon;
2123 bool OnlyZeros = (Opc == S2_cl0) || (Opc == S2_cl0p);
2124 bool OnlyOnes = (Opc == S2_cl1) || (Opc == S2_cl1p);
2126 assert(Inputs.has(R1.Reg));
2128 bool Eval = evaluateCLBr(R1, !OnlyOnes, !OnlyZeros, Inputs, T);
2135 LatticeCell RC = Outputs.get(DefR.Reg);
2136 for (
unsigned i = 0; i < T.size(); ++i) {
2139 CI = intToConst(C.
trunc(32));
2142 Outputs.update(DefR.Reg, RC);
2146 case Hexagon::S4_extract:
2147 case Hexagon::S4_extractp:
2148 case Hexagon::S2_extractu:
2149 case Hexagon::S2_extractup:
2151 bool Signed = (Opc == Hexagon::S4_extract) ||
2152 (Opc == Hexagon::S4_extractp);
2157 LatticeCell RC = Outputs.get(DefR.Reg);
2159 APInt Zero(BW, 0,
false);
2160 RC.add(intToConst(Zero));
2163 if (Offset+Bits > BW) {
2170 bool Eval = evaluateEXTRACTr(R1, BW, Bits, Offset, Signed, Inputs, RC);
2173 Outputs.update(DefR.Reg, RC);
2177 case Hexagon::S2_vsplatrb:
2178 case Hexagon::S2_vsplatrh:
2188 bool Eval = evaluateHexVector1(MI, Inputs, Outputs);
2204 bool HexagonConstEvaluator::evaluate(
const RegisterSubReg &R,
2205 const LatticeCell &Input, LatticeCell &Result) {
2211 if (RC != &Hexagon::DoubleRegsRegClass)
2213 if (R.SubReg != Hexagon::isub_lo && R.SubReg != Hexagon::isub_hi)
2217 if (Input.isBottom())
2220 using P = ConstantProperties;
2222 if (Input.isProperty()) {
2224 if (Ps & (P::Zero|P::NaN)) {
2225 uint32_t Ns = (Ps & (P::Zero|P::NaN|P::SignProperties));
2229 if (R.SubReg == Hexagon::isub_hi) {
2230 uint32_t Ns = (Ps & P::SignProperties);
2240 for (
unsigned i = 0; i < Input.size(); ++i) {
2241 const Constant *C = Input.Values[i];
2242 if (!constToInt(C, A))
2247 if (R.SubReg == Hexagon::isub_hi)
2252 memcpy(&V32, &U32,
sizeof V32);
2260 bool HexagonConstEvaluator::evaluate(
const MachineInstr &BrI,
2266 bool SimpleBranch =
false;
2267 bool Negated =
false;
2269 case Hexagon::J2_jumpf:
2270 case Hexagon::J2_jumpfnew:
2271 case Hexagon::J2_jumpfnewpt:
2274 case Hexagon::J2_jumpt:
2275 case Hexagon::J2_jumptnew:
2276 case Hexagon::J2_jumptnewpt:
2279 SimpleBranch =
true;
2281 case Hexagon::J2_jump:
2295 RegisterSubReg PR(MD);
2300 assert(Inputs.has(PR.Reg));
2301 const LatticeCell &PredC = Inputs.get(PR.Reg);
2302 if (PredC.isBottom())
2305 uint32_t Props = PredC.properties();
2306 bool CTrue =
false, CFalse =
false;
2307 if (Props & ConstantProperties::Zero)
2309 else if (Props & ConstantProperties::NonZero)
2312 if (!CTrue && !CFalse)
2318 if ((!Negated && CTrue) || (Negated && CFalse))
2319 Targets.
insert(BranchTarget);
2320 else if ((!Negated && CFalse) || (Negated && CTrue))
2331 return rewriteHexBranch(MI, Inputs);
2337 case Hexagon::A2_tfrsi:
2338 case Hexagon::A2_tfrpi:
2340 case Hexagon::CONST64:
2341 case Hexagon::PS_true:
2342 case Hexagon::PS_false:
2350 bool AllDefs, Changed;
2351 Changed = rewriteHexConstDefs(MI, Inputs, AllDefs);
2356 Changed |= rewriteHexConstUses(MI, Inputs);
2363 if (Hexagon::IntRegsRegClass.hasSubClassEq(RC))
2365 if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC))
2367 if (Hexagon::PredRegsRegClass.hasSubClassEq(RC))
2375 case Hexagon::C2_cmpeq:
2376 case Hexagon::C2_cmpeqp:
2377 case Hexagon::A4_cmpbeq:
2378 case Hexagon::A4_cmpheq:
2379 case Hexagon::A4_cmpbeqi:
2380 case Hexagon::A4_cmpheqi:
2381 case Hexagon::C2_cmpeqi:
2382 case Hexagon::J4_cmpeqn1_t_jumpnv_nt:
2383 case Hexagon::J4_cmpeqn1_t_jumpnv_t:
2384 case Hexagon::J4_cmpeqi_t_jumpnv_nt:
2385 case Hexagon::J4_cmpeqi_t_jumpnv_t:
2386 case Hexagon::J4_cmpeq_t_jumpnv_nt:
2387 case Hexagon::J4_cmpeq_t_jumpnv_t:
2390 case Hexagon::C4_cmpneq:
2391 case Hexagon::C4_cmpneqi:
2392 case Hexagon::J4_cmpeqn1_f_jumpnv_nt:
2393 case Hexagon::J4_cmpeqn1_f_jumpnv_t:
2394 case Hexagon::J4_cmpeqi_f_jumpnv_nt:
2395 case Hexagon::J4_cmpeqi_f_jumpnv_t:
2396 case Hexagon::J4_cmpeq_f_jumpnv_nt:
2397 case Hexagon::J4_cmpeq_f_jumpnv_t:
2400 case Hexagon::C2_cmpgt:
2401 case Hexagon::C2_cmpgtp:
2402 case Hexagon::A4_cmpbgt:
2403 case Hexagon::A4_cmphgt:
2404 case Hexagon::A4_cmpbgti:
2405 case Hexagon::A4_cmphgti:
2406 case Hexagon::C2_cmpgti:
2407 case Hexagon::J4_cmpgtn1_t_jumpnv_nt:
2408 case Hexagon::J4_cmpgtn1_t_jumpnv_t:
2409 case Hexagon::J4_cmpgti_t_jumpnv_nt:
2410 case Hexagon::J4_cmpgti_t_jumpnv_t:
2411 case Hexagon::J4_cmpgt_t_jumpnv_nt:
2412 case Hexagon::J4_cmpgt_t_jumpnv_t:
2413 return Comparison::GTs;
2415 case Hexagon::C4_cmplte:
2416 case Hexagon::C4_cmpltei:
2417 case Hexagon::J4_cmpgtn1_f_jumpnv_nt:
2418 case Hexagon::J4_cmpgtn1_f_jumpnv_t:
2419 case Hexagon::J4_cmpgti_f_jumpnv_nt:
2420 case Hexagon::J4_cmpgti_f_jumpnv_t:
2421 case Hexagon::J4_cmpgt_f_jumpnv_nt:
2422 case Hexagon::J4_cmpgt_f_jumpnv_t:
2423 return Comparison::LEs;
2425 case Hexagon::C2_cmpgtu:
2426 case Hexagon::C2_cmpgtup:
2427 case Hexagon::A4_cmpbgtu:
2428 case Hexagon::A4_cmpbgtui:
2429 case Hexagon::A4_cmphgtu:
2430 case Hexagon::A4_cmphgtui:
2431 case Hexagon::C2_cmpgtui:
2432 case Hexagon::J4_cmpgtui_t_jumpnv_nt:
2433 case Hexagon::J4_cmpgtui_t_jumpnv_t:
2434 case Hexagon::J4_cmpgtu_t_jumpnv_nt:
2435 case Hexagon::J4_cmpgtu_t_jumpnv_t:
2436 return Comparison::GTu;
2438 case Hexagon::J4_cmpltu_f_jumpnv_nt:
2439 case Hexagon::J4_cmpltu_f_jumpnv_t:
2440 return Comparison::GEu;
2442 case Hexagon::J4_cmpltu_t_jumpnv_nt:
2443 case Hexagon::J4_cmpltu_t_jumpnv_t:
2444 return Comparison::LTu;
2446 case Hexagon::J4_cmplt_f_jumpnv_nt:
2447 case Hexagon::J4_cmplt_f_jumpnv_t:
2448 return Comparison::GEs;
2450 case Hexagon::C4_cmplteu:
2451 case Hexagon::C4_cmplteui:
2452 case Hexagon::J4_cmpgtui_f_jumpnv_nt:
2453 case Hexagon::J4_cmpgtui_f_jumpnv_t:
2454 case Hexagon::J4_cmpgtu_f_jumpnv_nt:
2455 case Hexagon::J4_cmpgtu_f_jumpnv_t:
2456 return Comparison::LEu;
2458 case Hexagon::J4_cmplt_t_jumpnv_nt:
2459 case Hexagon::J4_cmplt_t_jumpnv_t:
2460 return Comparison::LTs;
2465 return Comparison::Unk;
2468 APInt HexagonConstEvaluator::getCmpImm(
unsigned Opc,
unsigned OpX,
2470 bool Signed =
false;
2472 case Hexagon::A4_cmpbgtui:
2473 case Hexagon::A4_cmphgtui:
2475 case Hexagon::A4_cmpheqi:
2476 case Hexagon::C4_cmpneqi:
2479 case Hexagon::A4_cmpbeqi:
2481 case Hexagon::C2_cmpgtui:
2482 case Hexagon::C4_cmplteui:
2484 case Hexagon::C2_cmpeqi:
2485 case Hexagon::C2_cmpgti:
2486 case Hexagon::C4_cmpltei:
2489 case Hexagon::J4_cmpeqi_f_jumpnv_nt:
2490 case Hexagon::J4_cmpeqi_f_jumpnv_t:
2491 case Hexagon::J4_cmpeqi_t_jumpnv_nt:
2492 case Hexagon::J4_cmpeqi_t_jumpnv_t:
2493 case Hexagon::J4_cmpgti_f_jumpnv_nt:
2494 case Hexagon::J4_cmpgti_f_jumpnv_t:
2495 case Hexagon::J4_cmpgti_t_jumpnv_nt:
2496 case Hexagon::J4_cmpgti_t_jumpnv_t:
2497 case Hexagon::J4_cmpgtui_f_jumpnv_nt:
2498 case Hexagon::J4_cmpgtui_f_jumpnv_t:
2499 case Hexagon::J4_cmpgtui_t_jumpnv_nt:
2500 case Hexagon::J4_cmpgtui_t_jumpnv_t:
2507 uint64_t Val = MO.
getImm();
2508 return APInt(32, Val, Signed);
2511 void HexagonConstEvaluator::replaceWithNop(
MachineInstr &MI) {
2512 MI.
setDesc(HII.get(Hexagon::A2_nop));
2517 bool HexagonConstEvaluator::evaluateHexRSEQ32(RegisterSubReg RL, RegisterSubReg RH,
2518 const CellMap &Inputs, LatticeCell &Result) {
2519 assert(Inputs.has(RL.Reg) && Inputs.has(RH.Reg));
2520 LatticeCell
LSL, LSH;
2521 if (!getCell(RL, Inputs, LSL) || !getCell(RH, Inputs, LSH))
2523 if (LSL.isProperty() || LSH.isProperty())
2526 unsigned LN = LSL.size(), HN = LSH.size();
2528 for (
unsigned i = 0; i < LN; ++i) {
2529 bool Eval = constToInt(LSL.Values[i], LoVs[i]);
2534 for (
unsigned i = 0; i < HN; ++i) {
2535 bool Eval = constToInt(LSH.Values[i], HiVs[i]);
2541 for (
unsigned i = 0; i < HiVs.
size(); ++i) {
2542 APInt HV = HiVs[i].zextOrSelf(64) << 32;
2543 for (
unsigned j = 0; j < LoVs.size(); ++j) {
2545 const Constant *C = intToConst(HV | LV);
2547 if (Result.isBottom())
2551 return !Result.isBottom();
2554 bool HexagonConstEvaluator::evaluateHexCompare(
const MachineInstr &MI,
2555 const CellMap &Inputs, CellMap &Outputs) {
2557 bool Classic =
false;
2559 case Hexagon::C2_cmpeq:
2560 case Hexagon::C2_cmpeqp:
2561 case Hexagon::C2_cmpgt:
2562 case Hexagon::C2_cmpgtp:
2563 case Hexagon::C2_cmpgtu:
2564 case Hexagon::C2_cmpgtup:
2565 case Hexagon::C2_cmpeqi:
2566 case Hexagon::C2_cmpgti:
2567 case Hexagon::C2_cmpgtui:
2582 bool Computed = evaluateHexCompare2(Opc, Src1, Src2, Inputs, Result);
2587 LatticeCell L = Outputs.get(DefR.Reg);
2588 uint32_t P = Result ? ConstantProperties::NonZero
2589 : ConstantProperties::Zero;
2591 Outputs.update(DefR.Reg, L);
2599 bool HexagonConstEvaluator::evaluateHexCompare2(
unsigned Opc,
2601 const CellMap &Inputs,
bool &Result) {
2603 bool Reg1 = Src1.
isReg(), Reg2 = Src2.
isReg();
2604 bool Imm1 = Src1.
isImm(), Imm2 = Src2.
isImm();
2606 RegisterSubReg R1(Src1);
2608 RegisterSubReg
R2(Src2);
2609 return evaluateCMPrr(Cmp, R1, R2, Inputs, Result);
2611 APInt A2 = getCmpImm(Opc, 2, Src2);
2612 return evaluateCMPri(Cmp, R1, A2, Inputs, Result);
2615 APInt A1 = getCmpImm(Opc, 1, Src1);
2617 RegisterSubReg
R2(Src2);
2618 uint32_t NegCmp = Comparison::negate(Cmp);
2619 return evaluateCMPri(NegCmp, R2, A1, Inputs, Result);
2621 APInt A2 = getCmpImm(Opc, 2, Src2);
2622 return evaluateCMPii(Cmp, A1, A2, Result);
2629 bool HexagonConstEvaluator::evaluateHexLogical(
const MachineInstr &MI,
2630 const CellMap &Inputs, CellMap &Outputs) {
2636 RegisterSubReg R1(Src1);
2642 case Hexagon::A2_and:
2643 case Hexagon::A2_andp:
2644 Eval = evaluateANDrr(R1, RegisterSubReg(Src2), Inputs, RC);
2646 case Hexagon::A2_andir: {
2650 Eval = evaluateANDri(R1, A, Inputs, RC);
2653 case Hexagon::A2_or:
2654 case Hexagon::A2_orp:
2655 Eval = evaluateORrr(R1, RegisterSubReg(Src2), Inputs, RC);
2657 case Hexagon::A2_orir: {
2661 Eval = evaluateORri(R1, A, Inputs, RC);
2664 case Hexagon::A2_xor:
2665 case Hexagon::A2_xorp:
2666 Eval = evaluateXORrr(R1, RegisterSubReg(Src2), Inputs, RC);
2671 Outputs.update(DefR.Reg, RC);
2676 bool HexagonConstEvaluator::evaluateHexCondMove(
const MachineInstr &MI,
2677 const CellMap &Inputs, CellMap &Outputs) {
2680 assert(Inputs.has(CR.Reg));
2682 if (!getCell(CR, Inputs, LS))
2686 if (Ps & ConstantProperties::Zero)
2688 else if (Ps & ConstantProperties::NonZero)
2695 LatticeCell RC = Outputs.get(DefR.Reg);
2697 if (ValOp.
isImm()) {
2698 int64_t V = ValOp.
getImm();
2700 APInt A(W, V,
true);
2703 Outputs.update(DefR.Reg, RC);
2706 if (ValOp.
isReg()) {
2707 RegisterSubReg R(ValOp);
2708 const LatticeCell &LR = Inputs.get(R.Reg);
2710 if (!evaluate(R, LR, LSR))
2713 Outputs.update(DefR.Reg, RC);
2719 bool HexagonConstEvaluator::evaluateHexExt(
const MachineInstr &MI,
2720 const CellMap &Inputs, CellMap &Outputs) {
2723 assert(Inputs.has(R1.Reg));
2728 case Hexagon::A2_sxtb:
2729 case Hexagon::A2_zxtb:
2732 case Hexagon::A2_sxth:
2733 case Hexagon::A2_zxth:
2736 case Hexagon::A2_sxtw:
2743 bool Signed =
false;
2745 case Hexagon::A2_sxtb:
2746 case Hexagon::A2_sxth:
2747 case Hexagon::A2_sxtw:
2754 LatticeCell RC = Outputs.get(DefR.Reg);
2755 bool Eval = Signed ? evaluateSEXTr(R1, BW, Bits, Inputs, RC)
2756 : evaluateZEXTr(R1, BW, Bits, Inputs, RC);
2759 Outputs.update(DefR.Reg, RC);
2763 bool HexagonConstEvaluator::evaluateHexVector1(
const MachineInstr &MI,
2764 const CellMap &Inputs, CellMap &Outputs) {
2768 assert(Inputs.has(R1.Reg));
2769 LatticeCell RC = Outputs.get(DefR.Reg);
2774 case Hexagon::S2_vsplatrb:
2776 Eval = evaluateSplatr(R1, 8, 4, Inputs, RC);
2778 case Hexagon::S2_vsplatrh:
2780 Eval = evaluateSplatr(R1, 16, 4, Inputs, RC);
2788 Outputs.update(DefR.Reg, RC);
2792 bool HexagonConstEvaluator::rewriteHexConstDefs(
MachineInstr &MI,
2793 const CellMap &Inputs,
bool &AllDefs) {
2801 bool Const =
true, HasUse =
false;
2805 RegisterSubReg R(MO);
2810 if (!MI.
isPHI() && !Inputs.has(R.Reg)) {
2812 <<
" in MI: " <<
MI;
2815 const LatticeCell &L = Inputs.get(R.Reg);
2816 Const &= L.isSingle();
2820 if (HasUse && Const) {
2822 dbgs() <<
"CONST: " <<
MI;
2827 dbgs() <<
printReg(R, &TRI) <<
": " << Inputs.get(R) <<
"\n";
2854 unsigned ChangedNum = 0;
2862 for (
unsigned i = 0, n = DefRegs.
size(); i < n; ++i) {
2863 unsigned R = DefRegs[i];
2864 const LatticeCell &L = Inputs.get(R);
2870 if (!L.isSingle()) {
2873 using P = ConstantProperties;
2875 uint64_t Ps = L.properties();
2876 if (!(Ps & (P::Zero|P::NonZero)))
2882 &HII.get(Hexagon::PS_false) :
2883 &HII.get(Hexagon::PS_true);
2884 Register NewR =
MRI->createVirtualRegister(PredRC);
2890 replaceAllRegUsesWith(R, NewR);
2901 assert(W == 32 || W == 64);
2903 NewRC = &Hexagon::IntRegsRegClass;
2905 NewRC = &Hexagon::DoubleRegsRegClass;
2906 Register NewR =
MRI->createVirtualRegister(NewRC);
2910 NewD = &HII.get(Hexagon::A2_tfrsi);
2911 NewMI =
BuildMI(B, At, DL, *NewD, NewR)
2915 NewD = &HII.get(Hexagon::A2_tfrpi);
2916 NewMI =
BuildMI(B, At, DL, *NewD, NewR)
2919 int32_t
Hi = V >> 32;
2920 int32_t
Lo = V & 0xFFFFFFFFLL;
2922 NewD = &HII.get(Hexagon::A2_combineii);
2923 NewMI =
BuildMI(B, At, DL, *NewD, NewR)
2927 NewD = &HII.get(Hexagon::CONST64);
2928 NewMI =
BuildMI(B, At, DL, *NewD, NewR)
2937 replaceAllRegUsesWith(R, NewR);
2943 if (!NewInstrs.
empty()) {
2945 dbgs() <<
"In function: " << MF.
getName() <<
"\n";
2946 dbgs() <<
"Rewrite: for " << MI <<
" created " << *NewInstrs[0];
2947 for (
unsigned i = 1; i < NewInstrs.size(); ++i)
2948 dbgs() <<
" " << *NewInstrs[i];
2952 AllDefs = (ChangedNum == DefRegs.
size());
2953 return ChangedNum > 0;
2956 bool HexagonConstEvaluator::rewriteHexConstUses(
MachineInstr &MI,
2957 const CellMap &Inputs) {
2958 bool Changed =
false;
2966 case Hexagon::M2_maci:
2975 assert(Inputs.has(R2.Reg) && Inputs.has(R3.Reg));
2976 LatticeCell LS2, LS3;
2979 bool HasC2 = getCell(R2, Inputs, LS2), HasC3 = getCell(R3, Inputs, LS3);
2980 if (!HasC2 && !HasC3)
2982 bool Zero = ((HasC2 && (LS2.properties() & ConstantProperties::Zero)) ||
2983 (HasC3 && (LS3.properties() & ConstantProperties::Zero)));
2988 RegisterSubReg R1(Acc);
2989 unsigned NewR = R1.Reg;
2993 NewR =
MRI->createVirtualRegister(RC);
2994 NewMI =
BuildMI(B, At, DL, HII.
get(TargetOpcode::COPY), NewR)
2997 replaceAllRegUsesWith(DefR.Reg, NewR);
2998 MRI->clearKillFlags(NewR);
3004 if (!LS3.isSingle()) {
3005 if (!LS2.isSingle())
3009 const LatticeCell &LI = Swap ? LS2 : LS3;
3017 const MCInstrDesc &
D = (V >= 0) ? HII.get(Hexagon::M2_macsip)
3018 : HII.get(Hexagon::M2_macsin);
3024 NewMI =
BuildMI(B, At, DL, D, NewR)
3028 replaceAllRegUsesWith(DefR.Reg, NewR);
3033 case Hexagon::A2_and:
3037 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3038 LatticeCell LS1, LS2;
3039 unsigned CopyOf = 0;
3041 if (getCell(R1, Inputs, LS1) && LS1.isSingle()) {
3043 if (constToInt(LS1.Value, M1) && !~M1)
3046 else if (getCell(R2, Inputs, LS2) && LS2.isSingle()) {
3048 if (constToInt(LS2.Value, M1) && !~M1)
3054 RegisterSubReg SR(SO);
3056 unsigned NewR = SR.Reg;
3059 NewR =
MRI->createVirtualRegister(RC);
3060 NewMI =
BuildMI(B, At, DL, HII.
get(TargetOpcode::COPY), NewR)
3063 replaceAllRegUsesWith(DefR.Reg, NewR);
3064 MRI->clearKillFlags(NewR);
3069 case Hexagon::A2_or:
3073 assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg));
3074 LatticeCell LS1, LS2;
3075 unsigned CopyOf = 0;
3077 using P = ConstantProperties;
3079 if (getCell(R1, Inputs, LS1) && (LS1.properties() & P::Zero))
3081 else if (getCell(R2, Inputs, LS2) && (LS2.properties() & P::Zero))
3086 RegisterSubReg SR(SO);
3088 unsigned NewR = SR.Reg;
3091 NewR =
MRI->createVirtualRegister(RC);
3092 NewMI =
BuildMI(B, At, DL, HII.
get(TargetOpcode::COPY), NewR)
3095 replaceAllRegUsesWith(DefR.Reg, NewR);
3096 MRI->clearKillFlags(NewR);
3111 dbgs() <<
"Rewrite: for " <<
MI;
3113 dbgs() <<
" created " << *NewMI;
3115 dbgs() <<
" modified the instruction itself and created:" << *NewMI;
3122 void HexagonConstEvaluator::replaceAllRegUsesWith(
unsigned FromReg,
3126 for (
auto I =
MRI->use_begin(FromReg),
E =
MRI->use_end();
I !=
E;) {
3133 bool HexagonConstEvaluator::rewriteHexBranch(
MachineInstr &BrI,
3134 const CellMap &Inputs) {
3142 bool Eval = evaluate(BrI, Inputs, Targets, FallsThru);
3143 unsigned NumTargets = Targets.
size();
3144 if (!Eval || NumTargets > 1 || (NumTargets == 1 && FallsThru))
3146 if (BrI.
getOpcode() == Hexagon::J2_jump)
3150 bool Rewritten =
false;
3151 if (NumTargets > 0) {
3152 assert(!FallsThru &&
"This should have been checked before");
3161 const MCInstrDesc &JD = HII.get(Hexagon::J2_jump);
3169 for (
auto &
Op : NI->operands())
3171 NI->eraseFromParent();
3180 replaceWithNop(BrI);
3185 return new HexagonConstPropagation();
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
const_iterator end(StringRef path)
Get end iterator over path.
DILocation * get() const
Get the underlying DILocation.
uint64_t getZExtValue() const
Get zero extended value.
APInt sext(unsigned width) const
Sign extend to a new width.
bool isCall(QueryType Type=AnyInBundle) const
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
MachineBasicBlock * getMBB() const
unsigned getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
This class represents lattice values for constants.
size_type size() const
Determine the number of elements in the SetVector.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
APInt zext(unsigned width) const
Zero extend to a new width.
bool slt(const APInt &RHS) const
Signed less than comparison.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Get a value with low bits set.
unsigned getSubReg() const
unsigned getRegBitWidth(unsigned RCID)
Get the size in bits of a register from the register class RC.
bool isRegSequence() const
constexpr bool isInt< 8 >(int64_t x)
APInt trunc(unsigned width) const
Truncate to new width.
unsigned const TargetRegisterInfo * TRI
static IntegerType * getInt64Ty(LLVMContext &C)
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
iterator_range< mop_iterator > operands()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
unsigned getBitWidth() const
Return the number of bits in the APInt.
iterator_range< succ_iterator > successors()
unsigned countTrailingZeros() const
Count the number of trailing zero bits.
void initializeHexagonConstPropagationPass(PassRegistry &Registry)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
APInt shl(unsigned shiftAmt) const
Left-shift function.
unsigned getNumOperands() const
Retuns the total number of operands.
APInt zextOrSelf(unsigned width) const
Zero extend or truncate to width.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
friend const_iterator begin(StringRef path, Style style)
Get begin iterator over path.
bool remove(const value_type &X)
Remove an item from the set vector.
This file implements a class to represent arbitrary precision integral constant values and operations...
int64_t getSExtValue() const
Get sign extended value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
bool isNegative() const
Return true if the sign bit is set.
const APInt & getValue() const
Return the constant as an APInt value reference.
void setReg(Register Reg)
Change the register this operand corresponds to.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
bool isBranch(QueryType Type=AnyInBundle) const
Returns true if this is a conditional, unconditional, or indirect branch.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
FunctionPass * createHexagonConstPropagationPass()
static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1, ISD::CondCode Cond, const SDLoc &DL, SDValue Chain=SDValue(), bool IsSignaling=false)
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool isNegative() const
Determine sign of this APInt.
friend const_iterator end(StringRef path)
Get end iterator over path.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isCompare(QueryType Type=IgnoreBundle) const
Return true if this instruction is a comparison.
This is an important class for using LLVM in a threaded context.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
ConstantFP - Floating Point Values [float, double].
This file declares a class to represent arbitrary precision floating point values and provide a varie...
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
FunctionPass class - This class is used to implement most global optimizations.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
iterator_range< po_iterator< T > > post_order(const T &G)
self_iterator getIterator()
Class to represent integer types.
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!) to ensure ...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool isDebugInstr() const
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
void setIsKill(bool Val=true)
const APFloat & getValueAPF() const
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Iterator for intrusive lists based on ilist_node.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
This is the shared class of boolean and integer constants.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
BlockVerifier::State From
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned countTrailingOnes() const
Count the number of trailing one bits.
void clear()
Completely clear the SetVector.
Class for arbitrary precision integers.
INITIALIZE_PASS(HexagonConstPropagation, "hexagon-constp", "Hexagon Constant Propagation", false, false) HexagonConstEvaluator
static void clear(coro::Shape &Shape)
bool isUnconditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which always transfers control flow to some other block...
static void propagate(InstantiatedValue From, InstantiatedValue To, MatchState State, ReachabilitySet &ReachSet, std::vector< WorkListItem > &WorkList)
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static IntegerType * getInt32Ty(LLVMContext &C)
LLVM_NODISCARD bool empty() const
bool isZero() const
Return true if the value is positive or negative zero.
bool isNaN() const
Return true if the value is a NaN.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static bool rewrite(Function &F)
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool isSignedIntN(unsigned N) const
Check if this APInt has an N-bits signed integer value.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A vector that has set insertion semantics.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
unsigned countLeadingOnes() const
Count the number of leading one bits.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
unsigned countLeadingZeros() const
The APInt version of the countLeadingZeros functions in MathExtras.h.
bool operator==(uint64_t V1, const APInt &V2)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool DebugFlag
This boolean is set to true if the '-debug' command line option is specified.
APInt sextOrSelf(unsigned width) const
Sign extend or truncate to width.
bool isCurrentDebugType(const char *Type)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
Wrapper class representing virtual and physical registers.