blob: 8ae96f8d4da7ea48dcf2b2e4ef5d442b3ed19049 [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;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000223 case ISD::VALUETYPE:
224 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
225 break;
Chris Lattner9c667932005-01-07 21:09:16 +0000226 case ISD::LOAD:
227 Loads.erase(std::make_pair(N->getOperand(1),
228 std::make_pair(N->getOperand(0),
229 N->getValueType(0))));
230 break;
231 case ISD::SETCC:
232 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
233 N->getOperand(1)),
Chris Lattnera9d53f92005-01-18 19:26:36 +0000234 std::make_pair(
235 cast<SetCCSDNode>(N)->getCondition(),
236 N->getValueType(0))));
Chris Lattner9c667932005-01-07 21:09:16 +0000237 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000238 case ISD::EXTLOAD:
239 case ISD::SEXTLOAD:
240 case ISD::ZEXTLOAD: {
241 EVTStruct NN;
Chris Lattner7d13eae2005-04-07 00:30:13 +0000242 NN.Opcode = N->getOpcode();
Chris Lattner3b8e7192005-01-14 22:38:01 +0000243 NN.VT = N->getValueType(0);
244 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner1001c6e2005-01-15 06:17:04 +0000245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
246 NN.Ops.push_back(N->getOperand(i));
Chris Lattner3b8e7192005-01-14 22:38:01 +0000247 MVTSDNodes.erase(NN);
248 break;
249 }
Chris Lattner9c667932005-01-07 21:09:16 +0000250 default:
251 if (N->getNumOperands() == 1)
252 UnaryOps.erase(std::make_pair(N->getOpcode(),
253 std::make_pair(N->getOperand(0),
254 N->getValueType(0))));
255 else if (N->getNumOperands() == 2)
256 BinaryOps.erase(std::make_pair(N->getOpcode(),
257 std::make_pair(N->getOperand(0),
258 N->getOperand(1))));
Chris Lattner566307f2005-05-14 07:42:29 +0000259 else if (N->getNumValues() == 1) {
260 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
261 OneResultNodes.erase(std::make_pair(N->getOpcode(),
262 std::make_pair(N->getValueType(0),
263 Ops)));
264 } else {
Chris Lattnerd5531332005-05-14 06:20:26 +0000265 // Remove the node from the ArbitraryNodes map.
266 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
267 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
268 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
269 std::make_pair(RV, Ops)));
270 }
Chris Lattner9c667932005-01-07 21:09:16 +0000271 break;
272 }
273
274 // Next, brutally remove the operand list.
Chris Lattner9c667932005-01-07 21:09:16 +0000275 while (!N->Operands.empty()) {
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000276 SDNode *O = N->Operands.back().Val;
Chris Lattner9c667932005-01-07 21:09:16 +0000277 N->Operands.pop_back();
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000278 O->removeUser(N);
279
280 // Now that we removed this operand, see if there are no uses of it left.
281 DeleteNodeIfDead(O, NodeSet);
Chris Lattner9c667932005-01-07 21:09:16 +0000282 }
Misha Brukman835702a2005-04-21 22:36:52 +0000283
Chris Lattner9c667932005-01-07 21:09:16 +0000284 // Remove the node from the nodes set and delete it.
285 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
286 AllNodeSet.erase(N);
Chris Lattner9c667932005-01-07 21:09:16 +0000287
288 // Now that the node is gone, check to see if any of the operands of this node
289 // are dead now.
Chris Lattnere0f1fe12005-01-07 23:32:00 +0000290 delete N;
Chris Lattner9c667932005-01-07 21:09:16 +0000291}
292
293
Chris Lattner600d3082003-08-11 14:57:33 +0000294SelectionDAG::~SelectionDAG() {
295 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
296 delete AllNodes[i];
297}
298
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000299SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner8c3d4092005-04-13 19:41:05 +0000300 if (Op.getValueType() == VT) return Op;
Jeff Cohen915594d2005-05-10 02:22:38 +0000301 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000302 return getNode(ISD::AND, Op.getValueType(), Op,
303 getConstant(Imm, Op.getValueType()));
304}
305
Chris Lattner061a1ea2005-01-07 07:46:32 +0000306SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
307 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
308 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000309 if (VT != MVT::i64)
310 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukman835702a2005-04-21 22:36:52 +0000311
Chris Lattner061a1ea2005-01-07 07:46:32 +0000312 SDNode *&N = Constants[std::make_pair(Val, VT)];
313 if (N) return SDOperand(N, 0);
314 N = new ConstantSDNode(Val, VT);
315 AllNodes.push_back(N);
316 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000317}
318
Chris Lattner061a1ea2005-01-07 07:46:32 +0000319SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
320 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
321 if (VT == MVT::f32)
322 Val = (float)Val; // Mask out extra precision.
323
Chris Lattner381dddc2005-02-17 20:17:32 +0000324 // Do the map lookup using the actual bit pattern for the floating point
325 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
326 // we don't have issues with SNANs.
327 union {
328 double DV;
329 uint64_t IV;
330 };
Misha Brukman835702a2005-04-21 22:36:52 +0000331
Chris Lattner381dddc2005-02-17 20:17:32 +0000332 DV = Val;
333
334 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000335 if (N) return SDOperand(N, 0);
336 N = new ConstantFPSDNode(Val, VT);
337 AllNodes.push_back(N);
338 return SDOperand(N, 0);
339}
340
341
342
343SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
344 MVT::ValueType VT) {
345 SDNode *&N = GlobalValues[GV];
346 if (N) return SDOperand(N, 0);
347 N = new GlobalAddressSDNode(GV,VT);
348 AllNodes.push_back(N);
349 return SDOperand(N, 0);
350}
351
352SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
353 SDNode *&N = FrameIndices[FI];
354 if (N) return SDOperand(N, 0);
355 N = new FrameIndexSDNode(FI, VT);
356 AllNodes.push_back(N);
357 return SDOperand(N, 0);
358}
359
360SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
361 SDNode *N = ConstantPoolIndices[CPIdx];
362 if (N) return SDOperand(N, 0);
363 N = new ConstantPoolSDNode(CPIdx, VT);
364 AllNodes.push_back(N);
365 return SDOperand(N, 0);
366}
367
368SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
369 SDNode *&N = BBNodes[MBB];
370 if (N) return SDOperand(N, 0);
371 N = new BasicBlockSDNode(MBB);
372 AllNodes.push_back(N);
373 return SDOperand(N, 0);
374}
375
Chris Lattner0b6ba902005-07-10 00:07:11 +0000376SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
377 if ((unsigned)VT >= ValueTypeNodes.size())
378 ValueTypeNodes.resize(VT+1);
379 if (ValueTypeNodes[VT] == 0) {
380 ValueTypeNodes[VT] = new VTSDNode(VT);
381 AllNodes.push_back(ValueTypeNodes[VT]);
382 }
383
384 return SDOperand(ValueTypeNodes[VT], 0);
385}
386
Chris Lattner061a1ea2005-01-07 07:46:32 +0000387SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
388 SDNode *&N = ExternalSymbols[Sym];
389 if (N) return SDOperand(N, 0);
390 N = new ExternalSymbolSDNode(Sym, VT);
391 AllNodes.push_back(N);
392 return SDOperand(N, 0);
393}
394
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000395SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
396 SDOperand N1, SDOperand N2) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000397 // These setcc operations always fold.
398 switch (Cond) {
399 default: break;
400 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000401 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000402 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000403 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000404 }
405
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000406 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
407 uint64_t C2 = N2C->getValue();
408 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
409 uint64_t C1 = N1C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000410
Chris Lattner061a1ea2005-01-07 07:46:32 +0000411 // Sign extend the operands if required
412 if (ISD::isSignedIntSetCC(Cond)) {
413 C1 = N1C->getSignExtended();
414 C2 = N2C->getSignExtended();
415 }
416
417 switch (Cond) {
418 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000419 case ISD::SETEQ: return getConstant(C1 == C2, VT);
420 case ISD::SETNE: return getConstant(C1 != C2, VT);
421 case ISD::SETULT: return getConstant(C1 < C2, VT);
422 case ISD::SETUGT: return getConstant(C1 > C2, VT);
423 case ISD::SETULE: return getConstant(C1 <= C2, VT);
424 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
425 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
426 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
427 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
428 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000429 }
Chris Lattnerdfed7352005-04-07 18:58:54 +0000430 } else {
Chris Lattner6d40fd02005-04-18 04:30:45 +0000431 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
432 // the comparison on the input.
433 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
434 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
435
436 // If the comparison constant has bits in the upper part, the
437 // zero-extended value could never match.
438 if (C2 & (~0ULL << InSize)) {
439 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
440 switch (Cond) {
441 case ISD::SETUGT:
442 case ISD::SETUGE:
443 case ISD::SETEQ: return getConstant(0, VT);
444 case ISD::SETULT:
445 case ISD::SETULE:
446 case ISD::SETNE: return getConstant(1, VT);
447 case ISD::SETGT:
448 case ISD::SETGE:
449 // True if the sign bit of C2 is set.
450 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
451 case ISD::SETLT:
452 case ISD::SETLE:
453 // True if the sign bit of C2 isn't set.
454 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
455 default:
456 break;
457 }
458 }
459
460 // Otherwise, we can perform the comparison with the low bits.
461 switch (Cond) {
462 case ISD::SETEQ:
463 case ISD::SETNE:
464 case ISD::SETUGT:
465 case ISD::SETUGE:
466 case ISD::SETULT:
467 case ISD::SETULE:
468 return getSetCC(Cond, VT, N1.getOperand(0),
469 getConstant(C2, N1.getOperand(0).getValueType()));
470 default:
471 break; // todo, be more careful with signed comparisons
472 }
473 }
474
475
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000476 uint64_t MinVal, MaxVal;
477 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
478 if (ISD::isSignedIntSetCC(Cond)) {
479 MinVal = 1ULL << (OperandBitSize-1);
480 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
481 MaxVal = ~0ULL >> (65-OperandBitSize);
482 else
483 MaxVal = 0;
484 } else {
485 MinVal = 0;
486 MaxVal = ~0ULL >> (64-OperandBitSize);
487 }
488
489 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
490 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
491 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
492 --C2; // X >= C1 --> X > (C1-1)
493 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
494 N2 = getConstant(C2, N2.getValueType());
495 N2C = cast<ConstantSDNode>(N2.Val);
496 }
497
498 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
499 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
500 ++C2; // X <= C1 --> X < (C1+1)
501 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
502 N2 = getConstant(C2, N2.getValueType());
503 N2C = cast<ConstantSDNode>(N2.Val);
504 }
Misha Brukman835702a2005-04-21 22:36:52 +0000505
Nate Begeman80c095f2005-04-14 08:56:52 +0000506 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
507 return getConstant(0, VT); // X < MIN --> false
Misha Brukman835702a2005-04-21 22:36:52 +0000508
Nate Begeman80c095f2005-04-14 08:56:52 +0000509 // Canonicalize setgt X, Min --> setne X, Min
510 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
511 return getSetCC(ISD::SETNE, VT, N1, N2);
Misha Brukman835702a2005-04-21 22:36:52 +0000512
Chris Lattner87bd6982005-04-12 00:28:49 +0000513 // If we have setult X, 1, turn it into seteq X, 0
514 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
515 return getSetCC(ISD::SETEQ, VT, N1,
516 getConstant(MinVal, N1.getValueType()));
Nate Begeman80c095f2005-04-14 08:56:52 +0000517 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner87bd6982005-04-12 00:28:49 +0000518 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
519 return getSetCC(ISD::SETEQ, VT, N1,
520 getConstant(MaxVal, N1.getValueType()));
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000521
522 // If we have "setcc X, C1", check to see if we can shrink the immediate
523 // by changing cc.
524
525 // SETUGT X, SINTMAX -> SETLT X, 0
526 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
527 C2 == (~0ULL >> (65-OperandBitSize)))
528 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
529
530 // FIXME: Implement the rest of these.
531
Chris Lattnerab1ed772005-04-21 06:12:41 +0000532
533 // Fold bit comparisons when we can.
534 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
535 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
536 if (ConstantSDNode *AndRHS =
537 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
538 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
539 // Perform the xform if the AND RHS is a single bit.
540 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
541 return getNode(ISD::SRL, VT, N1,
542 getConstant(ExactLog2(AndRHS->getValue()),
543 TLI.getShiftAmountTy()));
544 }
545 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
546 // (X & 8) == 8 --> (X & 8) >> 3
547 // Perform the xform if C2 is a single bit.
548 if ((C2 & (C2-1)) == 0) {
549 return getNode(ISD::SRL, VT, N1,
550 getConstant(ExactLog2(C2),TLI.getShiftAmountTy()));
551 }
552 }
553 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000554 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000555 } else if (isa<ConstantSDNode>(N1.Val)) {
556 // Ensure that the constant occurs on the RHS.
557 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
558 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000559
560 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
561 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
562 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000563
Chris Lattner061a1ea2005-01-07 07:46:32 +0000564 switch (Cond) {
565 default: break; // FIXME: Implement the rest of these!
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000566 case ISD::SETEQ: return getConstant(C1 == C2, VT);
567 case ISD::SETNE: return getConstant(C1 != C2, VT);
568 case ISD::SETLT: return getConstant(C1 < C2, VT);
569 case ISD::SETGT: return getConstant(C1 > C2, VT);
570 case ISD::SETLE: return getConstant(C1 <= C2, VT);
571 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000572 }
573 } else {
574 // Ensure that the constant occurs on the RHS.
575 Cond = ISD::getSetCCSwappedOperands(Cond);
576 std::swap(N1, N2);
577 }
578
579 if (N1 == N2) {
580 // We can always fold X == Y for integer setcc's.
581 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000582 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000583 unsigned UOF = ISD::getUnorderedFlavor(Cond);
584 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000585 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen915594d2005-05-10 02:22:38 +0000586 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000587 return getConstant(UOF, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000588 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
589 // if it is not already.
590 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
591 }
592
Chris Lattnerfde3a212005-01-09 20:52:51 +0000593 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner41b76412005-01-10 02:03:02 +0000594 MVT::isInteger(N1.getValueType())) {
595 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
596 N1.getOpcode() == ISD::XOR) {
597 // Simplify (X+Y) == (X+Z) --> Y == Z
598 if (N1.getOpcode() == N2.getOpcode()) {
599 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000600 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000601 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000602 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000603 if (isCommutativeBinOp(N1.getOpcode())) {
604 // If X op Y == Y op X, try other combinations.
605 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000606 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner41b76412005-01-10 02:03:02 +0000607 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000608 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner41b76412005-01-10 02:03:02 +0000609 }
610 }
Chris Lattner90b7c132005-01-23 04:39:44 +0000611
612 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukman835702a2005-04-21 22:36:52 +0000613
Chris Lattner41b76412005-01-10 02:03:02 +0000614 // Simplify (X+Z) == X --> Z == 0
615 if (N1.getOperand(0) == N2)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000616 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000617 getConstant(0, N1.getValueType()));
618 if (N1.getOperand(1) == N2) {
619 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000620 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000621 getConstant(0, N1.getValueType()));
622 else {
623 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
624 // (Z-X) == X --> Z == X<<1
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000625 return getSetCC(Cond, VT, N1.getOperand(0),
Misha Brukman835702a2005-04-21 22:36:52 +0000626 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner90b7c132005-01-23 04:39:44 +0000627 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner41b76412005-01-10 02:03:02 +0000628 }
Chris Lattnerfde3a212005-01-09 20:52:51 +0000629 }
630 }
631
Chris Lattner41b76412005-01-10 02:03:02 +0000632 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
633 N2.getOpcode() == ISD::XOR) {
634 // Simplify X == (X+Z) --> Z == 0
635 if (N2.getOperand(0) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000636 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner41b76412005-01-10 02:03:02 +0000637 getConstant(0, N2.getValueType()));
638 else if (N2.getOperand(1) == N1)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000639 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner41b76412005-01-10 02:03:02 +0000640 getConstant(0, N2.getValueType()));
641 }
642 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000643
Chris Lattnerb61ecb52005-04-18 04:48:12 +0000644 // Fold away ALL boolean setcc's.
645 if (N1.getValueType() == MVT::i1) {
646 switch (Cond) {
647 default: assert(0 && "Unknown integer setcc!");
648 case ISD::SETEQ: // X == Y -> (X^Y)^1
649 N1 = getNode(ISD::XOR, MVT::i1,
650 getNode(ISD::XOR, MVT::i1, N1, N2),
651 getConstant(1, MVT::i1));
652 break;
653 case ISD::SETNE: // X != Y --> (X^Y)
654 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
655 break;
656 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
657 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
658 N1 = getNode(ISD::AND, MVT::i1, N2,
659 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
660 break;
661 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
662 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
663 N1 = getNode(ISD::AND, MVT::i1, N1,
664 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
665 break;
666 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
667 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
668 N1 = getNode(ISD::OR, MVT::i1, N2,
669 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
670 break;
671 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
672 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
673 N1 = getNode(ISD::OR, MVT::i1, N1,
674 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
675 break;
676 }
677 if (VT != MVT::i1)
678 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
679 return N1;
680 }
681
682
Chris Lattnera9d53f92005-01-18 19:26:36 +0000683 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
684 std::make_pair(Cond, VT))];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000685 if (N) return SDOperand(N, 0);
686 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000687 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000688 AllNodes.push_back(N);
689 return SDOperand(N, 0);
690}
691
692
693
694/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000695///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000696SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
697 SDNode *N = new SDNode(Opcode, VT);
698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
700}
701
Chris Lattner061a1ea2005-01-07 07:46:32 +0000702SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
703 SDOperand Operand) {
704 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
705 uint64_t Val = C->getValue();
706 switch (Opcode) {
707 default: break;
708 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
709 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
710 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000711 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
712 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000713 }
714 }
715
716 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
717 switch (Opcode) {
Chris Lattner0ea81f92005-04-09 03:02:46 +0000718 case ISD::FNEG:
719 return getConstantFP(-C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000720 case ISD::FP_ROUND:
721 case ISD::FP_EXTEND:
722 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000723 case ISD::FP_TO_SINT:
724 return getConstant((int64_t)C->getValue(), VT);
725 case ISD::FP_TO_UINT:
726 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000727 }
728
729 unsigned OpOpcode = Operand.Val->getOpcode();
730 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000731 case ISD::TokenFactor:
732 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000733 case ISD::SIGN_EXTEND:
734 if (Operand.getValueType() == VT) return Operand; // noop extension
735 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
736 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
737 break;
738 case ISD::ZERO_EXTEND:
739 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattnerb32d9312005-04-07 19:43:53 +0000740 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000741 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000742 break;
743 case ISD::TRUNCATE:
744 if (Operand.getValueType() == VT) return Operand; // noop truncate
745 if (OpOpcode == ISD::TRUNCATE)
746 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000747 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
748 // If the source is smaller than the dest, we still need an extend.
749 if (Operand.Val->getOperand(0).getValueType() < VT)
750 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
751 else if (Operand.Val->getOperand(0).getValueType() > VT)
752 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
753 else
754 return Operand.Val->getOperand(0);
755 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000756 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +0000757 case ISD::FNEG:
758 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
759 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
760 Operand.Val->getOperand(0));
761 if (OpOpcode == ISD::FNEG) // --X -> X
762 return Operand.Val->getOperand(0);
763 break;
764 case ISD::FABS:
765 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
766 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
767 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000768 }
769
770 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
771 if (N) return SDOperand(N, 0);
772 N = new SDNode(Opcode, Operand);
773 N->setValueTypes(VT);
774 AllNodes.push_back(N);
775 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000776}
777
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000778/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
779/// this predicate to simplify operations downstream. V and Mask are known to
780/// be the same type.
781static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
782 const TargetLowering &TLI) {
783 unsigned SrcBits;
784 if (Mask == 0) return true;
Misha Brukman835702a2005-04-21 22:36:52 +0000785
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000786 // If we know the result of a setcc has the top bits zero, use this info.
787 switch (Op.getOpcode()) {
788 case ISD::UNDEF:
789 return true;
790 case ISD::Constant:
791 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
792
793 case ISD::SETCC:
Misha Brukman835702a2005-04-21 22:36:52 +0000794 return ((Mask & 1) == 0) &&
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000795 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
796
797 case ISD::ZEXTLOAD:
798 SrcBits = MVT::getSizeInBits(cast<MVTSDNode>(Op)->getExtraValueType());
799 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
800 case ISD::ZERO_EXTEND:
801 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
802 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
803
804 case ISD::AND:
805 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattnerf6302442005-04-21 06:28:15 +0000806 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000807 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
808
809 // FALL THROUGH
810 case ISD::OR:
811 case ISD::XOR:
812 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
813 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
814 case ISD::SELECT:
815 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
816 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattnerf6302442005-04-21 06:28:15 +0000817
818 case ISD::SRL:
819 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
820 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
821 uint64_t NewVal = Mask << ShAmt->getValue();
822 SrcBits = MVT::getSizeInBits(Op.getValueType());
823 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
824 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
825 }
826 return false;
827 case ISD::SHL:
828 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
829 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
830 uint64_t NewVal = Mask >> ShAmt->getValue();
831 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
832 }
833 return false;
Chris Lattnerd373ff62005-04-25 21:03:25 +0000834 // TODO we could handle some SRA cases here.
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000835 default: break;
836 }
837
838 return false;
839}
840
841
842
Chris Lattner061a1ea2005-01-07 07:46:32 +0000843SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
844 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000845#ifndef NDEBUG
846 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000847 case ISD::TokenFactor:
848 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
849 N2.getValueType() == MVT::Other && "Invalid token factor!");
850 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000851 case ISD::AND:
852 case ISD::OR:
853 case ISD::XOR:
854 case ISD::UDIV:
855 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +0000856 case ISD::MULHU:
857 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +0000858 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
859 // fall through
860 case ISD::ADD:
861 case ISD::SUB:
862 case ISD::MUL:
863 case ISD::SDIV:
864 case ISD::SREM:
865 assert(N1.getValueType() == N2.getValueType() &&
866 N1.getValueType() == VT && "Binary operator types must match!");
867 break;
868
869 case ISD::SHL:
870 case ISD::SRA:
871 case ISD::SRL:
872 assert(VT == N1.getValueType() &&
873 "Shift operators return type must be the same as their first arg");
874 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000875 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000876 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000877 case ISD::FP_ROUND_INREG: {
878 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
879 assert(VT == N1.getValueType() && "Not an inreg round!");
880 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
881 "Cannot FP_ROUND_INREG integer types");
882 assert(EVT <= VT && "Not rounding down!");
883 break;
884 }
885 case ISD::SIGN_EXTEND_INREG: {
886 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
887 assert(VT == N1.getValueType() && "Not an inreg extend!");
888 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
889 "Cannot *_EXTEND_INREG FP types");
890 assert(EVT <= VT && "Not extending!");
891 }
892
Chris Lattner4e550eb2005-01-16 02:23:22 +0000893 default: break;
894 }
895#endif
896
Chris Lattner061a1ea2005-01-07 07:46:32 +0000897 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
898 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
899 if (N1C) {
900 if (N2C) {
901 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
902 switch (Opcode) {
903 case ISD::ADD: return getConstant(C1 + C2, VT);
904 case ISD::SUB: return getConstant(C1 - C2, VT);
905 case ISD::MUL: return getConstant(C1 * C2, VT);
906 case ISD::UDIV:
907 if (C2) return getConstant(C1 / C2, VT);
908 break;
909 case ISD::UREM :
910 if (C2) return getConstant(C1 % C2, VT);
911 break;
912 case ISD::SDIV :
913 if (C2) return getConstant(N1C->getSignExtended() /
914 N2C->getSignExtended(), VT);
915 break;
916 case ISD::SREM :
917 if (C2) return getConstant(N1C->getSignExtended() %
918 N2C->getSignExtended(), VT);
919 break;
920 case ISD::AND : return getConstant(C1 & C2, VT);
921 case ISD::OR : return getConstant(C1 | C2, VT);
922 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +0000923 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
924 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
925 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000926 default: break;
927 }
928
929 } else { // Cannonicalize constant to RHS if commutative
930 if (isCommutativeBinOp(Opcode)) {
931 std::swap(N1C, N2C);
932 std::swap(N1, N2);
933 }
934 }
Chris Lattner32a5f022005-01-19 17:29:49 +0000935
936 switch (Opcode) {
937 default: break;
938 case ISD::SHL: // shl 0, X -> 0
939 if (N1C->isNullValue()) return N1;
940 break;
941 case ISD::SRL: // srl 0, X -> 0
942 if (N1C->isNullValue()) return N1;
943 break;
944 case ISD::SRA: // sra -1, X -> -1
945 if (N1C->isAllOnesValue()) return N1;
946 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000947 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
948 // Extending a constant? Just return the extended constant.
949 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
950 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner32a5f022005-01-19 17:29:49 +0000951 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000952 }
953
954 if (N2C) {
955 uint64_t C2 = N2C->getValue();
956
957 switch (Opcode) {
958 case ISD::ADD:
959 if (!C2) return N1; // add X, 0 -> X
960 break;
961 case ISD::SUB:
962 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner8005e912005-05-12 00:17:04 +0000963 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000964 case ISD::MUL:
965 if (!C2) return N2; // mul X, 0 -> 0
966 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
967 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
968
Chris Lattner90b7c132005-01-23 04:39:44 +0000969 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000970 if ((C2 & C2-1) == 0) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000971 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
972 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000973 }
974 break;
975
Chris Lattner51836bb2005-05-15 05:39:08 +0000976 case ISD::MULHU:
977 case ISD::MULHS:
978 if (!C2) return N2; // mul X, 0 -> 0
979
980 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
981 return getConstant(0, VT);
982
983 // Many others could be handled here, including -1, powers of 2, etc.
984 break;
985
Chris Lattner061a1ea2005-01-07 07:46:32 +0000986 case ISD::UDIV:
Chris Lattner90b7c132005-01-23 04:39:44 +0000987 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000988 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner90b7c132005-01-23 04:39:44 +0000989 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
990 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000991 }
992 break;
993
Chris Lattnera86fa442005-01-11 04:25:13 +0000994 case ISD::SHL:
995 case ISD::SRL:
996 case ISD::SRA:
Nate Begemanaf1c0f72005-04-12 23:12:17 +0000997 // If the shift amount is bigger than the size of the data, then all the
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000998 // bits are shifted out. Simplify to undef.
Nate Begemanaf1c0f72005-04-12 23:12:17 +0000999 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1000 return getNode(ISD::UNDEF, N1.getValueType());
1001 }
Chris Lattnera86fa442005-01-11 04:25:13 +00001002 if (C2 == 0) return N1;
Chris Lattner1ab16912005-05-09 17:06:45 +00001003
1004 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1005 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1006 unsigned OpSAC = OpSA->getValue();
1007 if (N1.getOpcode() == ISD::SHL) {
1008 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1009 return getConstant(0, N1.getValueType());
1010 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1011 getConstant(C2+OpSAC, N2.getValueType()));
1012 } else if (N1.getOpcode() == ISD::SRL) {
1013 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1014 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1015 getConstant(~0ULL << OpSAC, VT));
1016 if (C2 > OpSAC) {
1017 return getNode(ISD::SHL, VT, Mask,
1018 getConstant(C2-OpSAC, N2.getValueType()));
1019 } else {
1020 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1021 return getNode(ISD::SRL, VT, Mask,
1022 getConstant(OpSAC-C2, N2.getValueType()));
1023 }
1024 } else if (N1.getOpcode() == ISD::SRA) {
1025 // if C1 == C2, just mask out low bits.
1026 if (C2 == OpSAC)
1027 return getNode(ISD::AND, VT, N1.getOperand(0),
1028 getConstant(~0ULL << C2, VT));
1029 }
1030 }
Chris Lattnera86fa442005-01-11 04:25:13 +00001031 break;
1032
Chris Lattner061a1ea2005-01-07 07:46:32 +00001033 case ISD::AND:
1034 if (!C2) return N2; // X and 0 -> 0
1035 if (N2C->isAllOnesValue())
Nate Begeman4ddd8162005-04-13 21:23:31 +00001036 return N1; // X and -1 -> X
Chris Lattnerda504742005-04-09 21:43:54 +00001037
Chris Lattnerd929f8b2005-04-18 03:48:41 +00001038 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1039 return getConstant(0, VT);
1040
Chris Lattnerf6302442005-04-21 06:28:15 +00001041 {
1042 uint64_t NotC2 = ~C2;
1043 if (VT != MVT::i64)
1044 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1045
1046 if (MaskedValueIsZero(N1, NotC2, TLI))
1047 return N1; // if (X & ~C2) -> 0, the and is redundant
1048 }
Chris Lattnerd929f8b2005-04-18 03:48:41 +00001049
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00001050 // FIXME: Should add a corresponding version of this for
1051 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1052 // we don't have yet.
1053
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001054 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1055 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnerda504742005-04-09 21:43:54 +00001056 // If we are masking out the part of our input that was extended, just
1057 // mask the input to the extension directly.
1058 unsigned ExtendBits =
Chris Lattner0b6ba902005-07-10 00:07:11 +00001059 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnerda504742005-04-09 21:43:54 +00001060 if ((C2 & (~0ULL << ExtendBits)) == 0)
1061 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
1062 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001063 break;
1064 case ISD::OR:
1065 if (!C2)return N1; // X or 0 -> X
1066 if (N2C->isAllOnesValue())
Misha Brukman77451162005-04-22 04:01:18 +00001067 return N2; // X or -1 -> -1
Chris Lattner061a1ea2005-01-07 07:46:32 +00001068 break;
1069 case ISD::XOR:
1070 if (!C2) return N1; // X xor 0 -> X
1071 if (N2C->isAllOnesValue()) {
1072 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
1073 // !(X op Y) -> (X !op Y)
1074 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1075 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001076 SetCC->getValueType(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +00001077 SetCC->getOperand(0), SetCC->getOperand(1));
1078 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1079 SDNode *Op = N1.Val;
1080 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1081 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1082 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1083 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1084 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1085 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1086 if (Op->getOpcode() == ISD::AND)
1087 return getNode(ISD::OR, VT, LHS, RHS);
1088 return getNode(ISD::AND, VT, LHS, RHS);
1089 }
1090 }
Misha Brukman77451162005-04-22 04:01:18 +00001091 // X xor -1 -> not(x) ?
Chris Lattner061a1ea2005-01-07 07:46:32 +00001092 }
1093 break;
1094 }
1095
1096 // Reassociate ((X op C1) op C2) if possible.
1097 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1098 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +00001099 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +00001100 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1101 }
1102
1103 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1104 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001105 if (N1CFP) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001106 if (N2CFP) {
1107 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1108 switch (Opcode) {
1109 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1110 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1111 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1112 case ISD::SDIV:
1113 if (C2) return getConstantFP(C1 / C2, VT);
1114 break;
1115 case ISD::SREM :
1116 if (C2) return getConstantFP(fmod(C1, C2), VT);
1117 break;
1118 default: break;
1119 }
1120
1121 } else { // Cannonicalize constant to RHS if commutative
1122 if (isCommutativeBinOp(Opcode)) {
1123 std::swap(N1CFP, N2CFP);
1124 std::swap(N1, N2);
1125 }
1126 }
1127
Chris Lattner0b6ba902005-07-10 00:07:11 +00001128 if (Opcode == ISD::FP_ROUND_INREG)
1129 return getNode(ISD::FP_EXTEND, VT,
1130 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1131 }
1132
Chris Lattner061a1ea2005-01-07 07:46:32 +00001133 // Finally, fold operations that do not require constants.
1134 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +00001135 case ISD::TokenFactor:
1136 if (N1.getOpcode() == ISD::EntryToken)
1137 return N2;
1138 if (N2.getOpcode() == ISD::EntryToken)
1139 return N1;
1140 break;
1141
Chris Lattner061a1ea2005-01-07 07:46:32 +00001142 case ISD::AND:
1143 case ISD::OR:
1144 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
1145 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
1146 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1147 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1148 ISD::CondCode Op2 = RHS->getCondition();
1149
Chris Lattnerbd22d832005-04-18 03:59:53 +00001150 if (LR == RR && isa<ConstantSDNode>(LR) &&
Chris Lattnerbd22d832005-04-18 03:59:53 +00001151 Op2 == LHS->getCondition() && MVT::isInteger(LL.getValueType())) {
Chris Lattnerf8064592005-04-25 21:20:28 +00001152 // (X != 0) | (Y != 0) -> (X|Y != 0)
1153 // (X == 0) & (Y == 0) -> (X|Y == 0)
1154 // (X < 0) | (Y < 0) -> (X|Y < 0)
1155 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1156 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1157 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1158 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
Misha Brukman835702a2005-04-21 22:36:52 +00001159 return getSetCC(Op2, VT,
Chris Lattnerbd22d832005-04-18 03:59:53 +00001160 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
Chris Lattnerf8064592005-04-25 21:20:28 +00001161
1162 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1163 // (X == -1) & (Y == -1) -> (X&Y == -1)
1164 // (X != -1) | (Y != -1) -> (X&Y != -1)
Chris Lattnercfa7ddd2005-04-26 01:18:33 +00001165 // (X > -1) | (Y > -1) -> (X&Y > -1)
Chris Lattnerf8064592005-04-25 21:20:28 +00001166 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
Chris Lattnercfa7ddd2005-04-26 01:18:33 +00001167 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1168 (Opcode == ISD::OR && Op2 == ISD::SETGT))
Chris Lattnerf8064592005-04-25 21:20:28 +00001169 return getSetCC(Op2, VT,
1170 getNode(ISD::AND, LR.getValueType(), LL, RL), LR);
1171 // (X > -1) & (Y > -1) -> (X|Y > -1)
1172 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1173 return getSetCC(Op2, VT,
1174 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
1175 }
Chris Lattnerbd22d832005-04-18 03:59:53 +00001176 }
1177
Chris Lattner061a1ea2005-01-07 07:46:32 +00001178 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1179 if (LL == RR && LR == RL) {
1180 Op2 = ISD::getSetCCSwappedOperands(Op2);
1181 goto MatchedBackwards;
1182 }
Misha Brukman835702a2005-04-21 22:36:52 +00001183
Chris Lattner061a1ea2005-01-07 07:46:32 +00001184 if (LL == RL && LR == RR) {
1185 MatchedBackwards:
1186 ISD::CondCode Result;
1187 bool isInteger = MVT::isInteger(LL.getValueType());
1188 if (Opcode == ISD::OR)
1189 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
1190 isInteger);
1191 else
1192 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
1193 isInteger);
1194 if (Result != ISD::SETCC_INVALID)
Chris Lattnerb07e2d22005-01-18 02:52:03 +00001195 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001196 }
1197 }
Chris Lattner868d4732005-04-18 04:11:19 +00001198
1199 // and/or zext(a), zext(b) -> zext(and/or a, b)
1200 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1201 N2.getOpcode() == ISD::ZERO_EXTEND &&
1202 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1203 return getNode(ISD::ZERO_EXTEND, VT,
1204 getNode(Opcode, N1.getOperand(0).getValueType(),
1205 N1.getOperand(0), N2.getOperand(0)));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001206 break;
1207 case ISD::XOR:
1208 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1209 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00001210 case ISD::ADD:
1211 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1212 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1213 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1214 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattnerf2bff922005-04-10 04:04:49 +00001215 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1216 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1217 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1218 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1219 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1220 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begemana2e87792005-06-16 07:06:03 +00001221 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1222 !MVT::isFloatingPoint(N2.getValueType()))
1223 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner0ea81f92005-04-09 03:02:46 +00001224 break;
Chris Lattner3d5d5022005-01-09 20:09:57 +00001225 case ISD::SUB:
1226 if (N1.getOpcode() == ISD::ADD) {
Nate Begemana2e87792005-06-16 07:06:03 +00001227 if (N1.Val->getOperand(0) == N2 &&
1228 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattner3d5d5022005-01-09 20:09:57 +00001229 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begemana2e87792005-06-16 07:06:03 +00001230 if (N1.Val->getOperand(1) == N2 &&
1231 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattner3d5d5022005-01-09 20:09:57 +00001232 return N1.Val->getOperand(0); // (A+B)-B == A
1233 }
Chris Lattner0ea81f92005-04-09 03:02:46 +00001234 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1235 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattner3d5d5022005-01-09 20:09:57 +00001236 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00001237 case ISD::FP_ROUND_INREG:
1238 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1239 break;
1240 case ISD::SIGN_EXTEND_INREG: {
1241 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1242 if (EVT == VT) return N1; // Not actually extending
1243
1244 // If we are sign extending an extension, use the original source.
1245 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1246 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1247 return N1;
1248
1249 // If we are sign extending a sextload, return just the load.
1250 if (N1.getOpcode() == ISD::SEXTLOAD)
1251 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1252 return N1;
1253
1254 // If we are extending the result of a setcc, and we already know the
1255 // contents of the top bits, eliminate the extension.
1256 if (N1.getOpcode() == ISD::SETCC &&
1257 TLI.getSetCCResultContents() ==
1258 TargetLowering::ZeroOrNegativeOneSetCCResult)
1259 return N1;
1260
1261 // If we are sign extending the result of an (and X, C) operation, and we
1262 // know the extended bits are zeros already, don't do the extend.
1263 if (N1.getOpcode() == ISD::AND)
1264 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1265 uint64_t Mask = N1C->getValue();
1266 unsigned NumBits = MVT::getSizeInBits(EVT);
1267 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1268 return N1;
1269 }
1270 break;
1271 }
1272
Nate Begeman4ddd8162005-04-13 21:23:31 +00001273 // FIXME: figure out how to safely handle things like
1274 // int foo(int x) { return 1 << (x & 255); }
1275 // int bar() { return foo(256); }
1276#if 0
Nate Begemanca916ba2005-04-12 23:32:28 +00001277 case ISD::SHL:
1278 case ISD::SRL:
1279 case ISD::SRA:
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001280 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00001281 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemanca916ba2005-04-12 23:32:28 +00001282 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001283 else if (N2.getOpcode() == ISD::AND)
1284 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1285 // If the and is only masking out bits that cannot effect the shift,
1286 // eliminate the and.
1287 unsigned NumBits = MVT::getSizeInBits(VT);
1288 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1289 return getNode(Opcode, VT, N1, N2.getOperand(0));
1290 }
Nate Begemanca916ba2005-04-12 23:32:28 +00001291 break;
Nate Begeman4ddd8162005-04-13 21:23:31 +00001292#endif
Chris Lattner061a1ea2005-01-07 07:46:32 +00001293 }
1294
Chris Lattner724f7ee2005-05-11 18:57:39 +00001295 // Memoize this node if possible.
1296 SDNode *N;
Chris Lattner2dce7032005-05-12 23:24:06 +00001297 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner724f7ee2005-05-11 18:57:39 +00001298 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1299 if (BON) return SDOperand(BON, 0);
1300
1301 BON = N = new SDNode(Opcode, N1, N2);
1302 } else {
Chris Lattner8005e912005-05-12 00:17:04 +00001303 N = new SDNode(Opcode, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00001304 }
1305
Chris Lattner86535992005-05-14 07:45:46 +00001306 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001307 AllNodes.push_back(N);
1308 return SDOperand(N, 0);
1309}
1310
Chris Lattner8005e912005-05-12 00:17:04 +00001311// setAdjCallChain - This method changes the token chain of an
Chris Lattner2dce7032005-05-12 23:24:06 +00001312// CALLSEQ_START/END node to be the specified operand.
Chris Lattner724f7ee2005-05-11 18:57:39 +00001313void SDNode::setAdjCallChain(SDOperand N) {
1314 assert(N.getValueType() == MVT::Other);
Chris Lattner2dce7032005-05-12 23:24:06 +00001315 assert((getOpcode() == ISD::CALLSEQ_START ||
1316 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner724f7ee2005-05-11 18:57:39 +00001317
1318 Operands[0].Val->removeUser(this);
1319 Operands[0] = N;
1320 N.Val->Uses.push_back(this);
1321}
1322
1323
1324
Chris Lattner061a1ea2005-01-07 07:46:32 +00001325SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001326 SDOperand Chain, SDOperand Ptr,
1327 SDOperand SV) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001328 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1329 if (N) return SDOperand(N, 0);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001330 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001331
1332 // Loads have a token chain.
1333 N->setValueTypes(VT, MVT::Other);
1334 AllNodes.push_back(N);
1335 return SDOperand(N, 0);
1336}
1337
Chris Lattner061a1ea2005-01-07 07:46:32 +00001338SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1339 SDOperand N1, SDOperand N2, SDOperand N3) {
1340 // Perform various simplifications.
1341 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1342 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1343 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1344 switch (Opcode) {
1345 case ISD::SELECT:
1346 if (N1C)
1347 if (N1C->getValue())
1348 return N2; // select true, X, Y -> X
Misha Brukman835702a2005-04-21 22:36:52 +00001349 else
Chris Lattner061a1ea2005-01-07 07:46:32 +00001350 return N3; // select false, X, Y -> Y
1351
1352 if (N2 == N3) return N2; // select C, X, X -> X
1353
1354 if (VT == MVT::i1) { // Boolean SELECT
1355 if (N2C) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001356 if (N2C->getValue()) // select C, 1, X -> C | X
1357 return getNode(ISD::OR, VT, N1, N3);
1358 else // select C, 0, X -> ~C & X
1359 return getNode(ISD::AND, VT,
1360 getNode(ISD::XOR, N1.getValueType(), N1,
1361 getConstant(1, N1.getValueType())), N3);
1362 } else if (N3C) {
1363 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1364 return getNode(ISD::OR, VT,
1365 getNode(ISD::XOR, N1.getValueType(), N1,
1366 getConstant(1, N1.getValueType())), N2);
1367 else // select C, X, 0 -> C & X
1368 return getNode(ISD::AND, VT, N1, N2);
1369 }
Chris Lattneraf5b25f2005-04-12 02:54:39 +00001370
1371 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1372 return getNode(ISD::OR, VT, N1, N3);
1373 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1374 return getNode(ISD::AND, VT, N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001375 }
1376
Chris Lattner6a31b872005-04-09 05:15:53 +00001377 // If this is a selectcc, check to see if we can simplify the result.
1378 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1379 if (ConstantFPSDNode *CFP =
1380 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1381 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1382 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1383 if ((SetCC->getCondition() == ISD::SETGE ||
1384 SetCC->getCondition() == ISD::SETGT) &&
1385 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1386 N3.getOperand(0) == N2)
1387 return getNode(ISD::FABS, VT, N2);
1388
1389 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1390 if ((SetCC->getCondition() == ISD::SETLT ||
1391 SetCC->getCondition() == ISD::SETLE) &&
1392 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1393 N2.getOperand(0) == N3)
1394 return getNode(ISD::FABS, VT, N3);
1395 }
Chris Lattnerd2fb9ea2005-05-12 06:27:02 +00001396 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1), A
Nate Begeman4ddd8162005-04-13 21:23:31 +00001397 if (ConstantSDNode *CN =
1398 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1399 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
1400 if (SetCC->getCondition() == ISD::SETLT) {
1401 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1402 MVT::ValueType AType = N2.getValueType();
1403 if (XType >= AType) {
Chris Lattnerd2fb9ea2005-05-12 06:27:02 +00001404 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1405 // single-bit constant. FIXME: remove once the dag combiner
1406 // exists.
1407 if (ConstantSDNode *AC = dyn_cast<ConstantSDNode>(N2))
1408 if ((AC->getValue() & (AC->getValue()-1)) == 0) {
1409 unsigned ShCtV = ExactLog2(AC->getValue());
1410 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1411 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
1412 SDOperand Shift = getNode(ISD::SRL, XType,
1413 SetCC->getOperand(0), ShCt);
1414 if (XType > AType)
1415 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1416 return getNode(ISD::AND, AType, Shift, N2);
1417 }
1418
1419
Nate Begeman4ddd8162005-04-13 21:23:31 +00001420 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1421 getConstant(MVT::getSizeInBits(XType)-1,
1422 TLI.getShiftAmountTy()));
1423 if (XType > AType)
1424 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1425 return getNode(ISD::AND, AType, Shift, N2);
1426 }
1427 }
Chris Lattner6a31b872005-04-09 05:15:53 +00001428 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001429 break;
Chris Lattner5c66e452005-01-07 22:49:57 +00001430 case ISD::BRCOND:
1431 if (N2C)
1432 if (N2C->getValue()) // Unconditional branch
1433 return getNode(ISD::BR, MVT::Other, N1, N3);
1434 else
1435 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +00001436 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001437 }
1438
Chris Lattner566307f2005-05-14 07:42:29 +00001439 std::vector<SDOperand> Ops;
1440 Ops.reserve(3);
1441 Ops.push_back(N1);
1442 Ops.push_back(N2);
1443 Ops.push_back(N3);
1444
1445 // Memoize nodes.
1446 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1447 if (N) return SDOperand(N, 0);
1448
1449 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner96c262e2005-05-14 07:29:57 +00001450 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001451 AllNodes.push_back(N);
1452 return SDOperand(N, 0);
1453}
1454
1455SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001456 SDOperand N1, SDOperand N2, SDOperand N3,
1457 SDOperand N4) {
Chris Lattner833a4fb2005-05-14 07:32:14 +00001458 std::vector<SDOperand> Ops;
1459 Ops.reserve(4);
1460 Ops.push_back(N1);
1461 Ops.push_back(N2);
1462 Ops.push_back(N3);
1463 Ops.push_back(N4);
1464 return getNode(Opcode, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001465}
1466
Chris Lattner36db1ed2005-07-10 00:29:18 +00001467SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1468 SDOperand N1, SDOperand N2, SDOperand N3,
1469 SDOperand N4, SDOperand N5) {
1470 std::vector<SDOperand> Ops;
1471 Ops.reserve(5);
1472 Ops.push_back(N1);
1473 Ops.push_back(N2);
1474 Ops.push_back(N3);
1475 Ops.push_back(N4);
1476 Ops.push_back(N5);
1477 return getNode(Opcode, VT, Ops);
1478}
1479
1480
Chris Lattnerc14f3542005-05-09 04:14:13 +00001481SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth2edc1882005-06-29 18:54:02 +00001482 assert((!V || isa<PointerType>(V->getType())) &&
1483 "SrcValue is not a pointer?");
Chris Lattnerc14f3542005-05-09 04:14:13 +00001484 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1485 if (N) return SDOperand(N, 0);
1486
1487 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001488 AllNodes.push_back(N);
1489 return SDOperand(N, 0);
1490}
1491
1492SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerd5531332005-05-14 06:20:26 +00001493 std::vector<SDOperand> &Ops) {
1494 switch (Ops.size()) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001495 case 0: return getNode(Opcode, VT);
Chris Lattnerd5531332005-05-14 06:20:26 +00001496 case 1: return getNode(Opcode, VT, Ops[0]);
1497 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1498 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001499 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001500 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001501
Chris Lattnerd5531332005-05-14 06:20:26 +00001502 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001503 switch (Opcode) {
1504 default: break;
1505 case ISD::BRCONDTWOWAY:
1506 if (N1C)
1507 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001508 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001509 else // Unconditional branch to false dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001510 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001511 break;
Chris Lattner36db1ed2005-07-10 00:29:18 +00001512
1513 case ISD::TRUNCSTORE: {
1514 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1515 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1516#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1517 // If this is a truncating store of a constant, convert to the desired type
1518 // and store it instead.
1519 if (isa<Constant>(Ops[0])) {
1520 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1521 if (isa<Constant>(Op))
1522 N1 = Op;
1523 }
1524 // Also for ConstantFP?
1525#endif
1526 if (Ops[0].getValueType() == EVT) // Normal store?
1527 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1528 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1529 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1530 "Can't do FP-INT conversion!");
1531 break;
1532 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001533 }
1534
Chris Lattner566307f2005-05-14 07:42:29 +00001535 // Memoize nodes.
1536 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1537 if (N) return SDOperand(N, 0);
1538 N = new SDNode(Opcode, Ops);
Chris Lattner669e8c22005-05-14 07:25:05 +00001539 N->setValueTypes(VT);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001540 AllNodes.push_back(N);
1541 return SDOperand(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001542}
1543
Chris Lattnerd5531332005-05-14 06:20:26 +00001544SDOperand SelectionDAG::getNode(unsigned Opcode,
1545 std::vector<MVT::ValueType> &ResultTys,
1546 std::vector<SDOperand> &Ops) {
1547 if (ResultTys.size() == 1)
1548 return getNode(Opcode, ResultTys[0], Ops);
1549
Chris Lattner669e8c22005-05-14 07:25:05 +00001550 // FIXME: figure out how to safely handle things like
1551 // int foo(int x) { return 1 << (x & 255); }
1552 // int bar() { return foo(256); }
1553#if 0
1554 switch (Opcode) {
1555 case ISD::SRA_PARTS:
1556 case ISD::SRL_PARTS:
1557 case ISD::SHL_PARTS:
1558 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00001559 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattner669e8c22005-05-14 07:25:05 +00001560 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1561 else if (N3.getOpcode() == ISD::AND)
1562 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1563 // If the and is only masking out bits that cannot effect the shift,
1564 // eliminate the and.
1565 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1566 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1567 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1568 }
1569 break;
1570 }
1571#endif
Chris Lattnerd5531332005-05-14 06:20:26 +00001572
1573 // Memoize the node.
1574 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1575 Ops))];
1576 if (N) return SDOperand(N, 0);
1577 N = new SDNode(Opcode, Ops);
1578 N->setValueTypes(ResultTys);
Chris Lattner006f56b2005-05-14 06:42:57 +00001579 AllNodes.push_back(N);
Chris Lattnerd5531332005-05-14 06:20:26 +00001580 return SDOperand(N, 0);
1581}
1582
1583
Chris Lattner39c67442005-01-14 22:08:15 +00001584SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner1001c6e2005-01-15 06:17:04 +00001585 MVT::ValueType EVT) {
Chris Lattner1001c6e2005-01-15 06:17:04 +00001586 EVTStruct NN;
1587 NN.Opcode = Opcode;
1588 NN.VT = VT;
1589 NN.EVT = EVT;
1590 NN.Ops.push_back(N1);
1591
1592 SDNode *&N = MVTSDNodes[NN];
1593 if (N) return SDOperand(N, 0);
1594 N = new MVTSDNode(Opcode, VT, N1, EVT);
1595 AllNodes.push_back(N);
1596 return SDOperand(N, 0);
1597}
1598
1599SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001600 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
Chris Lattner39c67442005-01-14 22:08:15 +00001601 switch (Opcode) {
1602 default: assert(0 && "Bad opcode for this accessor!");
1603 case ISD::EXTLOAD:
1604 case ISD::SEXTLOAD:
1605 case ISD::ZEXTLOAD:
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001606 // If they are asking for an extending load from/to the same thing, return a
Chris Lattner39c67442005-01-14 22:08:15 +00001607 // normal load.
1608 if (VT == EVT)
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001609 return getLoad(VT, N1, N2, N3);
Chris Lattner39c67442005-01-14 22:08:15 +00001610 assert(EVT < VT && "Should only be an extending load, not truncating!");
Misha Brukman835702a2005-04-21 22:36:52 +00001611 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
Chris Lattner39c67442005-01-14 22:08:15 +00001612 "Cannot sign/zero extend a FP load!");
1613 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1614 "Cannot convert from FP to Int or Int -> FP!");
1615 break;
1616 }
1617
1618 EVTStruct NN;
1619 NN.Opcode = Opcode;
1620 NN.VT = VT;
1621 NN.EVT = EVT;
1622 NN.Ops.push_back(N1);
1623 NN.Ops.push_back(N2);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001624 NN.Ops.push_back(N3);
Chris Lattner39c67442005-01-14 22:08:15 +00001625
1626 SDNode *&N = MVTSDNodes[NN];
1627 if (N) return SDOperand(N, 0);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001628 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, N3, EVT);
Chris Lattner39c67442005-01-14 22:08:15 +00001629 AllNodes.push_back(N);
1630 return SDOperand(N, 0);
1631}
1632
Chris Lattner40e79822005-01-12 18:37:47 +00001633/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1634/// indicated value. This method ignores uses of other values defined by this
1635/// operation.
1636bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1637 assert(Value < getNumValues() && "Bad value!");
1638
1639 // If there is only one value, this is easy.
1640 if (getNumValues() == 1)
1641 return use_size() == NUses;
1642 if (Uses.size() < NUses) return false;
1643
1644 SDOperand TheValue(this, Value);
1645
1646 std::set<SDNode*> UsersHandled;
1647
1648 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1649 UI != E; ++UI) {
1650 SDNode *User = *UI;
1651 if (User->getNumOperands() == 1 ||
1652 UsersHandled.insert(User).second) // First time we've seen this?
1653 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1654 if (User->getOperand(i) == TheValue) {
1655 if (NUses == 0)
1656 return false; // too many uses
1657 --NUses;
1658 }
1659 }
1660
1661 // Found exactly the right number of uses?
1662 return NUses == 0;
1663}
1664
1665
Chris Lattner9e4c7612005-01-10 23:25:25 +00001666const char *SDNode::getOperationName() const {
1667 switch (getOpcode()) {
1668 default: return "<<Unknown>>";
Andrew Lenharthdec53922005-03-31 21:24:06 +00001669 case ISD::PCMARKER: return "PCMarker";
Chris Lattner9440d6e2005-05-09 04:08:27 +00001670 case ISD::SRCVALUE: return "SrcValue";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001671 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001672 case ISD::TokenFactor: return "TokenFactor";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001673 case ISD::Constant: return "Constant";
1674 case ISD::ConstantFP: return "ConstantFP";
1675 case ISD::GlobalAddress: return "GlobalAddress";
1676 case ISD::FrameIndex: return "FrameIndex";
1677 case ISD::BasicBlock: return "BasicBlock";
1678 case ISD::ExternalSymbol: return "ExternalSymbol";
1679 case ISD::ConstantPool: return "ConstantPoolIndex";
1680 case ISD::CopyToReg: return "CopyToReg";
1681 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattnere727af02005-01-13 20:50:02 +00001682 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemancda9aa72005-04-01 22:34:39 +00001683 case ISD::UNDEF: return "undef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001684
Chris Lattnerc4a20462005-04-02 04:58:41 +00001685 // Unary operators
1686 case ISD::FABS: return "fabs";
1687 case ISD::FNEG: return "fneg";
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001688 case ISD::FSQRT: return "fsqrt";
1689 case ISD::FSIN: return "fsin";
1690 case ISD::FCOS: return "fcos";
Chris Lattnerc4a20462005-04-02 04:58:41 +00001691
1692 // Binary operators
Chris Lattner9e4c7612005-01-10 23:25:25 +00001693 case ISD::ADD: return "add";
1694 case ISD::SUB: return "sub";
1695 case ISD::MUL: return "mul";
Nate Begeman55e86252005-04-05 22:36:56 +00001696 case ISD::MULHU: return "mulhu";
1697 case ISD::MULHS: return "mulhs";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001698 case ISD::SDIV: return "sdiv";
1699 case ISD::UDIV: return "udiv";
1700 case ISD::SREM: return "srem";
1701 case ISD::UREM: return "urem";
1702 case ISD::AND: return "and";
1703 case ISD::OR: return "or";
1704 case ISD::XOR: return "xor";
1705 case ISD::SHL: return "shl";
1706 case ISD::SRA: return "sra";
1707 case ISD::SRL: return "srl";
1708
1709 case ISD::SELECT: return "select";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001710 case ISD::ADD_PARTS: return "add_parts";
1711 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner5b7bb562005-04-02 03:30:42 +00001712 case ISD::SHL_PARTS: return "shl_parts";
1713 case ISD::SRA_PARTS: return "sra_parts";
1714 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001715
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001716 // Conversion operators.
Chris Lattner9e4c7612005-01-10 23:25:25 +00001717 case ISD::SIGN_EXTEND: return "sign_extend";
1718 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001719 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001720 case ISD::TRUNCATE: return "truncate";
1721 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001722 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001723 case ISD::FP_EXTEND: return "fp_extend";
1724
1725 case ISD::SINT_TO_FP: return "sint_to_fp";
1726 case ISD::UINT_TO_FP: return "uint_to_fp";
1727 case ISD::FP_TO_SINT: return "fp_to_sint";
1728 case ISD::FP_TO_UINT: return "fp_to_uint";
1729
1730 // Control flow instructions
1731 case ISD::BR: return "br";
1732 case ISD::BRCOND: return "brcond";
Chris Lattnerb0713c72005-04-09 03:27:28 +00001733 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001734 case ISD::RET: return "ret";
1735 case ISD::CALL: return "call";
Chris Lattnerd0feb642005-05-13 18:43:43 +00001736 case ISD::TAILCALL:return "tailcall";
Chris Lattnere3677d62005-05-12 23:51:40 +00001737 case ISD::CALLSEQ_START: return "callseq_start";
1738 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001739
1740 // Other operators
1741 case ISD::LOAD: return "load";
1742 case ISD::STORE: return "store";
Chris Lattner39c67442005-01-14 22:08:15 +00001743 case ISD::EXTLOAD: return "extload";
1744 case ISD::SEXTLOAD: return "sextload";
1745 case ISD::ZEXTLOAD: return "zextload";
1746 case ISD::TRUNCSTORE: return "truncstore";
1747
Chris Lattner9e4c7612005-01-10 23:25:25 +00001748 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1749 case ISD::EXTRACT_ELEMENT: return "extract_element";
1750 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001751 case ISD::MEMSET: return "memset";
1752 case ISD::MEMCPY: return "memcpy";
1753 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001754
Chris Lattner93f4f5f2005-05-11 04:50:30 +00001755 // Bit counting
1756 case ISD::CTPOP: return "ctpop";
1757 case ISD::CTTZ: return "cttz";
1758 case ISD::CTLZ: return "ctlz";
1759
1760 // IO Intrinsics
Chris Lattner67ab9452005-05-09 20:22:17 +00001761 case ISD::READPORT: return "readport";
1762 case ISD::WRITEPORT: return "writeport";
1763 case ISD::READIO: return "readio";
1764 case ISD::WRITEIO: return "writeio";
1765
Chris Lattner9e4c7612005-01-10 23:25:25 +00001766 case ISD::SETCC:
1767 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1768 switch (SetCC->getCondition()) {
1769 default: assert(0 && "Unknown setcc condition!");
1770 case ISD::SETOEQ: return "setcc:setoeq";
1771 case ISD::SETOGT: return "setcc:setogt";
1772 case ISD::SETOGE: return "setcc:setoge";
1773 case ISD::SETOLT: return "setcc:setolt";
1774 case ISD::SETOLE: return "setcc:setole";
1775 case ISD::SETONE: return "setcc:setone";
Misha Brukman835702a2005-04-21 22:36:52 +00001776
1777 case ISD::SETO: return "setcc:seto";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001778 case ISD::SETUO: return "setcc:setuo";
1779 case ISD::SETUEQ: return "setcc:setue";
1780 case ISD::SETUGT: return "setcc:setugt";
1781 case ISD::SETUGE: return "setcc:setuge";
1782 case ISD::SETULT: return "setcc:setult";
1783 case ISD::SETULE: return "setcc:setule";
1784 case ISD::SETUNE: return "setcc:setune";
Misha Brukman835702a2005-04-21 22:36:52 +00001785
Chris Lattner9e4c7612005-01-10 23:25:25 +00001786 case ISD::SETEQ: return "setcc:seteq";
1787 case ISD::SETGT: return "setcc:setgt";
1788 case ISD::SETGE: return "setcc:setge";
1789 case ISD::SETLT: return "setcc:setlt";
1790 case ISD::SETLE: return "setcc:setle";
1791 case ISD::SETNE: return "setcc:setne";
1792 }
1793 }
1794}
Chris Lattner061a1ea2005-01-07 07:46:32 +00001795
1796void SDNode::dump() const {
1797 std::cerr << (void*)this << ": ";
1798
1799 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1800 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00001801 if (getValueType(i) == MVT::Other)
1802 std::cerr << "ch";
1803 else
1804 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001805 }
Chris Lattner9e4c7612005-01-10 23:25:25 +00001806 std::cerr << " = " << getOperationName();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001807
1808 std::cerr << " ";
1809 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1810 if (i) std::cerr << ", ";
1811 std::cerr << (void*)getOperand(i).Val;
1812 if (unsigned RN = getOperand(i).ResNo)
1813 std::cerr << ":" << RN;
1814 }
1815
1816 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1817 std::cerr << "<" << CSDN->getValue() << ">";
1818 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1819 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukman835702a2005-04-21 22:36:52 +00001820 } else if (const GlobalAddressSDNode *GADN =
Chris Lattner061a1ea2005-01-07 07:46:32 +00001821 dyn_cast<GlobalAddressSDNode>(this)) {
1822 std::cerr << "<";
1823 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukman77451162005-04-22 04:01:18 +00001824 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001825 std::cerr << "<" << FIDN->getIndex() << ">";
1826 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1827 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukman77451162005-04-22 04:01:18 +00001828 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001829 std::cerr << "<";
1830 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1831 if (LBB)
1832 std::cerr << LBB->getName() << " ";
1833 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnere727af02005-01-13 20:50:02 +00001834 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001835 std::cerr << "<reg #" << C2V->getReg() << ">";
1836 } else if (const ExternalSymbolSDNode *ES =
1837 dyn_cast<ExternalSymbolSDNode>(this)) {
1838 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner630d1932005-01-15 21:11:37 +00001839 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1840 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattner9440d6e2005-05-09 04:08:27 +00001841 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1842 if (M->getValue())
1843 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1844 else
1845 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001846 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001847}
1848
Chris Lattnere6f78822005-01-09 20:38:33 +00001849static void DumpNodes(SDNode *N, unsigned indent) {
1850 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1851 if (N->getOperand(i).Val->hasOneUse())
1852 DumpNodes(N->getOperand(i).Val, indent+2);
1853 else
1854 std::cerr << "\n" << std::string(indent+2, ' ')
1855 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukman835702a2005-04-21 22:36:52 +00001856
Chris Lattnere6f78822005-01-09 20:38:33 +00001857
1858 std::cerr << "\n" << std::string(indent, ' ');
1859 N->dump();
1860}
1861
Chris Lattner061a1ea2005-01-07 07:46:32 +00001862void SelectionDAG::dump() const {
1863 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner1270acc2005-01-09 20:26:36 +00001864 std::vector<SDNode*> Nodes(AllNodes);
1865 std::sort(Nodes.begin(), Nodes.end());
1866
1867 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001868 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1869 DumpNodes(Nodes[i], 2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001870 }
Chris Lattnere6f78822005-01-09 20:38:33 +00001871
1872 DumpNodes(getRoot().Val, 2);
1873
Chris Lattner061a1ea2005-01-07 07:46:32 +00001874 std::cerr << "\n\n";
1875}
1876