llvm.org GIT mirror llvm / c10fa6c
Taken into account Duncan's comments for r149481 dated by 2nd Feb 2012: http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20120130/136146.html Implemented CaseIterator and it solves almost all described issues: we don't need to mix operand/case/successor indexing anymore. Base iterator class is implemented as a template since it may be initialized either from "const SwitchInst*" or from "SwitchInst*". ConstCaseIt is just a read-only iterator. CaseIt is read-write iterator; it allows to change case successor and case value. Usage of iterator allows totally remove resolveXXXX methods. All indexing convertions done automatically inside the iterator's getters. Main way of iterator usage looks like this: SwitchInst *SI = ... // intialize it somehow for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd(); i != e; ++i) { BasicBlock *BB = i.getCaseSuccessor(); ConstantInt *V = i.getCaseValue(); // Do something. } If you want to convert case number to TerminatorInst successor index, just use getSuccessorIndex iterator's method. If you want initialize iterator from TerminatorInst successor index, use CaseIt::fromSuccessorIndex(...) method. There are also related changes in llvm-clients: klee and clang. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152297 91177308-0d34-0410-b5e6-96231b3b80d8 Stepan Dyatkovskiy 8 years ago
25 changed file(s) with 307 addition(s) and 216 deletion(s). Raw diff Collapse all Expand all
9494
9595 std::string Str;
9696 raw_string_ostream OS(Str);
97 unsigned Case = SI->resolveCaseIndex(SuccNo);
98 OS << SI->getCaseValue(Case)->getValue();
97 SwitchInst::ConstCaseIt Case =
98 SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
99 OS << Case.getCaseValue()->getValue();
99100 return OS.str();
100101 }
101102 return "";
2323 #include "llvm/ADT/SmallVector.h"
2424 #include "llvm/Support/ErrorHandling.h"
2525 #include
26 #include
2726
2827 namespace llvm {
2928
24672466 protected:
24682467 virtual SwitchInst *clone_impl() const;
24692468 public:
2470
2471 enum { ErrorIndex = UINT_MAX };
2469
2470 static const unsigned DefaultPseudoIndex = ~0L-1; // -2
2471
2472 template
2473 class CaseIteratorT {
2474 protected:
2475
2476 SwitchInstTy *SI;
2477 unsigned Index;
2478
2479 public:
2480
2481 typedef CaseIteratorT Self;
2482
2483 /// Initializes case iterator for given SwitchInst and for given
2484 /// case number.
2485 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2486 this->SI = SI;
2487 Index = CaseNum;
2488 }
2489
2490 /// Initializes case iterator for given SwitchInst and for given
2491 /// TerminatorInst's successor index.
2492 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2493 assert(SuccessorIndex < SI->getNumSuccessors() &&
2494 "Successor index # out of range!");
2495 return SuccessorIndex != 0 ?
2496 Self(SI, SuccessorIndex - 1) :
2497 Self(SI, DefaultPseudoIndex);
2498 }
2499
2500 /// Resolves case value for current case.
2501 ConstantIntTy *getCaseValue() {
2502 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2503 return reinterpret_cast(SI->getOperand(2 + Index*2));
2504 }
2505
2506 /// Resolves successor for current case.
2507 BasicBlockTy *getCaseSuccessor() {
2508 assert((Index < SI->getNumCases() || DefaultPseudoIndex) &&
2509 "Index out the number of cases.");
2510 return SI->getSuccessor(getSuccessorIndex());
2511 }
2512
2513 /// Returns number of current case.
2514 unsigned getCaseIndex() const { return Index; }
2515
2516 /// Returns TerminatorInst's successor index for current case successor.
2517 unsigned getSuccessorIndex() const {
2518 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2519 "Index out the number of cases.");
2520 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2521 }
2522
2523 Self operator++() {
2524 // Check index correctness after increment.
2525 // Note: Index == getNumCases() means end().
2526 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2527 ++Index;
2528 return *this;
2529 }
2530 Self operator++(int) {
2531 Self tmp = *this;
2532 ++(*this);
2533 return tmp;
2534 }
2535 Self operator--() {
2536 // Check index correctness after decrement.
2537 // Note: Index == getNumCases() means end().
2538 // Also allow "-1" iterator here. That will became valid after ++.
2539 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2540 "Index out the number of cases.");
2541 --Index;
2542 return *this;
2543 }
2544 Self operator--(int) {
2545 Self tmp = *this;
2546 --(*this);
2547 return tmp;
2548 }
2549 bool operator==(const Self& RHS) const {
2550 assert(RHS.SI == SI && "Incompatible operators.");
2551 return RHS.Index == Index;
2552 }
2553 bool operator!=(const Self& RHS) const {
2554 assert(RHS.SI == SI && "Incompatible operators.");
2555 return RHS.Index != Index;
2556 }
2557 };
2558
2559 typedef CaseIteratorT
2560 ConstCaseIt;
2561
2562 class CaseIt : public CaseIteratorT {
2563
2564 typedef CaseIteratorT ParentTy;
2565
2566 public:
2567
2568 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2569 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2570
2571 /// Sets the new value for current case.
2572 void setValue(ConstantInt *V) {
2573 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2574 SI->setOperand(2 + Index*2, reinterpret_cast(V));
2575 }
2576
2577 /// Sets the new successor for current case.
2578 void setSuccessor(BasicBlock *S) {
2579 SI->setSuccessor(getSuccessorIndex(), S);
2580 }
2581 };
24722582
24732583 static SwitchInst *Create(Value *Value, BasicBlock *Default,
24742584 unsigned NumCases, Instruction *InsertBefore = 0) {
24782588 unsigned NumCases, BasicBlock *InsertAtEnd) {
24792589 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
24802590 }
2591
24812592 ~SwitchInst();
24822593
24832594 /// Provide fast operand accessors
25012612 return getNumOperands()/2 - 1;
25022613 }
25032614
2504 /// getCaseValue - Return the specified case value. Note that case #0, means
2505 /// first case, not a default case.
2506 ConstantInt *getCaseValue(unsigned i) {
2507 assert(i < getNumCases() && "Illegal case value to get!");
2508 return reinterpret_cast(getOperand(2 + i*2));
2509 }
2510
2511 /// getCaseValue - Return the specified case value. Note that case #0, means
2512 /// first case, not a default case.
2513 const ConstantInt *getCaseValue(unsigned i) const {
2514 assert(i < getNumCases() && "Illegal case value to get!");
2515 return reinterpret_cast(getOperand(2 + i*2));
2516 }
2517
2518 // setSuccessorValue - Updates the value associated with the specified
2519 // case.
2520 void setCaseValue(unsigned i, ConstantInt *CaseValue) {
2521 assert(i < getNumCases() && "Case index # out of range!");
2522 setOperand(2 + i*2, reinterpret_cast(CaseValue));
2523 }
2524
2615 /// Returns a read/write iterator that points to the first
2616 /// case in SwitchInst.
2617 CaseIt caseBegin() {
2618 return CaseIt(this, 0);
2619 }
2620 /// Returns a read-only iterator that points to the first
2621 /// case in the SwitchInst.
2622 ConstCaseIt caseBegin() const {
2623 return ConstCaseIt(this, 0);
2624 }
2625
2626 /// Returns a read/write iterator that points one past the last
2627 /// in the SwitchInst.
2628 CaseIt caseEnd() {
2629 return CaseIt(this, getNumCases());
2630 }
2631 /// Returns a read-only iterator that points one past the last
2632 /// in the SwitchInst.
2633 ConstCaseIt caseEnd() const {
2634 return ConstCaseIt(this, getNumCases());
2635 }
2636 /// Returns an iterator that points to default case.
2637 /// Note: this iterator allows to resolve successor only. Attempt
2638 /// to resolve case value causes an assertion.
2639 CaseIt caseDefault() {
2640 return CaseIt(this, DefaultPseudoIndex);
2641 }
2642 ConstCaseIt caseDefault() const {
2643 return ConstCaseIt(this, DefaultPseudoIndex);
2644 }
2645
25252646 /// findCaseValue - Search all of the case values for the specified constant.
2526 /// If it is explicitly handled, return the case number of it, otherwise
2527 /// return ErrorIndex to indicate that it is handled by the default handler.
2528 unsigned findCaseValue(const ConstantInt *C) const {
2529 for (unsigned i = 0, e = getNumCases(); i != e; ++i)
2530 if (getCaseValue(i) == C)
2647 /// If it is explicitly handled, return the case iterator of it, otherwise
2648 /// return default case iterator to indicate
2649 /// that it is handled by the default handler.
2650 CaseIt findCaseValue(const ConstantInt *C) {
2651 for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
2652 if (i.getCaseValue() == C)
25312653 return i;
2532 return ErrorIndex;
2533 }
2654 return caseDefault();
2655 }
2656 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2657 for (ConstCaseIt i = caseBegin(), e = caseEnd(); i != e; ++i)
2658 if (i.getCaseValue() == C)
2659 return i;
2660 return caseDefault();
2661 }
25342662
2535 /// resolveSuccessorIndex - Converts case index to index of its successor
2536 /// index in TerminatorInst successors collection.
2537 /// If CaseIndex == ErrorIndex, "default" successor will returned then.
2538 unsigned resolveSuccessorIndex(unsigned CaseIndex) const {
2539 assert((CaseIndex == ErrorIndex || CaseIndex < getNumCases()) &&
2540 "Case index # out of range!");
2541 return CaseIndex != ErrorIndex ? CaseIndex + 1 : 0;
2542 }
2543
2544 /// resolveCaseIndex - Converts index of successor in TerminatorInst
2545 /// collection to index of case that corresponds to this successor.
2546 unsigned resolveCaseIndex(unsigned SuccessorIndex) const {
2547 assert(SuccessorIndex < getNumSuccessors() &&
2548 "Successor index # out of range!");
2549 return SuccessorIndex != 0 ? SuccessorIndex - 1 : ErrorIndex;
2550 }
2551
25522663 /// findCaseDest - Finds the unique case value for a given successor. Returns
25532664 /// null if the successor is not found, not unique, or is the default case.
25542665 ConstantInt *findCaseDest(BasicBlock *BB) {
25552666 if (BB == getDefaultDest()) return NULL;
25562667
25572668 ConstantInt *CI = NULL;
2558 for (unsigned i = 0, e = getNumCases(); i != e; ++i) {
2559 if (getSuccessor(i + 1) == BB) {
2669 for (CaseIt i = caseBegin(), e = caseEnd(); i != e; ++i) {
2670 if (i.getCaseSuccessor() == BB) {
25602671 if (CI) return NULL; // Multiple cases lead to BB.
2561 else CI = getCaseValue(i);
2672 else CI = i.getCaseValue();
25622673 }
25632674 }
25642675 return CI;
25722683 /// from the switch instruction. Note that this operation may reorder the
25732684 /// remaining cases at index idx and above.
25742685 ///
2575 void removeCase(unsigned idx);
2686 void removeCase(CaseIt i);
25762687
25772688 unsigned getNumSuccessors() const { return getNumOperands()/2; }
25782689 BasicBlock *getSuccessor(unsigned idx) const {
25822693 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
25832694 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
25842695 setOperand(idx*2+1, (Value*)NewSucc);
2585 }
2586
2587 /// Resolves successor for idx-th case.
2588 /// Use getCaseSuccessor instead of TerminatorInst::getSuccessor,
2589 /// since internal SwitchInst organization of operands/successors is
2590 /// hidden and may be changed in any moment.
2591 BasicBlock *getCaseSuccessor(unsigned idx) const {
2592 return getSuccessor(resolveSuccessorIndex(idx));
2593 }
2594
2595 /// Set new successor for idx-th case.
2596 /// Use setCaseSuccessor instead of TerminatorInst::setSuccessor,
2597 /// since internal SwitchInst organization of operands/successors is
2598 /// hidden and may be changed in any moment.
2599 void setCaseSuccessor(unsigned idx, BasicBlock *NewSucc) {
2600 setSuccessor(resolveSuccessorIndex(idx), NewSucc);
2601 }
2602
2603 // getSuccessorValue - Return the value associated with the specified
2604 // successor.
2605 ConstantInt *getSuccessorValue(unsigned idx) const {
2606 assert(idx < getNumSuccessors() && "Successor # out of range!");
2607 return reinterpret_cast(getOperand(idx*2));
2608 }
2609
2610 // setSuccessorValue - Updates the value associated with the specified
2611 // successor.
2612 void setSuccessorValue(unsigned idx, ConstantInt* SuccessorValue) {
2613 assert(idx < getNumSuccessors() && "Successor # out of range!");
2614 setOperand(idx*2, reinterpret_cast(SuccessorValue));
26152696 }
26162697
26172698 // Methods for support type inquiry through isa, cast, and dyn_cast:
865865 // BBFrom to BBTo.
866866 unsigned NumEdges = 0;
867867 ConstantInt *EdgeVal = 0;
868 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
869 if (SI->getCaseSuccessor(i) != BBTo) continue;
868 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
869 i != e; ++i) {
870 if (i.getCaseSuccessor() != BBTo) continue;
870871 if (NumEdges++) break;
871 EdgeVal = SI->getCaseValue(i);
872 EdgeVal = i.getCaseValue();
872873 }
873874 assert(EdgeVal && "Missing successor?");
874875 if (NumEdges == 1) {
193193 Succs.assign(TI.getNumSuccessors(), true);
194194 return;
195195 }
196
197 unsigned CCase = SI.findCaseValue(cast(C));
198 Succs[SI.resolveSuccessorIndex(CCase)] = true;
196 SwitchInst::CaseIt Case = SI.findCaseValue(cast(C));
197 Succs[Case.getSuccessorIndex()] = true;
199198 }
200199
201200
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 (unsigned i = 0, e = SI.getNumCases(); i != e; ++i) {
1145 Vals.push_back(VE.getValueID(SI.getCaseValue(i)));
1146 Vals.push_back(VE.getValueID(SI.getCaseSuccessor(i)));
1144 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd();
1145 i != e; ++i) {
1146 Vals.push_back(VE.getValueID(i.getCaseValue()));
1147 Vals.push_back(VE.getValueID(i.getCaseSuccessor()));
11471148 }
11481149 }
11491150 break;
24102410
24112411 BranchProbabilityInfo *BPI = FuncInfo.BPI;
24122412 // Start with "simple" cases
2413 for (size_t i = 0; i < SI.getNumCases(); ++i) {
2414 BasicBlock *SuccBB = SI.getCaseSuccessor(i);
2413 for (SwitchInst::ConstCaseIt i = SI.caseBegin(), e = SI.caseEnd();
2414 i != e; ++i) {
2415 const BasicBlock *SuccBB = i.getCaseSuccessor();
24152416 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SuccBB];
24162417
24172418 uint32_t ExtraWeight = BPI ? BPI->getEdgeWeight(SI.getParent(), SuccBB) : 0;
24182419
2419 Cases.push_back(Case(SI.getCaseValue(i),
2420 SI.getCaseValue(i),
2420 Cases.push_back(Case(i.getCaseValue(), i.getCaseValue(),
24212421 SMBB, ExtraWeight));
24222422 }
24232423 std::sort(Cases.begin(), Cases.end(), CaseCmp());
649649
650650 // Check to see if any of the cases match...
651651 BasicBlock *Dest = 0;
652 unsigned NumCases = I.getNumCases();
653 // Skip the first item since that's the default case.
654 for (unsigned i = 0; i < NumCases; ++i) {
655 GenericValue CaseVal = getOperandValue(I.getCaseValue(i), SF);
652 for (SwitchInst::CaseIt i = I.caseBegin(), e = I.caseEnd(); i != e; ++i) {
653 GenericValue CaseVal = getOperandValue(i.getCaseValue(), SF);
656654 if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
657 Dest = cast(I.getCaseSuccessor(i));
655 Dest = cast(i.getCaseSuccessor());
658656 break;
659657 }
660658 }
23912391 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
23922392 Out << ";\n";
23932393
2394 unsigned NumCases = SI.getNumCases();
23952394 // Skip the first item since that's the default case.
2396 for (unsigned i = 0; i < NumCases; ++i) {
2397 ConstantInt* CaseVal = SI.getCaseValue(i);
2398 BasicBlock* Succ = SI.getCaseSuccessor(i);
2395 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd(); i != e; ++i) {
2396 ConstantInt* CaseVal = i.getCaseValue();
2397 BasicBlock* Succ = i.getCaseSuccessor();
23992398 Out << " case ";
24002399 writeOperand(CaseVal);
24012400 Out << ":\n";
24022401 printPHICopiesForSuccessor (SI.getParent(), Succ, 2);
24032402 printBranchToBlock(SI.getParent(), Succ, 2);
2404 if (Function::iterator(Succ) == llvm::next(Function::iterator(SI.getParent())))
2403 if (Function::iterator(Succ) ==
2404 llvm::next(Function::iterator(SI.getParent())))
24052405 Out << " break;\n";
24062406 }
24072407
10891089 << getOpName(SI->getDefaultDest()) << ", "
10901090 << SI->getNumCases() << ", " << bbname << ");";
10911091 nl(Out);
1092 unsigned NumCases = SI->getNumCases();
1093 for (unsigned i = 0; i < NumCases; ++i) {
1094 const ConstantInt* CaseVal = SI->getCaseValue(i);
1095 const BasicBlock *BB = SI->getCaseSuccessor(i);
1092 for (SwitchInst::ConstCaseIt i = SI->caseBegin(), e = SI->caseEnd();
1093 i != e; ++i) {
1094 const ConstantInt* CaseVal = i.getCaseValue();
1095 const BasicBlock *BB = i.getCaseSuccessor();
10961096 Out << iName << "->addCase("
10971097 << getOpName(CaseVal) << ", "
10981098 << getOpName(BB) << ");";
25812581 ConstantInt *Val =
25822582 dyn_cast(getVal(SI->getCondition()));
25832583 if (!Val) return false; // Cannot determine.
2584 unsigned ValTISucc = SI->resolveSuccessorIndex(SI->findCaseValue(Val));
2585 NextBB = SI->getSuccessor(ValTISucc);
2584 NextBB = SI->findCaseValue(Val).getCaseSuccessor();
25862585 } else if (IndirectBrInst *IBI = dyn_cast(CurInst)) {
25872586 Value *Val = getVal(IBI->getAddress())->stripPointerCasts();
25882587 if (BlockAddress *BA = dyn_cast(Val))
12441244 if (I->getOpcode() == Instruction::Add)
12451245 if (ConstantInt *AddRHS = dyn_cast(I->getOperand(1))) {
12461246 // change 'switch (X+4) case 1:' into 'switch (X) case -3'
1247 unsigned NumCases = SI.getNumCases();
12481247 // Skip the first item since that's the default case.
1249 for (unsigned i = 0; i < NumCases; ++i) {
1250 ConstantInt* CaseVal = SI.getCaseValue(i);
1248 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd();
1249 i != e; ++i) {
1250 ConstantInt* CaseVal = i.getCaseValue();
12511251 Constant* NewCaseVal = ConstantExpr::getSub(cast(CaseVal),
12521252 AddRHS);
12531253 assert(isa(NewCaseVal) &&
12541254 "Result of expression should be constant");
1255 SI.setCaseValue(i, cast(NewCaseVal));
1255 i.setValue(cast(NewCaseVal));
12561256 }
12571257 SI.setCondition(I->getOperand(0));
12581258 Worklist.Add(I);
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 (unsigned i = 0, e = SI->getNumCases(); i != e; ++i)
1876 if (SI->getCaseValue(i) == Cond) {
1877 BasicBlock *ReachableBB = SI->getCaseSuccessor(i);
1875 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
1876 i != e; ++i)
1877 if (i.getCaseValue() == Cond) {
1878 BasicBlock *ReachableBB = i.getCaseSuccessor();
18781879 Worklist.push_back(ReachableBB);
18791880 continue;
18801881 }
21572157 Value *SwitchCond = SI->getCondition();
21582158 BasicBlock *Parent = SI->getParent();
21592159 bool Changed = false;
2160 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
2161 BasicBlock *Dst = SI->getCaseSuccessor(i);
2160 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2161 i != e; ++i) {
2162 BasicBlock *Dst = i.getCaseSuccessor();
21622163 if (isOnlyReachableViaThisEdge(Parent, Dst, DT))
2163 Changed |= propagateEquality(SwitchCond, SI->getCaseValue(i), Dst);
2164 Changed |= propagateEquality(SwitchCond, i.getCaseValue(), Dst);
21642165 }
21652166 return Changed;
21662167 }
10861086 else if (BranchInst *BI = dyn_cast(BB->getTerminator()))
10871087 DestBB = BI->getSuccessor(cast(Val)->isZero());
10881088 else if (SwitchInst *SI = dyn_cast(BB->getTerminator())) {
1089 unsigned ValCase = SI->findCaseValue(cast(Val));
1090 DestBB = SI->getSuccessor(SI->resolveSuccessorIndex(ValCase));
1089 DestBB = SI->findCaseValue(cast(Val)).getCaseSuccessor();
10911090 } else {
10921091 assert(isa(BB->getTerminator())
10931092 && "Unexpected terminator");
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 (unsigned i = 0; i < NumCases; ++i) {
448 Constant* UnswitchValCandidate = SI->getCaseValue(i);
447 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
448 i != e; ++i) {
449 Constant* UnswitchValCandidate = i.getCaseValue();
449450 if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
450451 UnswitchVal = UnswitchValCandidate;
451452 break;
573574 // this.
574575 // Note that we can't trivially unswitch on the default case or
575576 // on already unswitched cases.
576 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
577 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
578 i != e; ++i) {
577579 BasicBlock* LoopExitCandidate;
578580 if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
579 SI->getCaseSuccessor(i)))) {
581 i.getCaseSuccessor()))) {
580582 // Okay, we found a trivial case, remember the value that is trivial.
581 ConstantInt* CaseVal = SI->getCaseValue(i);
583 ConstantInt* CaseVal = i.getCaseValue();
582584
583585 // Check that it was not unswitched before, since already unswitched
584586 // trivial vals are looks trivial too.
11161118 SwitchInst *SI = dyn_cast(U);
11171119 if (SI == 0 || !isa(Val)) continue;
11181120
1119 unsigned DeadCase = SI->findCaseValue(cast(Val));
1121 SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast(Val));
11201122 // Default case is live for multiple values.
1121 if (DeadCase == SwitchInst::ErrorIndex) continue;
1123 if (DeadCase == SI->caseDefault()) continue;
11221124
11231125 // Found a dead case value. Don't remove PHI nodes in the
11241126 // successor if they become single-entry, those PHI nodes may
11251127 // be in the Users list.
11261128
11271129 BasicBlock *Switch = SI->getParent();
1128 BasicBlock *SISucc = SI->getCaseSuccessor(DeadCase);
1130 BasicBlock *SISucc = DeadCase.getCaseSuccessor();
11291131 BasicBlock *Latch = L->getLoopLatch();
11301132
11311133 BranchesInfo.setUnswitched(SI, Val);
11451147 // Compute the successors instead of relying on the return value
11461148 // of SplitEdge, since it may have split the switch successor
11471149 // after PHI nodes.
1148 BasicBlock *NewSISucc = SI->getCaseSuccessor(DeadCase);
1150 BasicBlock *NewSISucc = DeadCase.getCaseSuccessor();
11491151 BasicBlock *OldSISucc = *succ_begin(NewSISucc);
11501152 // Create an "unreachable" destination.
11511153 BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
563563 return;
564564 }
565565
566 Succs[SI->resolveSuccessorIndex(SI->findCaseValue(CI))] = true;
566 Succs[SI->findCaseValue(CI).getSuccessorIndex()] = true;
567567 return;
568568 }
569569
622622 if (CI == 0)
623623 return !SCValue.isUndefined();
624624
625 // Make sure to skip the "default value" which isn't a value
626 for (unsigned i = 0, E = SI->getNumCases(); i != E; ++i)
627 if (SI->getCaseValue(i) == CI) // Found the taken branch.
628 return SI->getCaseSuccessor(i) == To;
629
630 // If the constant value is not equal to any of the branches, we must
631 // execute default branch.
632 return SI->getDefaultDest() == To;
625 return SI->findCaseValue(CI).getCaseSuccessor() == To;
633626 }
634627
635628 // Just mark all destinations executable!
14941487 // If the input to SCCP is actually switch on undef, fix the undef to
14951488 // the first constant.
14961489 if (isa(SI->getCondition())) {
1497 SI->setCondition(SI->getCaseValue(0));
1498 markEdgeExecutable(BB, SI->getCaseSuccessor(0));
1490 SI->setCondition(SI->caseBegin().getCaseValue());
1491 markEdgeExecutable(BB, SI->caseBegin().getCaseSuccessor());
14991492 return true;
15001493 }
15011494
1502 markForcedConstant(SI->getCondition(), SI->getCaseValue(0));
1495 markForcedConstant(SI->getCondition(), SI->caseBegin().getCaseValue());
15031496 return true;
15041497 }
15051498 }
312312 Cond = dyn_cast_or_null(V);
313313 }
314314 if (Cond) { // Constant fold to uncond branch!
315 unsigned CaseIndex = SI->findCaseValue(Cond);
316 BasicBlock *Dest = SI->getSuccessor(SI->resolveSuccessorIndex(CaseIndex));
315 SwitchInst::ConstCaseIt Case = SI->findCaseValue(Cond);
316 BasicBlock *Dest = const_cast(Case.getCaseSuccessor());
317317 VMap[OldTI] = BranchInst::Create(Dest, NewBB);
318318 ToClone.push_back(Dest);
319319 TerminatorDone = true;
616616 // of the other successors.
617617 TheSwitch->setCondition(call);
618618 TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
619 TheSwitch->removeCase(NumExitBlocks-1); // Remove redundant case
619 // Remove redundant case
620 TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1));
620621 break;
621622 }
622623 }
105105 // If we are switching on a constant, we can convert the switch into a
106106 // single branch instruction!
107107 ConstantInt *CI = dyn_cast(SI->getCondition());
108 BasicBlock *TheOnlyDest = SI->getDefaultDest(); // The default dest
108 BasicBlock *TheOnlyDest = SI->getDefaultDest();
109109 BasicBlock *DefaultDest = TheOnlyDest;
110110
111111 // Figure out which case it goes to.
112 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
112 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
113 i != e; ++i) {
113114 // Found case matching a constant operand?
114 if (SI->getCaseValue(i) == CI) {
115 TheOnlyDest = SI->getCaseSuccessor(i);
115 if (i.getCaseValue() == CI) {
116 TheOnlyDest = i.getCaseSuccessor();
116117 break;
117118 }
118119
119120 // Check to see if this branch is going to the same place as the default
120121 // dest. If so, eliminate it as an explicit compare.
121 if (SI->getCaseSuccessor(i) == DefaultDest) {
122 if (i.getCaseSuccessor() == DefaultDest) {
122123 // Remove this entry.
123124 DefaultDest->removePredecessor(SI->getParent());
124125 SI->removeCase(i);
125 --i; --e; // Don't skip an entry...
126 --i; --e;
126127 continue;
127128 }
128129
129130 // Otherwise, check to see if the switch only branches to one destination.
130131 // We do this by reseting "TheOnlyDest" to null when we find two non-equal
131132 // destinations.
132 if (SI->getCaseSuccessor(i) != TheOnlyDest) TheOnlyDest = 0;
133 if (i.getCaseSuccessor() != TheOnlyDest) TheOnlyDest = 0;
133134 }
134135
135136 if (CI && !TheOnlyDest) {
166167 if (SI->getNumCases() == 1) {
167168 // Otherwise, we can fold this switch into a conditional branch
168169 // instruction if it has only one non-default destination.
170 SwitchInst::CaseIt FirstCase = SI->caseBegin();
169171 Value *Cond = Builder.CreateICmpEQ(SI->getCondition(),
170 SI->getCaseValue(0), "cond");
172 FirstCase.getCaseValue(), "cond");
171173
172174 // Insert the new branch.
173 Builder.CreateCondBr(Cond, SI->getCaseSuccessor(0), SI->getDefaultDest());
175 Builder.CreateCondBr(Cond, FirstCase.getCaseSuccessor(),
176 SI->getDefaultDest());
174177
175178 // Delete the old switch.
176179 SI->eraseFromParent();
7272 LLVMContext &Context = CI->getContext();
7373 Type *Int32Ty = Type::getInt32Ty(Context);
7474
75 unsigned caseNo = SI->findCaseValue(ExpectedValue);
75 SwitchInst::CaseIt Case = SI->findCaseValue(ExpectedValue);
7676 std::vector Vec;
7777 unsigned n = SI->getNumCases();
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, SwitchInst::ErrorIndex == caseNo ?
81 Vec[1] = ConstantInt::get(Int32Ty, Case == SI->caseDefault() ?
8282 LikelyBranchWeight : UnlikelyBranchWeight);
8383 for (unsigned i = 0; i < n; ++i) {
84 Vec[i + 1 + 1] = ConstantInt::get(Int32Ty, i == caseNo ?
84 Vec[i + 1 + 1] = ConstantInt::get(Int32Ty, i == Case.getCaseIndex() ?
8585 LikelyBranchWeight : UnlikelyBranchWeight);
8686 }
8787
236236 unsigned numCmps = 0;
237237
238238 // Start with "simple" cases
239 for (unsigned i = 0; i < SI->getNumCases(); ++i)
240 Cases.push_back(CaseRange(SI->getCaseValue(i),
241 SI->getCaseValue(i),
242 SI->getCaseSuccessor(i)));
239 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd(); i != e; ++i)
240 Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
241 i.getCaseSuccessor()));
242
243243 std::sort(Cases.begin(), Cases.end(), CaseCmp());
244244
245245 // Merge case into clusters
479479 BasicBlock*> > &Cases) {
480480 if (SwitchInst *SI = dyn_cast(TI)) {
481481 Cases.reserve(SI->getNumCases());
482 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i)
483 Cases.push_back(std::make_pair(SI->getCaseValue(i),
484 SI->getCaseSuccessor(i)));
482 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd(); i != e; ++i)
483 Cases.push_back(std::make_pair(i.getCaseValue(),
484 i.getCaseSuccessor()));
485485 return SI->getDefaultDest();
486486 }
487487
604604 DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
605605 << "Through successor TI: " << *TI);
606606
607 for (unsigned i = SI->getNumCases(); i != 0;) {
607 for (SwitchInst::CaseIt i = SI->caseEnd(), e = SI->caseBegin(); i != e;) {
608608 --i;
609 if (DeadCases.count(SI->getCaseValue(i))) {
610 SI->getCaseSuccessor(i)->removePredecessor(TI->getParent());
609 if (DeadCases.count(i.getCaseValue())) {
610 i.getCaseSuccessor()->removePredecessor(TI->getParent());
611611 SI->removeCase(i);
612612 }
613613 }
20082008
20092009 // Find the relevant condition and destinations.
20102010 Value *Condition = Select->getCondition();
2011 unsigned TrueCase = SI->findCaseValue(TrueVal);
2012 unsigned FalseCase = SI->findCaseValue(FalseVal);
2013 BasicBlock *TrueBB = SI->getSuccessor(SI->resolveSuccessorIndex(TrueCase));
2014 BasicBlock *FalseBB = SI->getSuccessor(SI->resolveSuccessorIndex(FalseCase));
2011 BasicBlock *TrueBB = SI->findCaseValue(TrueVal).getCaseSuccessor();
2012 BasicBlock *FalseBB = SI->findCaseValue(FalseVal).getCaseSuccessor();
20152013
20162014 // Perform the actual simplification.
20172015 return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB);
20952093 // Ok, the block is reachable from the default dest. If the constant we're
20962094 // comparing exists in one of the other edges, then we can constant fold ICI
20972095 // and zap it.
2098 if (SI->findCaseValue(Cst) != SwitchInst::ErrorIndex) {
2096 if (SI->findCaseValue(Cst) != SI->caseDefault()) {
20992097 Value *V;
21002098 if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
21012099 V = ConstantInt::getFalse(BB->getContext());
24222420 }
24232421 }
24242422 } else if (SwitchInst *SI = dyn_cast(TI)) {
2425 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i)
2426 if (SI->getCaseSuccessor(i) == BB) {
2423 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2424 i != e; ++i)
2425 if (i.getCaseSuccessor() == BB) {
24272426 BB->removePredecessor(SI->getParent());
24282427 SI->removeCase(i);
24292428 --i; --e;
24332432 // destination and make it the default.
24342433 if (SI->getDefaultDest() == BB) {
24352434 std::map > Popularity;
2436 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i) {
2435 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2436 i != e; ++i) {
24372437 std::pair &entry =
2438 Popularity[SI->getCaseSuccessor(i)];
2438 Popularity[i.getCaseSuccessor()];
24392439 if (entry.first == 0) {
24402440 entry.first = 1;
2441 entry.second = i;
2441 entry.second = i.getCaseIndex();
24422442 } else {
24432443 entry.first++;
24442444 }
24692469 for (unsigned i = 0; i != MaxPop-1; ++i)
24702470 MaxBlock->removePredecessor(SI->getParent());
24712471
2472 for (unsigned i = 0, e = SI->getNumCases(); i != e; ++i)
2473 if (SI->getCaseSuccessor(i) == MaxBlock) {
2472 for (SwitchInst::CaseIt i = SI->caseBegin(), e = SI->caseEnd();
2473 i != e; ++i)
2474 if (i.getCaseSuccessor() == MaxBlock) {
24742475 SI->removeCase(i);
24752476 --i; --e;
24762477 }
25162517
25172518 // Make sure all cases point to the same destination and gather the values.
25182519 SmallVector Cases;
2519 Cases.push_back(SI->getCaseValue(0));
2520 for (unsigned I = 1, E = SI->getNumCases(); I != E; ++I) {
2521 if (SI->getCaseSuccessor(I-1) != SI->getCaseSuccessor(I))
2520 SwitchInst::CaseIt I = SI->caseBegin();
2521 Cases.push_back(I.getCaseValue());
2522 SwitchInst::CaseIt PrevI = I++;
2523 for (SwitchInst::CaseIt E = SI->caseEnd(); I != E; PrevI = I++) {
2524 if (PrevI.getCaseSuccessor() != I.getCaseSuccessor())
25222525 return false;
2523 Cases.push_back(SI->getCaseValue(I));
2526 Cases.push_back(I.getCaseValue());
25242527 }
25252528 assert(Cases.size() == SI->getNumCases() && "Not all cases gathered");
25262529
25382541 if (!Offset->isNullValue())
25392542 Sub = Builder.CreateAdd(Sub, Offset, Sub->getName()+".off");
25402543 Value *Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
2541 Builder.CreateCondBr(Cmp, SI->getCaseSuccessor(0), SI->getDefaultDest());
2544 Builder.CreateCondBr(
2545 Cmp, SI->caseBegin().getCaseSuccessor(), SI->getDefaultDest());
25422546
25432547 // Prune obsolete incoming values off the successor's PHI nodes.
2544 for (BasicBlock::iterator BBI = SI->getCaseSuccessor(0)->begin();
2548 for (BasicBlock::iterator BBI = SI->caseBegin().getCaseSuccessor()->begin();
25452549 isa(BBI); ++BBI) {
25462550 for (unsigned I = 0, E = SI->getNumCases()-1; I != E; ++I)
25472551 cast(BBI)->removeIncomingValue(SI->getParent());
25612565
25622566 // Gather dead cases.
25632567 SmallVector DeadCases;
2564 for (unsigned I = 0, E = SI->getNumCases(); I != E; ++I) {
2565 if ((SI->getCaseValue(I)->getValue() & KnownZero) != 0 ||
2566 (SI->getCaseValue(I)->getValue() & KnownOne) != KnownOne) {
2567 DeadCases.push_back(SI->getCaseValue(I));
2568 for (SwitchInst::CaseIt I = SI->caseBegin(), E = SI->caseEnd(); I != E; ++I) {
2569 if ((I.getCaseValue()->getValue() & KnownZero) != 0 ||
2570 (I.getCaseValue()->getValue() & KnownOne) != KnownOne) {
2571 DeadCases.push_back(I.getCaseValue());
25682572 DEBUG(dbgs() << "SimplifyCFG: switch case '"
2569 << SI->getCaseValue(I)->getValue() << "' is dead.\n");
2573 << I.getCaseValue() << "' is dead.\n");
25702574 }
25712575 }
25722576
25732577 // Remove dead cases from the switch.
25742578 for (unsigned I = 0, E = DeadCases.size(); I != E; ++I) {
2575 unsigned Case = SI->findCaseValue(DeadCases[I]);
2576 assert(Case != SwitchInst::ErrorIndex &&
2579 SwitchInst::CaseIt Case = SI->findCaseValue(DeadCases[I]);
2580 assert(Case != SI->caseDefault() &&
25772581 "Case was not found. Probably mistake in DeadCases forming.");
25782582 // Prune unused values from PHI nodes.
2579 SI->getCaseSuccessor(Case)->removePredecessor(SI->getParent());
2583 Case.getCaseSuccessor()->removePredecessor(SI->getParent());
25802584 SI->removeCase(Case);
25812585 }
25822586
26252629 typedef DenseMap > ForwardingNodesMap;
26262630 ForwardingNodesMap ForwardingNodes;
26272631
2628 for (unsigned I = 0; I < SI->getNumCases(); ++I) { // 0 is the default case.
2629 ConstantInt *CaseValue = SI->getCaseValue(I);
2630 BasicBlock *CaseDest = SI->getCaseSuccessor(I);
2632 for (SwitchInst::CaseIt I = SI->caseBegin(), E = SI->caseEnd(); I != E; ++I) {
2633 ConstantInt *CaseValue = I.getCaseValue();
2634 BasicBlock *CaseDest = I.getCaseSuccessor();
26312635
26322636 int PhiIndex;
26332637 PHINode *PHI = FindPHIForConditionForwarding(CaseValue, CaseDest,
17301730 Out << ", ";
17311731 writeOperand(SI.getDefaultDest(), true);
17321732 Out << " [";
1733 unsigned NumCases = SI.getNumCases();
1734 for (unsigned i = 0; i < NumCases; ++i) {
1733 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd();
1734 i != e; ++i) {
17351735 Out << "\n ";
1736 writeOperand(SI.getCaseValue(i), true);
1736 writeOperand(i.getCaseValue(), true);
17371737 Out << ", ";
1738 writeOperand(SI.getCaseSuccessor(i), true);
1738 writeOperand(i.getCaseSuccessor(), true);
17391739 }
17401740 Out << "\n ]";
17411741 } else if (isa(I)) {
31583158 // Initialize some new operands.
31593159 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
31603160 NumOperands = OpNo+2;
3161 setCaseValue(NewCaseIdx, OnVal);
3162 setCaseSuccessor(NewCaseIdx, Dest);
3161 CaseIt Case(this, NewCaseIdx);
3162 Case.setValue(OnVal);
3163 Case.setSuccessor(Dest);
31633164 }
31643165
31653166 /// removeCase - This method removes the specified case and its successor
31663167 /// from the switch instruction.
3167 void SwitchInst::removeCase(unsigned idx) {
3168 void SwitchInst::removeCase(CaseIt i) {
3169 unsigned idx = i.getCaseIndex();
3170
31683171 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
31693172
31703173 unsigned NumOps = getNumOperands();
812812 // have the same type as the switched-on value.
813813 Type *SwitchTy = SI.getCondition()->getType();
814814 SmallPtrSet Constants;
815 for (unsigned i = 0, e = SI.getNumCases(); i != e; ++i) {
816 Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
815 for (SwitchInst::CaseIt i = SI.caseBegin(), e = SI.caseEnd(); i != e; ++i) {
816 Assert1(i.getCaseValue()->getType() == SwitchTy,
817817 "Switch constants must all be same type as switch value!", &SI);
818 Assert2(Constants.insert(SI.getCaseValue(i)),
819 "Duplicate integer as switch case", &SI, SI.getCaseValue(i));
818 Assert2(Constants.insert(i.getCaseValue()),
819 "Duplicate integer as switch case", &SI, i.getCaseValue());
820820 }
821821
822822 visitTerminatorInst(SI);
318318 bool Difference = false;
319319
320320 DenseMap LCases;
321 for (unsigned I = 0, E = LI->getNumCases(); I != E; ++I)
322 LCases[LI->getCaseValue(I)] = LI->getCaseSuccessor(I);
323 for (unsigned I = 0, E = RI->getNumCases(); I != E; ++I) {
324 ConstantInt *CaseValue = RI->getCaseValue(I);
321
322 for (SwitchInst::CaseIt I = LI->caseBegin(), E = LI->caseEnd();
323 I != E; ++I)
324 LCases[I.getCaseValue()] = I.getCaseSuccessor();
325
326 for (SwitchInst::CaseIt I = RI->caseBegin(), E = RI->caseEnd();
327 I != E; ++I) {
328 ConstantInt *CaseValue = I.getCaseValue();
325329 BasicBlock *LCase = LCases[CaseValue];
326330 if (LCase) {
327 if (TryUnify) tryUnify(LCase, RI->getCaseSuccessor(I));
331 if (TryUnify) tryUnify(LCase, I.getCaseSuccessor());
328332 LCases.erase(CaseValue);
329333 } else if (Complain || !Difference) {
330334 if (Complain)