blob: 3d7d3bfa6fa42435054f2a981f4c6ae6e658369a [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswell482202a2003-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 Lattner600d3082003-08-11 14:57:33 +00009//
Chris Lattner061a1ea2005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner600d3082003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner061a1ea2005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000019#include <iostream>
Chris Lattner9c667932005-01-07 21:09:16 +000020#include <set>
Chris Lattner061a1ea2005-01-07 07:46:32 +000021#include <cmath>
Jeff Cohen7d1670da2005-01-09 20:41:56 +000022#include <algorithm>
Chris Lattner560b5e42004-06-02 04:28:06 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattnerfde3a212005-01-09 20:52:51 +000025static bool isCommutativeBinOp(unsigned Opcode) {
26 switch (Opcode) {
27 case ISD::ADD:
28 case ISD::MUL:
29 case ISD::AND:
30 case ISD::OR:
31 case ISD::XOR: return true;
32 default: return false; // FIXME: Need commutative info for user ops!
33 }
34}
35
36static bool isAssociativeBinOp(unsigned Opcode) {
37 switch (Opcode) {
38 case ISD::ADD:
39 case ISD::MUL:
40 case ISD::AND:
41 case ISD::OR:
42 case ISD::XOR: return true;
43 default: return false; // FIXME: Need associative info for user ops!
44 }
45}
46
47static unsigned ExactLog2(uint64_t Val) {
48 unsigned Count = 0;
49 while (Val != 1) {
50 Val >>= 1;
51 ++Count;
52 }
53 return Count;
54}
55
56// isInvertibleForFree - Return true if there is no cost to emitting the logical
57// inverse of this node.
58static bool isInvertibleForFree(SDOperand N) {
59 if (isa<ConstantSDNode>(N.Val)) return true;
60 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
61 return true;
62 return false;
63}
64
65
Chris Lattner061a1ea2005-01-07 07:46:32 +000066/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
67/// when given the operation for (X op Y).
68ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
69 // To perform this operation, we just need to swap the L and G bits of the
70 // operation.
71 unsigned OldL = (Operation >> 2) & 1;
72 unsigned OldG = (Operation >> 1) & 1;
73 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
74 (OldL << 1) | // New G bit
75 (OldG << 2)); // New L bit.
76}
77
78/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
79/// 'op' is a valid SetCC operation.
80ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
81 unsigned Operation = Op;
82 if (isInteger)
83 Operation ^= 7; // Flip L, G, E bits, but not U.
84 else
85 Operation ^= 15; // Flip all of the condition bits.
86 if (Operation > ISD::SETTRUE2)
87 Operation &= ~8; // Don't let N and U bits get set.
88 return ISD::CondCode(Operation);
89}
90
91
92/// isSignedOp - For an integer comparison, return 1 if the comparison is a
93/// signed operation and 2 if the result is an unsigned comparison. Return zero
94/// if the operation does not depend on the sign of the input (setne and seteq).
95static int isSignedOp(ISD::CondCode Opcode) {
96 switch (Opcode) {
97 default: assert(0 && "Illegal integer setcc operation!");
98 case ISD::SETEQ:
99 case ISD::SETNE: return 0;
100 case ISD::SETLT:
101 case ISD::SETLE:
102 case ISD::SETGT:
103 case ISD::SETGE: return 1;
104 case ISD::SETULT:
105 case ISD::SETULE:
106 case ISD::SETUGT:
107 case ISD::SETUGE: return 2;
108 }
109}
110
111/// getSetCCOrOperation - Return the result of a logical OR between different
112/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
113/// returns SETCC_INVALID if it is not possible to represent the resultant
114/// comparison.
115ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
116 bool isInteger) {
117 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
118 // Cannot fold a signed integer setcc with an unsigned integer setcc.
119 return ISD::SETCC_INVALID;
120
121 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
122
123 // If the N and U bits get set then the resultant comparison DOES suddenly
124 // care about orderedness, and is true when ordered.
125 if (Op > ISD::SETTRUE2)
126 Op &= ~16; // Clear the N bit.
127 return ISD::CondCode(Op);
128}
129
130/// getSetCCAndOperation - Return the result of a logical AND between different
131/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
132/// function returns zero if it is not possible to represent the resultant
133/// comparison.
134ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
135 bool isInteger) {
136 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
137 // Cannot fold a signed setcc with an unsigned setcc.
138 return ISD::SETCC_INVALID;
139
140 // Combine all of the condition bits.
141 return ISD::CondCode(Op1 & Op2);
142}
143
Chris Lattner9c667932005-01-07 21:09:16 +0000144/// RemoveDeadNodes - This method deletes all unreachable nodes in the
145/// SelectionDAG, including nodes (like loads) that have uses of their token
146/// chain but no other uses and no side effect. If a node is passed in as an
147/// argument, it is used as the seed for node deletion.
148void SelectionDAG::RemoveDeadNodes(SDNode *N) {
149 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
150
151 // Create a dummy node (which is not added to allnodes), that adds a reference
152 // to the root node, preventing it from being deleted.
153 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
154
155 DeleteNodeIfDead(N, &AllNodeSet);
156
157 Restart:
158 unsigned NumNodes = AllNodeSet.size();
159 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
160 I != E; ++I) {
161 // Try to delete this node.
162 DeleteNodeIfDead(*I, &AllNodeSet);
163
164 // If we actually deleted any nodes, do not use invalid iterators in
165 // AllNodeSet.
166 if (AllNodeSet.size() != NumNodes)
167 goto Restart;
168 }
169
170 // Restore AllNodes.
171 if (AllNodes.size() != NumNodes)
172 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
173
174 // If the root changed (e.g. it was a dead load, update the root).
175 setRoot(DummyNode->getOperand(0));
176
177 // Now that we are done with the dummy node, delete it.
178 DummyNode->getOperand(0).Val->removeUser(DummyNode);
179 delete DummyNode;
180}
181
182void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
183 if (!N->use_empty())
184 return;
185
186 // Okay, we really are going to delete this node. First take this out of the
187 // appropriate CSE map.
188 switch (N->getOpcode()) {
189 case ISD::Constant:
190 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
191 N->getValueType(0)));
192 break;
193 case ISD::ConstantFP:
194 ConstantFPs.erase(std::make_pair(cast<ConstantFPSDNode>(N)->getValue(),
195 N->getValueType(0)));
196 break;
197 case ISD::GlobalAddress:
198 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
199 break;
200 case ISD::FrameIndex:
201 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
202 break;
203 case ISD::ConstantPool:
204 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
205 break;
206 case ISD::BasicBlock:
207 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
208 break;
209 case ISD::ExternalSymbol:
210 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
211 break;
212
213 case ISD::LOAD:
214 Loads.erase(std::make_pair(N->getOperand(1),
215 std::make_pair(N->getOperand(0),
216 N->getValueType(0))));
217 break;
218 case ISD::SETCC:
219 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
220 N->getOperand(1)),
Chris Lattnera9d53f92005-01-18 19:26:36 +0000221 std::make_pair(
222 cast<SetCCSDNode>(N)->getCondition(),
223 N->getValueType(0))));
Chris Lattner9c667932005-01-07 21:09:16 +0000224 break;
Chris Lattner1001c6e2005-01-15 06:17:04 +0000225 case ISD::TRUNCSTORE:
226 case ISD::SIGN_EXTEND_INREG:
227 case ISD::ZERO_EXTEND_INREG:
228 case ISD::FP_ROUND_INREG:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000229 case ISD::EXTLOAD:
230 case ISD::SEXTLOAD:
231 case ISD::ZEXTLOAD: {
232 EVTStruct NN;
Chris Lattner1001c6e2005-01-15 06:17:04 +0000233 NN.Opcode = ISD::TRUNCSTORE;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000234 NN.VT = N->getValueType(0);
235 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner1001c6e2005-01-15 06:17:04 +0000236 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
237 NN.Ops.push_back(N->getOperand(i));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000238 MVTSDNodes.erase(NN);
239 break;
240 }
Chris Lattner9c667932005-01-07 21:09:16 +0000241 default:
242 if (N->getNumOperands() == 1)
243 UnaryOps.erase(std::make_pair(N->getOpcode(),
244 std::make_pair(N->getOperand(0),
245 N->getValueType(0))));
246 else if (N->getNumOperands() == 2)
247 BinaryOps.erase(std::make_pair(N->getOpcode(),
248 std::make_pair(N->getOperand(0),
249 N->getOperand(1))));
250 break;
251 }
252
253 // Next, brutally remove the operand list.
Chris Lattner9c667932005-01-07 21:09:16 +0000254 while (!N->Operands.empty()) {
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000255 SDNode *O = N->Operands.back().Val;
Chris Lattner9c667932005-01-07 21:09:16 +0000256 N->Operands.pop_back();
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000257 O->removeUser(N);
258
259 // Now that we removed this operand, see if there are no uses of it left.
260 DeleteNodeIfDead(O, NodeSet);
Chris Lattner9c667932005-01-07 21:09:16 +0000261 }
262
263 // Remove the node from the nodes set and delete it.
264 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
265 AllNodeSet.erase(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000266
267 // Now that the node is gone, check to see if any of the operands of this node
268 // are dead now.
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000269 delete N;
Chris Lattner9c667932005-01-07 21:09:16 +0000270}
271
272
Chris Lattner600d3082003-08-11 14:57:33 +0000273SelectionDAG::~SelectionDAG() {
274 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
275 delete AllNodes[i];
276}
277
Chris Lattner061a1ea2005-01-07 07:46:32 +0000278SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
279 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
280 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000281 if (VT != MVT::i64)
282 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
283
Chris Lattner061a1ea2005-01-07 07:46:32 +0000284 SDNode *&N = Constants[std::make_pair(Val, VT)];
285 if (N) return SDOperand(N, 0);
286 N = new ConstantSDNode(Val, VT);
287 AllNodes.push_back(N);
288 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000289}
290
Chris Lattner061a1ea2005-01-07 07:46:32 +0000291SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
292 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
293 if (VT == MVT::f32)
294 Val = (float)Val; // Mask out extra precision.
295
296 SDNode *&N = ConstantFPs[std::make_pair(Val, VT)];
297 if (N) return SDOperand(N, 0);
298 N = new ConstantFPSDNode(Val, VT);
299 AllNodes.push_back(N);
300 return SDOperand(N, 0);
301}
302
303
304
305SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
306 MVT::ValueType VT) {
307 SDNode *&N = GlobalValues[GV];
308 if (N) return SDOperand(N, 0);
309 N = new GlobalAddressSDNode(GV,VT);
310 AllNodes.push_back(N);
311 return SDOperand(N, 0);
312}
313
314SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
315 SDNode *&N = FrameIndices[FI];
316 if (N) return SDOperand(N, 0);
317 N = new FrameIndexSDNode(FI, VT);
318 AllNodes.push_back(N);
319 return SDOperand(N, 0);
320}
321
322SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
323 SDNode *N = ConstantPoolIndices[CPIdx];
324 if (N) return SDOperand(N, 0);
325 N = new ConstantPoolSDNode(CPIdx, VT);
326 AllNodes.push_back(N);
327 return SDOperand(N, 0);
328}
329
330SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
331 SDNode *&N = BBNodes[MBB];
332 if (N) return SDOperand(N, 0);
333 N = new BasicBlockSDNode(MBB);
334 AllNodes.push_back(N);
335 return SDOperand(N, 0);
336}
337
338SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
339 SDNode *&N = ExternalSymbols[Sym];
340 if (N) return SDOperand(N, 0);
341 N = new ExternalSymbolSDNode(Sym, VT);
342 AllNodes.push_back(N);
343 return SDOperand(N, 0);
344}
345
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000346SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
347 SDOperand N1, SDOperand N2) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000348 // These setcc operations always fold.
349 switch (Cond) {
350 default: break;
351 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000352 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000353 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000354 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000355 }
356
357 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val))
358 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
359 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
360
361 // Sign extend the operands if required
362 if (ISD::isSignedIntSetCC(Cond)) {
363 C1 = N1C->getSignExtended();
364 C2 = N2C->getSignExtended();
365 }
366
367 switch (Cond) {
368 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000369 case ISD::SETEQ: return getConstant(C1 == C2, VT);
370 case ISD::SETNE: return getConstant(C1 != C2, VT);
371 case ISD::SETULT: return getConstant(C1 < C2, VT);
372 case ISD::SETUGT: return getConstant(C1 > C2, VT);
373 case ISD::SETULE: return getConstant(C1 <= C2, VT);
374 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
375 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
376 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
377 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
378 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000379 }
380 } else {
381 // Ensure that the constant occurs on the RHS.
382 Cond = ISD::getSetCCSwappedOperands(Cond);
383 std::swap(N1, N2);
384 }
385
386 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
387 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
388 double C1 = N1C->getValue(), C2 = N2C->getValue();
389
390 switch (Cond) {
391 default: break; // FIXME: Implement the rest of these!
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000392 case ISD::SETEQ: return getConstant(C1 == C2, VT);
393 case ISD::SETNE: return getConstant(C1 != C2, VT);
394 case ISD::SETLT: return getConstant(C1 < C2, VT);
395 case ISD::SETGT: return getConstant(C1 > C2, VT);
396 case ISD::SETLE: return getConstant(C1 <= C2, VT);
397 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000398 }
399 } else {
400 // Ensure that the constant occurs on the RHS.
401 Cond = ISD::getSetCCSwappedOperands(Cond);
402 std::swap(N1, N2);
403 }
404
405 if (N1 == N2) {
406 // We can always fold X == Y for integer setcc's.
407 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000408 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000409 unsigned UOF = ISD::getUnorderedFlavor(Cond);
410 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000411 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000412 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000413 return getConstant(UOF, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000414 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
415 // if it is not already.
416 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
417 }
418
Chris Lattnerfde3a212005-01-09 20:52:51 +0000419 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner41b76412005-01-10 02:03:02 +0000420 MVT::isInteger(N1.getValueType())) {
421 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
422 N1.getOpcode() == ISD::XOR) {
423 // Simplify (X+Y) == (X+Z) --> Y == Z
424 if (N1.getOpcode() == N2.getOpcode()) {
425 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000426 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000427 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000428 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000429 if (isCommutativeBinOp(N1.getOpcode())) {
430 // If X op Y == Y op X, try other combinations.
431 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000432 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000433 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000434 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000435 }
436 }
437
438 // Simplify (X+Z) == X --> Z == 0
439 if (N1.getOperand(0) == N2)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000440 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000441 getConstant(0, N1.getValueType()));
442 if (N1.getOperand(1) == N2) {
443 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000444 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000445 getConstant(0, N1.getValueType()));
446 else {
447 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
448 // (Z-X) == X --> Z == X<<1
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000449 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000450 getNode(ISD::SHL, N2.getValueType(),
451 N2, getConstant(1, MVT::i8)));
452 }
Chris Lattnerfde3a212005-01-09 20:52:51 +0000453 }
454 }
455
Chris Lattner41b76412005-01-10 02:03:02 +0000456 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
457 N2.getOpcode() == ISD::XOR) {
458 // Simplify X == (X+Z) --> Z == 0
459 if (N2.getOperand(0) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000460 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000461 getConstant(0, N2.getValueType()));
462 else if (N2.getOperand(1) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000463 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000464 getConstant(0, N2.getValueType()));
465 }
466 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000467
Chris Lattnera9d53f92005-01-18 19:26:36 +0000468 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
469 std::make_pair(Cond, VT))];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000470 if (N) return SDOperand(N, 0);
471 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000472 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000473 AllNodes.push_back(N);
474 return SDOperand(N, 0);
475}
476
477
478
479/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000480///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000481SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
482 SDNode *N = new SDNode(Opcode, VT);
483 AllNodes.push_back(N);
484 return SDOperand(N, 0);
485}
486
Chris Lattner061a1ea2005-01-07 07:46:32 +0000487SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
488 SDOperand Operand) {
489 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
490 uint64_t Val = C->getValue();
491 switch (Opcode) {
492 default: break;
493 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
494 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
495 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000496 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
497 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000498 }
499 }
500
501 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
502 switch (Opcode) {
503 case ISD::FP_ROUND:
504 case ISD::FP_EXTEND:
505 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000506 case ISD::FP_TO_SINT:
507 return getConstant((int64_t)C->getValue(), VT);
508 case ISD::FP_TO_UINT:
509 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000510 }
511
512 unsigned OpOpcode = Operand.Val->getOpcode();
513 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000514 case ISD::TokenFactor:
515 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000516 case ISD::SIGN_EXTEND:
517 if (Operand.getValueType() == VT) return Operand; // noop extension
518 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
519 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
520 break;
521 case ISD::ZERO_EXTEND:
522 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000523 if (OpOpcode == ISD::ZERO_EXTEND)
524 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000525 break;
526 case ISD::TRUNCATE:
527 if (Operand.getValueType() == VT) return Operand; // noop truncate
528 if (OpOpcode == ISD::TRUNCATE)
529 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000530 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
531 // If the source is smaller than the dest, we still need an extend.
532 if (Operand.Val->getOperand(0).getValueType() < VT)
533 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
534 else if (Operand.Val->getOperand(0).getValueType() > VT)
535 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
536 else
537 return Operand.Val->getOperand(0);
538 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000539 break;
540 }
541
542 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
543 if (N) return SDOperand(N, 0);
544 N = new SDNode(Opcode, Operand);
545 N->setValueTypes(VT);
546 AllNodes.push_back(N);
547 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000548}
549
Chris Lattner061a1ea2005-01-07 07:46:32 +0000550SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
551 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000552#ifndef NDEBUG
553 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000554 case ISD::TokenFactor:
555 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
556 N2.getValueType() == MVT::Other && "Invalid token factor!");
557 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000558 case ISD::AND:
559 case ISD::OR:
560 case ISD::XOR:
561 case ISD::UDIV:
562 case ISD::UREM:
563 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
564 // fall through
565 case ISD::ADD:
566 case ISD::SUB:
567 case ISD::MUL:
568 case ISD::SDIV:
569 case ISD::SREM:
570 assert(N1.getValueType() == N2.getValueType() &&
571 N1.getValueType() == VT && "Binary operator types must match!");
572 break;
573
574 case ISD::SHL:
575 case ISD::SRA:
576 case ISD::SRL:
577 assert(VT == N1.getValueType() &&
578 "Shift operators return type must be the same as their first arg");
579 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000580 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000581 break;
582 default: break;
583 }
584#endif
585
Chris Lattner061a1ea2005-01-07 07:46:32 +0000586 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
587 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
588 if (N1C) {
589 if (N2C) {
590 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
591 switch (Opcode) {
592 case ISD::ADD: return getConstant(C1 + C2, VT);
593 case ISD::SUB: return getConstant(C1 - C2, VT);
594 case ISD::MUL: return getConstant(C1 * C2, VT);
595 case ISD::UDIV:
596 if (C2) return getConstant(C1 / C2, VT);
597 break;
598 case ISD::UREM :
599 if (C2) return getConstant(C1 % C2, VT);
600 break;
601 case ISD::SDIV :
602 if (C2) return getConstant(N1C->getSignExtended() /
603 N2C->getSignExtended(), VT);
604 break;
605 case ISD::SREM :
606 if (C2) return getConstant(N1C->getSignExtended() %
607 N2C->getSignExtended(), VT);
608 break;
609 case ISD::AND : return getConstant(C1 & C2, VT);
610 case ISD::OR : return getConstant(C1 | C2, VT);
611 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +0000612 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
613 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
614 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000615 default: break;
616 }
617
618 } else { // Cannonicalize constant to RHS if commutative
619 if (isCommutativeBinOp(Opcode)) {
620 std::swap(N1C, N2C);
621 std::swap(N1, N2);
622 }
623 }
Chris Lattner32a5f022005-01-19 17:29:49 +0000624
625 switch (Opcode) {
626 default: break;
627 case ISD::SHL: // shl 0, X -> 0
628 if (N1C->isNullValue()) return N1;
629 break;
630 case ISD::SRL: // srl 0, X -> 0
631 if (N1C->isNullValue()) return N1;
632 break;
633 case ISD::SRA: // sra -1, X -> -1
634 if (N1C->isAllOnesValue()) return N1;
635 break;
636 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000637 }
638
639 if (N2C) {
640 uint64_t C2 = N2C->getValue();
641
642 switch (Opcode) {
643 case ISD::ADD:
644 if (!C2) return N1; // add X, 0 -> X
645 break;
646 case ISD::SUB:
647 if (!C2) return N1; // sub X, 0 -> X
648 break;
649 case ISD::MUL:
650 if (!C2) return N2; // mul X, 0 -> 0
651 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
652 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
653
654 // FIXME: This should only be done if the target supports shift
655 // operations.
656 if ((C2 & C2-1) == 0) {
657 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
658 return getNode(ISD::SHL, VT, N1, ShAmt);
659 }
660 break;
661
662 case ISD::UDIV:
663 // FIXME: This should only be done if the target supports shift
664 // operations.
665 if ((C2 & C2-1) == 0 && C2) {
666 SDOperand ShAmt = getConstant(ExactLog2(C2), MVT::i8);
667 return getNode(ISD::SRL, VT, N1, ShAmt);
668 }
669 break;
670
Chris Lattnera86fa442005-01-11 04:25:13 +0000671 case ISD::SHL:
672 case ISD::SRL:
673 case ISD::SRA:
674 if (C2 == 0) return N1;
675 break;
676
Chris Lattner061a1ea2005-01-07 07:46:32 +0000677 case ISD::AND:
678 if (!C2) return N2; // X and 0 -> 0
679 if (N2C->isAllOnesValue())
680 return N1; // X and -1 -> X
681 break;
682 case ISD::OR:
683 if (!C2)return N1; // X or 0 -> X
684 if (N2C->isAllOnesValue())
685 return N2; // X or -1 -> -1
686 break;
687 case ISD::XOR:
688 if (!C2) return N1; // X xor 0 -> X
689 if (N2C->isAllOnesValue()) {
690 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
691 // !(X op Y) -> (X !op Y)
692 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
693 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000694 SetCC->getValueType(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000695 SetCC->getOperand(0), SetCC->getOperand(1));
696 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
697 SDNode *Op = N1.Val;
698 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
699 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
700 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
701 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
702 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
703 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
704 if (Op->getOpcode() == ISD::AND)
705 return getNode(ISD::OR, VT, LHS, RHS);
706 return getNode(ISD::AND, VT, LHS, RHS);
707 }
708 }
709 // X xor -1 -> not(x) ?
710 }
711 break;
712 }
713
714 // Reassociate ((X op C1) op C2) if possible.
715 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
716 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +0000717 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000718 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
719 }
720
721 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
722 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
723 if (N1CFP)
724 if (N2CFP) {
725 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
726 switch (Opcode) {
727 case ISD::ADD: return getConstantFP(C1 + C2, VT);
728 case ISD::SUB: return getConstantFP(C1 - C2, VT);
729 case ISD::MUL: return getConstantFP(C1 * C2, VT);
730 case ISD::SDIV:
731 if (C2) return getConstantFP(C1 / C2, VT);
732 break;
733 case ISD::SREM :
734 if (C2) return getConstantFP(fmod(C1, C2), VT);
735 break;
736 default: break;
737 }
738
739 } else { // Cannonicalize constant to RHS if commutative
740 if (isCommutativeBinOp(Opcode)) {
741 std::swap(N1CFP, N2CFP);
742 std::swap(N1, N2);
743 }
744 }
745
746 // Finally, fold operations that do not require constants.
747 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000748 case ISD::TokenFactor:
749 if (N1.getOpcode() == ISD::EntryToken)
750 return N2;
751 if (N2.getOpcode() == ISD::EntryToken)
752 return N1;
753 break;
754
Chris Lattner061a1ea2005-01-07 07:46:32 +0000755 case ISD::AND:
756 case ISD::OR:
757 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
758 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
759 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
760 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
761 ISD::CondCode Op2 = RHS->getCondition();
762
763 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
764 if (LL == RR && LR == RL) {
765 Op2 = ISD::getSetCCSwappedOperands(Op2);
766 goto MatchedBackwards;
767 }
768
769 if (LL == RL && LR == RR) {
770 MatchedBackwards:
771 ISD::CondCode Result;
772 bool isInteger = MVT::isInteger(LL.getValueType());
773 if (Opcode == ISD::OR)
774 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
775 isInteger);
776 else
777 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
778 isInteger);
779 if (Result != ISD::SETCC_INVALID)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000780 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000781 }
782 }
783 break;
784 case ISD::XOR:
785 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
786 break;
Chris Lattner3d5d5022005-01-09 20:09:57 +0000787 case ISD::SUB:
788 if (N1.getOpcode() == ISD::ADD) {
789 if (N1.Val->getOperand(0) == N2)
790 return N1.Val->getOperand(1); // (A+B)-A == B
791 if (N1.Val->getOperand(1) == N2)
792 return N1.Val->getOperand(0); // (A+B)-B == A
793 }
794 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000795 }
796
797 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
798 if (N) return SDOperand(N, 0);
799 N = new SDNode(Opcode, N1, N2);
800 N->setValueTypes(VT);
801
802 AllNodes.push_back(N);
803 return SDOperand(N, 0);
804}
805
806SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
807 SDOperand Chain, SDOperand Ptr) {
808 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
809 if (N) return SDOperand(N, 0);
810 N = new SDNode(ISD::LOAD, Chain, Ptr);
811
812 // Loads have a token chain.
813 N->setValueTypes(VT, MVT::Other);
814 AllNodes.push_back(N);
815 return SDOperand(N, 0);
816}
817
818
819SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
820 SDOperand N1, SDOperand N2, SDOperand N3) {
821 // Perform various simplifications.
822 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
823 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
824 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
825 switch (Opcode) {
826 case ISD::SELECT:
827 if (N1C)
828 if (N1C->getValue())
829 return N2; // select true, X, Y -> X
830 else
831 return N3; // select false, X, Y -> Y
832
833 if (N2 == N3) return N2; // select C, X, X -> X
834
835 if (VT == MVT::i1) { // Boolean SELECT
836 if (N2C) {
837 if (N3C) {
838 if (N2C->getValue()) // select C, 1, 0 -> C
839 return N1;
840 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
841 }
842
843 if (N2C->getValue()) // select C, 1, X -> C | X
844 return getNode(ISD::OR, VT, N1, N3);
845 else // select C, 0, X -> ~C & X
846 return getNode(ISD::AND, VT,
847 getNode(ISD::XOR, N1.getValueType(), N1,
848 getConstant(1, N1.getValueType())), N3);
849 } else if (N3C) {
850 if (N3C->getValue()) // select C, X, 1 -> ~C | X
851 return getNode(ISD::OR, VT,
852 getNode(ISD::XOR, N1.getValueType(), N1,
853 getConstant(1, N1.getValueType())), N2);
854 else // select C, X, 0 -> C & X
855 return getNode(ISD::AND, VT, N1, N2);
856 }
857 }
858
859 break;
Chris Lattner5c66e452005-01-07 22:49:57 +0000860 case ISD::BRCOND:
861 if (N2C)
862 if (N2C->getValue()) // Unconditional branch
863 return getNode(ISD::BR, MVT::Other, N1, N3);
864 else
865 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000866 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000867 }
868
869 SDNode *N = new SDNode(Opcode, N1, N2, N3);
870 switch (Opcode) {
871 default:
872 N->setValueTypes(VT);
873 break;
874 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
875 N->setValueTypes(VT, MVT::Other);
876 break;
877 }
878
879 // FIXME: memoize NODES
880 AllNodes.push_back(N);
881 return SDOperand(N, 0);
882}
883
884SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
885 std::vector<SDOperand> &Children) {
886 switch (Children.size()) {
887 case 0: return getNode(Opcode, VT);
888 case 1: return getNode(Opcode, VT, Children[0]);
889 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
890 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
891 default:
892 // FIXME: MEMOIZE!!
893 SDNode *N = new SDNode(Opcode, Children);
Chris Lattner1fe9b402005-01-20 18:50:55 +0000894 if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
895 N->setValueTypes(VT);
896 } else {
897 std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
898 N->setValueTypes(V);
899 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000900 AllNodes.push_back(N);
901 return SDOperand(N, 0);
902 }
903}
904
Chris Lattner39c67442005-01-14 22:08:15 +0000905SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner1001c6e2005-01-15 06:17:04 +0000906 MVT::ValueType EVT) {
907
908 switch (Opcode) {
909 default: assert(0 && "Bad opcode for this accessor!");
910 case ISD::FP_ROUND_INREG:
911 assert(VT == N1.getValueType() && "Not an inreg round!");
912 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
913 "Cannot FP_ROUND_INREG integer types");
914 if (EVT == VT) return N1; // Not actually rounding
915 assert(EVT < VT && "Not rounding down!");
916 break;
917 case ISD::ZERO_EXTEND_INREG:
918 case ISD::SIGN_EXTEND_INREG:
919 assert(VT == N1.getValueType() && "Not an inreg extend!");
920 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
921 "Cannot *_EXTEND_INREG FP types");
922 if (EVT == VT) return N1; // Not actually extending
923 assert(EVT < VT && "Not extending!");
Chris Lattner0fe77762005-01-16 00:17:20 +0000924
925 // If we are sign extending an extension, use the original source.
926 if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
927 N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
928 if (N1.getOpcode() == Opcode &&
929 cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
930 return N1;
931 }
932
Chris Lattner1001c6e2005-01-15 06:17:04 +0000933 break;
934 }
935
936 EVTStruct NN;
937 NN.Opcode = Opcode;
938 NN.VT = VT;
939 NN.EVT = EVT;
940 NN.Ops.push_back(N1);
941
942 SDNode *&N = MVTSDNodes[NN];
943 if (N) return SDOperand(N, 0);
944 N = new MVTSDNode(Opcode, VT, N1, EVT);
945 AllNodes.push_back(N);
946 return SDOperand(N, 0);
947}
948
949SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner39c67442005-01-14 22:08:15 +0000950 SDOperand N2, MVT::ValueType EVT) {
951 switch (Opcode) {
952 default: assert(0 && "Bad opcode for this accessor!");
953 case ISD::EXTLOAD:
954 case ISD::SEXTLOAD:
955 case ISD::ZEXTLOAD:
956 // If they are asking for an extending loat from/to the same thing, return a
957 // normal load.
958 if (VT == EVT)
959 return getNode(ISD::LOAD, VT, N1, N2);
960 assert(EVT < VT && "Should only be an extending load, not truncating!");
961 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
962 "Cannot sign/zero extend a FP load!");
963 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
964 "Cannot convert from FP to Int or Int -> FP!");
965 break;
966 }
967
968 EVTStruct NN;
969 NN.Opcode = Opcode;
970 NN.VT = VT;
971 NN.EVT = EVT;
972 NN.Ops.push_back(N1);
973 NN.Ops.push_back(N2);
974
975 SDNode *&N = MVTSDNodes[NN];
976 if (N) return SDOperand(N, 0);
Chris Lattner3b8e7192005-01-14 22:38:01 +0000977 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner39c67442005-01-14 22:08:15 +0000978 AllNodes.push_back(N);
979 return SDOperand(N, 0);
980}
981
982SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
983 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
984 switch (Opcode) {
985 default: assert(0 && "Bad opcode for this accessor!");
986 case ISD::TRUNCSTORE:
Chris Lattner1001c6e2005-01-15 06:17:04 +0000987#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
988 // If this is a truncating store of a constant, convert to the desired type
989 // and store it instead.
990 if (isa<Constant>(N1)) {
991 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
992 if (isa<Constant>(Op))
993 N1 = Op;
994 }
995 // Also for ConstantFP?
996#endif
Chris Lattner39c67442005-01-14 22:08:15 +0000997 if (N1.getValueType() == EVT) // Normal store?
998 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner1001c6e2005-01-15 06:17:04 +0000999 assert(N2.getValueType() > EVT && "Not a truncation?");
1000 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner39c67442005-01-14 22:08:15 +00001001 "Can't do FP-INT conversion!");
1002 break;
1003 }
1004
1005 EVTStruct NN;
1006 NN.Opcode = Opcode;
1007 NN.VT = VT;
1008 NN.EVT = EVT;
1009 NN.Ops.push_back(N1);
1010 NN.Ops.push_back(N2);
1011 NN.Ops.push_back(N3);
1012
1013 SDNode *&N = MVTSDNodes[NN];
1014 if (N) return SDOperand(N, 0);
1015 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1016 AllNodes.push_back(N);
1017 return SDOperand(N, 0);
1018}
1019
1020
Chris Lattner40e79822005-01-12 18:37:47 +00001021/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1022/// indicated value. This method ignores uses of other values defined by this
1023/// operation.
1024bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1025 assert(Value < getNumValues() && "Bad value!");
1026
1027 // If there is only one value, this is easy.
1028 if (getNumValues() == 1)
1029 return use_size() == NUses;
1030 if (Uses.size() < NUses) return false;
1031
1032 SDOperand TheValue(this, Value);
1033
1034 std::set<SDNode*> UsersHandled;
1035
1036 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1037 UI != E; ++UI) {
1038 SDNode *User = *UI;
1039 if (User->getNumOperands() == 1 ||
1040 UsersHandled.insert(User).second) // First time we've seen this?
1041 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1042 if (User->getOperand(i) == TheValue) {
1043 if (NUses == 0)
1044 return false; // too many uses
1045 --NUses;
1046 }
1047 }
1048
1049 // Found exactly the right number of uses?
1050 return NUses == 0;
1051}
1052
1053
Chris Lattner9e4c7612005-01-10 23:25:25 +00001054const char *SDNode::getOperationName() const {
1055 switch (getOpcode()) {
1056 default: return "<<Unknown>>";
1057 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001058 case ISD::TokenFactor: return "TokenFactor";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001059 case ISD::Constant: return "Constant";
1060 case ISD::ConstantFP: return "ConstantFP";
1061 case ISD::GlobalAddress: return "GlobalAddress";
1062 case ISD::FrameIndex: return "FrameIndex";
1063 case ISD::BasicBlock: return "BasicBlock";
1064 case ISD::ExternalSymbol: return "ExternalSymbol";
1065 case ISD::ConstantPool: return "ConstantPoolIndex";
1066 case ISD::CopyToReg: return "CopyToReg";
1067 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattnere727af02005-01-13 20:50:02 +00001068 case ISD::ImplicitDef: return "ImplicitDef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001069
Chris Lattner9e4c7612005-01-10 23:25:25 +00001070 case ISD::ADD: return "add";
1071 case ISD::SUB: return "sub";
1072 case ISD::MUL: return "mul";
1073 case ISD::SDIV: return "sdiv";
1074 case ISD::UDIV: return "udiv";
1075 case ISD::SREM: return "srem";
1076 case ISD::UREM: return "urem";
1077 case ISD::AND: return "and";
1078 case ISD::OR: return "or";
1079 case ISD::XOR: return "xor";
1080 case ISD::SHL: return "shl";
1081 case ISD::SRA: return "sra";
1082 case ISD::SRL: return "srl";
1083
1084 case ISD::SELECT: return "select";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001085 case ISD::ADD_PARTS: return "add_parts";
1086 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001087
1088 // Conversion operators.
1089 case ISD::SIGN_EXTEND: return "sign_extend";
1090 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001091 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1092 case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001093 case ISD::TRUNCATE: return "truncate";
1094 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001095 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001096 case ISD::FP_EXTEND: return "fp_extend";
1097
1098 case ISD::SINT_TO_FP: return "sint_to_fp";
1099 case ISD::UINT_TO_FP: return "uint_to_fp";
1100 case ISD::FP_TO_SINT: return "fp_to_sint";
1101 case ISD::FP_TO_UINT: return "fp_to_uint";
1102
1103 // Control flow instructions
1104 case ISD::BR: return "br";
1105 case ISD::BRCOND: return "brcond";
1106 case ISD::RET: return "ret";
1107 case ISD::CALL: return "call";
1108 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1109 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1110
1111 // Other operators
1112 case ISD::LOAD: return "load";
1113 case ISD::STORE: return "store";
Chris Lattner39c67442005-01-14 22:08:15 +00001114 case ISD::EXTLOAD: return "extload";
1115 case ISD::SEXTLOAD: return "sextload";
1116 case ISD::ZEXTLOAD: return "zextload";
1117 case ISD::TRUNCSTORE: return "truncstore";
1118
Chris Lattner9e4c7612005-01-10 23:25:25 +00001119 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1120 case ISD::EXTRACT_ELEMENT: return "extract_element";
1121 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001122 case ISD::MEMSET: return "memset";
1123 case ISD::MEMCPY: return "memcpy";
1124 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001125
1126 case ISD::SETCC:
1127 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1128 switch (SetCC->getCondition()) {
1129 default: assert(0 && "Unknown setcc condition!");
1130 case ISD::SETOEQ: return "setcc:setoeq";
1131 case ISD::SETOGT: return "setcc:setogt";
1132 case ISD::SETOGE: return "setcc:setoge";
1133 case ISD::SETOLT: return "setcc:setolt";
1134 case ISD::SETOLE: return "setcc:setole";
1135 case ISD::SETONE: return "setcc:setone";
1136
1137 case ISD::SETO: return "setcc:seto";
1138 case ISD::SETUO: return "setcc:setuo";
1139 case ISD::SETUEQ: return "setcc:setue";
1140 case ISD::SETUGT: return "setcc:setugt";
1141 case ISD::SETUGE: return "setcc:setuge";
1142 case ISD::SETULT: return "setcc:setult";
1143 case ISD::SETULE: return "setcc:setule";
1144 case ISD::SETUNE: return "setcc:setune";
1145
1146 case ISD::SETEQ: return "setcc:seteq";
1147 case ISD::SETGT: return "setcc:setgt";
1148 case ISD::SETGE: return "setcc:setge";
1149 case ISD::SETLT: return "setcc:setlt";
1150 case ISD::SETLE: return "setcc:setle";
1151 case ISD::SETNE: return "setcc:setne";
1152 }
1153 }
1154}
Chris Lattner061a1ea2005-01-07 07:46:32 +00001155
1156void SDNode::dump() const {
1157 std::cerr << (void*)this << ": ";
1158
1159 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1160 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00001161 if (getValueType(i) == MVT::Other)
1162 std::cerr << "ch";
1163 else
1164 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001165 }
Chris Lattner9e4c7612005-01-10 23:25:25 +00001166 std::cerr << " = " << getOperationName();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001167
1168 std::cerr << " ";
1169 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1170 if (i) std::cerr << ", ";
1171 std::cerr << (void*)getOperand(i).Val;
1172 if (unsigned RN = getOperand(i).ResNo)
1173 std::cerr << ":" << RN;
1174 }
1175
1176 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1177 std::cerr << "<" << CSDN->getValue() << ">";
1178 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1179 std::cerr << "<" << CSDN->getValue() << ">";
1180 } else if (const GlobalAddressSDNode *GADN =
1181 dyn_cast<GlobalAddressSDNode>(this)) {
1182 std::cerr << "<";
1183 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1184 } else if (const FrameIndexSDNode *FIDN =
1185 dyn_cast<FrameIndexSDNode>(this)) {
1186 std::cerr << "<" << FIDN->getIndex() << ">";
1187 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1188 std::cerr << "<" << CP->getIndex() << ">";
1189 } else if (const BasicBlockSDNode *BBDN =
1190 dyn_cast<BasicBlockSDNode>(this)) {
1191 std::cerr << "<";
1192 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1193 if (LBB)
1194 std::cerr << LBB->getName() << " ";
1195 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnere727af02005-01-13 20:50:02 +00001196 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001197 std::cerr << "<reg #" << C2V->getReg() << ">";
1198 } else if (const ExternalSymbolSDNode *ES =
1199 dyn_cast<ExternalSymbolSDNode>(this)) {
1200 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner630d1932005-01-15 21:11:37 +00001201 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1202 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001203 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001204}
1205
Chris Lattnere6f78822005-01-09 20:38:33 +00001206static void DumpNodes(SDNode *N, unsigned indent) {
1207 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1208 if (N->getOperand(i).Val->hasOneUse())
1209 DumpNodes(N->getOperand(i).Val, indent+2);
1210 else
1211 std::cerr << "\n" << std::string(indent+2, ' ')
1212 << (void*)N->getOperand(i).Val << ": <multiple use>";
1213
1214
1215 std::cerr << "\n" << std::string(indent, ' ');
1216 N->dump();
1217}
1218
Chris Lattner061a1ea2005-01-07 07:46:32 +00001219void SelectionDAG::dump() const {
1220 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner1270acc2005-01-09 20:26:36 +00001221 std::vector<SDNode*> Nodes(AllNodes);
1222 std::sort(Nodes.begin(), Nodes.end());
1223
1224 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001225 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1226 DumpNodes(Nodes[i], 2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001227 }
Chris Lattnere6f78822005-01-09 20:38:33 +00001228
1229 DumpNodes(getRoot().Val, 2);
1230
Chris Lattner061a1ea2005-01-07 07:46:32 +00001231 std::cerr << "\n\n";
1232}
1233