blob: c3889dd7ff81f6e15c8495abba46d52c5a1df3c1 [file] [log] [blame]
Chris Lattner061a1ea2005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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;
Misha Brukman835702a2005-04-21 22:36:52 +000063 return false;
Chris Lattnerfde3a212005-01-09 20:52:51 +000064}
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;
Misha Brukman835702a2005-04-21 22:36:52 +0000121
Chris Lattner061a1ea2005-01-07 07:46:32 +0000122 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000123
Chris Lattner061a1ea2005-01-07 07:46:32 +0000124 // 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.
Misha Brukman835702a2005-04-21 22:36:52 +0000139 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000140
141 // Combine all of the condition bits.
142 return ISD::CondCode(Op1 & Op2);
143}
144
Chris Lattner90b7c132005-01-23 04:39:44 +0000145const TargetMachine &SelectionDAG::getTarget() const {
146 return TLI.getTargetMachine();
147}
148
149
Chris Lattner9c667932005-01-07 21:09:16 +0000150/// RemoveDeadNodes - This method deletes all unreachable nodes in the
151/// SelectionDAG, including nodes (like loads) that have uses of their token
152/// chain but no other uses and no side effect. If a node is passed in as an
153/// argument, it is used as the seed for node deletion.
154void SelectionDAG::RemoveDeadNodes(SDNode *N) {
155 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
156
157 // Create a dummy node (which is not added to allnodes), that adds a reference
158 // to the root node, preventing it from being deleted.
159 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
160
161 DeleteNodeIfDead(N, &AllNodeSet);
162
163 Restart:
164 unsigned NumNodes = AllNodeSet.size();
165 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
166 I != E; ++I) {
167 // Try to delete this node.
168 DeleteNodeIfDead(*I, &AllNodeSet);
169
170 // If we actually deleted any nodes, do not use invalid iterators in
171 // AllNodeSet.
172 if (AllNodeSet.size() != NumNodes)
173 goto Restart;
174 }
175
176 // Restore AllNodes.
177 if (AllNodes.size() != NumNodes)
178 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
179
180 // If the root changed (e.g. it was a dead load, update the root).
181 setRoot(DummyNode->getOperand(0));
182
183 // Now that we are done with the dummy node, delete it.
184 DummyNode->getOperand(0).Val->removeUser(DummyNode);
185 delete DummyNode;
186}
187
188void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
189 if (!N->use_empty())
190 return;
191
192 // Okay, we really are going to delete this node. First take this out of the
193 // appropriate CSE map.
194 switch (N->getOpcode()) {
195 case ISD::Constant:
196 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
197 N->getValueType(0)));
198 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000199 case ISD::ConstantFP: {
200 union {
201 double DV;
202 uint64_t IV;
203 };
204 DV = cast<ConstantFPSDNode>(N)->getValue();
205 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner9c667932005-01-07 21:09:16 +0000206 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000207 }
Chris Lattner9c667932005-01-07 21:09:16 +0000208 case ISD::GlobalAddress:
209 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
210 break;
211 case ISD::FrameIndex:
212 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
213 break;
214 case ISD::ConstantPool:
215 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
216 break;
217 case ISD::BasicBlock:
218 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
219 break;
220 case ISD::ExternalSymbol:
221 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
222 break;
223
224 case ISD::LOAD:
225 Loads.erase(std::make_pair(N->getOperand(1),
226 std::make_pair(N->getOperand(0),
227 N->getValueType(0))));
228 break;
229 case ISD::SETCC:
230 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
231 N->getOperand(1)),
Chris Lattnera9d53f92005-01-18 19:26:36 +0000232 std::make_pair(
233 cast<SetCCSDNode>(N)->getCondition(),
234 N->getValueType(0))));
Chris Lattner9c667932005-01-07 21:09:16 +0000235 break;
Chris Lattner1001c6e2005-01-15 06:17:04 +0000236 case ISD::TRUNCSTORE:
237 case ISD::SIGN_EXTEND_INREG:
Chris Lattner1001c6e2005-01-15 06:17:04 +0000238 case ISD::FP_ROUND_INREG:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000239 case ISD::EXTLOAD:
240 case ISD::SEXTLOAD:
241 case ISD::ZEXTLOAD: {
242 EVTStruct NN;
Chris Lattner7d13eae2005-04-07 00:30:13 +0000243 NN.Opcode = N->getOpcode();
Chris Lattner3b8e7192005-01-14 22:38:01 +0000244 NN.VT = N->getValueType(0);
245 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner1001c6e2005-01-15 06:17:04 +0000246 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
247 NN.Ops.push_back(N->getOperand(i));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000248 MVTSDNodes.erase(NN);
249 break;
250 }
Chris Lattner9c667932005-01-07 21:09:16 +0000251 default:
252 if (N->getNumOperands() == 1)
253 UnaryOps.erase(std::make_pair(N->getOpcode(),
254 std::make_pair(N->getOperand(0),
255 N->getValueType(0))));
256 else if (N->getNumOperands() == 2)
257 BinaryOps.erase(std::make_pair(N->getOpcode(),
258 std::make_pair(N->getOperand(0),
259 N->getOperand(1))));
Chris Lattner566307f2005-05-14 07:42:29 +0000260 else if (N->getNumValues() == 1) {
261 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
262 OneResultNodes.erase(std::make_pair(N->getOpcode(),
263 std::make_pair(N->getValueType(0),
264 Ops)));
265 } else {
Chris Lattnerd5531332005-05-14 06:20:26 +0000266 // Remove the node from the ArbitraryNodes map.
267 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
268 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
269 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
270 std::make_pair(RV, Ops)));
271 }
Chris Lattner9c667932005-01-07 21:09:16 +0000272 break;
273 }
274
275 // Next, brutally remove the operand list.
Chris Lattner9c667932005-01-07 21:09:16 +0000276 while (!N->Operands.empty()) {
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000277 SDNode *O = N->Operands.back().Val;
Chris Lattner9c667932005-01-07 21:09:16 +0000278 N->Operands.pop_back();
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000279 O->removeUser(N);
280
281 // Now that we removed this operand, see if there are no uses of it left.
282 DeleteNodeIfDead(O, NodeSet);
Chris Lattner9c667932005-01-07 21:09:16 +0000283 }
Misha Brukman835702a2005-04-21 22:36:52 +0000284
Chris Lattner9c667932005-01-07 21:09:16 +0000285 // Remove the node from the nodes set and delete it.
286 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
287 AllNodeSet.erase(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000288
289 // Now that the node is gone, check to see if any of the operands of this node
290 // are dead now.
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000291 delete N;
Chris Lattner9c667932005-01-07 21:09:16 +0000292}
293
294
Chris Lattner600d3082003-08-11 14:57:33 +0000295SelectionDAG::~SelectionDAG() {
296 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
297 delete AllNodes[i];
298}
299
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000300SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner8c3d4092005-04-13 19:41:05 +0000301 if (Op.getValueType() == VT) return Op;
Jeff Cohen915594d2005-05-10 02:22:38 +0000302 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000303 return getNode(ISD::AND, Op.getValueType(), Op,
304 getConstant(Imm, Op.getValueType()));
305}
306
Chris Lattner061a1ea2005-01-07 07:46:32 +0000307SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
308 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
309 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000310 if (VT != MVT::i64)
311 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukman835702a2005-04-21 22:36:52 +0000312
Chris Lattner061a1ea2005-01-07 07:46:32 +0000313 SDNode *&N = Constants[std::make_pair(Val, VT)];
314 if (N) return SDOperand(N, 0);
315 N = new ConstantSDNode(Val, VT);
316 AllNodes.push_back(N);
317 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000318}
319
Chris Lattner061a1ea2005-01-07 07:46:32 +0000320SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
321 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
322 if (VT == MVT::f32)
323 Val = (float)Val; // Mask out extra precision.
324
Chris Lattner381dddc2005-02-17 20:17:32 +0000325 // Do the map lookup using the actual bit pattern for the floating point
326 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
327 // we don't have issues with SNANs.
328 union {
329 double DV;
330 uint64_t IV;
331 };
Misha Brukman835702a2005-04-21 22:36:52 +0000332
Chris Lattner381dddc2005-02-17 20:17:32 +0000333 DV = Val;
334
335 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000336 if (N) return SDOperand(N, 0);
337 N = new ConstantFPSDNode(Val, VT);
338 AllNodes.push_back(N);
339 return SDOperand(N, 0);
340}
341
342
343
344SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
345 MVT::ValueType VT) {
346 SDNode *&N = GlobalValues[GV];
347 if (N) return SDOperand(N, 0);
348 N = new GlobalAddressSDNode(GV,VT);
349 AllNodes.push_back(N);
350 return SDOperand(N, 0);
351}
352
353SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
354 SDNode *&N = FrameIndices[FI];
355 if (N) return SDOperand(N, 0);
356 N = new FrameIndexSDNode(FI, VT);
357 AllNodes.push_back(N);
358 return SDOperand(N, 0);
359}
360
361SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
362 SDNode *N = ConstantPoolIndices[CPIdx];
363 if (N) return SDOperand(N, 0);
364 N = new ConstantPoolSDNode(CPIdx, VT);
365 AllNodes.push_back(N);
366 return SDOperand(N, 0);
367}
368
369SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
370 SDNode *&N = BBNodes[MBB];
371 if (N) return SDOperand(N, 0);
372 N = new BasicBlockSDNode(MBB);
373 AllNodes.push_back(N);
374 return SDOperand(N, 0);
375}
376
377SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
378 SDNode *&N = ExternalSymbols[Sym];
379 if (N) return SDOperand(N, 0);
380 N = new ExternalSymbolSDNode(Sym, VT);
381 AllNodes.push_back(N);
382 return SDOperand(N, 0);
383}
384
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000385SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
386 SDOperand N1, SDOperand N2) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000387 // These setcc operations always fold.
388 switch (Cond) {
389 default: break;
390 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000391 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000392 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000393 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000394 }
395
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000396 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
397 uint64_t C2 = N2C->getValue();
398 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
399 uint64_t C1 = N1C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000400
Chris Lattner061a1ea2005-01-07 07:46:32 +0000401 // Sign extend the operands if required
402 if (ISD::isSignedIntSetCC(Cond)) {
403 C1 = N1C->getSignExtended();
404 C2 = N2C->getSignExtended();
405 }
406
407 switch (Cond) {
408 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000409 case ISD::SETEQ: return getConstant(C1 == C2, VT);
410 case ISD::SETNE: return getConstant(C1 != C2, VT);
411 case ISD::SETULT: return getConstant(C1 < C2, VT);
412 case ISD::SETUGT: return getConstant(C1 > C2, VT);
413 case ISD::SETULE: return getConstant(C1 <= C2, VT);
414 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
415 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
416 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
417 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
418 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000419 }
Chris Lattnerdfed7352005-04-07 18:58:54 +0000420 } else {
Chris Lattner6d40fd02005-04-18 04:30:45 +0000421 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
422 // the comparison on the input.
423 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
424 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
425
426 // If the comparison constant has bits in the upper part, the
427 // zero-extended value could never match.
428 if (C2 & (~0ULL << InSize)) {
429 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
430 switch (Cond) {
431 case ISD::SETUGT:
432 case ISD::SETUGE:
433 case ISD::SETEQ: return getConstant(0, VT);
434 case ISD::SETULT:
435 case ISD::SETULE:
436 case ISD::SETNE: return getConstant(1, VT);
437 case ISD::SETGT:
438 case ISD::SETGE:
439 // True if the sign bit of C2 is set.
440 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
441 case ISD::SETLT:
442 case ISD::SETLE:
443 // True if the sign bit of C2 isn't set.
444 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
445 default:
446 break;
447 }
448 }
449
450 // Otherwise, we can perform the comparison with the low bits.
451 switch (Cond) {
452 case ISD::SETEQ:
453 case ISD::SETNE:
454 case ISD::SETUGT:
455 case ISD::SETUGE:
456 case ISD::SETULT:
457 case ISD::SETULE:
458 return getSetCC(Cond, VT, N1.getOperand(0),
459 getConstant(C2, N1.getOperand(0).getValueType()));
460 default:
461 break; // todo, be more careful with signed comparisons
462 }
463 }
464
465
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000466 uint64_t MinVal, MaxVal;
467 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
468 if (ISD::isSignedIntSetCC(Cond)) {
469 MinVal = 1ULL << (OperandBitSize-1);
470 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
471 MaxVal = ~0ULL >> (65-OperandBitSize);
472 else
473 MaxVal = 0;
474 } else {
475 MinVal = 0;
476 MaxVal = ~0ULL >> (64-OperandBitSize);
477 }
478
479 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
480 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
481 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
482 --C2; // X >= C1 --> X > (C1-1)
483 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
484 N2 = getConstant(C2, N2.getValueType());
485 N2C = cast<ConstantSDNode>(N2.Val);
486 }
487
488 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
489 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
490 ++C2; // X <= C1 --> X < (C1+1)
491 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
492 N2 = getConstant(C2, N2.getValueType());
493 N2C = cast<ConstantSDNode>(N2.Val);
494 }
Misha Brukman835702a2005-04-21 22:36:52 +0000495
Nate Begeman80c095f2005-04-14 08:56:52 +0000496 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
497 return getConstant(0, VT); // X < MIN --> false
Misha Brukman835702a2005-04-21 22:36:52 +0000498
Nate Begeman80c095f2005-04-14 08:56:52 +0000499 // Canonicalize setgt X, Min --> setne X, Min
500 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
501 return getSetCC(ISD::SETNE, VT, N1, N2);
Misha Brukman835702a2005-04-21 22:36:52 +0000502
Chris Lattner87bd6982005-04-12 00:28:49 +0000503 // If we have setult X, 1, turn it into seteq X, 0
504 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
505 return getSetCC(ISD::SETEQ, VT, N1,
506 getConstant(MinVal, N1.getValueType()));
Nate Begeman80c095f2005-04-14 08:56:52 +0000507 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner87bd6982005-04-12 00:28:49 +0000508 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
509 return getSetCC(ISD::SETEQ, VT, N1,
510 getConstant(MaxVal, N1.getValueType()));
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000511
512 // If we have "setcc X, C1", check to see if we can shrink the immediate
513 // by changing cc.
514
515 // SETUGT X, SINTMAX -> SETLT X, 0
516 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
517 C2 == (~0ULL >> (65-OperandBitSize)))
518 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
519
520 // FIXME: Implement the rest of these.
521
Chris Lattnerab1ed772005-04-21 06:12:41 +0000522
523 // Fold bit comparisons when we can.
524 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
525 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
526 if (ConstantSDNode *AndRHS =
527 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
528 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
529 // Perform the xform if the AND RHS is a single bit.
530 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
531 return getNode(ISD::SRL, VT, N1,
532 getConstant(ExactLog2(AndRHS->getValue()),
533 TLI.getShiftAmountTy()));
534 }
535 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
536 // (X & 8) == 8 --> (X & 8) >> 3
537 // Perform the xform if C2 is a single bit.
538 if ((C2 & (C2-1)) == 0) {
539 return getNode(ISD::SRL, VT, N1,
540 getConstant(ExactLog2(C2),TLI.getShiftAmountTy()));
541 }
542 }
543 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000544 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000545 } else if (isa<ConstantSDNode>(N1.Val)) {
546 // Ensure that the constant occurs on the RHS.
547 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
548 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000549
550 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
551 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
552 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000553
Chris Lattner061a1ea2005-01-07 07:46:32 +0000554 switch (Cond) {
555 default: break; // FIXME: Implement the rest of these!
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000556 case ISD::SETEQ: return getConstant(C1 == C2, VT);
557 case ISD::SETNE: return getConstant(C1 != C2, VT);
558 case ISD::SETLT: return getConstant(C1 < C2, VT);
559 case ISD::SETGT: return getConstant(C1 > C2, VT);
560 case ISD::SETLE: return getConstant(C1 <= C2, VT);
561 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000562 }
563 } else {
564 // Ensure that the constant occurs on the RHS.
565 Cond = ISD::getSetCCSwappedOperands(Cond);
566 std::swap(N1, N2);
567 }
568
569 if (N1 == N2) {
570 // We can always fold X == Y for integer setcc's.
571 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000572 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000573 unsigned UOF = ISD::getUnorderedFlavor(Cond);
574 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000575 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen915594d2005-05-10 02:22:38 +0000576 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000577 return getConstant(UOF, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000578 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
579 // if it is not already.
580 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
581 }
582
Chris Lattnerfde3a212005-01-09 20:52:51 +0000583 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner41b76412005-01-10 02:03:02 +0000584 MVT::isInteger(N1.getValueType())) {
585 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
586 N1.getOpcode() == ISD::XOR) {
587 // Simplify (X+Y) == (X+Z) --> Y == Z
588 if (N1.getOpcode() == N2.getOpcode()) {
589 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000590 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000591 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000592 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000593 if (isCommutativeBinOp(N1.getOpcode())) {
594 // If X op Y == Y op X, try other combinations.
595 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000596 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000597 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000598 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000599 }
600 }
Chris Lattner90b7c132005-01-23 04:39:44 +0000601
602 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukman835702a2005-04-21 22:36:52 +0000603
Chris Lattner41b76412005-01-10 02:03:02 +0000604 // Simplify (X+Z) == X --> Z == 0
605 if (N1.getOperand(0) == N2)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000606 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000607 getConstant(0, N1.getValueType()));
608 if (N1.getOperand(1) == N2) {
609 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000610 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000611 getConstant(0, N1.getValueType()));
612 else {
613 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
614 // (Z-X) == X --> Z == X<<1
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000615 return getSetCC(Cond, VT, N1.getOperand(0),
Misha Brukman835702a2005-04-21 22:36:52 +0000616 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner90b7c132005-01-23 04:39:44 +0000617 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner41b76412005-01-10 02:03:02 +0000618 }
Chris Lattnerfde3a212005-01-09 20:52:51 +0000619 }
620 }
621
Chris Lattner41b76412005-01-10 02:03:02 +0000622 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
623 N2.getOpcode() == ISD::XOR) {
624 // Simplify X == (X+Z) --> Z == 0
625 if (N2.getOperand(0) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000626 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000627 getConstant(0, N2.getValueType()));
628 else if (N2.getOperand(1) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000629 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000630 getConstant(0, N2.getValueType()));
631 }
632 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000633
Chris Lattnerb61ecb52005-04-18 04:48:12 +0000634 // Fold away ALL boolean setcc's.
635 if (N1.getValueType() == MVT::i1) {
636 switch (Cond) {
637 default: assert(0 && "Unknown integer setcc!");
638 case ISD::SETEQ: // X == Y -> (X^Y)^1
639 N1 = getNode(ISD::XOR, MVT::i1,
640 getNode(ISD::XOR, MVT::i1, N1, N2),
641 getConstant(1, MVT::i1));
642 break;
643 case ISD::SETNE: // X != Y --> (X^Y)
644 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
645 break;
646 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
647 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
648 N1 = getNode(ISD::AND, MVT::i1, N2,
649 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
650 break;
651 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
652 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
653 N1 = getNode(ISD::AND, MVT::i1, N1,
654 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
655 break;
656 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
657 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
658 N1 = getNode(ISD::OR, MVT::i1, N2,
659 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
660 break;
661 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
662 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
663 N1 = getNode(ISD::OR, MVT::i1, N1,
664 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
665 break;
666 }
667 if (VT != MVT::i1)
668 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
669 return N1;
670 }
671
672
Chris Lattnera9d53f92005-01-18 19:26:36 +0000673 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
674 std::make_pair(Cond, VT))];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000675 if (N) return SDOperand(N, 0);
676 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000677 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000678 AllNodes.push_back(N);
679 return SDOperand(N, 0);
680}
681
682
683
684/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000685///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000686SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
687 SDNode *N = new SDNode(Opcode, VT);
688 AllNodes.push_back(N);
689 return SDOperand(N, 0);
690}
691
Chris Lattner061a1ea2005-01-07 07:46:32 +0000692SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
693 SDOperand Operand) {
694 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
695 uint64_t Val = C->getValue();
696 switch (Opcode) {
697 default: break;
698 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
699 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
700 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000701 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
702 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000703 }
704 }
705
706 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
707 switch (Opcode) {
Chris Lattner0ea81f92005-04-09 03:02:46 +0000708 case ISD::FNEG:
709 return getConstantFP(-C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000710 case ISD::FP_ROUND:
711 case ISD::FP_EXTEND:
712 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000713 case ISD::FP_TO_SINT:
714 return getConstant((int64_t)C->getValue(), VT);
715 case ISD::FP_TO_UINT:
716 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000717 }
718
719 unsigned OpOpcode = Operand.Val->getOpcode();
720 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000721 case ISD::TokenFactor:
722 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000723 case ISD::SIGN_EXTEND:
724 if (Operand.getValueType() == VT) return Operand; // noop extension
725 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
726 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
727 break;
728 case ISD::ZERO_EXTEND:
729 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattnerb32d9312005-04-07 19:43:53 +0000730 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000731 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000732 break;
733 case ISD::TRUNCATE:
734 if (Operand.getValueType() == VT) return Operand; // noop truncate
735 if (OpOpcode == ISD::TRUNCATE)
736 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000737 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
738 // If the source is smaller than the dest, we still need an extend.
739 if (Operand.Val->getOperand(0).getValueType() < VT)
740 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
741 else if (Operand.Val->getOperand(0).getValueType() > VT)
742 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
743 else
744 return Operand.Val->getOperand(0);
745 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000746 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +0000747 case ISD::FNEG:
748 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
749 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
750 Operand.Val->getOperand(0));
751 if (OpOpcode == ISD::FNEG) // --X -> X
752 return Operand.Val->getOperand(0);
753 break;
754 case ISD::FABS:
755 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
756 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
757 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000758 }
759
760 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
761 if (N) return SDOperand(N, 0);
762 N = new SDNode(Opcode, Operand);
763 N->setValueTypes(VT);
764 AllNodes.push_back(N);
765 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000766}
767
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000768/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
769/// this predicate to simplify operations downstream. V and Mask are known to
770/// be the same type.
771static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
772 const TargetLowering &TLI) {
773 unsigned SrcBits;
774 if (Mask == 0) return true;
Misha Brukman835702a2005-04-21 22:36:52 +0000775
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000776 // If we know the result of a setcc has the top bits zero, use this info.
777 switch (Op.getOpcode()) {
778 case ISD::UNDEF:
779 return true;
780 case ISD::Constant:
781 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
782
783 case ISD::SETCC:
Misha Brukman835702a2005-04-21 22:36:52 +0000784 return ((Mask & 1) == 0) &&
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000785 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
786
787 case ISD::ZEXTLOAD:
788 SrcBits = MVT::getSizeInBits(cast<MVTSDNode>(Op)->getExtraValueType());
789 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
790 case ISD::ZERO_EXTEND:
791 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
792 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
793
794 case ISD::AND:
795 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattnerf6302442005-04-21 06:28:15 +0000796 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000797 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
798
799 // FALL THROUGH
800 case ISD::OR:
801 case ISD::XOR:
802 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
803 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
804 case ISD::SELECT:
805 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
806 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattnerf6302442005-04-21 06:28:15 +0000807
808 case ISD::SRL:
809 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
810 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
811 uint64_t NewVal = Mask << ShAmt->getValue();
812 SrcBits = MVT::getSizeInBits(Op.getValueType());
813 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
814 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
815 }
816 return false;
817 case ISD::SHL:
818 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
819 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
820 uint64_t NewVal = Mask >> ShAmt->getValue();
821 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
822 }
823 return false;
Chris Lattnerd373ff62005-04-25 21:03:25 +0000824 // TODO we could handle some SRA cases here.
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000825 default: break;
826 }
827
828 return false;
829}
830
831
832
Chris Lattner061a1ea2005-01-07 07:46:32 +0000833SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
834 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000835#ifndef NDEBUG
836 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000837 case ISD::TokenFactor:
838 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
839 N2.getValueType() == MVT::Other && "Invalid token factor!");
840 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000841 case ISD::AND:
842 case ISD::OR:
843 case ISD::XOR:
844 case ISD::UDIV:
845 case ISD::UREM:
846 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
847 // fall through
848 case ISD::ADD:
849 case ISD::SUB:
850 case ISD::MUL:
851 case ISD::SDIV:
852 case ISD::SREM:
853 assert(N1.getValueType() == N2.getValueType() &&
854 N1.getValueType() == VT && "Binary operator types must match!");
855 break;
856
857 case ISD::SHL:
858 case ISD::SRA:
859 case ISD::SRL:
860 assert(VT == N1.getValueType() &&
861 "Shift operators return type must be the same as their first arg");
862 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000863 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000864 break;
865 default: break;
866 }
867#endif
868
Chris Lattner061a1ea2005-01-07 07:46:32 +0000869 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
870 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
871 if (N1C) {
872 if (N2C) {
873 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
874 switch (Opcode) {
875 case ISD::ADD: return getConstant(C1 + C2, VT);
876 case ISD::SUB: return getConstant(C1 - C2, VT);
877 case ISD::MUL: return getConstant(C1 * C2, VT);
878 case ISD::UDIV:
879 if (C2) return getConstant(C1 / C2, VT);
880 break;
881 case ISD::UREM :
882 if (C2) return getConstant(C1 % C2, VT);
883 break;
884 case ISD::SDIV :
885 if (C2) return getConstant(N1C->getSignExtended() /
886 N2C->getSignExtended(), VT);
887 break;
888 case ISD::SREM :
889 if (C2) return getConstant(N1C->getSignExtended() %
890 N2C->getSignExtended(), VT);
891 break;
892 case ISD::AND : return getConstant(C1 & C2, VT);
893 case ISD::OR : return getConstant(C1 | C2, VT);
894 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +0000895 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
896 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
897 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000898 default: break;
899 }
900
901 } else { // Cannonicalize constant to RHS if commutative
902 if (isCommutativeBinOp(Opcode)) {
903 std::swap(N1C, N2C);
904 std::swap(N1, N2);
905 }
906 }
Chris Lattner32a5f022005-01-19 17:29:49 +0000907
908 switch (Opcode) {
909 default: break;
910 case ISD::SHL: // shl 0, X -> 0
911 if (N1C->isNullValue()) return N1;
912 break;
913 case ISD::SRL: // srl 0, X -> 0
914 if (N1C->isNullValue()) return N1;
915 break;
916 case ISD::SRA: // sra -1, X -> -1
917 if (N1C->isAllOnesValue()) return N1;
918 break;
919 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000920 }
921
922 if (N2C) {
923 uint64_t C2 = N2C->getValue();
924
925 switch (Opcode) {
926 case ISD::ADD:
927 if (!C2) return N1; // add X, 0 -> X
928 break;
929 case ISD::SUB:
930 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner8005e912005-05-12 00:17:04 +0000931 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000932 case ISD::MUL:
933 if (!C2) return N2; // mul X, 0 -> 0
934 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
935 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
936
Chris Lattner90b7c132005-01-23 04:39:44 +0000937 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000938 if ((C2 & C2-1) == 0) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000939 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
940 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000941 }
942 break;
943
944 case ISD::UDIV:
Chris Lattner90b7c132005-01-23 04:39:44 +0000945 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000946 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000947 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
948 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000949 }
950 break;
951
Chris Lattnera86fa442005-01-11 04:25:13 +0000952 case ISD::SHL:
953 case ISD::SRL:
954 case ISD::SRA:
Nate Begemanaf1c0f72005-04-12 23:12:17 +0000955 // If the shift amount is bigger than the size of the data, then all the
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000956 // bits are shifted out. Simplify to undef.
Nate Begemanaf1c0f72005-04-12 23:12:17 +0000957 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
958 return getNode(ISD::UNDEF, N1.getValueType());
959 }
Chris Lattnera86fa442005-01-11 04:25:13 +0000960 if (C2 == 0) return N1;
Chris Lattner1ab16912005-05-09 17:06:45 +0000961
962 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
963 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
964 unsigned OpSAC = OpSA->getValue();
965 if (N1.getOpcode() == ISD::SHL) {
966 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
967 return getConstant(0, N1.getValueType());
968 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
969 getConstant(C2+OpSAC, N2.getValueType()));
970 } else if (N1.getOpcode() == ISD::SRL) {
971 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
972 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
973 getConstant(~0ULL << OpSAC, VT));
974 if (C2 > OpSAC) {
975 return getNode(ISD::SHL, VT, Mask,
976 getConstant(C2-OpSAC, N2.getValueType()));
977 } else {
978 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
979 return getNode(ISD::SRL, VT, Mask,
980 getConstant(OpSAC-C2, N2.getValueType()));
981 }
982 } else if (N1.getOpcode() == ISD::SRA) {
983 // if C1 == C2, just mask out low bits.
984 if (C2 == OpSAC)
985 return getNode(ISD::AND, VT, N1.getOperand(0),
986 getConstant(~0ULL << C2, VT));
987 }
988 }
Chris Lattnera86fa442005-01-11 04:25:13 +0000989 break;
990
Chris Lattner061a1ea2005-01-07 07:46:32 +0000991 case ISD::AND:
992 if (!C2) return N2; // X and 0 -> 0
993 if (N2C->isAllOnesValue())
Nate Begeman4ddd8162005-04-13 21:23:31 +0000994 return N1; // X and -1 -> X
Chris Lattnerda504742005-04-09 21:43:54 +0000995
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000996 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
997 return getConstant(0, VT);
998
Chris Lattnerf6302442005-04-21 06:28:15 +0000999 {
1000 uint64_t NotC2 = ~C2;
1001 if (VT != MVT::i64)
1002 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1003
1004 if (MaskedValueIsZero(N1, NotC2, TLI))
1005 return N1; // if (X & ~C2) -> 0, the and is redundant
1006 }
Chris Lattnerd929f8b2005-04-18 03:48:41 +00001007
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00001008 // FIXME: Should add a corresponding version of this for
1009 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1010 // we don't have yet.
1011
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001012 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1013 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnerda504742005-04-09 21:43:54 +00001014 // If we are masking out the part of our input that was extended, just
1015 // mask the input to the extension directly.
1016 unsigned ExtendBits =
1017 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
1018 if ((C2 & (~0ULL << ExtendBits)) == 0)
1019 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
1020 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001021 break;
1022 case ISD::OR:
1023 if (!C2)return N1; // X or 0 -> X
1024 if (N2C->isAllOnesValue())
Misha Brukman77451162005-04-22 04:01:18 +00001025 return N2; // X or -1 -> -1
Chris Lattner061a1ea2005-01-07 07:46:32 +00001026 break;
1027 case ISD::XOR:
1028 if (!C2) return N1; // X xor 0 -> X
1029 if (N2C->isAllOnesValue()) {
1030 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
1031 // !(X op Y) -> (X !op Y)
1032 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1033 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001034 SetCC->getValueType(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +00001035 SetCC->getOperand(0), SetCC->getOperand(1));
1036 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1037 SDNode *Op = N1.Val;
1038 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1039 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1040 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1041 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1042 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1043 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1044 if (Op->getOpcode() == ISD::AND)
1045 return getNode(ISD::OR, VT, LHS, RHS);
1046 return getNode(ISD::AND, VT, LHS, RHS);
1047 }
1048 }
Misha Brukman77451162005-04-22 04:01:18 +00001049 // X xor -1 -> not(x) ?
Chris Lattner061a1ea2005-01-07 07:46:32 +00001050 }
1051 break;
1052 }
1053
1054 // Reassociate ((X op C1) op C2) if possible.
1055 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1056 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +00001057 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +00001058 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1059 }
1060
1061 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1062 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
1063 if (N1CFP)
1064 if (N2CFP) {
1065 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1066 switch (Opcode) {
1067 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1068 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1069 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1070 case ISD::SDIV:
1071 if (C2) return getConstantFP(C1 / C2, VT);
1072 break;
1073 case ISD::SREM :
1074 if (C2) return getConstantFP(fmod(C1, C2), VT);
1075 break;
1076 default: break;
1077 }
1078
1079 } else { // Cannonicalize constant to RHS if commutative
1080 if (isCommutativeBinOp(Opcode)) {
1081 std::swap(N1CFP, N2CFP);
1082 std::swap(N1, N2);
1083 }
1084 }
1085
1086 // Finally, fold operations that do not require constants.
1087 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +00001088 case ISD::TokenFactor:
1089 if (N1.getOpcode() == ISD::EntryToken)
1090 return N2;
1091 if (N2.getOpcode() == ISD::EntryToken)
1092 return N1;
1093 break;
1094
Chris Lattner061a1ea2005-01-07 07:46:32 +00001095 case ISD::AND:
1096 case ISD::OR:
1097 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
1098 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
1099 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1100 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1101 ISD::CondCode Op2 = RHS->getCondition();
1102
Chris Lattnerbd22d832005-04-18 03:59:53 +00001103 if (LR == RR && isa<ConstantSDNode>(LR) &&
Chris Lattnerbd22d832005-04-18 03:59:53 +00001104 Op2 == LHS->getCondition() && MVT::isInteger(LL.getValueType())) {
Chris Lattnerf8064592005-04-25 21:20:28 +00001105 // (X != 0) | (Y != 0) -> (X|Y != 0)
1106 // (X == 0) & (Y == 0) -> (X|Y == 0)
1107 // (X < 0) | (Y < 0) -> (X|Y < 0)
1108 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1109 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1110 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1111 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
Misha Brukman835702a2005-04-21 22:36:52 +00001112 return getSetCC(Op2, VT,
Chris Lattnerbd22d832005-04-18 03:59:53 +00001113 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
Chris Lattnerf8064592005-04-25 21:20:28 +00001114
1115 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1116 // (X == -1) & (Y == -1) -> (X&Y == -1)
1117 // (X != -1) | (Y != -1) -> (X&Y != -1)
Chris Lattnercfa7ddd2005-04-26 01:18:33 +00001118 // (X > -1) | (Y > -1) -> (X&Y > -1)
Chris Lattnerf8064592005-04-25 21:20:28 +00001119 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
Chris Lattnercfa7ddd2005-04-26 01:18:33 +00001120 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1121 (Opcode == ISD::OR && Op2 == ISD::SETGT))
Chris Lattnerf8064592005-04-25 21:20:28 +00001122 return getSetCC(Op2, VT,
1123 getNode(ISD::AND, LR.getValueType(), LL, RL), LR);
1124 // (X > -1) & (Y > -1) -> (X|Y > -1)
1125 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1126 return getSetCC(Op2, VT,
1127 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
1128 }
Chris Lattnerbd22d832005-04-18 03:59:53 +00001129 }
1130
Chris Lattner061a1ea2005-01-07 07:46:32 +00001131 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1132 if (LL == RR && LR == RL) {
1133 Op2 = ISD::getSetCCSwappedOperands(Op2);
1134 goto MatchedBackwards;
1135 }
Misha Brukman835702a2005-04-21 22:36:52 +00001136
Chris Lattner061a1ea2005-01-07 07:46:32 +00001137 if (LL == RL && LR == RR) {
1138 MatchedBackwards:
1139 ISD::CondCode Result;
1140 bool isInteger = MVT::isInteger(LL.getValueType());
1141 if (Opcode == ISD::OR)
1142 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
1143 isInteger);
1144 else
1145 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
1146 isInteger);
1147 if (Result != ISD::SETCC_INVALID)
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001148 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001149 }
1150 }
Chris Lattner868d4732005-04-18 04:11:19 +00001151
1152 // and/or zext(a), zext(b) -> zext(and/or a, b)
1153 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1154 N2.getOpcode() == ISD::ZERO_EXTEND &&
1155 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1156 return getNode(ISD::ZERO_EXTEND, VT,
1157 getNode(Opcode, N1.getOperand(0).getValueType(),
1158 N1.getOperand(0), N2.getOperand(0)));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001159 break;
1160 case ISD::XOR:
1161 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1162 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00001163 case ISD::ADD:
1164 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1165 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1166 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1167 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattnerf2bff922005-04-10 04:04:49 +00001168 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1169 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1170 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1171 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1172 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1173 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Chris Lattner0ea81f92005-04-09 03:02:46 +00001174 break;
Chris Lattner3d5d5022005-01-09 20:09:57 +00001175 case ISD::SUB:
1176 if (N1.getOpcode() == ISD::ADD) {
1177 if (N1.Val->getOperand(0) == N2)
1178 return N1.Val->getOperand(1); // (A+B)-A == B
1179 if (N1.Val->getOperand(1) == N2)
1180 return N1.Val->getOperand(0); // (A+B)-B == A
1181 }
Chris Lattner0ea81f92005-04-09 03:02:46 +00001182 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1183 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattner3d5d5022005-01-09 20:09:57 +00001184 break;
Nate Begeman4ddd8162005-04-13 21:23:31 +00001185 // FIXME: figure out how to safely handle things like
1186 // int foo(int x) { return 1 << (x & 255); }
1187 // int bar() { return foo(256); }
1188#if 0
Nate Begemanca916ba2005-04-12 23:32:28 +00001189 case ISD::SHL:
1190 case ISD::SRL:
1191 case ISD::SRA:
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001192 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1193 cast<MVTSDNode>(N2)->getExtraValueType() != MVT::i1)
Nate Begemanca916ba2005-04-12 23:32:28 +00001194 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001195 else if (N2.getOpcode() == ISD::AND)
1196 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1197 // If the and is only masking out bits that cannot effect the shift,
1198 // eliminate the and.
1199 unsigned NumBits = MVT::getSizeInBits(VT);
1200 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1201 return getNode(Opcode, VT, N1, N2.getOperand(0));
1202 }
Nate Begemanca916ba2005-04-12 23:32:28 +00001203 break;
Nate Begeman4ddd8162005-04-13 21:23:31 +00001204#endif
Chris Lattner061a1ea2005-01-07 07:46:32 +00001205 }
1206
Chris Lattner724f7ee2005-05-11 18:57:39 +00001207 // Memoize this node if possible.
1208 SDNode *N;
Chris Lattner2dce7032005-05-12 23:24:06 +00001209 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner724f7ee2005-05-11 18:57:39 +00001210 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1211 if (BON) return SDOperand(BON, 0);
1212
1213 BON = N = new SDNode(Opcode, N1, N2);
1214 } else {
Chris Lattner8005e912005-05-12 00:17:04 +00001215 N = new SDNode(Opcode, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00001216 }
1217
Chris Lattner86535992005-05-14 07:45:46 +00001218 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001219 AllNodes.push_back(N);
1220 return SDOperand(N, 0);
1221}
1222
Chris Lattner8005e912005-05-12 00:17:04 +00001223// setAdjCallChain - This method changes the token chain of an
Chris Lattner2dce7032005-05-12 23:24:06 +00001224// CALLSEQ_START/END node to be the specified operand.
Chris Lattner724f7ee2005-05-11 18:57:39 +00001225void SDNode::setAdjCallChain(SDOperand N) {
1226 assert(N.getValueType() == MVT::Other);
Chris Lattner2dce7032005-05-12 23:24:06 +00001227 assert((getOpcode() == ISD::CALLSEQ_START ||
1228 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner724f7ee2005-05-11 18:57:39 +00001229
1230 Operands[0].Val->removeUser(this);
1231 Operands[0] = N;
1232 N.Val->Uses.push_back(this);
1233}
1234
1235
1236
Chris Lattner061a1ea2005-01-07 07:46:32 +00001237SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001238 SDOperand Chain, SDOperand Ptr,
1239 SDOperand SV) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001240 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1241 if (N) return SDOperand(N, 0);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001242 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001243
1244 // Loads have a token chain.
1245 N->setValueTypes(VT, MVT::Other);
1246 AllNodes.push_back(N);
1247 return SDOperand(N, 0);
1248}
1249
Chris Lattner061a1ea2005-01-07 07:46:32 +00001250SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1251 SDOperand N1, SDOperand N2, SDOperand N3) {
1252 // Perform various simplifications.
1253 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1254 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1255 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1256 switch (Opcode) {
1257 case ISD::SELECT:
1258 if (N1C)
1259 if (N1C->getValue())
1260 return N2; // select true, X, Y -> X
Misha Brukman835702a2005-04-21 22:36:52 +00001261 else
Chris Lattner061a1ea2005-01-07 07:46:32 +00001262 return N3; // select false, X, Y -> Y
1263
1264 if (N2 == N3) return N2; // select C, X, X -> X
1265
1266 if (VT == MVT::i1) { // Boolean SELECT
1267 if (N2C) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001268 if (N2C->getValue()) // select C, 1, X -> C | X
1269 return getNode(ISD::OR, VT, N1, N3);
1270 else // select C, 0, X -> ~C & X
1271 return getNode(ISD::AND, VT,
1272 getNode(ISD::XOR, N1.getValueType(), N1,
1273 getConstant(1, N1.getValueType())), N3);
1274 } else if (N3C) {
1275 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1276 return getNode(ISD::OR, VT,
1277 getNode(ISD::XOR, N1.getValueType(), N1,
1278 getConstant(1, N1.getValueType())), N2);
1279 else // select C, X, 0 -> C & X
1280 return getNode(ISD::AND, VT, N1, N2);
1281 }
Chris Lattneraf5b25f2005-04-12 02:54:39 +00001282
1283 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1284 return getNode(ISD::OR, VT, N1, N3);
1285 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1286 return getNode(ISD::AND, VT, N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001287 }
1288
Chris Lattner6a31b872005-04-09 05:15:53 +00001289 // If this is a selectcc, check to see if we can simplify the result.
1290 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1291 if (ConstantFPSDNode *CFP =
1292 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1293 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1294 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1295 if ((SetCC->getCondition() == ISD::SETGE ||
1296 SetCC->getCondition() == ISD::SETGT) &&
1297 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1298 N3.getOperand(0) == N2)
1299 return getNode(ISD::FABS, VT, N2);
1300
1301 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1302 if ((SetCC->getCondition() == ISD::SETLT ||
1303 SetCC->getCondition() == ISD::SETLE) &&
1304 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1305 N2.getOperand(0) == N3)
1306 return getNode(ISD::FABS, VT, N3);
1307 }
Chris Lattnerd2fb9ea2005-05-12 06:27:02 +00001308 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1), A
Nate Begeman4ddd8162005-04-13 21:23:31 +00001309 if (ConstantSDNode *CN =
1310 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1311 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
1312 if (SetCC->getCondition() == ISD::SETLT) {
1313 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1314 MVT::ValueType AType = N2.getValueType();
1315 if (XType >= AType) {
Chris Lattnerd2fb9ea2005-05-12 06:27:02 +00001316 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1317 // single-bit constant. FIXME: remove once the dag combiner
1318 // exists.
1319 if (ConstantSDNode *AC = dyn_cast<ConstantSDNode>(N2))
1320 if ((AC->getValue() & (AC->getValue()-1)) == 0) {
1321 unsigned ShCtV = ExactLog2(AC->getValue());
1322 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1323 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
1324 SDOperand Shift = getNode(ISD::SRL, XType,
1325 SetCC->getOperand(0), ShCt);
1326 if (XType > AType)
1327 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1328 return getNode(ISD::AND, AType, Shift, N2);
1329 }
1330
1331
Nate Begeman4ddd8162005-04-13 21:23:31 +00001332 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1333 getConstant(MVT::getSizeInBits(XType)-1,
1334 TLI.getShiftAmountTy()));
1335 if (XType > AType)
1336 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1337 return getNode(ISD::AND, AType, Shift, N2);
1338 }
1339 }
Chris Lattner6a31b872005-04-09 05:15:53 +00001340 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001341 break;
Chris Lattner5c66e452005-01-07 22:49:57 +00001342 case ISD::BRCOND:
1343 if (N2C)
1344 if (N2C->getValue()) // Unconditional branch
1345 return getNode(ISD::BR, MVT::Other, N1, N3);
1346 else
1347 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +00001348 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001349 }
1350
Chris Lattner566307f2005-05-14 07:42:29 +00001351 std::vector<SDOperand> Ops;
1352 Ops.reserve(3);
1353 Ops.push_back(N1);
1354 Ops.push_back(N2);
1355 Ops.push_back(N3);
1356
1357 // Memoize nodes.
1358 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1359 if (N) return SDOperand(N, 0);
1360
1361 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner96c262e2005-05-14 07:29:57 +00001362 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001363 AllNodes.push_back(N);
1364 return SDOperand(N, 0);
1365}
1366
1367SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001368 SDOperand N1, SDOperand N2, SDOperand N3,
1369 SDOperand N4) {
Chris Lattner833a4fb2005-05-14 07:32:14 +00001370 std::vector<SDOperand> Ops;
1371 Ops.reserve(4);
1372 Ops.push_back(N1);
1373 Ops.push_back(N2);
1374 Ops.push_back(N3);
1375 Ops.push_back(N4);
1376 return getNode(Opcode, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001377}
1378
Chris Lattnerc14f3542005-05-09 04:14:13 +00001379SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Chris Lattner57d294f2005-05-09 04:28:51 +00001380 assert((!V || isa<PointerType>(V->getType())) &&
1381 "SrcValue is not a pointer?");
Chris Lattnerc14f3542005-05-09 04:14:13 +00001382 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1383 if (N) return SDOperand(N, 0);
1384
1385 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001386 AllNodes.push_back(N);
1387 return SDOperand(N, 0);
1388}
1389
1390SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerd5531332005-05-14 06:20:26 +00001391 std::vector<SDOperand> &Ops) {
1392 switch (Ops.size()) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001393 case 0: return getNode(Opcode, VT);
Chris Lattnerd5531332005-05-14 06:20:26 +00001394 case 1: return getNode(Opcode, VT, Ops[0]);
1395 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1396 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001397 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001398 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001399
Chris Lattnerd5531332005-05-14 06:20:26 +00001400 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001401 switch (Opcode) {
1402 default: break;
1403 case ISD::BRCONDTWOWAY:
1404 if (N1C)
1405 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001406 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001407 else // Unconditional branch to false dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001408 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001409 break;
1410 }
1411
Chris Lattner566307f2005-05-14 07:42:29 +00001412 // Memoize nodes.
1413 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1414 if (N) return SDOperand(N, 0);
1415 N = new SDNode(Opcode, Ops);
Chris Lattner669e8c22005-05-14 07:25:05 +00001416 N->setValueTypes(VT);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001417 AllNodes.push_back(N);
1418 return SDOperand(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001419}
1420
Chris Lattnerd5531332005-05-14 06:20:26 +00001421SDOperand SelectionDAG::getNode(unsigned Opcode,
1422 std::vector<MVT::ValueType> &ResultTys,
1423 std::vector<SDOperand> &Ops) {
1424 if (ResultTys.size() == 1)
1425 return getNode(Opcode, ResultTys[0], Ops);
1426
Chris Lattner669e8c22005-05-14 07:25:05 +00001427 // FIXME: figure out how to safely handle things like
1428 // int foo(int x) { return 1 << (x & 255); }
1429 // int bar() { return foo(256); }
1430#if 0
1431 switch (Opcode) {
1432 case ISD::SRA_PARTS:
1433 case ISD::SRL_PARTS:
1434 case ISD::SHL_PARTS:
1435 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1436 cast<MVTSDNode>(N3)->getExtraValueType() != MVT::i1)
1437 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1438 else if (N3.getOpcode() == ISD::AND)
1439 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1440 // If the and is only masking out bits that cannot effect the shift,
1441 // eliminate the and.
1442 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1443 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1444 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1445 }
1446 break;
1447 }
1448#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00001449
1450 // Memoize the node.
1451 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1452 Ops))];
1453 if (N) return SDOperand(N, 0);
1454 N = new SDNode(Opcode, Ops);
1455 N->setValueTypes(ResultTys);
Chris Lattner006f56b2005-05-14 06:42:57 +00001456 AllNodes.push_back(N);
Chris Lattnerd5531332005-05-14 06:20:26 +00001457 return SDOperand(N, 0);
1458}
1459
1460
Chris Lattner39c67442005-01-14 22:08:15 +00001461SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner1001c6e2005-01-15 06:17:04 +00001462 MVT::ValueType EVT) {
1463
1464 switch (Opcode) {
1465 default: assert(0 && "Bad opcode for this accessor!");
1466 case ISD::FP_ROUND_INREG:
1467 assert(VT == N1.getValueType() && "Not an inreg round!");
1468 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1469 "Cannot FP_ROUND_INREG integer types");
1470 if (EVT == VT) return N1; // Not actually rounding
1471 assert(EVT < VT && "Not rounding down!");
Chris Lattner7f269462005-03-09 18:37:12 +00001472
1473 if (isa<ConstantFPSDNode>(N1))
1474 return getNode(ISD::FP_EXTEND, VT, getNode(ISD::FP_ROUND, EVT, N1));
Chris Lattner1001c6e2005-01-15 06:17:04 +00001475 break;
Chris Lattner1001c6e2005-01-15 06:17:04 +00001476 case ISD::SIGN_EXTEND_INREG:
1477 assert(VT == N1.getValueType() && "Not an inreg extend!");
1478 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1479 "Cannot *_EXTEND_INREG FP types");
1480 if (EVT == VT) return N1; // Not actually extending
1481 assert(EVT < VT && "Not extending!");
Chris Lattner0fe77762005-01-16 00:17:20 +00001482
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001483 // Extending a constant? Just return the extended constant.
Chris Lattner7f269462005-03-09 18:37:12 +00001484 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1485 SDOperand Tmp = getNode(ISD::TRUNCATE, EVT, N1);
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001486 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner7f269462005-03-09 18:37:12 +00001487 }
1488
Chris Lattner0fe77762005-01-16 00:17:20 +00001489 // If we are sign extending an extension, use the original source.
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001490 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1491 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
Chris Lattner0fe77762005-01-16 00:17:20 +00001492 return N1;
Chris Lattner0fe77762005-01-16 00:17:20 +00001493
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001494 // If we are sign extending a sextload, return just the load.
1495 if (N1.getOpcode() == ISD::SEXTLOAD && Opcode == ISD::SIGN_EXTEND_INREG)
Chris Lattnerf74c7942005-04-10 04:33:08 +00001496 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1497 return N1;
1498
Chris Lattnerb32d9312005-04-07 19:43:53 +00001499 // If we are extending the result of a setcc, and we already know the
1500 // contents of the top bits, eliminate the extension.
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001501 if (N1.getOpcode() == ISD::SETCC &&
Misha Brukman835702a2005-04-21 22:36:52 +00001502 TLI.getSetCCResultContents() ==
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001503 TargetLowering::ZeroOrNegativeOneSetCCResult)
1504 return N1;
Chris Lattnere2427c92005-04-10 23:37:16 +00001505
1506 // If we are sign extending the result of an (and X, C) operation, and we
1507 // know the extended bits are zeros already, don't do the extend.
1508 if (N1.getOpcode() == ISD::AND)
1509 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1510 uint64_t Mask = N1C->getValue();
1511 unsigned NumBits = MVT::getSizeInBits(EVT);
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001512 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1513 return N1;
Chris Lattnere2427c92005-04-10 23:37:16 +00001514 }
Chris Lattner1001c6e2005-01-15 06:17:04 +00001515 break;
1516 }
1517
1518 EVTStruct NN;
1519 NN.Opcode = Opcode;
1520 NN.VT = VT;
1521 NN.EVT = EVT;
1522 NN.Ops.push_back(N1);
1523
1524 SDNode *&N = MVTSDNodes[NN];
1525 if (N) return SDOperand(N, 0);
1526 N = new MVTSDNode(Opcode, VT, N1, EVT);
1527 AllNodes.push_back(N);
1528 return SDOperand(N, 0);
1529}
1530
1531SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001532 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
Chris Lattner39c67442005-01-14 22:08:15 +00001533 switch (Opcode) {
1534 default: assert(0 && "Bad opcode for this accessor!");
1535 case ISD::EXTLOAD:
1536 case ISD::SEXTLOAD:
1537 case ISD::ZEXTLOAD:
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001538 // If they are asking for an extending load from/to the same thing, return a
Chris Lattner39c67442005-01-14 22:08:15 +00001539 // normal load.
1540 if (VT == EVT)
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001541 return getLoad(VT, N1, N2, N3);
Chris Lattner39c67442005-01-14 22:08:15 +00001542 assert(EVT < VT && "Should only be an extending load, not truncating!");
Misha Brukman835702a2005-04-21 22:36:52 +00001543 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
Chris Lattner39c67442005-01-14 22:08:15 +00001544 "Cannot sign/zero extend a FP load!");
1545 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1546 "Cannot convert from FP to Int or Int -> FP!");
1547 break;
1548 }
1549
1550 EVTStruct NN;
1551 NN.Opcode = Opcode;
1552 NN.VT = VT;
1553 NN.EVT = EVT;
1554 NN.Ops.push_back(N1);
1555 NN.Ops.push_back(N2);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001556 NN.Ops.push_back(N3);
Chris Lattner39c67442005-01-14 22:08:15 +00001557
1558 SDNode *&N = MVTSDNodes[NN];
1559 if (N) return SDOperand(N, 0);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001560 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, N3, EVT);
Chris Lattner39c67442005-01-14 22:08:15 +00001561 AllNodes.push_back(N);
1562 return SDOperand(N, 0);
1563}
1564
1565SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner006f56b2005-05-14 06:42:57 +00001566 SDOperand N2, SDOperand N3, SDOperand N4,
1567 MVT::ValueType EVT) {
Chris Lattner39c67442005-01-14 22:08:15 +00001568 switch (Opcode) {
1569 default: assert(0 && "Bad opcode for this accessor!");
1570 case ISD::TRUNCSTORE:
Chris Lattner1001c6e2005-01-15 06:17:04 +00001571#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1572 // If this is a truncating store of a constant, convert to the desired type
1573 // and store it instead.
1574 if (isa<Constant>(N1)) {
1575 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1576 if (isa<Constant>(Op))
Misha Brukman835702a2005-04-21 22:36:52 +00001577 N1 = Op;
Chris Lattner1001c6e2005-01-15 06:17:04 +00001578 }
1579 // Also for ConstantFP?
1580#endif
Chris Lattner39c67442005-01-14 22:08:15 +00001581 if (N1.getValueType() == EVT) // Normal store?
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001582 return getNode(ISD::STORE, VT, N1, N2, N3, N4);
Chris Lattner1001c6e2005-01-15 06:17:04 +00001583 assert(N2.getValueType() > EVT && "Not a truncation?");
1584 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner39c67442005-01-14 22:08:15 +00001585 "Can't do FP-INT conversion!");
1586 break;
1587 }
1588
1589 EVTStruct NN;
1590 NN.Opcode = Opcode;
1591 NN.VT = VT;
1592 NN.EVT = EVT;
1593 NN.Ops.push_back(N1);
1594 NN.Ops.push_back(N2);
1595 NN.Ops.push_back(N3);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001596 NN.Ops.push_back(N4);
Chris Lattner39c67442005-01-14 22:08:15 +00001597
1598 SDNode *&N = MVTSDNodes[NN];
1599 if (N) return SDOperand(N, 0);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001600 N = new MVTSDNode(Opcode, VT, N1, N2, N3, N4, EVT);
Chris Lattner39c67442005-01-14 22:08:15 +00001601 AllNodes.push_back(N);
1602 return SDOperand(N, 0);
1603}
1604
1605
Chris Lattner40e79822005-01-12 18:37:47 +00001606/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1607/// indicated value. This method ignores uses of other values defined by this
1608/// operation.
1609bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1610 assert(Value < getNumValues() && "Bad value!");
1611
1612 // If there is only one value, this is easy.
1613 if (getNumValues() == 1)
1614 return use_size() == NUses;
1615 if (Uses.size() < NUses) return false;
1616
1617 SDOperand TheValue(this, Value);
1618
1619 std::set<SDNode*> UsersHandled;
1620
1621 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1622 UI != E; ++UI) {
1623 SDNode *User = *UI;
1624 if (User->getNumOperands() == 1 ||
1625 UsersHandled.insert(User).second) // First time we've seen this?
1626 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1627 if (User->getOperand(i) == TheValue) {
1628 if (NUses == 0)
1629 return false; // too many uses
1630 --NUses;
1631 }
1632 }
1633
1634 // Found exactly the right number of uses?
1635 return NUses == 0;
1636}
1637
1638
Chris Lattner9e4c7612005-01-10 23:25:25 +00001639const char *SDNode::getOperationName() const {
1640 switch (getOpcode()) {
1641 default: return "<<Unknown>>";
Andrew Lenharthdec53922005-03-31 21:24:06 +00001642 case ISD::PCMARKER: return "PCMarker";
Chris Lattner9440d6e2005-05-09 04:08:27 +00001643 case ISD::SRCVALUE: return "SrcValue";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001644 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001645 case ISD::TokenFactor: return "TokenFactor";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001646 case ISD::Constant: return "Constant";
1647 case ISD::ConstantFP: return "ConstantFP";
1648 case ISD::GlobalAddress: return "GlobalAddress";
1649 case ISD::FrameIndex: return "FrameIndex";
1650 case ISD::BasicBlock: return "BasicBlock";
1651 case ISD::ExternalSymbol: return "ExternalSymbol";
1652 case ISD::ConstantPool: return "ConstantPoolIndex";
1653 case ISD::CopyToReg: return "CopyToReg";
1654 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattnere727af02005-01-13 20:50:02 +00001655 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemancda9aa72005-04-01 22:34:39 +00001656 case ISD::UNDEF: return "undef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001657
Chris Lattnerc4a20462005-04-02 04:58:41 +00001658 // Unary operators
1659 case ISD::FABS: return "fabs";
1660 case ISD::FNEG: return "fneg";
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001661 case ISD::FSQRT: return "fsqrt";
1662 case ISD::FSIN: return "fsin";
1663 case ISD::FCOS: return "fcos";
Chris Lattnerc4a20462005-04-02 04:58:41 +00001664
1665 // Binary operators
Chris Lattner9e4c7612005-01-10 23:25:25 +00001666 case ISD::ADD: return "add";
1667 case ISD::SUB: return "sub";
1668 case ISD::MUL: return "mul";
Nate Begeman55e86252005-04-05 22:36:56 +00001669 case ISD::MULHU: return "mulhu";
1670 case ISD::MULHS: return "mulhs";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001671 case ISD::SDIV: return "sdiv";
1672 case ISD::UDIV: return "udiv";
1673 case ISD::SREM: return "srem";
1674 case ISD::UREM: return "urem";
1675 case ISD::AND: return "and";
1676 case ISD::OR: return "or";
1677 case ISD::XOR: return "xor";
1678 case ISD::SHL: return "shl";
1679 case ISD::SRA: return "sra";
1680 case ISD::SRL: return "srl";
1681
1682 case ISD::SELECT: return "select";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001683 case ISD::ADD_PARTS: return "add_parts";
1684 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner5b7bb562005-04-02 03:30:42 +00001685 case ISD::SHL_PARTS: return "shl_parts";
1686 case ISD::SRA_PARTS: return "sra_parts";
1687 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001688
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001689 // Conversion operators.
Chris Lattner9e4c7612005-01-10 23:25:25 +00001690 case ISD::SIGN_EXTEND: return "sign_extend";
1691 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001692 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001693 case ISD::TRUNCATE: return "truncate";
1694 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001695 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001696 case ISD::FP_EXTEND: return "fp_extend";
1697
1698 case ISD::SINT_TO_FP: return "sint_to_fp";
1699 case ISD::UINT_TO_FP: return "uint_to_fp";
1700 case ISD::FP_TO_SINT: return "fp_to_sint";
1701 case ISD::FP_TO_UINT: return "fp_to_uint";
1702
1703 // Control flow instructions
1704 case ISD::BR: return "br";
1705 case ISD::BRCOND: return "brcond";
Chris Lattnerb0713c72005-04-09 03:27:28 +00001706 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001707 case ISD::RET: return "ret";
1708 case ISD::CALL: return "call";
Chris Lattnerd0feb642005-05-13 18:43:43 +00001709 case ISD::TAILCALL:return "tailcall";
Chris Lattnere3677d62005-05-12 23:51:40 +00001710 case ISD::CALLSEQ_START: return "callseq_start";
1711 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001712
1713 // Other operators
1714 case ISD::LOAD: return "load";
1715 case ISD::STORE: return "store";
Chris Lattner39c67442005-01-14 22:08:15 +00001716 case ISD::EXTLOAD: return "extload";
1717 case ISD::SEXTLOAD: return "sextload";
1718 case ISD::ZEXTLOAD: return "zextload";
1719 case ISD::TRUNCSTORE: return "truncstore";
1720
Chris Lattner9e4c7612005-01-10 23:25:25 +00001721 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1722 case ISD::EXTRACT_ELEMENT: return "extract_element";
1723 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001724 case ISD::MEMSET: return "memset";
1725 case ISD::MEMCPY: return "memcpy";
1726 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001727
Chris Lattner93f4f5f2005-05-11 04:50:30 +00001728 // Bit counting
1729 case ISD::CTPOP: return "ctpop";
1730 case ISD::CTTZ: return "cttz";
1731 case ISD::CTLZ: return "ctlz";
1732
1733 // IO Intrinsics
Chris Lattner67ab9452005-05-09 20:22:17 +00001734 case ISD::READPORT: return "readport";
1735 case ISD::WRITEPORT: return "writeport";
1736 case ISD::READIO: return "readio";
1737 case ISD::WRITEIO: return "writeio";
1738
Chris Lattner9e4c7612005-01-10 23:25:25 +00001739 case ISD::SETCC:
1740 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1741 switch (SetCC->getCondition()) {
1742 default: assert(0 && "Unknown setcc condition!");
1743 case ISD::SETOEQ: return "setcc:setoeq";
1744 case ISD::SETOGT: return "setcc:setogt";
1745 case ISD::SETOGE: return "setcc:setoge";
1746 case ISD::SETOLT: return "setcc:setolt";
1747 case ISD::SETOLE: return "setcc:setole";
1748 case ISD::SETONE: return "setcc:setone";
Misha Brukman835702a2005-04-21 22:36:52 +00001749
1750 case ISD::SETO: return "setcc:seto";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001751 case ISD::SETUO: return "setcc:setuo";
1752 case ISD::SETUEQ: return "setcc:setue";
1753 case ISD::SETUGT: return "setcc:setugt";
1754 case ISD::SETUGE: return "setcc:setuge";
1755 case ISD::SETULT: return "setcc:setult";
1756 case ISD::SETULE: return "setcc:setule";
1757 case ISD::SETUNE: return "setcc:setune";
Misha Brukman835702a2005-04-21 22:36:52 +00001758
Chris Lattner9e4c7612005-01-10 23:25:25 +00001759 case ISD::SETEQ: return "setcc:seteq";
1760 case ISD::SETGT: return "setcc:setgt";
1761 case ISD::SETGE: return "setcc:setge";
1762 case ISD::SETLT: return "setcc:setlt";
1763 case ISD::SETLE: return "setcc:setle";
1764 case ISD::SETNE: return "setcc:setne";
1765 }
1766 }
1767}
Chris Lattner061a1ea2005-01-07 07:46:32 +00001768
1769void SDNode::dump() const {
1770 std::cerr << (void*)this << ": ";
1771
1772 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1773 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00001774 if (getValueType(i) == MVT::Other)
1775 std::cerr << "ch";
1776 else
1777 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001778 }
Chris Lattner9e4c7612005-01-10 23:25:25 +00001779 std::cerr << " = " << getOperationName();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001780
1781 std::cerr << " ";
1782 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1783 if (i) std::cerr << ", ";
1784 std::cerr << (void*)getOperand(i).Val;
1785 if (unsigned RN = getOperand(i).ResNo)
1786 std::cerr << ":" << RN;
1787 }
1788
1789 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1790 std::cerr << "<" << CSDN->getValue() << ">";
1791 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1792 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukman835702a2005-04-21 22:36:52 +00001793 } else if (const GlobalAddressSDNode *GADN =
Chris Lattner061a1ea2005-01-07 07:46:32 +00001794 dyn_cast<GlobalAddressSDNode>(this)) {
1795 std::cerr << "<";
1796 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukman77451162005-04-22 04:01:18 +00001797 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001798 std::cerr << "<" << FIDN->getIndex() << ">";
1799 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1800 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukman77451162005-04-22 04:01:18 +00001801 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001802 std::cerr << "<";
1803 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1804 if (LBB)
1805 std::cerr << LBB->getName() << " ";
1806 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnere727af02005-01-13 20:50:02 +00001807 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001808 std::cerr << "<reg #" << C2V->getReg() << ">";
1809 } else if (const ExternalSymbolSDNode *ES =
1810 dyn_cast<ExternalSymbolSDNode>(this)) {
1811 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner630d1932005-01-15 21:11:37 +00001812 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1813 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattner9440d6e2005-05-09 04:08:27 +00001814 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1815 if (M->getValue())
1816 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1817 else
1818 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001819 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001820}
1821
Chris Lattnere6f78822005-01-09 20:38:33 +00001822static void DumpNodes(SDNode *N, unsigned indent) {
1823 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1824 if (N->getOperand(i).Val->hasOneUse())
1825 DumpNodes(N->getOperand(i).Val, indent+2);
1826 else
1827 std::cerr << "\n" << std::string(indent+2, ' ')
1828 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukman835702a2005-04-21 22:36:52 +00001829
Chris Lattnere6f78822005-01-09 20:38:33 +00001830
1831 std::cerr << "\n" << std::string(indent, ' ');
1832 N->dump();
1833}
1834
Chris Lattner061a1ea2005-01-07 07:46:32 +00001835void SelectionDAG::dump() const {
1836 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner1270acc2005-01-09 20:26:36 +00001837 std::vector<SDNode*> Nodes(AllNodes);
1838 std::sort(Nodes.begin(), Nodes.end());
1839
1840 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001841 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1842 DumpNodes(Nodes[i], 2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001843 }
Chris Lattnere6f78822005-01-09 20:38:33 +00001844
1845 DumpNodes(getRoot().Val, 2);
1846
Chris Lattner061a1ea2005-01-07 07:46:32 +00001847 std::cerr << "\n\n";
1848}
1849