blob: a972ddf6026bc83b943aec58817215ae7b80e588 [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 Lattner78ec3112003-08-11 14:57:33 +000023
24struct SelectionDAGBuilder : public InstVisitor<SelectionDAGBuilder> {
25 // DAG - the current dag we are building.
26 SelectionDAG &DAG;
27
Chris Lattner7dc97ff2003-08-15 04:53:16 +000028 // SDTB - The target-specific builder interface, which indicates how to expand
29 // extremely target-specific aspects of the representation, such as function
30 // calls and arguments.
31 SelectionDAGTargetBuilder &SDTB;
32
Chris Lattner78ec3112003-08-11 14:57:33 +000033 // BB - The current machine basic block we are working on.
34 MachineBasicBlock *BB;
35
36 // CurRoot - The root built for the current basic block.
37 SelectionDAGNode *CurRoot;
38
Chris Lattner7dc97ff2003-08-15 04:53:16 +000039 SelectionDAGBuilder(SelectionDAG &dag, SelectionDAGTargetBuilder &sdtb)
40 : DAG(dag), SDTB(sdtb), BB(0), CurRoot(0) {}
Chris Lattner78ec3112003-08-11 14:57:33 +000041
42 void visitBB(BasicBlock &bb);
43
44 // Visitation methods for instructions: Create the appropriate DAG nodes for
45 // the instruction.
46 void visitAdd(BinaryOperator &BO);
47 void visitSub(BinaryOperator &BO);
48 void visitMul(BinaryOperator &BO);
Chris Lattner78ec3112003-08-11 14:57:33 +000049
50 void visitAnd(BinaryOperator &BO);
51 void visitOr (BinaryOperator &BO);
52 void visitXor(BinaryOperator &BO);
53
Chris Lattner7dc97ff2003-08-15 04:53:16 +000054 void visitSetEQ(BinaryOperator &BO);
55
56 void visitLoad(LoadInst &LI);
57 void visitCall(CallInst &CI);
58
59 void visitBr(BranchInst &BI);
60 void visitRet(ReturnInst &RI);
61
Chris Lattner78ec3112003-08-11 14:57:33 +000062 void visitInstruction(Instruction &I) {
Chris Lattner7dc97ff2003-08-15 04:53:16 +000063 std::cerr << "DAGBuilder: Cannot instruction select: " << I;
Chris Lattner78ec3112003-08-11 14:57:33 +000064 abort();
65 }
66
67private:
68 SelectionDAGNode *getNodeFor(Value *V);
69 SelectionDAGNode *getNodeFor(Value &V) { return getNodeFor(&V); }
70
71 SelectionDAGNode *addSeqNode(SelectionDAGNode *N);
72};
73
74/// addSeqNode - The same as addNode, but the node is also included in the
75/// sequence nodes for this block. This method should be called for any
76/// instructions which have a specified sequence they must be evaluated in.
77///
78SelectionDAGNode *SelectionDAGBuilder::addSeqNode(SelectionDAGNode *N) {
79 DAG.addNode(N); // First, add the node to the selection DAG
80
81 if (!CurRoot)
82 CurRoot = N;
83 else {
84 // Create and add a new chain node for the existing root and this node...
85 CurRoot = DAG.addNode(new SelectionDAGNode(ISD::ChainNode, MVT::isVoid,
86 BB, CurRoot, N));
87 }
88 return N;
89}
90
91/// getNodeFor - This method returns the SelectionDAGNode for the specified LLVM
92/// value, creating a node as necessary.
93///
94SelectionDAGNode *SelectionDAGBuilder::getNodeFor(Value *V) {
95 // If we already have the entry, return it.
96 SelectionDAGNode*& Entry = DAG.ValueMap[V];
97 if (Entry) return Entry;
98
99 // Otherwise, we need to create a node to return now... start by figuring out
100 // which type the node will be...
101 MVT::ValueType ValueType = DAG.getValueType(V->getType());
102
103 if (Instruction *I = dyn_cast<Instruction>(V))
104 // Instructions will be filled in later. For now, just create and return a
105 // dummy node.
106 return Entry = new SelectionDAGNode(ISD::ProtoNode, ValueType);
107
108 if (Constant *C = dyn_cast<Constant>(V)) {
109 if (ConstantBool *CB = dyn_cast<ConstantBool>(C)) {
110 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
111 Entry->addValue(new ReducedValue_Constant_i1(CB->getValue()));
112 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
113 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
114 switch (ValueType) {
115 case MVT::i8:
116 Entry->addValue(new ReducedValue_Constant_i8(CI->getRawValue()));
117 break;
118 case MVT::i16:
119 Entry->addValue(new ReducedValue_Constant_i16(CI->getRawValue()));
120 break;
121 case MVT::i32:
122 Entry->addValue(new ReducedValue_Constant_i32(CI->getRawValue()));
123 break;
124 case MVT::i64:
125 Entry->addValue(new ReducedValue_Constant_i64(CI->getRawValue()));
126 break;
127 default:
128 assert(0 && "Invalid ValueType for an integer constant!");
129 }
130
131 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
132 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
133 if (ValueType == MVT::f32)
134 Entry->addValue(new ReducedValue_Constant_f32(CFP->getValue()));
135 else
136 Entry->addValue(new ReducedValue_Constant_f64(CFP->getValue()));
137 }
138 if (Entry) return Entry;
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000139 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
140 Entry = new SelectionDAGNode(ISD::BasicBlock, ValueType);
141 Entry->addValue(new ReducedValue_BasicBlock_i32(DAG.BlockMap[BB]));
142 return Entry;
Chris Lattner78ec3112003-08-11 14:57:33 +0000143 }
144
145 std::cerr << "Unhandled LLVM value in DAG Builder!: " << *V << "\n";
146 abort();
147 return 0;
148}
149
150
151// visitBB - This method is used to visit a basic block in the program. It
152// manages the CurRoot instance variable so that all of the visit(Instruction)
153// methods can be written to assume that there is only one basic block being
154// constructed.
155//
156void SelectionDAGBuilder::visitBB(BasicBlock &bb) {
157 BB = DAG.BlockMap[&bb]; // Update BB instance var
158
159 // Save the current global DAG...
160 SelectionDAGNode *OldRoot = CurRoot;
161 CurRoot = 0;
162
163 visit(bb.begin(), bb.end()); // Visit all of the instructions...
164
165 if (OldRoot) {
166 if (!CurRoot)
167 CurRoot = OldRoot; // This block had no root of its own..
168 else {
169 // The previous basic block AND this basic block had roots, insert a
170 // block chain node now...
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000171 CurRoot = DAG.addNode(new SelectionDAGNode(ISD::BlockChainNode,
172 MVT::isVoid,
173 BB, OldRoot, CurRoot));
Chris Lattner78ec3112003-08-11 14:57:33 +0000174 }
175 }
176}
177
178//===----------------------------------------------------------------------===//
179// ...Visitation Methods...
180//===----------------------------------------------------------------------===//
181
182void SelectionDAGBuilder::visitAdd(BinaryOperator &BO) {
183 getNodeFor(BO)->setNode(ISD::Plus, BB, getNodeFor(BO.getOperand(0)),
184 getNodeFor(BO.getOperand(1)));
185}
186void SelectionDAGBuilder::visitSub(BinaryOperator &BO) {
187 getNodeFor(BO)->setNode(ISD::Minus, BB, getNodeFor(BO.getOperand(0)),
188 getNodeFor(BO.getOperand(1)));
189}
190void SelectionDAGBuilder::visitMul(BinaryOperator &BO) {
191 getNodeFor(BO)->setNode(ISD::Times, BB, getNodeFor(BO.getOperand(0)),
192 getNodeFor(BO.getOperand(1)));
193}
194
195void SelectionDAGBuilder::visitAnd(BinaryOperator &BO) {
196 getNodeFor(BO)->setNode(ISD::And, BB, getNodeFor(BO.getOperand(0)),
197 getNodeFor(BO.getOperand(1)));
198}
199void SelectionDAGBuilder::visitOr(BinaryOperator &BO) {
200 getNodeFor(BO)->setNode(ISD::Or, BB, getNodeFor(BO.getOperand(0)),
201 getNodeFor(BO.getOperand(1)));
202}
203void SelectionDAGBuilder::visitXor(BinaryOperator &BO) {
204 getNodeFor(BO)->setNode(ISD::Xor, BB, getNodeFor(BO.getOperand(0)),
205 getNodeFor(BO.getOperand(1)));
206}
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000207void SelectionDAGBuilder::visitSetEQ(BinaryOperator &BO) {
208 getNodeFor(BO)->setNode(ISD::SetEQ, BB, getNodeFor(BO.getOperand(0)),
209 getNodeFor(BO.getOperand(1)));
210}
211
Chris Lattner78ec3112003-08-11 14:57:33 +0000212
213void SelectionDAGBuilder::visitRet(ReturnInst &RI) {
214 if (RI.getNumOperands()) { // Value return
215 addSeqNode(new SelectionDAGNode(ISD::Ret, MVT::isVoid, BB,
216 getNodeFor(RI.getOperand(0))));
217 } else { // Void return
218 addSeqNode(new SelectionDAGNode(ISD::RetVoid, MVT::isVoid, BB));
219 }
220}
221
222
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000223void SelectionDAGBuilder::visitBr(BranchInst &BI) {
224 if (BI.isUnconditional())
225 addSeqNode(new SelectionDAGNode(ISD::Br, MVT::isVoid, BB,
226 getNodeFor(BI.getOperand(0))));
227 else
228 addSeqNode(new SelectionDAGNode(ISD::BrCond, MVT::isVoid, BB,
229 getNodeFor(BI.getCondition()),
230 getNodeFor(BI.getSuccessor(0)),
231 getNodeFor(BI.getSuccessor(1))));
232}
Chris Lattner78ec3112003-08-11 14:57:33 +0000233
234
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000235void SelectionDAGBuilder::visitLoad(LoadInst &LI) {
236 // FIXME: this won't prevent reordering of loads!
237 getNodeFor(LI)->setNode(ISD::Load, BB, getNodeFor(LI.getOperand(0)));
238}
239
240void SelectionDAGBuilder::visitCall(CallInst &CI) {
241 SDTB.expandCall(DAG, CI);
242}
243
244
245
246// SelectionDAG constructor - Just use the SelectionDAGBuilder to do all of the
Chris Lattner78ec3112003-08-11 14:57:33 +0000247// dirty work...
248SelectionDAG::SelectionDAG(MachineFunction &f, const TargetMachine &tm,
249 SelectionDAGTargetBuilder &SDTB)
250 : F(f), TM(tm) {
251
252 switch (TM.getTargetData().getPointerSize()) {
253 default: assert(0 && "Unknown pointer size!"); abort();
254 case 8: PointerType = MVT::i8; break;
255 case 16: PointerType = MVT::i16; break;
256 case 32: PointerType = MVT::i32; break;
257 case 64: PointerType = MVT::i64; break;
258 }
259
260 // Create all of the machine basic blocks for the function... building the
261 // BlockMap. This map is used for PHI node conversion.
262 const Function &Fn = *F.getFunction();
263 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
264 F.getBasicBlockList().push_back(BlockMap[I] = new MachineBasicBlock(I));
265
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000266 SDTB.expandArguments(*this);
Chris Lattner78ec3112003-08-11 14:57:33 +0000267
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000268 SelectionDAGBuilder SDB(*this, SDTB);
Chris Lattner78ec3112003-08-11 14:57:33 +0000269 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
270 SDB.visitBB(const_cast<BasicBlock&>(*I));
271 Root = SDB.CurRoot;
272}