blob: fd4e7a90c7dc03092a3b81e11a64c83914d89b26 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
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};
75
76/// addSeqNode - The same as addNode, but the node is also included in the
77/// sequence nodes for this block. This method should be called for any
78/// instructions which have a specified sequence they must be evaluated in.
79///
80SelectionDAGNode *SelectionDAGBuilder::addSeqNode(SelectionDAGNode *N) {
81 DAG.addNode(N); // First, add the node to the selection DAG
82
83 if (!CurRoot)
84 CurRoot = N;
85 else {
86 // Create and add a new chain node for the existing root and this node...
87 CurRoot = DAG.addNode(new SelectionDAGNode(ISD::ChainNode, MVT::isVoid,
88 BB, CurRoot, N));
89 }
90 return N;
91}
92
93/// getNodeFor - This method returns the SelectionDAGNode for the specified LLVM
94/// value, creating a node as necessary.
95///
96SelectionDAGNode *SelectionDAGBuilder::getNodeFor(Value *V) {
97 // If we already have the entry, return it.
98 SelectionDAGNode*& Entry = DAG.ValueMap[V];
99 if (Entry) return Entry;
100
101 // Otherwise, we need to create a node to return now... start by figuring out
102 // which type the node will be...
103 MVT::ValueType ValueType = DAG.getValueType(V->getType());
104
105 if (Instruction *I = dyn_cast<Instruction>(V))
106 // Instructions will be filled in later. For now, just create and return a
107 // dummy node.
108 return Entry = new SelectionDAGNode(ISD::ProtoNode, ValueType);
109
110 if (Constant *C = dyn_cast<Constant>(V)) {
111 if (ConstantBool *CB = dyn_cast<ConstantBool>(C)) {
112 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
113 Entry->addValue(new ReducedValue_Constant_i1(CB->getValue()));
114 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
115 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
116 switch (ValueType) {
117 case MVT::i8:
118 Entry->addValue(new ReducedValue_Constant_i8(CI->getRawValue()));
119 break;
120 case MVT::i16:
121 Entry->addValue(new ReducedValue_Constant_i16(CI->getRawValue()));
122 break;
123 case MVT::i32:
124 Entry->addValue(new ReducedValue_Constant_i32(CI->getRawValue()));
125 break;
126 case MVT::i64:
127 Entry->addValue(new ReducedValue_Constant_i64(CI->getRawValue()));
128 break;
129 default:
130 assert(0 && "Invalid ValueType for an integer constant!");
131 }
132
133 } else if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
134 Entry = new SelectionDAGNode(ISD::Constant, ValueType);
135 if (ValueType == MVT::f32)
136 Entry->addValue(new ReducedValue_Constant_f32(CFP->getValue()));
137 else
138 Entry->addValue(new ReducedValue_Constant_f64(CFP->getValue()));
139 }
140 if (Entry) return Entry;
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000141 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
142 Entry = new SelectionDAGNode(ISD::BasicBlock, ValueType);
143 Entry->addValue(new ReducedValue_BasicBlock_i32(DAG.BlockMap[BB]));
144 return Entry;
Chris Lattner78ec3112003-08-11 14:57:33 +0000145 }
146
147 std::cerr << "Unhandled LLVM value in DAG Builder!: " << *V << "\n";
148 abort();
149 return 0;
150}
151
152
153// visitBB - This method is used to visit a basic block in the program. It
154// manages the CurRoot instance variable so that all of the visit(Instruction)
155// methods can be written to assume that there is only one basic block being
156// constructed.
157//
158void SelectionDAGBuilder::visitBB(BasicBlock &bb) {
159 BB = DAG.BlockMap[&bb]; // Update BB instance var
160
161 // Save the current global DAG...
162 SelectionDAGNode *OldRoot = CurRoot;
163 CurRoot = 0;
164
165 visit(bb.begin(), bb.end()); // Visit all of the instructions...
166
167 if (OldRoot) {
168 if (!CurRoot)
169 CurRoot = OldRoot; // This block had no root of its own..
170 else {
171 // The previous basic block AND this basic block had roots, insert a
172 // block chain node now...
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000173 CurRoot = DAG.addNode(new SelectionDAGNode(ISD::BlockChainNode,
174 MVT::isVoid,
175 BB, OldRoot, CurRoot));
Chris Lattner78ec3112003-08-11 14:57:33 +0000176 }
177 }
178}
179
180//===----------------------------------------------------------------------===//
181// ...Visitation Methods...
182//===----------------------------------------------------------------------===//
183
184void SelectionDAGBuilder::visitAdd(BinaryOperator &BO) {
185 getNodeFor(BO)->setNode(ISD::Plus, BB, getNodeFor(BO.getOperand(0)),
186 getNodeFor(BO.getOperand(1)));
187}
188void SelectionDAGBuilder::visitSub(BinaryOperator &BO) {
189 getNodeFor(BO)->setNode(ISD::Minus, BB, getNodeFor(BO.getOperand(0)),
190 getNodeFor(BO.getOperand(1)));
191}
192void SelectionDAGBuilder::visitMul(BinaryOperator &BO) {
193 getNodeFor(BO)->setNode(ISD::Times, BB, getNodeFor(BO.getOperand(0)),
194 getNodeFor(BO.getOperand(1)));
195}
196
197void SelectionDAGBuilder::visitAnd(BinaryOperator &BO) {
198 getNodeFor(BO)->setNode(ISD::And, BB, getNodeFor(BO.getOperand(0)),
199 getNodeFor(BO.getOperand(1)));
200}
201void SelectionDAGBuilder::visitOr(BinaryOperator &BO) {
202 getNodeFor(BO)->setNode(ISD::Or, BB, getNodeFor(BO.getOperand(0)),
203 getNodeFor(BO.getOperand(1)));
204}
205void SelectionDAGBuilder::visitXor(BinaryOperator &BO) {
206 getNodeFor(BO)->setNode(ISD::Xor, BB, getNodeFor(BO.getOperand(0)),
207 getNodeFor(BO.getOperand(1)));
208}
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000209void SelectionDAGBuilder::visitSetEQ(BinaryOperator &BO) {
210 getNodeFor(BO)->setNode(ISD::SetEQ, BB, getNodeFor(BO.getOperand(0)),
211 getNodeFor(BO.getOperand(1)));
212}
213
Chris Lattner78ec3112003-08-11 14:57:33 +0000214
215void SelectionDAGBuilder::visitRet(ReturnInst &RI) {
216 if (RI.getNumOperands()) { // Value return
217 addSeqNode(new SelectionDAGNode(ISD::Ret, MVT::isVoid, BB,
218 getNodeFor(RI.getOperand(0))));
219 } else { // Void return
220 addSeqNode(new SelectionDAGNode(ISD::RetVoid, MVT::isVoid, BB));
221 }
222}
223
224
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000225void SelectionDAGBuilder::visitBr(BranchInst &BI) {
226 if (BI.isUnconditional())
227 addSeqNode(new SelectionDAGNode(ISD::Br, MVT::isVoid, BB,
228 getNodeFor(BI.getOperand(0))));
229 else
230 addSeqNode(new SelectionDAGNode(ISD::BrCond, MVT::isVoid, BB,
231 getNodeFor(BI.getCondition()),
232 getNodeFor(BI.getSuccessor(0)),
233 getNodeFor(BI.getSuccessor(1))));
234}
Chris Lattner78ec3112003-08-11 14:57:33 +0000235
236
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000237void SelectionDAGBuilder::visitLoad(LoadInst &LI) {
238 // FIXME: this won't prevent reordering of loads!
239 getNodeFor(LI)->setNode(ISD::Load, BB, getNodeFor(LI.getOperand(0)));
240}
241
242void SelectionDAGBuilder::visitCall(CallInst &CI) {
243 SDTB.expandCall(DAG, CI);
244}
245
246
247
248// SelectionDAG constructor - Just use the SelectionDAGBuilder to do all of the
Chris Lattner78ec3112003-08-11 14:57:33 +0000249// dirty work...
250SelectionDAG::SelectionDAG(MachineFunction &f, const TargetMachine &tm,
251 SelectionDAGTargetBuilder &SDTB)
252 : F(f), TM(tm) {
253
254 switch (TM.getTargetData().getPointerSize()) {
255 default: assert(0 && "Unknown pointer size!"); abort();
256 case 8: PointerType = MVT::i8; break;
257 case 16: PointerType = MVT::i16; break;
258 case 32: PointerType = MVT::i32; break;
259 case 64: PointerType = MVT::i64; break;
260 }
261
262 // Create all of the machine basic blocks for the function... building the
263 // BlockMap. This map is used for PHI node conversion.
264 const Function &Fn = *F.getFunction();
265 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
266 F.getBasicBlockList().push_back(BlockMap[I] = new MachineBasicBlock(I));
267
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000268 SDTB.expandArguments(*this);
Chris Lattner78ec3112003-08-11 14:57:33 +0000269
Chris Lattner7dc97ff2003-08-15 04:53:16 +0000270 SelectionDAGBuilder SDB(*this, SDTB);
Chris Lattner78ec3112003-08-11 14:57:33 +0000271 for (Function::const_iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
272 SDB.visitBB(const_cast<BasicBlock&>(*I));
273 Root = SDB.CurRoot;
274}
Brian Gaeked0fde302003-11-11 22:41:34 +0000275
276} // End llvm namespace