blob: 7faf19fd72fe86444b015da99278b468a4b109e8 [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:
238 case ISD::ZERO_EXTEND_INREG:
239 case ISD::FP_ROUND_INREG:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000240 case ISD::EXTLOAD:
241 case ISD::SEXTLOAD:
242 case ISD::ZEXTLOAD: {
243 EVTStruct NN;
Chris Lattner7d13eae2005-04-07 00:30:13 +0000244 NN.Opcode = N->getOpcode();
Chris Lattner3b8e7192005-01-14 22:38:01 +0000245 NN.VT = N->getValueType(0);
246 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner1001c6e2005-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 Lattner3b8e7192005-01-14 22:38:01 +0000249 MVTSDNodes.erase(NN);
250 break;
251 }
Chris Lattner9c667932005-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 Lattner9c667932005-01-07 21:09:16 +0000265 while (!N->Operands.empty()) {
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000266 SDNode *O = N->Operands.back().Val;
Chris Lattner9c667932005-01-07 21:09:16 +0000267 N->Operands.pop_back();
Chris Lattnere0f1fe12005-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 Lattner9c667932005-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 Lattner9c667932005-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 Lattnere0f1fe12005-01-07 23:32:00 +0000280 delete N;
Chris Lattner9c667932005-01-07 21:09:16 +0000281}
282
283
Chris Lattner600d3082003-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 Lattner061a1ea2005-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 Lattner9a97e4d2005-01-08 06:24:30 +0000292 if (VT != MVT::i64)
293 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
294
Chris Lattner061a1ea2005-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 Lattner600d3082003-08-11 14:57:33 +0000300}
301
Chris Lattner061a1ea2005-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 Lattner381dddc2005-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 Lattner061a1ea2005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000367SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
368 SDOperand N1, SDOperand N2) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000369 // These setcc operations always fold.
370 switch (Cond) {
371 default: break;
372 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000373 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000374 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000375 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000376 }
377
Chris Lattner6b03a0c2005-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 Lattner061a1ea2005-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 Lattnerb07e2d22005-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 Lattner061a1ea2005-01-07 07:46:32 +0000401 }
Chris Lattnerdfed7352005-04-07 18:58:54 +0000402 } else {
Chris Lattner6b03a0c2005-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 Lattner061a1ea2005-01-07 07:46:32 +0000443 }
Chris Lattner6b03a0c2005-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 Lattner061a1ea2005-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 Lattnerb07e2d22005-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 Lattner061a1ea2005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000471 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000472 unsigned UOF = ISD::getUnorderedFlavor(Cond);
473 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000474 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000475 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000476 return getConstant(UOF, VT);
Chris Lattner061a1ea2005-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 Lattnerfde3a212005-01-09 20:52:51 +0000482 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner41b76412005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000489 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000490 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000491 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner41b76412005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000495 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000496 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000497 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000498 }
499 }
Chris Lattner90b7c132005-01-23 04:39:44 +0000500
501 // FIXME: move this stuff to the DAG Combiner when it exists!
Chris Lattner41b76412005-01-10 02:03:02 +0000502
503 // Simplify (X+Z) == X --> Z == 0
504 if (N1.getOperand(0) == N2)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000505 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000506 getConstant(0, N1.getValueType()));
507 if (N1.getOperand(1) == N2) {
508 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000509 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000514 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000515 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner90b7c132005-01-23 04:39:44 +0000516 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner41b76412005-01-10 02:03:02 +0000517 }
Chris Lattnerfde3a212005-01-09 20:52:51 +0000518 }
519 }
520
Chris Lattner41b76412005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000525 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000526 getConstant(0, N2.getValueType()));
527 else if (N2.getOperand(1) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000528 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000529 getConstant(0, N2.getValueType()));
530 }
531 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000532
Chris Lattnera9d53f92005-01-18 19:26:36 +0000533 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
534 std::make_pair(Cond, VT))];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000535 if (N) return SDOperand(N, 0);
536 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000537 N->setValueTypes(VT);
Chris Lattner061a1ea2005-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 Lattner600d3082003-08-11 14:57:33 +0000545///
Chris Lattner061a1ea2005-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 Lattner061a1ea2005-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 Lattner2a6db3c2005-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 Lattner061a1ea2005-01-07 07:46:32 +0000563 }
564 }
565
566 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
567 switch (Opcode) {
568 case ISD::FP_ROUND:
569 case ISD::FP_EXTEND:
570 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000571 case ISD::FP_TO_SINT:
572 return getConstant((int64_t)C->getValue(), VT);
573 case ISD::FP_TO_UINT:
574 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000575 }
576
577 unsigned OpOpcode = Operand.Val->getOpcode();
578 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000579 case ISD::TokenFactor:
580 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000581 case ISD::SIGN_EXTEND:
582 if (Operand.getValueType() == VT) return Operand; // noop extension
583 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
584 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
585 break;
586 case ISD::ZERO_EXTEND:
587 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000588 if (OpOpcode == ISD::ZERO_EXTEND)
589 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000590 break;
591 case ISD::TRUNCATE:
592 if (Operand.getValueType() == VT) return Operand; // noop truncate
593 if (OpOpcode == ISD::TRUNCATE)
594 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000595 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
596 // If the source is smaller than the dest, we still need an extend.
597 if (Operand.Val->getOperand(0).getValueType() < VT)
598 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
599 else if (Operand.Val->getOperand(0).getValueType() > VT)
600 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
601 else
602 return Operand.Val->getOperand(0);
603 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000604 break;
605 }
606
607 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
608 if (N) return SDOperand(N, 0);
609 N = new SDNode(Opcode, Operand);
610 N->setValueTypes(VT);
611 AllNodes.push_back(N);
612 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000613}
614
Chris Lattner061a1ea2005-01-07 07:46:32 +0000615SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
616 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000617#ifndef NDEBUG
618 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000619 case ISD::TokenFactor:
620 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
621 N2.getValueType() == MVT::Other && "Invalid token factor!");
622 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000623 case ISD::AND:
624 case ISD::OR:
625 case ISD::XOR:
626 case ISD::UDIV:
627 case ISD::UREM:
628 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
629 // fall through
630 case ISD::ADD:
631 case ISD::SUB:
632 case ISD::MUL:
633 case ISD::SDIV:
634 case ISD::SREM:
635 assert(N1.getValueType() == N2.getValueType() &&
636 N1.getValueType() == VT && "Binary operator types must match!");
637 break;
638
639 case ISD::SHL:
640 case ISD::SRA:
641 case ISD::SRL:
642 assert(VT == N1.getValueType() &&
643 "Shift operators return type must be the same as their first arg");
644 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000645 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000646 break;
647 default: break;
648 }
649#endif
650
Chris Lattner061a1ea2005-01-07 07:46:32 +0000651 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
652 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
653 if (N1C) {
654 if (N2C) {
655 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
656 switch (Opcode) {
657 case ISD::ADD: return getConstant(C1 + C2, VT);
658 case ISD::SUB: return getConstant(C1 - C2, VT);
659 case ISD::MUL: return getConstant(C1 * C2, VT);
660 case ISD::UDIV:
661 if (C2) return getConstant(C1 / C2, VT);
662 break;
663 case ISD::UREM :
664 if (C2) return getConstant(C1 % C2, VT);
665 break;
666 case ISD::SDIV :
667 if (C2) return getConstant(N1C->getSignExtended() /
668 N2C->getSignExtended(), VT);
669 break;
670 case ISD::SREM :
671 if (C2) return getConstant(N1C->getSignExtended() %
672 N2C->getSignExtended(), VT);
673 break;
674 case ISD::AND : return getConstant(C1 & C2, VT);
675 case ISD::OR : return getConstant(C1 | C2, VT);
676 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +0000677 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
678 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
679 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000680 default: break;
681 }
682
683 } else { // Cannonicalize constant to RHS if commutative
684 if (isCommutativeBinOp(Opcode)) {
685 std::swap(N1C, N2C);
686 std::swap(N1, N2);
687 }
688 }
Chris Lattner32a5f022005-01-19 17:29:49 +0000689
690 switch (Opcode) {
691 default: break;
692 case ISD::SHL: // shl 0, X -> 0
693 if (N1C->isNullValue()) return N1;
694 break;
695 case ISD::SRL: // srl 0, X -> 0
696 if (N1C->isNullValue()) return N1;
697 break;
698 case ISD::SRA: // sra -1, X -> -1
699 if (N1C->isAllOnesValue()) return N1;
700 break;
701 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000702 }
703
704 if (N2C) {
705 uint64_t C2 = N2C->getValue();
706
707 switch (Opcode) {
708 case ISD::ADD:
709 if (!C2) return N1; // add X, 0 -> X
710 break;
711 case ISD::SUB:
712 if (!C2) return N1; // sub X, 0 -> X
713 break;
714 case ISD::MUL:
715 if (!C2) return N2; // mul X, 0 -> 0
716 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
717 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
718
Chris Lattner90b7c132005-01-23 04:39:44 +0000719 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000720 if ((C2 & C2-1) == 0) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000721 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
722 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000723 }
724 break;
725
726 case ISD::UDIV:
Chris Lattner90b7c132005-01-23 04:39:44 +0000727 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000728 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000729 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
730 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000731 }
732 break;
733
Chris Lattnera86fa442005-01-11 04:25:13 +0000734 case ISD::SHL:
735 case ISD::SRL:
Chris Lattner90b7c132005-01-23 04:39:44 +0000736 // If the shift amount is bigger than the size of the data, simplify.
737 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
738 if (TLI.getShiftAmountFlavor() == TargetLowering::Mask) {
739 unsigned NewAmt =
740 C2 & ((1 << MVT::getSizeInBits(N1.getValueType()))-1);
741 return getNode(Opcode, VT, N1, getConstant(NewAmt,N2.getValueType()));
742 } else if (TLI.getShiftAmountFlavor() == TargetLowering::Extend) {
743 // Shifting all of the bits out?
744 return getConstant(0, N1.getValueType());
745 }
746 }
747 // FALL THROUGH.
Chris Lattnera86fa442005-01-11 04:25:13 +0000748 case ISD::SRA:
749 if (C2 == 0) return N1;
750 break;
751
Chris Lattner061a1ea2005-01-07 07:46:32 +0000752 case ISD::AND:
753 if (!C2) return N2; // X and 0 -> 0
754 if (N2C->isAllOnesValue())
755 return N1; // X and -1 -> X
756 break;
757 case ISD::OR:
758 if (!C2)return N1; // X or 0 -> X
759 if (N2C->isAllOnesValue())
760 return N2; // X or -1 -> -1
761 break;
762 case ISD::XOR:
763 if (!C2) return N1; // X xor 0 -> X
764 if (N2C->isAllOnesValue()) {
765 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
766 // !(X op Y) -> (X !op Y)
767 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
768 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000769 SetCC->getValueType(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000770 SetCC->getOperand(0), SetCC->getOperand(1));
771 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
772 SDNode *Op = N1.Val;
773 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
774 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
775 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
776 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
777 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
778 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
779 if (Op->getOpcode() == ISD::AND)
780 return getNode(ISD::OR, VT, LHS, RHS);
781 return getNode(ISD::AND, VT, LHS, RHS);
782 }
783 }
784 // X xor -1 -> not(x) ?
785 }
786 break;
787 }
788
789 // Reassociate ((X op C1) op C2) if possible.
790 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
791 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +0000792 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +0000793 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
794 }
795
796 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
797 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
798 if (N1CFP)
799 if (N2CFP) {
800 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
801 switch (Opcode) {
802 case ISD::ADD: return getConstantFP(C1 + C2, VT);
803 case ISD::SUB: return getConstantFP(C1 - C2, VT);
804 case ISD::MUL: return getConstantFP(C1 * C2, VT);
805 case ISD::SDIV:
806 if (C2) return getConstantFP(C1 / C2, VT);
807 break;
808 case ISD::SREM :
809 if (C2) return getConstantFP(fmod(C1, C2), VT);
810 break;
811 default: break;
812 }
813
814 } else { // Cannonicalize constant to RHS if commutative
815 if (isCommutativeBinOp(Opcode)) {
816 std::swap(N1CFP, N2CFP);
817 std::swap(N1, N2);
818 }
819 }
820
821 // Finally, fold operations that do not require constants.
822 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000823 case ISD::TokenFactor:
824 if (N1.getOpcode() == ISD::EntryToken)
825 return N2;
826 if (N2.getOpcode() == ISD::EntryToken)
827 return N1;
828 break;
829
Chris Lattner061a1ea2005-01-07 07:46:32 +0000830 case ISD::AND:
831 case ISD::OR:
832 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
833 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
834 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
835 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
836 ISD::CondCode Op2 = RHS->getCondition();
837
838 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
839 if (LL == RR && LR == RL) {
840 Op2 = ISD::getSetCCSwappedOperands(Op2);
841 goto MatchedBackwards;
842 }
843
844 if (LL == RL && LR == RR) {
845 MatchedBackwards:
846 ISD::CondCode Result;
847 bool isInteger = MVT::isInteger(LL.getValueType());
848 if (Opcode == ISD::OR)
849 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
850 isInteger);
851 else
852 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
853 isInteger);
854 if (Result != ISD::SETCC_INVALID)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000855 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000856 }
857 }
858 break;
859 case ISD::XOR:
860 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
861 break;
Chris Lattner3d5d5022005-01-09 20:09:57 +0000862 case ISD::SUB:
863 if (N1.getOpcode() == ISD::ADD) {
864 if (N1.Val->getOperand(0) == N2)
865 return N1.Val->getOperand(1); // (A+B)-A == B
866 if (N1.Val->getOperand(1) == N2)
867 return N1.Val->getOperand(0); // (A+B)-B == A
868 }
869 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000870 }
871
872 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
873 if (N) return SDOperand(N, 0);
874 N = new SDNode(Opcode, N1, N2);
875 N->setValueTypes(VT);
876
877 AllNodes.push_back(N);
878 return SDOperand(N, 0);
879}
880
881SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
882 SDOperand Chain, SDOperand Ptr) {
883 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
884 if (N) return SDOperand(N, 0);
885 N = new SDNode(ISD::LOAD, Chain, Ptr);
886
887 // Loads have a token chain.
888 N->setValueTypes(VT, MVT::Other);
889 AllNodes.push_back(N);
890 return SDOperand(N, 0);
891}
892
893
894SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
895 SDOperand N1, SDOperand N2, SDOperand N3) {
896 // Perform various simplifications.
897 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
898 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
899 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
900 switch (Opcode) {
901 case ISD::SELECT:
902 if (N1C)
903 if (N1C->getValue())
904 return N2; // select true, X, Y -> X
905 else
906 return N3; // select false, X, Y -> Y
907
908 if (N2 == N3) return N2; // select C, X, X -> X
909
910 if (VT == MVT::i1) { // Boolean SELECT
911 if (N2C) {
912 if (N3C) {
913 if (N2C->getValue()) // select C, 1, 0 -> C
914 return N1;
915 return getNode(ISD::XOR, VT, N1, N3); // select C, 0, 1 -> ~C
916 }
917
918 if (N2C->getValue()) // select C, 1, X -> C | X
919 return getNode(ISD::OR, VT, N1, N3);
920 else // select C, 0, X -> ~C & X
921 return getNode(ISD::AND, VT,
922 getNode(ISD::XOR, N1.getValueType(), N1,
923 getConstant(1, N1.getValueType())), N3);
924 } else if (N3C) {
925 if (N3C->getValue()) // select C, X, 1 -> ~C | X
926 return getNode(ISD::OR, VT,
927 getNode(ISD::XOR, N1.getValueType(), N1,
928 getConstant(1, N1.getValueType())), N2);
929 else // select C, X, 0 -> C & X
930 return getNode(ISD::AND, VT, N1, N2);
931 }
932 }
933
934 break;
Chris Lattner5c66e452005-01-07 22:49:57 +0000935 case ISD::BRCOND:
936 if (N2C)
937 if (N2C->getValue()) // Unconditional branch
938 return getNode(ISD::BR, MVT::Other, N1, N3);
939 else
940 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000941 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000942 }
943
944 SDNode *N = new SDNode(Opcode, N1, N2, N3);
945 switch (Opcode) {
946 default:
947 N->setValueTypes(VT);
948 break;
949 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
950 N->setValueTypes(VT, MVT::Other);
951 break;
Chris Lattner4157c412005-04-02 04:00:59 +0000952
953 case ISD::SRA_PARTS:
954 case ISD::SRL_PARTS:
955 case ISD::SHL_PARTS: {
956 std::vector<MVT::ValueType> V(N->getNumOperands()-1, VT);
957 N->setValueTypes(V);
958 break;
959 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000960 }
961
962 // FIXME: memoize NODES
963 AllNodes.push_back(N);
964 return SDOperand(N, 0);
965}
966
967SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
968 std::vector<SDOperand> &Children) {
969 switch (Children.size()) {
970 case 0: return getNode(Opcode, VT);
971 case 1: return getNode(Opcode, VT, Children[0]);
972 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
973 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
974 default:
975 // FIXME: MEMOIZE!!
976 SDNode *N = new SDNode(Opcode, Children);
Chris Lattner4157c412005-04-02 04:00:59 +0000977 if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
Chris Lattner1fe9b402005-01-20 18:50:55 +0000978 N->setValueTypes(VT);
979 } else {
980 std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
981 N->setValueTypes(V);
982 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000983 AllNodes.push_back(N);
984 return SDOperand(N, 0);
985 }
986}
987
Chris Lattner39c67442005-01-14 22:08:15 +0000988SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner1001c6e2005-01-15 06:17:04 +0000989 MVT::ValueType EVT) {
990
991 switch (Opcode) {
992 default: assert(0 && "Bad opcode for this accessor!");
993 case ISD::FP_ROUND_INREG:
994 assert(VT == N1.getValueType() && "Not an inreg round!");
995 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
996 "Cannot FP_ROUND_INREG integer types");
997 if (EVT == VT) return N1; // Not actually rounding
998 assert(EVT < VT && "Not rounding down!");
Chris Lattner7f269462005-03-09 18:37:12 +0000999
1000 if (isa<ConstantFPSDNode>(N1))
1001 return getNode(ISD::FP_EXTEND, VT, getNode(ISD::FP_ROUND, EVT, N1));
Chris Lattner1001c6e2005-01-15 06:17:04 +00001002 break;
1003 case ISD::ZERO_EXTEND_INREG:
1004 case ISD::SIGN_EXTEND_INREG:
1005 assert(VT == N1.getValueType() && "Not an inreg extend!");
1006 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1007 "Cannot *_EXTEND_INREG FP types");
1008 if (EVT == VT) return N1; // Not actually extending
1009 assert(EVT < VT && "Not extending!");
Chris Lattner0fe77762005-01-16 00:17:20 +00001010
Chris Lattner7f269462005-03-09 18:37:12 +00001011 // Extending a constant? Just return the constant.
1012 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1013 SDOperand Tmp = getNode(ISD::TRUNCATE, EVT, N1);
Chris Lattner85e71632005-03-10 20:55:51 +00001014 if (Opcode == ISD::ZERO_EXTEND_INREG)
Chris Lattner7f269462005-03-09 18:37:12 +00001015 return getNode(ISD::ZERO_EXTEND, VT, Tmp);
1016 else
1017 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
1018 }
1019
Chris Lattner0fe77762005-01-16 00:17:20 +00001020 // If we are sign extending an extension, use the original source.
1021 if (N1.getOpcode() == ISD::ZERO_EXTEND_INREG ||
1022 N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
1023 if (N1.getOpcode() == Opcode &&
1024 cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1025 return N1;
1026 }
1027
Chris Lattner1001c6e2005-01-15 06:17:04 +00001028 break;
1029 }
1030
1031 EVTStruct NN;
1032 NN.Opcode = Opcode;
1033 NN.VT = VT;
1034 NN.EVT = EVT;
1035 NN.Ops.push_back(N1);
1036
1037 SDNode *&N = MVTSDNodes[NN];
1038 if (N) return SDOperand(N, 0);
1039 N = new MVTSDNode(Opcode, VT, N1, EVT);
1040 AllNodes.push_back(N);
1041 return SDOperand(N, 0);
1042}
1043
1044SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner39c67442005-01-14 22:08:15 +00001045 SDOperand N2, MVT::ValueType EVT) {
1046 switch (Opcode) {
1047 default: assert(0 && "Bad opcode for this accessor!");
1048 case ISD::EXTLOAD:
1049 case ISD::SEXTLOAD:
1050 case ISD::ZEXTLOAD:
1051 // If they are asking for an extending loat from/to the same thing, return a
1052 // normal load.
1053 if (VT == EVT)
1054 return getNode(ISD::LOAD, VT, N1, N2);
1055 assert(EVT < VT && "Should only be an extending load, not truncating!");
1056 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1057 "Cannot sign/zero extend a FP load!");
1058 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1059 "Cannot convert from FP to Int or Int -> FP!");
1060 break;
1061 }
1062
1063 EVTStruct NN;
1064 NN.Opcode = Opcode;
1065 NN.VT = VT;
1066 NN.EVT = EVT;
1067 NN.Ops.push_back(N1);
1068 NN.Ops.push_back(N2);
1069
1070 SDNode *&N = MVTSDNodes[NN];
1071 if (N) return SDOperand(N, 0);
Chris Lattner3b8e7192005-01-14 22:38:01 +00001072 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner39c67442005-01-14 22:08:15 +00001073 AllNodes.push_back(N);
1074 return SDOperand(N, 0);
1075}
1076
1077SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
1078 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
1079 switch (Opcode) {
1080 default: assert(0 && "Bad opcode for this accessor!");
1081 case ISD::TRUNCSTORE:
Chris Lattner1001c6e2005-01-15 06:17:04 +00001082#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1083 // If this is a truncating store of a constant, convert to the desired type
1084 // and store it instead.
1085 if (isa<Constant>(N1)) {
1086 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1087 if (isa<Constant>(Op))
1088 N1 = Op;
1089 }
1090 // Also for ConstantFP?
1091#endif
Chris Lattner39c67442005-01-14 22:08:15 +00001092 if (N1.getValueType() == EVT) // Normal store?
1093 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner1001c6e2005-01-15 06:17:04 +00001094 assert(N2.getValueType() > EVT && "Not a truncation?");
1095 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner39c67442005-01-14 22:08:15 +00001096 "Can't do FP-INT conversion!");
1097 break;
1098 }
1099
1100 EVTStruct NN;
1101 NN.Opcode = Opcode;
1102 NN.VT = VT;
1103 NN.EVT = EVT;
1104 NN.Ops.push_back(N1);
1105 NN.Ops.push_back(N2);
1106 NN.Ops.push_back(N3);
1107
1108 SDNode *&N = MVTSDNodes[NN];
1109 if (N) return SDOperand(N, 0);
1110 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1111 AllNodes.push_back(N);
1112 return SDOperand(N, 0);
1113}
1114
1115
Chris Lattner40e79822005-01-12 18:37:47 +00001116/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1117/// indicated value. This method ignores uses of other values defined by this
1118/// operation.
1119bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1120 assert(Value < getNumValues() && "Bad value!");
1121
1122 // If there is only one value, this is easy.
1123 if (getNumValues() == 1)
1124 return use_size() == NUses;
1125 if (Uses.size() < NUses) return false;
1126
1127 SDOperand TheValue(this, Value);
1128
1129 std::set<SDNode*> UsersHandled;
1130
1131 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1132 UI != E; ++UI) {
1133 SDNode *User = *UI;
1134 if (User->getNumOperands() == 1 ||
1135 UsersHandled.insert(User).second) // First time we've seen this?
1136 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1137 if (User->getOperand(i) == TheValue) {
1138 if (NUses == 0)
1139 return false; // too many uses
1140 --NUses;
1141 }
1142 }
1143
1144 // Found exactly the right number of uses?
1145 return NUses == 0;
1146}
1147
1148
Chris Lattner9e4c7612005-01-10 23:25:25 +00001149const char *SDNode::getOperationName() const {
1150 switch (getOpcode()) {
1151 default: return "<<Unknown>>";
Andrew Lenharthdec53922005-03-31 21:24:06 +00001152 case ISD::PCMARKER: return "PCMarker";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001153 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001154 case ISD::TokenFactor: return "TokenFactor";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001155 case ISD::Constant: return "Constant";
1156 case ISD::ConstantFP: return "ConstantFP";
1157 case ISD::GlobalAddress: return "GlobalAddress";
1158 case ISD::FrameIndex: return "FrameIndex";
1159 case ISD::BasicBlock: return "BasicBlock";
1160 case ISD::ExternalSymbol: return "ExternalSymbol";
1161 case ISD::ConstantPool: return "ConstantPoolIndex";
1162 case ISD::CopyToReg: return "CopyToReg";
1163 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattnere727af02005-01-13 20:50:02 +00001164 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemancda9aa72005-04-01 22:34:39 +00001165 case ISD::UNDEF: return "undef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001166
Chris Lattnerc4a20462005-04-02 04:58:41 +00001167 // Unary operators
1168 case ISD::FABS: return "fabs";
1169 case ISD::FNEG: return "fneg";
1170
1171 // Binary operators
Chris Lattner9e4c7612005-01-10 23:25:25 +00001172 case ISD::ADD: return "add";
1173 case ISD::SUB: return "sub";
1174 case ISD::MUL: return "mul";
Nate Begeman55e86252005-04-05 22:36:56 +00001175 case ISD::MULHU: return "mulhu";
1176 case ISD::MULHS: return "mulhs";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001177 case ISD::SDIV: return "sdiv";
1178 case ISD::UDIV: return "udiv";
1179 case ISD::SREM: return "srem";
1180 case ISD::UREM: return "urem";
1181 case ISD::AND: return "and";
1182 case ISD::OR: return "or";
1183 case ISD::XOR: return "xor";
1184 case ISD::SHL: return "shl";
1185 case ISD::SRA: return "sra";
1186 case ISD::SRL: return "srl";
1187
1188 case ISD::SELECT: return "select";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001189 case ISD::ADD_PARTS: return "add_parts";
1190 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner5b7bb562005-04-02 03:30:42 +00001191 case ISD::SHL_PARTS: return "shl_parts";
1192 case ISD::SRA_PARTS: return "sra_parts";
1193 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001194
1195 // Conversion operators.
1196 case ISD::SIGN_EXTEND: return "sign_extend";
1197 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001198 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
1199 case ISD::ZERO_EXTEND_INREG: return "zero_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001200 case ISD::TRUNCATE: return "truncate";
1201 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001202 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001203 case ISD::FP_EXTEND: return "fp_extend";
1204
1205 case ISD::SINT_TO_FP: return "sint_to_fp";
1206 case ISD::UINT_TO_FP: return "uint_to_fp";
1207 case ISD::FP_TO_SINT: return "fp_to_sint";
1208 case ISD::FP_TO_UINT: return "fp_to_uint";
1209
1210 // Control flow instructions
1211 case ISD::BR: return "br";
1212 case ISD::BRCOND: return "brcond";
1213 case ISD::RET: return "ret";
1214 case ISD::CALL: return "call";
1215 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1216 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1217
1218 // Other operators
1219 case ISD::LOAD: return "load";
1220 case ISD::STORE: return "store";
Chris Lattner39c67442005-01-14 22:08:15 +00001221 case ISD::EXTLOAD: return "extload";
1222 case ISD::SEXTLOAD: return "sextload";
1223 case ISD::ZEXTLOAD: return "zextload";
1224 case ISD::TRUNCSTORE: return "truncstore";
1225
Chris Lattner9e4c7612005-01-10 23:25:25 +00001226 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1227 case ISD::EXTRACT_ELEMENT: return "extract_element";
1228 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001229 case ISD::MEMSET: return "memset";
1230 case ISD::MEMCPY: return "memcpy";
1231 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001232
1233 case ISD::SETCC:
1234 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1235 switch (SetCC->getCondition()) {
1236 default: assert(0 && "Unknown setcc condition!");
1237 case ISD::SETOEQ: return "setcc:setoeq";
1238 case ISD::SETOGT: return "setcc:setogt";
1239 case ISD::SETOGE: return "setcc:setoge";
1240 case ISD::SETOLT: return "setcc:setolt";
1241 case ISD::SETOLE: return "setcc:setole";
1242 case ISD::SETONE: return "setcc:setone";
1243
1244 case ISD::SETO: return "setcc:seto";
1245 case ISD::SETUO: return "setcc:setuo";
1246 case ISD::SETUEQ: return "setcc:setue";
1247 case ISD::SETUGT: return "setcc:setugt";
1248 case ISD::SETUGE: return "setcc:setuge";
1249 case ISD::SETULT: return "setcc:setult";
1250 case ISD::SETULE: return "setcc:setule";
1251 case ISD::SETUNE: return "setcc:setune";
1252
1253 case ISD::SETEQ: return "setcc:seteq";
1254 case ISD::SETGT: return "setcc:setgt";
1255 case ISD::SETGE: return "setcc:setge";
1256 case ISD::SETLT: return "setcc:setlt";
1257 case ISD::SETLE: return "setcc:setle";
1258 case ISD::SETNE: return "setcc:setne";
1259 }
1260 }
1261}
Chris Lattner061a1ea2005-01-07 07:46:32 +00001262
1263void SDNode::dump() const {
1264 std::cerr << (void*)this << ": ";
1265
1266 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1267 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00001268 if (getValueType(i) == MVT::Other)
1269 std::cerr << "ch";
1270 else
1271 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001272 }
Chris Lattner9e4c7612005-01-10 23:25:25 +00001273 std::cerr << " = " << getOperationName();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001274
1275 std::cerr << " ";
1276 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1277 if (i) std::cerr << ", ";
1278 std::cerr << (void*)getOperand(i).Val;
1279 if (unsigned RN = getOperand(i).ResNo)
1280 std::cerr << ":" << RN;
1281 }
1282
1283 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1284 std::cerr << "<" << CSDN->getValue() << ">";
1285 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1286 std::cerr << "<" << CSDN->getValue() << ">";
1287 } else if (const GlobalAddressSDNode *GADN =
1288 dyn_cast<GlobalAddressSDNode>(this)) {
1289 std::cerr << "<";
1290 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1291 } else if (const FrameIndexSDNode *FIDN =
1292 dyn_cast<FrameIndexSDNode>(this)) {
1293 std::cerr << "<" << FIDN->getIndex() << ">";
1294 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1295 std::cerr << "<" << CP->getIndex() << ">";
1296 } else if (const BasicBlockSDNode *BBDN =
1297 dyn_cast<BasicBlockSDNode>(this)) {
1298 std::cerr << "<";
1299 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1300 if (LBB)
1301 std::cerr << LBB->getName() << " ";
1302 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnere727af02005-01-13 20:50:02 +00001303 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001304 std::cerr << "<reg #" << C2V->getReg() << ">";
1305 } else if (const ExternalSymbolSDNode *ES =
1306 dyn_cast<ExternalSymbolSDNode>(this)) {
1307 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner630d1932005-01-15 21:11:37 +00001308 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1309 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattner061a1ea2005-01-07 07:46:32 +00001310 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001311}
1312
Chris Lattnere6f78822005-01-09 20:38:33 +00001313static void DumpNodes(SDNode *N, unsigned indent) {
1314 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1315 if (N->getOperand(i).Val->hasOneUse())
1316 DumpNodes(N->getOperand(i).Val, indent+2);
1317 else
1318 std::cerr << "\n" << std::string(indent+2, ' ')
1319 << (void*)N->getOperand(i).Val << ": <multiple use>";
1320
1321
1322 std::cerr << "\n" << std::string(indent, ' ');
1323 N->dump();
1324}
1325
Chris Lattner061a1ea2005-01-07 07:46:32 +00001326void SelectionDAG::dump() const {
1327 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner1270acc2005-01-09 20:26:36 +00001328 std::vector<SDNode*> Nodes(AllNodes);
1329 std::sort(Nodes.begin(), Nodes.end());
1330
1331 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001332 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1333 DumpNodes(Nodes[i], 2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001334 }
Chris Lattnere6f78822005-01-09 20:38:33 +00001335
1336 DumpNodes(getRoot().Val, 2);
1337
Chris Lattner061a1ea2005-01-07 07:46:32 +00001338 std::cerr << "\n\n";
1339}
1340