blob: 464d30d03082bd3dee887ae061cc9e90fb9f8ebd [file] [log] [blame]
Chris Lattner18bdbc32004-03-30 18:41:10 +00001//===- LowerSelect.cpp - Transform select insts to branches ---------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattner18bdbc32004-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 Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattner18bdbc32004-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"
Chris Lattner8d89e7b2006-05-09 04:13:41 +000022#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner18bdbc32004-03-30 18:41:10 +000023#include "llvm/Function.h"
24#include "llvm/Instructions.h"
25#include "llvm/Pass.h"
26#include "llvm/Type.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
Chris Lattner18bdbc32004-03-30 18:41:10 +000028using namespace llvm;
29
30namespace {
31 Statistic<> NumLowered("lowerselect","Number of select instructions lowered");
32
33 /// LowerSelect - Turn select instructions into conditional branches.
34 ///
35 class LowerSelect : public FunctionPass {
36 bool OnlyFP; // Only lower FP select instructions?
37 public:
38 LowerSelect(bool onlyfp = false) : OnlyFP(onlyfp) {}
39
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner8d89e7b2006-05-09 04:13:41 +000041 // This certainly destroys the CFG.
Chris Lattnered96fe82006-05-17 21:05:27 +000042 // This is a cluster of orthogonal Transforms:
Chris Lattner8d89e7b2006-05-09 04:13:41 +000043 AU.addPreserved<UnifyFunctionExitNodes>();
44 AU.addPreservedID(PromoteMemoryToRegisterID);
45 AU.addPreservedID(LowerSwitchID);
Chris Lattnered96fe82006-05-17 21:05:27 +000046 AU.addPreservedID(LowerInvokePassID);
47 AU.addPreservedID(LowerAllocationsID);
Chris Lattner18bdbc32004-03-30 18:41:10 +000048 }
49
50 bool runOnFunction(Function &F);
51 };
52
53 RegisterOpt<LowerSelect>
54 X("lowerselect", "Lower select instructions to branches");
55}
56
Chris Lattnerb3674e42006-05-02 04:24:36 +000057// Publically exposed interface to pass...
58const PassInfo *llvm::LowerSelectID = X.getPassInfo();
Chris Lattner18bdbc32004-03-30 18:41:10 +000059//===----------------------------------------------------------------------===//
60// This pass converts SelectInst instructions into conditional branch and PHI
61// instructions. If the OnlyFP flag is set to true, then only floating point
62// select instructions are lowered.
63//
64FunctionPass *llvm::createLowerSelectPass(bool OnlyFP) {
65 return new LowerSelect(OnlyFP);
66}
67
68
69bool LowerSelect::runOnFunction(Function &F) {
70 bool Changed = false;
71 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
72 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
73 if (SelectInst *SI = dyn_cast<SelectInst>(I))
74 if (!OnlyFP || SI->getType()->isFloatingPoint()) {
75 // Split this basic block in half right before the select instruction.
76 BasicBlock *NewCont =
77 BB->splitBasicBlock(I, BB->getName()+".selectcont");
78
79 // Make the true block, and make it branch to the continue block.
80 BasicBlock *NewTrue = new BasicBlock(BB->getName()+".selecttrue",
81 BB->getParent(), NewCont);
82 new BranchInst(NewCont, NewTrue);
83
84 // Make the unconditional branch in the incoming block be a
85 // conditional branch on the select predicate.
86 BB->getInstList().erase(BB->getTerminator());
87 new BranchInst(NewTrue, NewCont, SI->getCondition(), BB);
88
89 // Create a new PHI node in the cont block with the entries we need.
90 std::string Name = SI->getName(); SI->setName("");
91 PHINode *PN = new PHINode(SI->getType(), Name, NewCont->begin());
92 PN->addIncoming(SI->getTrueValue(), NewTrue);
93 PN->addIncoming(SI->getFalseValue(), BB);
94
95 // Use the PHI instead of the select.
96 SI->replaceAllUsesWith(PN);
97 NewCont->getInstList().erase(SI);
Misha Brukmanfd939082005-04-21 23:48:37 +000098
Chris Lattner18bdbc32004-03-30 18:41:10 +000099 Changed = true;
100 break; // This block is done with.
101 }
102 }
103 return Changed;
104}