65 #define DEBUG_TYPE "sccp" 67 STATISTIC(NumInstRemoved,
"Number of instructions removed");
68 STATISTIC(NumDeadBlocks ,
"Number of basic blocks unreachable");
70 STATISTIC(IPNumInstRemoved,
"Number of instructions removed by IPSCCP");
71 STATISTIC(IPNumArgsElimed ,
"Number of arguments constant propagated by IPSCCP");
72 STATISTIC(IPNumGlobalConst,
"Number of globals found to be constant by IPSCCP");
102 LatticeValueTy getLatticeValue()
const {
107 LatticeVal() : Val(nullptr, unknown) {}
109 bool isUnknown()
const {
return getLatticeValue() == unknown; }
112 return getLatticeValue() == constant || getLatticeValue() == forcedconstant;
115 bool isOverdefined()
const {
return getLatticeValue() == overdefined; }
123 bool markOverdefined() {
133 if (getLatticeValue() == constant) {
140 assert(V &&
"Marking constant with NULL");
143 assert(getLatticeValue() == forcedconstant &&
144 "Cannot move from overdefined to constant!");
172 void markForcedConstant(
Constant *V) {
173 assert(isUnknown() &&
"Can't force a defined value!");
174 Val.
setInt(forcedconstant);
192 class SCCPSolver :
public InstVisitor<SCCPSolver> {
247 using Edge = std::pair<BasicBlock *, BasicBlock *>;
255 AnalysisResults.
insert({&
F, std::move(A)});
260 if (A == AnalysisResults.
end())
262 return A->second.
PredInfo->getPredicateInfoFor(I);
266 auto A = AnalysisResults.
find(&F);
267 assert(A != AnalysisResults.
end() &&
"Need analysis results for function.");
272 : DL(DL), TLI(tli) {}
279 if (!BBExecutable.
insert(BB).second)
293 LatticeVal &IV = TrackedGlobals[GV];
302 void AddTrackedFunction(
Function *F) {
305 MRVFunctionsTracked.
insert(F);
306 for (
unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
307 TrackedMultipleRetVals.
insert(std::make_pair(std::make_pair(F, i),
310 TrackedRetVals.
insert(std::make_pair(F, LatticeVal()));
315 void AddMustTailCallee(
Function *F) {
316 MustTailCallees.
insert(F);
321 bool isMustTailCallee(
Function *F) {
322 return MustTailCallees.
count(F);
325 void AddArgumentTrackedFunction(
Function *F) {
326 TrackingIncomingArguments.
insert(F);
331 bool isArgumentTrackedFunction(
Function *F) {
332 return TrackingIncomingArguments.
count(F);
345 bool isBlockExecutable(
BasicBlock *BB)
const {
346 return BBExecutable.
count(BB);
353 std::vector<LatticeVal> getStructLatticeValueFor(
Value *V)
const {
354 std::vector<LatticeVal> StructValues;
356 assert(STy &&
"getStructLatticeValueFor() can be called only on structs");
357 for (
unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
358 auto I = StructValueState.
find(std::make_pair(V, i));
359 assert(I != StructValueState.
end() &&
"Value not in valuemap!");
360 StructValues.push_back(I->second);
365 const LatticeVal &getLatticeValueFor(
Value *V)
const {
367 "Should use getStructLatticeValueFor");
370 "V not found in ValueState nor Paramstate map!");
376 return TrackedRetVals;
382 return TrackedGlobals;
388 return MRVFunctionsTracked;
394 return MustTailCallees;
399 void markOverdefined(
Value *V) {
400 if (
auto *STy = dyn_cast<StructType>(V->
getType()))
401 for (
unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
402 markOverdefined(getStructValueState(V, i), V);
404 markOverdefined(ValueState[V], V);
412 const auto &It = TrackedMultipleRetVals.
find(std::make_pair(F, i));
413 assert(It != TrackedMultipleRetVals.
end());
414 LatticeVal LV = It->second;
415 if (LV.isOverdefined())
423 void pushToWorkList(LatticeVal &IV,
Value *V) {
424 if (IV.isOverdefined())
425 return OverdefinedInstWorkList.
push_back(V);
433 if (!IV.markConstant(C))
return false;
434 LLVM_DEBUG(
dbgs() <<
"markConstant: " << *C <<
": " << *V <<
'\n');
435 pushToWorkList(IV, V);
441 return markConstant(ValueState[V], V, C);
446 LatticeVal &IV = ValueState[V];
447 IV.markForcedConstant(C);
448 LLVM_DEBUG(
dbgs() <<
"markForcedConstant: " << *C <<
": " << *V <<
'\n');
449 pushToWorkList(IV, V);
455 bool markOverdefined(LatticeVal &IV,
Value *V) {
456 if (!IV.markOverdefined())
return false;
459 if (
auto *F = dyn_cast<Function>(V))
dbgs()
460 <<
"Function '" << F->
getName() <<
"'\n";
461 else dbgs() << *V <<
'\n');
463 pushToWorkList(IV, V);
467 bool mergeInValue(LatticeVal &IV,
Value *V, LatticeVal MergeWithV) {
468 if (IV.isOverdefined() || MergeWithV.isUnknown())
470 if (MergeWithV.isOverdefined())
471 return markOverdefined(IV, V);
473 return markConstant(IV, V, MergeWithV.getConstant());
474 if (IV.getConstant() != MergeWithV.getConstant())
475 return markOverdefined(IV, V);
479 bool mergeInValue(
Value *V, LatticeVal MergeWithV) {
481 "non-structs should use markConstant");
482 return mergeInValue(ValueState[V], V, MergeWithV);
488 LatticeVal &getValueState(
Value *V) {
491 std::pair<DenseMap<Value*, LatticeVal>::iterator,
bool> I =
492 ValueState.
insert(std::make_pair(V, LatticeVal()));
493 LatticeVal &LV = I.first->second;
498 if (
auto *C = dyn_cast<Constant>(V)) {
500 if (!isa<UndefValue>(V))
511 std::pair<DenseMap<Value*, ValueLatticeElement>::iterator,
bool>
515 LV = getValueState(V).toValueLattice();
523 LatticeVal &getStructValueState(
Value *V,
unsigned i) {
525 assert(i < cast<StructType>(V->
getType())->getNumElements() &&
526 "Invalid element #");
528 std::pair<DenseMap<std::pair<Value*, unsigned>, LatticeVal>::iterator,
529 bool> I = StructValueState.
insert(
530 std::make_pair(std::make_pair(V, i), LatticeVal()));
531 LatticeVal &LV = I.first->second;
536 if (
auto *C = dyn_cast<Constant>(V)) {
540 LV.markOverdefined();
541 else if (isa<UndefValue>(Elt))
544 LV.markConstant(Elt);
554 if (!KnownFeasibleEdges.
insert(Edge(Source, Dest)).second)
557 if (!MarkBlockExecutable(Dest)) {
562 <<
" -> " << Dest->
getName() <<
'\n');
583 void addAdditionalUser(
Value *V,
User *U) {
584 auto Iter = AdditionalUsers.
insert({V, {}});
585 Iter.first->second.insert(U);
589 void markUsersAsChanged(
Value *I) {
591 if (
auto *UI = dyn_cast<Instruction>(U))
592 OperandChangedState(UI);
594 auto Iter = AdditionalUsers.
find(I);
595 if (Iter != AdditionalUsers.
end()) {
596 for (
User *U : Iter->second)
597 if (
auto *UI = dyn_cast<Instruction>(U))
598 OperandChangedState(UI);
623 markOverdefined(&CPI);
624 visitTerminator(CPI);
650 LLVM_DEBUG(
dbgs() <<
"SCCP: Don't know how to handle: " << I <<
'\n');
659 void SCCPSolver::getFeasibleSuccessors(
Instruction &TI,
662 if (
auto *BI = dyn_cast<BranchInst>(&TI)) {
663 if (BI->isUnconditional()) {
668 LatticeVal BCValue = getValueState(BI->getCondition());
673 if (!BCValue.isUnknown())
674 Succs[0] = Succs[1] =
true;
679 Succs[CI->isZero()] =
true;
689 if (
auto *
SI = dyn_cast<SwitchInst>(&TI)) {
690 if (!
SI->getNumCases()) {
694 LatticeVal SCValue = getValueState(
SI->getCondition());
699 if (!SCValue.isUnknown())
704 Succs[
SI->findCaseValue(CI)->getSuccessorIndex()] =
true;
710 if (
auto *IBR = dyn_cast<IndirectBrInst>(&TI)) {
712 LatticeVal IBRValue = getValueState(IBR->getAddress());
716 if (!IBRValue.isUnknown())
723 "Block address of a different function ?");
724 for (
unsigned i = 0; i < IBR->getNumSuccessors(); ++i) {
726 if (IBR->getDestination(i) ==
T) {
737 LLVM_DEBUG(
dbgs() <<
"Unknown terminator instruction: " << TI <<
'\n');
747 return KnownFeasibleEdges.
count(Edge(From, To));
767 void SCCPSolver::visitPHINode(
PHINode &PN) {
771 return (
void)markOverdefined(&PN);
773 if (getValueState(&PN).isOverdefined())
779 return (
void)markOverdefined(&PN);
789 if (IV.isUnknown())
continue;
794 if (IV.isOverdefined())
795 return (
void)markOverdefined(&PN);
798 OperandVal = IV.getConstant();
808 if (IV.getConstant() != OperandVal)
809 return (
void)markOverdefined(&PN);
817 markConstant(&PN, OperandVal);
820 void SCCPSolver::visitReturnInst(
ReturnInst &I) {
829 TrackedRetVals.
find(F);
830 if (TFRVI != TrackedRetVals.
end()) {
831 mergeInValue(TFRVI->second, F, getValueState(ResultOp));
837 if (!TrackedMultipleRetVals.
empty()) {
838 if (
auto *STy = dyn_cast<StructType>(ResultOp->
getType()))
839 if (MRVFunctionsTracked.
count(F))
841 mergeInValue(TrackedMultipleRetVals[std::make_pair(F, i)],
F,
842 getStructValueState(ResultOp, i));
846 void SCCPSolver::visitTerminator(
Instruction &TI) {
848 getFeasibleSuccessors(TI, SuccFeasible);
853 for (
unsigned i = 0, e = SuccFeasible.
size(); i != e; ++i)
858 void SCCPSolver::visitCastInst(
CastInst &I) {
859 LatticeVal OpSt = getValueState(I.
getOperand(0));
860 if (OpSt.isOverdefined())
862 else if (OpSt.isConstant()) {
866 if (isa<UndefValue>(C))
877 return (
void)markOverdefined(&EVI);
881 return (
void)markOverdefined(&EVI);
886 LatticeVal EltVal = getStructValueState(AggVal, i);
887 mergeInValue(getValueState(&EVI), &EVI, EltVal);
890 return (
void)markOverdefined(&EVI);
897 return (
void)markOverdefined(&IVI);
902 return (
void)markOverdefined(&IVI);
911 LatticeVal EltVal = getStructValueState(Aggr, i);
912 mergeInValue(getStructValueState(&IVI, i), &IVI, EltVal);
919 markOverdefined(getStructValueState(&IVI, i), &IVI);
921 LatticeVal InVal = getValueState(Val);
922 mergeInValue(getStructValueState(&IVI, i), &IVI, InVal);
927 void SCCPSolver::visitSelectInst(
SelectInst &I) {
931 return (
void)markOverdefined(&I);
934 if (CondValue.isUnknown())
937 if (
ConstantInt *CondCB = CondValue.getConstantInt()) {
939 mergeInValue(&I, getValueState(OpVal));
950 if (TVal.isConstant() && FVal.isConstant() &&
951 TVal.getConstant() == FVal.getConstant())
952 return (
void)markConstant(&I, FVal.getConstant());
954 if (TVal.isUnknown())
955 return (
void)mergeInValue(&I, FVal);
956 if (FVal.isUnknown())
957 return (
void)mergeInValue(&I, TVal);
962 void SCCPSolver::visitBinaryOperator(
Instruction &I) {
963 LatticeVal V1State = getValueState(I.
getOperand(0));
964 LatticeVal V2State = getValueState(I.
getOperand(1));
966 LatticeVal &IV = ValueState[&
I];
967 if (IV.isOverdefined())
return;
969 if (V1State.isConstant() && V2State.isConstant()) {
971 V2State.getConstant());
973 if (isa<UndefValue>(C))
975 return (
void)markConstant(IV, &I, C);
979 if (!V1State.isOverdefined() && !V2State.isOverdefined())
987 if (V1State.isConstant() && V1State.getConstant()->isNullValue())
988 return (
void)markConstant(IV, &I, V1State.getConstant());
996 LatticeVal *NonOverdefVal =
nullptr;
997 if (!V1State.isOverdefined())
998 NonOverdefVal = &V1State;
999 else if (!V2State.isOverdefined())
1000 NonOverdefVal = &V2State;
1002 if (NonOverdefVal) {
1003 if (NonOverdefVal->isUnknown())
1006 if (I.
getOpcode() == Instruction::And ||
1010 if (NonOverdefVal->getConstant()->isNullValue())
1011 return (
void)markConstant(IV, &I, NonOverdefVal->getConstant());
1014 if (
ConstantInt *CI = NonOverdefVal->getConstantInt())
1015 if (CI->isMinusOne())
1016 return (
void)markConstant(IV, &I, NonOverdefVal->getConstant());
1021 markOverdefined(&I);
1025 void SCCPSolver::visitCmpInst(
CmpInst &I) {
1028 if (ValueState[&I].isOverdefined())
return;
1035 auto V1Param = ParamState.
find(Op1);
1038 : getValueState(Op1).toValueLattice();
1040 auto V2Param = ParamState.
find(Op2);
1043 : getValueState(Op2).toValueLattice();
1047 if (isa<UndefValue>(C))
1051 mergeInValue(&I, CV);
1057 !ValueState[&
I].isConstant())
1060 markOverdefined(&I);
1066 if (ValueState[&I].isOverdefined())
return;
1072 LatticeVal State = getValueState(I.
getOperand(i));
1073 if (State.isUnknown())
1076 if (State.isOverdefined())
1077 return (
void)markOverdefined(&I);
1079 assert(State.isConstant() &&
"Unknown state!");
1080 Operands.
push_back(State.getConstant());
1087 if (isa<UndefValue>(C))
1089 markConstant(&I, C);
1102 if (I == TrackedGlobals.
end() || I->second.isOverdefined())
return;
1105 mergeInValue(I->second, GV, getValueState(SI.
getOperand(0)));
1106 if (I->second.isOverdefined())
1107 TrackedGlobals.
erase(I);
1112 void SCCPSolver::visitLoadInst(
LoadInst &I) {
1115 return (
void)markOverdefined(&I);
1117 LatticeVal PtrVal = getValueState(I.
getOperand(0));
1118 if (PtrVal.isUnknown())
return;
1120 LatticeVal &IV = ValueState[&
I];
1121 if (IV.isOverdefined())
return;
1124 return (
void)markOverdefined(IV, &I);
1126 Constant *Ptr = PtrVal.getConstant();
1129 if (isa<ConstantPointerNull>(Ptr)) {
1131 return (
void)markOverdefined(IV, &I);
1137 if (
auto *GV = dyn_cast<GlobalVariable>(Ptr)) {
1138 if (!TrackedGlobals.
empty()) {
1141 TrackedGlobals.
find(GV);
1142 if (It != TrackedGlobals.
end()) {
1143 mergeInValue(IV, &I, It->second);
1151 if (isa<UndefValue>(C))
1153 return (
void)markConstant(IV, &I, C);
1158 markOverdefined(IV, &I);
1161 void SCCPSolver::visitCallSite(
CallSite CS) {
1165 if (
auto *II = dyn_cast<IntrinsicInst>(I)) {
1167 if (ValueState[I].isOverdefined())
1170 auto *PI = getPredicateInfoFor(I);
1177 mergeInValue(ValueState[I], I, getValueState(CopyOf));
1181 Value *Cond = PBranch->Condition;
1186 mergeInValue(ValueState[I], I, getValueState(CopyOf));
1190 Value *CmpOp0 = Cmp->getOperand(0);
1191 Value *CmpOp1 = Cmp->getOperand(1);
1192 if (CopyOf != CmpOp0 && CopyOf != CmpOp1) {
1193 mergeInValue(ValueState[I], I, getValueState(CopyOf));
1197 if (CmpOp0 != CopyOf)
1200 LatticeVal OriginalVal = getValueState(CopyOf);
1201 LatticeVal EqVal = getValueState(CmpOp1);
1202 LatticeVal &IV = ValueState[
I];
1204 addAdditionalUser(CmpOp1, I);
1205 if (OriginalVal.isConstant())
1206 mergeInValue(IV, I, OriginalVal);
1208 mergeInValue(IV, I, EqVal);
1212 addAdditionalUser(CmpOp1, I);
1213 if (OriginalVal.isConstant())
1214 mergeInValue(IV, I, OriginalVal);
1216 mergeInValue(IV, I, EqVal);
1220 return (
void)mergeInValue(IV, I, getValueState(CopyOf));
1239 if (AI->get()->getType()->isStructTy())
1240 return markOverdefined(I);
1241 LatticeVal State = getValueState(*AI);
1243 if (State.isUnknown())
1245 if (State.isOverdefined())
1246 return (
void)markOverdefined(I);
1247 assert(State.isConstant() &&
"Unknown state!");
1248 Operands.
push_back(State.getConstant());
1251 if (getValueState(I).isOverdefined())
1258 if (isa<UndefValue>(C))
1260 return (
void)markConstant(I, C);
1265 return (
void)markOverdefined(I);
1271 if (!TrackingIncomingArguments.
empty() && TrackingIncomingArguments.
count(F)){
1272 MarkBlockExecutable(&F->
front());
1277 AI !=
E; ++AI, ++CAI) {
1281 markOverdefined(&*AI);
1285 if (
auto *STy = dyn_cast<StructType>(AI->getType())) {
1287 LatticeVal
CallArg = getStructValueState(*CAI, i);
1288 mergeInValue(getStructValueState(&*AI, i), &*AI, CallArg);
1293 LatticeVal ConcreteArgument = getValueState(*CAI);
1295 getParamState(&*AI).mergeIn(ConcreteArgument.toValueLattice(), DL);
1296 bool ValueChanged = mergeInValue(&*AI, ConcreteArgument);
1301 if (!ValueChanged && ParamChanged)
1302 pushToWorkList(ValueState[&*AI], &*AI);
1309 if (!MRVFunctionsTracked.
count(F))
1310 goto CallOverdefined;
1315 mergeInValue(getStructValueState(I, i),
I,
1316 TrackedMultipleRetVals[std::make_pair(F, i)]);
1319 if (TFRVI == TrackedRetVals.
end())
1320 goto CallOverdefined;
1323 mergeInValue(I, TFRVI->second);
1327 void SCCPSolver::Solve() {
1329 while (!BBWorkList.
empty() || !InstWorkList.
empty() ||
1330 !OverdefinedInstWorkList.
empty()) {
1333 while (!OverdefinedInstWorkList.
empty()) {
1345 markUsersAsChanged(I);
1349 while (!InstWorkList.
empty()) {
1362 markUsersAsChanged(I);
1366 while (!BBWorkList.
empty()) {
1397 bool SCCPSolver::ResolvedUndefsIn(
Function &F) {
1399 if (!BBExecutable.
count(&BB))
1406 if (
auto *STy = dyn_cast<StructType>(I.
getType())) {
1412 if (MRVFunctionsTracked.
count(F))
1417 if (isa<ExtractValueInst>(I) || isa<InsertValueInst>(
I))
1423 LatticeVal &LV = getStructValueState(&I, i);
1425 markOverdefined(LV, &I);
1430 LatticeVal &LV = getValueState(&I);
1431 if (!LV.isUnknown())
continue;
1434 if (isa<ExtractValueInst>(I))
1441 markOverdefined(&I);
1444 LatticeVal Op0LV = getValueState(I.getOperand(0));
1446 if (I.getNumOperands() == 2) {
1448 markOverdefined(&I);
1452 Op1LV = getValueState(I.getOperand(1));
1457 switch (I.getOpcode()) {
1459 case Instruction::Sub:
1460 case Instruction::Trunc:
1461 case Instruction::FPTrunc:
1462 case Instruction::BitCast:
1464 case Instruction::FSub:
1465 case Instruction::FAdd:
1466 case Instruction::FMul:
1467 case Instruction::FDiv:
1468 case Instruction::FRem:
1470 if (Op0LV.isUnknown() && Op1LV.isUnknown())
1473 markOverdefined(&I);
1475 case Instruction::ZExt:
1476 case Instruction::SExt:
1477 case Instruction::FPToUI:
1478 case Instruction::FPToSI:
1479 case Instruction::FPExt:
1480 case Instruction::PtrToInt:
1481 case Instruction::IntToPtr:
1482 case Instruction::SIToFP:
1483 case Instruction::UIToFP:
1487 case Instruction::Mul:
1488 case Instruction::And:
1490 if (Op0LV.isUnknown() && Op1LV.isUnknown())
1496 case Instruction::Or:
1498 if (Op0LV.isUnknown() && Op1LV.isUnknown())
1503 case Instruction::Xor:
1507 if (Op0LV.isUnknown() && Op1LV.isUnknown()) {
1513 case Instruction::SDiv:
1514 case Instruction::UDiv:
1515 case Instruction::SRem:
1516 case Instruction::URem:
1519 if (Op1LV.isUnknown())
break;
1523 if (Op1LV.isConstant() && Op1LV.getConstant()->isZeroValue())
1530 case Instruction::AShr:
1532 if (Op1LV.isUnknown())
break;
1535 if (Op1LV.isConstant()) {
1536 if (
auto *ShiftAmt = Op1LV.getConstantInt())
1537 if (ShiftAmt->getLimitedValue() >=
1538 ShiftAmt->getType()->getScalarSizeInBits())
1545 case Instruction::LShr:
1546 case Instruction::Shl:
1549 if (Op1LV.isUnknown())
break;
1552 if (Op1LV.isConstant()) {
1553 if (
auto *ShiftAmt = Op1LV.getConstantInt())
1554 if (ShiftAmt->getLimitedValue() >=
1555 ShiftAmt->getType()->getScalarSizeInBits())
1564 Op1LV = getValueState(I.getOperand(1));
1566 if (Op0LV.isUnknown()) {
1567 if (!Op1LV.isConstant())
1568 Op1LV = getValueState(I.getOperand(2));
1569 }
else if (Op1LV.isUnknown()) {
1571 Op1LV = getValueState(I.getOperand(2));
1572 if (Op1LV.isUnknown())
1579 if (Op1LV.isConstant())
1580 markForcedConstant(&I, Op1LV.getConstant());
1582 markOverdefined(&I);
1589 case Instruction::ICmp:
1591 Op0LV = getValueState(I.getOperand(0));
1592 Op1LV = getValueState(I.getOperand(1));
1594 if ((Op0LV.isUnknown() || Op1LV.isUnknown()) &&
1595 cast<ICmpInst>(&I)->isEquality())
1597 markOverdefined(&I);
1600 case Instruction::Invoke:
1607 if (TrackedRetVals.
count(F))
1612 markOverdefined(&I);
1617 markOverdefined(&I);
1626 if (
auto *BI = dyn_cast<BranchInst>(TI)) {
1627 if (!BI->isConditional())
continue;
1628 if (!getValueState(BI->getCondition()).isUnknown())
1633 if (isa<UndefValue>(BI->getCondition())) {
1644 if (markEdgeExecutable(&BB, DefaultSuccessor))
1650 if (
auto *IBR = dyn_cast<IndirectBrInst>(TI)) {
1653 if (IBR->getNumSuccessors() < 1)
1656 if (!getValueState(IBR->getAddress()).isUnknown())
1661 if (isa<UndefValue>(IBR->getAddress())) {
1663 markEdgeExecutable(&BB, IBR->getSuccessor(0));
1672 BasicBlock *DefaultSuccessor = IBR->getSuccessor(0);
1673 if (markEdgeExecutable(&BB, DefaultSuccessor))
1679 if (
auto *SI = dyn_cast<SwitchInst>(TI)) {
1680 if (!SI->getNumCases() || !getValueState(SI->getCondition()).isUnknown())
1685 if (isa<UndefValue>(SI->getCondition())) {
1686 SI->setCondition(SI->case_begin()->getCaseValue());
1687 markEdgeExecutable(&BB, SI->case_begin()->getCaseSuccessor());
1695 BasicBlock *DefaultSuccessor = SI->case_begin()->getCaseSuccessor();
1696 if (markEdgeExecutable(&BB, DefaultSuccessor))
1709 std::vector<LatticeVal> IVs = Solver.getStructLatticeValueFor(V);
1711 [](
const LatticeVal &LV) {
return LV.isOverdefined(); }))
1713 std::vector<Constant *> ConstVals;
1715 for (
unsigned i = 0, e =
ST->getNumElements(); i != e; ++i) {
1716 LatticeVal V = IVs[i];
1717 ConstVals.push_back(V.isConstant()
1723 const LatticeVal &IV = Solver.getLatticeValueFor(V);
1724 if (IV.isOverdefined())
1729 assert(Const &&
"Constant is nullptr here!");
1740 Solver.AddMustTailCallee(F);
1742 LLVM_DEBUG(
dbgs() <<
" Can\'t treat the result of musttail call : " << *CI
1743 <<
" as a constant\n");
1747 LLVM_DEBUG(
dbgs() <<
" Constant: " << *Const <<
" = " << *V <<
'\n');
1759 SCCPSolver Solver(DL, TLI);
1762 Solver.MarkBlockExecutable(&F.
front());
1766 Solver.markOverdefined(&AI);
1769 bool ResolvedUndefs =
true;
1770 while (ResolvedUndefs) {
1773 ResolvedUndefs = Solver.ResolvedUndefsIn(F);
1776 bool MadeChanges =
false;
1783 if (!Solver.isBlockExecutable(&BB)) {
1850 if (skipFunction(F))
1854 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1864 "Sparse Conditional Constant Propagation",
false,
false)
1874 SCCPSolver &Solver) {
1876 if (!Solver.isArgumentTrackedFunction(&F))
1881 if (Solver.isMustTailCallee(&F)) {
1883 <<
" due to present musttail call of it\n");
1888 if (
CallInst *CI = BB.getTerminatingMustTailCall()) {
1889 LLVM_DEBUG(
dbgs() <<
"Can't zap return of the block due to present " 1890 <<
"musttail call : " << *CI <<
"\n");
1895 if (
auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
1896 if (!isa<UndefValue>(RI->getOperand(0)))
1906 if (
SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
1907 if (!isa<ConstantInt>(SI->getCondition())) {
1909 Dest = SI->case_begin()->getCaseSuccessor();
1910 C = SI->case_begin()->getCaseValue();
1912 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(I)) {
1913 if (!isa<ConstantInt>(BI->getCondition())) {
1915 Dest = BI->getSuccessor(1);
1919 if (!isa<BlockAddress>(IBR->getAddress()->stripPointerCasts())) {
1921 Dest = IBR->getSuccessor(0);
1929 "Didn't find feasible edge?");
1939 SCCPSolver Solver(DL, TLI);
1944 if (F.isDeclaration())
1947 Solver.addAnalysis(F, getAnalysis(F));
1952 Solver.AddTrackedFunction(&F);
1957 Solver.AddArgumentTrackedFunction(&F);
1962 Solver.MarkBlockExecutable(&F.front());
1966 Solver.markOverdefined(&AI);
1973 G.removeDeadConstantUsers();
1975 Solver.TrackValueOfGlobalVariable(&
G);
1979 bool ResolvedUndefs =
true;
1981 while (ResolvedUndefs) {
1983 ResolvedUndefs =
false;
1985 if (Solver.ResolvedUndefsIn(F)) {
1989 ResolvedUndefs =
true;
1993 bool MadeChanges =
false;
1999 if (F.isDeclaration())
2004 if (Solver.isBlockExecutable(&F.front()))
2014 if (!Solver.isBlockExecutable(&*BB)) {
2020 if (&*BB != &F.front())
2049 if (!Solver.isBlockExecutable(&F.front()))
2060 UE = DeadBB->user_end();
2065 do { ++UI; }
while (UI != UE && *UI == I);
2077 "Expect TermInst on constantint or blockaddress to be folded");
2087 if (Solver.getPredicateInfoFor(Inst)) {
2088 if (
auto *II = dyn_cast<IntrinsicInst>(Inst)) {
2090 Value *
Op = II->getOperand(0);
2113 for (
const auto &I : RV) {
2120 for (
const auto &F : Solver.getMRVFunctionsTracked()) {
2121 assert(F->getReturnType()->isStructTy() &&
2122 "The return type should be a struct");
2123 StructType *STy = cast<StructType>(F->getReturnType());
2124 if (Solver.isStructLatticeConstant(F, STy))
2129 for (
unsigned i = 0, e = ReturnsToZap.
size(); i != e; ++i) {
2130 Function *F = ReturnsToZap[i]->getParent()->getParent();
2138 E = TG.
end(); I !=
E; ++
I) {
2140 assert(!I->second.isOverdefined() &&
2141 "Overdefined values should have been taken out of the map!");
2143 <<
"' is constant!\n");
2148 M.getGlobalList().
erase(GV);
Legacy wrapper pass to provide the GlobalsAAResult object.
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool onlyReadsMemory() const
Determine if the function does not access or only reads memory.
Return a value (possibly void), from a function.
User::op_iterator arg_iterator
The type of iterator to use when looping over actual arguments at this call site. ...
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
This class is the base class for the comparison instructions.
void setInt(IntType IntVal)
static bool isConstant(const MachineInstr &MI)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents an incoming formal argument to a Function.
Base class for instruction visitors.
Value * getAggregateOperand()
Interprocedural Sparse Conditional Constant Propagation
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
iterator erase(iterator where)
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This class represents lattice values for constants.
PointerTy getPointer() const
This is the interface for a simple mod/ref and alias analysis over globals.
unsigned getNumIndices() const
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
A Module instance is used to store all the information related to an LLVM module. ...
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant *> IdxList, bool InBounds=false, Optional< unsigned > InRangeIndex=None, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
An instruction for ordering other memory operations.
bool canTrackArgumentsInterprocedurally(Function *F)
Determine if the values of the given function's arguments can be tracked interprocedurally.
Implements a dense probed hash-table based set.
unsigned getNumElements() const
Random access to the elements.
void push_back(const T &Elt)
bool isSafeToRemove() const
Return true if the instruction can be removed if the result is unused.
static ValueLatticeElement get(Constant *C)
This class represents a function call, abstracting a target machine's calling convention.
const Value * getTrueValue() const
An efficient, type-erasing, non-owning reference to a callable.
bool isTerminator() const
STATISTIC(NumFunctions, "Total number of functions")
An instruction for reading from memory.
void reserve(size_type N)
bool isMustTailCall() const
static void findReturnsToZap(Function &F, SmallVector< ReturnInst *, 8 > &ReturnsToZap, SCCPSolver &Solver)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
std::unique_ptr< PredicateInfo > PredInfo
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
The address of a basic block.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void setPointer(PointerTy PtrVal)
bool isVolatile() const
Return true if this is a load from a volatile memory location.
This class represents the LLVM 'select' instruction.
unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
This is the base class for all instructions that perform data casts.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Class to represent struct types.
void initializeSCCPLegacyPassPass(PassRegistry &)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
InstrTy * getInstruction() const
static void forceIndeterminateEdge(Instruction *I, SCCPSolver &Solver)
FunctionPass * createSCCPPass()
Type * getSourceElementType() const
Helper struct for bundling up the analysis results per function for IPSCCP.
static int64_t getConstant(const MachineInstr *MI)
void assign(size_type NumElts, const T &Elt)
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Type * getType() const
All values are typed, get the type of this value.
Value * getInsertedValueOperand()
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
An instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
bool isOverdefined() const
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Value * getOperand(unsigned i) const
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
iterator find(const_arg_type_t< KeyT > Val)
bool canTrackReturnsInterprocedurally(Function *F)
Determine if the values of the given function's returns can be tracked interprocedurally.
bool isVoidTy() const
Return true if this is 'void'.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
static bool runOnFunction(Function &F, bool PostInlining)
bool erase(const KeyT &Val)
Type * getReturnType() const
Returns the type of the ret val.
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
PointerIntPair - This class implements a pair of a pointer and small integer.
The instances of the Type class are immutable: once they are created, they are never changed...
void deleteBB(BasicBlock *DelBB)
Delete DelBB.
Conditional or Unconditional Branch instruction.
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
This function has undefined behavior.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
LLVM_NODISCARD bool empty() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Resume the propagation of an exception.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Indirect Branch Instruction.
std::pair< iterator, bool > insert(const ValueT &V)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
static Constant * get(StructType *T, ArrayRef< Constant *> V)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
const Function * getFunction() const
Return the function this instruction belongs to.
static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V)
Constant * ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty, const DataLayout &DL)
ConstantFoldLoadFromConstPtr - Return the value that a load from C would produce if it is constant an...
const Value * getCondition() const
static Constant * getAllOnesValue(Type *Ty)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
bool isExceptionalTerminator() const
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
This file implements the PredicateInfo analysis, which creates an Extended SSA form for operations us...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
idx_iterator idx_begin() const
Iterator for intrusive lists based on ilist_node.
unsigned getNumOperands() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
BlockVerifier::State From
INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp", "Interprocedural Sparse Conditional Constant Propagation", false, false) INITIALIZE_PASS_END(IPSCCPLegacyPass
Constant * getCompare(CmpInst::Predicate Pred, Type *Ty, const ValueLatticeElement &Other) const
Compares this symbolic value with Other using Pred and returns either true, false or undef constants...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
bool canTrackGlobalVariableInterprocedurally(GlobalVariable *GV)
Determine if the value maintained in the given global variable can be tracked interprocedurally.
LLVM_NODISCARD T pop_back_val()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB)
Remove all instructions from a basic block other than it's terminator and any present EH pad instruct...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static const Function * getCalledFunction(const Value *V, bool LookThroughBitCast, bool &IsNoBuiltin)
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
iterator_range< user_iterator > users()
Represents analyses that only rely on functions' control flow.
const Value * getFalseValue() const
Predicate getPredicate() const
Return the predicate for this instruction.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
LLVM_NODISCARD bool empty() const
StringRef getName() const
Return a constant reference to the value's name.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
user_iterator_impl< User > user_iterator
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Type * getValueType() const
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
LLVM_NODISCARD bool empty() const
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it's an indirect...
Analysis pass providing the TargetLibraryInfo.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const BasicBlock & front() const
bool isSingleValueType() const
Return true if the type is a valid type for a register in codegen.
bool runIPSCCP(Module &M, const DataLayout &DL, const TargetLibraryInfo *TLI, function_ref< AnalysisResultsForFn(Function &)> getAnalysis)
Module * getParent()
Get the module that this global value is contained inside of...
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
LLVM Value Representation.
bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function...
static ValueLatticeElement getOverdefined()
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
Constant * ConstantFoldCall(ImmutableCallSite CS, Function *F, ArrayRef< Constant *> Operands, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCall - Attempt to constant fold a call to the specified function with the specified argum...
static bool runSCCP(Function &F, const DataLayout &DL, const TargetLibraryInfo *TLI)
iterator_range< arg_iterator > args()
bool isStructTy() const
True if this is an instance of StructType.
const BasicBlock * getParent() const
This instruction inserts a struct field of array element value into an aggregate value.
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.