2003-04-23 16:23:59 +00:00
|
|
|
//===- LowerSwitch.cpp - Eliminate Switch instructions --------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-04-23 16:23:59 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// The LowerSwitch transformation rewrites switch instructions with a sequence
|
|
|
|
// of branches, which allows targets to get away with not implementing the
|
|
|
|
// switch instruction until it is convenient.
|
2003-04-23 16:23:59 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-03-23 19:32:43 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2003-04-23 16:23:59 +00:00
|
|
|
#include "llvm/Pass.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-10-25 06:57:41 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2008-08-23 22:23:09 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-03-23 19:32:43 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2004-09-03 18:19:51 +00:00
|
|
|
#include <algorithm>
|
2004-01-09 06:02:20 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2014-04-21 22:55:11 +00:00
|
|
|
#define DEBUG_TYPE "lower-switch"
|
|
|
|
|
2003-04-23 16:23:59 +00:00
|
|
|
namespace {
|
2015-01-23 20:43:51 +00:00
|
|
|
struct IntRange {
|
|
|
|
int64_t Low, High;
|
|
|
|
};
|
|
|
|
// Return true iff R is covered by Ranges.
|
|
|
|
static bool IsInRanges(const IntRange &R,
|
|
|
|
const std::vector<IntRange> &Ranges) {
|
|
|
|
// Note: Ranges must be sorted, non-overlapping and non-adjacent.
|
|
|
|
|
|
|
|
// Find the first range whose High field is >= R.High,
|
|
|
|
// then check if the Low field is <= R.Low. If so, we
|
|
|
|
// have a Range that covers R.
|
|
|
|
auto I = std::lower_bound(
|
|
|
|
Ranges.begin(), Ranges.end(), R,
|
|
|
|
[](const IntRange &A, const IntRange &B) { return A.High < B.High; });
|
|
|
|
return I != Ranges.end() && I->Low <= R.Low;
|
|
|
|
}
|
|
|
|
|
2003-04-23 16:23:59 +00:00
|
|
|
/// LowerSwitch Pass - Replace all SwitchInst instructions with chained branch
|
2010-08-18 02:41:56 +00:00
|
|
|
/// instructions.
|
2009-10-25 06:33:48 +00:00
|
|
|
class LowerSwitch : public FunctionPass {
|
2003-10-07 18:46:23 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
LowerSwitch() : FunctionPass(ID) {
|
|
|
|
initializeLowerSwitchPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2007-04-16 18:10:23 +00:00
|
|
|
// This is a cluster of orthogonal Transforms
|
2006-05-09 04:13:41 +00:00
|
|
|
AU.addPreserved<UnifyFunctionExitNodes>();
|
2006-05-17 21:05:27 +00:00
|
|
|
AU.addPreservedID(LowerInvokePassID);
|
2006-05-09 04:13:41 +00:00
|
|
|
}
|
2007-03-10 16:46:28 +00:00
|
|
|
|
|
|
|
struct CaseRange {
|
2015-02-05 16:58:10 +00:00
|
|
|
ConstantInt* Low;
|
|
|
|
ConstantInt* High;
|
2007-03-10 16:46:28 +00:00
|
|
|
BasicBlock* BB;
|
|
|
|
|
2015-02-05 16:58:10 +00:00
|
|
|
CaseRange(ConstantInt *low, ConstantInt *high, BasicBlock *bb)
|
2015-02-05 16:50:27 +00:00
|
|
|
: Low(low), High(high), BB(bb) {}
|
2007-03-10 16:46:28 +00:00
|
|
|
};
|
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
typedef std::vector<CaseRange> CaseVector;
|
2007-03-10 16:46:28 +00:00
|
|
|
typedef std::vector<CaseRange>::iterator CaseItr;
|
2003-10-07 18:46:23 +00:00
|
|
|
private:
|
2003-04-23 16:23:59 +00:00
|
|
|
void processSwitchInst(SwitchInst *SI);
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
BasicBlock *switchConvert(CaseItr Begin, CaseItr End,
|
|
|
|
ConstantInt *LowerBound, ConstantInt *UpperBound,
|
2014-07-11 10:34:36 +00:00
|
|
|
Value *Val, BasicBlock *Predecessor,
|
2015-01-23 20:43:51 +00:00
|
|
|
BasicBlock *OrigBlock, BasicBlock *Default,
|
|
|
|
const std::vector<IntRange> &UnreachableRanges);
|
2014-06-16 16:55:20 +00:00
|
|
|
BasicBlock *newLeafBlock(CaseRange &Leaf, Value *Val, BasicBlock *OrigBlock,
|
|
|
|
BasicBlock *Default);
|
|
|
|
unsigned Clusterify(CaseVector &Cases, SwitchInst *SI);
|
2003-10-07 18:46:23 +00:00
|
|
|
};
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
|
|
|
|
/// The comparison function for sorting the switch case values in the vector.
|
|
|
|
/// WARNING: Case ranges should be disjoint!
|
|
|
|
struct CaseCmp {
|
|
|
|
bool operator () (const LowerSwitch::CaseRange& C1,
|
|
|
|
const LowerSwitch::CaseRange& C2) {
|
|
|
|
|
|
|
|
const ConstantInt* CI1 = cast<const ConstantInt>(C1.Low);
|
|
|
|
const ConstantInt* CI2 = cast<const ConstantInt>(C2.High);
|
|
|
|
return CI1->getValue().slt(CI2->getValue());
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2003-04-23 16:23:59 +00:00
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char LowerSwitch::ID = 0;
|
2010-08-23 17:52:01 +00:00
|
|
|
INITIALIZE_PASS(LowerSwitch, "lowerswitch",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Lower SwitchInst's to branches", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2011-04-15 05:18:47 +00:00
|
|
|
// Publicly exposed interface to pass...
|
2010-08-06 18:33:48 +00:00
|
|
|
char &llvm::LowerSwitchID = LowerSwitch::ID;
|
2003-04-23 16:23:59 +00:00
|
|
|
// createLowerSwitchPass - Interface to this file...
|
2004-01-09 06:02:20 +00:00
|
|
|
FunctionPass *llvm::createLowerSwitchPass() {
|
2003-04-23 16:23:59 +00:00
|
|
|
return new LowerSwitch();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerSwitch::runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
|
|
|
|
BasicBlock *Cur = I++; // Advance over block so we don't traverse new blocks
|
|
|
|
|
|
|
|
if (SwitchInst *SI = dyn_cast<SwitchInst>(Cur->getTerminator())) {
|
|
|
|
Changed = true;
|
|
|
|
processSwitchInst(SI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
// operator<< - Used for debugging purposes.
|
|
|
|
//
|
2009-07-27 23:33:34 +00:00
|
|
|
static raw_ostream& operator<<(raw_ostream &O,
|
2010-10-23 08:10:43 +00:00
|
|
|
const LowerSwitch::CaseVector &C)
|
|
|
|
LLVM_ATTRIBUTE_USED;
|
2009-07-24 10:36:58 +00:00
|
|
|
static raw_ostream& operator<<(raw_ostream &O,
|
|
|
|
const LowerSwitch::CaseVector &C) {
|
2003-10-07 18:46:23 +00:00
|
|
|
O << "[";
|
|
|
|
|
2007-03-10 16:46:28 +00:00
|
|
|
for (LowerSwitch::CaseVector::const_iterator B = C.begin(),
|
2004-01-09 06:02:20 +00:00
|
|
|
E = C.end(); B != E; ) {
|
2007-03-10 16:46:28 +00:00
|
|
|
O << *B->Low << " -" << *B->High;
|
2003-10-07 18:46:23 +00:00
|
|
|
if (++B != E) O << ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return O << "]";
|
|
|
|
}
|
2007-03-10 16:46:28 +00:00
|
|
|
|
2014-11-28 19:47:33 +00:00
|
|
|
// \brief Update the first occurrence of the "switch statement" BB in the PHI
|
|
|
|
// node with the "new" BB. The other occurrences will:
|
|
|
|
//
|
|
|
|
// 1) Be updated by subsequent calls to this function. Switch statements may
|
|
|
|
// have more than one outcoming edge into the same BB if they all have the same
|
|
|
|
// value. When the switch statement is converted these incoming edges are now
|
|
|
|
// coming from multiple BBs.
|
|
|
|
// 2) Removed if subsequent incoming values now share the same case, i.e.,
|
|
|
|
// multiple outcome edges are condensed into one. This is necessary to keep the
|
|
|
|
// number of phi values equal to the number of branches to SuccBB.
|
|
|
|
static void fixPhis(BasicBlock *SuccBB, BasicBlock *OrigBB, BasicBlock *NewBB,
|
|
|
|
unsigned NumMergedCases) {
|
|
|
|
for (BasicBlock::iterator I = SuccBB->begin(), IE = SuccBB->getFirstNonPHI();
|
|
|
|
I != IE; ++I) {
|
2014-07-11 10:34:36 +00:00
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
|
2014-11-10 21:05:27 +00:00
|
|
|
// Only update the first occurence.
|
2014-11-28 19:47:33 +00:00
|
|
|
unsigned Idx = 0, E = PN->getNumIncomingValues();
|
2014-12-02 18:31:53 +00:00
|
|
|
unsigned LocalNumMergedCases = NumMergedCases;
|
2014-11-28 19:47:33 +00:00
|
|
|
for (; Idx != E; ++Idx) {
|
2014-11-10 21:05:27 +00:00
|
|
|
if (PN->getIncomingBlock(Idx) == OrigBB) {
|
|
|
|
PN->setIncomingBlock(Idx, NewBB);
|
|
|
|
break;
|
|
|
|
}
|
2014-07-11 10:34:36 +00:00
|
|
|
}
|
2014-11-28 19:47:33 +00:00
|
|
|
|
|
|
|
// Remove additional occurences coming from condensed cases and keep the
|
|
|
|
// number of incoming values equal to the number of branches to SuccBB.
|
2015-03-17 18:03:10 +00:00
|
|
|
SmallVector<unsigned, 8> Indices;
|
2014-12-02 18:31:53 +00:00
|
|
|
for (++Idx; LocalNumMergedCases > 0 && Idx < E; ++Idx)
|
2014-11-28 19:47:33 +00:00
|
|
|
if (PN->getIncomingBlock(Idx) == OrigBB) {
|
2015-03-17 18:03:10 +00:00
|
|
|
Indices.push_back(Idx);
|
2014-12-02 18:31:53 +00:00
|
|
|
LocalNumMergedCases--;
|
2014-11-28 19:47:33 +00:00
|
|
|
}
|
2015-03-17 18:03:10 +00:00
|
|
|
// Remove incoming values in the reverse order to prevent invalidating
|
|
|
|
// *successive* index.
|
|
|
|
for (auto III = Indices.rbegin(), IIE = Indices.rend(); III != IIE; ++III)
|
|
|
|
PN->removeIncomingValue(*III);
|
2014-07-11 10:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
// switchConvert - Convert the switch statement into a binary lookup of
|
|
|
|
// the case values. The function recursively builds this tree.
|
2014-06-16 16:55:20 +00:00
|
|
|
// LowerBound and UpperBound are used to keep track of the bounds for Val
|
|
|
|
// that have already been checked by a block emitted by one of the previous
|
|
|
|
// calls to switchConvert in the call stack.
|
2015-01-23 20:43:51 +00:00
|
|
|
BasicBlock *
|
|
|
|
LowerSwitch::switchConvert(CaseItr Begin, CaseItr End, ConstantInt *LowerBound,
|
|
|
|
ConstantInt *UpperBound, Value *Val,
|
|
|
|
BasicBlock *Predecessor, BasicBlock *OrigBlock,
|
|
|
|
BasicBlock *Default,
|
|
|
|
const std::vector<IntRange> &UnreachableRanges) {
|
2003-10-07 18:46:23 +00:00
|
|
|
unsigned Size = End - Begin;
|
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
if (Size == 1) {
|
|
|
|
// Check if the Case Range is perfectly squeezed in between
|
|
|
|
// already checked Upper and Lower bounds. If it is then we can avoid
|
|
|
|
// emitting the code that checks if the value actually falls in the range
|
|
|
|
// because the bounds already tell us so.
|
|
|
|
if (Begin->Low == LowerBound && Begin->High == UpperBound) {
|
2014-11-28 19:47:33 +00:00
|
|
|
unsigned NumMergedCases = 0;
|
|
|
|
if (LowerBound && UpperBound)
|
|
|
|
NumMergedCases =
|
|
|
|
UpperBound->getSExtValue() - LowerBound->getSExtValue();
|
|
|
|
fixPhis(Begin->BB, OrigBlock, Predecessor, NumMergedCases);
|
2014-06-16 16:55:20 +00:00
|
|
|
return Begin->BB;
|
|
|
|
}
|
2003-10-07 18:46:23 +00:00
|
|
|
return newLeafBlock(*Begin, Val, OrigBlock, Default);
|
2014-06-16 16:55:20 +00:00
|
|
|
}
|
2003-10-07 18:46:23 +00:00
|
|
|
|
|
|
|
unsigned Mid = Size / 2;
|
2007-03-10 16:46:28 +00:00
|
|
|
std::vector<CaseRange> LHS(Begin, Begin + Mid);
|
2010-01-05 01:26:45 +00:00
|
|
|
DEBUG(dbgs() << "LHS: " << LHS << "\n");
|
2007-03-10 16:46:28 +00:00
|
|
|
std::vector<CaseRange> RHS(Begin + Mid, End);
|
2010-01-05 01:26:45 +00:00
|
|
|
DEBUG(dbgs() << "RHS: " << RHS << "\n");
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
CaseRange &Pivot = *(Begin + Mid);
|
|
|
|
DEBUG(dbgs() << "Pivot ==> "
|
2015-02-05 16:58:10 +00:00
|
|
|
<< Pivot.Low->getValue()
|
|
|
|
<< " -" << Pivot.High->getValue() << "\n");
|
2014-06-16 16:55:20 +00:00
|
|
|
|
|
|
|
// NewLowerBound here should never be the integer minimal value.
|
|
|
|
// This is because it is computed from a case range that is never
|
|
|
|
// the smallest, so there is always a case range that has at least
|
|
|
|
// a smaller value.
|
2015-02-05 16:58:10 +00:00
|
|
|
ConstantInt *NewLowerBound = Pivot.Low;
|
2015-01-23 20:43:51 +00:00
|
|
|
|
|
|
|
// Because NewLowerBound is never the smallest representable integer
|
|
|
|
// it is safe here to subtract one.
|
|
|
|
ConstantInt *NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
|
|
|
|
NewLowerBound->getValue() - 1);
|
|
|
|
|
|
|
|
if (!UnreachableRanges.empty()) {
|
|
|
|
// Check if the gap between LHS's highest and NewLowerBound is unreachable.
|
2015-02-05 16:58:10 +00:00
|
|
|
int64_t GapLow = LHS.back().High->getSExtValue() + 1;
|
2015-01-23 20:43:51 +00:00
|
|
|
int64_t GapHigh = NewLowerBound->getSExtValue() - 1;
|
|
|
|
IntRange Gap = { GapLow, GapHigh };
|
|
|
|
if (GapHigh >= GapLow && IsInRanges(Gap, UnreachableRanges))
|
2015-02-05 16:58:10 +00:00
|
|
|
NewUpperBound = LHS.back().High;
|
2014-06-16 16:55:20 +00:00
|
|
|
}
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
DEBUG(dbgs() << "LHS Bounds ==> ";
|
|
|
|
if (LowerBound) {
|
2015-02-05 16:58:10 +00:00
|
|
|
dbgs() << LowerBound->getSExtValue();
|
2014-06-16 16:55:20 +00:00
|
|
|
} else {
|
|
|
|
dbgs() << "NONE";
|
|
|
|
}
|
|
|
|
dbgs() << " - " << NewUpperBound->getSExtValue() << "\n";
|
|
|
|
dbgs() << "RHS Bounds ==> ";
|
|
|
|
dbgs() << NewLowerBound->getSExtValue() << " - ";
|
|
|
|
if (UpperBound) {
|
2015-02-05 16:58:10 +00:00
|
|
|
dbgs() << UpperBound->getSExtValue() << "\n";
|
2014-06-16 16:55:20 +00:00
|
|
|
} else {
|
|
|
|
dbgs() << "NONE\n";
|
|
|
|
});
|
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
// Create a new node that checks if the value is < pivot. Go to the
|
|
|
|
// left branch if it is and right branch if not.
|
|
|
|
Function* F = OrigBlock->getParent();
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock* NewNode = BasicBlock::Create(Val->getContext(), "NodeBlock");
|
2003-10-07 18:46:23 +00:00
|
|
|
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
ICmpInst* Comp = new ICmpInst(ICmpInst::ICMP_SLT,
|
2009-07-09 23:48:35 +00:00
|
|
|
Val, Pivot.Low, "Pivot");
|
2014-07-11 10:34:36 +00:00
|
|
|
|
|
|
|
BasicBlock *LBranch = switchConvert(LHS.begin(), LHS.end(), LowerBound,
|
|
|
|
NewUpperBound, Val, NewNode, OrigBlock,
|
2015-01-23 20:43:51 +00:00
|
|
|
Default, UnreachableRanges);
|
2014-07-11 10:34:36 +00:00
|
|
|
BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
|
|
|
|
UpperBound, Val, NewNode, OrigBlock,
|
2015-01-23 20:43:51 +00:00
|
|
|
Default, UnreachableRanges);
|
2014-07-11 10:34:36 +00:00
|
|
|
|
|
|
|
Function::iterator FI = OrigBlock;
|
|
|
|
F->getBasicBlockList().insert(++FI, NewNode);
|
2003-10-07 18:46:23 +00:00
|
|
|
NewNode->getInstList().push_back(Comp);
|
2014-07-11 10:34:36 +00:00
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(LBranch, RBranch, Comp, NewNode);
|
2003-10-07 18:46:23 +00:00
|
|
|
return NewNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLeafBlock - Create a new leaf block for the binary lookup tree. It
|
|
|
|
// checks if the switch's value == the case's value. If not, then it
|
|
|
|
// jumps to the default branch. At this point in the tree, the value
|
|
|
|
// can't be another valid case value, so the jump to the "default" branch
|
|
|
|
// is warranted.
|
|
|
|
//
|
2007-03-10 16:46:28 +00:00
|
|
|
BasicBlock* LowerSwitch::newLeafBlock(CaseRange& Leaf, Value* Val,
|
2003-10-07 18:46:23 +00:00
|
|
|
BasicBlock* OrigBlock,
|
|
|
|
BasicBlock* Default)
|
|
|
|
{
|
|
|
|
Function* F = OrigBlock->getParent();
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock* NewLeaf = BasicBlock::Create(Val->getContext(), "LeafBlock");
|
2007-04-17 18:09:47 +00:00
|
|
|
Function::iterator FI = OrigBlock;
|
|
|
|
F->getBasicBlockList().insert(++FI, NewLeaf);
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2007-03-10 16:46:28 +00:00
|
|
|
// Emit comparison
|
2014-04-25 05:29:35 +00:00
|
|
|
ICmpInst* Comp = nullptr;
|
2007-03-10 16:46:28 +00:00
|
|
|
if (Leaf.Low == Leaf.High) {
|
|
|
|
// Make the seteq instruction...
|
2009-07-09 23:48:35 +00:00
|
|
|
Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_EQ, Val,
|
|
|
|
Leaf.Low, "SwitchLeaf");
|
2007-03-10 16:46:28 +00:00
|
|
|
} else {
|
|
|
|
// Make range comparison
|
2015-02-05 16:58:10 +00:00
|
|
|
if (Leaf.Low->isMinValue(true /*isSigned*/)) {
|
2007-03-10 16:46:28 +00:00
|
|
|
// Val >= Min && Val <= Hi --> Val <= Hi
|
2009-07-09 23:48:35 +00:00
|
|
|
Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_SLE, Val, Leaf.High,
|
|
|
|
"SwitchLeaf");
|
2015-02-05 16:58:10 +00:00
|
|
|
} else if (Leaf.Low->isZero()) {
|
2007-03-10 16:46:28 +00:00
|
|
|
// Val >= 0 && Val <= Hi --> Val <=u Hi
|
2009-07-09 23:48:35 +00:00
|
|
|
Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Val, Leaf.High,
|
|
|
|
"SwitchLeaf");
|
2007-03-10 16:46:28 +00:00
|
|
|
} else {
|
|
|
|
// Emit V-Lo <=u Hi-Lo
|
2009-07-29 18:55:55 +00:00
|
|
|
Constant* NegLo = ConstantExpr::getNeg(Leaf.Low);
|
2008-05-16 19:29:10 +00:00
|
|
|
Instruction* Add = BinaryOperator::CreateAdd(Val, NegLo,
|
2007-03-10 16:46:28 +00:00
|
|
|
Val->getName()+".off",
|
|
|
|
NewLeaf);
|
2009-07-29 18:55:55 +00:00
|
|
|
Constant *UpperBound = ConstantExpr::getAdd(NegLo, Leaf.High);
|
2009-07-09 23:48:35 +00:00
|
|
|
Comp = new ICmpInst(*NewLeaf, ICmpInst::ICMP_ULE, Add, UpperBound,
|
|
|
|
"SwitchLeaf");
|
2007-03-10 16:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-07 18:46:23 +00:00
|
|
|
|
|
|
|
// Make the conditional branch...
|
2007-03-10 16:46:28 +00:00
|
|
|
BasicBlock* Succ = Leaf.BB;
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(Succ, Default, Comp, NewLeaf);
|
2003-10-07 18:46:23 +00:00
|
|
|
|
|
|
|
// If there were any PHI nodes in this successor, rewrite one entry
|
|
|
|
// from OrigBlock to come from NewLeaf.
|
2004-09-15 17:06:42 +00:00
|
|
|
for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode* PN = cast<PHINode>(I);
|
2007-03-10 16:46:28 +00:00
|
|
|
// Remove all but one incoming entries from the cluster
|
2015-02-05 16:58:10 +00:00
|
|
|
uint64_t Range = Leaf.High->getSExtValue() -
|
|
|
|
Leaf.Low->getSExtValue();
|
2007-03-10 16:46:28 +00:00
|
|
|
for (uint64_t j = 0; j < Range; ++j) {
|
|
|
|
PN->removeIncomingValue(OrigBlock);
|
|
|
|
}
|
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
|
|
|
|
assert(BlockIdx != -1 && "Switch didn't go to this successor??");
|
|
|
|
PN->setIncomingBlock((unsigned)BlockIdx, NewLeaf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewLeaf;
|
|
|
|
}
|
|
|
|
|
2007-03-10 16:46:28 +00:00
|
|
|
// Clusterify - Transform simple list of Cases into list of CaseRange's
|
|
|
|
unsigned LowerSwitch::Clusterify(CaseVector& Cases, SwitchInst *SI) {
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
unsigned numCmps = 0;
|
2007-03-10 16:46:28 +00:00
|
|
|
|
|
|
|
// Start with "simple" cases
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
|
|
|
|
Cases.push_back(CaseRange(i.getCaseValue(), i.getCaseValue(),
|
|
|
|
i.getCaseSuccessor()));
|
2012-05-24 09:33:20 +00:00
|
|
|
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
std::sort(Cases.begin(), Cases.end(), CaseCmp());
|
|
|
|
|
|
|
|
// Merge case into clusters
|
2015-06-20 15:59:34 +00:00
|
|
|
if (Cases.size() >= 2) {
|
|
|
|
CaseItr I = Cases.begin();
|
|
|
|
for (CaseItr J = std::next(I), E = Cases.end(); J != E; ++J) {
|
2015-02-05 16:58:10 +00:00
|
|
|
int64_t nextValue = J->Low->getSExtValue();
|
|
|
|
int64_t currentValue = I->High->getSExtValue();
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
BasicBlock* nextBB = J->BB;
|
|
|
|
BasicBlock* currentBB = I->BB;
|
|
|
|
|
|
|
|
// If the two neighboring cases go to the same destination, merge them
|
|
|
|
// into a single case.
|
2015-06-20 00:28:25 +00:00
|
|
|
assert(nextValue > currentValue && "Cases should be strictly ascending");
|
|
|
|
if ((nextValue == currentValue + 1) && (currentBB == nextBB)) {
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
I->High = J->High;
|
2015-06-20 15:59:34 +00:00
|
|
|
// FIXME: Combine branch weights.
|
|
|
|
} else if (++I != J) {
|
|
|
|
*I = *J;
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-20 15:59:34 +00:00
|
|
|
Cases.erase(std::next(I), Cases.end());
|
|
|
|
}
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
|
|
|
|
for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
|
|
|
|
if (I->Low != I->High)
|
2007-03-10 16:46:28 +00:00
|
|
|
// A range counts double, since it requires two compares.
|
|
|
|
++numCmps;
|
|
|
|
}
|
|
|
|
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
return numCmps;
|
2007-03-10 16:46:28 +00:00
|
|
|
}
|
|
|
|
|
2003-04-23 16:23:59 +00:00
|
|
|
// processSwitchInst - Replace the specified switch instruction with a sequence
|
2003-10-07 18:46:23 +00:00
|
|
|
// of chained if-then insts in a balanced binary search.
|
2003-04-23 16:23:59 +00:00
|
|
|
//
|
|
|
|
void LowerSwitch::processSwitchInst(SwitchInst *SI) {
|
|
|
|
BasicBlock *CurBlock = SI->getParent();
|
|
|
|
BasicBlock *OrigBlock = CurBlock;
|
|
|
|
Function *F = CurBlock->getParent();
|
2011-09-29 20:21:17 +00:00
|
|
|
Value *Val = SI->getCondition(); // The value we are switching on...
|
2003-10-07 18:46:23 +00:00
|
|
|
BasicBlock* Default = SI->getDefaultDest();
|
2003-04-23 16:23:59 +00:00
|
|
|
|
2015-01-23 20:43:51 +00:00
|
|
|
// If there is only the default destination, just branch.
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149481 91177308-0d34-0410-b5e6-96231b3b80d8
2012-02-01 07:49:51 +00:00
|
|
|
if (!SI->getNumCases()) {
|
2015-01-23 20:43:51 +00:00
|
|
|
BranchInst::Create(Default, CurBlock);
|
|
|
|
SI->eraseFromParent();
|
2003-08-23 22:54:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-23 20:43:51 +00:00
|
|
|
// Prepare cases vector.
|
|
|
|
CaseVector Cases;
|
|
|
|
unsigned numCmps = Clusterify(Cases, SI);
|
|
|
|
DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
|
|
|
|
<< ". Total compares: " << numCmps << "\n");
|
|
|
|
DEBUG(dbgs() << "Cases: " << Cases << "\n");
|
|
|
|
(void)numCmps;
|
|
|
|
|
|
|
|
ConstantInt *LowerBound = nullptr;
|
|
|
|
ConstantInt *UpperBound = nullptr;
|
|
|
|
std::vector<IntRange> UnreachableRanges;
|
|
|
|
|
|
|
|
if (isa<UnreachableInst>(Default->getFirstNonPHIOrDbg())) {
|
|
|
|
// Make the bounds tightly fitted around the case value range, becase we
|
|
|
|
// know that the value passed to the switch must be exactly one of the case
|
|
|
|
// values.
|
|
|
|
assert(!Cases.empty());
|
2015-02-05 16:58:10 +00:00
|
|
|
LowerBound = Cases.front().Low;
|
|
|
|
UpperBound = Cases.back().High;
|
2015-01-23 20:43:51 +00:00
|
|
|
|
|
|
|
DenseMap<BasicBlock *, unsigned> Popularity;
|
|
|
|
unsigned MaxPop = 0;
|
|
|
|
BasicBlock *PopSucc = nullptr;
|
|
|
|
|
|
|
|
IntRange R = { INT64_MIN, INT64_MAX };
|
|
|
|
UnreachableRanges.push_back(R);
|
|
|
|
for (const auto &I : Cases) {
|
2015-02-05 16:58:10 +00:00
|
|
|
int64_t Low = I.Low->getSExtValue();
|
|
|
|
int64_t High = I.High->getSExtValue();
|
2015-01-23 20:43:51 +00:00
|
|
|
|
|
|
|
IntRange &LastRange = UnreachableRanges.back();
|
|
|
|
if (LastRange.Low == Low) {
|
|
|
|
// There is nothing left of the previous range.
|
|
|
|
UnreachableRanges.pop_back();
|
|
|
|
} else {
|
|
|
|
// Terminate the previous range.
|
|
|
|
assert(Low > LastRange.Low);
|
|
|
|
LastRange.High = Low - 1;
|
|
|
|
}
|
|
|
|
if (High != INT64_MAX) {
|
|
|
|
IntRange R = { High + 1, INT64_MAX };
|
|
|
|
UnreachableRanges.push_back(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count popularity.
|
|
|
|
int64_t N = High - Low + 1;
|
|
|
|
unsigned &Pop = Popularity[I.BB];
|
|
|
|
if ((Pop += N) > MaxPop) {
|
|
|
|
MaxPop = Pop;
|
|
|
|
PopSucc = I.BB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
/* UnreachableRanges should be sorted and the ranges non-adjacent. */
|
|
|
|
for (auto I = UnreachableRanges.begin(), E = UnreachableRanges.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
assert(I->Low <= I->High);
|
|
|
|
auto Next = I + 1;
|
|
|
|
if (Next != E) {
|
|
|
|
assert(Next->Low > I->High);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Use the most popular block as the new default, reducing the number of
|
|
|
|
// cases.
|
|
|
|
assert(MaxPop > 0 && PopSucc);
|
|
|
|
Default = PopSucc;
|
2015-06-20 15:59:34 +00:00
|
|
|
Cases.erase(std::remove_if(
|
|
|
|
Cases.begin(), Cases.end(),
|
|
|
|
[PopSucc](const CaseRange &R) { return R.BB == PopSucc; }),
|
|
|
|
Cases.end());
|
2015-01-23 20:43:51 +00:00
|
|
|
|
|
|
|
// If there are no cases left, just branch.
|
|
|
|
if (Cases.empty()) {
|
|
|
|
BranchInst::Create(Default, CurBlock);
|
|
|
|
SI->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
// Create a new, empty default block so that the new hierarchy of
|
|
|
|
// if-then statements go to this and the PHI nodes are happy.
|
2015-01-23 20:43:51 +00:00
|
|
|
BasicBlock *NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
|
|
|
|
F->getBasicBlockList().insert(Default, NewDefault);
|
|
|
|
BranchInst::Create(Default, NewDefault);
|
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
// If there is an entry in any PHI nodes for the default edge, make sure
|
|
|
|
// to update them as well.
|
2004-09-15 17:06:42 +00:00
|
|
|
for (BasicBlock::iterator I = Default->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2003-10-07 18:46:23 +00:00
|
|
|
int BlockIdx = PN->getBasicBlockIndex(OrigBlock);
|
|
|
|
assert(BlockIdx != -1 && "Switch didn't go to this successor??");
|
|
|
|
PN->setIncomingBlock((unsigned)BlockIdx, NewDefault);
|
2003-04-23 16:23:59 +00:00
|
|
|
}
|
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
BasicBlock *SwitchBlock =
|
|
|
|
switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
|
2015-01-23 20:43:51 +00:00
|
|
|
OrigBlock, OrigBlock, NewDefault, UnreachableRanges);
|
2003-10-07 18:46:23 +00:00
|
|
|
|
|
|
|
// Branch to our shiny new if-then stuff...
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(SwitchBlock, OrigBlock);
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2003-04-23 16:23:59 +00:00
|
|
|
// We are now done with the switch instruction, delete it.
|
2015-01-23 20:43:51 +00:00
|
|
|
BasicBlock *OldDefault = SI->getDefaultDest();
|
2004-03-14 04:14:31 +00:00
|
|
|
CurBlock->getInstList().erase(SI);
|
2014-06-16 16:55:20 +00:00
|
|
|
|
2015-01-23 20:43:51 +00:00
|
|
|
// If the Default block has no more predecessors just remove it.
|
|
|
|
if (pred_begin(OldDefault) == pred_end(OldDefault))
|
|
|
|
DeleteDeadBlock(OldDefault);
|
2003-04-23 16:23:59 +00:00
|
|
|
}
|