blob: 48e0f7bc9ad435d0872c7f5fe604a91bb96230c9 [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"
Chris Lattner90b7c132005-01-23 04:39:44 +000019#include "llvm/Target/TargetLowering.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000020#include <iostream>
Chris Lattner9c667932005-01-07 21:09:16 +000021#include <set>
Chris Lattner061a1ea2005-01-07 07:46:32 +000022#include <cmath>
Jeff Cohen7d1670da2005-01-09 20:41:56 +000023#include <algorithm>
Chris Lattner560b5e42004-06-02 04:28:06 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattnerfde3a212005-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 Lattner061a1ea2005-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 Lattner90b7c132005-01-23 04:39:44 +0000145const TargetMachine &SelectionDAG::getTarget() const {
146 return TLI.getTargetMachine();
147}
148
149
Chris Lattner9c667932005-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 Lattner381dddc2005-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 Lattner9c667932005-01-07 21:09:16 +0000206 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000207 }
Chris Lattner9c667932005-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 Lattnera9d53f92005-01-18 19:26:36 +0000232 std::make_pair(
233 cast<SetCCSDNode>(N)->getCondition(),
234 N->getValueType(0))));
Chris Lattner9c667932005-01-07 21:09:16 +0000235 break;
Chris Lattner1001c6e2005-01-15 06:17:04 +0000236 case ISD::TRUNCSTORE:
237 case ISD::SIGN_EXTEND_INREG:
Chris Lattner1001c6e2005-01-15 06:17:04 +0000238 case ISD::FP_ROUND_INREG:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000239 case ISD::EXTLOAD:
240 case ISD::SEXTLOAD:
241 case ISD::ZEXTLOAD: {
242 EVTStruct NN;
Chris Lattner7d13eae2005-04-07 00:30:13 +0000243 NN.Opcode = N->getOpcode();
Chris Lattner3b8e7192005-01-14 22:38:01 +0000244 NN.VT = N->getValueType(0);
245 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner1001c6e2005-01-15 06:17:04 +0000246 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
247 NN.Ops.push_back(N->getOperand(i));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000248 MVTSDNodes.erase(NN);
249 break;
250 }
Chris Lattner9c667932005-01-07 21:09:16 +0000251 default:
252 if (N->getNumOperands() == 1)
253 UnaryOps.erase(std::make_pair(N->getOpcode(),
254 std::make_pair(N->getOperand(0),
255 N->getValueType(0))));
256 else if (N->getNumOperands() == 2)
257 BinaryOps.erase(std::make_pair(N->getOpcode(),
258 std::make_pair(N->getOperand(0),
259 N->getOperand(1))));
260 break;
261 }
262
263 // Next, brutally remove the operand list.
Chris Lattner9c667932005-01-07 21:09:16 +0000264 while (!N->Operands.empty()) {
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000265 SDNode *O = N->Operands.back().Val;
Chris Lattner9c667932005-01-07 21:09:16 +0000266 N->Operands.pop_back();
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000267 O->removeUser(N);
268
269 // Now that we removed this operand, see if there are no uses of it left.
270 DeleteNodeIfDead(O, NodeSet);
Chris Lattner9c667932005-01-07 21:09:16 +0000271 }
272
273 // Remove the node from the nodes set and delete it.
274 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
275 AllNodeSet.erase(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000276
277 // Now that the node is gone, check to see if any of the operands of this node
278 // are dead now.
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000279 delete N;
Chris Lattner9c667932005-01-07 21:09:16 +0000280}
281
282
Chris Lattner600d3082003-08-11 14:57:33 +0000283SelectionDAG::~SelectionDAG() {
284 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
285 delete AllNodes[i];
286}
287
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000288SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
289 int64_t Imm = ~0ULL >> 64-MVT::getSizeInBits(VT);
290 return getNode(ISD::AND, Op.getValueType(), Op,
291 getConstant(Imm, Op.getValueType()));
292}
293
Chris Lattner061a1ea2005-01-07 07:46:32 +0000294SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
295 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
296 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000297 if (VT != MVT::i64)
298 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
299
Chris Lattner061a1ea2005-01-07 07:46:32 +0000300 SDNode *&N = Constants[std::make_pair(Val, VT)];
301 if (N) return SDOperand(N, 0);
302 N = new ConstantSDNode(Val, VT);
303 AllNodes.push_back(N);
304 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000305}
306
Chris Lattner061a1ea2005-01-07 07:46:32 +0000307SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
308 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
309 if (VT == MVT::f32)
310 Val = (float)Val; // Mask out extra precision.
311
Chris Lattner381dddc2005-02-17 20:17:32 +0000312 // Do the map lookup using the actual bit pattern for the floating point
313 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
314 // we don't have issues with SNANs.
315 union {
316 double DV;
317 uint64_t IV;
318 };
319
320 DV = Val;
321
322 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000323 if (N) return SDOperand(N, 0);
324 N = new ConstantFPSDNode(Val, VT);
325 AllNodes.push_back(N);
326 return SDOperand(N, 0);
327}
328
329
330
331SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
332 MVT::ValueType VT) {
333 SDNode *&N = GlobalValues[GV];
334 if (N) return SDOperand(N, 0);
335 N = new GlobalAddressSDNode(GV,VT);
336 AllNodes.push_back(N);
337 return SDOperand(N, 0);
338}
339
340SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
341 SDNode *&N = FrameIndices[FI];
342 if (N) return SDOperand(N, 0);
343 N = new FrameIndexSDNode(FI, VT);
344 AllNodes.push_back(N);
345 return SDOperand(N, 0);
346}
347
348SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
349 SDNode *N = ConstantPoolIndices[CPIdx];
350 if (N) return SDOperand(N, 0);
351 N = new ConstantPoolSDNode(CPIdx, VT);
352 AllNodes.push_back(N);
353 return SDOperand(N, 0);
354}
355
356SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
357 SDNode *&N = BBNodes[MBB];
358 if (N) return SDOperand(N, 0);
359 N = new BasicBlockSDNode(MBB);
360 AllNodes.push_back(N);
361 return SDOperand(N, 0);
362}
363
364SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
365 SDNode *&N = ExternalSymbols[Sym];
366 if (N) return SDOperand(N, 0);
367 N = new ExternalSymbolSDNode(Sym, VT);
368 AllNodes.push_back(N);
369 return SDOperand(N, 0);
370}
371
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000372SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
373 SDOperand N1, SDOperand N2) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000374 // These setcc operations always fold.
375 switch (Cond) {
376 default: break;
377 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000378 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000379 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000380 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000381 }
382
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000383 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
384 uint64_t C2 = N2C->getValue();
385 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
386 uint64_t C1 = N1C->getValue();
Chris Lattner061a1ea2005-01-07 07:46:32 +0000387
388 // Sign extend the operands if required
389 if (ISD::isSignedIntSetCC(Cond)) {
390 C1 = N1C->getSignExtended();
391 C2 = N2C->getSignExtended();
392 }
393
394 switch (Cond) {
395 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000396 case ISD::SETEQ: return getConstant(C1 == C2, VT);
397 case ISD::SETNE: return getConstant(C1 != C2, VT);
398 case ISD::SETULT: return getConstant(C1 < C2, VT);
399 case ISD::SETUGT: return getConstant(C1 > C2, VT);
400 case ISD::SETULE: return getConstant(C1 <= C2, VT);
401 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
402 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
403 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
404 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
405 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000406 }
Chris Lattnerdfed7352005-04-07 18:58:54 +0000407 } else {
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000408 uint64_t MinVal, MaxVal;
409 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
410 if (ISD::isSignedIntSetCC(Cond)) {
411 MinVal = 1ULL << (OperandBitSize-1);
412 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
413 MaxVal = ~0ULL >> (65-OperandBitSize);
414 else
415 MaxVal = 0;
416 } else {
417 MinVal = 0;
418 MaxVal = ~0ULL >> (64-OperandBitSize);
419 }
420
421 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
422 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
423 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
424 --C2; // X >= C1 --> X > (C1-1)
425 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
426 N2 = getConstant(C2, N2.getValueType());
427 N2C = cast<ConstantSDNode>(N2.Val);
428 }
429
430 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
431 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
432 ++C2; // X <= C1 --> X < (C1+1)
433 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
434 N2 = getConstant(C2, N2.getValueType());
435 N2C = cast<ConstantSDNode>(N2.Val);
436 }
Chris Lattner87bd6982005-04-12 00:28:49 +0000437
438 // If we have setult X, 1, turn it into seteq X, 0
439 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
440 return getSetCC(ISD::SETEQ, VT, N1,
441 getConstant(MinVal, N1.getValueType()));
442 // If we have setult X, 1, turn it into seteq X, 0
443 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
444 return getSetCC(ISD::SETEQ, VT, N1,
445 getConstant(MaxVal, N1.getValueType()));
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000446
447 // If we have "setcc X, C1", check to see if we can shrink the immediate
448 // by changing cc.
449
450 // SETUGT X, SINTMAX -> SETLT X, 0
451 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
452 C2 == (~0ULL >> (65-OperandBitSize)))
453 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
454
455 // FIXME: Implement the rest of these.
456
Chris Lattner061a1ea2005-01-07 07:46:32 +0000457 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000458 } else if (isa<ConstantSDNode>(N1.Val)) {
459 // Ensure that the constant occurs on the RHS.
460 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
461 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000462
463 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
464 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
465 double C1 = N1C->getValue(), C2 = N2C->getValue();
466
467 switch (Cond) {
468 default: break; // FIXME: Implement the rest of these!
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000469 case ISD::SETEQ: return getConstant(C1 == C2, VT);
470 case ISD::SETNE: return getConstant(C1 != C2, VT);
471 case ISD::SETLT: return getConstant(C1 < C2, VT);
472 case ISD::SETGT: return getConstant(C1 > C2, VT);
473 case ISD::SETLE: return getConstant(C1 <= C2, VT);
474 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000475 }
476 } else {
477 // Ensure that the constant occurs on the RHS.
478 Cond = ISD::getSetCCSwappedOperands(Cond);
479 std::swap(N1, N2);
480 }
481
482 if (N1 == N2) {
483 // We can always fold X == Y for integer setcc's.
484 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000485 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000486 unsigned UOF = ISD::getUnorderedFlavor(Cond);
487 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000488 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000489 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000490 return getConstant(UOF, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000491 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
492 // if it is not already.
493 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
494 }
495
Chris Lattnerfde3a212005-01-09 20:52:51 +0000496 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner41b76412005-01-10 02:03:02 +0000497 MVT::isInteger(N1.getValueType())) {
498 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
499 N1.getOpcode() == ISD::XOR) {
500 // Simplify (X+Y) == (X+Z) --> Y == Z
501 if (N1.getOpcode() == N2.getOpcode()) {
502 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000503 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000504 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000505 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000506 if (isCommutativeBinOp(N1.getOpcode())) {
507 // If X op Y == Y op X, try other combinations.
508 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000509 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000510 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000511 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000512 }
513 }
Chris Lattner90b7c132005-01-23 04:39:44 +0000514
515 // FIXME: move this stuff to the DAG Combiner when it exists!
Chris Lattner41b76412005-01-10 02:03:02 +0000516
517 // Simplify (X+Z) == X --> Z == 0
518 if (N1.getOperand(0) == N2)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000519 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000520 getConstant(0, N1.getValueType()));
521 if (N1.getOperand(1) == N2) {
522 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000523 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000524 getConstant(0, N1.getValueType()));
525 else {
526 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
527 // (Z-X) == X --> Z == X<<1
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000528 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000529 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner90b7c132005-01-23 04:39:44 +0000530 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner41b76412005-01-10 02:03:02 +0000531 }
Chris Lattnerfde3a212005-01-09 20:52:51 +0000532 }
533 }
534
Chris Lattner41b76412005-01-10 02:03:02 +0000535 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
536 N2.getOpcode() == ISD::XOR) {
537 // Simplify X == (X+Z) --> Z == 0
538 if (N2.getOperand(0) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000539 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000540 getConstant(0, N2.getValueType()));
541 else if (N2.getOperand(1) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000542 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000543 getConstant(0, N2.getValueType()));
544 }
545 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000546
Chris Lattnera9d53f92005-01-18 19:26:36 +0000547 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
548 std::make_pair(Cond, VT))];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000549 if (N) return SDOperand(N, 0);
550 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000551 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000552 AllNodes.push_back(N);
553 return SDOperand(N, 0);
554}
555
556
557
558/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000559///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000560SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
561 SDNode *N = new SDNode(Opcode, VT);
562 AllNodes.push_back(N);
563 return SDOperand(N, 0);
564}
565
Chris Lattner061a1ea2005-01-07 07:46:32 +0000566SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
567 SDOperand Operand) {
568 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
569 uint64_t Val = C->getValue();
570 switch (Opcode) {
571 default: break;
572 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
573 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
574 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000575 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
576 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000577 }
578 }
579
580 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
581 switch (Opcode) {
Chris Lattner0ea81f92005-04-09 03:02:46 +0000582 case ISD::FNEG:
583 return getConstantFP(-C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000584 case ISD::FP_ROUND:
585 case ISD::FP_EXTEND:
586 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000587 case ISD::FP_TO_SINT:
588 return getConstant((int64_t)C->getValue(), VT);
589 case ISD::FP_TO_UINT:
590 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000591 }
592
593 unsigned OpOpcode = Operand.Val->getOpcode();
594 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000595 case ISD::TokenFactor:
596 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000597 case ISD::SIGN_EXTEND:
598 if (Operand.getValueType() == VT) return Operand; // noop extension
599 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
600 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
601 break;
602 case ISD::ZERO_EXTEND:
603 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattnerb32d9312005-04-07 19:43:53 +0000604 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000605 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000606 break;
607 case ISD::TRUNCATE:
608 if (Operand.getValueType() == VT) return Operand; // noop truncate
609 if (OpOpcode == ISD::TRUNCATE)
610 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000611 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
612 // If the source is smaller than the dest, we still need an extend.
613 if (Operand.Val->getOperand(0).getValueType() < VT)
614 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
615 else if (Operand.Val->getOperand(0).getValueType() > VT)
616 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
617 else
618 return Operand.Val->getOperand(0);
619 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000620 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +0000621 case ISD::FNEG:
622 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
623 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
624 Operand.Val->getOperand(0));
625 if (OpOpcode == ISD::FNEG) // --X -> X
626 return Operand.Val->getOperand(0);
627 break;
628 case ISD::FABS:
629 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
630 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
631 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000632 }
633
634 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
635 if (N) return SDOperand(N, 0);
636 N = new SDNode(Opcode, Operand);
637 N->setValueTypes(VT);
638 AllNodes.push_back(N);
639 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000640}
641
Chris Lattner061a1ea2005-01-07 07:46:32 +0000642SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
643 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000644#ifndef NDEBUG
645 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000646 case ISD::TokenFactor:
647 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
648 N2.getValueType() == MVT::Other && "Invalid token factor!");
649 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000650 case ISD::AND:
651 case ISD::OR:
652 case ISD::XOR:
653 case ISD::UDIV:
654 case ISD::UREM:
655 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
656 // fall through
657 case ISD::ADD:
658 case ISD::SUB:
659 case ISD::MUL:
660 case ISD::SDIV:
661 case ISD::SREM:
662 assert(N1.getValueType() == N2.getValueType() &&
663 N1.getValueType() == VT && "Binary operator types must match!");
664 break;
665
666 case ISD::SHL:
667 case ISD::SRA:
668 case ISD::SRL:
669 assert(VT == N1.getValueType() &&
670 "Shift operators return type must be the same as their first arg");
671 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000672 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000673 break;
674 default: break;
675 }
676#endif
677
Chris Lattner061a1ea2005-01-07 07:46:32 +0000678 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
679 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
680 if (N1C) {
681 if (N2C) {
682 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
683 switch (Opcode) {
684 case ISD::ADD: return getConstant(C1 + C2, VT);
685 case ISD::SUB: return getConstant(C1 - C2, VT);
686 case ISD::MUL: return getConstant(C1 * C2, VT);
687 case ISD::UDIV:
688 if (C2) return getConstant(C1 / C2, VT);
689 break;
690 case ISD::UREM :
691 if (C2) return getConstant(C1 % C2, VT);
692 break;
693 case ISD::SDIV :
694 if (C2) return getConstant(N1C->getSignExtended() /
695 N2C->getSignExtended(), VT);
696 break;
697 case ISD::SREM :
698 if (C2) return getConstant(N1C->getSignExtended() %
699 N2C->getSignExtended(), VT);
700 break;
701 case ISD::AND : return getConstant(C1 & C2, VT);
702 case ISD::OR : return getConstant(C1 | C2, VT);
703 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +0000704 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
705 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
706 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000707 default: break;
708 }
709
710 } else { // Cannonicalize constant to RHS if commutative
711 if (isCommutativeBinOp(Opcode)) {
712 std::swap(N1C, N2C);
713 std::swap(N1, N2);
714 }
715 }
Chris Lattner32a5f022005-01-19 17:29:49 +0000716
717 switch (Opcode) {
718 default: break;
719 case ISD::SHL: // shl 0, X -> 0
720 if (N1C->isNullValue()) return N1;
721 break;
722 case ISD::SRL: // srl 0, X -> 0
723 if (N1C->isNullValue()) return N1;
724 break;
725 case ISD::SRA: // sra -1, X -> -1
726 if (N1C->isAllOnesValue()) return N1;
727 break;
728 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000729 }
730
731 if (N2C) {
732 uint64_t C2 = N2C->getValue();
733
734 switch (Opcode) {
735 case ISD::ADD:
736 if (!C2) return N1; // add X, 0 -> X
737 break;
738 case ISD::SUB:
739 if (!C2) return N1; // sub X, 0 -> X
740 break;
741 case ISD::MUL:
742 if (!C2) return N2; // mul X, 0 -> 0
743 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
744 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
745
Chris Lattner90b7c132005-01-23 04:39:44 +0000746 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000747 if ((C2 & C2-1) == 0) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000748 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
749 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000750 }
751 break;
752
753 case ISD::UDIV:
Chris Lattner90b7c132005-01-23 04:39:44 +0000754 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000755 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000756 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
757 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000758 }
759 break;
760
Chris Lattnera86fa442005-01-11 04:25:13 +0000761 case ISD::SHL:
762 case ISD::SRL:
763 case ISD::SRA:
Nate Begemanaf1c0f72005-04-12 23:12:17 +0000764 // If the shift amount is bigger than the size of the data, then all the
765 // bits are shifted out. Simplify to loading constant zero.
766 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
767 return getNode(ISD::UNDEF, N1.getValueType());
768 }
Chris Lattnera86fa442005-01-11 04:25:13 +0000769 if (C2 == 0) return N1;
770 break;
771
Chris Lattner061a1ea2005-01-07 07:46:32 +0000772 case ISD::AND:
773 if (!C2) return N2; // X and 0 -> 0
774 if (N2C->isAllOnesValue())
775 return N1; // X and -1 -> X
Chris Lattnerda504742005-04-09 21:43:54 +0000776
Chris Lattnerd8cbfe82005-04-10 01:13:15 +0000777 // FIXME: Should add a corresponding version of this for
778 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
779 // we don't have yet.
780
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000781 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
782 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnerda504742005-04-09 21:43:54 +0000783 // If we are masking out the part of our input that was extended, just
784 // mask the input to the extension directly.
785 unsigned ExtendBits =
786 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
787 if ((C2 & (~0ULL << ExtendBits)) == 0)
788 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
789 }
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000790 if (N1.getOpcode() == ISD::AND)
791 if (ConstantSDNode *OpRHS = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
792 return getNode(ISD::AND, VT, N1.getOperand(0),
793 getNode(ISD::AND, VT, N1.getOperand(1), N2));
794
795 // If we are anding the result of a setcc, and we know setcc always
796 // returns 0 or 1, simplify the RHS to either be 0 or 1
797 if (N1.getOpcode() == ISD::SETCC &&
798 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
799 if (C2 & 1)
800 return getNode(ISD::AND, VT, N1.getOperand(1), getConstant(1, VT));
801 else
802 return getConstant(0, VT);
803
804 if (N1.getOpcode() == ISD::ZEXTLOAD) {
805 // If we are anding the result of a zext load, realize that the top bits
806 // of the loaded value are already zero to simplify C2.
807 unsigned SrcBits =
808 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
809 uint64_t C3 = C2 & (~0ULL >> (64-SrcBits));
810 if (C3 != C2)
811 return getNode(ISD::AND, VT, N1, getConstant(C3, VT));
812 else if (C2 == (~0ULL >> (64-SrcBits)))
813 return N1; // Anding out just what is already masked.
814 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000815 break;
816 case ISD::OR:
817 if (!C2)return N1; // X or 0 -> X
818 if (N2C->isAllOnesValue())
819 return N2; // X or -1 -> -1
820 break;
821 case ISD::XOR:
822 if (!C2) return N1; // X xor 0 -> X
823 if (N2C->isAllOnesValue()) {
824 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
825 // !(X op Y) -> (X !op Y)
826 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
827 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000828 SetCC->getValueType(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000829 SetCC->getOperand(0), SetCC->getOperand(1));
830 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
831 SDNode *Op = N1.Val;
832 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
833 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
834 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
835 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
836 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
837 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
838 if (Op->getOpcode() == ISD::AND)
839 return getNode(ISD::OR, VT, LHS, RHS);
840 return getNode(ISD::AND, VT, LHS, RHS);
841 }
842 }
843 // X xor -1 -> not(x) ?
844 }
845 break;
846 }
847
848 // Reassociate ((X op C1) op C2) if possible.
849 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
850 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +0000851 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000852 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
853 }
854
855 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
856 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
857 if (N1CFP)
858 if (N2CFP) {
859 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
860 switch (Opcode) {
861 case ISD::ADD: return getConstantFP(C1 + C2, VT);
862 case ISD::SUB: return getConstantFP(C1 - C2, VT);
863 case ISD::MUL: return getConstantFP(C1 * C2, VT);
864 case ISD::SDIV:
865 if (C2) return getConstantFP(C1 / C2, VT);
866 break;
867 case ISD::SREM :
868 if (C2) return getConstantFP(fmod(C1, C2), VT);
869 break;
870 default: break;
871 }
872
873 } else { // Cannonicalize constant to RHS if commutative
874 if (isCommutativeBinOp(Opcode)) {
875 std::swap(N1CFP, N2CFP);
876 std::swap(N1, N2);
877 }
878 }
879
880 // Finally, fold operations that do not require constants.
881 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000882 case ISD::TokenFactor:
883 if (N1.getOpcode() == ISD::EntryToken)
884 return N2;
885 if (N2.getOpcode() == ISD::EntryToken)
886 return N1;
887 break;
888
Chris Lattner061a1ea2005-01-07 07:46:32 +0000889 case ISD::AND:
890 case ISD::OR:
891 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
892 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
893 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
894 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
895 ISD::CondCode Op2 = RHS->getCondition();
896
897 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
898 if (LL == RR && LR == RL) {
899 Op2 = ISD::getSetCCSwappedOperands(Op2);
900 goto MatchedBackwards;
901 }
902
903 if (LL == RL && LR == RR) {
904 MatchedBackwards:
905 ISD::CondCode Result;
906 bool isInteger = MVT::isInteger(LL.getValueType());
907 if (Opcode == ISD::OR)
908 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
909 isInteger);
910 else
911 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
912 isInteger);
913 if (Result != ISD::SETCC_INVALID)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000914 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000915 }
916 }
917 break;
918 case ISD::XOR:
919 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
920 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +0000921 case ISD::ADD:
922 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
923 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
924 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
925 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattnerf2bff922005-04-10 04:04:49 +0000926 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
927 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
928 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
929 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
930 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
931 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Chris Lattner0ea81f92005-04-09 03:02:46 +0000932 break;
Chris Lattner3d5d5022005-01-09 20:09:57 +0000933 case ISD::SUB:
934 if (N1.getOpcode() == ISD::ADD) {
935 if (N1.Val->getOperand(0) == N2)
936 return N1.Val->getOperand(1); // (A+B)-A == B
937 if (N1.Val->getOperand(1) == N2)
938 return N1.Val->getOperand(0); // (A+B)-B == A
939 }
Chris Lattner0ea81f92005-04-09 03:02:46 +0000940 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
941 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattner3d5d5022005-01-09 20:09:57 +0000942 break;
Nate Begemanca916ba2005-04-12 23:32:28 +0000943 case ISD::SHL:
944 case ISD::SRL:
945 case ISD::SRA:
Chris Lattnerb1f25ac2005-04-13 02:58:13 +0000946 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
947 cast<MVTSDNode>(N2)->getExtraValueType() != MVT::i1)
Nate Begemanca916ba2005-04-12 23:32:28 +0000948 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnerb1f25ac2005-04-13 02:58:13 +0000949 else if (N2.getOpcode() == ISD::AND)
950 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
951 // If the and is only masking out bits that cannot effect the shift,
952 // eliminate the and.
953 unsigned NumBits = MVT::getSizeInBits(VT);
954 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
955 return getNode(Opcode, VT, N1, N2.getOperand(0));
956 }
957
Nate Begemanca916ba2005-04-12 23:32:28 +0000958 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000959 }
960
961 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
962 if (N) return SDOperand(N, 0);
963 N = new SDNode(Opcode, N1, N2);
964 N->setValueTypes(VT);
965
966 AllNodes.push_back(N);
967 return SDOperand(N, 0);
968}
969
970SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
971 SDOperand Chain, SDOperand Ptr) {
972 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
973 if (N) return SDOperand(N, 0);
974 N = new SDNode(ISD::LOAD, Chain, Ptr);
975
976 // Loads have a token chain.
977 N->setValueTypes(VT, MVT::Other);
978 AllNodes.push_back(N);
979 return SDOperand(N, 0);
980}
981
982
983SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
984 SDOperand N1, SDOperand N2, SDOperand N3) {
985 // Perform various simplifications.
986 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
987 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
988 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
989 switch (Opcode) {
990 case ISD::SELECT:
991 if (N1C)
992 if (N1C->getValue())
993 return N2; // select true, X, Y -> X
994 else
995 return N3; // select false, X, Y -> Y
996
997 if (N2 == N3) return N2; // select C, X, X -> X
998
999 if (VT == MVT::i1) { // Boolean SELECT
1000 if (N2C) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001001 if (N2C->getValue()) // select C, 1, X -> C | X
1002 return getNode(ISD::OR, VT, N1, N3);
1003 else // select C, 0, X -> ~C & X
1004 return getNode(ISD::AND, VT,
1005 getNode(ISD::XOR, N1.getValueType(), N1,
1006 getConstant(1, N1.getValueType())), N3);
1007 } else if (N3C) {
1008 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1009 return getNode(ISD::OR, VT,
1010 getNode(ISD::XOR, N1.getValueType(), N1,
1011 getConstant(1, N1.getValueType())), N2);
1012 else // select C, X, 0 -> C & X
1013 return getNode(ISD::AND, VT, N1, N2);
1014 }
Chris Lattneraf5b25f2005-04-12 02:54:39 +00001015
1016 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1017 return getNode(ISD::OR, VT, N1, N3);
1018 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1019 return getNode(ISD::AND, VT, N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001020 }
1021
Chris Lattner6a31b872005-04-09 05:15:53 +00001022 // If this is a selectcc, check to see if we can simplify the result.
1023 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1024 if (ConstantFPSDNode *CFP =
1025 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1026 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1027 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1028 if ((SetCC->getCondition() == ISD::SETGE ||
1029 SetCC->getCondition() == ISD::SETGT) &&
1030 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1031 N3.getOperand(0) == N2)
1032 return getNode(ISD::FABS, VT, N2);
1033
1034 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1035 if ((SetCC->getCondition() == ISD::SETLT ||
1036 SetCC->getCondition() == ISD::SETLE) &&
1037 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1038 N2.getOperand(0) == N3)
1039 return getNode(ISD::FABS, VT, N3);
1040 }
Chris Lattner6a31b872005-04-09 05:15:53 +00001041 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001042 break;
Chris Lattner5c66e452005-01-07 22:49:57 +00001043 case ISD::BRCOND:
1044 if (N2C)
1045 if (N2C->getValue()) // Unconditional branch
1046 return getNode(ISD::BR, MVT::Other, N1, N3);
1047 else
1048 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +00001049 break;
Nate Begemanca916ba2005-04-12 23:32:28 +00001050 case ISD::SRA_PARTS:
1051 case ISD::SRL_PARTS:
1052 case ISD::SHL_PARTS:
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001053 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1054 cast<MVTSDNode>(N3)->getExtraValueType() != MVT::i1)
Nate Begemanca916ba2005-04-12 23:32:28 +00001055 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001056 else if (N3.getOpcode() == ISD::AND)
1057 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1058 // If the and is only masking out bits that cannot effect the shift,
1059 // eliminate the and.
1060 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1061 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1062 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1063 }
1064
1065
Nate Begemanca916ba2005-04-12 23:32:28 +00001066 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001067 }
1068
1069 SDNode *N = new SDNode(Opcode, N1, N2, N3);
1070 switch (Opcode) {
1071 default:
1072 N->setValueTypes(VT);
1073 break;
1074 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
1075 N->setValueTypes(VT, MVT::Other);
1076 break;
Chris Lattner4157c412005-04-02 04:00:59 +00001077
1078 case ISD::SRA_PARTS:
1079 case ISD::SRL_PARTS:
1080 case ISD::SHL_PARTS: {
1081 std::vector<MVT::ValueType> V(N->getNumOperands()-1, VT);
1082 N->setValueTypes(V);
1083 break;
1084 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001085 }
1086
1087 // FIXME: memoize NODES
1088 AllNodes.push_back(N);
1089 return SDOperand(N, 0);
1090}
1091
1092SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1093 std::vector<SDOperand> &Children) {
1094 switch (Children.size()) {
1095 case 0: return getNode(Opcode, VT);
1096 case 1: return getNode(Opcode, VT, Children[0]);
1097 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
1098 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001099 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001100 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001101
1102 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Children[1].Val);
1103 switch (Opcode) {
1104 default: break;
1105 case ISD::BRCONDTWOWAY:
1106 if (N1C)
1107 if (N1C->getValue()) // Unconditional branch to true dest.
1108 return getNode(ISD::BR, MVT::Other, Children[0], Children[2]);
1109 else // Unconditional branch to false dest.
1110 return getNode(ISD::BR, MVT::Other, Children[0], Children[3]);
1111 break;
1112 }
1113
1114 // FIXME: MEMOIZE!!
1115 SDNode *N = new SDNode(Opcode, Children);
1116 if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
1117 N->setValueTypes(VT);
1118 } else {
1119 std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
1120 N->setValueTypes(V);
1121 }
1122 AllNodes.push_back(N);
1123 return SDOperand(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001124}
1125
Chris Lattner39c67442005-01-14 22:08:15 +00001126SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner1001c6e2005-01-15 06:17:04 +00001127 MVT::ValueType EVT) {
1128
1129 switch (Opcode) {
1130 default: assert(0 && "Bad opcode for this accessor!");
1131 case ISD::FP_ROUND_INREG:
1132 assert(VT == N1.getValueType() && "Not an inreg round!");
1133 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1134 "Cannot FP_ROUND_INREG integer types");
1135 if (EVT == VT) return N1; // Not actually rounding
1136 assert(EVT < VT && "Not rounding down!");
Chris Lattner7f269462005-03-09 18:37:12 +00001137
1138 if (isa<ConstantFPSDNode>(N1))
1139 return getNode(ISD::FP_EXTEND, VT, getNode(ISD::FP_ROUND, EVT, N1));
Chris Lattner1001c6e2005-01-15 06:17:04 +00001140 break;
Chris Lattner1001c6e2005-01-15 06:17:04 +00001141 case ISD::SIGN_EXTEND_INREG:
1142 assert(VT == N1.getValueType() && "Not an inreg extend!");
1143 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1144 "Cannot *_EXTEND_INREG FP types");
1145 if (EVT == VT) return N1; // Not actually extending
1146 assert(EVT < VT && "Not extending!");
Chris Lattner0fe77762005-01-16 00:17:20 +00001147
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001148 // Extending a constant? Just return the extended constant.
Chris Lattner7f269462005-03-09 18:37:12 +00001149 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1150 SDOperand Tmp = getNode(ISD::TRUNCATE, EVT, N1);
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001151 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner7f269462005-03-09 18:37:12 +00001152 }
1153
Chris Lattner0fe77762005-01-16 00:17:20 +00001154 // If we are sign extending an extension, use the original source.
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001155 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1156 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
Chris Lattner0fe77762005-01-16 00:17:20 +00001157 return N1;
Chris Lattner0fe77762005-01-16 00:17:20 +00001158
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001159 // If we are sign extending a sextload, return just the load.
1160 if (N1.getOpcode() == ISD::SEXTLOAD && Opcode == ISD::SIGN_EXTEND_INREG)
Chris Lattnerf74c7942005-04-10 04:33:08 +00001161 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1162 return N1;
1163
Chris Lattnerb32d9312005-04-07 19:43:53 +00001164 // If we are extending the result of a setcc, and we already know the
1165 // contents of the top bits, eliminate the extension.
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001166 if (N1.getOpcode() == ISD::SETCC &&
1167 TLI.getSetCCResultContents() ==
1168 TargetLowering::ZeroOrNegativeOneSetCCResult)
1169 return N1;
Chris Lattnere2427c92005-04-10 23:37:16 +00001170
1171 // If we are sign extending the result of an (and X, C) operation, and we
1172 // know the extended bits are zeros already, don't do the extend.
1173 if (N1.getOpcode() == ISD::AND)
1174 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1175 uint64_t Mask = N1C->getValue();
1176 unsigned NumBits = MVT::getSizeInBits(EVT);
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001177 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1178 return N1;
Chris Lattnere2427c92005-04-10 23:37:16 +00001179 }
Chris Lattner1001c6e2005-01-15 06:17:04 +00001180 break;
1181 }
1182
1183 EVTStruct NN;
1184 NN.Opcode = Opcode;
1185 NN.VT = VT;
1186 NN.EVT = EVT;
1187 NN.Ops.push_back(N1);
1188
1189 SDNode *&N = MVTSDNodes[NN];
1190 if (N) return SDOperand(N, 0);
1191 N = new MVTSDNode(Opcode, VT, N1, EVT);
1192 AllNodes.push_back(N);
1193 return SDOperand(N, 0);
1194}
1195
1196SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner39c67442005-01-14 22:08:15 +00001197 SDOperand N2, MVT::ValueType EVT) {
1198 switch (Opcode) {
1199 default: assert(0 && "Bad opcode for this accessor!");
1200 case ISD::EXTLOAD:
1201 case ISD::SEXTLOAD:
1202 case ISD::ZEXTLOAD:
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001203 // If they are asking for an extending load from/to the same thing, return a
Chris Lattner39c67442005-01-14 22:08:15 +00001204 // normal load.
1205 if (VT == EVT)
1206 return getNode(ISD::LOAD, VT, N1, N2);
1207 assert(EVT < VT && "Should only be an extending load, not truncating!");
1208 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1209 "Cannot sign/zero extend a FP load!");
1210 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1211 "Cannot convert from FP to Int or Int -> FP!");
1212 break;
1213 }
1214
1215 EVTStruct NN;
1216 NN.Opcode = Opcode;
1217 NN.VT = VT;
1218 NN.EVT = EVT;
1219 NN.Ops.push_back(N1);
1220 NN.Ops.push_back(N2);
1221
1222 SDNode *&N = MVTSDNodes[NN];
1223 if (N) return SDOperand(N, 0);
Chris Lattner3b8e7192005-01-14 22:38:01 +00001224 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner39c67442005-01-14 22:08:15 +00001225 AllNodes.push_back(N);
1226 return SDOperand(N, 0);
1227}
1228
1229SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
1230 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
1231 switch (Opcode) {
1232 default: assert(0 && "Bad opcode for this accessor!");
1233 case ISD::TRUNCSTORE:
Chris Lattner1001c6e2005-01-15 06:17:04 +00001234#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1235 // If this is a truncating store of a constant, convert to the desired type
1236 // and store it instead.
1237 if (isa<Constant>(N1)) {
1238 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1239 if (isa<Constant>(Op))
1240 N1 = Op;
1241 }
1242 // Also for ConstantFP?
1243#endif
Chris Lattner39c67442005-01-14 22:08:15 +00001244 if (N1.getValueType() == EVT) // Normal store?
1245 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner1001c6e2005-01-15 06:17:04 +00001246 assert(N2.getValueType() > EVT && "Not a truncation?");
1247 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner39c67442005-01-14 22:08:15 +00001248 "Can't do FP-INT conversion!");
1249 break;
1250 }
1251
1252 EVTStruct NN;
1253 NN.Opcode = Opcode;
1254 NN.VT = VT;
1255 NN.EVT = EVT;
1256 NN.Ops.push_back(N1);
1257 NN.Ops.push_back(N2);
1258 NN.Ops.push_back(N3);
1259
1260 SDNode *&N = MVTSDNodes[NN];
1261 if (N) return SDOperand(N, 0);
1262 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1263 AllNodes.push_back(N);
1264 return SDOperand(N, 0);
1265}
1266
1267
Chris Lattner40e79822005-01-12 18:37:47 +00001268/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1269/// indicated value. This method ignores uses of other values defined by this
1270/// operation.
1271bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1272 assert(Value < getNumValues() && "Bad value!");
1273
1274 // If there is only one value, this is easy.
1275 if (getNumValues() == 1)
1276 return use_size() == NUses;
1277 if (Uses.size() < NUses) return false;
1278
1279 SDOperand TheValue(this, Value);
1280
1281 std::set<SDNode*> UsersHandled;
1282
1283 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1284 UI != E; ++UI) {
1285 SDNode *User = *UI;
1286 if (User->getNumOperands() == 1 ||
1287 UsersHandled.insert(User).second) // First time we've seen this?
1288 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1289 if (User->getOperand(i) == TheValue) {
1290 if (NUses == 0)
1291 return false; // too many uses
1292 --NUses;
1293 }
1294 }
1295
1296 // Found exactly the right number of uses?
1297 return NUses == 0;
1298}
1299
1300
Chris Lattner9e4c7612005-01-10 23:25:25 +00001301const char *SDNode::getOperationName() const {
1302 switch (getOpcode()) {
1303 default: return "<<Unknown>>";
Andrew Lenharthdec53922005-03-31 21:24:06 +00001304 case ISD::PCMARKER: return "PCMarker";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001305 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001306 case ISD::TokenFactor: return "TokenFactor";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001307 case ISD::Constant: return "Constant";
1308 case ISD::ConstantFP: return "ConstantFP";
1309 case ISD::GlobalAddress: return "GlobalAddress";
1310 case ISD::FrameIndex: return "FrameIndex";
1311 case ISD::BasicBlock: return "BasicBlock";
1312 case ISD::ExternalSymbol: return "ExternalSymbol";
1313 case ISD::ConstantPool: return "ConstantPoolIndex";
1314 case ISD::CopyToReg: return "CopyToReg";
1315 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattnere727af02005-01-13 20:50:02 +00001316 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemancda9aa72005-04-01 22:34:39 +00001317 case ISD::UNDEF: return "undef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001318
Chris Lattnerc4a20462005-04-02 04:58:41 +00001319 // Unary operators
1320 case ISD::FABS: return "fabs";
1321 case ISD::FNEG: return "fneg";
1322
1323 // Binary operators
Chris Lattner9e4c7612005-01-10 23:25:25 +00001324 case ISD::ADD: return "add";
1325 case ISD::SUB: return "sub";
1326 case ISD::MUL: return "mul";
Nate Begeman55e86252005-04-05 22:36:56 +00001327 case ISD::MULHU: return "mulhu";
1328 case ISD::MULHS: return "mulhs";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001329 case ISD::SDIV: return "sdiv";
1330 case ISD::UDIV: return "udiv";
1331 case ISD::SREM: return "srem";
1332 case ISD::UREM: return "urem";
1333 case ISD::AND: return "and";
1334 case ISD::OR: return "or";
1335 case ISD::XOR: return "xor";
1336 case ISD::SHL: return "shl";
1337 case ISD::SRA: return "sra";
1338 case ISD::SRL: return "srl";
1339
1340 case ISD::SELECT: return "select";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001341 case ISD::ADD_PARTS: return "add_parts";
1342 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner5b7bb562005-04-02 03:30:42 +00001343 case ISD::SHL_PARTS: return "shl_parts";
1344 case ISD::SRA_PARTS: return "sra_parts";
1345 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001346
1347 // Conversion operators.
1348 case ISD::SIGN_EXTEND: return "sign_extend";
1349 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001350 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001351 case ISD::TRUNCATE: return "truncate";
1352 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001353 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001354 case ISD::FP_EXTEND: return "fp_extend";
1355
1356 case ISD::SINT_TO_FP: return "sint_to_fp";
1357 case ISD::UINT_TO_FP: return "uint_to_fp";
1358 case ISD::FP_TO_SINT: return "fp_to_sint";
1359 case ISD::FP_TO_UINT: return "fp_to_uint";
1360
1361 // Control flow instructions
1362 case ISD::BR: return "br";
1363 case ISD::BRCOND: return "brcond";
Chris Lattnerb0713c72005-04-09 03:27:28 +00001364 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001365 case ISD::RET: return "ret";
1366 case ISD::CALL: return "call";
1367 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1368 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1369
1370 // Other operators
1371 case ISD::LOAD: return "load";
1372 case ISD::STORE: return "store";
Chris Lattner39c67442005-01-14 22:08:15 +00001373 case ISD::EXTLOAD: return "extload";
1374 case ISD::SEXTLOAD: return "sextload";
1375 case ISD::ZEXTLOAD: return "zextload";
1376 case ISD::TRUNCSTORE: return "truncstore";
1377
Chris Lattner9e4c7612005-01-10 23:25:25 +00001378 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1379 case ISD::EXTRACT_ELEMENT: return "extract_element";
1380 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001381 case ISD::MEMSET: return "memset";
1382 case ISD::MEMCPY: return "memcpy";
1383 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001384
1385 case ISD::SETCC:
1386 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1387 switch (SetCC->getCondition()) {
1388 default: assert(0 && "Unknown setcc condition!");
1389 case ISD::SETOEQ: return "setcc:setoeq";
1390 case ISD::SETOGT: return "setcc:setogt";
1391 case ISD::SETOGE: return "setcc:setoge";
1392 case ISD::SETOLT: return "setcc:setolt";
1393 case ISD::SETOLE: return "setcc:setole";
1394 case ISD::SETONE: return "setcc:setone";
1395
1396 case ISD::SETO: return "setcc:seto";
1397 case ISD::SETUO: return "setcc:setuo";
1398 case ISD::SETUEQ: return "setcc:setue";
1399 case ISD::SETUGT: return "setcc:setugt";
1400 case ISD::SETUGE: return "setcc:setuge";
1401 case ISD::SETULT: return "setcc:setult";
1402 case ISD::SETULE: return "setcc:setule";
1403 case ISD::SETUNE: return "setcc:setune";
1404
1405 case ISD::SETEQ: return "setcc:seteq";
1406 case ISD::SETGT: return "setcc:setgt";
1407 case ISD::SETGE: return "setcc:setge";
1408 case ISD::SETLT: return "setcc:setlt";
1409 case ISD::SETLE: return "setcc:setle";
1410 case ISD::SETNE: return "setcc:setne";
1411 }
1412 }
1413}
Chris Lattner061a1ea2005-01-07 07:46:32 +00001414
1415void SDNode::dump() const {
1416 std::cerr << (void*)this << ": ";
1417
1418 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1419 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00001420 if (getValueType(i) == MVT::Other)
1421 std::cerr << "ch";
1422 else
1423 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001424 }
Chris Lattner9e4c7612005-01-10 23:25:25 +00001425 std::cerr << " = " << getOperationName();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001426
1427 std::cerr << " ";
1428 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1429 if (i) std::cerr << ", ";
1430 std::cerr << (void*)getOperand(i).Val;
1431 if (unsigned RN = getOperand(i).ResNo)
1432 std::cerr << ":" << RN;
1433 }
1434
1435 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1436 std::cerr << "<" << CSDN->getValue() << ">";
1437 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1438 std::cerr << "<" << CSDN->getValue() << ">";
1439 } else if (const GlobalAddressSDNode *GADN =
1440 dyn_cast<GlobalAddressSDNode>(this)) {
1441 std::cerr << "<";
1442 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1443 } else if (const FrameIndexSDNode *FIDN =
1444 dyn_cast<FrameIndexSDNode>(this)) {
1445 std::cerr << "<" << FIDN->getIndex() << ">";
1446 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1447 std::cerr << "<" << CP->getIndex() << ">";
1448 } else if (const BasicBlockSDNode *BBDN =
1449 dyn_cast<BasicBlockSDNode>(this)) {
1450 std::cerr << "<";
1451 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1452 if (LBB)
1453 std::cerr << LBB->getName() << " ";
1454 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnere727af02005-01-13 20:50:02 +00001455 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001456 std::cerr << "<reg #" << C2V->getReg() << ">";
1457 } else if (const ExternalSymbolSDNode *ES =
1458 dyn_cast<ExternalSymbolSDNode>(this)) {
1459 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner630d1932005-01-15 21:11:37 +00001460 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1461 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001462 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001463}
1464
Chris Lattnere6f78822005-01-09 20:38:33 +00001465static void DumpNodes(SDNode *N, unsigned indent) {
1466 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1467 if (N->getOperand(i).Val->hasOneUse())
1468 DumpNodes(N->getOperand(i).Val, indent+2);
1469 else
1470 std::cerr << "\n" << std::string(indent+2, ' ')
1471 << (void*)N->getOperand(i).Val << ": <multiple use>";
1472
1473
1474 std::cerr << "\n" << std::string(indent, ' ');
1475 N->dump();
1476}
1477
Chris Lattner061a1ea2005-01-07 07:46:32 +00001478void SelectionDAG::dump() const {
1479 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner1270acc2005-01-09 20:26:36 +00001480 std::vector<SDNode*> Nodes(AllNodes);
1481 std::sort(Nodes.begin(), Nodes.end());
1482
1483 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001484 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1485 DumpNodes(Nodes[i], 2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001486 }
Chris Lattnere6f78822005-01-09 20:38:33 +00001487
1488 DumpNodes(getRoot().Val, 2);
1489
Chris Lattner061a1ea2005-01-07 07:46:32 +00001490 std::cerr << "\n\n";
1491}
1492