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"
|
2014-06-16 16:55:20 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/STLExtras.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"
|
2014-06-16 16:55:20 +00:00
|
|
|
#include "llvm/IR/CFG.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"
|
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 {
|
|
|
|
/// 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>();
|
2010-08-06 21:48:06 +00:00
|
|
|
AU.addPreserved("mem2reg");
|
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 {
|
|
|
|
Constant* Low;
|
|
|
|
Constant* High;
|
|
|
|
BasicBlock* BB;
|
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
CaseRange(Constant *low = nullptr, Constant *high = nullptr,
|
|
|
|
BasicBlock *bb = nullptr) :
|
2007-03-12 17:56: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,
|
|
|
|
BasicBlock *OrigBlock, BasicBlock *Default);
|
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());
|
|
|
|
}
|
|
|
|
};
|
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-07-11 10:34:36 +00:00
|
|
|
static void fixPhis(BasicBlock *Succ,
|
|
|
|
BasicBlock *OrigBlock,
|
|
|
|
BasicBlock *NewNode) {
|
|
|
|
for (BasicBlock::iterator I = Succ->begin(),
|
|
|
|
E = Succ->getFirstNonPHI();
|
|
|
|
I != E; ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
|
|
|
|
|
|
|
for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
|
|
|
|
if (PN->getIncomingBlock(I) == OrigBlock)
|
|
|
|
PN->setIncomingBlock(I, NewNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
BasicBlock *LowerSwitch::switchConvert(CaseItr Begin, CaseItr End,
|
|
|
|
ConstantInt *LowerBound,
|
|
|
|
ConstantInt *UpperBound, Value *Val,
|
2014-07-11 10:34:36 +00:00
|
|
|
BasicBlock *Predecessor,
|
2014-06-16 16:55:20 +00:00
|
|
|
BasicBlock *OrigBlock,
|
|
|
|
BasicBlock *Default) {
|
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-07-11 10:34:36 +00:00
|
|
|
fixPhis(Begin->BB, OrigBlock, Predecessor);
|
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 ==> "
|
|
|
|
<< cast<ConstantInt>(Pivot.Low)->getValue()
|
|
|
|
<< " -" << cast<ConstantInt>(Pivot.High)->getValue() << "\n");
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ConstantInt *NewLowerBound = cast<ConstantInt>(Pivot.Low);
|
|
|
|
ConstantInt *NewUpperBound;
|
|
|
|
|
|
|
|
// If we don't have a Default block then it means that we can never
|
|
|
|
// have a value outside of a case range, so set the UpperBound to the highest
|
|
|
|
// value in the LHS part of the case ranges.
|
|
|
|
if (Default != nullptr) {
|
|
|
|
// Because NewLowerBound is never the smallest representable integer
|
|
|
|
// it is safe here to subtract one.
|
|
|
|
NewUpperBound = ConstantInt::get(NewLowerBound->getContext(),
|
|
|
|
NewLowerBound->getValue() - 1);
|
|
|
|
} else {
|
|
|
|
CaseItr LastLHS = LHS.begin() + LHS.size() - 1;
|
|
|
|
NewUpperBound = cast<ConstantInt>(LastLHS->High);
|
|
|
|
}
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
DEBUG(dbgs() << "LHS Bounds ==> ";
|
|
|
|
if (LowerBound) {
|
|
|
|
dbgs() << cast<ConstantInt>(LowerBound)->getSExtValue();
|
|
|
|
} else {
|
|
|
|
dbgs() << "NONE";
|
|
|
|
}
|
|
|
|
dbgs() << " - " << NewUpperBound->getSExtValue() << "\n";
|
|
|
|
dbgs() << "RHS Bounds ==> ";
|
|
|
|
dbgs() << NewLowerBound->getSExtValue() << " - ";
|
|
|
|
if (UpperBound) {
|
|
|
|
dbgs() << cast<ConstantInt>(UpperBound)->getSExtValue() << "\n";
|
|
|
|
} 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,
|
|
|
|
Default);
|
|
|
|
BasicBlock *RBranch = switchConvert(RHS.begin(), RHS.end(), NewLowerBound,
|
|
|
|
UpperBound, Val, NewNode, OrigBlock,
|
|
|
|
Default);
|
|
|
|
|
|
|
|
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
|
|
|
|
if (cast<ConstantInt>(Leaf.Low)->isMinValue(true /*isSigned*/)) {
|
|
|
|
// 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");
|
2007-03-10 16:46:28 +00:00
|
|
|
} else if (cast<ConstantInt>(Leaf.Low)->isZero()) {
|
|
|
|
// 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
|
|
|
|
uint64_t Range = cast<ConstantInt>(Leaf.High)->getSExtValue() -
|
|
|
|
cast<ConstantInt>(Leaf.Low)->getSExtValue();
|
|
|
|
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
|
|
|
|
if (Cases.size()>=2)
|
2014-03-02 12:27:27 +00:00
|
|
|
for (CaseItr I = Cases.begin(), J = std::next(Cases.begin());
|
|
|
|
J != 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
|
|
|
int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
|
|
|
|
int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
|
|
|
|
BasicBlock* nextBB = J->BB;
|
|
|
|
BasicBlock* currentBB = I->BB;
|
|
|
|
|
|
|
|
// If the two neighboring cases go to the same destination, merge them
|
|
|
|
// into a single case.
|
|
|
|
if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
|
|
|
|
I->High = J->High;
|
|
|
|
J = Cases.erase(J);
|
|
|
|
} else {
|
|
|
|
I = J++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2003-08-23 22:54:34 +00:00
|
|
|
// If there is only the default destination, don't bother with the code below.
|
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()) {
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(SI->getDefaultDest(), CurBlock);
|
2004-03-14 04:14:31 +00:00
|
|
|
CurBlock->getInstList().erase(SI);
|
2003-08-23 22:54:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
const bool DefaultIsUnreachable =
|
|
|
|
Default->size() == 1 && isa<UnreachableInst>(Default->getTerminator());
|
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.
|
2014-06-16 16:55:20 +00:00
|
|
|
// if the default block is set as an unreachable we avoid creating one
|
|
|
|
// because will never be a valid target.
|
|
|
|
BasicBlock *NewDefault = nullptr;
|
|
|
|
if (!DefaultIsUnreachable) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-03-10 16:46:28 +00:00
|
|
|
// Prepare cases vector.
|
|
|
|
CaseVector Cases;
|
|
|
|
unsigned numCmps = Clusterify(Cases, SI);
|
2003-10-07 18:46:23 +00:00
|
|
|
|
2010-01-05 01:26:45 +00:00
|
|
|
DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
|
2009-07-25 01:13:51 +00:00
|
|
|
<< ". Total compares: " << numCmps << "\n");
|
2010-01-05 01:26:45 +00:00
|
|
|
DEBUG(dbgs() << "Cases: " << Cases << "\n");
|
2009-07-27 23:14:11 +00:00
|
|
|
(void)numCmps;
|
2007-03-10 16:46:28 +00:00
|
|
|
|
2014-06-16 16:55:20 +00:00
|
|
|
ConstantInt *UpperBound = nullptr;
|
|
|
|
ConstantInt *LowerBound = nullptr;
|
|
|
|
|
|
|
|
// Optimize the condition where Default is an unreachable block. In this case
|
|
|
|
// we can make the bounds tightly fitted around the case value ranges,
|
|
|
|
// because we know that the value passed to the switch should always be
|
|
|
|
// exactly one of the case values.
|
|
|
|
if (DefaultIsUnreachable) {
|
|
|
|
CaseItr LastCase = Cases.begin() + Cases.size() - 1;
|
|
|
|
UpperBound = cast<ConstantInt>(LastCase->High);
|
|
|
|
LowerBound = cast<ConstantInt>(Cases.begin()->Low);
|
|
|
|
}
|
|
|
|
BasicBlock *SwitchBlock =
|
|
|
|
switchConvert(Cases.begin(), Cases.end(), LowerBound, UpperBound, Val,
|
2014-07-11 10:34:36 +00:00
|
|
|
OrigBlock, OrigBlock, NewDefault);
|
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.
|
2004-03-14 04:14:31 +00:00
|
|
|
CurBlock->getInstList().erase(SI);
|
2014-06-16 16:55:20 +00:00
|
|
|
|
|
|
|
pred_iterator PI = pred_begin(Default), E = pred_end(Default);
|
|
|
|
// If the Default block has no more predecessors just remove it
|
|
|
|
if (PI == E) {
|
|
|
|
DeleteDeadBlock(Default);
|
|
|
|
}
|
2003-04-23 16:23:59 +00:00
|
|
|
}
|