llvm.org GIT mirror llvm / 3d3abe0
llvm::SwitchInst Renamed methods caseBegin, caseEnd and caseDefault with case_begin, case_end, and case_default. Added some notes relative to case iterators. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152532 91177308-0d34-0410-b5e6-96231b3b80d8 Stepan Dyatkovskiy 8 years ago
19 changed file(s) with 52 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
26152615
26162616 /// Returns a read/write iterator that points to the first
26172617 /// case in SwitchInst.
2618 CaseIt caseBegin() {
2618 CaseIt case_begin() {
26192619 return CaseIt(this, 0);
26202620 }
26212621 /// Returns a read-only iterator that points to the first
26222622 /// case in the SwitchInst.
2623 ConstCaseIt caseBegin() const {
2623 ConstCaseIt case_begin() const {
26242624 return ConstCaseIt(this, 0);
26252625 }
26262626
26272627 /// Returns a read/write iterator that points one past the last
26282628 /// in the SwitchInst.
2629 CaseIt caseEnd() {
2629 CaseIt case_end() {
26302630 return CaseIt(this, getNumCases());
26312631 }
26322632 /// Returns a read-only iterator that points one past the last
26332633 /// in the SwitchInst.
2634 ConstCaseIt caseEnd() const {
2634 ConstCaseIt case_end() const {
26352635 return ConstCaseIt(this, getNumCases());
26362636 }
2637 /// Returns an iterator that points to default case.
2637 /// Returns an iterator that points to the default case.
26382638 /// Note: this iterator allows to resolve successor only. Attempt
26392639 /// to resolve case value causes an assertion.
2640 CaseIt caseDefault() {
2640 /// Also note, that increment and decrement also causes an assertion and
2641 /// makes iterator invalid.
2642 CaseIt case_default() {
26412643 return CaseIt(this, DefaultPseudoIndex);
26422644 }
2643 ConstCaseIt caseDefault() const {
2645 ConstCaseIt case_default() const {
26442646 return ConstCaseIt(this, DefaultPseudoIndex);
26452647 }
26462648
26492651 /// return default case iterator to indicate
26502652 /// that it is handled by the default handler.
26512653 CaseIt findCaseValue(const ConstantInt *C) {
2652 for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
2654 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
26532655 if (i.getCaseValue() == C)
26542656 return i;
2655 return caseDefault();
2657 return case_default();
26562658 }
26572659 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2658 for (ConstCaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
2660 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
26592661 if (i.getCaseValue() == C)
26602662 return i;
2661 return caseDefault();
2663 return case_default();
26622664 }
26632665
26642666 /// findCaseDest - Finds the unique case value for a given successor. Returns
26672669 if (BB == getDefaultDest()) return NULL;
26682670
26692671 ConstantInt *CI = NULL;
2670 for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i) {
2672 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
26712673 if (i.getCaseSuccessor() == BB) {
26722674 if (CI) return NULL; // Multiple cases lead to BB.
26732675 else CI = i.getCaseValue();
26832685 /// removeCase - This method removes the specified case and its successor
26842686 /// from the switch instruction. Note that this operation may reorder the
26852687 /// remaining cases at index idx and above.
2688 /// Also note, that iterator becomes invalid after this operation.
26862689 ///
26872690 void removeCase(CaseIt i);
26882691
865865 // BBFrom to BBTo.
866866 unsigned NumEdges = 0;
867867 ConstantInt *EdgeVal = 0;
868 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
868 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
869869 i != e; ++i) {
870870 if (i.getCaseSuccessor() != BBTo) continue;
871871 if (NumEdges++) break;
11411141 Vals.push_back(VE.getTypeID(SI.getCondition()->getType()));
11421142 Vals.push_back(VE.getValueID(SI.getCondition()));
11431143 Vals.push_back(VE.getValueID(SI.getDefaultDest()));
1144 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd();
1144 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
11451145 i != e; ++i) {
11461146 Vals.push_back(VE.getValueID(i.getCaseValue()));
11471147 Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
24102410
24112411 BranchProbabilityInfo *BPI = FuncInfo.BPI;
24122412 // Start with "simple" cases
2413 for (SwitchInst::ConstCaseIt i = SI.caseBegin(), e = SI.caseEnd();
2413 for (SwitchInst::ConstCaseIt i = SI.case_begin(), e = SI.case_end();
24142414 i != e; ++i) {
24152415 const BasicBlock *SuccBB = i.getCaseSuccessor();
24162416 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
649649
650650 // Check to see if any of the cases match...
651651 BasicBlock *Dest = 0;
652 for (SwitchInst::CaseIt i = I.caseBegin(), e = I.caseEnd(); i != e; ++i) {
652 for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
653653 GenericValue CaseVal = getOperandValue(i.getCaseValue(), SF);
654654 if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
655655 Dest = cast(i.getCaseSuccessor());
23922392 Out << ";\n";
23932393
23942394 // Skip the first item since that's the default case.
2395 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd(); i != e; ++i) {
2395 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
23962396 ConstantInt* CaseVal = i.getCaseValue();
23972397 BasicBlock* Succ = i.getCaseSuccessor();
23982398 Out << " case ";
10891089 << getOpName(SI->getDefaultDest()) << ", "
10901090 << SI->getNumCases() << ", " << bbname << ");";
10911091 nl(Out);
1092 for (SwitchInst::ConstCaseIt i = SI->caseBegin(), e = SI->caseEnd();
1092 for (SwitchInst::ConstCaseIt i = SI->case_begin(), e = SI->case_end();
10931093 i != e; ++i) {
10941094 const ConstantInt* CaseVal = i.getCaseValue();
10951095 const BasicBlock *BB = i.getCaseSuccessor();
12451245 if (ConstantInt *AddRHS = dyn_cast(I->getOperand(1))) {
12461246 // change 'switch (X+4) case 1:' into 'switch (X) case -3'
12471247 // Skip the first item since that's the default case.
1248 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd();
1248 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
12491249 i != e; ++i) {
12501250 ConstantInt* CaseVal = i.getCaseValue();
12511251 Constant* NewCaseVal = ConstantExpr::getSub(cast(CaseVal),
18721872 } else if (SwitchInst *SI = dyn_cast(TI)) {
18731873 if (ConstantInt *Cond = dyn_cast(SI->getCondition())) {
18741874 // See if this is an explicit destination.
1875 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
1875 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
18761876 i != e; ++i)
18771877 if (i.getCaseValue() == Cond) {
18781878 BasicBlock *ReachableBB = i.getCaseSuccessor();
198198 // Analyse each switch case in turn. This is done in reverse order so that
199199 // removing a case doesn't cause trouble for the iteration.
200200 bool Changed = false;
201 for (SwitchInst::CaseIt CI = SI->caseEnd(), CE = SI->caseBegin(); CI-- != CE;
201 for (SwitchInst::CaseIt CI = SI->case_end(), CE = SI->case_begin(); CI-- != CE;
202202 ) {
203203 ConstantInt *Case = CI.getCaseValue();
204204
21572157 Value *SwitchCond = SI->getCondition();
21582158 BasicBlock *Parent = SI->getParent();
21592159 bool Changed = false;
2160 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2160 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
21612161 i != e; ++i) {
21622162 BasicBlock *Dst = i.getCaseSuccessor();
21632163 if (isOnlyReachableViaThisEdge(Parent, Dst, DT))
444444 // Do not process same value again and again.
445445 // At this point we have some cases already unswitched and
446446 // some not yet unswitched. Let's find the first not yet unswitched one.
447 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
447 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
448448 i != e; ++i) {
449449 Constant* UnswitchValCandidate = i.getCaseValue();
450450 if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
574574 // this.
575575 // Note that we can't trivially unswitch on the default case or
576576 // on already unswitched cases.
577 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
577 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
578578 i != e; ++i) {
579579 BasicBlock* LoopExitCandidate;
580580 if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
11201120
11211121 SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast(Val));
11221122 // Default case is live for multiple values.
1123 if (DeadCase == SI->caseDefault()) continue;
1123 if (DeadCase == SI->case_default()) continue;
11241124
11251125 // Found a dead case value. Don't remove PHI nodes in the
11261126 // successor if they become single-entry, those PHI nodes may
14871487 // If the input to SCCP is actually switch on undef, fix the undef to
14881488 // the first constant.
14891489 if (isa(SI->getCondition())) {
1490 SI->setCondition(SI->caseBegin().getCaseValue());
1491 markEdgeExecutable(BB, SI->caseBegin().getCaseSuccessor());
1490 SI->setCondition(SI->case_begin().getCaseValue());
1491 markEdgeExecutable(BB, SI->case_begin().getCaseSuccessor());
14921492 return true;
14931493 }
14941494
1495 markForcedConstant(SI->getCondition(), SI->caseBegin().getCaseValue());
1495 markForcedConstant(SI->getCondition(), SI->case_begin().getCaseValue());
14961496 return true;
14971497 }
14981498 }
109109 BasicBlock *DefaultDest = TheOnlyDest;
110110
111111 // Figure out which case it goes to.
112 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
112 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
113113 i != e; ++i) {
114114 // Found case matching a constant operand?
115115 if (i.getCaseValue() == CI) {
167167 if (SI->getNumCases() == 1) {
168168 // Otherwise, we can fold this switch into a conditional branch
169169 // instruction if it has only one non-default destination.
170 SwitchInst::CaseIt FirstCase = SI->caseBegin();
170 SwitchInst::CaseIt FirstCase = SI->case_begin();
171171 Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
172172 FirstCase.getCaseValue(), "cond");
173173
7878 Vec.resize(n + 1 + 1); // +1 for MDString and +1 for default case
7979
8080 Vec[0] = MDString::get(Context, "branch_weights");
81 Vec[1] = ConstantInt::get(Int32Ty, Case == SI->caseDefault() ?
81 Vec[1] = ConstantInt::get(Int32Ty, Case == SI->case_default() ?
8282 LikelyBranchWeight : UnlikelyBranchWeight);
8383 for (unsigned i = 0; i < n; ++i) {
8484 Vec[i + 1 + 1] = ConstantInt::get(Int32Ty, i == Case.getCaseIndex() ?
236236 unsigned numCmps = 0;
237237
238238 // Start with "simple" cases
239 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd(); i != e; ++i)
239 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
240240 Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
241241 i.getCaseSuccessor()));
242242
479479 BasicBlock*> > &Cases) {
480480 if (SwitchInst *SI = dyn_cast(TI)) {
481481 Cases.reserve(SI->getNumCases());
482 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd(); i != e; ++i)
482 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
483483 Cases.push_back(std::make_pair(i.getCaseValue(),
484484 i.getCaseSuccessor()));
485485 return SI->getDefaultDest();
604604 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
605605 << "Through successor TI: " << *TI);
606606
607 for (SwitchInst::CaseIt i = SI->caseEnd(), e = SI->caseBegin(); i != e;) {
607 for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
608608 --i;
609609 if (DeadCases.count(i.getCaseValue())) {
610610 i.getCaseSuccessor()->removePredecessor(TI->getParent());
20932093 // Ok, the block is reachable from the default dest. If the constant we're
20942094 // comparing exists in one of the other edges, then we can constant fold ICI
20952095 // and zap it.
2096 if (SI->findCaseValue(Cst) != SI->caseDefault()) {
2096 if (SI->findCaseValue(Cst) != SI->case_default()) {
20972097 Value *V;
20982098 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
20992099 V = ConstantInt::getFalse(BB->getContext());
24202420 }
24212421 }
24222422 } else if (SwitchInst *SI = dyn_cast(TI)) {
2423 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2423 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
24242424 i != e; ++i)
24252425 if (i.getCaseSuccessor() == BB) {
24262426 BB->removePredecessor(SI->getParent());
24322432 // destination and make it the default.
24332433 if (SI->getDefaultDest() == BB) {
24342434 std::map > Popularity;
2435 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2435 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
24362436 i != e; ++i) {
24372437 std::pair &entry =
24382438 Popularity[i.getCaseSuccessor()];
24692469 for (unsigned i = 0; i != MaxPop-1; ++i)
24702470 MaxBlock->removePredecessor(SI->getParent());
24712471
2472 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2472 for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
24732473 i != e; ++i)
24742474 if (i.getCaseSuccessor() == MaxBlock) {
24752475 SI->removeCase(i);
25172517
25182518 // Make sure all cases point to the same destination and gather the values.
25192519 SmallVector Cases;
2520 SwitchInst::CaseIt I = SI->caseBegin();
2520 SwitchInst::CaseIt I = SI->case_begin();
25212521 Cases.push_back(I.getCaseValue());
25222522 SwitchInst::CaseIt PrevI = I++;
2523 for (SwitchInst::CaseIt E = SI->caseEnd(); I != E; PrevI = I++) {
2523 for (SwitchInst::CaseIt E = SI->case_end(); I != E; PrevI = I++) {
25242524 if (PrevI.getCaseSuccessor() != I.getCaseSuccessor())
25252525 return false;
25262526 Cases.push_back(I.getCaseValue());
25422542 Sub = Builder.CreateAdd(Sub, Offset, Sub->getName()+".off");
25432543 Value *Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
25442544 Builder.CreateCondBr(
2545 Cmp, SI->caseBegin().getCaseSuccessor(), SI->getDefaultDest());
2545 Cmp, SI->case_begin().getCaseSuccessor(), SI->getDefaultDest());
25462546
25472547 // Prune obsolete incoming values off the successor's PHI nodes.
2548 for (BasicBlock::iterator BBI = SI->caseBegin().getCaseSuccessor()->begin();
2548 for (BasicBlock::iterator BBI = SI->case_begin().getCaseSuccessor()->begin();
25492549 isa(BBI); ++BBI) {
25502550 for (unsigned I = 0, E = SI->getNumCases()-1; I != E; ++I)
25512551 cast(BBI)->removeIncomingValue(SI->getParent());
25652565
25662566 // Gather dead cases.
25672567 SmallVector DeadCases;
2568 for (SwitchInst::CaseIt I = SI->caseBegin(), E = SI->caseEnd(); I != E; ++I) {
2568 for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
25692569 if ((I.getCaseValue()->getValue() & KnownZero) != 0 ||
25702570 (I.getCaseValue()->getValue() & KnownOne) != KnownOne) {
25712571 DeadCases.push_back(I.getCaseValue());
25772577 // Remove dead cases from the switch.
25782578 for (unsigned I = 0, E = DeadCases.size(); I != E; ++I) {
25792579 SwitchInst::CaseIt Case = SI->findCaseValue(DeadCases[I]);
2580 assert(Case != SI->caseDefault() &&
2580 assert(Case != SI->case_default() &&
25812581 "Case was not found. Probably mistake in DeadCases forming.");
25822582 // Prune unused values from PHI nodes.
25832583 Case.getCaseSuccessor()->removePredecessor(SI->getParent());
26292629 typedef DenseMap > ForwardingNodesMap;
26302630 ForwardingNodesMap ForwardingNodes;
26312631
2632 for (SwitchInst::CaseIt I = SI->caseBegin(), E = SI->caseEnd(); I != E; ++I) {
2632 for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
26332633 ConstantInt *CaseValue = I.getCaseValue();
26342634 BasicBlock *CaseDest = I.getCaseSuccessor();
26352635
17301730 Out << ", ";
17311731 writeOperand(SI.getDefaultDest(), true);
17321732 Out << " [";
1733 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd();
1733 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end();
17341734 i != e; ++i) {
17351735 Out << "\n ";
17361736 writeOperand(i.getCaseValue(), true);
812812 // have the same type as the switched-on value.
813813 Type *SwitchTy = SI.getCondition()->getType();
814814 SmallPtrSet Constants;
815 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd(); i != e; ++i) {
815 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
816816 Assert1(i.getCaseValue()->getType() == SwitchTy,
817817 "Switch constants must all be same type as switch value!", &SI);
818818 Assert2(Constants.insert(i.getCaseValue()),
319319
320320 DenseMap LCases;
321321
322 for (SwitchInst::CaseIt I = LI->caseBegin(), E = LI->caseEnd();
322 for (SwitchInst::CaseIt I = LI->case_begin(), E = LI->case_end();
323323 I != E; ++I)
324324 LCases[I.getCaseValue()] = I.getCaseSuccessor();
325325
326 for (SwitchInst::CaseIt I = RI->caseBegin(), E = RI->caseEnd();
326 for (SwitchInst::CaseIt I = RI->case_begin(), E = RI->case_end();
327327 I != E; ++I) {
328328 ConstantInt *CaseValue = I.getCaseValue();
329329 BasicBlock *LCase = LCases[CaseValue];