2004-03-31 02:41:10 +08:00
|
|
|
//===- LowerSelect.cpp - Transform select insts to branches ---------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-03-31 02:41:10 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-03-31 02:41:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass lowers select instructions into conditional branches for targets
|
|
|
|
// that do not have conditional moves or that have not implemented the select
|
|
|
|
// instruction yet.
|
|
|
|
//
|
|
|
|
// Note that this pass could be improved. In particular it turns every select
|
|
|
|
// instruction into a new conditional branch, even though some common cases have
|
|
|
|
// select instructions on the same predicate next to each other. It would be
|
|
|
|
// better to use the same branch for the whole group of selects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2006-05-09 12:13:41 +08:00
|
|
|
#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
|
2004-03-31 02:41:10 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Type.h"
|
2007-02-06 07:32:05 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2004-03-31 02:41:10 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// LowerSelect - Turn select instructions into conditional branches.
|
|
|
|
///
|
2007-02-06 07:32:05 +08:00
|
|
|
class VISIBILITY_HIDDEN LowerSelect : public FunctionPass {
|
2004-03-31 02:41:10 +08:00
|
|
|
bool OnlyFP; // Only lower FP select instructions?
|
|
|
|
public:
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 23:32:29 +08:00
|
|
|
explicit LowerSelect(bool onlyfp = false) : FunctionPass((intptr_t)&ID),
|
2007-05-02 05:15:47 +08:00
|
|
|
OnlyFP(onlyfp) {}
|
2004-03-31 02:41:10 +08:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2006-05-09 12:13:41 +08:00
|
|
|
// This certainly destroys the CFG.
|
2007-04-17 02:10:23 +08:00
|
|
|
// This is a cluster of orthogonal Transforms:
|
2006-05-09 12:13:41 +08:00
|
|
|
AU.addPreserved<UnifyFunctionExitNodes>();
|
|
|
|
AU.addPreservedID(PromoteMemoryToRegisterID);
|
|
|
|
AU.addPreservedID(LowerSwitchID);
|
2006-05-18 05:05:27 +08:00
|
|
|
AU.addPreservedID(LowerInvokePassID);
|
|
|
|
AU.addPreservedID(LowerAllocationsID);
|
2004-03-31 02:41:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
};
|
|
|
|
|
2007-05-03 09:11:54 +08:00
|
|
|
char LowerSelect::ID = 0;
|
2006-08-28 06:42:52 +08:00
|
|
|
RegisterPass<LowerSelect>
|
2004-03-31 02:41:10 +08:00
|
|
|
X("lowerselect", "Lower select instructions to branches");
|
|
|
|
}
|
|
|
|
|
2006-05-02 12:24:36 +08:00
|
|
|
// Publically exposed interface to pass...
|
|
|
|
const PassInfo *llvm::LowerSelectID = X.getPassInfo();
|
2004-03-31 02:41:10 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// This pass converts SelectInst instructions into conditional branch and PHI
|
|
|
|
// instructions. If the OnlyFP flag is set to true, then only floating point
|
|
|
|
// select instructions are lowered.
|
|
|
|
//
|
|
|
|
FunctionPass *llvm::createLowerSelectPass(bool OnlyFP) {
|
|
|
|
return new LowerSelect(OnlyFP);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool LowerSelect::runOnFunction(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
|
|
|
|
if (SelectInst *SI = dyn_cast<SelectInst>(I))
|
|
|
|
if (!OnlyFP || SI->getType()->isFloatingPoint()) {
|
|
|
|
// Split this basic block in half right before the select instruction.
|
|
|
|
BasicBlock *NewCont =
|
|
|
|
BB->splitBasicBlock(I, BB->getName()+".selectcont");
|
|
|
|
|
|
|
|
// Make the true block, and make it branch to the continue block.
|
|
|
|
BasicBlock *NewTrue = new BasicBlock(BB->getName()+".selecttrue",
|
|
|
|
BB->getParent(), NewCont);
|
|
|
|
new BranchInst(NewCont, NewTrue);
|
|
|
|
|
|
|
|
// Make the unconditional branch in the incoming block be a
|
|
|
|
// conditional branch on the select predicate.
|
|
|
|
BB->getInstList().erase(BB->getTerminator());
|
|
|
|
new BranchInst(NewTrue, NewCont, SI->getCondition(), BB);
|
|
|
|
|
|
|
|
// Create a new PHI node in the cont block with the entries we need.
|
2007-02-11 09:37:51 +08:00
|
|
|
PHINode *PN = new PHINode(SI->getType(), "", NewCont->begin());
|
|
|
|
PN->takeName(SI);
|
2004-03-31 02:41:10 +08:00
|
|
|
PN->addIncoming(SI->getTrueValue(), NewTrue);
|
|
|
|
PN->addIncoming(SI->getFalseValue(), BB);
|
|
|
|
|
|
|
|
// Use the PHI instead of the select.
|
|
|
|
SI->replaceAllUsesWith(PN);
|
|
|
|
NewCont->getInstList().erase(SI);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-03-31 02:41:10 +08:00
|
|
|
Changed = true;
|
|
|
|
break; // This block is done with.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|