blob: 5f9f593f9d154456dd1888560cc4997843b444b5 [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"
Chris Lattner4fe87d62006-05-09 04:13:41 +000022#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Chris Lattner059f3902004-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 Spencer557ab152007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Chris Lattner059f3902004-03-30 18:41:10 +000028using namespace llvm;
29
30namespace {
Chris Lattner059f3902004-03-30 18:41:10 +000031 /// LowerSelect - Turn select instructions into conditional branches.
32 ///
Reid Spencer557ab152007-02-05 23:32:05 +000033 class VISIBILITY_HIDDEN LowerSelect : public FunctionPass {
Chris Lattner059f3902004-03-30 18:41:10 +000034 bool OnlyFP; // Only lower FP select instructions?
35 public:
36 LowerSelect(bool onlyfp = false) : OnlyFP(onlyfp) {}
37
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner4fe87d62006-05-09 04:13:41 +000039 // This certainly destroys the CFG.
Chris Lattnere4cb4762006-05-17 21:05:27 +000040 // This is a cluster of orthogonal Transforms:
Chris Lattner4fe87d62006-05-09 04:13:41 +000041 AU.addPreserved<UnifyFunctionExitNodes>();
42 AU.addPreservedID(PromoteMemoryToRegisterID);
43 AU.addPreservedID(LowerSwitchID);
Chris Lattnere4cb4762006-05-17 21:05:27 +000044 AU.addPreservedID(LowerInvokePassID);
45 AU.addPreservedID(LowerAllocationsID);
Chris Lattner059f3902004-03-30 18:41:10 +000046 }
47
48 bool runOnFunction(Function &F);
49 };
50
Chris Lattnerc2d3d312006-08-27 22:42:52 +000051 RegisterPass<LowerSelect>
Chris Lattner059f3902004-03-30 18:41:10 +000052 X("lowerselect", "Lower select instructions to branches");
53}
54
Chris Lattner2d3a0272006-05-02 04:24:36 +000055// Publically exposed interface to pass...
56const PassInfo *llvm::LowerSelectID = X.getPassInfo();
Chris Lattner059f3902004-03-30 18:41:10 +000057//===----------------------------------------------------------------------===//
58// This pass converts SelectInst instructions into conditional branch and PHI
59// instructions. If the OnlyFP flag is set to true, then only floating point
60// select instructions are lowered.
61//
62FunctionPass *llvm::createLowerSelectPass(bool OnlyFP) {
63 return new LowerSelect(OnlyFP);
64}
65
66
67bool LowerSelect::runOnFunction(Function &F) {
68 bool Changed = false;
69 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
70 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
71 if (SelectInst *SI = dyn_cast<SelectInst>(I))
72 if (!OnlyFP || SI->getType()->isFloatingPoint()) {
73 // Split this basic block in half right before the select instruction.
74 BasicBlock *NewCont =
75 BB->splitBasicBlock(I, BB->getName()+".selectcont");
76
77 // Make the true block, and make it branch to the continue block.
78 BasicBlock *NewTrue = new BasicBlock(BB->getName()+".selecttrue",
79 BB->getParent(), NewCont);
80 new BranchInst(NewCont, NewTrue);
81
82 // Make the unconditional branch in the incoming block be a
83 // conditional branch on the select predicate.
84 BB->getInstList().erase(BB->getTerminator());
85 new BranchInst(NewTrue, NewCont, SI->getCondition(), BB);
86
87 // Create a new PHI node in the cont block with the entries we need.
88 std::string Name = SI->getName(); SI->setName("");
89 PHINode *PN = new PHINode(SI->getType(), Name, NewCont->begin());
90 PN->addIncoming(SI->getTrueValue(), NewTrue);
91 PN->addIncoming(SI->getFalseValue(), BB);
92
93 // Use the PHI instead of the select.
94 SI->replaceAllUsesWith(PN);
95 NewCont->getInstList().erase(SI);
Misha Brukmanb1c93172005-04-21 23:48:37 +000096
Chris Lattner059f3902004-03-30 18:41:10 +000097 Changed = true;
98 break; // This block is done with.
99 }
100 }
101 return Changed;
102}