blob: 470be877a39750f5c2b0d9a5db871e1f85dd59a1 [file] [log] [blame]
Chris Lattner78ec3112003-08-11 14:57:33 +00001//===-- DAGBuilder.cpp - Turn an LLVM BasicBlock into a DAG for selection -===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
10// This file turns an LLVM BasicBlock into a target independent SelectionDAG in
11// preparation for target specific optimizations and instruction selection.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner7dc97ff2003-08-15 04:53:16 +000016#include "llvm/Constants.h"
Chris Lattner78ec3112003-08-11 14:57:33 +000017#include "llvm/Function.h"
18#include "llvm/Instructions.h"
Chris Lattner7dc97ff2003-08-15 04:53:16 +000019#include "llvm/Type.h"
Chris Lattner78ec3112003-08-11 14:57:33 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/Target/TargetMachine.h"
Chris Lattner7dc97ff2003-08-15 04:53:16 +000022#include "llvm/Support/InstVisitor.h"
Chris Lattnere25738c2004-06-02 04:28:06 +000023using namespace llvm;
Chris Lattner78ec3112003-08-11 14:57:33 +000024
Brian Gaeked0fde302003-11-11 22:41:34 +000025namespace llvm {
Chris Lattner78ec3112003-08-11 14:57:33 +000026struct SelectionDAGBuilder : public InstVisitor<SelectionDAGBuilder> {
27 // DAG - the current dag we are building.
28 SelectionDAG &DAG;
29
Chris Lattner7dc97ff2003-08-15 04:53:16 +000030 // SDTB - The target-specific builder interface, which indicates how to expand
31 // extremely target-specific aspects of the representation, such as function
32 // calls and arguments.
33 SelectionDAGTargetBuilder &SDTB;
34
Chris Lattner78ec3112003-08-11 14:57:33 +000035 // BB - The current machine basic block we are working on.
36 MachineBasicBlock *BB;
37
38 // CurRoot - The root built for the current basic block.
39 SelectionDAGNode *CurRoot;
40
Chris Lattner7dc97ff2003-08-15 04:53:16 +000041 SelectionDAGBuilder(SelectionDAG &dag, SelectionDAGTargetBuilder &sdtb)
42 : DAG(dag), SDTB(sdtb), BB(0), CurRoot(0) {}
Chris Lattner78ec3112003-08-11 14:57:33 +000043
44 void visitBB(BasicBlock &bb);
45
46 // Visitation methods for instructions: Create the appropriate DAG nodes for
47 // the instruction.
48 void visitAdd(BinaryOperator &BO);
49 void visitSub(BinaryOperator &BO);
50 void visitMul(BinaryOperator &BO);
Chris Lattner78ec3112003-08-11 14:57:33 +000051
52 void visitAnd(BinaryOperator &BO);
53 void visitOr (BinaryOperator &BO);
54 void visitXor(BinaryOperator &BO);
55
Chris Lattner7dc97ff2003-08-15 04:53:16 +000056 void visitSetEQ(BinaryOperator &BO);
57
58 void visitLoad(LoadInst &LI);
59 void visitCall(CallInst &CI);
60
61 void visitBr(BranchInst &BI);
62 void visitRet(ReturnInst &RI);
63
Chris Lattner78ec3112003-08-11 14:57:33 +000064 void visitInstruction(Instruction &I) {
Chris Lattner7dc97ff2003-08-15 04:53:16 +000065 std::cerr << "DAGBuilder: Cannot instruction select: " << I;
Chris Lattner78ec3112003-08-11 14:57:33 +000066 abort();
67 }
68
69private:
70 SelectionDAGNode *getNodeFor(Value *V);
71 SelectionDAGNode *getNodeFor(Value &V) { return getNodeFor(&V); }
72
73 SelectionDAGNode *addSeqNode(SelectionDAGNode *N);
74};
Chris Lattnere25738c2004-06-02 04:28:06 +000075} // end llvm namespace
Chris Lattner78ec3112003-08-11 14:57:33 +000076
77/// addSeqNode - The same as addNode, but the node is also included in the
78/// sequence nodes for this block. This method should be called for any
79/// instructions which have a specified sequence they must be evaluated in.
80///
81SelectionDAGNode *SelectionDAGBuilder::addSeqNode(SelectionDAGNode *N) {
82 DAG.addNode(N); // First, add the node to the selection DAG
83
84 if (!CurRoot)
85 CurRoot = N;
86 else {
87 // Create and add a new chain node for the existing root and this node...
88 CurRoot = DAG.addNode(new SelectionDAGNode(ISD::ChainNode, MVT::isVoid,
89 BB, CurRoot, N));
90 }
91 return N;
92}
93
94/// getNodeFor - This method returns the SelectionDAGNode for the specified LLVM
95/// value, creating a node as necessary.
96///
97SelectionDAGNode *SelectionDAGBuilder::getNodeFor(Value *V) {
98 // If we already have the entry, return it.
99 SelectionDAGNode*& Entry = DAG.ValueMap[V];
100 if (Entry) return Entry;
101
102 // Otherwise, we need to create a node to return now... start by figuring out
103 // which type the node will be...
104 MVT::ValueType ValueType = DAG.getValueType(V->getType());
105
106 if (Instruction *I = dyn_cast<Instruction>(V))
107 // Instructions will be filled in later. For now, just create and return a
108 // dummy node.
109 return Entry = new SelectionDAGNode(ISD::ProtoNode, ValueType);
110
111 if (Constant *C = dyn_cast<Constant>(V)) {
112 if (ConstantBool *CB = dyn_cast<ConstantBool>(C)) {
113 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
114 Entry->addValue(new ReducedValue_Constant_i1(CB->getValue()));
115 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
116 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
117 switch (ValueType) {
118 case MVT::i8:
119 Entry->addValue(new ReducedValue_Constant_i8(CI->getRawValue()));
120 break;
121 case MVT::i16:
122 Entry->addValue(new ReducedValue_Constant_i16(CI->getRawValue()));
123 break;
124 case MVT::i32:
125 Entry->addValue(new ReducedValue_Constant_i32(CI->getRawValue()));
126 break;
127 case MVT::i64:
128 Entry->addValue(new ReducedValue_Constant_i64(CI->getRawValue()));
129 break;
130 default:
131 assert(0 && "Invalid ValueType for an integer constant!");
132 }
133
134 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
135 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
136 if (ValueType == MVT::f32)
137 Entry->addValue(new ReducedValue_Constant_f32(CFP->getValue()));
138 else
139 Entry->addValue(new ReducedValue_Constant_f64(CFP->getValue()));
140 }
141 if (Entry) return Entry;
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000142 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
143 Entry = new SelectionDAGNode(ISD::BasicBlock, ValueType);
144 Entry->addValue(new ReducedValue_BasicBlock_i32(DAG.BlockMap[BB]));
145 return Entry;
Chris Lattner78ec3112003-08-11 14:57:33 +0000146 }
147
148 std::cerr << "Unhandled LLVM value in DAG Builder!: " << *V << "\n";
149 abort();
150 return 0;
151}
152
153
154// visitBB - This method is used to visit a basic block in the program. It
155// manages the CurRoot instance variable so that all of the visit(Instruction)
156// methods can be written to assume that there is only one basic block being
157// constructed.
158//
159void SelectionDAGBuilder::visitBB(BasicBlock &bb) {
160 BB = DAG.BlockMap[&bb]; // Update BB instance var
161
162 // Save the current global DAG...
163 SelectionDAGNode *OldRoot = CurRoot;
164 CurRoot = 0;
165
166 visit(bb.begin(), bb.end()); // Visit all of the instructions...
167
168 if (OldRoot) {
169 if (!CurRoot)
170 CurRoot = OldRoot; // This block had no root of its own..
171 else {
172 // The previous basic block AND this basic block had roots, insert a
173 // block chain node now...
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000174 CurRoot = DAG.addNode(new SelectionDAGNode(ISD::BlockChainNode,
175 MVT::isVoid,
176 BB, OldRoot, CurRoot));
Chris Lattner78ec3112003-08-11 14:57:33 +0000177 }
178 }
179}
180
181//===----------------------------------------------------------------------===//
182// ...Visitation Methods...
183//===----------------------------------------------------------------------===//
184
185void SelectionDAGBuilder::visitAdd(BinaryOperator &BO) {
186 getNodeFor(BO)->setNode(ISD::Plus, BB, getNodeFor(BO.getOperand(0)),
187 getNodeFor(BO.getOperand(1)));
188}
189void SelectionDAGBuilder::visitSub(BinaryOperator &BO) {
190 getNodeFor(BO)->setNode(ISD::Minus, BB, getNodeFor(BO.getOperand(0)),
191 getNodeFor(BO.getOperand(1)));
192}
193void SelectionDAGBuilder::visitMul(BinaryOperator &BO) {
194 getNodeFor(BO)->setNode(ISD::Times, BB, getNodeFor(BO.getOperand(0)),
195 getNodeFor(BO.getOperand(1)));
196}
197
198void SelectionDAGBuilder::visitAnd(BinaryOperator &BO) {
199 getNodeFor(BO)->setNode(ISD::And, BB, getNodeFor(BO.getOperand(0)),
200 getNodeFor(BO.getOperand(1)));
201}
202void SelectionDAGBuilder::visitOr(BinaryOperator &BO) {
203 getNodeFor(BO)->setNode(ISD::Or, BB, getNodeFor(BO.getOperand(0)),
204 getNodeFor(BO.getOperand(1)));
205}
206void SelectionDAGBuilder::visitXor(BinaryOperator &BO) {
207 getNodeFor(BO)->setNode(ISD::Xor, BB, getNodeFor(BO.getOperand(0)),
208 getNodeFor(BO.getOperand(1)));
209}
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000210void SelectionDAGBuilder::visitSetEQ(BinaryOperator &BO) {
211 getNodeFor(BO)->setNode(ISD::SetEQ, BB, getNodeFor(BO.getOperand(0)),
212 getNodeFor(BO.getOperand(1)));
213}
214
Chris Lattner78ec3112003-08-11 14:57:33 +0000215
216void SelectionDAGBuilder::visitRet(ReturnInst &RI) {
217 if (RI.getNumOperands()) { // Value return
218 addSeqNode(new SelectionDAGNode(ISD::Ret, MVT::isVoid, BB,
219 getNodeFor(RI.getOperand(0))));
220 } else { // Void return
221 addSeqNode(new SelectionDAGNode(ISD::RetVoid, MVT::isVoid, BB));
222 }
223}
224
225
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000226void SelectionDAGBuilder::visitBr(BranchInst &BI) {
227 if (BI.isUnconditional())
228 addSeqNode(new SelectionDAGNode(ISD::Br, MVT::isVoid, BB,
229 getNodeFor(BI.getOperand(0))));
230 else
231 addSeqNode(new SelectionDAGNode(ISD::BrCond, MVT::isVoid, BB,
232 getNodeFor(BI.getCondition()),
233 getNodeFor(BI.getSuccessor(0)),
234 getNodeFor(BI.getSuccessor(1))));
235}
Chris Lattner78ec3112003-08-11 14:57:33 +0000236
237
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000238void SelectionDAGBuilder::visitLoad(LoadInst &LI) {
239 // FIXME: this won't prevent reordering of loads!
240 getNodeFor(LI)->setNode(ISD::Load, BB, getNodeFor(LI.getOperand(0)));
241}
242
243void SelectionDAGBuilder::visitCall(CallInst &CI) {
244 SDTB.expandCall(DAG, CI);
245}
246
247
248
249// SelectionDAG constructor - Just use the SelectionDAGBuilder to do all of the
Chris Lattner78ec3112003-08-11 14:57:33 +0000250// dirty work...
251SelectionDAG::SelectionDAG(MachineFunction &f, const TargetMachine &tm,
252 SelectionDAGTargetBuilder &SDTB)
253 : F(f), TM(tm) {
254
255 switch (TM.getTargetData().getPointerSize()) {
256 default: assert(0 && "Unknown pointer size!"); abort();
Chris Lattnerfd0f7b12004-06-02 03:57:43 +0000257 case 1: PointerType = MVT::i8; break;
258 case 2: PointerType = MVT::i16; break;
259 case 3: PointerType = MVT::i32; break;
260 case 4: PointerType = MVT::i64; break;
Chris Lattner78ec3112003-08-11 14:57:33 +0000261 }
262
263 // Create all of the machine basic blocks for the function... building the
264 // BlockMap. This map is used for PHI node conversion.
265 const Function &Fn = *F.getFunction();
266 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
267 F.getBasicBlockList().push_back(BlockMap[I] = new MachineBasicBlock(I));
268
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000269 SDTB.expandArguments(*this);
Chris Lattner78ec3112003-08-11 14:57:33 +0000270
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000271 SelectionDAGBuilder SDB(*this, SDTB);
Chris Lattner78ec3112003-08-11 14:57:33 +0000272 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
273 SDB.visitBB(const_cast<BasicBlock&>(*I));
274 Root = SDB.CurRoot;
275}
Brian Gaeked0fde302003-11-11 22:41:34 +0000276