52 using namespace PatternMatch;
54 #define DEBUG_TYPE "simplifycfg"
62 cl::desc(
"Control the amount of phi node folding to perform (default = 2)"));
66 cl::desc(
"Duplicate return instructions into unconditional branches"));
70 cl::desc(
"Sink common instructions down to the end block"));
74 cl::desc(
"Hoist conditional stores if an unconditional store precedes"));
76 STATISTIC(NumBitMaps,
"Number of switch instructions turned into bitmaps");
77 STATISTIC(NumLinearMaps,
"Number of switch instructions turned into linear mapping");
78 STATISTIC(NumLookupTables,
"Number of switch instructions turned into lookup tables");
79 STATISTIC(NumLookupTablesHoles,
"Number of switch instructions turned into lookup tables (holes checked)");
80 STATISTIC(NumTableCmpReuses,
"Number of reused switch table lookup compares");
81 STATISTIC(NumSinkCommons,
"Number of common instructions sunk down to the end block");
82 STATISTIC(NumSpeculations,
"Number of speculative executed instructions");
89 SwitchCaseResultVectorTy;
96 struct ValueEqualityComparisonCase {
101 : Value(Value), Dest(Dest) {}
103 bool operator<(ValueEqualityComparisonCase RHS)
const {
105 return Value < RHS.Value;
111 class SimplifyCFGOpt {
114 unsigned BonusInstThreshold;
118 std::vector<ValueEqualityComparisonCase> &Cases);
119 bool SimplifyEqualityComparisonWithOnlyPredecessor(
TerminatorInst *TI,
136 : TTI(TTI), DL(DL), BonusInstThreshold(BonusInstThreshold), AC(AC) {}
144 if (SI1 == SI2)
return false;
156 isa<PHINode>(BBI); ++BBI) {
157 PHINode *PN = cast<PHINode>(BBI);
173 if (SI1 == SI2)
return false;
182 if (!Ci2)
return false;
183 if (!(Cond->
getOperand(0) == Ci2->getOperand(0) &&
185 !(Cond->
getOperand(0) == Ci2->getOperand(1) &&
195 isa<PHINode>(BBI); ++BBI) {
196 PHINode *PN = cast<PHINode>(BBI);
211 if (!isa<PHINode>(Succ->
begin()))
return;
226 "Instruction is not safe to speculatively execute!");
248 unsigned &CostRemaining,
263 if (PBB == BB)
return false;
274 if (!AggressiveInsts)
return false;
277 if (AggressiveInsts->
count(I))
return true;
287 if (Cost > CostRemaining)
290 CostRemaining -= Cost;
298 AggressiveInsts->
insert(I);
315 if (isa<ConstantPointerNull>(V))
320 if (CE->getOpcode() == Instruction::IntToPtr)
321 if (
ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
326 return cast<ConstantInt>
344 struct ConstantComparesGatherer {
353 : DL(DL), CompValue(nullptr), Extra(nullptr), UsedICmps(0) {
358 ConstantComparesGatherer(
const ConstantComparesGatherer &) =
delete;
359 ConstantComparesGatherer &
360 operator=(
const ConstantComparesGatherer &) =
delete;
366 bool setValueOnce(
Value *NewVal) {
367 if(CompValue && CompValue != NewVal)
return false;
369 return (CompValue !=
nullptr);
383 if (!((ICI = dyn_cast<ICmpInst>(I)) &&
400 if(!setValueOnce(RHSVal))
429 CandidateVal = RHSVal;
444 if(!setValueOnce(CandidateVal))
461 void gather(
Value *V) {
471 while(!DFT.
empty()) {
483 if (matchInstruction(I, isEQ))
508 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
509 if (BI->isConditional())
510 Cond = dyn_cast<Instruction>(BI->getCondition());
523 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
526 if (
SI->getNumSuccessors()*std::distance(
pred_begin(
SI->getParent()),
528 CV =
SI->getCondition();
529 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(TI))
530 if (BI->isConditional() && BI->getCondition()->hasOneUse())
531 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
539 Value *Ptr = PTII->getPointerOperand();
540 if (PTII->getType() ==
DL.getIntPtrType(Ptr->
getType()))
551 std::vector<ValueEqualityComparisonCase>
553 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
554 Cases.reserve(
SI->getNumCases());
556 Cases.push_back(ValueEqualityComparisonCase(i.getCaseValue(),
557 i.getCaseSuccessor()));
558 return SI->getDefaultDest();
564 Cases.push_back(ValueEqualityComparisonCase(
GetConstantInt(ICI->getOperand(1),
574 std::vector<ValueEqualityComparisonCase> &Cases) {
575 Cases.erase(
std::remove(Cases.begin(), Cases.end(), BB), Cases.end());
581 std::vector<ValueEqualityComparisonCase > &C2) {
582 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *
V2 = &C2;
585 if (V1->size() > V2->size())
588 if (V1->size() == 0)
return false;
589 if (V1->size() == 1) {
592 for (
unsigned i = 0, e = V2->size(); i != e; ++i)
593 if (TheVal == (*V2)[i].
Value)
600 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
601 while (i1 != e1 && i2 != e2) {
604 if ((*V1)[i1].Value < (*V2)[i2].Value)
617 bool SimplifyCFGOpt::
622 if (!PredVal)
return false;
624 Value *ThisVal = isValueEqualityComparison(TI);
625 assert(ThisVal &&
"This isn't a value comparison!!");
626 if (ThisVal != PredVal)
return false;
632 std::vector<ValueEqualityComparisonCase> PredCases;
638 std::vector<ValueEqualityComparisonCase> ThisCases;
639 BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
651 if (isa<BranchInst>(TI)) {
654 assert(ThisCases.size() == 1 &&
"Branch can only have one case!");
660 ThisCases[0].Dest->removePredecessor(TI->
getParent());
663 <<
"Through successor TI: " << *TI <<
"Leaving: " << *NI <<
"\n");
672 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
673 DeadCases.
insert(PredCases[i].Value);
676 <<
"Through successor TI: " << *TI);
690 if (DeadCases.count(i.getCaseValue())) {
692 std::swap(Weights[i.getCaseIndex()+1], Weights.back());
695 i.getCaseSuccessor()->removePredecessor(TI->
getParent());
699 if (HasWeight && Weights.size() >= 2)
702 createBranchWeights(Weights));
704 DEBUG(
dbgs() <<
"Leaving: " << *TI <<
"\n");
712 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
713 if (PredCases[i].Dest == TIBB) {
716 TIV = PredCases[i].
Value;
718 assert(TIV &&
"No edge from pred to succ?");
723 for (
unsigned i = 0, e = ThisCases.size(); i != e; ++i)
724 if (ThisCases[i].
Value == TIV) {
725 TheRealDest = ThisCases[i].Dest;
730 if (!TheRealDest) TheRealDest = ThisDef;
735 if (*SI != CheckEdge)
745 <<
"Through successor TI: " << *TI <<
"Leaving: " << *NI <<
"\n");
755 struct ConstantIntOrdering {
777 return MDS->getString().equals(
"branch_weights");
797 if (
BranchInst* BI = dyn_cast<BranchInst>(TI)) {
798 assert(Weights.
size() == 2);
807 uint64_t
Max = *std::max_element(Weights.
begin(), Weights.
end());
808 if (Max > UINT_MAX) {
810 for (uint64_t &I : Weights)
819 bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(
TerminatorInst *TI,
822 Value *CV = isValueEqualityComparison(TI);
823 assert(CV &&
"Not a comparison?");
824 bool Changed =
false;
827 while (!Preds.empty()) {
832 Value *PCV = isValueEqualityComparison(PTI);
836 std::vector<ValueEqualityComparisonCase> BBCases;
837 BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
839 std::vector<ValueEqualityComparisonCase> PredCases;
840 BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
852 if (PredHasWeights) {
855 if (Weights.
size() != 1 + PredCases.size())
856 PredHasWeights = SuccHasWeights =
false;
857 }
else if (SuccHasWeights)
861 Weights.
assign(1 + PredCases.size(), 1);
864 if (SuccHasWeights) {
867 if (SuccWeights.
size() != 1 + BBCases.size())
868 PredHasWeights = SuccHasWeights =
false;
869 }
else if (PredHasWeights)
870 SuccWeights.
assign(1 + BBCases.size(), 1);
872 if (PredDefault == BB) {
875 std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
876 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
877 if (PredCases[i].Dest != BB)
878 PTIHandled.insert(PredCases[i].
Value);
881 std::swap(PredCases[i], PredCases.back());
883 if (PredHasWeights || SuccHasWeights) {
885 Weights[0] += Weights[i+1];
890 PredCases.pop_back();
895 if (PredDefault != BBDefault) {
897 PredDefault = BBDefault;
901 unsigned CasesFromPred = Weights.
size();
902 uint64_t ValidTotalSuccWeight = 0;
903 for (
unsigned i = 0, e = BBCases.size(); i != e; ++i)
904 if (!PTIHandled.count(BBCases[i].Value) &&
905 BBCases[i].Dest != BBDefault) {
906 PredCases.push_back(BBCases[i]);
907 NewSuccessors.
push_back(BBCases[i].Dest);
908 if (SuccHasWeights || PredHasWeights) {
912 Weights.
push_back(Weights[0] * SuccWeights[i+1]);
913 ValidTotalSuccWeight += SuccWeights[i+1];
917 if (SuccHasWeights || PredHasWeights) {
918 ValidTotalSuccWeight += SuccWeights[0];
920 for (
unsigned i = 1; i < CasesFromPred; ++i)
921 Weights[i] *= ValidTotalSuccWeight;
923 Weights[0] *= SuccWeights[0];
929 std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
930 std::map<ConstantInt*, uint64_t> WeightsForHandled;
931 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
932 if (PredCases[i].Dest == BB) {
933 PTIHandled.
insert(PredCases[i].Value);
935 if (PredHasWeights || SuccHasWeights) {
936 WeightsForHandled[PredCases[i].Value] = Weights[i+1];
941 std::swap(PredCases[i], PredCases.back());
942 PredCases.pop_back();
948 for (
unsigned i = 0, e = BBCases.size(); i != e; ++i)
949 if (PTIHandled.count(BBCases[i].Value)) {
951 if (PredHasWeights || SuccHasWeights)
952 Weights.
push_back(WeightsForHandled[BBCases[i].Value]);
953 PredCases.push_back(BBCases[i]);
954 NewSuccessors.
push_back(BBCases[i].Dest);
955 PTIHandled.erase(BBCases[i].Value);
960 for (std::set<ConstantInt*, ConstantIntOrdering>::iterator I =
962 E = PTIHandled.end(); I != E; ++
I) {
963 if (PredHasWeights || SuccHasWeights)
964 Weights.
push_back(WeightsForHandled[*I]);
965 PredCases.push_back(ValueEqualityComparisonCase(*I, BBDefault));
973 for (
unsigned i = 0, e = NewSuccessors.
size(); i != e; ++i)
987 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
988 NewSI->
addCase(PredCases[i].Value, PredCases[i].Dest);
990 if (PredHasWeights || SuccHasWeights) {
998 createBranchWeights(MDWeights));
1009 if (!InfLoopBlock) {
1036 if (BB1V != BB2V && (BB1V==I1 || BB2V==I2)) {
1067 while (isa<DbgInfoIntrinsic>(I1))
1069 while (isa<DbgInfoIntrinsic>(I2))
1078 bool Changed =
false;
1082 if (isa<TerminatorInst>(I1))
1083 goto HoistTerminator;
1092 if (!I2->use_empty())
1093 I2->replaceAllUsesWith(I1);
1095 unsigned KnownIDs[] = {
1103 I2->eraseFromParent();
1112 while (isa<DbgInfoIntrinsic>(I1))
1114 while (isa<DbgInfoIntrinsic>(I2))
1153 I2->replaceAllUsesWith(NT);
1162 std::map<std::pair<Value*,Value*>,
SelectInst*> InsertedSelects;
1169 if (BB1V == BB2V)
continue;
1173 SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1175 SI = cast<SelectInst>
1177 BB1V->
getName()+
"."+BB2V->getName()));
1212 BasicBlock *BB2 = (Pred0 == BB1) ? Pred1 : Pred0;
1221 if (
PHINode *PN = dyn_cast<PHINode>(I)) {
1222 Value *BB1V = PN->getIncomingValueForBlock(BB1);
1223 Value *BB2V = PN->getIncomingValueForBlock(BB2);
1224 JointValueMap[std::make_pair(BB1V, BB2V)] = PN;
1226 FirstNonPhiInBBEnd = &*
I;
1230 if (!FirstNonPhiInBBEnd)
1237 RE1 = BB1->getInstList().rend(),
1241 while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1;
1244 while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2;
1251 bool Changed =
false;
1252 while (RI1 != RE1 && RI2 != RE2) {
1254 while (RI1 != RE1 && isa<DbgInfoIntrinsic>(&*RI1)) ++RI1;
1257 while (RI2 != RE2 && isa<DbgInfoIntrinsic>(&*RI2)) ++RI2;
1262 auto InstPair = std::make_pair(I1, I2);
1266 if (isa<PHINode>(I1) || isa<PHINode>(I2) ||
1267 isa<TerminatorInst>(I1) || isa<TerminatorInst>(I2) ||
1268 isa<LandingPadInst>(I1) || isa<LandingPadInst>(I2) ||
1269 isa<AllocaInst>(I1) || isa<AllocaInst>(I2) ||
1273 !JointValueMap.
count(InstPair))
1279 bool SwapOpnds =
false;
1280 if (ICmp1 && ICmp2 &&
1281 ICmp1->
getOperand(0) != ICmp2->getOperand(0) &&
1282 ICmp1->
getOperand(1) != ICmp2->getOperand(1) &&
1283 (ICmp1->
getOperand(0) == ICmp2->getOperand(1) ||
1284 ICmp1->
getOperand(1) == ICmp2->getOperand(0))) {
1285 ICmp2->swapOperands();
1290 ICmp2->swapOperands();
1297 Value *DifferentOp1 =
nullptr, *DifferentOp2 =
nullptr;
1298 unsigned Op1Idx = ~0U;
1304 if (Op1Idx != ~0U ||
1306 isa<Constant>(I2->getOperand(I))) {
1309 ICmp2->swapOperands();
1314 DifferentOp2 = I2->getOperand(I);
1317 DEBUG(
dbgs() <<
"SINK common instructions " << *I1 <<
"\n");
1322 if (Op1Idx != ~0U) {
1323 auto &NewPN = JointValueMap[std::make_pair(DifferentOp1, DifferentOp2)];
1329 NewPN->addIncoming(DifferentOp2, BB2);
1330 DEBUG(
dbgs() <<
"Create PHI node " << *NewPN <<
"\n";);
1335 PHINode *OldPN = JointValueMap[InstPair];
1336 JointValueMap.
erase(InstPair);
1340 bool UpdateRE1 = (I1 == BB1->begin()), UpdateRE2 = (I2 == BB2->
begin());
1347 if (!I2->use_empty())
1348 I2->replaceAllUsesWith(I1);
1352 I2->eraseFromParent();
1355 RE1 = BB1->getInstList().rend();
1358 FirstNonPhiInBBEnd = I1;
1404 unsigned MaxNumInstToLookAt = 10;
1406 RE = BrBB->
rend(); RI != RE && (--MaxNumInstToLookAt); ++RI) {
1466 if (isa<FCmpInst>(BrCond))
1474 bool Invert =
false;
1476 assert(ThenBB == BI->
getSuccessor(1) &&
"No edge from 'if' block?");
1479 assert(EndBB == BI->
getSuccessor(!Invert) &&
"No edge from to end block");
1488 unsigned SpeculationCost = 0;
1489 Value *SpeculatedStoreValue =
nullptr;
1492 BBE = std::prev(ThenBB->
end());
1493 BBI != BBE; ++BBI) {
1496 if (isa<DbgInfoIntrinsic>(I))
1502 if (SpeculationCost > 1)
1508 I, BB, ThenBB, EndBB))))
1510 if (!SpeculatedStoreValue &&
1516 if (SpeculatedStoreValue)
1517 SpeculatedStore = cast<StoreInst>(
I);
1529 ++SinkCandidateUseCounts[OpI];
1537 SinkCandidateUseCounts.
begin(), E = SinkCandidateUseCounts.end();
1539 if (I->first->getNumUses() == I->second) {
1541 if (SpeculationCost > 1)
1546 bool HaveRewritablePHIs =
false;
1562 HaveRewritablePHIs =
true;
1565 if (!OrigCE && !ThenCE)
1575 if (OrigCost + ThenCost > MaxCost)
1583 if (SpeculationCost > 1)
1589 if (!HaveRewritablePHIs && !(
HoistCondStores && SpeculatedStoreValue))
1593 DEBUG(
dbgs() <<
"SPECULATIVELY EXECUTING BB" << *ThenBB <<
"\n";);
1596 if (SpeculatedStoreValue) {
1599 Value *FalseV = SpeculatedStoreValue;
1609 std::prev(ThenBB->
end()));
1627 Value *TrueV = ThenV, *FalseV = OrigV;
1631 TrueV->
getName() +
"." + FalseV->getName());
1659 if (isa<DbgInfoIntrinsic>(BBI))
1661 if (Size > 10)
return false;
1668 if (UI->
getParent() != BB || isa<PHINode>(UI))
return false;
1710 if (RealDest == BB)
continue;
1719 RealDest->
getName()+
".critedge",
1732 if (
PHINode *PN = dyn_cast<PHINode>(BBI)) {
1738 if (BBI->hasName()) N->
setName(BBI->getName()+
".c");
1744 if (PI != TranslateMap.
end())
1750 TranslateMap[BBI] = V;
1755 if (!BBI->use_empty())
1756 TranslateMap[BBI] = N;
1791 isa<ConstantInt>(IfCond))
1799 unsigned NumPhis = 0;
1814 PHINode *PN = cast<PHINode>(II++);
1822 MaxCostVal0, TTI) ||
1831 if (!PN)
return true;
1838 isa<BinaryOperator>(IfCond)))
1848 if (cast<BranchInst>(IfBlock1->
getTerminator())->isConditional()) {
1853 if (!AggressiveInsts.
count(I) && !isa<DbgInfoIntrinsic>(
I)) {
1861 if (cast<BranchInst>(IfBlock2->
getTerminator())->isConditional()) {
1866 if (!AggressiveInsts.
count(I) && !isa<DbgInfoIntrinsic>(
I)) {
1874 DEBUG(
dbgs() <<
"FOUND IF CONDITION! " << *IfCond <<
" T: "
1899 cast<SelectInst>(Builder.
CreateSelect(IfCond, TrueVal, FalseVal,
""));
1920 assert(BI->
isConditional() &&
"Must be a conditional branch");
1923 ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
1929 if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
1938 if (FalseRet->getNumOperands() == 0) {
1939 TrueSucc->removePredecessor(BI->
getParent());
1949 Value *FalseValue = FalseRet->getReturnValue();
1952 if (
PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
1953 if (TVPN->getParent() == TrueSucc)
1954 TrueValue = TVPN->getIncomingValueForBlock(BI->
getParent());
1955 if (
PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
1956 if (FVPN->getParent() == FalseSucc)
1957 FalseValue = FVPN->getIncomingValueForBlock(BI->
getParent());
1964 if (
ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
1967 if (
ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
1973 TrueSucc->removePredecessor(BI->
getParent());
1980 if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
1981 }
else if (isa<UndefValue>(TrueValue)) {
1982 TrueValue = FalseValue;
1985 FalseValue,
"retval");
1989 Value *RI = !TrueValue ?
1994 DEBUG(
dbgs() <<
"\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
1995 <<
"\n " << *BI <<
"NewRet = " << *RI
1996 <<
"TRUEBLOCK: " << *TrueSucc <<
"FALSEBLOCK: "<< *FalseSucc);
2007 uint64_t &ProbTrue, uint64_t &ProbFalse) {
2009 "Looking for probabilities on unconditional branch?");
2011 if (!ProfileData || ProfileData->getNumOperands() != 3)
return false;
2013 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
2015 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
2016 if (!CITrue || !CIFalse)
return false;
2018 ProbFalse = CIFalse->getValue().getZExtValue();
2025 if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
2054 if (
BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
2055 if (PBI->isConditional() &&
2061 if (isa<CmpInst>(Curr)) {
2075 if (!Cond || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
2083 while (isa<DbgInfoIntrinsic>(CondIt)) ++CondIt;
2093 unsigned NumBonusInsts = 0;
2094 for (
auto I = BB->
begin(); Cond !=
I; ++
I) {
2096 if (isa<DbgInfoIntrinsic>(I))
2102 if (User ==
nullptr || User->
getParent() != BB)
2109 if (NumBonusInsts > BonusInstThreshold)
2125 if (TrueDest == BB || FalseDest == BB)
2145 bool InvertPredCond =
false;
2163 DEBUG(
dbgs() <<
"FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
2167 if (InvertPredCond) {
2170 if (NewCond->
hasOneUse() && isa<CmpInst>(NewCond)) {
2171 CmpInst *CI = cast<CmpInst>(NewCond);
2189 for (
auto BonusInst = BB->
begin(); Cond != BonusInst; ++BonusInst) {
2190 if (isa<DbgInfoIntrinsic>(BonusInst))
2195 VMap[BonusInst] = NewBonusInst;
2206 BonusInst->setName(BonusInst->getName() +
".old");
2224 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2232 if (PredHasWeights && SuccHasWeights) {
2236 NewWeights.
push_back(PredTrueWeight * SuccTrueWeight);
2241 NewWeights.
push_back(PredFalseWeight * (SuccFalseWeight +
2242 SuccTrueWeight) + PredTrueWeight * SuccFalseWeight);
2248 if (PredHasWeights && SuccHasWeights) {
2253 NewWeights.
push_back(PredTrueWeight * (SuccFalseWeight +
2254 SuccTrueWeight) + PredFalseWeight * SuccTrueWeight);
2256 NewWeights.
push_back(PredFalseWeight * SuccFalseWeight);
2261 if (NewWeights.
size() == 2) {
2268 createBranchWeights(MDWeights));
2273 for (
unsigned i = 0, e = PHIs.size(); i != e; ++i) {
2275 PHIs[i]->getIncomingValueForBlock(PBI->
getParent()));
2302 if (PBI_C->
isOne()) {
2308 NotCond, MergedCond,
2313 PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->
getParent()),
2327 if (isa<DbgInfoIntrinsic>(*I))
2350 if (BB->getSinglePredecessor()) {
2364 std::distance(PB, PE),
2394 while (isa<DbgInfoIntrinsic>(BBI))
2408 PBIOp = 0, BIOp = 1;
2410 PBIOp = 1, BIOp = 0;
2431 unsigned NumPhis = 0;
2433 isa<PHINode>(II); ++II, ++NumPhis) {
2437 PHINode *PN = cast<PHINode>(II);
2464 if (OtherDest == BB) {
2468 "infloop", BB->getParent());
2470 OtherDest = InfLoopBlock;
2482 PBICond = Builder.
CreateNot(PBICond, PBICond->getName()+
".not");
2489 Value *Cond = Builder.
CreateOr(PBICond, BICond,
"brmerge");
2497 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2502 if (PredHasWeights && SuccHasWeights) {
2503 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
2504 uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
2505 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
2506 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
2510 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
2511 PredOther * SuccCommon,
2512 PredOther * SuccOther};
2518 .createBranchWeights(NewWeights[0], NewWeights[1]));
2533 unsigned PBBIdx = PN->getBasicBlockIndex(PBI->
getParent());
2534 Value *PBIV = PN->getIncomingValue(PBBIdx);
2537 Value *
NV = cast<SelectInst>
2539 PN->setIncomingValue(PBBIdx, NV);
2558 uint32_t TrueWeight,
2559 uint32_t FalseWeight){
2565 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB :
nullptr;
2571 if (Succ == KeepEdge1)
2572 KeepEdge1 =
nullptr;
2573 else if (Succ == KeepEdge2)
2574 KeepEdge2 =
nullptr;
2583 if (!KeepEdge1 && !KeepEdge2) {
2584 if (TrueBB == FalseBB)
2592 if (TrueWeight != FalseWeight)
2595 createBranchWeights(TrueWeight, FalseWeight));
2597 }
else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
2625 if (!TrueVal || !FalseVal)
2634 uint32_t TrueWeight = 0, FalseWeight = 0;
2641 getSuccessorIndex()];
2642 FalseWeight = (uint32_t)Weights[SI->
findCaseValue(FalseVal).
2643 getSuccessorIndex()];
2649 TrueWeight, FalseWeight);
2698 if (isa<PHINode>(BB->
begin()) || !ICI->
hasOneUse())
return false;
2707 if (!Pred || !isa<SwitchInst>(Pred->getTerminator()))
return false;
2709 SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
2718 assert(VVal &&
"Should have a unique destination value");
2726 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
2742 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
2749 if (PHIUse ==
nullptr || PHIUse != &SuccBlock->
front() ||
2776 Weights[0] = (Weights[0]+1) >> 1;
2777 Weights.push_back(Weights[0]);
2782 createBranchWeights(MDWeights));
2801 if (!Cond)
return false;
2808 ConstantComparesGatherer ConstantCompare(Cond, DL);
2811 Value *CompVal = ConstantCompare.CompValue;
2812 unsigned UsedICmps = ConstantCompare.UsedICmps;
2813 Value *ExtraCase = ConstantCompare.Extra;
2816 if (!CompVal)
return false;
2831 if (ExtraCase && Values.
size() < 2)
return false;
2839 if (!TrueWhenEqual)
std::swap(DefaultBB, EdgeBB);
2843 DEBUG(
dbgs() <<
"Converting 'icmp' chain with " << Values.
size()
2844 <<
" cases into SWITCH. BB is:\n" << *BB);
2866 DEBUG(
dbgs() <<
" ** 'icmp' chain unhandled condition: " << *ExtraCase
2867 <<
"\nEXTRABB = " << *BB);
2882 for (
unsigned i = 0, e = Values.
size(); i != e; ++i)
2883 New->
addCase(Values[i], EdgeBB);
2889 isa<PHINode>(BBI); ++BBI) {
2890 PHINode *PN = cast<PHINode>(BBI);
2892 for (
unsigned i = 0, e = Values.
size()-1; i != e; ++i)
2899 DEBUG(
dbgs() <<
" ** 'icmp' chain result is:\n" << *BB <<
'\n');
2916 if (!isa<DbgInfoIntrinsic>(I))
2921 InvokeInst *II = cast<InvokeInst>((*PI++)->getTerminator());
2959 if (
BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
2969 while (!UncondBranchPreds.
empty()) {
2972 <<
"INTO UNCOND BRANCH PRED: " << *Pred);
2987 while (!CondBranchPreds.
empty()) {
3002 bool Changed =
false;
3006 while (UI != BB->
begin()) {
3012 if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI))
break;
3014 if (BBI->mayHaveSideEffects()) {
3015 if (
StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
3016 if (SI->isVolatile())
3018 }
else if (
LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
3019 if (LI->isVolatile())
3021 }
else if (
AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
3022 if (RMWI->isVolatile())
3025 if (CXI->isVolatile())
3027 }
else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
3028 !isa<LandingPadInst>(BBI)) {
3038 if (!BBI->use_empty())
3040 BBI->eraseFromParent();
3046 if (&BB->
front() != UI)
return Changed;
3049 for (
unsigned i = 0, e = Preds.size(); i != e; ++i) {
3052 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
3069 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
3072 if (i.getCaseSuccessor() == BB) {
3078 }
else if (
InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
3112 assert(Cases.
size() >= 1);
3115 for (
size_t I = 1, E = Cases.
size(); I != E; ++
I) {
3116 if (Cases[I - 1]->getValue() != Cases[
I]->getValue() + 1)
3125 assert(SI->
getNumCases() > 1 &&
"Degenerate switch?");
3138 if (!DestA) DestA = Dest;
3139 if (Dest == DestA) {
3143 if (!DestB) DestB = Dest;
3144 if (Dest == DestB) {
3151 assert(DestA && DestB &&
"Single-destination switch should have been folded.");
3152 assert(DestA != DestB);
3154 assert(!CasesB.
empty() &&
"There must be non-default cases.");
3155 assert(!CasesA.
empty() || HasDefault);
3162 ContiguousCases = &CasesA;
3163 ContiguousDest = DestA;
3166 ContiguousCases = &CasesB;
3167 ContiguousDest = DestB;
3183 if (NumCases->isNullValue() && !ContiguousCases->empty())
3194 uint64_t TrueWeight = 0;
3195 uint64_t FalseWeight = 0;
3196 for (
size_t I = 0, E = Weights.
size(); I != E; ++
I) {
3198 TrueWeight += Weights[I];
3200 FalseWeight += Weights[
I];
3202 while (TrueWeight > UINT32_MAX || FalseWeight > UINT32_MAX) {
3208 (uint32_t)TrueWeight, (uint32_t)FalseWeight));
3213 for (
auto BBI = ContiguousDest->
begin(); isa<PHINode>(BBI); ++BBI) {
3214 unsigned PreviousEdges = ContiguousCases->size();
3216 for (
unsigned I = 0, E = PreviousEdges - 1; I != E; ++
I)
3217 cast<PHINode>(BBI)->removeIncomingValue(SI->
getParent());
3219 for (
auto BBI = OtherDest->
begin(); isa<PHINode>(BBI); ++BBI) {
3220 unsigned PreviousEdges = SI->
getNumCases() - ContiguousCases->size();
3222 for (
unsigned I = 0, E = PreviousEdges - 1; I != E; ++
I)
3223 cast<PHINode>(BBI)->removeIncomingValue(SI->
getParent());
3238 APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
3244 if ((I.getCaseValue()->getValue() & KnownZero) != 0 ||
3245 (I.getCaseValue()->getValue() & KnownOne) != KnownOne) {
3247 DEBUG(
dbgs() <<
"SimplifyCFG: switch case '"
3248 << I.getCaseValue() <<
"' is dead.\n");
3260 for (
unsigned I = 0, E = DeadCases.
size(); I != E; ++
I) {
3263 "Case was not found. Probably mistake in DeadCases forming.");
3273 if (HasWeight && Weights.
size() >= 2) {
3277 createBranchWeights(MDWeights));
3280 return !DeadCases.
empty();
3303 while (
PHINode *
PHI = dyn_cast<PHINode>(I++)) {
3304 int Idx =
PHI->getBasicBlockIndex(BB);
3305 assert(Idx >= 0 &&
"PHI has no entry for predecessor?");
3307 Value *InValue =
PHI->getIncomingValue(Idx);
3308 if (InValue != CaseValue)
continue;
3323 ForwardingNodesMap ForwardingNodes;
3334 ForwardingNodes[
PHI].push_back(PhiIndex);
3337 bool Changed =
false;
3339 for (ForwardingNodesMap::iterator I = ForwardingNodes.begin(),
3340 E = ForwardingNodes.end(); I != E; ++
I) {
3344 if (Indexes.
size() < 2)
continue;
3346 for (
size_t I = 0, E = Indexes.
size(); I != E; ++
I)
3363 return CE->isGEPWithNoNotionalOverIndexing();
3365 return isa<ConstantFP>(C) ||
3366 isa<ConstantInt>(C) ||
3367 isa<ConstantPointerNull>(C) ||
3368 isa<GlobalValue>(C) ||
3376 if (
Constant *C = dyn_cast<Constant>(V))
3378 return ConstantPool.
lookup(V);
3407 if (
CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
3435 if (
T->getNumSuccessors() != 1)
3438 CaseDest =
T->getSuccessor(0);
3439 }
else if (isa<DbgInfoIntrinsic>(I)) {
3448 for (
auto &
Use : I->uses()) {
3451 if (I->getParent() == CaseDest)
3453 if (
PHINode *Phi = dyn_cast<PHINode>(User))
3454 if (Phi->getIncomingBlock(
Use) == CaseDest)
3459 ConstantPool.
insert(std::make_pair(I, C));
3467 *CommonDest = CaseDest;
3469 if (CaseDest != *CommonDest)
3474 while (
PHINode *
PHI = dyn_cast<PHINode>(I++)) {
3475 int Idx =
PHI->getBasicBlockIndex(Pred);
3488 Res.push_back(std::make_pair(
PHI, ConstVal));
3491 return Res.size() > 0;
3497 SwitchCaseResultVectorTy &UniqueResults,
3499 for (
auto &I : UniqueResults) {
3500 if (I.first == Result) {
3501 I.second.push_back(CaseVal);
3505 UniqueResults.push_back(std::make_pair(Result,
3515 SwitchCaseResultVectorTy &UniqueResults,
3518 for (
auto &I : SI->
cases()) {
3522 SwitchCaseResultsTy Results;
3523 if (!
GetCaseResults(SI, CaseVal, I.getCaseSuccessor(), &CommonDest, Results,
3528 if (Results.size() > 1)
3534 PHI = Results[0].first;
3535 else if (PHI != Results[0].first)
3546 DefaultResults.size() == 1 ? DefaultResults.begin()->second :
nullptr;
3547 if ((!DefaultResult &&
3567 Constant *DefaultResult, Value *Condition,
3569 assert(ResultVector.size() == 2 &&
3570 "We should have exactly two unique results at this point");
3573 if (ResultVector[0].second.size() == 1 &&
3574 ResultVector[1].second.size() == 1) {
3575 ConstantInt *
const FirstCase = ResultVector[0].second[0];
3576 ConstantInt *
const SecondCase = ResultVector[1].second[0];
3578 bool DefaultCanTrigger = DefaultResult;
3579 Value *SelectValue = ResultVector[1].first;
3580 if (DefaultCanTrigger) {
3581 Value *
const ValueCompare =
3582 Builder.
CreateICmpEQ(Condition, SecondCase,
"switch.selectcmp");
3583 SelectValue = Builder.
CreateSelect(ValueCompare, ResultVector[1].first,
3584 DefaultResult,
"switch.select");
3586 Value *
const ValueCompare =
3587 Builder.
CreateICmpEQ(Condition, FirstCase,
"switch.selectcmp");
3588 return Builder.
CreateSelect(ValueCompare, ResultVector[0].first, SelectValue,
3627 SwitchCaseResultVectorTy UniqueResults;
3633 if (UniqueResults.size() != 2)
3635 assert(PHI !=
nullptr &&
"PHI for value select not found");
3640 DefaultResult, Cond, Builder);
3651 class SwitchLookupTable {
3662 Value *BuildLookup(Value *Index,
IRBuilder<> &Builder);
3666 static bool WouldFitInRegister(
const DataLayout &DL, uint64_t TableSize,
3667 const Type *ElementType);
3708 SwitchLookupTable::SwitchLookupTable(
3712 : SingleValue(nullptr), BitMap(nullptr), BitMapElementTy(nullptr),
3713 LinearOffset(nullptr), LinearMultiplier(nullptr), Array(nullptr) {
3714 assert(Values.size() &&
"Can't build lookup table without values!");
3715 assert(TableSize >= Values.size() &&
"Can't fit values in table!");
3718 SingleValue = Values.begin()->second;
3724 for (
size_t I = 0, E = Values.size(); I != E; ++
I) {
3731 TableContents[Idx] = CaseRes;
3733 if (CaseRes != SingleValue)
3734 SingleValue =
nullptr;
3738 if (Values.size() < TableSize) {
3739 assert(DefaultValue &&
3740 "Need a default value to fill the lookup table holes.");
3742 for (uint64_t I = 0; I < TableSize; ++
I) {
3743 if (!TableContents[I])
3744 TableContents[
I] = DefaultValue;
3747 if (DefaultValue != SingleValue)
3748 SingleValue =
nullptr;
3754 Kind = SingleValueKind;
3760 if (isa<IntegerType>(ValueType)) {
3761 bool LinearMappingPossible =
true;
3764 assert(TableSize >= 2 &&
"Should be a SingleValue table.");
3766 for (uint64_t I = 0; I < TableSize; ++
I) {
3771 LinearMappingPossible =
false;
3776 APInt Dist = Val - PrevVal;
3779 }
else if (Dist != DistToPrev) {
3780 LinearMappingPossible =
false;
3786 if (LinearMappingPossible) {
3787 LinearOffset = cast<ConstantInt>(TableContents[0]);
3788 LinearMultiplier = ConstantInt::get(M.
getContext(), DistToPrev);
3789 Kind = LinearMapKind;
3796 if (WouldFitInRegister(DL, TableSize, ValueType)) {
3799 for (uint64_t I = TableSize; I > 0; --
I) {
3802 if (!isa<UndefValue>(TableContents[I - 1])) {
3803 ConstantInt *Val = cast<ConstantInt>(TableContents[I - 1]);
3804 TableInt |= Val->
getValue().
zext(TableInt.getBitWidth());
3807 BitMap = ConstantInt::get(M.
getContext(), TableInt);
3808 BitMapElementTy =
IT;
3815 ArrayType *ArrayTy = ArrayType::get(ValueType, TableSize);
3816 Constant *Initializer = ConstantArray::get(ArrayTy, TableContents);
3819 GlobalVariable::PrivateLinkage,
3822 Array->setUnnamedAddr(
true);
3826 Value *SwitchLookupTable::BuildLookup(Value *Index,
IRBuilder<> &Builder) {
3828 case SingleValueKind:
3830 case LinearMapKind: {
3833 false,
"switch.idx.cast");
3834 if (!LinearMultiplier->isOne())
3835 Result = Builder.
CreateMul(Result, LinearMultiplier,
"switch.idx.mult");
3836 if (!LinearOffset->isZero())
3837 Result = Builder.
CreateAdd(Result, LinearOffset,
"switch.offset");
3851 ConstantInt::get(MapTy, BitMapElementTy->
getBitWidth()),
3855 Value *DownShifted = Builder.
CreateLShr(BitMap, ShiftAmt,
3856 "switch.downshift");
3858 return Builder.
CreateTrunc(DownShifted, BitMapElementTy,
3864 uint64_t TableSize = Array->getInitializer()->getType()
3866 if (TableSize > (1ULL << (IT->
getBitWidth() - 1)))
3870 "switch.tableidx.zext");
3872 Value *GEPIndices[] = { Builder.
getInt32(0), Index };
3874 GEPIndices,
"switch.gep");
3875 return Builder.
CreateLoad(GEP,
"switch.load");
3881 bool SwitchLookupTable::WouldFitInRegister(
const DataLayout &DL,
3883 const Type *ElementType) {
3902 if (SI->
getNumCases() > TableSize || TableSize >= UINT64_MAX / 10)
3905 bool AllTablesFitInRegister =
true;
3906 bool HasIllegalType =
false;
3907 for (
const auto &I : ResultTypes) {
3908 Type *Ty = I.second;
3911 HasIllegalType = HasIllegalType || !TTI.
isTypeLegal(Ty);
3914 AllTablesFitInRegister = AllTablesFitInRegister &&
3915 SwitchLookupTable::WouldFitInRegister(DL, TableSize, Ty);
3920 if (HasIllegalType && !AllTablesFitInRegister)
3925 if (AllTablesFitInRegister)
3981 DefaultValue, CmpOp1,
true);
3982 if (DefaultConst != TrueConst && DefaultConst != FalseConst)
3987 for (
auto ValuePair : Values) {
3989 ValuePair.second, CmpOp1,
true);
3990 if (!CaseConst || CaseConst == DefaultConst)
3992 assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
3993 "Expect true or false as compare result.");
4007 if (DefaultConst == FalseConst) {
4010 ++NumTableCmpReuses;
4013 Value *InvertedTableCmp = BinaryOperator::CreateXor(RangeCmp,
4014 ConstantInt::get(RangeCmp->
getType(), 1),
"inverted.cmp",
4017 ++NumTableCmpReuses;
4027 assert(SI->
getNumCases() > 1 &&
"Degenerate switch?");
4062 MinCaseVal = CaseVal;
4064 MaxCaseVal = CaseVal;
4069 if (!
GetCaseResults(SI, CaseVal, CI.getCaseSuccessor(), &CommonDest,
4074 for (
const auto &I : Results) {
4077 if (!ResultLists.
count(PHI))
4079 ResultLists[
PHI].push_back(std::make_pair(CaseVal, Value));
4085 ResultTypes[
PHI] = ResultLists[
PHI][0].second->getType();
4088 uint64_t NumResults = ResultLists[PHIs[0]].
size();
4091 bool TableHasHoles = (NumResults < TableSize);
4097 &CommonDest, DefaultResultsList, DL);
4099 bool NeedMask = (TableHasHoles && !HasDefaultResults);
4108 for (
const auto &I : DefaultResultsList) {
4111 DefaultResults[
PHI] = Result;
4132 uint64_t MaxTableSize = CaseSize > 63 ? UINT64_MAX : 1ULL << CaseSize;
4133 assert(MaxTableSize >= TableSize &&
4134 "It is impossible for a switch to have more entries than the max "
4135 "representable value of its input integer type's size.");
4140 const bool DefaultIsReachable =
4142 const bool GeneratingCoveredLookupTable = (MaxTableSize == TableSize);
4145 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
4150 Value *Cmp = Builder.
CreateICmpULT(TableIndex, ConstantInt::get(
4151 MinCaseVal->
getType(), TableSize));
4163 MaskBB->
setName(
"switch.hole_check");
4164 LookupBB = BasicBlock::Create(Mod.
getContext(),
4171 uint64_t TableSizePowOf2 =
NextPowerOf2(std::max(7ULL, TableSize - 1ULL));
4172 APInt MaskInt(TableSizePowOf2, 0);
4175 const ResultListTy &ResultList = ResultLists[PHIs[0]];
4176 for (
size_t I = 0, E = ResultList.size(); I != E; ++
I) {
4177 uint64_t Idx = (ResultList[
I].first->getValue() -
4178 MinCaseVal->
getValue()).getLimitedValue();
4179 MaskInt |= One << Idx;
4188 "switch.maskindex");
4189 Value *Shifted = Builder.
CreateLShr(TableMask, MaskIndex,
4200 if (!DefaultIsReachable || GeneratingCoveredLookupTable) {
4207 bool ReturnedEarly =
false;
4208 for (
size_t I = 0, E = PHIs.size(); I != E; ++
I) {
4210 const ResultListTy &ResultList = ResultLists[
PHI];
4213 Constant *DV = NeedMask ? ResultLists[
PHI][0].second : DefaultResults[
PHI];
4214 SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL);
4216 Value *Result = Table.BuildLookup(TableIndex, Builder);
4223 ReturnedEarly =
true;
4229 if (!TableHasHoles && HasDefaultResults && RangeCheckBranch) {
4255 ++NumLookupTablesHoles;
4262 if (isValueEqualityComparison(SI)) {
4266 if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
4267 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4272 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4278 while (isa<DbgInfoIntrinsic>(BBI))
4281 if (FoldValueComparisonIntoPredecessors(SI, Builder))
4282 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4287 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4291 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4294 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4297 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4300 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4307 bool Changed =
false;
4337 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4369 if (isa<PHINode>(*Succ->begin()))
4373 if (BB == OtherPred)
4379 for (++I; isa<DbgInfoIntrinsic>(
I); ++
I) {}
4397 for (
auto I = OtherPred->begin(), E = OtherPred->end();
4400 if (isa<DbgInfoIntrinsic>(Inst))
4407 Succ->removePredecessor(BB);
4432 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(I))
4433 if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
4434 for (++I; isa<DbgInfoIntrinsic>(
I); ++
I)
4436 if (I->isTerminator() &&
4438 BonusInstThreshold, AC))
4445 for (++I; isa<DbgInfoIntrinsic>(
I); ++
I) {}
4446 if (I->isTerminator() &&
4456 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4465 if (isValueEqualityComparison(BI)) {
4470 if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
4471 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4477 while (isa<DbgInfoIntrinsic>(I))
4480 if (FoldValueComparisonIntoPredecessors(BI, Builder))
4481 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4482 }
else if (&*I == cast<Instruction>(BI->
getCondition())){
4485 while (isa<DbgInfoIntrinsic>(I))
4487 if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
4488 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4500 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4509 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4517 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4526 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4534 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4538 if (
BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
4541 return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) |
true;
4562 if (i == I->
getParent()->
end() || i->mayHaveSideEffects())
4567 if (
GEP->getPointerOperand() ==
I)
4575 if (
LoadInst *LI = dyn_cast<LoadInst>(Use))
4576 if (!LI->isVolatile())
4577 return LI->getPointerAddressSpace() == 0;
4580 if (
StoreInst *SI = dyn_cast<StoreInst>(Use))
4581 if (!SI->isVolatile())
4582 return SI->getPointerAddressSpace() == 0 && SI->getPointerOperand() ==
I;
4596 if (
BranchInst *BI = dyn_cast<BranchInst>(T)) {
4615 bool Changed =
false;
4617 assert(BB && BB->
getParent() &&
"Block not embedded in function!");
4618 assert(BB->
getTerminator() &&
"Degenerate basic block encountered!");
4625 DEBUG(
dbgs() <<
"Removing BB: \n" << *BB);
4652 if (PN->getNumIncomingValues() == 2)
4658 if (SimplifyUncondBranch(BI, Builder))
return true;
4660 if (SimplifyCondBranch(BI, Builder))
return true;
4663 if (SimplifyReturn(RI, Builder))
return true;
4665 if (SimplifyResume(RI, Builder))
return true;
4667 if (SimplifySwitch(SI, Builder))
return true;
4670 if (SimplifyUnreachable(UI))
return true;
4673 if (SimplifyIndirectBr(IBI))
return true;
4687 BonusInstThreshold, AC).run(BB);
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
ReturnInst - Return a value (possibly void), from a function.
Value * getValueOperand()
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
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...
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.
static bool HasNoDuplicateCall(const BasicBlock *BB)
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)
LoadInst * CreateLoad(Value *Ptr, const char *Name)
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
iterator_range< CaseIt > cases()
cases - iteration adapter for range-for loops.
uint64_t getZExtValue() const
Get zero extended value.
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
STATISTIC(NumFunctions,"Total number of functions")
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 * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
A Module instance is used to store all the information related to an LLVM module. ...
AtomicCmpXchgInst - an instruction that atomically checks whether a specified value is in a memory lo...
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)
DeleteDeadBlock - Delete the specified block, which must have no predecessors.
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
addCase - Add an entry to the switch instruction...
Value * getValue() const
Convenience accessor.
CallInst - 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.
SwitchInst * CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases=10, MDNode *BranchWeights=nullptr)
Create a switch instruction with the specified value, default dest, and with a hint for the number of...
static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2, Instruction *I1, Instruction *I2)
void computeKnownBits(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...
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)"))
CaseIt case_begin()
Returns a read/write iterator that points to the first case in SwitchInst.
bool mayHaveSideEffects() const
mayHaveSideEffects - Return true if the instruction may have side effects.
A cache of .assume calls within a function.
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.
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select)
static bool isEquality(Predicate P)
isEquality - Return true if this predicate is either EQ or NE.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
LoadInst - an instruction for reading from memory.
reverse_iterator rbegin()
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
AtomicRMWInst - an instruction that atomically reads a memory location, combines it with another valu...
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
static Constant * getTrue(Type *Ty)
getTrue - For a boolean type, or a vector of boolean type, return true, or a vector with every elemen...
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...
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
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...
ConstantInt * findCaseDest(BasicBlock *BB)
findCaseDest - Finds the unique case value for a given successor.
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
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...
iterator begin()
Instruction iterator methods.
reverse_iterator rbegin()
void setCallingConv(CallingConv::ID CC)
APInt Not(const APInt &APIVal)
Bitwise complement function.
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...
BlockAddress - The address of a basic block.
bool match(Val *V, const Pattern &P)
bool isUnconditional() const
static Constant * getIntegerCast(Constant *C, Type *Ty, bool isSigned)
Create a ZExt, Bitcast or Trunc for integer -> integer casts.
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
removeIncomingValue - 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...
SelectInst - This class represents the LLVM 'select' instruction.
bool isIdenticalTo(const Instruction *I) const
isIdenticalTo - Return true if the specified instruction is exactly identical to the current one...
static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI)
If we have a conditional branch as a predecessor of another block, this function tries to simplify it...
static bool InitializeUniqueCases(SwitchInst *SI, PHINode *&PHI, BasicBlock *&CommonDest, SwitchCaseResultVectorTy &UniqueResults, Constant *&DefaultResult, const DataLayout &DL)
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
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"), clEnumValEnd))
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...
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A Use represents the edge between a Value definition and its users.
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldTerminator - If a terminator instruction is predicated on a constant value, convert it into an unconditional branch to the constant destination.
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.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
const Value * getCalledValue() const
getCalledValue - Get a pointer to the function that is invoked by this instruction ...
static bool DominatesMergePoint(Value *V, BasicBlock *BB, SmallPtrSetImpl< Instruction * > *AggressiveInsts, unsigned &CostRemaining, const TargetTransformInfo &TTI)
If we have a merge point of an "if condition" as accepted above, return true if the specified value d...
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI)
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
clone() - 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.
static bool SwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder, AssumptionCache *AC, const DataLayout &DL)
If the switch is only used to initialize one or more phi nodes in a common successor block with only ...
BasicBlock * getDestination(unsigned i)
getDestination - Return the specified destination.
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.
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.
void setUnwindDest(BasicBlock *B)
ConstantExpr - a constant value that is initialized with an expression using other constant 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
getContext - Return the LLVMContext in which this type was uniqued.
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
static void MapCaseToResult(ConstantInt *CaseVal, SwitchCaseResultVectorTy &UniqueResults, Constant *Result)
ArrayType - Class to represent array types.
UnreachableInst * CreateUnreachable()
bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, if possible.
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"))
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
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.
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...
StoreInst - 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.
bool EliminateDuplicatePHINodes(BasicBlock *BB)
EliminateDuplicatePHINodes - 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.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
BasicBlock * getNormalDest() const
RF_NoModuleLevelChanges - If this flag is set, the remapper knows that only local values within a fun...
bool mayReadOrWriteMemory() const
mayReadOrWriteMemory - Return true if this instruction may read or write memory.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
RF_IgnoreMissingEntries - If this flag is set, the remapper ignores entries that are not in the value...
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
Interval::succ_iterator succ_end(Interval *I)
void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA=nullptr, MemoryDependenceAnalysis *MemDep=nullptr)
FoldSingleEntryPHINodes - We know that BB has one predecessor.
static cl::opt< bool > HoistCondStores("simplifycfg-hoist-cond-stores", cl::Hidden, cl::init(true), cl::desc("Hoist conditional stores if an unconditional store precedes"))
InstListType::reverse_iterator reverse_iterator
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
void intersectOptionalDataWith(const Value *V)
Clear any optional flags not set in the given Value.
initializer< Ty > init(const Ty &Val)
bool erase(const KeyT &Val)
bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold=1)
FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch, and if a predecessor branc...
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...
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
Subclasses of this class are all able to terminate a basic block.
void setDebugLoc(DebugLoc Loc)
setDebugLoc - 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.
bool isIdenticalToWhenDefined(const Instruction *I) const
isIdenticalToWhenDefined - This is like isIdenticalTo, except that it ignores the SubclassOptionalDat...
static bool HasBranchWeights(const Instruction *I)
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB)
TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an unconditional branch...
BranchInst - Conditional or Unconditional Branch instruction.
Value * CreateNot(Value *V, const Twine &Name="")
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder)
Turn a switch with two reachable destinations into an integer range comparison and branch...
UnreachableInst - This function has undefined behavior.
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
This is an important base class in LLVM.
const Value * getCondition() const
ResumeInst - Resume the propagation of an exception.
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...
IndirectBrInst - Indirect Branch Instruction.
ReturnInst * CreateRet(Value *V)
Create a 'ret <val>' 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
getDebugLoc - Return the debug location for this node as a DebugLoc.
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
const InstListType & getInstList() const
Return the underlying instruction list container.
This instruction compares its operands according to the predicate given to the constructor.
iterator insert(iterator where, NodeTy *New)
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.
bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, unsigned BonusInstThreshold, AssumptionCache *AC=nullptr)
SimplifyCFG - This function is used to do simplification of a CFG.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
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...
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.
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Value * CreateInBoundsGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
static UndefValue * get(Type *T)
get() - 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...
iterator erase(iterator I)
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a trivially dead instruction...
Value(Type *Ty, unsigned scid)
LLVMContext & getContext() const
All values hold a context through their type.
CallInst * CreateCall(Value *Callee, ArrayRef< Value * > Args=None, const Twine &Name="")
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.
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
CallingConv::ID getCallingConv() const
getCallingConv/setCallingConv - Get or set the calling convention of this function call...
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
bool isTerminator() 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
BasicBlock * getUnwindDest() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
unsigned getIntegerBitWidth() const
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.
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
bool slt(const APInt &RHS) const
Signed less than comparison.
Value * getIncomingValue(unsigned i) const
getIncomingValue - 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.
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...
Constant * ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands...
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()
user_back - Specialize the methods defined in Value, as we know that an instruction can only be used ...
MDNode * getMetadata(unsigned KindID) const
getMetadata - Get the metadata of given kind attached to this Instruction.
static bool ExtractBranchMetadata(BranchInst *BI, uint64_t &ProbTrue, uint64_t &ProbFalse)
Given a conditional BranchInstruction, retrieve the probabilities of the branch taking each edge...
This class represents a range of values.
DbgInfoIntrinsic - This is the common base class for debug info intrinsics.
Value * GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, BasicBlock *&IfFalse)
GetIfCondition - Check whether BB is the merge point of a if-region.
const APInt & getLower() const
Return the lower value for this range.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
BasicBlockTy * getCaseSuccessor()
Resolves successor for current case.
BasicBlock * getBasicBlock() const
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
std::reverse_iterator< iterator > reverse_iterator
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 isNullValue() const
isNullValue - Return true if this is the value that would be returned by getNullValue.
CaseIt findCaseValue(const ConstantInt *C)
findCaseValue - 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.
void splice(iterator where, iplist &L2)
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
isAllOnesValue - 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)
getFalse - For a boolean type, or a vector of boolean type, return false, or a vector with every elem...
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 ...
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
isIntegerTy - True if this is an instance of IntegerType.
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="")
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)
RemapInstruction - Convert the instruction operands from referencing the current values into those sp...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Value * getCondition() const
iterator insert(iterator I, T &&Elt)
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
void removeFromParent()
removeFromParent - This method unlinks 'this' from the containing basic block, but does not delete it...
static Constant * getNeg(Constant *C, bool HasNUW=false, bool HasNSW=false)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
BasicBlock * getSuccessor(unsigned idx) const
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Value * getCondition() const
const AttributeSet & getAttributes() const
getAttributes - Return the parameter attributes for this invoke.
BasicBlock * getDefaultDest() const
static unsigned ComputeSpeculationCost(const User *I, const TargetTransformInfo &TTI)
Compute an abstract "cost" of speculating the given instruction, which is assumed to be safe to specu...
static bool GetCaseResults(SwitchInst *SI, ConstantInt *CaseVal, BasicBlock *CaseDest, BasicBlock **CommonDest, SmallVectorImpl< std::pair< PHINode *, Constant * >> &Res, const DataLayout &DL)
Try to determine the resulting constant values in phi nodes at the common destination basic block...
iplist< 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...
bool hasOneUse() const
Return true if there is exactly one user of this value.
iterator find(const KeyT &Val)
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred)
FoldReturnIntoUncondBranch - This method duplicates the specified return instruction into a predecess...
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.
uint64_t getArrayNumElements() const
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
void removeCase(CaseIt i)
removeCase - This method removes the specified case and its successor from the switch instruction...
void dropUnknownMetadata(ArrayRef< unsigned > KnownIDs)
Drop unknown metadata.
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 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
getNumCases - return the number of 'cases' in this switch instruction, except the default case ...
void setCondition(Value *V)
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...
SwitchInst - Multiway switch.
const APInt & getUpper() const
Return the upper value for this range.
void setAttributes(const AttributeSet &Attrs)
setAttributes - Set the parameter attributes for this call.
static Constant * LookupConstant(Value *V, const SmallDenseMap< Value *, Constant * > &ConstantPool)
If V is a Constant, return it.
user_iterator user_begin()
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.
const ARM::ArchExtKind Kind
LLVMContext & getContext() const
Get the context in which this basic block lives.
bool operator<(int64_t V1, const APSInt &V2)
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
getPrimitiveSizeInBits - 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...
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
LLVM Value Representation.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
ReturnInst * CreateRetVoid()
Create a 'ret void' instruction.
static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2)
Return true if it is safe to merge these two terminator instructions together.
BranchInst * CreateBr(BasicBlock *Dest)
Create an unconditional 'br label X' instruction.
InvokeInst - Invoke instruction.
static bool ValidLookupTableConstant(Constant *C)
Return true if the backend will be able to handle initializing an array of constants like C...
C - The default llvm calling convention, compatible with C.
static PHINode * FindPHIForConditionForwarding(ConstantInt *CaseValue, BasicBlock *BB, int *PhiIndex)
If BB would be eligible for simplification by TryToSimplifyUncondBranchFromEmptyBlock (i...
const Value * getFalseValue() const
APInt LLVM_ATTRIBUTE_UNUSED_RESULT 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)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
isSafeToSpeculativelyExecute - Return true if the instruction does not have any effects besides calcu...
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
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.
void setIncomingValue(unsigned i, Value *V)
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
isThreadDependent - 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
getBasicBlockIndex - Return the first index of the specified basic block in the value list for this P...
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
getNumDestinations - return the number of possible destinations in this indirectbr instruction...
void removeDestination(unsigned i)
removeDestination - This method removes the specified successor from the indirectbr instruction...
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.
LLVMContext & getContext() const
Get the global data context.
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.
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...