blob: 6a24c294cfdef477e04a863a68984123c0c5eb80 [file] [log] [blame]
Chris Lattner059f3902004-03-30 18:41:10 +00001//===- LowerSelect.cpp - Transform select insts to branches ---------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattner059f3902004-03-30 18:41:10 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattner059f3902004-03-30 18:41:10 +00008//===----------------------------------------------------------------------===//
9//
10// This pass lowers select instructions into conditional branches for targets
11// that do not have conditional moves or that have not implemented the select
12// instruction yet.
13//
14// Note that this pass could be improved. In particular it turns every select
15// instruction into a new conditional branch, even though some common cases have
16// select instructions on the same predicate next to each other. It would be
17// better to use the same branch for the whole group of selects.
18//
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Function.h"
23#include "llvm/Instructions.h"
24#include "llvm/Pass.h"
25#include "llvm/Type.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
Chris Lattner059f3902004-03-30 18:41:10 +000027using namespace llvm;
28
29namespace {
30 Statistic<> NumLowered("lowerselect","Number of select instructions lowered");
31
32 /// LowerSelect - Turn select instructions into conditional branches.
33 ///
34 class LowerSelect : public FunctionPass {
35 bool OnlyFP; // Only lower FP select instructions?
36 public:
37 LowerSelect(bool onlyfp = false) : OnlyFP(onlyfp) {}
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 // Doesn't really preserve anything. It can certainly destroy the CFG.
41 }
42
43 bool runOnFunction(Function &F);
44 };
45
46 RegisterOpt<LowerSelect>
47 X("lowerselect", "Lower select instructions to branches");
48}
49
Chris Lattner2d3a0272006-05-02 04:24:36 +000050// Publically exposed interface to pass...
51const PassInfo *llvm::LowerSelectID = X.getPassInfo();
Chris Lattner059f3902004-03-30 18:41:10 +000052//===----------------------------------------------------------------------===//
53// This pass converts SelectInst instructions into conditional branch and PHI
54// instructions. If the OnlyFP flag is set to true, then only floating point
55// select instructions are lowered.
56//
57FunctionPass *llvm::createLowerSelectPass(bool OnlyFP) {
58 return new LowerSelect(OnlyFP);
59}
60
61
62bool LowerSelect::runOnFunction(Function &F) {
63 bool Changed = false;
64 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
65 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
66 if (SelectInst *SI = dyn_cast<SelectInst>(I))
67 if (!OnlyFP || SI->getType()->isFloatingPoint()) {
68 // Split this basic block in half right before the select instruction.
69 BasicBlock *NewCont =
70 BB->splitBasicBlock(I, BB->getName()+".selectcont");
71
72 // Make the true block, and make it branch to the continue block.
73 BasicBlock *NewTrue = new BasicBlock(BB->getName()+".selecttrue",
74 BB->getParent(), NewCont);
75 new BranchInst(NewCont, NewTrue);
76
77 // Make the unconditional branch in the incoming block be a
78 // conditional branch on the select predicate.
79 BB->getInstList().erase(BB->getTerminator());
80 new BranchInst(NewTrue, NewCont, SI->getCondition(), BB);
81
82 // Create a new PHI node in the cont block with the entries we need.
83 std::string Name = SI->getName(); SI->setName("");
84 PHINode *PN = new PHINode(SI->getType(), Name, NewCont->begin());
85 PN->addIncoming(SI->getTrueValue(), NewTrue);
86 PN->addIncoming(SI->getFalseValue(), BB);
87
88 // Use the PHI instead of the select.
89 SI->replaceAllUsesWith(PN);
90 NewCont->getInstList().erase(SI);
Misha Brukmanb1c93172005-04-21 23:48:37 +000091
Chris Lattner059f3902004-03-30 18:41:10 +000092 Changed = true;
93 break; // This block is done with.
94 }
95 }
96 return Changed;
97}