blob: 513bab2f538958a9242e1948441f3c6738bdef05 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
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//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-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"
Chris Lattnerb48da392005-01-23 04:39:44 +000019#include "llvm/Target/TargetLowering.h"
Reid Spencer954da372004-07-04 12:19:56 +000020#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000021#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000023#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner5cdcc582005-01-09 20:52:51 +000026static bool isCommutativeBinOp(unsigned Opcode) {
27 switch (Opcode) {
28 case ISD::ADD:
29 case ISD::MUL:
30 case ISD::AND:
31 case ISD::OR:
32 case ISD::XOR: return true;
33 default: return false; // FIXME: Need commutative info for user ops!
34 }
35}
36
37static bool isAssociativeBinOp(unsigned Opcode) {
38 switch (Opcode) {
39 case ISD::ADD:
40 case ISD::MUL:
41 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need associative info for user ops!
45 }
46}
47
48static unsigned ExactLog2(uint64_t Val) {
49 unsigned Count = 0;
50 while (Val != 1) {
51 Val >>= 1;
52 ++Count;
53 }
54 return Count;
55}
56
57// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
61 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
62 return true;
63 return false;
64}
65
66
Chris Lattnerc3aae252005-01-07 07:46:32 +000067/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
68/// when given the operation for (X op Y).
69ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
70 // To perform this operation, we just need to swap the L and G bits of the
71 // operation.
72 unsigned OldL = (Operation >> 2) & 1;
73 unsigned OldG = (Operation >> 1) & 1;
74 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
75 (OldL << 1) | // New G bit
76 (OldG << 2)); // New L bit.
77}
78
79/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
80/// 'op' is a valid SetCC operation.
81ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
82 unsigned Operation = Op;
83 if (isInteger)
84 Operation ^= 7; // Flip L, G, E bits, but not U.
85 else
86 Operation ^= 15; // Flip all of the condition bits.
87 if (Operation > ISD::SETTRUE2)
88 Operation &= ~8; // Don't let N and U bits get set.
89 return ISD::CondCode(Operation);
90}
91
92
93/// isSignedOp - For an integer comparison, return 1 if the comparison is a
94/// signed operation and 2 if the result is an unsigned comparison. Return zero
95/// if the operation does not depend on the sign of the input (setne and seteq).
96static int isSignedOp(ISD::CondCode Opcode) {
97 switch (Opcode) {
98 default: assert(0 && "Illegal integer setcc operation!");
99 case ISD::SETEQ:
100 case ISD::SETNE: return 0;
101 case ISD::SETLT:
102 case ISD::SETLE:
103 case ISD::SETGT:
104 case ISD::SETGE: return 1;
105 case ISD::SETULT:
106 case ISD::SETULE:
107 case ISD::SETUGT:
108 case ISD::SETUGE: return 2;
109 }
110}
111
112/// getSetCCOrOperation - Return the result of a logical OR between different
113/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
114/// returns SETCC_INVALID if it is not possible to represent the resultant
115/// comparison.
116ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
117 bool isInteger) {
118 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
119 // Cannot fold a signed integer setcc with an unsigned integer setcc.
120 return ISD::SETCC_INVALID;
121
122 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
123
124 // If the N and U bits get set then the resultant comparison DOES suddenly
125 // care about orderedness, and is true when ordered.
126 if (Op > ISD::SETTRUE2)
127 Op &= ~16; // Clear the N bit.
128 return ISD::CondCode(Op);
129}
130
131/// getSetCCAndOperation - Return the result of a logical AND between different
132/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
133/// function returns zero if it is not possible to represent the resultant
134/// comparison.
135ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
136 bool isInteger) {
137 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
138 // Cannot fold a signed setcc with an unsigned setcc.
139 return ISD::SETCC_INVALID;
140
141 // Combine all of the condition bits.
142 return ISD::CondCode(Op1 & Op2);
143}
144
Chris Lattnerb48da392005-01-23 04:39:44 +0000145const TargetMachine &SelectionDAG::getTarget() const {
146 return TLI.getTargetMachine();
147}
148
149
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000150/// RemoveDeadNodes - This method deletes all unreachable nodes in the
151/// SelectionDAG, including nodes (like loads) that have uses of their token
152/// chain but no other uses and no side effect. If a node is passed in as an
153/// argument, it is used as the seed for node deletion.
154void SelectionDAG::RemoveDeadNodes(SDNode *N) {
155 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
156
157 // Create a dummy node (which is not added to allnodes), that adds a reference
158 // to the root node, preventing it from being deleted.
159 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
160
161 DeleteNodeIfDead(N, &AllNodeSet);
162
163 Restart:
164 unsigned NumNodes = AllNodeSet.size();
165 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
166 I != E; ++I) {
167 // Try to delete this node.
168 DeleteNodeIfDead(*I, &AllNodeSet);
169
170 // If we actually deleted any nodes, do not use invalid iterators in
171 // AllNodeSet.
172 if (AllNodeSet.size() != NumNodes)
173 goto Restart;
174 }
175
176 // Restore AllNodes.
177 if (AllNodes.size() != NumNodes)
178 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
179
180 // If the root changed (e.g. it was a dead load, update the root).
181 setRoot(DummyNode->getOperand(0));
182
183 // Now that we are done with the dummy node, delete it.
184 DummyNode->getOperand(0).Val->removeUser(DummyNode);
185 delete DummyNode;
186}
187
188void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
189 if (!N->use_empty())
190 return;
191
192 // Okay, we really are going to delete this node. First take this out of the
193 // appropriate CSE map.
194 switch (N->getOpcode()) {
195 case ISD::Constant:
196 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
197 N->getValueType(0)));
198 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000199 case ISD::ConstantFP: {
200 union {
201 double DV;
202 uint64_t IV;
203 };
204 DV = cast<ConstantFPSDNode>(N)->getValue();
205 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000206 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000207 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000208 case ISD::GlobalAddress:
209 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
210 break;
211 case ISD::FrameIndex:
212 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
213 break;
214 case ISD::ConstantPool:
215 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
216 break;
217 case ISD::BasicBlock:
218 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
219 break;
220 case ISD::ExternalSymbol:
221 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
222 break;
223
224 case ISD::LOAD:
225 Loads.erase(std::make_pair(N->getOperand(1),
226 std::make_pair(N->getOperand(0),
227 N->getValueType(0))));
228 break;
229 case ISD::SETCC:
230 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
231 N->getOperand(1)),
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000232 std::make_pair(
233 cast<SetCCSDNode>(N)->getCondition(),
234 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000235 break;
Chris Lattner859157d2005-01-15 06:17:04 +0000236 case ISD::TRUNCSTORE:
237 case ISD::SIGN_EXTEND_INREG:
238 case ISD::ZERO_EXTEND_INREG:
239 case ISD::FP_ROUND_INREG:
Chris Lattner69a52152005-01-14 22:38:01 +0000240 case ISD::EXTLOAD:
241 case ISD::SEXTLOAD:
242 case ISD::ZEXTLOAD: {
243 EVTStruct NN;
Chris Lattner8e8bd652005-04-07 00:30:13 +0000244 NN.Opcode = N->getOpcode();
Chris Lattner69a52152005-01-14 22:38:01 +0000245 NN.VT = N->getValueType(0);
246 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner859157d2005-01-15 06:17:04 +0000247 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
248 NN.Ops.push_back(N->getOperand(i));
Chris Lattner69a52152005-01-14 22:38:01 +0000249 MVTSDNodes.erase(NN);
250 break;
251 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000252 default:
253 if (N->getNumOperands() == 1)
254 UnaryOps.erase(std::make_pair(N->getOpcode(),
255 std::make_pair(N->getOperand(0),
256 N->getValueType(0))));
257 else if (N->getNumOperands() == 2)
258 BinaryOps.erase(std::make_pair(N->getOpcode(),
259 std::make_pair(N->getOperand(0),
260 N->getOperand(1))));
261 break;
262 }
263
264 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000266 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000267 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000268 O->removeUser(N);
269
270 // Now that we removed this operand, see if there are no uses of it left.
271 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000272 }
273
274 // Remove the node from the nodes set and delete it.
275 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
276 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000277
278 // Now that the node is gone, check to see if any of the operands of this node
279 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000280 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000281}
282
283
Chris Lattner78ec3112003-08-11 14:57:33 +0000284SelectionDAG::~SelectionDAG() {
285 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
286 delete AllNodes[i];
287}
288
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
290 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
291 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000292 if (VT != MVT::i64)
293 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
294
Chris Lattnerc3aae252005-01-07 07:46:32 +0000295 SDNode *&N = Constants[std::make_pair(Val, VT)];
296 if (N) return SDOperand(N, 0);
297 N = new ConstantSDNode(Val, VT);
298 AllNodes.push_back(N);
299 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000300}
301
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
303 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
304 if (VT == MVT::f32)
305 Val = (float)Val; // Mask out extra precision.
306
Chris Lattnerd8658612005-02-17 20:17:32 +0000307 // Do the map lookup using the actual bit pattern for the floating point
308 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
309 // we don't have issues with SNANs.
310 union {
311 double DV;
312 uint64_t IV;
313 };
314
315 DV = Val;
316
317 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000318 if (N) return SDOperand(N, 0);
319 N = new ConstantFPSDNode(Val, VT);
320 AllNodes.push_back(N);
321 return SDOperand(N, 0);
322}
323
324
325
326SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
327 MVT::ValueType VT) {
328 SDNode *&N = GlobalValues[GV];
329 if (N) return SDOperand(N, 0);
330 N = new GlobalAddressSDNode(GV,VT);
331 AllNodes.push_back(N);
332 return SDOperand(N, 0);
333}
334
335SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
336 SDNode *&N = FrameIndices[FI];
337 if (N) return SDOperand(N, 0);
338 N = new FrameIndexSDNode(FI, VT);
339 AllNodes.push_back(N);
340 return SDOperand(N, 0);
341}
342
343SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
344 SDNode *N = ConstantPoolIndices[CPIdx];
345 if (N) return SDOperand(N, 0);
346 N = new ConstantPoolSDNode(CPIdx, VT);
347 AllNodes.push_back(N);
348 return SDOperand(N, 0);
349}
350
351SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
352 SDNode *&N = BBNodes[MBB];
353 if (N) return SDOperand(N, 0);
354 N = new BasicBlockSDNode(MBB);
355 AllNodes.push_back(N);
356 return SDOperand(N, 0);
357}
358
359SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
360 SDNode *&N = ExternalSymbols[Sym];
361 if (N) return SDOperand(N, 0);
362 N = new ExternalSymbolSDNode(Sym, VT);
363 AllNodes.push_back(N);
364 return SDOperand(N, 0);
365}
366
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000367SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
368 SDOperand N1, SDOperand N2) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000369 // These setcc operations always fold.
370 switch (Cond) {
371 default: break;
372 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000373 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000374 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000375 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000376 }
377
Chris Lattner67255a12005-04-07 18:14:58 +0000378 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
379 uint64_t C2 = N2C->getValue();
380 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
381 uint64_t C1 = N1C->getValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000382
383 // Sign extend the operands if required
384 if (ISD::isSignedIntSetCC(Cond)) {
385 C1 = N1C->getSignExtended();
386 C2 = N2C->getSignExtended();
387 }
388
389 switch (Cond) {
390 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000391 case ISD::SETEQ: return getConstant(C1 == C2, VT);
392 case ISD::SETNE: return getConstant(C1 != C2, VT);
393 case ISD::SETULT: return getConstant(C1 < C2, VT);
394 case ISD::SETUGT: return getConstant(C1 > C2, VT);
395 case ISD::SETULE: return getConstant(C1 <= C2, VT);
396 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
397 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
398 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
399 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
400 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000401 }
Chris Lattner24673922005-04-07 18:58:54 +0000402 } else {
Chris Lattner67255a12005-04-07 18:14:58 +0000403 uint64_t MinVal, MaxVal;
404 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
405 if (ISD::isSignedIntSetCC(Cond)) {
406 MinVal = 1ULL << (OperandBitSize-1);
407 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
408 MaxVal = ~0ULL >> (65-OperandBitSize);
409 else
410 MaxVal = 0;
411 } else {
412 MinVal = 0;
413 MaxVal = ~0ULL >> (64-OperandBitSize);
414 }
415
416 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
417 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
418 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
419 --C2; // X >= C1 --> X > (C1-1)
420 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
421 N2 = getConstant(C2, N2.getValueType());
422 N2C = cast<ConstantSDNode>(N2.Val);
423 }
424
425 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
426 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
427 ++C2; // X <= C1 --> X < (C1+1)
428 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
429 N2 = getConstant(C2, N2.getValueType());
430 N2C = cast<ConstantSDNode>(N2.Val);
431 }
432
433 // If we have "setcc X, C1", check to see if we can shrink the immediate
434 // by changing cc.
435
436 // SETUGT X, SINTMAX -> SETLT X, 0
437 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
438 C2 == (~0ULL >> (65-OperandBitSize)))
439 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
440
441 // FIXME: Implement the rest of these.
442
Chris Lattnerc3aae252005-01-07 07:46:32 +0000443 }
Chris Lattner67255a12005-04-07 18:14:58 +0000444 } else if (isa<ConstantSDNode>(N1.Val)) {
445 // Ensure that the constant occurs on the RHS.
446 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
447 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000448
449 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
450 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
451 double C1 = N1C->getValue(), C2 = N2C->getValue();
452
453 switch (Cond) {
454 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000455 case ISD::SETEQ: return getConstant(C1 == C2, VT);
456 case ISD::SETNE: return getConstant(C1 != C2, VT);
457 case ISD::SETLT: return getConstant(C1 < C2, VT);
458 case ISD::SETGT: return getConstant(C1 > C2, VT);
459 case ISD::SETLE: return getConstant(C1 <= C2, VT);
460 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000461 }
462 } else {
463 // Ensure that the constant occurs on the RHS.
464 Cond = ISD::getSetCCSwappedOperands(Cond);
465 std::swap(N1, N2);
466 }
467
468 if (N1 == N2) {
469 // We can always fold X == Y for integer setcc's.
470 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000471 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000472 unsigned UOF = ISD::getUnorderedFlavor(Cond);
473 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000474 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000475 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000476 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000477 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
478 // if it is not already.
479 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
480 }
481
Chris Lattner5cdcc582005-01-09 20:52:51 +0000482 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000483 MVT::isInteger(N1.getValueType())) {
484 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
485 N1.getOpcode() == ISD::XOR) {
486 // Simplify (X+Y) == (X+Z) --> Y == Z
487 if (N1.getOpcode() == N2.getOpcode()) {
488 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000489 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000490 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000491 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000492 if (isCommutativeBinOp(N1.getOpcode())) {
493 // If X op Y == Y op X, try other combinations.
494 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000495 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000496 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000497 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000498 }
499 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000500
501 // FIXME: move this stuff to the DAG Combiner when it exists!
Chris Lattner68dc3102005-01-10 02:03:02 +0000502
503 // Simplify (X+Z) == X --> Z == 0
504 if (N1.getOperand(0) == N2)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000505 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000506 getConstant(0, N1.getValueType()));
507 if (N1.getOperand(1) == N2) {
508 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000509 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000510 getConstant(0, N1.getValueType()));
511 else {
512 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
513 // (Z-X) == X --> Z == X<<1
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000514 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000515 getNode(ISD::SHL, N2.getValueType(),
Chris Lattnerb48da392005-01-23 04:39:44 +0000516 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner68dc3102005-01-10 02:03:02 +0000517 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000518 }
519 }
520
Chris Lattner68dc3102005-01-10 02:03:02 +0000521 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
522 N2.getOpcode() == ISD::XOR) {
523 // Simplify X == (X+Z) --> Z == 0
524 if (N2.getOperand(0) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000525 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000526 getConstant(0, N2.getValueType()));
527 else if (N2.getOperand(1) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000528 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000529 getConstant(0, N2.getValueType()));
530 }
531 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000532
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000533 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
534 std::make_pair(Cond, VT))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000535 if (N) return SDOperand(N, 0);
536 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000537 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000538 AllNodes.push_back(N);
539 return SDOperand(N, 0);
540}
541
542
543
544/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000545///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000546SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
547 SDNode *N = new SDNode(Opcode, VT);
548 AllNodes.push_back(N);
549 return SDOperand(N, 0);
550}
551
Chris Lattnerc3aae252005-01-07 07:46:32 +0000552SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
553 SDOperand Operand) {
554 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
555 uint64_t Val = C->getValue();
556 switch (Opcode) {
557 default: break;
558 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
559 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
560 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000561 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
562 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000563 }
564 }
565
566 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
567 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000568 case ISD::FNEG:
569 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000570 case ISD::FP_ROUND:
571 case ISD::FP_EXTEND:
572 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000573 case ISD::FP_TO_SINT:
574 return getConstant((int64_t)C->getValue(), VT);
575 case ISD::FP_TO_UINT:
576 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000577 }
578
579 unsigned OpOpcode = Operand.Val->getOpcode();
580 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000581 case ISD::TokenFactor:
582 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000583 case ISD::SIGN_EXTEND:
584 if (Operand.getValueType() == VT) return Operand; // noop extension
585 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
586 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
587 break;
588 case ISD::ZERO_EXTEND:
589 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000590 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000591 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 break;
593 case ISD::TRUNCATE:
594 if (Operand.getValueType() == VT) return Operand; // noop truncate
595 if (OpOpcode == ISD::TRUNCATE)
596 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000597 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
598 // If the source is smaller than the dest, we still need an extend.
599 if (Operand.Val->getOperand(0).getValueType() < VT)
600 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
601 else if (Operand.Val->getOperand(0).getValueType() > VT)
602 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
603 else
604 return Operand.Val->getOperand(0);
605 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000606 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000607 case ISD::FNEG:
608 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
609 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
610 Operand.Val->getOperand(0));
611 if (OpOpcode == ISD::FNEG) // --X -> X
612 return Operand.Val->getOperand(0);
613 break;
614 case ISD::FABS:
615 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
616 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
617 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000618 }
619
620 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
621 if (N) return SDOperand(N, 0);
622 N = new SDNode(Opcode, Operand);
623 N->setValueTypes(VT);
624 AllNodes.push_back(N);
625 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000626}
627
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
629 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000630#ifndef NDEBUG
631 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000632 case ISD::TokenFactor:
633 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
634 N2.getValueType() == MVT::Other && "Invalid token factor!");
635 break;
Chris Lattner76365122005-01-16 02:23:22 +0000636 case ISD::AND:
637 case ISD::OR:
638 case ISD::XOR:
639 case ISD::UDIV:
640 case ISD::UREM:
641 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
642 // fall through
643 case ISD::ADD:
644 case ISD::SUB:
645 case ISD::MUL:
646 case ISD::SDIV:
647 case ISD::SREM:
648 assert(N1.getValueType() == N2.getValueType() &&
649 N1.getValueType() == VT && "Binary operator types must match!");
650 break;
651
652 case ISD::SHL:
653 case ISD::SRA:
654 case ISD::SRL:
655 assert(VT == N1.getValueType() &&
656 "Shift operators return type must be the same as their first arg");
657 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000658 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000659 break;
660 default: break;
661 }
662#endif
663
Chris Lattnerc3aae252005-01-07 07:46:32 +0000664 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
665 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
666 if (N1C) {
667 if (N2C) {
668 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
669 switch (Opcode) {
670 case ISD::ADD: return getConstant(C1 + C2, VT);
671 case ISD::SUB: return getConstant(C1 - C2, VT);
672 case ISD::MUL: return getConstant(C1 * C2, VT);
673 case ISD::UDIV:
674 if (C2) return getConstant(C1 / C2, VT);
675 break;
676 case ISD::UREM :
677 if (C2) return getConstant(C1 % C2, VT);
678 break;
679 case ISD::SDIV :
680 if (C2) return getConstant(N1C->getSignExtended() /
681 N2C->getSignExtended(), VT);
682 break;
683 case ISD::SREM :
684 if (C2) return getConstant(N1C->getSignExtended() %
685 N2C->getSignExtended(), VT);
686 break;
687 case ISD::AND : return getConstant(C1 & C2, VT);
688 case ISD::OR : return getConstant(C1 | C2, VT);
689 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000690 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
691 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
692 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000693 default: break;
694 }
695
696 } else { // Cannonicalize constant to RHS if commutative
697 if (isCommutativeBinOp(Opcode)) {
698 std::swap(N1C, N2C);
699 std::swap(N1, N2);
700 }
701 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000702
703 switch (Opcode) {
704 default: break;
705 case ISD::SHL: // shl 0, X -> 0
706 if (N1C->isNullValue()) return N1;
707 break;
708 case ISD::SRL: // srl 0, X -> 0
709 if (N1C->isNullValue()) return N1;
710 break;
711 case ISD::SRA: // sra -1, X -> -1
712 if (N1C->isAllOnesValue()) return N1;
713 break;
714 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715 }
716
717 if (N2C) {
718 uint64_t C2 = N2C->getValue();
719
720 switch (Opcode) {
721 case ISD::ADD:
722 if (!C2) return N1; // add X, 0 -> X
723 break;
724 case ISD::SUB:
725 if (!C2) return N1; // sub X, 0 -> X
726 break;
727 case ISD::MUL:
728 if (!C2) return N2; // mul X, 0 -> 0
729 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
730 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
731
Chris Lattnerb48da392005-01-23 04:39:44 +0000732 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733 if ((C2 & C2-1) == 0) {
Chris Lattnerb48da392005-01-23 04:39:44 +0000734 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
735 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000736 }
737 break;
738
739 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +0000740 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000741 if ((C2 & C2-1) == 0 && C2) {
Chris Lattnerb48da392005-01-23 04:39:44 +0000742 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
743 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000744 }
745 break;
746
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000747 case ISD::SHL:
748 case ISD::SRL:
Chris Lattnerb48da392005-01-23 04:39:44 +0000749 // If the shift amount is bigger than the size of the data, simplify.
750 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
751 if (TLI.getShiftAmountFlavor() == TargetLowering::Mask) {
752 unsigned NewAmt =
753 C2 & ((1 << MVT::getSizeInBits(N1.getValueType()))-1);
754 return getNode(Opcode, VT, N1, getConstant(NewAmt,N2.getValueType()));
755 } else if (TLI.getShiftAmountFlavor() == TargetLowering::Extend) {
756 // Shifting all of the bits out?
757 return getConstant(0, N1.getValueType());
758 }
759 }
760 // FALL THROUGH.
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000761 case ISD::SRA:
762 if (C2 == 0) return N1;
763 break;
764
Chris Lattnerc3aae252005-01-07 07:46:32 +0000765 case ISD::AND:
766 if (!C2) return N2; // X and 0 -> 0
767 if (N2C->isAllOnesValue())
768 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +0000769
Chris Lattnerdea29e22005-04-10 01:13:15 +0000770 // FIXME: Should add a corresponding version of this for
771 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
772 // we don't have yet.
773
Chris Lattnera2daa8c2005-04-09 21:43:54 +0000774 // and (zero_extend_inreg x:16:32), 1 -> and x, 1
775 if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
776 N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
777 // If we are masking out the part of our input that was extended, just
778 // mask the input to the extension directly.
779 unsigned ExtendBits =
780 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
781 if ((C2 & (~0ULL << ExtendBits)) == 0)
782 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
783 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784 break;
785 case ISD::OR:
786 if (!C2)return N1; // X or 0 -> X
787 if (N2C->isAllOnesValue())
788 return N2; // X or -1 -> -1
789 break;
790 case ISD::XOR:
791 if (!C2) return N1; // X xor 0 -> X
792 if (N2C->isAllOnesValue()) {
793 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
794 // !(X op Y) -> (X !op Y)
795 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
796 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000797 SetCC->getValueType(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000798 SetCC->getOperand(0), SetCC->getOperand(1));
799 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
800 SDNode *Op = N1.Val;
801 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
802 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
803 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
804 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
805 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
806 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
807 if (Op->getOpcode() == ISD::AND)
808 return getNode(ISD::OR, VT, LHS, RHS);
809 return getNode(ISD::AND, VT, LHS, RHS);
810 }
811 }
812 // X xor -1 -> not(x) ?
813 }
814 break;
815 }
816
817 // Reassociate ((X op C1) op C2) if possible.
818 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
819 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +0000820 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000821 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
822 }
823
824 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
825 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
826 if (N1CFP)
827 if (N2CFP) {
828 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
829 switch (Opcode) {
830 case ISD::ADD: return getConstantFP(C1 + C2, VT);
831 case ISD::SUB: return getConstantFP(C1 - C2, VT);
832 case ISD::MUL: return getConstantFP(C1 * C2, VT);
833 case ISD::SDIV:
834 if (C2) return getConstantFP(C1 / C2, VT);
835 break;
836 case ISD::SREM :
837 if (C2) return getConstantFP(fmod(C1, C2), VT);
838 break;
839 default: break;
840 }
841
842 } else { // Cannonicalize constant to RHS if commutative
843 if (isCommutativeBinOp(Opcode)) {
844 std::swap(N1CFP, N2CFP);
845 std::swap(N1, N2);
846 }
847 }
848
849 // Finally, fold operations that do not require constants.
850 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000851 case ISD::TokenFactor:
852 if (N1.getOpcode() == ISD::EntryToken)
853 return N2;
854 if (N2.getOpcode() == ISD::EntryToken)
855 return N1;
856 break;
857
Chris Lattnerc3aae252005-01-07 07:46:32 +0000858 case ISD::AND:
859 case ISD::OR:
860 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
861 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
862 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
863 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
864 ISD::CondCode Op2 = RHS->getCondition();
865
866 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
867 if (LL == RR && LR == RL) {
868 Op2 = ISD::getSetCCSwappedOperands(Op2);
869 goto MatchedBackwards;
870 }
871
872 if (LL == RL && LR == RR) {
873 MatchedBackwards:
874 ISD::CondCode Result;
875 bool isInteger = MVT::isInteger(LL.getValueType());
876 if (Opcode == ISD::OR)
877 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
878 isInteger);
879 else
880 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
881 isInteger);
882 if (Result != ISD::SETCC_INVALID)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000883 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884 }
885 }
886 break;
887 case ISD::XOR:
888 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
889 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000890 case ISD::ADD:
891 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
892 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
893 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
894 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +0000895 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
896 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
897 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
898 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
899 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
900 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Chris Lattner485df9b2005-04-09 03:02:46 +0000901 break;
Chris Lattnerabd21822005-01-09 20:09:57 +0000902 case ISD::SUB:
903 if (N1.getOpcode() == ISD::ADD) {
904 if (N1.Val->getOperand(0) == N2)
905 return N1.Val->getOperand(1); // (A+B)-A == B
906 if (N1.Val->getOperand(1) == N2)
907 return N1.Val->getOperand(0); // (A+B)-B == A
908 }
Chris Lattner485df9b2005-04-09 03:02:46 +0000909 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
910 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +0000911 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000912 }
913
914 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
915 if (N) return SDOperand(N, 0);
916 N = new SDNode(Opcode, N1, N2);
917 N->setValueTypes(VT);
918
919 AllNodes.push_back(N);
920 return SDOperand(N, 0);
921}
922
923SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
924 SDOperand Chain, SDOperand Ptr) {
925 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
926 if (N) return SDOperand(N, 0);
927 N = new SDNode(ISD::LOAD, Chain, Ptr);
928
929 // Loads have a token chain.
930 N->setValueTypes(VT, MVT::Other);
931 AllNodes.push_back(N);
932 return SDOperand(N, 0);
933}
934
935
936SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
937 SDOperand N1, SDOperand N2, SDOperand N3) {
938 // Perform various simplifications.
939 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
940 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
941 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
942 switch (Opcode) {
943 case ISD::SELECT:
944 if (N1C)
945 if (N1C->getValue())
946 return N2; // select true, X, Y -> X
947 else
948 return N3; // select false, X, Y -> Y
949
950 if (N2 == N3) return N2; // select C, X, X -> X
951
952 if (VT == MVT::i1) { // Boolean SELECT
953 if (N2C) {
954 if (N3C) {
955 if (N2C->getValue()) // select C, 1, 0 -> C
956 return N1;
957 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
958 }
959
960 if (N2C->getValue()) // select C, 1, X -> C | X
961 return getNode(ISD::OR, VT, N1, N3);
962 else // select C, 0, X -> ~C & X
963 return getNode(ISD::AND, VT,
964 getNode(ISD::XOR, N1.getValueType(), N1,
965 getConstant(1, N1.getValueType())), N3);
966 } else if (N3C) {
967 if (N3C->getValue()) // select C, X, 1 -> ~C | X
968 return getNode(ISD::OR, VT,
969 getNode(ISD::XOR, N1.getValueType(), N1,
970 getConstant(1, N1.getValueType())), N2);
971 else // select C, X, 0 -> C & X
972 return getNode(ISD::AND, VT, N1, N2);
973 }
974 }
975
Chris Lattner59723e92005-04-09 05:15:53 +0000976 // If this is a selectcc, check to see if we can simplify the result.
977 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
978 if (ConstantFPSDNode *CFP =
979 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
980 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
981 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
982 if ((SetCC->getCondition() == ISD::SETGE ||
983 SetCC->getCondition() == ISD::SETGT) &&
984 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
985 N3.getOperand(0) == N2)
986 return getNode(ISD::FABS, VT, N2);
987
988 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
989 if ((SetCC->getCondition() == ISD::SETLT ||
990 SetCC->getCondition() == ISD::SETLE) &&
991 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
992 N2.getOperand(0) == N3)
993 return getNode(ISD::FABS, VT, N3);
994 }
995
996 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000997 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +0000998 case ISD::BRCOND:
999 if (N2C)
1000 if (N2C->getValue()) // Unconditional branch
1001 return getNode(ISD::BR, MVT::Other, N1, N3);
1002 else
1003 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001004 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001005 }
1006
1007 SDNode *N = new SDNode(Opcode, N1, N2, N3);
1008 switch (Opcode) {
1009 default:
1010 N->setValueTypes(VT);
1011 break;
1012 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
1013 N->setValueTypes(VT, MVT::Other);
1014 break;
Chris Lattner5b359c62005-04-02 04:00:59 +00001015
1016 case ISD::SRA_PARTS:
1017 case ISD::SRL_PARTS:
1018 case ISD::SHL_PARTS: {
1019 std::vector<MVT::ValueType> V(N->getNumOperands()-1, VT);
1020 N->setValueTypes(V);
1021 break;
1022 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023 }
1024
1025 // FIXME: memoize NODES
1026 AllNodes.push_back(N);
1027 return SDOperand(N, 0);
1028}
1029
1030SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1031 std::vector<SDOperand> &Children) {
1032 switch (Children.size()) {
1033 case 0: return getNode(Opcode, VT);
1034 case 1: return getNode(Opcode, VT, Children[0]);
1035 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
1036 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001037 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001038 }
Chris Lattneref847df2005-04-09 03:27:28 +00001039
1040 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Children[1].Val);
1041 switch (Opcode) {
1042 default: break;
1043 case ISD::BRCONDTWOWAY:
1044 if (N1C)
1045 if (N1C->getValue()) // Unconditional branch to true dest.
1046 return getNode(ISD::BR, MVT::Other, Children[0], Children[2]);
1047 else // Unconditional branch to false dest.
1048 return getNode(ISD::BR, MVT::Other, Children[0], Children[3]);
1049 break;
1050 }
1051
1052 // FIXME: MEMOIZE!!
1053 SDNode *N = new SDNode(Opcode, Children);
1054 if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
1055 N->setValueTypes(VT);
1056 } else {
1057 std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
1058 N->setValueTypes(V);
1059 }
1060 AllNodes.push_back(N);
1061 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062}
1063
Chris Lattner2ee743f2005-01-14 22:08:15 +00001064SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner859157d2005-01-15 06:17:04 +00001065 MVT::ValueType EVT) {
1066
1067 switch (Opcode) {
1068 default: assert(0 && "Bad opcode for this accessor!");
1069 case ISD::FP_ROUND_INREG:
1070 assert(VT == N1.getValueType() && "Not an inreg round!");
1071 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1072 "Cannot FP_ROUND_INREG integer types");
1073 if (EVT == VT) return N1; // Not actually rounding
1074 assert(EVT < VT && "Not rounding down!");
Chris Lattner14723c22005-03-09 18:37:12 +00001075
1076 if (isa<ConstantFPSDNode>(N1))
1077 return getNode(ISD::FP_EXTEND, VT, getNode(ISD::FP_ROUND, EVT, N1));
Chris Lattner859157d2005-01-15 06:17:04 +00001078 break;
1079 case ISD::ZERO_EXTEND_INREG:
1080 case ISD::SIGN_EXTEND_INREG:
1081 assert(VT == N1.getValueType() && "Not an inreg extend!");
1082 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1083 "Cannot *_EXTEND_INREG FP types");
1084 if (EVT == VT) return N1; // Not actually extending
1085 assert(EVT < VT && "Not extending!");
Chris Lattner950aa3c2005-01-16 00:17:20 +00001086
Chris Lattner14723c22005-03-09 18:37:12 +00001087 // Extending a constant? Just return the constant.
1088 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1089 SDOperand Tmp = getNode(ISD::TRUNCATE, EVT, N1);
Chris Lattneree639f12005-03-10 20:55:51 +00001090 if (Opcode == ISD::ZERO_EXTEND_INREG)
Chris Lattner14723c22005-03-09 18:37:12 +00001091 return getNode(ISD::ZERO_EXTEND, VT, Tmp);
1092 else
1093 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
1094 }
1095
Chris Lattner950aa3c2005-01-16 00:17:20 +00001096 // If we are sign extending an extension, use the original source.
1097 if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
1098 N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1099 if (N1.getOpcode() == Opcode &&
1100 cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1101 return N1;
1102 }
1103
Chris Lattner45278e32005-04-10 04:33:08 +00001104 // If we are (zero|sign) extending a [zs]extload, return just the load.
1105 if ((N1.getOpcode() == ISD::ZEXTLOAD && Opcode == ISD::ZERO_EXTEND_INREG) ||
1106 (N1.getOpcode() == ISD::SEXTLOAD && Opcode == ISD::SIGN_EXTEND_INREG))
1107 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1108 return N1;
1109
Chris Lattner5a6bace2005-04-07 19:43:53 +00001110 // If we are extending the result of a setcc, and we already know the
1111 // contents of the top bits, eliminate the extension.
1112 if (N1.getOpcode() == ISD::SETCC)
1113 switch (TLI.getSetCCResultContents()) {
1114 case TargetLowering::UndefinedSetCCResult: break;
1115 case TargetLowering::ZeroOrOneSetCCResult:
1116 if (Opcode == ISD::ZERO_EXTEND_INREG) return N1;
1117 break;
1118 case TargetLowering::ZeroOrNegativeOneSetCCResult:
1119 if (Opcode == ISD::SIGN_EXTEND_INREG) return N1;
1120 break;
1121 }
Chris Lattner2bb6f412005-04-10 23:37:16 +00001122
1123 // If we are sign extending the result of an (and X, C) operation, and we
1124 // know the extended bits are zeros already, don't do the extend.
1125 if (N1.getOpcode() == ISD::AND)
1126 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1127 uint64_t Mask = N1C->getValue();
1128 unsigned NumBits = MVT::getSizeInBits(EVT);
1129 if (Opcode == ISD::ZERO_EXTEND_INREG) {
1130 if ((Mask & (~0ULL << NumBits)) == 0)
1131 return N1;
1132 else
1133 return getNode(ISD::AND, VT, N1.getOperand(0),
1134 getConstant(Mask & (~0ULL >> (64-NumBits)), VT));
1135 } else {
1136 assert(Opcode == ISD::SIGN_EXTEND_INREG);
1137 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1138 return N1;
1139 }
1140 }
Chris Lattner859157d2005-01-15 06:17:04 +00001141 break;
1142 }
1143
1144 EVTStruct NN;
1145 NN.Opcode = Opcode;
1146 NN.VT = VT;
1147 NN.EVT = EVT;
1148 NN.Ops.push_back(N1);
1149
1150 SDNode *&N = MVTSDNodes[NN];
1151 if (N) return SDOperand(N, 0);
1152 N = new MVTSDNode(Opcode, VT, N1, EVT);
1153 AllNodes.push_back(N);
1154 return SDOperand(N, 0);
1155}
1156
1157SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner2ee743f2005-01-14 22:08:15 +00001158 SDOperand N2, MVT::ValueType EVT) {
1159 switch (Opcode) {
1160 default: assert(0 && "Bad opcode for this accessor!");
1161 case ISD::EXTLOAD:
1162 case ISD::SEXTLOAD:
1163 case ISD::ZEXTLOAD:
1164 // If they are asking for an extending loat from/to the same thing, return a
1165 // normal load.
1166 if (VT == EVT)
1167 return getNode(ISD::LOAD, VT, N1, N2);
1168 assert(EVT < VT && "Should only be an extending load, not truncating!");
1169 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1170 "Cannot sign/zero extend a FP load!");
1171 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1172 "Cannot convert from FP to Int or Int -> FP!");
1173 break;
1174 }
1175
1176 EVTStruct NN;
1177 NN.Opcode = Opcode;
1178 NN.VT = VT;
1179 NN.EVT = EVT;
1180 NN.Ops.push_back(N1);
1181 NN.Ops.push_back(N2);
1182
1183 SDNode *&N = MVTSDNodes[NN];
1184 if (N) return SDOperand(N, 0);
Chris Lattner69a52152005-01-14 22:38:01 +00001185 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner2ee743f2005-01-14 22:08:15 +00001186 AllNodes.push_back(N);
1187 return SDOperand(N, 0);
1188}
1189
1190SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
1191 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
1192 switch (Opcode) {
1193 default: assert(0 && "Bad opcode for this accessor!");
1194 case ISD::TRUNCSTORE:
Chris Lattner859157d2005-01-15 06:17:04 +00001195#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1196 // If this is a truncating store of a constant, convert to the desired type
1197 // and store it instead.
1198 if (isa<Constant>(N1)) {
1199 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1200 if (isa<Constant>(Op))
1201 N1 = Op;
1202 }
1203 // Also for ConstantFP?
1204#endif
Chris Lattner2ee743f2005-01-14 22:08:15 +00001205 if (N1.getValueType() == EVT) // Normal store?
1206 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner859157d2005-01-15 06:17:04 +00001207 assert(N2.getValueType() > EVT && "Not a truncation?");
1208 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner2ee743f2005-01-14 22:08:15 +00001209 "Can't do FP-INT conversion!");
1210 break;
1211 }
1212
1213 EVTStruct NN;
1214 NN.Opcode = Opcode;
1215 NN.VT = VT;
1216 NN.EVT = EVT;
1217 NN.Ops.push_back(N1);
1218 NN.Ops.push_back(N2);
1219 NN.Ops.push_back(N3);
1220
1221 SDNode *&N = MVTSDNodes[NN];
1222 if (N) return SDOperand(N, 0);
1223 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1224 AllNodes.push_back(N);
1225 return SDOperand(N, 0);
1226}
1227
1228
Chris Lattner5c884562005-01-12 18:37:47 +00001229/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1230/// indicated value. This method ignores uses of other values defined by this
1231/// operation.
1232bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1233 assert(Value < getNumValues() && "Bad value!");
1234
1235 // If there is only one value, this is easy.
1236 if (getNumValues() == 1)
1237 return use_size() == NUses;
1238 if (Uses.size() < NUses) return false;
1239
1240 SDOperand TheValue(this, Value);
1241
1242 std::set<SDNode*> UsersHandled;
1243
1244 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1245 UI != E; ++UI) {
1246 SDNode *User = *UI;
1247 if (User->getNumOperands() == 1 ||
1248 UsersHandled.insert(User).second) // First time we've seen this?
1249 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1250 if (User->getOperand(i) == TheValue) {
1251 if (NUses == 0)
1252 return false; // too many uses
1253 --NUses;
1254 }
1255 }
1256
1257 // Found exactly the right number of uses?
1258 return NUses == 0;
1259}
1260
1261
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001262const char *SDNode::getOperationName() const {
1263 switch (getOpcode()) {
1264 default: return "<<Unknown>>";
Andrew Lenharth95762122005-03-31 21:24:06 +00001265 case ISD::PCMARKER: return "PCMarker";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001266 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001267 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001268 case ISD::Constant: return "Constant";
1269 case ISD::ConstantFP: return "ConstantFP";
1270 case ISD::GlobalAddress: return "GlobalAddress";
1271 case ISD::FrameIndex: return "FrameIndex";
1272 case ISD::BasicBlock: return "BasicBlock";
1273 case ISD::ExternalSymbol: return "ExternalSymbol";
1274 case ISD::ConstantPool: return "ConstantPoolIndex";
1275 case ISD::CopyToReg: return "CopyToReg";
1276 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001277 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001278 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001279
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001280 // Unary operators
1281 case ISD::FABS: return "fabs";
1282 case ISD::FNEG: return "fneg";
1283
1284 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001285 case ISD::ADD: return "add";
1286 case ISD::SUB: return "sub";
1287 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001288 case ISD::MULHU: return "mulhu";
1289 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001290 case ISD::SDIV: return "sdiv";
1291 case ISD::UDIV: return "udiv";
1292 case ISD::SREM: return "srem";
1293 case ISD::UREM: return "urem";
1294 case ISD::AND: return "and";
1295 case ISD::OR: return "or";
1296 case ISD::XOR: return "xor";
1297 case ISD::SHL: return "shl";
1298 case ISD::SRA: return "sra";
1299 case ISD::SRL: return "srl";
1300
1301 case ISD::SELECT: return "select";
Chris Lattner17eee182005-01-20 18:50:55 +00001302 case ISD::ADD_PARTS: return "add_parts";
1303 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001304 case ISD::SHL_PARTS: return "shl_parts";
1305 case ISD::SRA_PARTS: return "sra_parts";
1306 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001307
1308 // Conversion operators.
1309 case ISD::SIGN_EXTEND: return "sign_extend";
1310 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001311 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1312 case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001313 case ISD::TRUNCATE: return "truncate";
1314 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001315 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001316 case ISD::FP_EXTEND: return "fp_extend";
1317
1318 case ISD::SINT_TO_FP: return "sint_to_fp";
1319 case ISD::UINT_TO_FP: return "uint_to_fp";
1320 case ISD::FP_TO_SINT: return "fp_to_sint";
1321 case ISD::FP_TO_UINT: return "fp_to_uint";
1322
1323 // Control flow instructions
1324 case ISD::BR: return "br";
1325 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001326 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001327 case ISD::RET: return "ret";
1328 case ISD::CALL: return "call";
1329 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1330 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1331
1332 // Other operators
1333 case ISD::LOAD: return "load";
1334 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001335 case ISD::EXTLOAD: return "extload";
1336 case ISD::SEXTLOAD: return "sextload";
1337 case ISD::ZEXTLOAD: return "zextload";
1338 case ISD::TRUNCSTORE: return "truncstore";
1339
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001340 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1341 case ISD::EXTRACT_ELEMENT: return "extract_element";
1342 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001343 case ISD::MEMSET: return "memset";
1344 case ISD::MEMCPY: return "memcpy";
1345 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001346
1347 case ISD::SETCC:
1348 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1349 switch (SetCC->getCondition()) {
1350 default: assert(0 && "Unknown setcc condition!");
1351 case ISD::SETOEQ: return "setcc:setoeq";
1352 case ISD::SETOGT: return "setcc:setogt";
1353 case ISD::SETOGE: return "setcc:setoge";
1354 case ISD::SETOLT: return "setcc:setolt";
1355 case ISD::SETOLE: return "setcc:setole";
1356 case ISD::SETONE: return "setcc:setone";
1357
1358 case ISD::SETO: return "setcc:seto";
1359 case ISD::SETUO: return "setcc:setuo";
1360 case ISD::SETUEQ: return "setcc:setue";
1361 case ISD::SETUGT: return "setcc:setugt";
1362 case ISD::SETUGE: return "setcc:setuge";
1363 case ISD::SETULT: return "setcc:setult";
1364 case ISD::SETULE: return "setcc:setule";
1365 case ISD::SETUNE: return "setcc:setune";
1366
1367 case ISD::SETEQ: return "setcc:seteq";
1368 case ISD::SETGT: return "setcc:setgt";
1369 case ISD::SETGE: return "setcc:setge";
1370 case ISD::SETLT: return "setcc:setlt";
1371 case ISD::SETLE: return "setcc:setle";
1372 case ISD::SETNE: return "setcc:setne";
1373 }
1374 }
1375}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001376
1377void SDNode::dump() const {
1378 std::cerr << (void*)this << ": ";
1379
1380 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1381 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001382 if (getValueType(i) == MVT::Other)
1383 std::cerr << "ch";
1384 else
1385 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001386 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001387 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001388
1389 std::cerr << " ";
1390 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1391 if (i) std::cerr << ", ";
1392 std::cerr << (void*)getOperand(i).Val;
1393 if (unsigned RN = getOperand(i).ResNo)
1394 std::cerr << ":" << RN;
1395 }
1396
1397 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1398 std::cerr << "<" << CSDN->getValue() << ">";
1399 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1400 std::cerr << "<" << CSDN->getValue() << ">";
1401 } else if (const GlobalAddressSDNode *GADN =
1402 dyn_cast<GlobalAddressSDNode>(this)) {
1403 std::cerr << "<";
1404 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1405 } else if (const FrameIndexSDNode *FIDN =
1406 dyn_cast<FrameIndexSDNode>(this)) {
1407 std::cerr << "<" << FIDN->getIndex() << ">";
1408 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1409 std::cerr << "<" << CP->getIndex() << ">";
1410 } else if (const BasicBlockSDNode *BBDN =
1411 dyn_cast<BasicBlockSDNode>(this)) {
1412 std::cerr << "<";
1413 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1414 if (LBB)
1415 std::cerr << LBB->getName() << " ";
1416 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001417 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001418 std::cerr << "<reg #" << C2V->getReg() << ">";
1419 } else if (const ExternalSymbolSDNode *ES =
1420 dyn_cast<ExternalSymbolSDNode>(this)) {
1421 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner8a389bb2005-01-15 21:11:37 +00001422 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1423 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001424 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001425}
1426
Chris Lattnerea946cd2005-01-09 20:38:33 +00001427static void DumpNodes(SDNode *N, unsigned indent) {
1428 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1429 if (N->getOperand(i).Val->hasOneUse())
1430 DumpNodes(N->getOperand(i).Val, indent+2);
1431 else
1432 std::cerr << "\n" << std::string(indent+2, ' ')
1433 << (void*)N->getOperand(i).Val << ": <multiple use>";
1434
1435
1436 std::cerr << "\n" << std::string(indent, ' ');
1437 N->dump();
1438}
1439
Chris Lattnerc3aae252005-01-07 07:46:32 +00001440void SelectionDAG::dump() const {
1441 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001442 std::vector<SDNode*> Nodes(AllNodes);
1443 std::sort(Nodes.begin(), Nodes.end());
1444
1445 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001446 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1447 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001448 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001449
1450 DumpNodes(getRoot().Val, 2);
1451
Chris Lattnerc3aae252005-01-07 07:46:32 +00001452 std::cerr << "\n\n";
1453}
1454