79 using namespace PatternMatch;
81 #define DEBUG_TYPE "simplifycfg"
90 "Control the amount of phi node folding to perform (default = 2)"));
94 cl::desc(
"Duplicate return instructions into unconditional branches"));
98 cl::desc(
"Sink common instructions down to the end block"));
102 cl::desc(
"Hoist conditional stores if an unconditional store precedes"));
106 cl::desc(
"Hoist conditional stores even if an unconditional store does not "
107 "precede - hoist multiple conditional stores into a single "
108 "predicated store"));
112 cl::desc(
"When merging conditional stores, do so even if the resultant "
113 "basic blocks are unlikely to be if-converted as a result"));
117 cl::desc(
"Allow exactly one expensive instruction to be speculatively "
122 cl::desc(
"Limit maximum recursion depth when calculating costs of "
123 "speculatively executed instructions"));
125 STATISTIC(NumBitMaps,
"Number of switch instructions turned into bitmaps");
127 "Number of switch instructions turned into linear mapping");
129 "Number of switch instructions turned into lookup tables");
131 NumLookupTablesHoles,
132 "Number of switch instructions turned into lookup tables (holes checked)");
133 STATISTIC(NumTableCmpReuses,
"Number of reused switch table lookup compares");
135 "Number of common instructions sunk down to the end block");
136 STATISTIC(NumSpeculations,
"Number of speculative executed instructions");
144 SwitchCaseResultVectorTy;
151 struct ValueEqualityComparisonCase {
156 : Value(Value), Dest(Dest) {}
158 bool operator<(ValueEqualityComparisonCase RHS)
const {
160 return Value < RHS.Value;
166 class SimplifyCFGOpt {
169 unsigned BonusInstThreshold;
175 bool SimplifyEqualityComparisonWithOnlyPredecessor(
TerminatorInst *TI,
196 : TTI(TTI), DL(DL), BonusInstThreshold(BonusInstThreshold), AC(AC),
197 LoopHeaders(LoopHeaders) {}
221 if (SI1Succs.
count(Succ))
223 PHINode *PN = cast<PHINode>(BBI);
227 FailBlocks->insert(Succ);
254 if (!(Cond->
getOperand(0) == Ci2->getOperand(0) &&
256 !(Cond->
getOperand(0) == Ci2->getOperand(1) &&
264 if (SI1Succs.
count(Succ))
266 PHINode *PN = cast<PHINode>(BBI);
281 if (!isa<PHINode>(Succ->
begin()))
296 "Instruction is not safe to speculatively execute!");
319 unsigned &CostRemaining,
321 unsigned Depth = 0) {
354 if (!AggressiveInsts)
358 if (AggressiveInsts->
count(I))
375 if (Cost > CostRemaining &&
380 CostRemaining = (Cost > CostRemaining) ? 0 : CostRemaining - Cost;
389 AggressiveInsts->
insert(I);
406 if (isa<ConstantPointerNull>(V))
411 if (CE->getOpcode() == Instruction::IntToPtr)
412 if (
ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
417 return cast<ConstantInt>(
435 struct ConstantComparesGatherer {
444 : DL(DL), CompValue(nullptr), Extra(nullptr), UsedICmps(0) {
449 ConstantComparesGatherer(
const ConstantComparesGatherer &) =
delete;
450 ConstantComparesGatherer &
451 operator=(
const ConstantComparesGatherer &) =
delete;
456 bool setValueOnce(
Value *NewVal) {
457 if (CompValue && CompValue != NewVal)
460 return (CompValue !=
nullptr);
474 if (!((ICI = dyn_cast<ICmpInst>(I)) &&
532 if (!setValueOnce(RHSVal))
555 if (!setValueOnce(RHSVal))
584 CandidateVal = RHSVal;
599 if (!setValueOnce(CandidateVal))
615 void gather(
Value *V) {
627 while (!DFT.
empty()) {
641 if (matchInstruction(I, isEQ))
666 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
667 if (BI->isConditional())
668 Cond = dyn_cast<Instruction>(BI->getCondition());
682 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
685 if (
SI->getNumSuccessors() * std::distance(
pred_begin(
SI->getParent()),
688 CV =
SI->getCondition();
689 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(TI))
690 if (BI->isConditional() && BI->getCondition()->hasOneUse())
691 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
699 Value *
Ptr = PTII->getPointerOperand();
700 if (PTII->getType() ==
DL.getIntPtrType(Ptr->
getType()))
709 BasicBlock *SimplifyCFGOpt::GetValueEqualityComparisonCases(
711 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
712 Cases.reserve(
SI->getNumCases());
716 ValueEqualityComparisonCase(
i.getCaseValue(),
i.getCaseSuccessor()));
717 return SI->getDefaultDest();
723 Cases.push_back(ValueEqualityComparisonCase(
732 std::vector<ValueEqualityComparisonCase> &Cases) {
733 Cases.erase(
std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
738 std::vector<ValueEqualityComparisonCase> &C2) {
739 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *
V2 = &C2;
742 if (V1->size() > V2->size())
747 if (V1->size() == 1) {
750 for (
unsigned i = 0, e = V2->size();
i != e; ++
i)
751 if (TheVal == (*V2)[
i].
Value)
758 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
759 while (i1 != e1 && i2 != e2) {
762 if ((*V1)[i1].Value < (*V2)[i2].Value)
775 bool SimplifyCFGOpt::SimplifyEqualityComparisonWithOnlyPredecessor(
781 Value *ThisVal = isValueEqualityComparison(TI);
782 assert(ThisVal &&
"This isn't a value comparison!!");
783 if (ThisVal != PredVal)
790 std::vector<ValueEqualityComparisonCase> PredCases;
792 GetValueEqualityComparisonCases(Pred->
getTerminator(), PredCases);
796 std::vector<ValueEqualityComparisonCase> ThisCases;
797 BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
809 if (isa<BranchInst>(TI)) {
812 assert(ThisCases.size() == 1 &&
"Branch can only have one case!");
818 ThisCases[0].Dest->removePredecessor(TI->
getParent());
821 <<
"Through successor TI: " << *TI <<
"Leaving: " << *NI
831 for (
unsigned i = 0, e = PredCases.size();
i != e; ++
i)
832 DeadCases.
insert(PredCases[
i].Value);
835 <<
"Through successor TI: " << *TI);
849 if (DeadCases.count(i.getCaseValue())) {
851 std::swap(Weights[i.getCaseIndex() + 1], Weights.back());
854 i.getCaseSuccessor()->removePredecessor(TI->
getParent());
858 if (HasWeight && Weights.size() >= 2)
861 .createBranchWeights(Weights));
863 DEBUG(
dbgs() <<
"Leaving: " << *TI <<
"\n");
871 for (
unsigned i = 0, e = PredCases.size(); i != e; ++
i)
872 if (PredCases[i].Dest == TIBB) {
877 assert(TIV &&
"No edge from pred to succ?");
882 for (
unsigned i = 0, e = ThisCases.size(); i != e; ++
i)
883 if (ThisCases[i].
Value == TIV) {
884 TheRealDest = ThisCases[
i].Dest;
890 TheRealDest = ThisDef;
895 if (Succ != CheckEdge)
905 <<
"Through successor TI: " << *TI <<
"Leaving: " << *NI
917 struct ConstantIntOrdering {
938 return MDS->getString().equals(
"branch_weights");
958 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
968 uint64_t Max = *std::max_element(Weights.
begin(), Weights.
end());
969 if (Max > UINT_MAX) {
971 for (uint64_t &I : Weights)
980 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(
TerminatorInst *TI,
983 Value *CV = isValueEqualityComparison(TI);
984 assert(CV &&
"Not a comparison?");
985 bool Changed =
false;
988 while (!Preds.empty()) {
993 Value *PCV = isValueEqualityComparison(PTI);
995 if (PCV == CV && TI != PTI) {
998 for (
auto *Succ : FailBlocks) {
999 std::vector<BasicBlock*> Blocks = { TI->
getParent() };
1006 std::vector<ValueEqualityComparisonCase> BBCases;
1007 BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
1009 std::vector<ValueEqualityComparisonCase> PredCases;
1010 BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
1022 if (PredHasWeights) {
1025 if (Weights.
size() != 1 + PredCases.size())
1026 PredHasWeights = SuccHasWeights =
false;
1027 }
else if (SuccHasWeights)
1031 Weights.
assign(1 + PredCases.size(), 1);
1034 if (SuccHasWeights) {
1037 if (SuccWeights.
size() != 1 + BBCases.size())
1038 PredHasWeights = SuccHasWeights =
false;
1039 }
else if (PredHasWeights)
1040 SuccWeights.
assign(1 + BBCases.size(), 1);
1042 if (PredDefault == BB) {
1045 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1046 for (
unsigned i = 0, e = PredCases.size(); i != e; ++
i)
1047 if (PredCases[i].Dest != BB)
1048 PTIHandled.insert(PredCases[i].
Value);
1051 std::swap(PredCases[i], PredCases.back());
1053 if (PredHasWeights || SuccHasWeights) {
1055 Weights[0] += Weights[i + 1];
1060 PredCases.pop_back();
1066 if (PredDefault != BBDefault) {
1068 PredDefault = BBDefault;
1072 unsigned CasesFromPred = Weights.
size();
1073 uint64_t ValidTotalSuccWeight = 0;
1074 for (
unsigned i = 0, e = BBCases.size(); i != e; ++
i)
1075 if (!PTIHandled.count(BBCases[i].Value) &&
1076 BBCases[
i].Dest != BBDefault) {
1077 PredCases.push_back(BBCases[i]);
1078 NewSuccessors.
push_back(BBCases[i].Dest);
1079 if (SuccHasWeights || PredHasWeights) {
1083 Weights.
push_back(Weights[0] * SuccWeights[i + 1]);
1084 ValidTotalSuccWeight += SuccWeights[i + 1];
1088 if (SuccHasWeights || PredHasWeights) {
1089 ValidTotalSuccWeight += SuccWeights[0];
1091 for (
unsigned i = 1; i < CasesFromPred; ++
i)
1092 Weights[i] *= ValidTotalSuccWeight;
1094 Weights[0] *= SuccWeights[0];
1100 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1101 std::map<ConstantInt *, uint64_t> WeightsForHandled;
1102 for (
unsigned i = 0, e = PredCases.size(); i != e; ++
i)
1103 if (PredCases[i].Dest == BB) {
1104 PTIHandled.
insert(PredCases[i].Value);
1106 if (PredHasWeights || SuccHasWeights) {
1107 WeightsForHandled[PredCases[
i].Value] = Weights[i + 1];
1112 std::swap(PredCases[i], PredCases.back());
1113 PredCases.pop_back();
1120 for (
unsigned i = 0, e = BBCases.size(); i != e; ++
i)
1121 if (PTIHandled.count(BBCases[i].Value)) {
1123 if (PredHasWeights || SuccHasWeights)
1124 Weights.
push_back(WeightsForHandled[BBCases[i].Value]);
1125 PredCases.push_back(BBCases[i]);
1126 NewSuccessors.
push_back(BBCases[i].Dest);
1134 if (PredHasWeights || SuccHasWeights)
1135 Weights.
push_back(WeightsForHandled[I]);
1136 PredCases.push_back(ValueEqualityComparisonCase(I, BBDefault));
1144 for (
BasicBlock *NewSuccessor : NewSuccessors)
1158 for (ValueEqualityComparisonCase &V : PredCases)
1161 if (PredHasWeights || SuccHasWeights) {
1180 if (!InfLoopBlock) {
1207 if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1238 while (isa<DbgInfoIntrinsic>(I1))
1240 while (isa<DbgInfoIntrinsic>(I2))
1249 bool Changed =
false;
1253 if (isa<TerminatorInst>(I1))
1254 goto HoistTerminator;
1263 if (!I2->use_empty())
1264 I2->replaceAllUsesWith(I1);
1280 if (!isa<CallInst>(I1))
1284 I2->eraseFromParent();
1293 while (isa<DbgInfoIntrinsic>(I1))
1295 while (isa<DbgInfoIntrinsic>(I2))
1334 I2->replaceAllUsesWith(NT);
1343 std::map<std::pair<Value *, Value *>,
SelectInst *> InsertedSelects;
1355 SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1357 SI = cast<SelectInst>(
1359 BB1V->
getName() +
"." + BB2V->getName(), BI));
1392 case Instruction::Invoke:
1396 if (isa<IntrinsicInst>(I))
1406 case Instruction::ShuffleVector:
1409 case Instruction::ExtractValue:
1410 case Instruction::InsertValue:
1413 case Instruction::Alloca:
1415 case Instruction::GetElementPtr:
1434 for (
auto *I : Insts) {
1436 if (isa<PHINode>(I) || I->isEHPad() || isa<AllocaInst>(
I) ||
1437 I->getType()->isTokenTy())
1443 if (
const auto *C = dyn_cast<CallInst>(I))
1444 if (C->isInlineAsm())
1449 if (!isa<StoreInst>(I) && !I->hasOneUse())
1454 for (
auto *I : Insts)
1455 if (!I->isSameOperationAs(I0))
1462 if (!isa<StoreInst>(I0)) {
1466 auto *U = cast<Instruction>(*I->
user_begin());
1468 PNUse->getParent() == Succ &&
1469 PNUse->getIncomingValueForBlock(I->
getParent()) == I) ||
1475 for (
unsigned OI = 0, OE = I0->
getNumOperands(); OI != OE; ++OI) {
1488 if (OI == 1 && isa<StoreInst>(I0) &&
1502 if (!
all_of(Insts, SameAsI0)) {
1507 if ((isa<CallInst>(I0) || isa<InvokeInst>(I0)) && OI == OE - 1) {
1511 for (
auto *I : Insts)
1522 auto *BBEnd = Blocks[0]->getTerminator()->getSuccessor(0);
1527 for (
auto *BB : Blocks) {
1531 }
while (isa<DbgInfoIntrinsic>(I) && I != &BB->
front());
1532 if (!isa<DbgInfoIntrinsic>(I))
1541 if (!isa<StoreInst>(I0)) {
1544 auto *U = cast<Instruction>(*I->
user_begin());
1570 assert(!
Op->getType()->isTokenTy() &&
"Can't PHI tokens!");
1572 Op->getName() +
".sink", &BBEnd->front());
1573 for (
auto *I : Insts)
1582 I0->
moveBefore(&*BBEnd->getFirstInsertionPt());
1590 for (
auto *I : Insts)
1596 if (!isa<CallInst>(I0))
1599 if (!isa<StoreInst>(I0)) {
1605 PN->replaceAllUsesWith(I0);
1606 PN->eraseFromParent();
1610 for (
auto *I : Insts)
1612 I->eraseFromParent();
1627 class LockstepReverseIterator {
1640 for (
auto *BB : Blocks) {
1642 for (Inst = Inst->
getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1649 Insts.push_back(Inst);
1657 void operator -- () {
1660 for (
auto *&Inst : Insts) {
1661 for (Inst = Inst->
getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
1730 auto *
T =
B->getTerminator();
1731 if (isa<BranchInst>(
T) && cast<BranchInst>(
T)->isUnconditional())
1733 else if ((isa<BranchInst>(
T) || isa<SwitchInst>(
T)) && !Cond)
1738 if (UnconditionalPreds.
size() < 2)
1741 bool Changed =
false;
1748 unsigned ScanIdx = 0;
1751 LockstepReverseIterator LRI(UnconditionalPreds);
1752 while (LRI.isValid() &&
1754 DEBUG(
dbgs() <<
"SINK: instruction can be sunk: " << *(*LRI)[0] <<
"\n");
1755 InstructionsToSink.
insert((*LRI).begin(), (*LRI).end());
1760 auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
1761 unsigned NumPHIdValues = 0;
1762 for (
auto *I : *LRI)
1763 for (
auto *V : PHIOperands[I])
1764 if (InstructionsToSink.
count(V) == 0)
1766 DEBUG(
dbgs() <<
"SINK: #phid values: " << NumPHIdValues <<
"\n");
1767 unsigned NumPHIInsts = NumPHIdValues / UnconditionalPreds.
size();
1768 if ((NumPHIdValues % UnconditionalPreds.
size()) != 0)
1771 return NumPHIInsts <= 1;
1774 if (ScanIdx > 0 && Cond) {
1782 bool Profitable =
false;
1783 while (ProfitableToSinkInstruction(LRI) && Idx < ScanIdx) {
1794 DEBUG(
dbgs() <<
"SINK: Splitting edge\n");
1816 for (
unsigned SinkIdx = 0; SinkIdx != ScanIdx; ++SinkIdx) {
1818 << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
1824 if (!ProfitableToSinkInstruction(LRI)) {
1826 DEBUG(
dbgs() <<
"SINK: stopping here, too many PHIs would be created!\n");
1877 unsigned MaxNumInstToLookAt = 9;
1879 if (!MaxNumInstToLookAt)
1882 if (isa<DbgInfoIntrinsic>(CurI))
1884 --MaxNumInstToLookAt;
1887 if (CurI.mayHaveSideEffects() && !isa<StoreInst>(CurI))
1890 if (
auto *SI = dyn_cast<StoreInst>(&CurI)) {
1892 if (SI->getPointerOperand() == StorePtr)
1894 return SI->getValueOperand();
1943 if (isa<FCmpInst>(BrCond))
1951 bool Invert =
false;
1965 unsigned SpeculationCost = 0;
1966 Value *SpeculatedStoreValue =
nullptr;
1969 BBE = std::prev(ThenBB->
end());
1970 BBI != BBE; ++BBI) {
1973 if (isa<DbgInfoIntrinsic>(I))
1979 if (SpeculationCost > 1)
1985 I, BB, ThenBB, EndBB))))
1987 if (!SpeculatedStoreValue &&
1993 if (SpeculatedStoreValue)
1994 SpeculatedStore = cast<StoreInst>(
I);
2004 ++SinkCandidateUseCounts[OpI];
2012 I = SinkCandidateUseCounts.
begin(),
2013 E = SinkCandidateUseCounts.end();
2015 if (I->first->getNumUses() == I->second) {
2017 if (SpeculationCost > 1)
2022 bool HaveRewritablePHIs =
false;
2038 HaveRewritablePHIs =
true;
2041 if (!OrigCE && !ThenCE)
2051 if (OrigCost + ThenCost > MaxCost)
2059 if (SpeculationCost > 1)
2065 if (!HaveRewritablePHIs && !(
HoistCondStores && SpeculatedStoreValue))
2069 DEBUG(
dbgs() <<
"SPECULATIVELY EXECUTING BB" << *ThenBB <<
"\n";);
2072 if (SpeculatedStoreValue) {
2075 Value *FalseV = SpeculatedStoreValue;
2079 BrCond, TrueV, FalseV, TrueV->
getName() +
"." + FalseV->
getName(), BI);
2085 for (
auto &I : *ThenBB)
2086 I.dropUnknownNonDebugMetadata();
2090 ThenBB->begin(), std::prev(ThenBB->end()));
2108 Value *TrueV = ThenV, *FalseV = OrigV;
2112 BrCond, TrueV, FalseV, TrueV->
getName() +
"." + FalseV->getName(), BI);
2127 if (isa<DbgInfoIntrinsic>(BBI))
2137 if (UI->
getParent() != BB || isa<PHINode>(UI))
2211 if (
PHINode *PN = dyn_cast<PHINode>(BBI)) {
2218 N->
setName(BBI->getName() +
".c");
2223 if (PI != TranslateMap.
end())
2229 if (!BBI->use_empty())
2230 TranslateMap[&*BBI] = V;
2236 if (!BBI->use_empty())
2237 TranslateMap[&*BBI] = N;
2275 isa<ConstantInt>(IfCond))
2283 unsigned NumPhis = 0;
2298 PHINode *PN = cast<PHINode>(II++);
2306 MaxCostVal0, TTI) ||
2323 isa<BinaryOperator>(IfCond)))
2333 if (cast<BranchInst>(IfBlock1->
getTerminator())->isConditional()) {
2339 if (!AggressiveInsts.
count(&*I) && !isa<DbgInfoIntrinsic>(
I)) {
2347 if (cast<BranchInst>(IfBlock2->
getTerminator())->isConditional()) {
2353 if (!AggressiveInsts.
count(&*I) && !isa<DbgInfoIntrinsic>(
I)) {
2361 DEBUG(
dbgs() <<
"FOUND IF CONDITION! " << *IfCond <<
" T: "
2372 for (
auto &I : *IfBlock1)
2373 I.dropUnknownNonDebugMetadata();
2375 IfBlock1->getInstList(), IfBlock1->begin(),
2376 IfBlock1->getTerminator()->getIterator());
2379 for (
auto &I : *IfBlock2)
2380 I.dropUnknownNonDebugMetadata();
2382 IfBlock2->getInstList(), IfBlock2->begin(),
2383 IfBlock2->getTerminator()->getIterator());
2391 Value *Sel = Builder.
CreateSelect(IfCond, TrueVal, FalseVal,
"", InsertPt);
2415 ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
2421 if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
2430 if (FalseRet->getNumOperands() == 0) {
2431 TrueSucc->removePredecessor(BI->
getParent());
2441 Value *FalseValue = FalseRet->getReturnValue();
2444 if (
PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
2445 if (TVPN->getParent() == TrueSucc)
2446 TrueValue = TVPN->getIncomingValueForBlock(BI->
getParent());
2447 if (
PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
2448 if (FVPN->getParent() == FalseSucc)
2449 FalseValue = FVPN->getIncomingValueForBlock(BI->
getParent());
2456 if (
ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
2459 if (
ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
2465 TrueSucc->removePredecessor(BI->
getParent());
2472 if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
2473 }
else if (isa<UndefValue>(TrueValue)) {
2474 TrueValue = FalseValue;
2477 Builder.
CreateSelect(BrCond, TrueValue, FalseValue,
"retval", BI);
2486 DEBUG(
dbgs() <<
"\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
2487 <<
"\n " << *BI <<
"NewRet = " << *RI
2488 <<
"TRUEBLOCK: " << *TrueSucc <<
"FALSEBLOCK: " << *FalseSucc);
2498 if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2516 uint64_t &PredTrueWeight,
2517 uint64_t &PredFalseWeight,
2518 uint64_t &SuccTrueWeight,
2519 uint64_t &SuccFalseWeight) {
2520 bool PredHasWeights =
2522 bool SuccHasWeights =
2524 if (PredHasWeights || SuccHasWeights) {
2525 if (!PredHasWeights)
2526 PredTrueWeight = PredFalseWeight = 1;
2527 if (!SuccHasWeights)
2528 SuccTrueWeight = SuccFalseWeight = 1;
2550 if (
BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
2551 if (PBI->isConditional() &&
2556 if (isa<CmpInst>(Curr)) {
2570 if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2578 while (isa<DbgInfoIntrinsic>(CondIt))
2589 unsigned NumBonusInsts = 0;
2590 for (
auto I = BB->
begin(); Cond != &*
I; ++
I) {
2592 if (isa<DbgInfoIntrinsic>(I))
2598 if (User ==
nullptr || User->
getParent() != BB)
2605 if (NumBonusInsts > BonusInstThreshold)
2621 if (TrueDest == BB || FalseDest == BB)
2640 bool InvertPredCond =
false;
2649 InvertPredCond =
true;
2652 InvertPredCond =
true;
2661 DEBUG(
dbgs() <<
"FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
2665 if (InvertPredCond) {
2668 if (NewCond->
hasOneUse() && isa<CmpInst>(NewCond)) {
2669 CmpInst *CI = cast<CmpInst>(NewCond);
2687 for (
auto BonusInst = BB->
begin(); Cond != &*BonusInst; ++BonusInst) {
2688 if (isa<DbgInfoIntrinsic>(BonusInst))
2693 VMap[&*BonusInst] = NewBonusInst;
2703 NewBonusInst->
takeName(&*BonusInst);
2704 BonusInst->setName(BonusInst->getName() +
".old");
2721 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2724 SuccTrueWeight, SuccFalseWeight);
2732 NewWeights.
push_back(PredTrueWeight * SuccTrueWeight);
2738 (SuccFalseWeight + SuccTrueWeight) +
2739 PredTrueWeight * SuccFalseWeight);
2751 (SuccFalseWeight + SuccTrueWeight) +
2752 PredFalseWeight * SuccTrueWeight);
2754 NewWeights.
push_back(PredFalseWeight * SuccFalseWeight);
2759 if (NewWeights.
size() == 2) {
2772 for (
unsigned i = 0, e = PHIs.size(); i != e; ++
i) {
2774 PHIs[
i]->getIncomingValueForBlock(PBI->
getParent()));
2783 MergedCond = cast<Instruction>(
2786 MergedCond = cast<Instruction>(Builder.
CreateBinOp(
2792 MergedCond = cast<Instruction>(Builder.
CreateBinOp(
2794 if (PBI_C->
isOne()) {
2797 MergedCond = cast<Instruction>(Builder.
CreateBinOp(
2802 PHIs[
i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->
getParent()),
2821 if (isa<DbgInfoIntrinsic>(I))
2822 I.clone()->insertBefore(PBI);
2833 for (
auto *BB : {BB1, BB2}) {
2837 if (
auto *SI = dyn_cast<StoreInst>(&I)) {
2849 Value *AlternativeV =
nullptr) {
2867 for (
auto I = Succ->
begin(); isa<PHINode>(
I); ++
I)
2868 if (cast<PHINode>(I)->getIncomingValueForBlock(BB) == V) {
2869 PHI = cast<PHINode>(
I);
2875 BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
2884 if (!AlternativeV &&
2885 (!isa<Instruction>(V) || cast<Instruction>(V)->
getParent() != BB))
2900 bool InvertPCond,
bool InvertQCond) {
2901 auto IsaBitcastOfPointerType = [](
const Instruction &
I) {
2903 I.getType()->isPointerTy();
2915 for (
auto &I : *BB) {
2917 if (isa<BinaryOperator>(I) || isa<GetElementPtrInst>(
I) ||
2921 else if (isa<TerminatorInst>(I) || isa<DbgInfoIntrinsic>(
I) ||
2922 IsaBitcastOfPointerType(I))
2931 (!IsWorthwhile(PTB) || !IsWorthwhile(PFB) || !IsWorthwhile(QTB) ||
2932 !IsWorthwhile(QFB)))
2942 if (!PStore || !QStore)
2961 if (I.mayReadOrWriteMemory())
2963 for (
auto &I : *QFB)
2967 for (
auto &I : *QTB)
2979 Value *QCond = cast<BranchInst>(QFB->getSinglePredecessor()->getTerminator())
2989 Value *PPred = PStore->
getParent() == PTB ? PCond : QB.CreateNot(PCond);
2990 Value *QPred = QStore->
getParent() == QTB ? QCond : QB.CreateNot(QCond);
2993 PPred = QB.CreateNot(PPred);
2995 QPred = QB.CreateNot(QPred);
2996 Value *CombinedPred = QB.CreateOr(PPred, QPred);
3000 QB.SetInsertPoint(
T);
3001 StoreInst *SI = cast<StoreInst>(QB.CreateStore(QPHI, Address));
3048 bool InvertPCond =
false, InvertQCond =
false;
3054 if (QFB == PostBB) {
3074 !HasOnePredAndOneSucc(QFB, QBI->
getParent(), PostBB))
3077 (QTB && !HasOnePredAndOneSucc(QTB, QBI->
getParent(), PostBB)))
3085 for (
auto *BB : {PTB, PFB}) {
3089 if (
StoreInst *SI = dyn_cast<StoreInst>(&I))
3090 PStoreAddresses.
insert(SI->getPointerOperand());
3092 for (
auto *BB : {QTB, QFB}) {
3096 if (
StoreInst *SI = dyn_cast<StoreInst>(&I))
3097 QStoreAddresses.
insert(SI->getPointerOperand());
3103 auto &CommonAddresses = PStoreAddresses;
3105 bool Changed =
false;
3106 for (
auto *
Address : CommonAddresses)
3108 PTB, PFB, QTB, QFB, PostBB,
Address, InvertPCond, InvertQCond);
3128 if (BB->getSinglePredecessor()) {
3149 if ((PBI = dyn_cast<BranchInst>(P->
getTerminator())) && PBI != BI &&
3166 if (
auto *CE = dyn_cast<ConstantExpr>(BI->
getCondition()))
3181 while (isa<DbgInfoIntrinsic>(BBI))
3218 unsigned NumPhis = 0;
3224 PHINode *PN = cast<PHINode>(II);
3250 if (OtherDest == BB) {
3256 OtherDest = InfLoopBlock;
3268 PBICond = Builder.
CreateNot(PBICond, PBICond->getName() +
".not");
3275 Value *Cond = Builder.
CreateOr(PBICond, BICond,
"brmerge");
3283 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3284 uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
3287 SuccTrueWeight, SuccFalseWeight);
3289 PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
3290 PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
3291 SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
3292 SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
3296 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
3297 PredOther * SuccCommon,
3298 PredOther * SuccOther};
3304 .createBranchWeights(NewWeights[0], NewWeights[1]));
3319 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->
getParent());
3320 Value *PBIV = PN->getIncomingValue(PBBIdx);
3325 PN->setIncomingValue(PBBIdx, NV);
3331 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
3332 uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
3333 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
3334 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
3337 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
3338 PredOther * SuccCommon};
3344 .createBranchWeights(NewWeights[0], NewWeights[1]));
3371 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB :
nullptr;
3376 if (Succ == KeepEdge1)
3377 KeepEdge1 =
nullptr;
3378 else if (Succ == KeepEdge2)
3379 KeepEdge2 =
nullptr;
3389 if (!KeepEdge1 && !KeepEdge2) {
3390 if (TrueBB == FalseBB)
3398 if (TrueWeight != FalseWeight)
3401 .createBranchWeights(TrueWeight, FalseWeight));
3403 }
else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
3431 if (!TrueVal || !FalseVal)
3440 uint32_t TrueWeight = 0, FalseWeight = 0;
3514 if (!Pred || !isa<SwitchInst>(Pred->getTerminator()))
3517 SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
3526 assert(VVal &&
"Should have a unique destination value");
3534 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
3550 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
3557 if (PHIUse ==
nullptr || PHIUse != &SuccBlock->
front() ||
3584 Weights[0] = (Weights[0] + 1) >> 1;
3585 Weights.push_back(Weights[0]);
3617 ConstantComparesGatherer ConstantCompare(Cond, DL);
3620 Value *CompVal = ConstantCompare.CompValue;
3621 unsigned UsedICmps = ConstantCompare.UsedICmps;
3622 Value *ExtraCase = ConstantCompare.Extra;
3641 if (ExtraCase && Values.
size() < 2)
3655 DEBUG(
dbgs() <<
"Converting 'icmp' chain with " << Values.
size()
3656 <<
" cases into SWITCH. BB is:\n"
3680 DEBUG(
dbgs() <<
" ** 'icmp' chain unhandled condition: " << *ExtraCase
3681 <<
"\nEXTRABB = " << *BB);
3696 for (
unsigned i = 0, e = Values.
size(); i != e; ++
i)
3697 New->
addCase(Values[i], EdgeBB);
3703 PHINode *PN = cast<PHINode>(BBI);
3705 for (
unsigned i = 0, e = Values.
size() - 1; i != e; ++
i)
3712 DEBUG(
dbgs() <<
" ** 'icmp' chain result is:\n" << *BB <<
'\n');
3718 return SimplifyCommonResume(RI);
3722 return SimplifySingleResume(RI);
3728 bool SimplifyCFGOpt::SimplifyCommonResume(
ResumeInst *RI) {
3736 if (!isa<DbgInfoIntrinsic>(I))
3740 auto *PhiLPInst = cast<PHINode>(RI->
getValue());
3743 for (
unsigned Idx = 0,
End = PhiLPInst->getNumIncomingValues(); Idx !=
End;
3745 auto *IncomingBB = PhiLPInst->getIncomingBlock(Idx);
3746 auto *IncomingValue = PhiLPInst->getIncomingValue(Idx);
3750 if (IncomingBB->getUniqueSuccessor() != BB)
3755 if (IncomingValue != LandingPad)
3758 bool isTrivial =
true;
3760 I = IncomingBB->getFirstNonPHI()->getIterator();
3761 E = IncomingBB->getTerminator()->getIterator();
3763 if (!isa<DbgInfoIntrinsic>(I)) {
3769 TrivialUnwindBlocks.
insert(IncomingBB);
3773 if (TrivialUnwindBlocks.
empty())
3777 for (
auto *TrivialBB : TrivialUnwindBlocks) {
3781 while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
3795 TrivialBB->getTerminator()->eraseFromParent();
3803 return !TrivialUnwindBlocks.empty();
3807 bool SimplifyCFGOpt::SimplifySingleResume(
ResumeInst *RI) {
3811 "Resume must unwind the exception that caused control to here");
3816 if (!isa<DbgInfoIntrinsic>(I))
3828 LoopHeaders->
erase(BB);
3860 switch (IntrinsicID) {
3861 case Intrinsic::dbg_declare:
3862 case Intrinsic::dbg_value:
3863 case Intrinsic::lifetime_end:
3886 PHINode *DestPN = cast<PHINode>(
I);
3908 if (SrcPN && SrcPN->
getParent() == BB) {
3913 SrcIdx != SrcE; ++SrcIdx) {
3934 PHINode *PN = cast<PHINode>(I++);
3954 if (UnwindDest ==
nullptr) {
3982 if (!SuccessorCleanupPad)
3991 SuccessorCleanupPad->eraseFromParent();
4027 if (
BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
4037 while (!UncondBranchPreds.
empty()) {
4040 <<
"INTO UNCOND BRANCH PRED: " << *Pred);
4049 LoopHeaders->
erase(BB);
4058 while (!CondBranchPreds.
empty()) {
4073 bool Changed =
false;
4083 if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
4086 if (BBI->mayHaveSideEffects()) {
4087 if (
auto *SI = dyn_cast<StoreInst>(BBI)) {
4088 if (SI->isVolatile())
4090 }
else if (
auto *LI = dyn_cast<LoadInst>(BBI)) {
4091 if (LI->isVolatile())
4093 }
else if (
auto *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
4094 if (RMWI->isVolatile())
4096 }
else if (
auto *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
4097 if (CXI->isVolatile())
4099 }
else if (isa<CatchPadInst>(BBI)) {
4107 }
else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
4108 !isa<LandingPadInst>(BBI)) {
4118 if (!BBI->use_empty())
4120 BBI->eraseFromParent();
4126 if (&BB->
front() != UI)
4130 for (
unsigned i = 0, e = Preds.size(); i != e; ++
i) {
4133 if (
auto *BI = dyn_cast<BranchInst>(TI)) {
4150 }
else if (
auto *SI = dyn_cast<SwitchInst>(TI)) {
4153 if (i.getCaseSuccessor() == BB) {
4160 }
else if (
auto *II = dyn_cast<InvokeInst>(TI)) {
4161 if (II->getUnwindDest() == BB) {
4165 }
else if (
auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
4166 if (CSI->getUnwindDest() == BB) {
4173 E = CSI->handler_end();
4176 CSI->removeHandler(I);
4182 if (CSI->getNumHandlers() == 0) {
4184 if (CSI->hasUnwindDest()) {
4198 }
else if (isa<CleanupReturnInst>(TI)) {
4210 LoopHeaders->
erase(BB);
4221 for (
size_t I = 1,
E = Cases.
size(); I !=
E; ++
I) {
4222 if (Cases[I - 1]->getValue() != Cases[
I]->getValue() + 1)
4246 if (Dest == DestA) {
4252 if (Dest == DestB) {
4260 "Single-destination switch should have been folded.");
4263 assert(!CasesB.
empty() &&
"There must be non-default cases.");
4271 ContiguousCases = &CasesA;
4272 ContiguousDest = DestA;
4275 ContiguousCases = &CasesB;
4276 ContiguousDest = DestB;
4293 if (NumCases->isNullValue() && !ContiguousCases->empty())
4304 uint64_t TrueWeight = 0;
4305 uint64_t FalseWeight = 0;
4306 for (
size_t I = 0,
E = Weights.
size(); I !=
E; ++
I) {
4308 TrueWeight += Weights[I];
4310 FalseWeight += Weights[
I];
4312 while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
4318 .createBranchWeights((
uint32_t)TrueWeight,
4324 for (
auto BBI = ContiguousDest->
begin(); isa<PHINode>(BBI); ++BBI) {
4325 unsigned PreviousEdges = ContiguousCases->size();
4328 for (
unsigned I = 0,
E = PreviousEdges - 1; I !=
E; ++
I)
4329 cast<PHINode>(BBI)->removeIncomingValue(SI->
getParent());
4331 for (
auto BBI = OtherDest->
begin(); isa<PHINode>(BBI); ++BBI) {
4332 unsigned PreviousEdges = SI->
getNumCases() - ContiguousCases->size();
4335 for (
unsigned I = 0,
E = PreviousEdges - 1; I !=
E; ++
I)
4336 cast<PHINode>(BBI)->removeIncomingValue(SI->
getParent());
4351 APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
4358 unsigned MaxSignificantBitsInCond = Bits - ExtraSignBits;
4362 for (
auto &Case : SI->
cases()) {
4363 APInt CaseVal = Case.getCaseValue()->getValue();
4364 if ((CaseVal & KnownZero) != 0 || (CaseVal & KnownOne) != KnownOne ||
4366 DeadCases.
push_back(Case.getCaseValue());
4367 DEBUG(
dbgs() <<
"SimplifyCFG: switch case " << CaseVal <<
" is dead.\n");
4377 const unsigned NumUnknownBits =
4379 assert(NumUnknownBits <= Bits);
4380 if (HasDefault && DeadCases.
empty() &&
4381 NumUnknownBits < 64 &&
4383 DEBUG(
dbgs() <<
"SimplifyCFG: switch default is dead.\n");
4405 "Case was not found. Probably mistake in DeadCases forming.");
4415 if (HasWeight && Weights.
size() >= 2) {
4419 .createBranchWeights(MDWeights));
4422 return !DeadCases.empty();
4444 while (
PHINode *PHI = dyn_cast<PHINode>(I++)) {
4445 int Idx = PHI->getBasicBlockIndex(BB);
4446 assert(Idx >= 0 &&
"PHI has no entry for predecessor?");
4448 Value *InValue = PHI->getIncomingValue(Idx);
4449 if (InValue != CaseValue)
4465 ForwardingNodesMap ForwardingNodes;
4478 ForwardingNodes[PHI].push_back(PhiIndex);
4481 bool Changed =
false;
4483 for (ForwardingNodesMap::iterator I = ForwardingNodes.begin(),
4484 E = ForwardingNodes.end();
4489 if (Indexes.
size() < 2)
4492 for (
size_t I = 0,
E = Indexes.
size(); I !=
E; ++
I)
4508 if (!isa<ConstantFP>(C) && !isa<ConstantInt>(
C) &&
4509 !isa<ConstantPointerNull>(C) && !isa<GlobalValue>(
C) &&
4510 !isa<UndefValue>(C) && !isa<ConstantExpr>(
C))
4514 if (!CE->isGEPWithNoNotionalOverIndexing())
4531 if (
Constant *C = dyn_cast<Constant>(V))
4533 return ConstantPool.
lookup(V);
4562 if (
CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
4590 if (
T->getNumSuccessors() != 1 ||
T->isExceptional())
4593 CaseDest =
T->getSuccessor(0);
4594 }
else if (isa<DbgInfoIntrinsic>(I)) {
4606 if (I->getParent() == CaseDest)
4608 if (
PHINode *Phi = dyn_cast<PHINode>(User))
4609 if (Phi->getIncomingBlock(
Use) == CaseDest)
4614 ConstantPool.
insert(std::make_pair(&*I, C));
4622 *CommonDest = CaseDest;
4624 if (CaseDest != *CommonDest)
4629 while (
PHINode *PHI = dyn_cast<PHINode>(I++)) {
4630 int Idx = PHI->getBasicBlockIndex(Pred);
4643 Res.push_back(std::make_pair(PHI, ConstVal));
4646 return Res.size() > 0;
4652 SwitchCaseResultVectorTy &UniqueResults,
4654 for (
auto &I : UniqueResults) {
4655 if (I.first == Result) {
4656 I.second.push_back(CaseVal);
4660 UniqueResults.push_back(
4670 SwitchCaseResultVectorTy &UniqueResults,
4674 for (
auto &I : SI->
cases()) {
4684 if (Results.size() > 1)
4690 PHI = Results[0].first;
4691 else if (PHI != Results[0].first)
4702 DefaultResults.size() == 1 ? DefaultResults.begin()->second :
nullptr;
4703 if ((!DefaultResult &&
4722 Constant *DefaultResult, Value *Condition,
4724 assert(ResultVector.size() == 2 &&
4725 "We should have exactly two unique results at this point");
4728 if (ResultVector[0].second.size() == 1 &&
4729 ResultVector[1].second.size() == 1) {
4730 ConstantInt *
const FirstCase = ResultVector[0].second[0];
4731 ConstantInt *
const SecondCase = ResultVector[1].second[0];
4733 bool DefaultCanTrigger = DefaultResult;
4734 Value *SelectValue = ResultVector[1].first;
4735 if (DefaultCanTrigger) {
4736 Value *
const ValueCompare =
4737 Builder.
CreateICmpEQ(Condition, SecondCase,
"switch.selectcmp");
4738 SelectValue = Builder.
CreateSelect(ValueCompare, ResultVector[1].first,
4739 DefaultResult,
"switch.select");
4741 Value *
const ValueCompare =
4742 Builder.
CreateICmpEQ(Condition, FirstCase,
"switch.selectcmp");
4743 return Builder.
CreateSelect(ValueCompare, ResultVector[0].first,
4744 SelectValue,
"switch.select");
4783 SwitchCaseResultVectorTy UniqueResults;
4789 if (UniqueResults.size() != 2)
4791 assert(PHI !=
nullptr &&
"PHI for value select not found");
4794 Value *SelectValue =
4807 class SwitchLookupTable {
4818 Value *BuildLookup(Value *Index,
IRBuilder<> &Builder);
4822 static bool WouldFitInRegister(
const DataLayout &DL, uint64_t TableSize,
4865 SwitchLookupTable::SwitchLookupTable(
4869 : SingleValue(nullptr), BitMap(nullptr), BitMapElementTy(nullptr),
4870 LinearOffset(nullptr), LinearMultiplier(nullptr), Array(nullptr) {
4871 assert(Values.size() &&
"Can't build lookup table without values!");
4872 assert(TableSize >= Values.size() &&
"Can't fit values in table!");
4875 SingleValue = Values.begin()->second;
4881 for (
size_t I = 0,
E = Values.size(); I !=
E; ++
I) {
4886 uint64_t Idx = (CaseVal->
getValue() - Offset->
getValue()).getLimitedValue();
4887 TableContents[Idx] = CaseRes;
4889 if (CaseRes != SingleValue)
4890 SingleValue =
nullptr;
4894 if (Values.size() < TableSize) {
4896 "Need a default value to fill the lookup table holes.");
4898 for (uint64_t I = 0; I < TableSize; ++
I) {
4899 if (!TableContents[I])
4900 TableContents[
I] = DefaultValue;
4903 if (DefaultValue != SingleValue)
4904 SingleValue =
nullptr;
4910 Kind = SingleValueKind;
4916 if (isa<IntegerType>(ValueType)) {
4917 bool LinearMappingPossible =
true;
4920 assert(TableSize >= 2 &&
"Should be a SingleValue table.");
4922 for (uint64_t I = 0; I < TableSize; ++
I) {
4927 LinearMappingPossible =
false;
4932 APInt Dist = Val - PrevVal;
4935 }
else if (Dist != DistToPrev) {
4936 LinearMappingPossible =
false;
4942 if (LinearMappingPossible) {
4943 LinearOffset = cast<ConstantInt>(TableContents[0]);
4944 LinearMultiplier = ConstantInt::get(M.
getContext(), DistToPrev);
4945 Kind = LinearMapKind;
4952 if (WouldFitInRegister(DL, TableSize, ValueType)) {
4955 for (uint64_t I = TableSize; I > 0; --
I) {
4958 if (!isa<UndefValue>(TableContents[I - 1])) {
4959 ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
4960 TableInt |= Val->
getValue().
zext(TableInt.getBitWidth());
4963 BitMap = ConstantInt::get(M.
getContext(), TableInt);
4964 BitMapElementTy =
IT;
4971 ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
4972 Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
4975 GlobalVariable::PrivateLinkage, Initializer,
4981 Value *SwitchLookupTable::BuildLookup(Value *Index,
IRBuilder<> &Builder) {
4983 case SingleValueKind:
4985 case LinearMapKind: {
4988 false,
"switch.idx.cast");
4989 if (!LinearMultiplier->isOne())
4990 Result = Builder.
CreateMul(Result, LinearMultiplier,
"switch.idx.mult");
4991 if (!LinearOffset->isZero())
4992 Result = Builder.
CreateAdd(Result, LinearOffset,
"switch.offset");
5006 ShiftAmt, ConstantInt::get(MapTy, BitMapElementTy->
getBitWidth()),
5010 Value *DownShifted =
5011 Builder.
CreateLShr(BitMap, ShiftAmt,
"switch.downshift");
5013 return Builder.
CreateTrunc(DownShifted, BitMapElementTy,
"switch.masked");
5018 uint64_t TableSize =
5020 if (TableSize > (1ULL << (IT->
getBitWidth() - 1)))
5023 "switch.tableidx.zext");
5025 Value *GEPIndices[] = {Builder.
getInt32(0), Index};
5027 GEPIndices,
"switch.gep");
5028 return Builder.
CreateLoad(GEP,
"switch.load");
5034 bool SwitchLookupTable::WouldFitInRegister(
const DataLayout &DL,
5036 Type *ElementType) {
5055 if (SI->
getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
5058 bool AllTablesFitInRegister =
true;
5059 bool HasIllegalType =
false;
5060 for (
const auto &I : ResultTypes) {
5061 Type *Ty = I.second;
5064 HasIllegalType = HasIllegalType || !TTI.
isTypeLegal(Ty);
5067 AllTablesFitInRegister =
5068 AllTablesFitInRegister &&
5069 SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
5074 if (HasIllegalType && !AllTablesFitInRegister)
5079 if (AllTablesFitInRegister)
5115 const SmallVectorImpl<std::pair<ConstantInt *, Constant *>> &Values) {
5136 DefaultValue, CmpOp1,
true);
5137 if (DefaultConst != TrueConst && DefaultConst != FalseConst)
5142 for (
auto ValuePair : Values) {
5144 ValuePair.second, CmpOp1,
true);
5145 if (!CaseConst || CaseConst == DefaultConst)
5147 assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
5148 "Expect true or false as compare result.");
5162 if (DefaultConst == FalseConst) {
5165 ++NumTableCmpReuses;
5168 Value *InvertedTableCmp = BinaryOperator::CreateXor(
5169 RangeCmp, ConstantInt::get(RangeCmp->
getType(), 1),
"inverted.cmp",
5172 ++NumTableCmpReuses;
5218 MinCaseVal = CaseVal;
5220 MaxCaseVal = CaseVal;
5225 if (!
GetCaseResults(SI, CaseVal, CI.getCaseSuccessor(), &CommonDest,
5230 for (
const auto &I : Results) {
5233 if (!ResultLists.
count(PHI))
5235 ResultLists[PHI].push_back(std::make_pair(CaseVal, Value));
5241 ResultTypes[PHI] = ResultLists[PHI][0].second->getType();
5244 uint64_t NumResults = ResultLists[PHIs[0]].
size();
5247 bool TableHasHoles = (NumResults < TableSize);
5252 bool HasDefaultResults =
5254 DefaultResultsList, DL, TTI);
5256 bool NeedMask = (TableHasHoles && !HasDefaultResults);
5261 if (!DL.fitsInLegalInteger(TableSize))
5265 for (
const auto &I : DefaultResultsList) {
5268 DefaultResults[PHI] = Result;
5287 uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
5288 assert(MaxTableSize >= TableSize &&
5289 "It is impossible for a switch to have more entries than the max "
5290 "representable value of its input integer type's size.");
5295 const bool DefaultIsReachable =
5297 const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
5300 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5306 TableIndex, ConstantInt::get(MinCaseVal->
getType(), TableSize));
5319 MaskBB->
setName(
"switch.hole_check");
5320 LookupBB = BasicBlock::Create(Mod.
getContext(),
"switch.lookup",
5325 uint64_t TableSizePowOf2 =
NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
5326 APInt MaskInt(TableSizePowOf2, 0);
5327 APInt One(TableSizePowOf2, 1);
5329 const ResultListTy &ResultList = ResultLists[PHIs[0]];
5330 for (
size_t I = 0,
E = ResultList.size(); I !=
E; ++
I) {
5331 uint64_t Idx = (ResultList[
I].first->getValue() - MinCaseVal->
getValue())
5333 MaskInt |= One << Idx;
5343 Value *Shifted = Builder.
CreateLShr(TableMask, MaskIndex,
"switch.shifted");
5345 Shifted, Type::getInt1Ty(Mod.
getContext()),
"switch.lobit");
5352 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
5359 bool ReturnedEarly =
false;
5360 for (
size_t I = 0,
E = PHIs.size(); I !=
E; ++
I) {
5362 const ResultListTy &ResultList = ResultLists[PHI];
5365 Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI];
5366 SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL);
5368 Value *Result = Table.BuildLookup(TableIndex, Builder);
5375 ReturnedEarly =
true;
5381 if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
5407 ++NumLookupTablesHoles;
5413 uint64_t Diff = (uint64_t)Values.
back() - (uint64_t)Values.
front();
5414 uint64_t Range = Diff + 1;
5415 uint64_t NumCases = Values.
size();
5417 uint64_t MinDensity = 40;
5419 return NumCases * 100 >= Range * MinDensity;
5434 if (CondTy->getIntegerBitWidth() > 64 ||
5447 for (
auto &C : SI->
cases())
5449 std::sort(Values.
begin(), Values.
end());
5456 int64_t Base = Values[0];
5457 for (
auto &V : Values)
5464 for (
auto &V : Values)
5479 unsigned Shift =
Log2_64(GCD);
5480 for (
auto &V : Values)
5481 V = (int64_t)((uint64_t)V >> Shift);
5499 auto *ShiftC = ConstantInt::get(Ty, Shift);
5501 auto *LShr = Builder.
CreateLShr(Sub, ShiftC);
5502 auto *Shl = Builder.
CreateShl(Sub, Ty->getBitWidth() - Shift);
5503 auto *Rot = Builder.
CreateOr(LShr, Shl);
5508 auto *Orig = C.getCaseValue();
5509 auto Sub = Orig->
getValue() -
APInt(Ty->getBitWidth(), Base);
5511 cast<ConstantInt>(ConstantInt::get(Ty, Sub.lshr(ShiftC->getValue()))));
5519 if (isValueEqualityComparison(SI)) {
5523 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
5524 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5529 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5535 while (isa<DbgInfoIntrinsic>(BBI))
5538 if (FoldValueComparisonIntoPredecessors(SI, Builder))
5539 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5544 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5548 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5551 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5554 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5557 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5560 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5567 bool Changed =
false;
5598 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5630 if (isa<PHINode>(*Succ->begin()))
5634 if (BB == OtherPred)
5640 for (++I; isa<DbgInfoIntrinsic>(
I); ++
I) {
5653 "unexpected successor");
5659 for (
auto I = OtherPred->begin(),
E = OtherPred->end(); I !=
E;) {
5662 if (isa<DbgInfoIntrinsic>(Inst))
5669 Succ->removePredecessor(BB);
5680 bool SimplifyCFGOpt::SimplifyUncondBranch(
BranchInst *BI,
5695 (!LoopHeaders || !LoopHeaders->count(BB)) &&
5701 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(I))
5702 if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
5703 for (++I; isa<DbgInfoIntrinsic>(
I); ++
I)
5705 if (I->isTerminator() &&
5707 BonusInstThreshold, AC))
5714 for (++I; isa<DbgInfoIntrinsic>(
I); ++
I) {
5725 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5733 if (!PPred || (PredPred && PredPred != PPred))
5744 if (isValueEqualityComparison(BI)) {
5749 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
5750 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5756 while (isa<DbgInfoIntrinsic>(I))
5759 if (FoldValueComparisonIntoPredecessors(BI, Builder))
5760 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5761 }
else if (&*I == cast<Instruction>(BI->
getCondition())) {
5764 while (isa<DbgInfoIntrinsic>(I))
5766 if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
5767 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5779 auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator());
5794 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5803 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5812 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5820 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5829 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5837 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5841 if (
BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
5844 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5849 if (
BranchInst *PBI = dyn_cast<BranchInst>(PrevBB->getTerminator()))
5852 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
5876 if (i == I->
getParent()->
end() || i->mayHaveSideEffects())
5881 if (
GEP->getPointerOperand() ==
I)
5889 if (
LoadInst *LI = dyn_cast<LoadInst>(Use))
5890 if (!LI->isVolatile())
5891 return LI->getPointerAddressSpace() == 0;
5894 if (
StoreInst *SI = dyn_cast<StoreInst>(Use))
5895 if (!SI->isVolatile())
5896 return SI->getPointerAddressSpace() == 0 &&
5897 SI->getPointerOperand() ==
I;
5901 return CS.getCalledValue() ==
I;
5915 if (
BranchInst *BI = dyn_cast<BranchInst>(T)) {
5934 bool Changed =
false;
5943 DEBUG(
dbgs() <<
"Removing BB: \n" << *BB);
5970 if (PN->getNumIncomingValues() == 2)
5976 if (SimplifyUncondBranch(BI, Builder))
5979 if (SimplifyCondBranch(BI, Builder))
5983 if (SimplifyReturn(RI, Builder))
5986 if (SimplifyResume(RI, Builder))
5990 if (SimplifyCleanupReturn(RI))
5993 if (SimplifySwitch(SI, Builder))
5997 if (SimplifyUnreachable(UI))
6001 if (SimplifyIndirectBr(IBI))
6017 BonusInstThreshold, AC, LoopHeaders)
const Use & getOperandUse(unsigned i) const
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT,"arm-default-it","Generate IT block based on arch"), clEnumValN(RestrictedIT,"arm-restrict-it","Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT,"arm-no-restrict-it","Allow IT blocks based on ARMv7")))
Return a value (possibly void), from a function.
Value * getValueOperand()
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void computeKnownBits(const Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
BasicBlock * getUniqueSuccessor()
Return the successor of this block if it has a unique successor.
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
This class is the base class for the comparison instructions.
iterator_range< use_iterator > uses()
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
uint64_t getZExtValue() const
Get zero extended value.
DiagnosticInfoOptimizationBase::Argument NV
uint64_t GreatestCommonDivisor64(uint64_t A, uint64_t B)
GreatestCommonDivisor64 - Return the greatest common divisor of the two values using Euclid's algorit...
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, unsigned BonusInstThreshold, AssumptionCache *AC=nullptr, SmallPtrSetImpl< BasicBlock * > *LoopHeaders=nullptr)
This function is used to do simplification of a CFG.
STATISTIC(NumFunctions,"Total number of functions")
iterator erase(iterator where)
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the specified block at the specified instruction - everything before SplitPt stays in Old and e...
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
void swapSuccessors()
Swap the successors of this branch instruction.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
A Module instance is used to store all the information related to an LLVM module. ...
void dropUnknownNonDebugMetadata(ArrayRef< unsigned > KnownIDs)
Drop all unknown metadata except for debug locations.
static cl::opt< bool > MergeCondStoresAggressively("simplifycfg-merge-cond-stores-aggressively", cl::Hidden, cl::init(false), cl::desc("When merging conditional stores, do so even if the resultant ""basic blocks are unlikely to be if-converted as a result"))
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
unsigned getSuccessorIndex() const
Returns TerminatorInst's successor index for current case successor.
const T & front() const
front - Get the first element.
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
static bool removeUndefIntroducingPredecessor(BasicBlock *BB)
If BB has an incoming value that will always trigger undefined behavior (eg.
unsigned getNumOperands() const
unsigned getNumOperands() const
Return number of MDNode operands.
void DeleteDeadBlock(BasicBlock *BB)
Delete the specified block, which must have no predecessors.
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
Value * getValue() const
Convenience accessor.
This class represents a function call, abstracting a target machine's calling convention.
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc.
static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2, Instruction *I1, Instruction *I2)
static cl::opt< unsigned > PHINodeFoldingThreshold("phi-node-folding-threshold", cl::Hidden, cl::init(2), cl::desc("Control the amount of phi node folding to perform (default = 2)"))
static bool mergeCleanupPad(CleanupReturnInst *RI)
CaseIt case_begin()
Returns a read/write iterator that points to the first case in the SwitchInst.
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
A cache of .assume calls within a function.
Function Alias Analysis Results
static void GetBranchWeights(TerminatorInst *TI, SmallVectorImpl< uint64_t > &Weights)
Get Weights of a given TerminatorInst, the default weight is at the front of the vector.
static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI, const DataLayout &DL)
If we have a conditional branch as a predecessor of another block, this function tries to simplify it...
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
bool isTokenTy() const
Return true if this is 'token'.
static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
const Function * getParent() const
Return the enclosing method, or null if none.
static bool GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest, BasicBlock **CommonDest, SmallVectorImpl< std::pair< PHINode *, Constant * >> &Res, const DataLayout &DL, const TargetTransformInfo &TTI)
Try to determine the resulting constant values in phi nodes at the common destination basic block...
static unsigned ComputeSpeculationCost(const Instruction *I, const TargetTransformInfo &TTI)
const Instruction & front() const
static StoreInst * findUniqueStoreInBlocks(BasicBlock *BB1, BasicBlock *BB2)
An instruction for reading from memory.
CleanupPadInst * getCleanupPad() const
Convenience accessor.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
static Constant * getTrue(Type *Ty)
For a boolean type, or a vector of boolean type, return true, or a vector with every element true...
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
bool isSequential() const
std::size_t countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
static bool ValidLookupTableConstant(Constant *C, const TargetTransformInfo &TTI)
Return true if the backend will be able to handle initializing an array of constants like C...
ConstantInt * findCaseDest(BasicBlock *BB)
Finds the unique case value for a given successor.
static bool extractPredSuccWeights(BranchInst *PBI, BranchInst *BI, uint64_t &PredTrueWeight, uint64_t &PredFalseWeight, uint64_t &SuccTrueWeight, uint64_t &SuccFalseWeight)
Return true if either PBI or BI has branch weight available, and store the weights in {Pred|Succ}{Tru...
uint64_t getArrayNumElements() const
static ConstantInt * GetConstantInt(Value *V, const DataLayout &DL)
Extract ConstantInt from value, looking through IntToPtr and PointerNullValue.
StringRef getName() const
Return a constant reference to the value's name.
static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I)
Check if passing a value to an instruction will cause undefined behavior.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Value * CreateNot(Value *V, const Twine &Name="")
iterator begin()
Instruction iterator methods.
bool isBundleOperand(Value::const_user_iterator UI) const
Determine whether the passed iterator points to a bundle operand.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Instruction * getFirstNonPHIOrDbg()
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
static bool isProfitableToFoldUnconditional(BranchInst *SI1, BranchInst *SI2, Instruction *Cond, SmallVectorImpl< PHINode * > &PhiNodes)
Return true if it is safe and profitable to merge these two terminator instructions together...
The address of a basic block.
bool match(Val *V, const Pattern &P)
LLVM_NODISCARD bool empty() const
bool isUnconditional() const
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
static bool SwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder, AssumptionCache *AC, const DataLayout &DL, const TargetTransformInfo &TTI)
If the switch is only used to initialize one or more phi nodes in a common successor block with only ...
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
static bool SinkThenElseCodeToEnd(BranchInst *BI1)
Given an unconditional branch that goes to BBEnd, check whether BBEnd has only two predecessors and t...
This class represents the LLVM 'select' instruction.
static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB, BasicBlock *QTB, BasicBlock *QFB, BasicBlock *PostBB, Value *Address, bool InvertPCond, bool InvertQCond)
bool isIdenticalTo(const Instruction *I) const
Return true if the specified instruction is exactly identical to the current one. ...
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
const APInt & getValue() const
Return the constant as an APInt value reference.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
A Use represents the edge between a Value definition and its users.
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond, BasicBlock *TrueBB, BasicBlock *FalseBB, uint32_t TrueWeight, uint32_t FalseWeight)
static int ConstantIntSortPredicate(ConstantInt *const *P1, ConstantInt *const *P2)
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' instruction.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static const DILocation * getMergedLocation(const DILocation *LocA, const DILocation *LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
void setName(const Twine &Name)
Change the name of the value.
static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB)
Return true if we can thread a branch across this block.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
static Value * isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB, BasicBlock *StoreBB, BasicBlock *EndBB)
Determine if we can hoist sink a sole store instruction out of a conditional block.
BasicBlock * getDestination(unsigned i)
Return the specified destination.
This file implements a class to represent arbitrary precision integral constant values and operations...
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
This class represents a cast from a pointer to an integer.
LLVM_NODISCARD bool empty() const
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
void assign(size_type NumElts, const T &Elt)
bool isDLLImportDependent() const
Return true if the value is dependent on a dllimport variable.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
void setUnwindDest(BasicBlock *B)
A constant value that is initialized with an expression using other constant values.
static bool isSwitchDense(ArrayRef< int64_t > Values)
static bool SimplifyCondBranchToTwoReturns(BranchInst *BI, IRBuilder<> &Builder)
If we found a conditional branch that goes to two returning blocks, try to merge them together into o...
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static void MapCaseToResult(ConstantInt *CaseVal, SwitchCaseResultVectorTy &UniqueResults, Constant *Result)
Class to represent array types.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
bool sgt(const APInt &RHS) const
Signed greather than comparison.
BasicBlock * getSuccessor(unsigned i) const
This class represents a no-op cast from one type to another.
static cl::opt< bool > SinkCommon("simplifycfg-sink-common", cl::Hidden, cl::init(true), cl::desc("Sink common instructions down to the end block"))
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static Value * ConvertTwoCaseSwitch(const SwitchCaseResultVectorTy &ResultVector, Constant *DefaultResult, Value *Condition, IRBuilder<> &Builder)
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
static void EliminateBlockCases(BasicBlock *BB, std::vector< ValueEqualityComparisonCase > &Cases)
Given a vector of bb/value pairs, remove any entries in the list that match the specified block...
An instruction for storing to memory.
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static cl::opt< bool > MergeCondStores("simplifycfg-merge-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores even if an unconditional store does not ""precede - hoist multiple conditional stores into a single ""predicated store"))
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
LLVM_NODISCARD char front() const
front - Get the first character in the string.
bool EliminateDuplicatePHINodes(BasicBlock *BB)
Check for and eliminate duplicate PHI nodes in this block.
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
void takeName(Value *V)
Transfer the name from V to this value.
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
unsigned getMinSignedBits() const
Get the minimum bit size for this signed APInt.
size_t size() const
size - Get the array size.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
BasicBlock * getNormalDest() const
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
bool mayReadOrWriteMemory() const
Return true if this instruction may read or write memory.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Interval::succ_iterator succ_end(Interval *I)
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
static cl::opt< bool > HoistCondStores("simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores if an unconditional store precedes"))
static void reuseTableCompare(User *PhiUser, BasicBlock *PhiBlock, BranchInst *RangeCheckBranch, Constant *DefaultValue, const SmallVectorImpl< std::pair< ConstantInt *, Constant * >> &Values)
Try to reuse the switch table index compare.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
LoadInst * CreateLoad(Value *Ptr, const char *Name)
initializer< Ty > init(const Ty &Val)
bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold=1)
If this basic block is ONLY a setcc and a branch, and if a predecessor branches to us and one of our ...
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI)
If the switch is only used to initialize one or more phi nodes in a common successor block with diffe...
The landingpad instruction holds all of the information necessary to generate correct exception handl...
Subclasses of this class are all able to terminate a basic block.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
unsigned getCaseIndex() const
Returns number of current case.
static Value * ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB, Value *AlternativeV=nullptr)
bool isIdenticalToWhenDefined(const Instruction *I) const
This is like isIdenticalTo, except that it ignores the SubclassOptionalData flags, which may specify conditions under which the instruction's result is undefined.
void set_intersect(S1Ty &S1, const S2Ty &S2)
set_intersect(A, B) - Compute A := A ^ B Identical to set_intersection, except that it works on set<&g...
static bool HasBranchWeights(const Instruction *I)
void setAAMetadata(const AAMDNodes &N)
Sets the metadata on this instruction from the AAMDNodes structure.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB)
BB is known to contain an unconditional branch, and contains no instructions other than PHI nodes...
UnreachableInst * CreateUnreachable()
Conditional or Unconditional Branch instruction.
static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder)
Turn a switch with two reachable destinations into an integer range comparison and branch...
This function has undefined behavior.
This is an important base class in LLVM.
const Value * getCondition() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Resume the propagation of an exception.
int64_t getSExtValue() const
Get sign extended value.
static bool CasesAreContiguous(SmallVectorImpl< ConstantInt * > &Cases)
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Indirect Branch Instruction.
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const
Retrieve the raw weight values of a conditional branch or select.
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
constexpr bool isPowerOf2_64(uint64_t Value)
isPowerOf2_64 - This function returns true if the argument is a power of two 0 (64 bit edition...
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
void splice(iterator where, iplist_impl &L2)
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
const InstListType & getInstList() const
Return the underlying instruction list container.
static bool canSinkInstructions(ArrayRef< Instruction * > Insts, DenseMap< Instruction *, SmallVector< Value *, 4 >> &PHIOperands)
This instruction compares its operands according to the predicate given to the constructor.
static const unsigned End
User * getUser() const
Returns the User that contains this Use.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
static bool EliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC, const DataLayout &DL)
Compute masked bits for the condition of a switch and use it to remove dead cases.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
self_iterator getIterator()
unsigned getIntegerBitWidth() const
Class to represent integer types.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Predicate getPredicate() const
Return the predicate for this instruction.
bool pred_empty(const BasicBlock *BB)
static bool checkCSEInPredecessor(Instruction *Inst, BasicBlock *PB)
Return true if the given instruction is available in its predecessor block.
static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI, const DataLayout &DL)
Given a BB that starts with the specified two-entry PHI node, see if we can eliminate it...
Optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool InvertAPred=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if RHS is known to be implied true by LHS.
LLVM_NODISCARD bool empty() const
static cl::opt< bool > SpeculateOneExpensiveInst("speculate-one-expensive-inst", cl::Hidden, cl::init(true), cl::desc("Allow exactly one expensive instruction to be speculatively ""executed"))
bool isEmptySet() const
Return true if this set contains no members.
static Constant * ConstantFold(Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool)
Try to fold instruction I into a constant.
BlockMass operator*(BlockMass L, BranchProbability R)
bool isPointerTy() const
True if this is an instance of PointerType.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
uint64_t NextPowerOf2(uint64_t A)
NextPowerOf2 - Returns the next power of two (in 64-bits) that is strictly greater than A...
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
If the specified value is a trivially dead instruction, delete it.
iterator erase(const_iterator CI)
Value(Type *Ty, unsigned scid)
LLVMContext & getContext() const
All values hold a context through their type.
static bool sinkLastInstruction(ArrayRef< BasicBlock * > Blocks)
const Value * getTrueValue() const
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldCompareInstOperands - Attempt to constant fold a compare instruction (icmp/fcmp) with the...
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
const T & back() const
back - Get the last element.
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
bool isTerminator() const
BasicBlock * getUnwindDest() const
bool isConditional() const
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
const MDOperand & getOperand(unsigned I) const
A SetVector that performs no allocations if smaller than a certain size.
Iterator for intrusive lists based on ilist_node.
static cl::opt< unsigned > MaxSpeculationDepth("max-speculation-depth", cl::Hidden, cl::init(10), cl::desc("Limit maximum recursion depth when calculating costs of ""speculatively executed instructions"))
BasicBlock * getUnwindDest() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
unsigned countPopulation(T Value)
Count the number of set bits in a value.
bool fitsInLegalInteger(unsigned Width) const
Returns true if the specified type fits in a native integer type supported by the CPU...
This is the shared class of boolean and integer constants.
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return the number of times the sign bit of the register is replicated into the other bits...
bool slt(const APInt &RHS) const
Signed less than comparison.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
bool cannotDuplicate() const
Determine if the call cannot be duplicated.
unsigned getNumSuccessors() const
APInt getSetSize() const
Return the number of elements in this set.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
static bool SimplifyBranchOnICmpChain(BranchInst *BI, IRBuilder<> &Builder, const DataLayout &DL)
The specified branch is a conditional branch.
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
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.
Type * getType() const
All values are typed, get the type of this value.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
This class represents a range of values.
This is the common base class for debug info intrinsics.
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
Check whether BB is the merge point of a if-region.
TerminatorInst * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
const APInt & getLower() const
Return the lower value for this range.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
INITIALIZE_PASS(HexagonGenMux,"hexagon-mux","Hexagon generate mux instructions", false, false) void HexagonGenMux I isValid()
LLVM_NODISCARD T pop_back_val()
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
BasicBlockTy * getCaseSuccessor()
Resolves successor for current case.
BasicBlock * getBasicBlock() const
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.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
const BasicBlock & getEntryBlock() const
bool isConvergent() const
Determine if the call is convergent.
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
CaseIt findCaseValue(const ConstantInt *C)
Search all of the case values for the specified constant.
static ConstantInt * getTrue(LLVMContext &Context)
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
static void EraseTerminatorInstAndDCECond(TerminatorInst *TI)
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
bool isAllOnesValue() const
Return true if this is the value that would be returned by getAllOnesValue.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
static Constant * getFalse(Type *Ty)
For a boolean type, or a vector of boolean type, return false, or a vector with every element false...
static bool HoistThenElseCodeToIf(BranchInst *BI, const TargetTransformInfo &TTI)
Given a conditional branch that goes to BB1 and BB2, hoist any common code in the two blocks up into ...
BasicBlock * getSingleSuccessor()
Return the successor of this block if it has a single successor.
static cl::opt< bool > DupRet("simplifycfg-dup-ret", cl::Hidden, cl::init(false), cl::desc("Duplicate return instructions into unconditional branches"))
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
True if this is an instance of IntegerType.
iterator_range< user_iterator > users()
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM...
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
void FoldSingleEntryPHINodes(BasicBlock *BB, MemoryDependenceResults *MemDep=nullptr)
We know that BB has one predecessor.
Value * getCondition() const
iterator insert(iterator I, T &&Elt)
void removeUnwindEdge(BasicBlock *BB)
Replace 'BB's terminator with one that does not have an unwind successor block.
If this flag is set, the remapper ignores missing function-local entries (Argument, Instruction, BasicBlock) that are not in the value map.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
SwitchInst * CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases=10, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a switch instruction with the specified value, default dest, and with a hint for the number of...
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
BasicBlock * getSuccessor(unsigned idx) const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Value * getCondition() const
iterator insert(iterator where, pointer New)
BasicBlock * getDefaultDest() const
bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemoryDependenceResults *MemDep=nullptr)
Attempts to merge a block into its predecessor, if possible.
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
Fills the AAMDNodes structure with AA metadata from this instruction.
ImmutableCallSite - establish a view to a call site for examination.
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
static bool removeEmptyCleanup(CleanupReturnInst *RI)
iterator find(const KeyT &Val)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred)
This method duplicates the specified return instruction into a predecessor which ends in an unconditi...
static bool ValuesOverlap(std::vector< ValueEqualityComparisonCase > &C1, std::vector< ValueEqualityComparisonCase > &C2)
Return true if there are any keys in C1 that exist in C2 as well.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
void removeCase(CaseIt i)
This method removes the specified case and its successor from the switch instruction.
void combineMetadataForCSE(Instruction *K, const Instruction *J)
Combine the metadata of two instructions so that K can replace J.
CaseIt case_default()
Returns an iterator that points to the default case.
static void RemoveSwitchAfterSelectConversion(SwitchInst *SI, PHINode *PHI, Value *SelectValue, IRBuilder<> &Builder)
static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *ThenBB, const TargetTransformInfo &TTI)
Speculate a conditional basic block flattening the CFG.
static bool InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest, SwitchCaseResultVectorTy &UniqueResults, Constant *&DefaultResult, const DataLayout &DL, const TargetTransformInfo &TTI)
static bool ShouldBuildLookupTable(SwitchInst *SI, uint64_t TableSize, const TargetTransformInfo &TTI, const DataLayout &DL, const SmallDenseMap< PHINode *, Type * > &ResultTypes)
Determine whether a lookup table should be built for this switch, based on the number of cases...
unsigned getNumCases() const
Return the number of 'cases' in this switch instruction, excluding the default case.
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
void setCondition(Value *V)
static BasicBlock * allPredecessorsComeFromSameSource(BasicBlock *BB)
static bool FoldCondBranchOnPHI(BranchInst *BI, const DataLayout &DL)
If we have a conditional branch on a PHI node value that is defined in the same block as the branch a...
static bool ReduceSwitchRange(SwitchInst *SI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI)
const APInt & getUpper() const
Return the upper value for this range.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Constant * LookupConstant(Value *V, const SmallDenseMap< Value *, Constant * > &ConstantPool)
If V is a Constant, return it.
user_iterator user_begin()
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands...
LLVMContext & getContext() const
Get the context in which this basic block lives.
static bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx)
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
bool operator<(int64_t V1, const APSInt &V2)
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
static bool ForwardSwitchConditionToPHI(SwitchInst *SI)
Try to forward the condition of a switch instruction to a phi node dominated by the switch...
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
succ_range successors(BasicBlock *BB)
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDefaultDest(BasicBlock *DefaultCase)
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
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.
static const Function * getParent(const Value *V)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
static PHINode * FindPHIForConditionForwarding(ConstantInt *CaseValue, BasicBlock *BB, int *PhiIndex)
If BB would be eligible for simplification by TryToSimplifyUncondBranchFromEmptyBlock (i...
const Value * getFalseValue() const
APInt zext(unsigned width) const
Zero extend to a new width.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
See if we can compute a simplified version of this instruction.
bool operator==(uint64_t V1, const APInt &V2)
static void FitWeights(MutableArrayRef< uint64_t > Weights)
Keep halving the weights until all can fit in uint32_t.
iterator getFirstInsertionPt()
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
unsigned getNumUses() const
This method computes the number of uses of this Value.
void setIncomingValue(unsigned i, Value *V)
static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2, SmallSetVector< BasicBlock *, 4 > *FailBlocks=nullptr)
Return true if it is safe to merge these two terminator instructions together.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI)
static bool TryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI, IRBuilder<> &Builder, const DataLayout &DL, const TargetTransformInfo &TTI, unsigned BonusInstThreshold, AssumptionCache *AC)
This is called when we find an icmp instruction (a seteq/setne with a constant) as the only instructi...
bool isThreadDependent() const
Return true if the value can vary between threads.
Value * getPointerOperand()
static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred, BasicBlock *ExistPred)
Update PHI nodes in Succ to indicate that there will now be entries in it from the 'NewPred' block...
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
void combineMetadata(Instruction *K, const Instruction *J, ArrayRef< unsigned > KnownIDs)
Combine the metadata of two instructions so that K can replace J.
unsigned getNumDestinations() const
return the number of possible destinations in this indirectbr instruction.
void removeDestination(unsigned i)
This method removes the specified successor from the indirectbr instruction.
unsigned Log2_64(uint64_t Value)
Log2_64 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
const BasicBlock * getParent() const
InstListType::iterator iterator
Instruction iterators...
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
static bool DominatesMergePoint(Value *V, BasicBlock *BB, SmallPtrSetImpl< Instruction * > *AggressiveInsts, unsigned &CostRemaining, const TargetTransformInfo &TTI, unsigned Depth=0)
If we have a merge point of an "if condition" as accepted above, return true if the specified value d...
A wrapper class for inspecting calls to intrinsic functions.
LLVMContext & getContext() const
Get the global data context.
bool isVoidTy() const
Return true if this is 'void'.
bool isMetadataTy() const
Return true if this is 'metadata'.
gep_type_iterator gep_type_begin(const User *GEP)
static bool TryToMergeLandingPad(LandingPadInst *LPad, BranchInst *BI, BasicBlock *BB)
Given an block with only a single landing pad and a unconditional branch try to find another basic bl...