blob: 6790adb9d2c73046af719a7ac54d5a46406b6dfb [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000019#include "llvm/Target/TargetLowering.h"
Reid Spencer954da372004-07-04 12:19:56 +000020#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000021#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000023#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner5cdcc582005-01-09 20:52:51 +000026static bool isCommutativeBinOp(unsigned Opcode) {
27 switch (Opcode) {
28 case ISD::ADD:
29 case ISD::MUL:
30 case ISD::AND:
31 case ISD::OR:
32 case ISD::XOR: return true;
33 default: return false; // FIXME: Need commutative info for user ops!
34 }
35}
36
37static bool isAssociativeBinOp(unsigned Opcode) {
38 switch (Opcode) {
39 case ISD::ADD:
40 case ISD::MUL:
41 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need associative info for user ops!
45 }
46}
47
48static unsigned ExactLog2(uint64_t Val) {
49 unsigned Count = 0;
50 while (Val != 1) {
51 Val >>= 1;
52 ++Count;
53 }
54 return Count;
55}
56
57// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
61 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
62 return true;
63 return false;
64}
65
66
Chris Lattnerc3aae252005-01-07 07:46:32 +000067/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
68/// when given the operation for (X op Y).
69ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
70 // To perform this operation, we just need to swap the L and G bits of the
71 // operation.
72 unsigned OldL = (Operation >> 2) & 1;
73 unsigned OldG = (Operation >> 1) & 1;
74 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
75 (OldL << 1) | // New G bit
76 (OldG << 2)); // New L bit.
77}
78
79/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
80/// 'op' is a valid SetCC operation.
81ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
82 unsigned Operation = Op;
83 if (isInteger)
84 Operation ^= 7; // Flip L, G, E bits, but not U.
85 else
86 Operation ^= 15; // Flip all of the condition bits.
87 if (Operation > ISD::SETTRUE2)
88 Operation &= ~8; // Don't let N and U bits get set.
89 return ISD::CondCode(Operation);
90}
91
92
93/// isSignedOp - For an integer comparison, return 1 if the comparison is a
94/// signed operation and 2 if the result is an unsigned comparison. Return zero
95/// if the operation does not depend on the sign of the input (setne and seteq).
96static int isSignedOp(ISD::CondCode Opcode) {
97 switch (Opcode) {
98 default: assert(0 && "Illegal integer setcc operation!");
99 case ISD::SETEQ:
100 case ISD::SETNE: return 0;
101 case ISD::SETLT:
102 case ISD::SETLE:
103 case ISD::SETGT:
104 case ISD::SETGE: return 1;
105 case ISD::SETULT:
106 case ISD::SETULE:
107 case ISD::SETUGT:
108 case ISD::SETUGE: return 2;
109 }
110}
111
112/// getSetCCOrOperation - Return the result of a logical OR between different
113/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
114/// returns SETCC_INVALID if it is not possible to represent the resultant
115/// comparison.
116ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
117 bool isInteger) {
118 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
119 // Cannot fold a signed integer setcc with an unsigned integer setcc.
120 return ISD::SETCC_INVALID;
121
122 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
123
124 // If the N and U bits get set then the resultant comparison DOES suddenly
125 // care about orderedness, and is true when ordered.
126 if (Op > ISD::SETTRUE2)
127 Op &= ~16; // Clear the N bit.
128 return ISD::CondCode(Op);
129}
130
131/// getSetCCAndOperation - Return the result of a logical AND between different
132/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
133/// function returns zero if it is not possible to represent the resultant
134/// comparison.
135ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
136 bool isInteger) {
137 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
138 // Cannot fold a signed setcc with an unsigned setcc.
139 return ISD::SETCC_INVALID;
140
141 // Combine all of the condition bits.
142 return ISD::CondCode(Op1 & Op2);
143}
144
Chris Lattnerb48da392005-01-23 04:39:44 +0000145const TargetMachine &SelectionDAG::getTarget() const {
146 return TLI.getTargetMachine();
147}
148
149
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000150/// RemoveDeadNodes - This method deletes all unreachable nodes in the
151/// SelectionDAG, including nodes (like loads) that have uses of their token
152/// chain but no other uses and no side effect. If a node is passed in as an
153/// argument, it is used as the seed for node deletion.
154void SelectionDAG::RemoveDeadNodes(SDNode *N) {
155 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
156
157 // Create a dummy node (which is not added to allnodes), that adds a reference
158 // to the root node, preventing it from being deleted.
159 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
160
161 DeleteNodeIfDead(N, &AllNodeSet);
162
163 Restart:
164 unsigned NumNodes = AllNodeSet.size();
165 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
166 I != E; ++I) {
167 // Try to delete this node.
168 DeleteNodeIfDead(*I, &AllNodeSet);
169
170 // If we actually deleted any nodes, do not use invalid iterators in
171 // AllNodeSet.
172 if (AllNodeSet.size() != NumNodes)
173 goto Restart;
174 }
175
176 // Restore AllNodes.
177 if (AllNodes.size() != NumNodes)
178 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
179
180 // If the root changed (e.g. it was a dead load, update the root).
181 setRoot(DummyNode->getOperand(0));
182
183 // Now that we are done with the dummy node, delete it.
184 DummyNode->getOperand(0).Val->removeUser(DummyNode);
185 delete DummyNode;
186}
187
188void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
189 if (!N->use_empty())
190 return;
191
192 // Okay, we really are going to delete this node. First take this out of the
193 // appropriate CSE map.
194 switch (N->getOpcode()) {
195 case ISD::Constant:
196 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
197 N->getValueType(0)));
198 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000199 case ISD::ConstantFP: {
200 union {
201 double DV;
202 uint64_t IV;
203 };
204 DV = cast<ConstantFPSDNode>(N)->getValue();
205 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000206 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000207 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000208 case ISD::GlobalAddress:
209 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
210 break;
211 case ISD::FrameIndex:
212 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
213 break;
214 case ISD::ConstantPool:
215 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
216 break;
217 case ISD::BasicBlock:
218 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
219 break;
220 case ISD::ExternalSymbol:
221 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
222 break;
223
224 case ISD::LOAD:
225 Loads.erase(std::make_pair(N->getOperand(1),
226 std::make_pair(N->getOperand(0),
227 N->getValueType(0))));
228 break;
229 case ISD::SETCC:
230 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
231 N->getOperand(1)),
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000232 std::make_pair(
233 cast<SetCCSDNode>(N)->getCondition(),
234 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000235 break;
Chris Lattner859157d2005-01-15 06:17:04 +0000236 case ISD::TRUNCSTORE:
237 case ISD::SIGN_EXTEND_INREG:
Chris Lattner859157d2005-01-15 06:17:04 +0000238 case ISD::FP_ROUND_INREG:
Chris Lattner69a52152005-01-14 22:38:01 +0000239 case ISD::EXTLOAD:
240 case ISD::SEXTLOAD:
241 case ISD::ZEXTLOAD: {
242 EVTStruct NN;
Chris Lattner8e8bd652005-04-07 00:30:13 +0000243 NN.Opcode = N->getOpcode();
Chris Lattner69a52152005-01-14 22:38:01 +0000244 NN.VT = N->getValueType(0);
245 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner859157d2005-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 Lattner69a52152005-01-14 22:38:01 +0000248 MVTSDNodes.erase(NN);
249 break;
250 }
Chris Lattner0e12e6e2005-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 Lattner0e12e6e2005-01-07 21:09:16 +0000264 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000265 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 N->Operands.pop_back();
Chris Lattner7c68ec62005-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 Lattner0e12e6e2005-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 Lattner0e12e6e2005-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 Lattner7c68ec62005-01-07 23:32:00 +0000279 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000280}
281
282
Chris Lattner78ec3112003-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 Lattner0f2287b2005-04-13 02:38:18 +0000288SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000289 if (Op.getValueType() == VT) return Op;
Chris Lattner0f2287b2005-04-13 02:38:18 +0000290 int64_t Imm = ~0ULL >> 64-MVT::getSizeInBits(VT);
291 return getNode(ISD::AND, Op.getValueType(), Op,
292 getConstant(Imm, Op.getValueType()));
293}
294
Chris Lattnerc3aae252005-01-07 07:46:32 +0000295SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
296 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
297 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000298 if (VT != MVT::i64)
299 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
300
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301 SDNode *&N = Constants[std::make_pair(Val, VT)];
302 if (N) return SDOperand(N, 0);
303 N = new ConstantSDNode(Val, VT);
304 AllNodes.push_back(N);
305 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000306}
307
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
309 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
310 if (VT == MVT::f32)
311 Val = (float)Val; // Mask out extra precision.
312
Chris Lattnerd8658612005-02-17 20:17:32 +0000313 // Do the map lookup using the actual bit pattern for the floating point
314 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
315 // we don't have issues with SNANs.
316 union {
317 double DV;
318 uint64_t IV;
319 };
320
321 DV = Val;
322
323 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000324 if (N) return SDOperand(N, 0);
325 N = new ConstantFPSDNode(Val, VT);
326 AllNodes.push_back(N);
327 return SDOperand(N, 0);
328}
329
330
331
332SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
333 MVT::ValueType VT) {
334 SDNode *&N = GlobalValues[GV];
335 if (N) return SDOperand(N, 0);
336 N = new GlobalAddressSDNode(GV,VT);
337 AllNodes.push_back(N);
338 return SDOperand(N, 0);
339}
340
341SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
342 SDNode *&N = FrameIndices[FI];
343 if (N) return SDOperand(N, 0);
344 N = new FrameIndexSDNode(FI, VT);
345 AllNodes.push_back(N);
346 return SDOperand(N, 0);
347}
348
349SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
350 SDNode *N = ConstantPoolIndices[CPIdx];
351 if (N) return SDOperand(N, 0);
352 N = new ConstantPoolSDNode(CPIdx, VT);
353 AllNodes.push_back(N);
354 return SDOperand(N, 0);
355}
356
357SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
358 SDNode *&N = BBNodes[MBB];
359 if (N) return SDOperand(N, 0);
360 N = new BasicBlockSDNode(MBB);
361 AllNodes.push_back(N);
362 return SDOperand(N, 0);
363}
364
365SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
366 SDNode *&N = ExternalSymbols[Sym];
367 if (N) return SDOperand(N, 0);
368 N = new ExternalSymbolSDNode(Sym, VT);
369 AllNodes.push_back(N);
370 return SDOperand(N, 0);
371}
372
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000373SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
374 SDOperand N1, SDOperand N2) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000375 // These setcc operations always fold.
376 switch (Cond) {
377 default: break;
378 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000379 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000380 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000381 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000382 }
383
Chris Lattner67255a12005-04-07 18:14:58 +0000384 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
385 uint64_t C2 = N2C->getValue();
386 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
387 uint64_t C1 = N1C->getValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000388
389 // Sign extend the operands if required
390 if (ISD::isSignedIntSetCC(Cond)) {
391 C1 = N1C->getSignExtended();
392 C2 = N2C->getSignExtended();
393 }
394
395 switch (Cond) {
396 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000397 case ISD::SETEQ: return getConstant(C1 == C2, VT);
398 case ISD::SETNE: return getConstant(C1 != C2, VT);
399 case ISD::SETULT: return getConstant(C1 < C2, VT);
400 case ISD::SETUGT: return getConstant(C1 > C2, VT);
401 case ISD::SETULE: return getConstant(C1 <= C2, VT);
402 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
403 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
404 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
405 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
406 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000407 }
Chris Lattner24673922005-04-07 18:58:54 +0000408 } else {
Chris Lattner67255a12005-04-07 18:14:58 +0000409 uint64_t MinVal, MaxVal;
410 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
411 if (ISD::isSignedIntSetCC(Cond)) {
412 MinVal = 1ULL << (OperandBitSize-1);
413 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
414 MaxVal = ~0ULL >> (65-OperandBitSize);
415 else
416 MaxVal = 0;
417 } else {
418 MinVal = 0;
419 MaxVal = ~0ULL >> (64-OperandBitSize);
420 }
421
422 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
423 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
424 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
425 --C2; // X >= C1 --> X > (C1-1)
426 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
427 N2 = getConstant(C2, N2.getValueType());
428 N2C = cast<ConstantSDNode>(N2.Val);
429 }
430
431 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
432 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
433 ++C2; // X <= C1 --> X < (C1+1)
434 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
435 N2 = getConstant(C2, N2.getValueType());
436 N2C = cast<ConstantSDNode>(N2.Val);
437 }
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000438
Nate Begeman72ea2812005-04-14 08:56:52 +0000439 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
440 return getConstant(0, VT); // X < MIN --> false
441
442 // Canonicalize setgt X, Min --> setne X, Min
443 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
444 return getSetCC(ISD::SETNE, VT, N1, N2);
445
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000446 // If we have setult X, 1, turn it into seteq X, 0
447 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
448 return getSetCC(ISD::SETEQ, VT, N1,
449 getConstant(MinVal, N1.getValueType()));
Nate Begeman72ea2812005-04-14 08:56:52 +0000450 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000451 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
452 return getSetCC(ISD::SETEQ, VT, N1,
453 getConstant(MaxVal, N1.getValueType()));
Chris Lattner67255a12005-04-07 18:14:58 +0000454
455 // If we have "setcc X, C1", check to see if we can shrink the immediate
456 // by changing cc.
457
458 // SETUGT X, SINTMAX -> SETLT X, 0
459 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
460 C2 == (~0ULL >> (65-OperandBitSize)))
461 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
462
463 // FIXME: Implement the rest of these.
464
Chris Lattnerc3aae252005-01-07 07:46:32 +0000465 }
Chris Lattner67255a12005-04-07 18:14:58 +0000466 } else if (isa<ConstantSDNode>(N1.Val)) {
467 // Ensure that the constant occurs on the RHS.
468 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
469 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000470
471 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
472 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
473 double C1 = N1C->getValue(), C2 = N2C->getValue();
474
475 switch (Cond) {
476 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000477 case ISD::SETEQ: return getConstant(C1 == C2, VT);
478 case ISD::SETNE: return getConstant(C1 != C2, VT);
479 case ISD::SETLT: return getConstant(C1 < C2, VT);
480 case ISD::SETGT: return getConstant(C1 > C2, VT);
481 case ISD::SETLE: return getConstant(C1 <= C2, VT);
482 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000483 }
484 } else {
485 // Ensure that the constant occurs on the RHS.
486 Cond = ISD::getSetCCSwappedOperands(Cond);
487 std::swap(N1, N2);
488 }
489
490 if (N1 == N2) {
491 // We can always fold X == Y for integer setcc's.
492 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000493 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000494 unsigned UOF = ISD::getUnorderedFlavor(Cond);
495 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000496 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000497 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000498 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000499 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
500 // if it is not already.
501 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
502 }
503
Chris Lattner5cdcc582005-01-09 20:52:51 +0000504 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000505 MVT::isInteger(N1.getValueType())) {
506 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
507 N1.getOpcode() == ISD::XOR) {
508 // Simplify (X+Y) == (X+Z) --> Y == Z
509 if (N1.getOpcode() == N2.getOpcode()) {
510 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000511 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000512 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000513 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000514 if (isCommutativeBinOp(N1.getOpcode())) {
515 // If X op Y == Y op X, try other combinations.
516 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000517 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000518 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000519 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000520 }
521 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000522
523 // FIXME: move this stuff to the DAG Combiner when it exists!
Chris Lattner68dc3102005-01-10 02:03:02 +0000524
525 // Simplify (X+Z) == X --> Z == 0
526 if (N1.getOperand(0) == N2)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000527 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000528 getConstant(0, N1.getValueType()));
529 if (N1.getOperand(1) == N2) {
530 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000531 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000532 getConstant(0, N1.getValueType()));
533 else {
534 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
535 // (Z-X) == X --> Z == X<<1
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000536 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000537 getNode(ISD::SHL, N2.getValueType(),
Chris Lattnerb48da392005-01-23 04:39:44 +0000538 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner68dc3102005-01-10 02:03:02 +0000539 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000540 }
541 }
542
Chris Lattner68dc3102005-01-10 02:03:02 +0000543 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
544 N2.getOpcode() == ISD::XOR) {
545 // Simplify X == (X+Z) --> Z == 0
546 if (N2.getOperand(0) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000547 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000548 getConstant(0, N2.getValueType()));
549 else if (N2.getOperand(1) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000550 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000551 getConstant(0, N2.getValueType()));
552 }
553 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000554
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000555 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
556 std::make_pair(Cond, VT))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000557 if (N) return SDOperand(N, 0);
558 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000559 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000560 AllNodes.push_back(N);
561 return SDOperand(N, 0);
562}
563
564
565
566/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000567///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000568SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
569 SDNode *N = new SDNode(Opcode, VT);
570 AllNodes.push_back(N);
571 return SDOperand(N, 0);
572}
573
Chris Lattnerc3aae252005-01-07 07:46:32 +0000574SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
575 SDOperand Operand) {
576 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
577 uint64_t Val = C->getValue();
578 switch (Opcode) {
579 default: break;
580 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
581 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
582 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000583 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
584 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000585 }
586 }
587
588 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
589 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000590 case ISD::FNEG:
591 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 case ISD::FP_ROUND:
593 case ISD::FP_EXTEND:
594 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000595 case ISD::FP_TO_SINT:
596 return getConstant((int64_t)C->getValue(), VT);
597 case ISD::FP_TO_UINT:
598 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000599 }
600
601 unsigned OpOpcode = Operand.Val->getOpcode();
602 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000603 case ISD::TokenFactor:
604 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000605 case ISD::SIGN_EXTEND:
606 if (Operand.getValueType() == VT) return Operand; // noop extension
607 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
608 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
609 break;
610 case ISD::ZERO_EXTEND:
611 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000612 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000613 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000614 break;
615 case ISD::TRUNCATE:
616 if (Operand.getValueType() == VT) return Operand; // noop truncate
617 if (OpOpcode == ISD::TRUNCATE)
618 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000619 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
620 // If the source is smaller than the dest, we still need an extend.
621 if (Operand.Val->getOperand(0).getValueType() < VT)
622 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
623 else if (Operand.Val->getOperand(0).getValueType() > VT)
624 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
625 else
626 return Operand.Val->getOperand(0);
627 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000629 case ISD::FNEG:
630 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
631 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
632 Operand.Val->getOperand(0));
633 if (OpOpcode == ISD::FNEG) // --X -> X
634 return Operand.Val->getOperand(0);
635 break;
636 case ISD::FABS:
637 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
638 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
639 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000640 }
641
642 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
643 if (N) return SDOperand(N, 0);
644 N = new SDNode(Opcode, Operand);
645 N->setValueTypes(VT);
646 AllNodes.push_back(N);
647 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000648}
649
Chris Lattnerc3aae252005-01-07 07:46:32 +0000650SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
651 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000652#ifndef NDEBUG
653 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000654 case ISD::TokenFactor:
655 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
656 N2.getValueType() == MVT::Other && "Invalid token factor!");
657 break;
Chris Lattner76365122005-01-16 02:23:22 +0000658 case ISD::AND:
659 case ISD::OR:
660 case ISD::XOR:
661 case ISD::UDIV:
662 case ISD::UREM:
663 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
664 // fall through
665 case ISD::ADD:
666 case ISD::SUB:
667 case ISD::MUL:
668 case ISD::SDIV:
669 case ISD::SREM:
670 assert(N1.getValueType() == N2.getValueType() &&
671 N1.getValueType() == VT && "Binary operator types must match!");
672 break;
673
674 case ISD::SHL:
675 case ISD::SRA:
676 case ISD::SRL:
677 assert(VT == N1.getValueType() &&
678 "Shift operators return type must be the same as their first arg");
679 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000680 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000681 break;
682 default: break;
683 }
684#endif
685
Chris Lattnerc3aae252005-01-07 07:46:32 +0000686 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
687 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
688 if (N1C) {
689 if (N2C) {
690 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
691 switch (Opcode) {
692 case ISD::ADD: return getConstant(C1 + C2, VT);
693 case ISD::SUB: return getConstant(C1 - C2, VT);
694 case ISD::MUL: return getConstant(C1 * C2, VT);
695 case ISD::UDIV:
696 if (C2) return getConstant(C1 / C2, VT);
697 break;
698 case ISD::UREM :
699 if (C2) return getConstant(C1 % C2, VT);
700 break;
701 case ISD::SDIV :
702 if (C2) return getConstant(N1C->getSignExtended() /
703 N2C->getSignExtended(), VT);
704 break;
705 case ISD::SREM :
706 if (C2) return getConstant(N1C->getSignExtended() %
707 N2C->getSignExtended(), VT);
708 break;
709 case ISD::AND : return getConstant(C1 & C2, VT);
710 case ISD::OR : return getConstant(C1 | C2, VT);
711 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000712 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
713 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
714 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000715 default: break;
716 }
717
718 } else { // Cannonicalize constant to RHS if commutative
719 if (isCommutativeBinOp(Opcode)) {
720 std::swap(N1C, N2C);
721 std::swap(N1, N2);
722 }
723 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000724
725 switch (Opcode) {
726 default: break;
727 case ISD::SHL: // shl 0, X -> 0
728 if (N1C->isNullValue()) return N1;
729 break;
730 case ISD::SRL: // srl 0, X -> 0
731 if (N1C->isNullValue()) return N1;
732 break;
733 case ISD::SRA: // sra -1, X -> -1
734 if (N1C->isAllOnesValue()) return N1;
735 break;
736 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737 }
738
739 if (N2C) {
740 uint64_t C2 = N2C->getValue();
741
742 switch (Opcode) {
743 case ISD::ADD:
744 if (!C2) return N1; // add X, 0 -> X
745 break;
746 case ISD::SUB:
747 if (!C2) return N1; // sub X, 0 -> X
748 break;
749 case ISD::MUL:
750 if (!C2) return N2; // mul X, 0 -> 0
751 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
752 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
753
Chris Lattnerb48da392005-01-23 04:39:44 +0000754 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000755 if ((C2 & C2-1) == 0) {
Chris Lattnerb48da392005-01-23 04:39:44 +0000756 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
757 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000758 }
759 break;
760
761 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +0000762 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000763 if ((C2 & C2-1) == 0 && C2) {
Chris Lattnerb48da392005-01-23 04:39:44 +0000764 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
765 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000766 }
767 break;
768
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000769 case ISD::SHL:
770 case ISD::SRL:
771 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +0000772 // If the shift amount is bigger than the size of the data, then all the
773 // bits are shifted out. Simplify to loading constant zero.
774 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
775 return getNode(ISD::UNDEF, N1.getValueType());
776 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000777 if (C2 == 0) return N1;
778 break;
779
Chris Lattnerc3aae252005-01-07 07:46:32 +0000780 case ISD::AND:
781 if (!C2) return N2; // X and 0 -> 0
782 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +0000783 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +0000784
Chris Lattnerdea29e22005-04-10 01:13:15 +0000785 // FIXME: Should add a corresponding version of this for
786 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
787 // we don't have yet.
788
Chris Lattner0f2287b2005-04-13 02:38:18 +0000789 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
790 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +0000791 // If we are masking out the part of our input that was extended, just
792 // mask the input to the extension directly.
793 unsigned ExtendBits =
794 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
795 if ((C2 & (~0ULL << ExtendBits)) == 0)
796 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
797 }
Chris Lattner0f2287b2005-04-13 02:38:18 +0000798 if (N1.getOpcode() == ISD::AND)
799 if (ConstantSDNode *OpRHS = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
800 return getNode(ISD::AND, VT, N1.getOperand(0),
801 getNode(ISD::AND, VT, N1.getOperand(1), N2));
802
803 // If we are anding the result of a setcc, and we know setcc always
804 // returns 0 or 1, simplify the RHS to either be 0 or 1
Nate Begemaneea805e2005-04-13 21:23:31 +0000805 if (N1.getOpcode() == ISD::SETCC &&
Chris Lattner0f2287b2005-04-13 02:38:18 +0000806 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult)
807 if (C2 & 1)
Nate Begemaneea805e2005-04-13 21:23:31 +0000808 return N1;
Chris Lattner0f2287b2005-04-13 02:38:18 +0000809 else
810 return getConstant(0, VT);
Nate Begemaneea805e2005-04-13 21:23:31 +0000811
Chris Lattner0f2287b2005-04-13 02:38:18 +0000812 if (N1.getOpcode() == ISD::ZEXTLOAD) {
813 // If we are anding the result of a zext load, realize that the top bits
814 // of the loaded value are already zero to simplify C2.
815 unsigned SrcBits =
816 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
817 uint64_t C3 = C2 & (~0ULL >> (64-SrcBits));
818 if (C3 != C2)
819 return getNode(ISD::AND, VT, N1, getConstant(C3, VT));
820 else if (C2 == (~0ULL >> (64-SrcBits)))
821 return N1; // Anding out just what is already masked.
822 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000823 break;
824 case ISD::OR:
825 if (!C2)return N1; // X or 0 -> X
826 if (N2C->isAllOnesValue())
827 return N2; // X or -1 -> -1
828 break;
829 case ISD::XOR:
830 if (!C2) return N1; // X xor 0 -> X
831 if (N2C->isAllOnesValue()) {
832 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
833 // !(X op Y) -> (X !op Y)
834 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
835 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000836 SetCC->getValueType(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000837 SetCC->getOperand(0), SetCC->getOperand(1));
838 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
839 SDNode *Op = N1.Val;
840 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
841 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
842 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
843 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
844 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
845 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
846 if (Op->getOpcode() == ISD::AND)
847 return getNode(ISD::OR, VT, LHS, RHS);
848 return getNode(ISD::AND, VT, LHS, RHS);
849 }
850 }
851 // X xor -1 -> not(x) ?
852 }
853 break;
854 }
855
856 // Reassociate ((X op C1) op C2) if possible.
857 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
858 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +0000859 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000860 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
861 }
862
863 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
864 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
865 if (N1CFP)
866 if (N2CFP) {
867 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
868 switch (Opcode) {
869 case ISD::ADD: return getConstantFP(C1 + C2, VT);
870 case ISD::SUB: return getConstantFP(C1 - C2, VT);
871 case ISD::MUL: return getConstantFP(C1 * C2, VT);
872 case ISD::SDIV:
873 if (C2) return getConstantFP(C1 / C2, VT);
874 break;
875 case ISD::SREM :
876 if (C2) return getConstantFP(fmod(C1, C2), VT);
877 break;
878 default: break;
879 }
880
881 } else { // Cannonicalize constant to RHS if commutative
882 if (isCommutativeBinOp(Opcode)) {
883 std::swap(N1CFP, N2CFP);
884 std::swap(N1, N2);
885 }
886 }
887
888 // Finally, fold operations that do not require constants.
889 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000890 case ISD::TokenFactor:
891 if (N1.getOpcode() == ISD::EntryToken)
892 return N2;
893 if (N2.getOpcode() == ISD::EntryToken)
894 return N1;
895 break;
896
Chris Lattnerc3aae252005-01-07 07:46:32 +0000897 case ISD::AND:
898 case ISD::OR:
899 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
900 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
901 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
902 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
903 ISD::CondCode Op2 = RHS->getCondition();
904
905 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
906 if (LL == RR && LR == RL) {
907 Op2 = ISD::getSetCCSwappedOperands(Op2);
908 goto MatchedBackwards;
909 }
910
911 if (LL == RL && LR == RR) {
912 MatchedBackwards:
913 ISD::CondCode Result;
914 bool isInteger = MVT::isInteger(LL.getValueType());
915 if (Opcode == ISD::OR)
916 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
917 isInteger);
918 else
919 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
920 isInteger);
921 if (Result != ISD::SETCC_INVALID)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000922 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000923 }
924 }
925 break;
926 case ISD::XOR:
927 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
928 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000929 case ISD::ADD:
930 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
931 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
932 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
933 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +0000934 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
935 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
936 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
937 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
938 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
939 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Chris Lattner485df9b2005-04-09 03:02:46 +0000940 break;
Chris Lattnerabd21822005-01-09 20:09:57 +0000941 case ISD::SUB:
942 if (N1.getOpcode() == ISD::ADD) {
943 if (N1.Val->getOperand(0) == N2)
944 return N1.Val->getOperand(1); // (A+B)-A == B
945 if (N1.Val->getOperand(1) == N2)
946 return N1.Val->getOperand(0); // (A+B)-B == A
947 }
Chris Lattner485df9b2005-04-09 03:02:46 +0000948 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
949 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +0000950 break;
Nate Begemaneea805e2005-04-13 21:23:31 +0000951 // FIXME: figure out how to safely handle things like
952 // int foo(int x) { return 1 << (x & 255); }
953 // int bar() { return foo(256); }
954#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +0000955 case ISD::SHL:
956 case ISD::SRL:
957 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +0000958 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
959 cast<MVTSDNode>(N2)->getExtraValueType() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +0000960 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +0000961 else if (N2.getOpcode() == ISD::AND)
962 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
963 // If the and is only masking out bits that cannot effect the shift,
964 // eliminate the and.
965 unsigned NumBits = MVT::getSizeInBits(VT);
966 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
967 return getNode(Opcode, VT, N1, N2.getOperand(0));
968 }
Nate Begemandb81eba2005-04-12 23:32:28 +0000969 break;
Nate Begemaneea805e2005-04-13 21:23:31 +0000970#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 }
972
973 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
974 if (N) return SDOperand(N, 0);
975 N = new SDNode(Opcode, N1, N2);
976 N->setValueTypes(VT);
977
978 AllNodes.push_back(N);
979 return SDOperand(N, 0);
980}
981
982SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
983 SDOperand Chain, SDOperand Ptr) {
984 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
985 if (N) return SDOperand(N, 0);
986 N = new SDNode(ISD::LOAD, Chain, Ptr);
987
988 // Loads have a token chain.
989 N->setValueTypes(VT, MVT::Other);
990 AllNodes.push_back(N);
991 return SDOperand(N, 0);
992}
993
994
995SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
996 SDOperand N1, SDOperand N2, SDOperand N3) {
997 // Perform various simplifications.
998 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
999 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1000 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1001 switch (Opcode) {
1002 case ISD::SELECT:
1003 if (N1C)
1004 if (N1C->getValue())
1005 return N2; // select true, X, Y -> X
1006 else
1007 return N3; // select false, X, Y -> Y
1008
1009 if (N2 == N3) return N2; // select C, X, X -> X
1010
1011 if (VT == MVT::i1) { // Boolean SELECT
1012 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001013 if (N2C->getValue()) // select C, 1, X -> C | X
1014 return getNode(ISD::OR, VT, N1, N3);
1015 else // select C, 0, X -> ~C & X
1016 return getNode(ISD::AND, VT,
1017 getNode(ISD::XOR, N1.getValueType(), N1,
1018 getConstant(1, N1.getValueType())), N3);
1019 } else if (N3C) {
1020 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1021 return getNode(ISD::OR, VT,
1022 getNode(ISD::XOR, N1.getValueType(), N1,
1023 getConstant(1, N1.getValueType())), N2);
1024 else // select C, X, 0 -> C & X
1025 return getNode(ISD::AND, VT, N1, N2);
1026 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001027
1028 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1029 return getNode(ISD::OR, VT, N1, N3);
1030 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1031 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001032 }
1033
Chris Lattner59723e92005-04-09 05:15:53 +00001034 // If this is a selectcc, check to see if we can simplify the result.
1035 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1036 if (ConstantFPSDNode *CFP =
1037 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1038 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1039 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1040 if ((SetCC->getCondition() == ISD::SETGE ||
1041 SetCC->getCondition() == ISD::SETGT) &&
1042 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1043 N3.getOperand(0) == N2)
1044 return getNode(ISD::FABS, VT, N2);
1045
1046 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1047 if ((SetCC->getCondition() == ISD::SETLT ||
1048 SetCC->getCondition() == ISD::SETLE) &&
1049 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1050 N2.getOperand(0) == N3)
1051 return getNode(ISD::FABS, VT, N3);
1052 }
Nate Begemaneea805e2005-04-13 21:23:31 +00001053 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1, A)
1054 if (ConstantSDNode *CN =
1055 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1056 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
1057 if (SetCC->getCondition() == ISD::SETLT) {
1058 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1059 MVT::ValueType AType = N2.getValueType();
1060 if (XType >= AType) {
1061 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1062 getConstant(MVT::getSizeInBits(XType)-1,
1063 TLI.getShiftAmountTy()));
1064 if (XType > AType)
1065 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1066 return getNode(ISD::AND, AType, Shift, N2);
1067 }
1068 }
Chris Lattner59723e92005-04-09 05:15:53 +00001069 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001071 case ISD::BRCOND:
1072 if (N2C)
1073 if (N2C->getValue()) // Unconditional branch
1074 return getNode(ISD::BR, MVT::Other, N1, N3);
1075 else
1076 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001077 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001078 // FIXME: figure out how to safely handle things like
1079 // int foo(int x) { return 1 << (x & 255); }
1080 // int bar() { return foo(256); }
1081#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001082 case ISD::SRA_PARTS:
1083 case ISD::SRL_PARTS:
1084 case ISD::SHL_PARTS:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001085 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1086 cast<MVTSDNode>(N3)->getExtraValueType() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001087 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001088 else if (N3.getOpcode() == ISD::AND)
1089 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1090 // If the and is only masking out bits that cannot effect the shift,
1091 // eliminate the and.
1092 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1093 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1094 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1095 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001096 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001097#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001098 }
1099
1100 SDNode *N = new SDNode(Opcode, N1, N2, N3);
1101 switch (Opcode) {
1102 default:
1103 N->setValueTypes(VT);
1104 break;
1105 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
1106 N->setValueTypes(VT, MVT::Other);
1107 break;
Chris Lattner5b359c62005-04-02 04:00:59 +00001108
1109 case ISD::SRA_PARTS:
1110 case ISD::SRL_PARTS:
1111 case ISD::SHL_PARTS: {
1112 std::vector<MVT::ValueType> V(N->getNumOperands()-1, VT);
1113 N->setValueTypes(V);
1114 break;
1115 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001116 }
1117
1118 // FIXME: memoize NODES
1119 AllNodes.push_back(N);
1120 return SDOperand(N, 0);
1121}
1122
1123SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1124 std::vector<SDOperand> &Children) {
1125 switch (Children.size()) {
1126 case 0: return getNode(Opcode, VT);
1127 case 1: return getNode(Opcode, VT, Children[0]);
1128 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
1129 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001130 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001131 }
Chris Lattneref847df2005-04-09 03:27:28 +00001132
1133 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Children[1].Val);
1134 switch (Opcode) {
1135 default: break;
1136 case ISD::BRCONDTWOWAY:
1137 if (N1C)
1138 if (N1C->getValue()) // Unconditional branch to true dest.
1139 return getNode(ISD::BR, MVT::Other, Children[0], Children[2]);
1140 else // Unconditional branch to false dest.
1141 return getNode(ISD::BR, MVT::Other, Children[0], Children[3]);
1142 break;
1143 }
1144
1145 // FIXME: MEMOIZE!!
1146 SDNode *N = new SDNode(Opcode, Children);
1147 if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
1148 N->setValueTypes(VT);
1149 } else {
1150 std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
1151 N->setValueTypes(V);
1152 }
1153 AllNodes.push_back(N);
1154 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001155}
1156
Chris Lattner2ee743f2005-01-14 22:08:15 +00001157SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner859157d2005-01-15 06:17:04 +00001158 MVT::ValueType EVT) {
1159
1160 switch (Opcode) {
1161 default: assert(0 && "Bad opcode for this accessor!");
1162 case ISD::FP_ROUND_INREG:
1163 assert(VT == N1.getValueType() && "Not an inreg round!");
1164 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1165 "Cannot FP_ROUND_INREG integer types");
1166 if (EVT == VT) return N1; // Not actually rounding
1167 assert(EVT < VT && "Not rounding down!");
Chris Lattner14723c22005-03-09 18:37:12 +00001168
1169 if (isa<ConstantFPSDNode>(N1))
1170 return getNode(ISD::FP_EXTEND, VT, getNode(ISD::FP_ROUND, EVT, N1));
Chris Lattner859157d2005-01-15 06:17:04 +00001171 break;
Chris Lattner859157d2005-01-15 06:17:04 +00001172 case ISD::SIGN_EXTEND_INREG:
1173 assert(VT == N1.getValueType() && "Not an inreg extend!");
1174 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1175 "Cannot *_EXTEND_INREG FP types");
1176 if (EVT == VT) return N1; // Not actually extending
1177 assert(EVT < VT && "Not extending!");
Chris Lattner950aa3c2005-01-16 00:17:20 +00001178
Chris Lattner0f2287b2005-04-13 02:38:18 +00001179 // Extending a constant? Just return the extended constant.
Chris Lattner14723c22005-03-09 18:37:12 +00001180 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1181 SDOperand Tmp = getNode(ISD::TRUNCATE, EVT, N1);
Chris Lattner0f2287b2005-04-13 02:38:18 +00001182 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner14723c22005-03-09 18:37:12 +00001183 }
1184
Chris Lattner950aa3c2005-01-16 00:17:20 +00001185 // If we are sign extending an extension, use the original source.
Chris Lattner0f2287b2005-04-13 02:38:18 +00001186 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1187 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
Chris Lattner950aa3c2005-01-16 00:17:20 +00001188 return N1;
Chris Lattner950aa3c2005-01-16 00:17:20 +00001189
Chris Lattner0f2287b2005-04-13 02:38:18 +00001190 // If we are sign extending a sextload, return just the load.
1191 if (N1.getOpcode() == ISD::SEXTLOAD && Opcode == ISD::SIGN_EXTEND_INREG)
Chris Lattner45278e32005-04-10 04:33:08 +00001192 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1193 return N1;
1194
Chris Lattner5a6bace2005-04-07 19:43:53 +00001195 // If we are extending the result of a setcc, and we already know the
1196 // contents of the top bits, eliminate the extension.
Chris Lattner0f2287b2005-04-13 02:38:18 +00001197 if (N1.getOpcode() == ISD::SETCC &&
1198 TLI.getSetCCResultContents() ==
1199 TargetLowering::ZeroOrNegativeOneSetCCResult)
1200 return N1;
Chris Lattner2bb6f412005-04-10 23:37:16 +00001201
1202 // If we are sign extending the result of an (and X, C) operation, and we
1203 // know the extended bits are zeros already, don't do the extend.
1204 if (N1.getOpcode() == ISD::AND)
1205 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1206 uint64_t Mask = N1C->getValue();
1207 unsigned NumBits = MVT::getSizeInBits(EVT);
Chris Lattner0f2287b2005-04-13 02:38:18 +00001208 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1209 return N1;
Chris Lattner2bb6f412005-04-10 23:37:16 +00001210 }
Chris Lattner859157d2005-01-15 06:17:04 +00001211 break;
1212 }
1213
1214 EVTStruct NN;
1215 NN.Opcode = Opcode;
1216 NN.VT = VT;
1217 NN.EVT = EVT;
1218 NN.Ops.push_back(N1);
1219
1220 SDNode *&N = MVTSDNodes[NN];
1221 if (N) return SDOperand(N, 0);
1222 N = new MVTSDNode(Opcode, VT, N1, EVT);
1223 AllNodes.push_back(N);
1224 return SDOperand(N, 0);
1225}
1226
1227SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner2ee743f2005-01-14 22:08:15 +00001228 SDOperand N2, MVT::ValueType EVT) {
1229 switch (Opcode) {
1230 default: assert(0 && "Bad opcode for this accessor!");
1231 case ISD::EXTLOAD:
1232 case ISD::SEXTLOAD:
1233 case ISD::ZEXTLOAD:
Chris Lattner0f2287b2005-04-13 02:38:18 +00001234 // If they are asking for an extending load from/to the same thing, return a
Chris Lattner2ee743f2005-01-14 22:08:15 +00001235 // normal load.
1236 if (VT == EVT)
1237 return getNode(ISD::LOAD, VT, N1, N2);
1238 assert(EVT < VT && "Should only be an extending load, not truncating!");
1239 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1240 "Cannot sign/zero extend a FP load!");
1241 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1242 "Cannot convert from FP to Int or Int -> FP!");
1243 break;
1244 }
1245
1246 EVTStruct NN;
1247 NN.Opcode = Opcode;
1248 NN.VT = VT;
1249 NN.EVT = EVT;
1250 NN.Ops.push_back(N1);
1251 NN.Ops.push_back(N2);
1252
1253 SDNode *&N = MVTSDNodes[NN];
1254 if (N) return SDOperand(N, 0);
Chris Lattner69a52152005-01-14 22:38:01 +00001255 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner2ee743f2005-01-14 22:08:15 +00001256 AllNodes.push_back(N);
1257 return SDOperand(N, 0);
1258}
1259
1260SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
1261 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
1262 switch (Opcode) {
1263 default: assert(0 && "Bad opcode for this accessor!");
1264 case ISD::TRUNCSTORE:
Chris Lattner859157d2005-01-15 06:17:04 +00001265#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1266 // If this is a truncating store of a constant, convert to the desired type
1267 // and store it instead.
1268 if (isa<Constant>(N1)) {
1269 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1270 if (isa<Constant>(Op))
1271 N1 = Op;
1272 }
1273 // Also for ConstantFP?
1274#endif
Chris Lattner2ee743f2005-01-14 22:08:15 +00001275 if (N1.getValueType() == EVT) // Normal store?
1276 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner859157d2005-01-15 06:17:04 +00001277 assert(N2.getValueType() > EVT && "Not a truncation?");
1278 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner2ee743f2005-01-14 22:08:15 +00001279 "Can't do FP-INT conversion!");
1280 break;
1281 }
1282
1283 EVTStruct NN;
1284 NN.Opcode = Opcode;
1285 NN.VT = VT;
1286 NN.EVT = EVT;
1287 NN.Ops.push_back(N1);
1288 NN.Ops.push_back(N2);
1289 NN.Ops.push_back(N3);
1290
1291 SDNode *&N = MVTSDNodes[NN];
1292 if (N) return SDOperand(N, 0);
1293 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1294 AllNodes.push_back(N);
1295 return SDOperand(N, 0);
1296}
1297
1298
Chris Lattner5c884562005-01-12 18:37:47 +00001299/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1300/// indicated value. This method ignores uses of other values defined by this
1301/// operation.
1302bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1303 assert(Value < getNumValues() && "Bad value!");
1304
1305 // If there is only one value, this is easy.
1306 if (getNumValues() == 1)
1307 return use_size() == NUses;
1308 if (Uses.size() < NUses) return false;
1309
1310 SDOperand TheValue(this, Value);
1311
1312 std::set<SDNode*> UsersHandled;
1313
1314 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1315 UI != E; ++UI) {
1316 SDNode *User = *UI;
1317 if (User->getNumOperands() == 1 ||
1318 UsersHandled.insert(User).second) // First time we've seen this?
1319 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1320 if (User->getOperand(i) == TheValue) {
1321 if (NUses == 0)
1322 return false; // too many uses
1323 --NUses;
1324 }
1325 }
1326
1327 // Found exactly the right number of uses?
1328 return NUses == 0;
1329}
1330
1331
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001332const char *SDNode::getOperationName() const {
1333 switch (getOpcode()) {
1334 default: return "<<Unknown>>";
Andrew Lenharth95762122005-03-31 21:24:06 +00001335 case ISD::PCMARKER: return "PCMarker";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001336 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001337 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001338 case ISD::Constant: return "Constant";
1339 case ISD::ConstantFP: return "ConstantFP";
1340 case ISD::GlobalAddress: return "GlobalAddress";
1341 case ISD::FrameIndex: return "FrameIndex";
1342 case ISD::BasicBlock: return "BasicBlock";
1343 case ISD::ExternalSymbol: return "ExternalSymbol";
1344 case ISD::ConstantPool: return "ConstantPoolIndex";
1345 case ISD::CopyToReg: return "CopyToReg";
1346 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001347 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001348 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001349
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001350 // Unary operators
1351 case ISD::FABS: return "fabs";
1352 case ISD::FNEG: return "fneg";
1353
1354 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001355 case ISD::ADD: return "add";
1356 case ISD::SUB: return "sub";
1357 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001358 case ISD::MULHU: return "mulhu";
1359 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001360 case ISD::SDIV: return "sdiv";
1361 case ISD::UDIV: return "udiv";
1362 case ISD::SREM: return "srem";
1363 case ISD::UREM: return "urem";
1364 case ISD::AND: return "and";
1365 case ISD::OR: return "or";
1366 case ISD::XOR: return "xor";
1367 case ISD::SHL: return "shl";
1368 case ISD::SRA: return "sra";
1369 case ISD::SRL: return "srl";
1370
1371 case ISD::SELECT: return "select";
Chris Lattner17eee182005-01-20 18:50:55 +00001372 case ISD::ADD_PARTS: return "add_parts";
1373 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001374 case ISD::SHL_PARTS: return "shl_parts";
1375 case ISD::SRA_PARTS: return "sra_parts";
1376 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001377
1378 // Conversion operators.
1379 case ISD::SIGN_EXTEND: return "sign_extend";
1380 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001381 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001382 case ISD::TRUNCATE: return "truncate";
1383 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001384 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001385 case ISD::FP_EXTEND: return "fp_extend";
1386
1387 case ISD::SINT_TO_FP: return "sint_to_fp";
1388 case ISD::UINT_TO_FP: return "uint_to_fp";
1389 case ISD::FP_TO_SINT: return "fp_to_sint";
1390 case ISD::FP_TO_UINT: return "fp_to_uint";
1391
1392 // Control flow instructions
1393 case ISD::BR: return "br";
1394 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001395 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001396 case ISD::RET: return "ret";
1397 case ISD::CALL: return "call";
1398 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1399 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1400
1401 // Other operators
1402 case ISD::LOAD: return "load";
1403 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001404 case ISD::EXTLOAD: return "extload";
1405 case ISD::SEXTLOAD: return "sextload";
1406 case ISD::ZEXTLOAD: return "zextload";
1407 case ISD::TRUNCSTORE: return "truncstore";
1408
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001409 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1410 case ISD::EXTRACT_ELEMENT: return "extract_element";
1411 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001412 case ISD::MEMSET: return "memset";
1413 case ISD::MEMCPY: return "memcpy";
1414 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001415
1416 case ISD::SETCC:
1417 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1418 switch (SetCC->getCondition()) {
1419 default: assert(0 && "Unknown setcc condition!");
1420 case ISD::SETOEQ: return "setcc:setoeq";
1421 case ISD::SETOGT: return "setcc:setogt";
1422 case ISD::SETOGE: return "setcc:setoge";
1423 case ISD::SETOLT: return "setcc:setolt";
1424 case ISD::SETOLE: return "setcc:setole";
1425 case ISD::SETONE: return "setcc:setone";
1426
1427 case ISD::SETO: return "setcc:seto";
1428 case ISD::SETUO: return "setcc:setuo";
1429 case ISD::SETUEQ: return "setcc:setue";
1430 case ISD::SETUGT: return "setcc:setugt";
1431 case ISD::SETUGE: return "setcc:setuge";
1432 case ISD::SETULT: return "setcc:setult";
1433 case ISD::SETULE: return "setcc:setule";
1434 case ISD::SETUNE: return "setcc:setune";
1435
1436 case ISD::SETEQ: return "setcc:seteq";
1437 case ISD::SETGT: return "setcc:setgt";
1438 case ISD::SETGE: return "setcc:setge";
1439 case ISD::SETLT: return "setcc:setlt";
1440 case ISD::SETLE: return "setcc:setle";
1441 case ISD::SETNE: return "setcc:setne";
1442 }
1443 }
1444}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001445
1446void SDNode::dump() const {
1447 std::cerr << (void*)this << ": ";
1448
1449 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1450 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001451 if (getValueType(i) == MVT::Other)
1452 std::cerr << "ch";
1453 else
1454 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001455 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001456 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001457
1458 std::cerr << " ";
1459 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1460 if (i) std::cerr << ", ";
1461 std::cerr << (void*)getOperand(i).Val;
1462 if (unsigned RN = getOperand(i).ResNo)
1463 std::cerr << ":" << RN;
1464 }
1465
1466 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1467 std::cerr << "<" << CSDN->getValue() << ">";
1468 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1469 std::cerr << "<" << CSDN->getValue() << ">";
1470 } else if (const GlobalAddressSDNode *GADN =
1471 dyn_cast<GlobalAddressSDNode>(this)) {
1472 std::cerr << "<";
1473 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1474 } else if (const FrameIndexSDNode *FIDN =
1475 dyn_cast<FrameIndexSDNode>(this)) {
1476 std::cerr << "<" << FIDN->getIndex() << ">";
1477 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1478 std::cerr << "<" << CP->getIndex() << ">";
1479 } else if (const BasicBlockSDNode *BBDN =
1480 dyn_cast<BasicBlockSDNode>(this)) {
1481 std::cerr << "<";
1482 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1483 if (LBB)
1484 std::cerr << LBB->getName() << " ";
1485 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001486 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001487 std::cerr << "<reg #" << C2V->getReg() << ">";
1488 } else if (const ExternalSymbolSDNode *ES =
1489 dyn_cast<ExternalSymbolSDNode>(this)) {
1490 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner8a389bb2005-01-15 21:11:37 +00001491 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1492 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001493 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001494}
1495
Chris Lattnerea946cd2005-01-09 20:38:33 +00001496static void DumpNodes(SDNode *N, unsigned indent) {
1497 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1498 if (N->getOperand(i).Val->hasOneUse())
1499 DumpNodes(N->getOperand(i).Val, indent+2);
1500 else
1501 std::cerr << "\n" << std::string(indent+2, ' ')
1502 << (void*)N->getOperand(i).Val << ": <multiple use>";
1503
1504
1505 std::cerr << "\n" << std::string(indent, ' ');
1506 N->dump();
1507}
1508
Chris Lattnerc3aae252005-01-07 07:46:32 +00001509void SelectionDAG::dump() const {
1510 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001511 std::vector<SDNode*> Nodes(AllNodes);
1512 std::sort(Nodes.begin(), Nodes.end());
1513
1514 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001515 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1516 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001517 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001518
1519 DumpNodes(getRoot().Val, 2);
1520
Chris Lattnerc3aae252005-01-07 07:46:32 +00001521 std::cerr << "\n\n";
1522}
1523