Be a bit more efficient when processing the active and inactive
[oota-llvm.git] / lib / Transforms / Utils / LowerSelect.cpp
1 //===- LowerSelect.cpp - Transform select insts to branches ---------------===//
2 // 
3 //                     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.
7 // 
8 //===----------------------------------------------------------------------===//
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"
26 #include "Support/Statistic.h"
27 using namespace llvm;
28
29 namespace {
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
50 //===----------------------------------------------------------------------===//
51 // This pass converts SelectInst instructions into conditional branch and PHI
52 // instructions.  If the OnlyFP flag is set to true, then only floating point
53 // select instructions are lowered.
54 //
55 FunctionPass *llvm::createLowerSelectPass(bool OnlyFP) {
56   return new LowerSelect(OnlyFP);
57 }
58
59
60 bool LowerSelect::runOnFunction(Function &F) {
61   bool Changed = false;
62   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
63     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
64       if (SelectInst *SI = dyn_cast<SelectInst>(I))
65         if (!OnlyFP || SI->getType()->isFloatingPoint()) {
66           // Split this basic block in half right before the select instruction.
67           BasicBlock *NewCont =
68             BB->splitBasicBlock(I, BB->getName()+".selectcont");
69
70           // Make the true block, and make it branch to the continue block.
71           BasicBlock *NewTrue = new BasicBlock(BB->getName()+".selecttrue",
72                                                BB->getParent(), NewCont);
73           new BranchInst(NewCont, NewTrue);
74
75           // Make the unconditional branch in the incoming block be a
76           // conditional branch on the select predicate.
77           BB->getInstList().erase(BB->getTerminator());
78           new BranchInst(NewTrue, NewCont, SI->getCondition(), BB);
79
80           // Create a new PHI node in the cont block with the entries we need.
81           std::string Name = SI->getName(); SI->setName("");
82           PHINode *PN = new PHINode(SI->getType(), Name, NewCont->begin());
83           PN->addIncoming(SI->getTrueValue(), NewTrue);
84           PN->addIncoming(SI->getFalseValue(), BB);
85
86           // Use the PHI instead of the select.
87           SI->replaceAllUsesWith(PN);
88           NewCont->getInstList().erase(SI);
89         
90           Changed = true;
91           break; // This block is done with.
92         }
93     }
94   return Changed;
95 }