switch more statistics over to STATISTIC, eliminating static ctors. Also,
[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/Transforms/Utils/UnifyFunctionExitNodes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Instructions.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Type.h"
27 using namespace llvm;
28
29 namespace {
30   /// LowerSelect - Turn select instructions into conditional branches.
31   ///
32   class LowerSelect : public FunctionPass {
33     bool OnlyFP;   // Only lower FP select instructions?
34   public:
35     LowerSelect(bool onlyfp = false) : OnlyFP(onlyfp) {}
36
37     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38       // This certainly destroys the CFG.
39       // This is a cluster of orthogonal Transforms:    
40       AU.addPreserved<UnifyFunctionExitNodes>();
41       AU.addPreservedID(PromoteMemoryToRegisterID);
42       AU.addPreservedID(LowerSwitchID);
43       AU.addPreservedID(LowerInvokePassID);
44       AU.addPreservedID(LowerAllocationsID);
45     }
46
47     bool runOnFunction(Function &F);
48   };
49
50   RegisterPass<LowerSelect>
51   X("lowerselect", "Lower select instructions to branches");
52 }
53
54 // Publically exposed interface to pass...
55 const PassInfo *llvm::LowerSelectID = X.getPassInfo();
56 //===----------------------------------------------------------------------===//
57 // This pass converts SelectInst instructions into conditional branch and PHI
58 // instructions.  If the OnlyFP flag is set to true, then only floating point
59 // select instructions are lowered.
60 //
61 FunctionPass *llvm::createLowerSelectPass(bool OnlyFP) {
62   return new LowerSelect(OnlyFP);
63 }
64
65
66 bool LowerSelect::runOnFunction(Function &F) {
67   bool Changed = false;
68   for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
69     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
70       if (SelectInst *SI = dyn_cast<SelectInst>(I))
71         if (!OnlyFP || SI->getType()->isFloatingPoint()) {
72           // Split this basic block in half right before the select instruction.
73           BasicBlock *NewCont =
74             BB->splitBasicBlock(I, BB->getName()+".selectcont");
75
76           // Make the true block, and make it branch to the continue block.
77           BasicBlock *NewTrue = new BasicBlock(BB->getName()+".selecttrue",
78                                                BB->getParent(), NewCont);
79           new BranchInst(NewCont, NewTrue);
80
81           // Make the unconditional branch in the incoming block be a
82           // conditional branch on the select predicate.
83           BB->getInstList().erase(BB->getTerminator());
84           new BranchInst(NewTrue, NewCont, SI->getCondition(), BB);
85
86           // Create a new PHI node in the cont block with the entries we need.
87           std::string Name = SI->getName(); SI->setName("");
88           PHINode *PN = new PHINode(SI->getType(), Name, NewCont->begin());
89           PN->addIncoming(SI->getTrueValue(), NewTrue);
90           PN->addIncoming(SI->getFalseValue(), BB);
91
92           // Use the PHI instead of the select.
93           SI->replaceAllUsesWith(PN);
94           NewCont->getInstList().erase(SI);
95
96           Changed = true;
97           break; // This block is done with.
98         }
99     }
100   return Changed;
101 }