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"
|
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"
|
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
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::vector<CaseRange> CaseVector;
|
|
|
|
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
|
|
|
|
|
|
|
BasicBlock* switchConvert(CaseItr Begin, CaseItr End, Value* Val,
|
|
|
|
BasicBlock* OrigBlock, BasicBlock* Default);
|
2007-03-10 16:46:28 +00:00
|
|
|
BasicBlock* newLeafBlock(CaseRange& Leaf, Value* Val,
|
2003-10-07 18:46:23 +00:00
|
|
|
BasicBlock* OrigBlock, BasicBlock* Default);
|
2007-03-10 16:46:28 +00:00
|
|
|
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
|
|
|
|
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.
|
|
|
|
//
|
|
|
|
BasicBlock* LowerSwitch::switchConvert(CaseItr Begin, CaseItr End,
|
|
|
|
Value* Val, BasicBlock* OrigBlock,
|
|
|
|
BasicBlock* Default)
|
|
|
|
{
|
|
|
|
unsigned Size = End - Begin;
|
|
|
|
|
|
|
|
if (Size == 1)
|
|
|
|
return newLeafBlock(*Begin, Val, OrigBlock, Default);
|
|
|
|
|
|
|
|
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
|
|
|
|
2007-03-10 16:46:28 +00:00
|
|
|
CaseRange& Pivot = *(Begin + Mid);
|
2010-01-05 01:26:45 +00:00
|
|
|
DEBUG(dbgs() << "Pivot ==> "
|
2008-08-23 22:23:09 +00:00
|
|
|
<< cast<ConstantInt>(Pivot.Low)->getValue() << " -"
|
2009-03-23 15:57:19 +00:00
|
|
|
<< cast<ConstantInt>(Pivot.High)->getValue() << "\n");
|
2003-10-07 18:46:23 +00:00
|
|
|
|
|
|
|
BasicBlock* LBranch = switchConvert(LHS.begin(), LHS.end(), Val,
|
|
|
|
OrigBlock, Default);
|
|
|
|
BasicBlock* RBranch = switchConvert(RHS.begin(), RHS.end(), Val,
|
|
|
|
OrigBlock, Default);
|
|
|
|
|
|
|
|
// 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");
|
2007-04-17 18:09:47 +00:00
|
|
|
Function::iterator FI = OrigBlock;
|
|
|
|
F->getBasicBlockList().insert(++FI, NewNode);
|
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");
|
2003-10-07 18:46:23 +00:00
|
|
|
NewNode->getInstList().push_back(Comp);
|
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;
|
|
|
|
}
|
|
|
|
|
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.
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock* NewDefault = BasicBlock::Create(SI->getContext(), "NewDefault");
|
2003-10-07 18:46:23 +00:00
|
|
|
F->getBasicBlockList().insert(Default, NewDefault);
|
2003-04-23 16:23:59 +00:00
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
BranchInst::Create(Default, NewDefault);
|
2003-05-01 22:21:21 +00:00
|
|
|
|
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
|
|
|
|
2003-10-07 18:46:23 +00:00
|
|
|
BasicBlock* SwitchBlock = switchConvert(Cases.begin(), Cases.end(), Val,
|
|
|
|
OrigBlock, NewDefault);
|
|
|
|
|
|
|
|
// 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);
|
2003-04-23 16:23:59 +00:00
|
|
|
}
|