blob: cf3cb706ecb002ad996d2cac4dcd91bba7491659 [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 Lattner6667bdb2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattner90b7c132005-01-23 04:39:44 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnerbc892262005-08-16 18:33:07 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000023#include <iostream>
Chris Lattner9c667932005-01-07 21:09:16 +000024#include <set>
Chris Lattner061a1ea2005-01-07 07:46:32 +000025#include <cmath>
Jeff Cohen7d1670da2005-01-09 20:41:56 +000026#include <algorithm>
Chris Lattner560b5e42004-06-02 04:28:06 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattnerfde3a212005-01-09 20:52:51 +000029static bool isCommutativeBinOp(unsigned Opcode) {
30 switch (Opcode) {
31 case ISD::ADD:
32 case ISD::MUL:
33 case ISD::AND:
34 case ISD::OR:
35 case ISD::XOR: return true;
36 default: return false; // FIXME: Need commutative info for user ops!
37 }
38}
39
40static bool isAssociativeBinOp(unsigned Opcode) {
41 switch (Opcode) {
42 case ISD::ADD:
43 case ISD::MUL:
44 case ISD::AND:
45 case ISD::OR:
46 case ISD::XOR: return true;
47 default: return false; // FIXME: Need associative info for user ops!
48 }
49}
50
Chris Lattnerfde3a212005-01-09 20:52:51 +000051// isInvertibleForFree - Return true if there is no cost to emitting the logical
52// inverse of this node.
53static bool isInvertibleForFree(SDOperand N) {
54 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattnerd47675e2005-08-09 20:20:18 +000055 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattnerfde3a212005-01-09 20:52:51 +000056 return true;
Misha Brukman835702a2005-04-21 22:36:52 +000057 return false;
Chris Lattnerfde3a212005-01-09 20:52:51 +000058}
59
60
Chris Lattner061a1ea2005-01-07 07:46:32 +000061/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
62/// when given the operation for (X op Y).
63ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
64 // To perform this operation, we just need to swap the L and G bits of the
65 // operation.
66 unsigned OldL = (Operation >> 2) & 1;
67 unsigned OldG = (Operation >> 1) & 1;
68 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
69 (OldL << 1) | // New G bit
70 (OldG << 2)); // New L bit.
71}
72
73/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
74/// 'op' is a valid SetCC operation.
75ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
76 unsigned Operation = Op;
77 if (isInteger)
78 Operation ^= 7; // Flip L, G, E bits, but not U.
79 else
80 Operation ^= 15; // Flip all of the condition bits.
81 if (Operation > ISD::SETTRUE2)
82 Operation &= ~8; // Don't let N and U bits get set.
83 return ISD::CondCode(Operation);
84}
85
86
87/// isSignedOp - For an integer comparison, return 1 if the comparison is a
88/// signed operation and 2 if the result is an unsigned comparison. Return zero
89/// if the operation does not depend on the sign of the input (setne and seteq).
90static int isSignedOp(ISD::CondCode Opcode) {
91 switch (Opcode) {
92 default: assert(0 && "Illegal integer setcc operation!");
93 case ISD::SETEQ:
94 case ISD::SETNE: return 0;
95 case ISD::SETLT:
96 case ISD::SETLE:
97 case ISD::SETGT:
98 case ISD::SETGE: return 1;
99 case ISD::SETULT:
100 case ISD::SETULE:
101 case ISD::SETUGT:
102 case ISD::SETUGE: return 2;
103 }
104}
105
106/// getSetCCOrOperation - Return the result of a logical OR between different
107/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
108/// returns SETCC_INVALID if it is not possible to represent the resultant
109/// comparison.
110ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
111 bool isInteger) {
112 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
113 // Cannot fold a signed integer setcc with an unsigned integer setcc.
114 return ISD::SETCC_INVALID;
Misha Brukman835702a2005-04-21 22:36:52 +0000115
Chris Lattner061a1ea2005-01-07 07:46:32 +0000116 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukman835702a2005-04-21 22:36:52 +0000117
Chris Lattner061a1ea2005-01-07 07:46:32 +0000118 // If the N and U bits get set then the resultant comparison DOES suddenly
119 // care about orderedness, and is true when ordered.
120 if (Op > ISD::SETTRUE2)
121 Op &= ~16; // Clear the N bit.
122 return ISD::CondCode(Op);
123}
124
125/// getSetCCAndOperation - Return the result of a logical AND between different
126/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
127/// function returns zero if it is not possible to represent the resultant
128/// comparison.
129ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
130 bool isInteger) {
131 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
132 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukman835702a2005-04-21 22:36:52 +0000133 return ISD::SETCC_INVALID;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000134
135 // Combine all of the condition bits.
136 return ISD::CondCode(Op1 & Op2);
137}
138
Chris Lattner90b7c132005-01-23 04:39:44 +0000139const TargetMachine &SelectionDAG::getTarget() const {
140 return TLI.getTargetMachine();
141}
142
143
Chris Lattner9c667932005-01-07 21:09:16 +0000144/// RemoveDeadNodes - This method deletes all unreachable nodes in the
145/// SelectionDAG, including nodes (like loads) that have uses of their token
146/// chain but no other uses and no side effect. If a node is passed in as an
147/// argument, it is used as the seed for node deletion.
148void SelectionDAG::RemoveDeadNodes(SDNode *N) {
149 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
150
151 // Create a dummy node (which is not added to allnodes), that adds a reference
152 // to the root node, preventing it from being deleted.
153 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
154
155 DeleteNodeIfDead(N, &AllNodeSet);
156
157 Restart:
158 unsigned NumNodes = AllNodeSet.size();
159 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
160 I != E; ++I) {
161 // Try to delete this node.
162 DeleteNodeIfDead(*I, &AllNodeSet);
163
164 // If we actually deleted any nodes, do not use invalid iterators in
165 // AllNodeSet.
166 if (AllNodeSet.size() != NumNodes)
167 goto Restart;
168 }
169
170 // Restore AllNodes.
171 if (AllNodes.size() != NumNodes)
172 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
173
174 // If the root changed (e.g. it was a dead load, update the root).
175 setRoot(DummyNode->getOperand(0));
176
177 // Now that we are done with the dummy node, delete it.
178 DummyNode->getOperand(0).Val->removeUser(DummyNode);
179 delete DummyNode;
180}
181
Chris Lattner19732782005-08-16 18:17:10 +0000182
Chris Lattner9c667932005-01-07 21:09:16 +0000183void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
184 if (!N->use_empty())
185 return;
186
187 // Okay, we really are going to delete this node. First take this out of the
188 // appropriate CSE map.
Chris Lattner19732782005-08-16 18:17:10 +0000189 RemoveNodeFromCSEMaps(N);
190
191 // Next, brutally remove the operand list. This is safe to do, as there are
192 // no cycles in the graph.
193 while (!N->Operands.empty()) {
194 SDNode *O = N->Operands.back().Val;
195 N->Operands.pop_back();
196 O->removeUser(N);
197
198 // Now that we removed this operand, see if there are no uses of it left.
199 DeleteNodeIfDead(O, NodeSet);
200 }
201
202 // Remove the node from the nodes set and delete it.
203 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
204 AllNodeSet.erase(N);
205
206 // Now that the node is gone, check to see if any of the operands of this node
207 // are dead now.
208 delete N;
209}
210
211/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
212/// correspond to it. This is useful when we're about to delete or repurpose
213/// the node. We don't want future request for structurally identical nodes
214/// to return N anymore.
215void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner9c667932005-01-07 21:09:16 +0000216 switch (N->getOpcode()) {
217 case ISD::Constant:
218 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
219 N->getValueType(0)));
220 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000221 case ISD::ConstantFP: {
222 union {
223 double DV;
224 uint64_t IV;
225 };
226 DV = cast<ConstantFPSDNode>(N)->getValue();
227 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner9c667932005-01-07 21:09:16 +0000228 break;
Chris Lattner381dddc2005-02-17 20:17:32 +0000229 }
Chris Lattnerd47675e2005-08-09 20:20:18 +0000230 case ISD::CONDCODE:
231 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
232 "Cond code doesn't exist!");
233 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
234 break;
Chris Lattner9c667932005-01-07 21:09:16 +0000235 case ISD::GlobalAddress:
236 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
237 break;
238 case ISD::FrameIndex:
239 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
240 break;
241 case ISD::ConstantPool:
242 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
243 break;
244 case ISD::BasicBlock:
245 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
246 break;
247 case ISD::ExternalSymbol:
248 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
249 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000250 case ISD::VALUETYPE:
251 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
252 break;
Chris Lattner1095dc92005-08-05 16:55:31 +0000253 case ISD::SRCVALUE: {
254 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
255 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
256 break;
257 }
Chris Lattner9c667932005-01-07 21:09:16 +0000258 case ISD::LOAD:
259 Loads.erase(std::make_pair(N->getOperand(1),
260 std::make_pair(N->getOperand(0),
261 N->getValueType(0))));
262 break;
Chris Lattner9c667932005-01-07 21:09:16 +0000263 default:
264 if (N->getNumOperands() == 1)
265 UnaryOps.erase(std::make_pair(N->getOpcode(),
266 std::make_pair(N->getOperand(0),
267 N->getValueType(0))));
268 else if (N->getNumOperands() == 2)
269 BinaryOps.erase(std::make_pair(N->getOpcode(),
270 std::make_pair(N->getOperand(0),
271 N->getOperand(1))));
Chris Lattner566307f2005-05-14 07:42:29 +0000272 else if (N->getNumValues() == 1) {
273 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
274 OneResultNodes.erase(std::make_pair(N->getOpcode(),
275 std::make_pair(N->getValueType(0),
276 Ops)));
277 } else {
Chris Lattnerd5531332005-05-14 06:20:26 +0000278 // Remove the node from the ArbitraryNodes map.
279 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
280 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
281 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
282 std::make_pair(RV, Ops)));
283 }
Chris Lattner9c667932005-01-07 21:09:16 +0000284 break;
285 }
Chris Lattner9c667932005-01-07 21:09:16 +0000286}
287
288
Chris Lattner600d3082003-08-11 14:57:33 +0000289SelectionDAG::~SelectionDAG() {
290 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
291 delete AllNodes[i];
292}
293
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000294SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner8c3d4092005-04-13 19:41:05 +0000295 if (Op.getValueType() == VT) return Op;
Jeff Cohen915594d2005-05-10 02:22:38 +0000296 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner2b4e3fc2005-04-13 02:38:18 +0000297 return getNode(ISD::AND, Op.getValueType(), Op,
298 getConstant(Imm, Op.getValueType()));
299}
300
Chris Lattner061a1ea2005-01-07 07:46:32 +0000301SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
302 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
303 // Mask out any bits that are not valid for this constant.
Chris Lattner9a97e4d2005-01-08 06:24:30 +0000304 if (VT != MVT::i64)
305 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukman835702a2005-04-21 22:36:52 +0000306
Chris Lattner061a1ea2005-01-07 07:46:32 +0000307 SDNode *&N = Constants[std::make_pair(Val, VT)];
308 if (N) return SDOperand(N, 0);
309 N = new ConstantSDNode(Val, VT);
310 AllNodes.push_back(N);
311 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000312}
313
Chris Lattner061a1ea2005-01-07 07:46:32 +0000314SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
315 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
316 if (VT == MVT::f32)
317 Val = (float)Val; // Mask out extra precision.
318
Chris Lattner381dddc2005-02-17 20:17:32 +0000319 // Do the map lookup using the actual bit pattern for the floating point
320 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
321 // we don't have issues with SNANs.
322 union {
323 double DV;
324 uint64_t IV;
325 };
Misha Brukman835702a2005-04-21 22:36:52 +0000326
Chris Lattner381dddc2005-02-17 20:17:32 +0000327 DV = Val;
328
329 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattner061a1ea2005-01-07 07:46:32 +0000330 if (N) return SDOperand(N, 0);
331 N = new ConstantFPSDNode(Val, VT);
332 AllNodes.push_back(N);
333 return SDOperand(N, 0);
334}
335
336
337
338SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
339 MVT::ValueType VT) {
340 SDNode *&N = GlobalValues[GV];
341 if (N) return SDOperand(N, 0);
342 N = new GlobalAddressSDNode(GV,VT);
343 AllNodes.push_back(N);
344 return SDOperand(N, 0);
345}
346
347SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
348 SDNode *&N = FrameIndices[FI];
349 if (N) return SDOperand(N, 0);
350 N = new FrameIndexSDNode(FI, VT);
351 AllNodes.push_back(N);
352 return SDOperand(N, 0);
353}
354
355SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
356 SDNode *N = ConstantPoolIndices[CPIdx];
357 if (N) return SDOperand(N, 0);
358 N = new ConstantPoolSDNode(CPIdx, VT);
359 AllNodes.push_back(N);
360 return SDOperand(N, 0);
361}
362
363SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
364 SDNode *&N = BBNodes[MBB];
365 if (N) return SDOperand(N, 0);
366 N = new BasicBlockSDNode(MBB);
367 AllNodes.push_back(N);
368 return SDOperand(N, 0);
369}
370
Chris Lattner0b6ba902005-07-10 00:07:11 +0000371SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
372 if ((unsigned)VT >= ValueTypeNodes.size())
373 ValueTypeNodes.resize(VT+1);
374 if (ValueTypeNodes[VT] == 0) {
375 ValueTypeNodes[VT] = new VTSDNode(VT);
376 AllNodes.push_back(ValueTypeNodes[VT]);
377 }
378
379 return SDOperand(ValueTypeNodes[VT], 0);
380}
381
Chris Lattner061a1ea2005-01-07 07:46:32 +0000382SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
383 SDNode *&N = ExternalSymbols[Sym];
384 if (N) return SDOperand(N, 0);
385 N = new ExternalSymbolSDNode(Sym, VT);
386 AllNodes.push_back(N);
387 return SDOperand(N, 0);
388}
389
Chris Lattnerd47675e2005-08-09 20:20:18 +0000390SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
391 if ((unsigned)Cond >= CondCodeNodes.size())
392 CondCodeNodes.resize(Cond+1);
393
Chris Lattner14e060f2005-08-09 20:40:02 +0000394 if (CondCodeNodes[Cond] == 0) {
Chris Lattnerd47675e2005-08-09 20:20:18 +0000395 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner14e060f2005-08-09 20:40:02 +0000396 AllNodes.push_back(CondCodeNodes[Cond]);
397 }
Chris Lattnerd47675e2005-08-09 20:20:18 +0000398 return SDOperand(CondCodeNodes[Cond], 0);
399}
400
Chris Lattner679f5b02005-08-09 23:09:05 +0000401SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
402 SDOperand N2, ISD::CondCode Cond) {
Chris Lattner061a1ea2005-01-07 07:46:32 +0000403 // These setcc operations always fold.
404 switch (Cond) {
405 default: break;
406 case ISD::SETFALSE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000407 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000408 case ISD::SETTRUE:
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000409 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000410 }
411
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000412 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
413 uint64_t C2 = N2C->getValue();
414 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
415 uint64_t C1 = N1C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000416
Chris Lattner061a1ea2005-01-07 07:46:32 +0000417 // Sign extend the operands if required
418 if (ISD::isSignedIntSetCC(Cond)) {
419 C1 = N1C->getSignExtended();
420 C2 = N2C->getSignExtended();
421 }
422
423 switch (Cond) {
424 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000425 case ISD::SETEQ: return getConstant(C1 == C2, VT);
426 case ISD::SETNE: return getConstant(C1 != C2, VT);
427 case ISD::SETULT: return getConstant(C1 < C2, VT);
428 case ISD::SETUGT: return getConstant(C1 > C2, VT);
429 case ISD::SETULE: return getConstant(C1 <= C2, VT);
430 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
431 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
432 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
433 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
434 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000435 }
Chris Lattnerdfed7352005-04-07 18:58:54 +0000436 } else {
Chris Lattner6d40fd02005-04-18 04:30:45 +0000437 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
438 // the comparison on the input.
439 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
440 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
441
442 // If the comparison constant has bits in the upper part, the
443 // zero-extended value could never match.
444 if (C2 & (~0ULL << InSize)) {
445 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
446 switch (Cond) {
447 case ISD::SETUGT:
448 case ISD::SETUGE:
449 case ISD::SETEQ: return getConstant(0, VT);
450 case ISD::SETULT:
451 case ISD::SETULE:
452 case ISD::SETNE: return getConstant(1, VT);
453 case ISD::SETGT:
454 case ISD::SETGE:
455 // True if the sign bit of C2 is set.
456 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
457 case ISD::SETLT:
458 case ISD::SETLE:
459 // True if the sign bit of C2 isn't set.
460 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
461 default:
462 break;
463 }
464 }
465
466 // Otherwise, we can perform the comparison with the low bits.
467 switch (Cond) {
468 case ISD::SETEQ:
469 case ISD::SETNE:
470 case ISD::SETUGT:
471 case ISD::SETUGE:
472 case ISD::SETULT:
473 case ISD::SETULE:
Chris Lattnerd47675e2005-08-09 20:20:18 +0000474 return getSetCC(VT, N1.getOperand(0),
475 getConstant(C2, N1.getOperand(0).getValueType()),
476 Cond);
Chris Lattner6d40fd02005-04-18 04:30:45 +0000477 default:
478 break; // todo, be more careful with signed comparisons
479 }
480 }
481
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000482 uint64_t MinVal, MaxVal;
483 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
484 if (ISD::isSignedIntSetCC(Cond)) {
485 MinVal = 1ULL << (OperandBitSize-1);
486 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
487 MaxVal = ~0ULL >> (65-OperandBitSize);
488 else
489 MaxVal = 0;
490 } else {
491 MinVal = 0;
492 MaxVal = ~0ULL >> (64-OperandBitSize);
493 }
494
495 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
496 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
497 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
498 --C2; // X >= C1 --> X > (C1-1)
Chris Lattner679f5b02005-08-09 23:09:05 +0000499 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
500 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000501 }
502
503 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
504 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
505 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattner679f5b02005-08-09 23:09:05 +0000506 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
507 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000508 }
Misha Brukman835702a2005-04-21 22:36:52 +0000509
Nate Begeman80c095f2005-04-14 08:56:52 +0000510 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
511 return getConstant(0, VT); // X < MIN --> false
Misha Brukman835702a2005-04-21 22:36:52 +0000512
Nate Begeman80c095f2005-04-14 08:56:52 +0000513 // Canonicalize setgt X, Min --> setne X, Min
514 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000515 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukman835702a2005-04-21 22:36:52 +0000516
Chris Lattner87bd6982005-04-12 00:28:49 +0000517 // If we have setult X, 1, turn it into seteq X, 0
518 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000519 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
520 ISD::SETEQ);
Nate Begeman80c095f2005-04-14 08:56:52 +0000521 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner87bd6982005-04-12 00:28:49 +0000522 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000523 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
524 ISD::SETEQ);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000525
526 // If we have "setcc X, C1", check to see if we can shrink the immediate
527 // by changing cc.
528
529 // SETUGT X, SINTMAX -> SETLT X, 0
530 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
531 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000532 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000533
534 // FIXME: Implement the rest of these.
535
Chris Lattnerab1ed772005-04-21 06:12:41 +0000536
537 // Fold bit comparisons when we can.
538 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
539 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
540 if (ConstantSDNode *AndRHS =
541 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
542 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
543 // Perform the xform if the AND RHS is a single bit.
544 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
545 return getNode(ISD::SRL, VT, N1,
Chris Lattner6667bdb2005-08-02 19:26:06 +0000546 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattnerab1ed772005-04-21 06:12:41 +0000547 TLI.getShiftAmountTy()));
548 }
549 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
550 // (X & 8) == 8 --> (X & 8) >> 3
551 // Perform the xform if C2 is a single bit.
552 if ((C2 & (C2-1)) == 0) {
553 return getNode(ISD::SRL, VT, N1,
Chris Lattner6667bdb2005-08-02 19:26:06 +0000554 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattnerab1ed772005-04-21 06:12:41 +0000555 }
556 }
557 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000558 }
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000559 } else if (isa<ConstantSDNode>(N1.Val)) {
560 // Ensure that the constant occurs on the RHS.
Chris Lattnerd47675e2005-08-09 20:20:18 +0000561 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner6b03a0c2005-04-07 18:14:58 +0000562 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000563
564 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
565 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
566 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukman835702a2005-04-21 22:36:52 +0000567
Chris Lattner061a1ea2005-01-07 07:46:32 +0000568 switch (Cond) {
569 default: break; // FIXME: Implement the rest of these!
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000570 case ISD::SETEQ: return getConstant(C1 == C2, VT);
571 case ISD::SETNE: return getConstant(C1 != C2, VT);
572 case ISD::SETLT: return getConstant(C1 < C2, VT);
573 case ISD::SETGT: return getConstant(C1 > C2, VT);
574 case ISD::SETLE: return getConstant(C1 <= C2, VT);
575 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000576 }
577 } else {
578 // Ensure that the constant occurs on the RHS.
Chris Lattner679f5b02005-08-09 23:09:05 +0000579 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000580 }
581
582 if (N1 == N2) {
583 // We can always fold X == Y for integer setcc's.
584 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000585 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000586 unsigned UOF = ISD::getUnorderedFlavor(Cond);
587 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000588 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen915594d2005-05-10 02:22:38 +0000589 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000590 return getConstant(UOF, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000591 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
592 // if it is not already.
Chris Lattner679f5b02005-08-09 23:09:05 +0000593 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
594 if (NewCond != Cond)
595 return getSetCC(VT, N1, N2, NewCond);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000596 }
597
Chris Lattnerfde3a212005-01-09 20:52:51 +0000598 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner41b76412005-01-10 02:03:02 +0000599 MVT::isInteger(N1.getValueType())) {
600 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
601 N1.getOpcode() == ISD::XOR) {
602 // Simplify (X+Y) == (X+Z) --> Y == Z
603 if (N1.getOpcode() == N2.getOpcode()) {
604 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000605 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000606 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000607 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000608 if (isCommutativeBinOp(N1.getOpcode())) {
609 // If X op Y == Y op X, try other combinations.
610 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000611 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000612 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000613 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000614 }
615 }
Chris Lattner90b7c132005-01-23 04:39:44 +0000616
617 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukman835702a2005-04-21 22:36:52 +0000618
Chris Lattner41b76412005-01-10 02:03:02 +0000619 // Simplify (X+Z) == X --> Z == 0
620 if (N1.getOperand(0) == N2)
Chris Lattnerd47675e2005-08-09 20:20:18 +0000621 return getSetCC(VT, N1.getOperand(1),
622 getConstant(0, N1.getValueType()), Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000623 if (N1.getOperand(1) == N2) {
624 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerd47675e2005-08-09 20:20:18 +0000625 return getSetCC(VT, N1.getOperand(0),
626 getConstant(0, N1.getValueType()), Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000627 else {
628 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
629 // (Z-X) == X --> Z == X<<1
Chris Lattnerd47675e2005-08-09 20:20:18 +0000630 return getSetCC(VT, N1.getOperand(0),
Misha Brukman835702a2005-04-21 22:36:52 +0000631 getNode(ISD::SHL, N2.getValueType(),
Chris Lattnerd47675e2005-08-09 20:20:18 +0000632 N2, getConstant(1, TLI.getShiftAmountTy())),
633 Cond);
Chris Lattner41b76412005-01-10 02:03:02 +0000634 }
Chris Lattnerfde3a212005-01-09 20:52:51 +0000635 }
636 }
637
Chris Lattner41b76412005-01-10 02:03:02 +0000638 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
639 N2.getOpcode() == ISD::XOR) {
640 // Simplify X == (X+Z) --> Z == 0
Chris Lattner21c0fd92005-08-10 17:37:53 +0000641 if (N2.getOperand(0) == N1) {
Chris Lattnerd47675e2005-08-09 20:20:18 +0000642 return getSetCC(VT, N2.getOperand(1),
643 getConstant(0, N2.getValueType()), Cond);
Chris Lattner21c0fd92005-08-10 17:37:53 +0000644 } else if (N2.getOperand(1) == N1) {
645 if (isCommutativeBinOp(N2.getOpcode())) {
646 return getSetCC(VT, N2.getOperand(0),
647 getConstant(0, N2.getValueType()), Cond);
648 } else {
649 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
650 // X == (Z-X) --> X<<1 == Z
651 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
652 getConstant(1, TLI.getShiftAmountTy())),
653 N2.getOperand(0), Cond);
654 }
655 }
Chris Lattner41b76412005-01-10 02:03:02 +0000656 }
657 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000658
Chris Lattnerb61ecb52005-04-18 04:48:12 +0000659 // Fold away ALL boolean setcc's.
660 if (N1.getValueType() == MVT::i1) {
661 switch (Cond) {
662 default: assert(0 && "Unknown integer setcc!");
663 case ISD::SETEQ: // X == Y -> (X^Y)^1
664 N1 = getNode(ISD::XOR, MVT::i1,
665 getNode(ISD::XOR, MVT::i1, N1, N2),
666 getConstant(1, MVT::i1));
667 break;
668 case ISD::SETNE: // X != Y --> (X^Y)
669 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
670 break;
671 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
672 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
673 N1 = getNode(ISD::AND, MVT::i1, N2,
674 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
675 break;
676 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
677 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
678 N1 = getNode(ISD::AND, MVT::i1, N1,
679 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
680 break;
681 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
682 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
683 N1 = getNode(ISD::OR, MVT::i1, N2,
684 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
685 break;
686 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
687 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
688 N1 = getNode(ISD::OR, MVT::i1, N1,
689 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
690 break;
691 }
692 if (VT != MVT::i1)
693 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
694 return N1;
695 }
696
Chris Lattnerd47675e2005-08-09 20:20:18 +0000697 // Could not fold it.
698 return SDOperand();
Chris Lattner061a1ea2005-01-07 07:46:32 +0000699}
700
Nate Begemandc3154e2005-08-13 06:14:17 +0000701SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
702 SDOperand N3, SDOperand N4,
703 ISD::CondCode CC) {
704 MVT::ValueType VT = N3.getValueType();
Nate Begemanb6651e82005-08-13 06:00:21 +0000705 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
706 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
707 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
708
709 // Check to see if we can simplify the select into an fabs node
710 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
711 // Allow either -0.0 or 0.0
712 if (CFP->getValue() == 0.0) {
713 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
714 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
715 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
716 N1 == N4.getOperand(0))
717 return getNode(ISD::FABS, VT, N1);
718
719 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
720 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
721 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
722 N3.getOperand(0) == N4)
723 return getNode(ISD::FABS, VT, N4);
724 }
725 }
726
727 // Check to see if we can perform the "gzip trick", transforming
728 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
729 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
730 MVT::isInteger(N1.getValueType()) &&
731 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
732 MVT::ValueType XType = N1.getValueType();
733 MVT::ValueType AType = N3.getValueType();
734 if (XType >= AType) {
735 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
736 // single-bit constant. FIXME: remove once the dag combiner
737 // exists.
738 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
739 unsigned ShCtV = Log2_64(N3C->getValue());
740 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
741 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
742 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
743 if (XType > AType)
744 Shift = getNode(ISD::TRUNCATE, AType, Shift);
745 return getNode(ISD::AND, AType, Shift, N3);
746 }
747 SDOperand Shift = getNode(ISD::SRA, XType, N1,
748 getConstant(MVT::getSizeInBits(XType)-1,
749 TLI.getShiftAmountTy()));
750 if (XType > AType)
751 Shift = getNode(ISD::TRUNCATE, AType, Shift);
752 return getNode(ISD::AND, AType, Shift, N3);
753 }
754 }
755
756 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
757 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
758 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
759 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
760 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
761 MVT::ValueType XType = N1.getValueType();
762 if (SubC->isNullValue() && MVT::isInteger(XType)) {
763 SDOperand Shift = getNode(ISD::SRA, XType, N1,
764 getConstant(MVT::getSizeInBits(XType)-1,
765 TLI.getShiftAmountTy()));
766 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
767 Shift);
768 }
769 }
770 }
771
772 // Could not fold it.
773 return SDOperand();
774}
775
Chris Lattner061a1ea2005-01-07 07:46:32 +0000776/// getNode - Gets or creates the specified node.
Chris Lattner600d3082003-08-11 14:57:33 +0000777///
Chris Lattner061a1ea2005-01-07 07:46:32 +0000778SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
779 SDNode *N = new SDNode(Opcode, VT);
780 AllNodes.push_back(N);
781 return SDOperand(N, 0);
782}
783
Chris Lattner061a1ea2005-01-07 07:46:32 +0000784SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
785 SDOperand Operand) {
786 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
787 uint64_t Val = C->getValue();
788 switch (Opcode) {
789 default: break;
790 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
791 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
792 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000793 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
794 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000795 }
796 }
797
798 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
799 switch (Opcode) {
Chris Lattner0ea81f92005-04-09 03:02:46 +0000800 case ISD::FNEG:
801 return getConstantFP(-C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000802 case ISD::FP_ROUND:
803 case ISD::FP_EXTEND:
804 return getConstantFP(C->getValue(), VT);
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000805 case ISD::FP_TO_SINT:
806 return getConstant((int64_t)C->getValue(), VT);
807 case ISD::FP_TO_UINT:
808 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +0000809 }
810
811 unsigned OpOpcode = Operand.Val->getOpcode();
812 switch (Opcode) {
Chris Lattner96e809c2005-01-21 18:01:22 +0000813 case ISD::TokenFactor:
814 return Operand; // Factor of one node? No factor.
Chris Lattner061a1ea2005-01-07 07:46:32 +0000815 case ISD::SIGN_EXTEND:
816 if (Operand.getValueType() == VT) return Operand; // noop extension
817 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
818 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
819 break;
820 case ISD::ZERO_EXTEND:
821 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattnerb32d9312005-04-07 19:43:53 +0000822 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner4dfd2cf2005-01-12 18:51:15 +0000823 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattner061a1ea2005-01-07 07:46:32 +0000824 break;
825 case ISD::TRUNCATE:
826 if (Operand.getValueType() == VT) return Operand; // noop truncate
827 if (OpOpcode == ISD::TRUNCATE)
828 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4d5ba992005-01-07 21:56:24 +0000829 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
830 // If the source is smaller than the dest, we still need an extend.
831 if (Operand.Val->getOperand(0).getValueType() < VT)
832 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
833 else if (Operand.Val->getOperand(0).getValueType() > VT)
834 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
835 else
836 return Operand.Val->getOperand(0);
837 }
Chris Lattner061a1ea2005-01-07 07:46:32 +0000838 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +0000839 case ISD::FNEG:
840 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
841 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
842 Operand.Val->getOperand(0));
843 if (OpOpcode == ISD::FNEG) // --X -> X
844 return Operand.Val->getOperand(0);
845 break;
846 case ISD::FABS:
847 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
848 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
849 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +0000850 }
851
852 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
853 if (N) return SDOperand(N, 0);
854 N = new SDNode(Opcode, Operand);
855 N->setValueTypes(VT);
856 AllNodes.push_back(N);
857 return SDOperand(N, 0);
Chris Lattner600d3082003-08-11 14:57:33 +0000858}
859
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000860/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
861/// this predicate to simplify operations downstream. V and Mask are known to
862/// be the same type.
863static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
864 const TargetLowering &TLI) {
865 unsigned SrcBits;
866 if (Mask == 0) return true;
Misha Brukman835702a2005-04-21 22:36:52 +0000867
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000868 // If we know the result of a setcc has the top bits zero, use this info.
869 switch (Op.getOpcode()) {
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000870 case ISD::Constant:
871 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
872
873 case ISD::SETCC:
Misha Brukman835702a2005-04-21 22:36:52 +0000874 return ((Mask & 1) == 0) &&
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000875 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
876
877 case ISD::ZEXTLOAD:
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000878 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000879 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
880 case ISD::ZERO_EXTEND:
881 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
882 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
883
884 case ISD::AND:
885 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattnerf6302442005-04-21 06:28:15 +0000886 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000887 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
888
889 // FALL THROUGH
890 case ISD::OR:
891 case ISD::XOR:
892 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
893 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
894 case ISD::SELECT:
895 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
896 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattnerf6302442005-04-21 06:28:15 +0000897
898 case ISD::SRL:
899 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
900 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
901 uint64_t NewVal = Mask << ShAmt->getValue();
902 SrcBits = MVT::getSizeInBits(Op.getValueType());
903 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
904 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
905 }
906 return false;
907 case ISD::SHL:
908 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
909 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
910 uint64_t NewVal = Mask >> ShAmt->getValue();
911 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
912 }
913 return false;
Chris Lattnerd373ff62005-04-25 21:03:25 +0000914 // TODO we could handle some SRA cases here.
Chris Lattnerd929f8b2005-04-18 03:48:41 +0000915 default: break;
916 }
917
918 return false;
919}
920
921
922
Chris Lattner061a1ea2005-01-07 07:46:32 +0000923SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
924 SDOperand N1, SDOperand N2) {
Chris Lattner4e550eb2005-01-16 02:23:22 +0000925#ifndef NDEBUG
926 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +0000927 case ISD::TokenFactor:
928 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
929 N2.getValueType() == MVT::Other && "Invalid token factor!");
930 break;
Chris Lattner4e550eb2005-01-16 02:23:22 +0000931 case ISD::AND:
932 case ISD::OR:
933 case ISD::XOR:
934 case ISD::UDIV:
935 case ISD::UREM:
Chris Lattner51836bb2005-05-15 05:39:08 +0000936 case ISD::MULHU:
937 case ISD::MULHS:
Chris Lattner4e550eb2005-01-16 02:23:22 +0000938 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
939 // fall through
940 case ISD::ADD:
941 case ISD::SUB:
942 case ISD::MUL:
943 case ISD::SDIV:
944 case ISD::SREM:
945 assert(N1.getValueType() == N2.getValueType() &&
946 N1.getValueType() == VT && "Binary operator types must match!");
947 break;
948
949 case ISD::SHL:
950 case ISD::SRA:
951 case ISD::SRL:
952 assert(VT == N1.getValueType() &&
953 "Shift operators return type must be the same as their first arg");
954 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner16f64df2005-01-17 17:15:02 +0000955 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner4e550eb2005-01-16 02:23:22 +0000956 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +0000957 case ISD::FP_ROUND_INREG: {
958 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
959 assert(VT == N1.getValueType() && "Not an inreg round!");
960 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
961 "Cannot FP_ROUND_INREG integer types");
962 assert(EVT <= VT && "Not rounding down!");
963 break;
964 }
965 case ISD::SIGN_EXTEND_INREG: {
966 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
967 assert(VT == N1.getValueType() && "Not an inreg extend!");
968 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
969 "Cannot *_EXTEND_INREG FP types");
970 assert(EVT <= VT && "Not extending!");
971 }
972
Chris Lattner4e550eb2005-01-16 02:23:22 +0000973 default: break;
974 }
975#endif
976
Chris Lattner061a1ea2005-01-07 07:46:32 +0000977 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
978 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
979 if (N1C) {
980 if (N2C) {
981 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
982 switch (Opcode) {
983 case ISD::ADD: return getConstant(C1 + C2, VT);
984 case ISD::SUB: return getConstant(C1 - C2, VT);
985 case ISD::MUL: return getConstant(C1 * C2, VT);
986 case ISD::UDIV:
987 if (C2) return getConstant(C1 / C2, VT);
988 break;
989 case ISD::UREM :
990 if (C2) return getConstant(C1 % C2, VT);
991 break;
992 case ISD::SDIV :
993 if (C2) return getConstant(N1C->getSignExtended() /
994 N2C->getSignExtended(), VT);
995 break;
996 case ISD::SREM :
997 if (C2) return getConstant(N1C->getSignExtended() %
998 N2C->getSignExtended(), VT);
999 break;
1000 case ISD::AND : return getConstant(C1 & C2, VT);
1001 case ISD::OR : return getConstant(C1 | C2, VT);
1002 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner0966a752005-01-10 00:07:15 +00001003 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1004 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1005 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001006 default: break;
1007 }
1008
1009 } else { // Cannonicalize constant to RHS if commutative
1010 if (isCommutativeBinOp(Opcode)) {
1011 std::swap(N1C, N2C);
1012 std::swap(N1, N2);
1013 }
1014 }
Chris Lattner32a5f022005-01-19 17:29:49 +00001015
1016 switch (Opcode) {
1017 default: break;
1018 case ISD::SHL: // shl 0, X -> 0
1019 if (N1C->isNullValue()) return N1;
1020 break;
1021 case ISD::SRL: // srl 0, X -> 0
1022 if (N1C->isNullValue()) return N1;
1023 break;
1024 case ISD::SRA: // sra -1, X -> -1
1025 if (N1C->isAllOnesValue()) return N1;
1026 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00001027 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1028 // Extending a constant? Just return the extended constant.
1029 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1030 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner32a5f022005-01-19 17:29:49 +00001031 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001032 }
1033
1034 if (N2C) {
1035 uint64_t C2 = N2C->getValue();
1036
1037 switch (Opcode) {
1038 case ISD::ADD:
1039 if (!C2) return N1; // add X, 0 -> X
1040 break;
1041 case ISD::SUB:
1042 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner8005e912005-05-12 00:17:04 +00001043 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001044 case ISD::MUL:
1045 if (!C2) return N2; // mul X, 0 -> 0
1046 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1047 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1048
Chris Lattner90b7c132005-01-23 04:39:44 +00001049 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +00001050 if ((C2 & C2-1) == 0) {
Chris Lattner6667bdb2005-08-02 19:26:06 +00001051 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattner90b7c132005-01-23 04:39:44 +00001052 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001053 }
1054 break;
1055
Chris Lattner51836bb2005-05-15 05:39:08 +00001056 case ISD::MULHU:
1057 case ISD::MULHS:
1058 if (!C2) return N2; // mul X, 0 -> 0
1059
1060 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1061 return getConstant(0, VT);
1062
1063 // Many others could be handled here, including -1, powers of 2, etc.
1064 break;
1065
Chris Lattner061a1ea2005-01-07 07:46:32 +00001066 case ISD::UDIV:
Chris Lattner90b7c132005-01-23 04:39:44 +00001067 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattner061a1ea2005-01-07 07:46:32 +00001068 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner6667bdb2005-08-02 19:26:06 +00001069 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattner90b7c132005-01-23 04:39:44 +00001070 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001071 }
1072 break;
1073
Chris Lattnera86fa442005-01-11 04:25:13 +00001074 case ISD::SHL:
1075 case ISD::SRL:
1076 case ISD::SRA:
Nate Begemanaf1c0f72005-04-12 23:12:17 +00001077 // If the shift amount is bigger than the size of the data, then all the
Chris Lattnerd929f8b2005-04-18 03:48:41 +00001078 // bits are shifted out. Simplify to undef.
Nate Begemanaf1c0f72005-04-12 23:12:17 +00001079 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1080 return getNode(ISD::UNDEF, N1.getValueType());
1081 }
Chris Lattnera86fa442005-01-11 04:25:13 +00001082 if (C2 == 0) return N1;
Chris Lattner21381e82005-08-12 23:54:58 +00001083
1084 if (Opcode == ISD::SRA) {
1085 // If the sign bit is known to be zero, switch this to a SRL.
1086 if (MaskedValueIsZero(N1,
1087 1ULL << MVT::getSizeInBits(N1.getValueType())-1,
1088 TLI))
1089 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1090 } else {
1091 // If the part left over is known to be zero, the whole thing is zero.
1092 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1093 if (Opcode == ISD::SRL) {
1094 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1095 return getConstant(0, N1.getValueType());
1096 } else if (Opcode == ISD::SHL) {
1097 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1098 return getConstant(0, N1.getValueType());
1099 }
1100 }
Chris Lattner1ab16912005-05-09 17:06:45 +00001101
1102 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1103 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1104 unsigned OpSAC = OpSA->getValue();
1105 if (N1.getOpcode() == ISD::SHL) {
1106 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1107 return getConstant(0, N1.getValueType());
1108 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1109 getConstant(C2+OpSAC, N2.getValueType()));
1110 } else if (N1.getOpcode() == ISD::SRL) {
1111 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1112 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1113 getConstant(~0ULL << OpSAC, VT));
1114 if (C2 > OpSAC) {
1115 return getNode(ISD::SHL, VT, Mask,
1116 getConstant(C2-OpSAC, N2.getValueType()));
1117 } else {
1118 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1119 return getNode(ISD::SRL, VT, Mask,
1120 getConstant(OpSAC-C2, N2.getValueType()));
1121 }
1122 } else if (N1.getOpcode() == ISD::SRA) {
1123 // if C1 == C2, just mask out low bits.
1124 if (C2 == OpSAC)
1125 return getNode(ISD::AND, VT, N1.getOperand(0),
1126 getConstant(~0ULL << C2, VT));
1127 }
1128 }
Chris Lattnera86fa442005-01-11 04:25:13 +00001129 break;
1130
Chris Lattner061a1ea2005-01-07 07:46:32 +00001131 case ISD::AND:
1132 if (!C2) return N2; // X and 0 -> 0
1133 if (N2C->isAllOnesValue())
Nate Begeman4ddd8162005-04-13 21:23:31 +00001134 return N1; // X and -1 -> X
Chris Lattnerda504742005-04-09 21:43:54 +00001135
Chris Lattnerd929f8b2005-04-18 03:48:41 +00001136 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1137 return getConstant(0, VT);
1138
Chris Lattnerf6302442005-04-21 06:28:15 +00001139 {
1140 uint64_t NotC2 = ~C2;
1141 if (VT != MVT::i64)
1142 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1143
1144 if (MaskedValueIsZero(N1, NotC2, TLI))
1145 return N1; // if (X & ~C2) -> 0, the and is redundant
1146 }
Chris Lattnerd929f8b2005-04-18 03:48:41 +00001147
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00001148 // FIXME: Should add a corresponding version of this for
1149 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1150 // we don't have yet.
1151
Chris Lattner2b4e3fc2005-04-13 02:38:18 +00001152 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1153 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnerda504742005-04-09 21:43:54 +00001154 // If we are masking out the part of our input that was extended, just
1155 // mask the input to the extension directly.
1156 unsigned ExtendBits =
Chris Lattner0b6ba902005-07-10 00:07:11 +00001157 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnerda504742005-04-09 21:43:54 +00001158 if ((C2 & (~0ULL << ExtendBits)) == 0)
1159 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattner0c26a0b2005-08-07 05:00:44 +00001160 } else if (N1.getOpcode() == ISD::OR) {
1161 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1162 if ((ORI->getValue() & C2) == C2) {
1163 // If the 'or' is setting all of the bits that we are masking for,
1164 // we know the result of the AND will be the AND mask itself.
1165 return N2;
1166 }
Chris Lattnerda504742005-04-09 21:43:54 +00001167 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001168 break;
1169 case ISD::OR:
1170 if (!C2)return N1; // X or 0 -> X
1171 if (N2C->isAllOnesValue())
Misha Brukman77451162005-04-22 04:01:18 +00001172 return N2; // X or -1 -> -1
Chris Lattner061a1ea2005-01-07 07:46:32 +00001173 break;
1174 case ISD::XOR:
1175 if (!C2) return N1; // X xor 0 -> X
1176 if (N2C->isAllOnesValue()) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001177 if (N1.Val->getOpcode() == ISD::SETCC){
1178 SDNode *SetCC = N1.Val;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001179 // !(X op Y) -> (X !op Y)
1180 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattnerd47675e2005-08-09 20:20:18 +00001181 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1182 return getSetCC(SetCC->getValueType(0),
1183 SetCC->getOperand(0), SetCC->getOperand(1),
1184 ISD::getSetCCInverse(CC, isInteger));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001185 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1186 SDNode *Op = N1.Val;
1187 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1188 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1189 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1190 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1191 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1192 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1193 if (Op->getOpcode() == ISD::AND)
1194 return getNode(ISD::OR, VT, LHS, RHS);
1195 return getNode(ISD::AND, VT, LHS, RHS);
1196 }
1197 }
Misha Brukman77451162005-04-22 04:01:18 +00001198 // X xor -1 -> not(x) ?
Chris Lattner061a1ea2005-01-07 07:46:32 +00001199 }
1200 break;
1201 }
1202
1203 // Reassociate ((X op C1) op C2) if possible.
1204 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1205 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattnercda3efa2005-01-07 22:44:09 +00001206 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattner061a1ea2005-01-07 07:46:32 +00001207 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1208 }
1209
1210 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1211 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner0b6ba902005-07-10 00:07:11 +00001212 if (N1CFP) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001213 if (N2CFP) {
1214 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1215 switch (Opcode) {
1216 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1217 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1218 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1219 case ISD::SDIV:
1220 if (C2) return getConstantFP(C1 / C2, VT);
1221 break;
1222 case ISD::SREM :
1223 if (C2) return getConstantFP(fmod(C1, C2), VT);
1224 break;
1225 default: break;
1226 }
1227
1228 } else { // Cannonicalize constant to RHS if commutative
1229 if (isCommutativeBinOp(Opcode)) {
1230 std::swap(N1CFP, N2CFP);
1231 std::swap(N1, N2);
1232 }
1233 }
1234
Chris Lattner0b6ba902005-07-10 00:07:11 +00001235 if (Opcode == ISD::FP_ROUND_INREG)
1236 return getNode(ISD::FP_EXTEND, VT,
1237 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1238 }
1239
Chris Lattner061a1ea2005-01-07 07:46:32 +00001240 // Finally, fold operations that do not require constants.
1241 switch (Opcode) {
Chris Lattner9b75e142005-01-19 18:01:40 +00001242 case ISD::TokenFactor:
1243 if (N1.getOpcode() == ISD::EntryToken)
1244 return N2;
1245 if (N2.getOpcode() == ISD::EntryToken)
1246 return N1;
1247 break;
1248
Chris Lattner061a1ea2005-01-07 07:46:32 +00001249 case ISD::AND:
1250 case ISD::OR:
Chris Lattnerd47675e2005-08-09 20:20:18 +00001251 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1252 SDNode *LHS = N1.Val, *RHS = N2.Val;
1253 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1254 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1255 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1256 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattner061a1ea2005-01-07 07:46:32 +00001257
Chris Lattnerd47675e2005-08-09 20:20:18 +00001258 if (LR == RR && isa<ConstantSDNode>(LR) &&
1259 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1260 // (X != 0) | (Y != 0) -> (X|Y != 0)
1261 // (X == 0) & (Y == 0) -> (X|Y == 0)
1262 // (X < 0) | (Y < 0) -> (X|Y < 0)
1263 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1264 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1265 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1266 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1267 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1268 Op2);
Chris Lattnerf8064592005-04-25 21:20:28 +00001269
Chris Lattnerd47675e2005-08-09 20:20:18 +00001270 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1271 // (X == -1) & (Y == -1) -> (X&Y == -1)
1272 // (X != -1) | (Y != -1) -> (X&Y != -1)
1273 // (X > -1) | (Y > -1) -> (X&Y > -1)
1274 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1275 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1276 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1277 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1278 LR, Op2);
1279 // (X > -1) & (Y > -1) -> (X|Y > -1)
1280 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1281 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1282 LR, Op2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001283 }
1284 }
Chris Lattner868d4732005-04-18 04:11:19 +00001285
Chris Lattnerd47675e2005-08-09 20:20:18 +00001286 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1287 if (LL == RR && LR == RL) {
1288 Op2 = ISD::getSetCCSwappedOperands(Op2);
1289 goto MatchedBackwards;
1290 }
1291
1292 if (LL == RL && LR == RR) {
1293 MatchedBackwards:
1294 ISD::CondCode Result;
1295 bool isInteger = MVT::isInteger(LL.getValueType());
1296 if (Opcode == ISD::OR)
1297 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1298 else
1299 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1300
1301 if (Result != ISD::SETCC_INVALID)
1302 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1303 }
1304 }
1305
Chris Lattner868d4732005-04-18 04:11:19 +00001306 // and/or zext(a), zext(b) -> zext(and/or a, b)
1307 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1308 N2.getOpcode() == ISD::ZERO_EXTEND &&
1309 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1310 return getNode(ISD::ZERO_EXTEND, VT,
1311 getNode(Opcode, N1.getOperand(0).getValueType(),
1312 N1.getOperand(0), N2.getOperand(0)));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001313 break;
1314 case ISD::XOR:
1315 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1316 break;
Chris Lattner0ea81f92005-04-09 03:02:46 +00001317 case ISD::ADD:
1318 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1319 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1320 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1321 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattnerf2bff922005-04-10 04:04:49 +00001322 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1323 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1324 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1325 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1326 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1327 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begemana2e87792005-06-16 07:06:03 +00001328 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1329 !MVT::isFloatingPoint(N2.getValueType()))
1330 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner0ea81f92005-04-09 03:02:46 +00001331 break;
Chris Lattner3d5d5022005-01-09 20:09:57 +00001332 case ISD::SUB:
1333 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001334 if (N1.Val->getOperand(0) == N2 &&
Nate Begemana2e87792005-06-16 07:06:03 +00001335 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattner3d5d5022005-01-09 20:09:57 +00001336 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begemana2e87792005-06-16 07:06:03 +00001337 if (N1.Val->getOperand(1) == N2 &&
1338 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattner3d5d5022005-01-09 20:09:57 +00001339 return N1.Val->getOperand(0); // (A+B)-B == A
1340 }
Chris Lattner0ea81f92005-04-09 03:02:46 +00001341 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1342 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattner3d5d5022005-01-09 20:09:57 +00001343 break;
Chris Lattner0b6ba902005-07-10 00:07:11 +00001344 case ISD::FP_ROUND_INREG:
1345 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1346 break;
1347 case ISD::SIGN_EXTEND_INREG: {
1348 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1349 if (EVT == VT) return N1; // Not actually extending
1350
1351 // If we are sign extending an extension, use the original source.
1352 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1353 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1354 return N1;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001355
Chris Lattner0b6ba902005-07-10 00:07:11 +00001356 // If we are sign extending a sextload, return just the load.
1357 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001358 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001359 return N1;
1360
1361 // If we are extending the result of a setcc, and we already know the
1362 // contents of the top bits, eliminate the extension.
1363 if (N1.getOpcode() == ISD::SETCC &&
1364 TLI.getSetCCResultContents() ==
1365 TargetLowering::ZeroOrNegativeOneSetCCResult)
1366 return N1;
1367
1368 // If we are sign extending the result of an (and X, C) operation, and we
1369 // know the extended bits are zeros already, don't do the extend.
1370 if (N1.getOpcode() == ISD::AND)
1371 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1372 uint64_t Mask = N1C->getValue();
1373 unsigned NumBits = MVT::getSizeInBits(EVT);
1374 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1375 return N1;
1376 }
1377 break;
1378 }
1379
Nate Begeman4ddd8162005-04-13 21:23:31 +00001380 // FIXME: figure out how to safely handle things like
1381 // int foo(int x) { return 1 << (x & 255); }
1382 // int bar() { return foo(256); }
1383#if 0
Nate Begemanca916ba2005-04-12 23:32:28 +00001384 case ISD::SHL:
1385 case ISD::SRL:
1386 case ISD::SRA:
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001387 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00001388 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemanca916ba2005-04-12 23:32:28 +00001389 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnerb1f25ac2005-04-13 02:58:13 +00001390 else if (N2.getOpcode() == ISD::AND)
1391 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1392 // If the and is only masking out bits that cannot effect the shift,
1393 // eliminate the and.
1394 unsigned NumBits = MVT::getSizeInBits(VT);
1395 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1396 return getNode(Opcode, VT, N1, N2.getOperand(0));
1397 }
Nate Begemanca916ba2005-04-12 23:32:28 +00001398 break;
Nate Begeman4ddd8162005-04-13 21:23:31 +00001399#endif
Chris Lattner061a1ea2005-01-07 07:46:32 +00001400 }
1401
Chris Lattner724f7ee2005-05-11 18:57:39 +00001402 // Memoize this node if possible.
1403 SDNode *N;
Chris Lattner2dce7032005-05-12 23:24:06 +00001404 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner724f7ee2005-05-11 18:57:39 +00001405 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1406 if (BON) return SDOperand(BON, 0);
1407
1408 BON = N = new SDNode(Opcode, N1, N2);
1409 } else {
Chris Lattner8005e912005-05-12 00:17:04 +00001410 N = new SDNode(Opcode, N1, N2);
Chris Lattner724f7ee2005-05-11 18:57:39 +00001411 }
1412
Chris Lattner86535992005-05-14 07:45:46 +00001413 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001414 AllNodes.push_back(N);
1415 return SDOperand(N, 0);
1416}
1417
Chris Lattner8005e912005-05-12 00:17:04 +00001418// setAdjCallChain - This method changes the token chain of an
Chris Lattner2dce7032005-05-12 23:24:06 +00001419// CALLSEQ_START/END node to be the specified operand.
Chris Lattner724f7ee2005-05-11 18:57:39 +00001420void SDNode::setAdjCallChain(SDOperand N) {
1421 assert(N.getValueType() == MVT::Other);
Chris Lattner2dce7032005-05-12 23:24:06 +00001422 assert((getOpcode() == ISD::CALLSEQ_START ||
1423 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner724f7ee2005-05-11 18:57:39 +00001424
1425 Operands[0].Val->removeUser(this);
1426 Operands[0] = N;
1427 N.Val->Uses.push_back(this);
1428}
1429
1430
1431
Chris Lattner061a1ea2005-01-07 07:46:32 +00001432SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001433 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001434 SDOperand SV) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001435 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1436 if (N) return SDOperand(N, 0);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001437 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001438
1439 // Loads have a token chain.
1440 N->setValueTypes(VT, MVT::Other);
1441 AllNodes.push_back(N);
1442 return SDOperand(N, 0);
1443}
1444
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001445
1446SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1447 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1448 MVT::ValueType EVT) {
1449 std::vector<SDOperand> Ops;
1450 Ops.reserve(4);
1451 Ops.push_back(Chain);
1452 Ops.push_back(Ptr);
1453 Ops.push_back(SV);
1454 Ops.push_back(getValueType(EVT));
1455 std::vector<MVT::ValueType> VTs;
1456 VTs.reserve(2);
1457 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1458 return getNode(Opcode, VTs, Ops);
1459}
1460
Chris Lattner061a1ea2005-01-07 07:46:32 +00001461SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1462 SDOperand N1, SDOperand N2, SDOperand N3) {
1463 // Perform various simplifications.
1464 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1465 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1466 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1467 switch (Opcode) {
Chris Lattnerd47675e2005-08-09 20:20:18 +00001468 case ISD::SETCC: {
1469 // Use SimplifySetCC to simplify SETCC's.
Chris Lattner679f5b02005-08-09 23:09:05 +00001470 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattnerd47675e2005-08-09 20:20:18 +00001471 if (Simp.Val) return Simp;
1472 break;
1473 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001474 case ISD::SELECT:
1475 if (N1C)
1476 if (N1C->getValue())
1477 return N2; // select true, X, Y -> X
Misha Brukman835702a2005-04-21 22:36:52 +00001478 else
Chris Lattner061a1ea2005-01-07 07:46:32 +00001479 return N3; // select false, X, Y -> Y
1480
1481 if (N2 == N3) return N2; // select C, X, X -> X
1482
1483 if (VT == MVT::i1) { // Boolean SELECT
1484 if (N2C) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001485 if (N2C->getValue()) // select C, 1, X -> C | X
1486 return getNode(ISD::OR, VT, N1, N3);
1487 else // select C, 0, X -> ~C & X
1488 return getNode(ISD::AND, VT,
1489 getNode(ISD::XOR, N1.getValueType(), N1,
1490 getConstant(1, N1.getValueType())), N3);
1491 } else if (N3C) {
1492 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1493 return getNode(ISD::OR, VT,
1494 getNode(ISD::XOR, N1.getValueType(), N1,
1495 getConstant(1, N1.getValueType())), N2);
1496 else // select C, X, 0 -> C & X
1497 return getNode(ISD::AND, VT, N1, N2);
1498 }
Chris Lattneraf5b25f2005-04-12 02:54:39 +00001499
1500 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1501 return getNode(ISD::OR, VT, N1, N3);
1502 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1503 return getNode(ISD::AND, VT, N1, N2);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001504 }
Nate Begemanb6651e82005-08-13 06:00:21 +00001505 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemandc3154e2005-08-13 06:14:17 +00001506 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1507 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begemanb6651e82005-08-13 06:00:21 +00001508 if (Simp.Val) return Simp;
1509 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001510 break;
Chris Lattner5c66e452005-01-07 22:49:57 +00001511 case ISD::BRCOND:
1512 if (N2C)
1513 if (N2C->getValue()) // Unconditional branch
1514 return getNode(ISD::BR, MVT::Other, N1, N3);
1515 else
1516 return N1; // Never-taken branch
Chris Lattnere0f1fe12005-01-07 23:32:00 +00001517 break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001518 }
1519
Chris Lattner566307f2005-05-14 07:42:29 +00001520 std::vector<SDOperand> Ops;
1521 Ops.reserve(3);
1522 Ops.push_back(N1);
1523 Ops.push_back(N2);
1524 Ops.push_back(N3);
1525
1526 // Memoize nodes.
1527 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1528 if (N) return SDOperand(N, 0);
1529
1530 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattner96c262e2005-05-14 07:29:57 +00001531 N->setValueTypes(VT);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001532 AllNodes.push_back(N);
1533 return SDOperand(N, 0);
1534}
1535
1536SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001537 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001538 SDOperand N4) {
Chris Lattner833a4fb2005-05-14 07:32:14 +00001539 std::vector<SDOperand> Ops;
1540 Ops.reserve(4);
1541 Ops.push_back(N1);
1542 Ops.push_back(N2);
1543 Ops.push_back(N3);
1544 Ops.push_back(N4);
1545 return getNode(Opcode, VT, Ops);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001546}
1547
Chris Lattner36db1ed2005-07-10 00:29:18 +00001548SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1549 SDOperand N1, SDOperand N2, SDOperand N3,
1550 SDOperand N4, SDOperand N5) {
Nate Begeman180b0882005-08-11 01:12:20 +00001551 if (ISD::SELECT_CC == Opcode) {
1552 assert(N1.getValueType() == N2.getValueType() &&
1553 "LHS and RHS of condition must have same type!");
1554 assert(N3.getValueType() == N4.getValueType() &&
1555 "True and False arms of SelectCC must have same type!");
Nate Begemandc3154e2005-08-13 06:14:17 +00001556 assert(N3.getValueType() == VT &&
1557 "select_cc node must be of same type as true and false value!");
1558 SDOperand Simp = SimplifySelectCC(N1, N2, N3, N4,
1559 cast<CondCodeSDNode>(N5)->get());
Nate Begemanb6651e82005-08-13 06:00:21 +00001560 if (Simp.Val) return Simp;
Nate Begeman180b0882005-08-11 01:12:20 +00001561 }
1562
Chris Lattner36db1ed2005-07-10 00:29:18 +00001563 std::vector<SDOperand> Ops;
1564 Ops.reserve(5);
1565 Ops.push_back(N1);
1566 Ops.push_back(N2);
1567 Ops.push_back(N3);
1568 Ops.push_back(N4);
1569 Ops.push_back(N5);
1570 return getNode(Opcode, VT, Ops);
1571}
1572
1573
Chris Lattnerc14f3542005-05-09 04:14:13 +00001574SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth2edc1882005-06-29 18:54:02 +00001575 assert((!V || isa<PointerType>(V->getType())) &&
1576 "SrcValue is not a pointer?");
Chris Lattnerc14f3542005-05-09 04:14:13 +00001577 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1578 if (N) return SDOperand(N, 0);
1579
1580 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001581 AllNodes.push_back(N);
1582 return SDOperand(N, 0);
1583}
1584
1585SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattnerd5531332005-05-14 06:20:26 +00001586 std::vector<SDOperand> &Ops) {
1587 switch (Ops.size()) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001588 case 0: return getNode(Opcode, VT);
Chris Lattnerd5531332005-05-14 06:20:26 +00001589 case 1: return getNode(Opcode, VT, Ops[0]);
1590 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1591 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001592 default: break;
Chris Lattner061a1ea2005-01-07 07:46:32 +00001593 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001594
Chris Lattnerd5531332005-05-14 06:20:26 +00001595 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001596 switch (Opcode) {
1597 default: break;
1598 case ISD::BRCONDTWOWAY:
1599 if (N1C)
1600 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001601 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001602 else // Unconditional branch to false dest.
Chris Lattnerd5531332005-05-14 06:20:26 +00001603 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001604 break;
Chris Lattner36db1ed2005-07-10 00:29:18 +00001605
1606 case ISD::TRUNCSTORE: {
1607 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1608 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1609#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1610 // If this is a truncating store of a constant, convert to the desired type
1611 // and store it instead.
1612 if (isa<Constant>(Ops[0])) {
1613 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1614 if (isa<Constant>(Op))
1615 N1 = Op;
1616 }
1617 // Also for ConstantFP?
1618#endif
1619 if (Ops[0].getValueType() == EVT) // Normal store?
1620 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1621 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1622 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1623 "Can't do FP-INT conversion!");
1624 break;
1625 }
Chris Lattnerb0713c72005-04-09 03:27:28 +00001626 }
1627
Chris Lattner566307f2005-05-14 07:42:29 +00001628 // Memoize nodes.
1629 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1630 if (N) return SDOperand(N, 0);
1631 N = new SDNode(Opcode, Ops);
Chris Lattner669e8c22005-05-14 07:25:05 +00001632 N->setValueTypes(VT);
Chris Lattnerb0713c72005-04-09 03:27:28 +00001633 AllNodes.push_back(N);
1634 return SDOperand(N, 0);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001635}
1636
Chris Lattnerd5531332005-05-14 06:20:26 +00001637SDOperand SelectionDAG::getNode(unsigned Opcode,
1638 std::vector<MVT::ValueType> &ResultTys,
1639 std::vector<SDOperand> &Ops) {
1640 if (ResultTys.size() == 1)
1641 return getNode(Opcode, ResultTys[0], Ops);
1642
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001643 switch (Opcode) {
1644 case ISD::EXTLOAD:
1645 case ISD::SEXTLOAD:
1646 case ISD::ZEXTLOAD: {
1647 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1648 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1649 // If they are asking for an extending load from/to the same thing, return a
1650 // normal load.
1651 if (ResultTys[0] == EVT)
1652 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1653 assert(EVT < ResultTys[0] &&
1654 "Should only be an extending load, not truncating!");
1655 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1656 "Cannot sign/zero extend a FP load!");
1657 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1658 "Cannot convert from FP to Int or Int -> FP!");
1659 break;
1660 }
1661
Chris Lattner669e8c22005-05-14 07:25:05 +00001662 // FIXME: figure out how to safely handle things like
1663 // int foo(int x) { return 1 << (x & 255); }
1664 // int bar() { return foo(256); }
1665#if 0
Chris Lattner669e8c22005-05-14 07:25:05 +00001666 case ISD::SRA_PARTS:
1667 case ISD::SRL_PARTS:
1668 case ISD::SHL_PARTS:
1669 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner0b6ba902005-07-10 00:07:11 +00001670 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattner669e8c22005-05-14 07:25:05 +00001671 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1672 else if (N3.getOpcode() == ISD::AND)
1673 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1674 // If the and is only masking out bits that cannot effect the shift,
1675 // eliminate the and.
1676 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1677 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1678 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1679 }
1680 break;
Chris Lattner669e8c22005-05-14 07:25:05 +00001681#endif
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001682 }
Chris Lattnerd5531332005-05-14 06:20:26 +00001683
1684 // Memoize the node.
1685 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1686 Ops))];
1687 if (N) return SDOperand(N, 0);
1688 N = new SDNode(Opcode, Ops);
1689 N->setValueTypes(ResultTys);
Chris Lattner006f56b2005-05-14 06:42:57 +00001690 AllNodes.push_back(N);
Chris Lattnerd5531332005-05-14 06:20:26 +00001691 return SDOperand(N, 0);
1692}
1693
Chris Lattner19732782005-08-16 18:17:10 +00001694
1695/// SelectNodeTo - These are used for target selectors to *mutate* the
1696/// specified node to have the specified return type, Target opcode, and
1697/// operands. Note that target opcodes are stored as
1698/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1699void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1700 unsigned TargetOpc, SDOperand Op1) {
1701 RemoveNodeFromCSEMaps(N);
1702 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1703 N->setValueTypes(VT);
1704 N->setOperands(Op1);
1705}
1706void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1707 unsigned TargetOpc, SDOperand Op1,
1708 SDOperand Op2) {
1709 RemoveNodeFromCSEMaps(N);
1710 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1711 N->setValueTypes(VT);
1712 N->setOperands(Op1, Op2);
1713}
1714void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1715 unsigned TargetOpc, SDOperand Op1,
1716 SDOperand Op2, SDOperand Op3) {
1717 RemoveNodeFromCSEMaps(N);
1718 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1719 N->setValueTypes(VT);
1720 N->setOperands(Op1, Op2, Op3);
1721}
1722
1723
Chris Lattner40e79822005-01-12 18:37:47 +00001724/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1725/// indicated value. This method ignores uses of other values defined by this
1726/// operation.
1727bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1728 assert(Value < getNumValues() && "Bad value!");
1729
1730 // If there is only one value, this is easy.
1731 if (getNumValues() == 1)
1732 return use_size() == NUses;
1733 if (Uses.size() < NUses) return false;
1734
1735 SDOperand TheValue(this, Value);
1736
1737 std::set<SDNode*> UsersHandled;
1738
1739 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1740 UI != E; ++UI) {
1741 SDNode *User = *UI;
1742 if (User->getNumOperands() == 1 ||
1743 UsersHandled.insert(User).second) // First time we've seen this?
1744 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1745 if (User->getOperand(i) == TheValue) {
1746 if (NUses == 0)
1747 return false; // too many uses
1748 --NUses;
1749 }
1750 }
1751
1752 // Found exactly the right number of uses?
1753 return NUses == 0;
1754}
1755
1756
Chris Lattnerbc892262005-08-16 18:33:07 +00001757const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattner9e4c7612005-01-10 23:25:25 +00001758 switch (getOpcode()) {
Chris Lattnerbc892262005-08-16 18:33:07 +00001759 default:
1760 if (getOpcode() < ISD::BUILTIN_OP_END)
1761 return "<<Unknown DAG Node>>";
1762 else {
1763 if (G)
1764 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1765 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1766 return "<<Unknown Target Node>>";
1767 }
1768
Andrew Lenharthdec53922005-03-31 21:24:06 +00001769 case ISD::PCMARKER: return "PCMarker";
Chris Lattner9440d6e2005-05-09 04:08:27 +00001770 case ISD::SRCVALUE: return "SrcValue";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001771 case ISD::EntryToken: return "EntryToken";
Chris Lattner4b1be0d2005-01-13 17:59:10 +00001772 case ISD::TokenFactor: return "TokenFactor";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001773 case ISD::Constant: return "Constant";
1774 case ISD::ConstantFP: return "ConstantFP";
1775 case ISD::GlobalAddress: return "GlobalAddress";
1776 case ISD::FrameIndex: return "FrameIndex";
1777 case ISD::BasicBlock: return "BasicBlock";
1778 case ISD::ExternalSymbol: return "ExternalSymbol";
1779 case ISD::ConstantPool: return "ConstantPoolIndex";
1780 case ISD::CopyToReg: return "CopyToReg";
1781 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattnere727af02005-01-13 20:50:02 +00001782 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemancda9aa72005-04-01 22:34:39 +00001783 case ISD::UNDEF: return "undef";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001784
Chris Lattnerc4a20462005-04-02 04:58:41 +00001785 // Unary operators
1786 case ISD::FABS: return "fabs";
1787 case ISD::FNEG: return "fneg";
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001788 case ISD::FSQRT: return "fsqrt";
1789 case ISD::FSIN: return "fsin";
1790 case ISD::FCOS: return "fcos";
Chris Lattnerc4a20462005-04-02 04:58:41 +00001791
1792 // Binary operators
Chris Lattner9e4c7612005-01-10 23:25:25 +00001793 case ISD::ADD: return "add";
1794 case ISD::SUB: return "sub";
1795 case ISD::MUL: return "mul";
Nate Begeman55e86252005-04-05 22:36:56 +00001796 case ISD::MULHU: return "mulhu";
1797 case ISD::MULHS: return "mulhs";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001798 case ISD::SDIV: return "sdiv";
1799 case ISD::UDIV: return "udiv";
1800 case ISD::SREM: return "srem";
1801 case ISD::UREM: return "urem";
1802 case ISD::AND: return "and";
1803 case ISD::OR: return "or";
1804 case ISD::XOR: return "xor";
1805 case ISD::SHL: return "shl";
1806 case ISD::SRA: return "sra";
1807 case ISD::SRL: return "srl";
1808
Chris Lattnerd47675e2005-08-09 20:20:18 +00001809 case ISD::SETCC: return "setcc";
1810 case ISD::SELECT: return "select";
Nate Begemane5b86d72005-08-10 20:51:12 +00001811 case ISD::SELECT_CC: return "select_cc";
Chris Lattner1fe9b402005-01-20 18:50:55 +00001812 case ISD::ADD_PARTS: return "add_parts";
1813 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner5b7bb562005-04-02 03:30:42 +00001814 case ISD::SHL_PARTS: return "shl_parts";
1815 case ISD::SRA_PARTS: return "sra_parts";
1816 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001817
Chris Lattner2f82d2d2005-04-28 21:44:03 +00001818 // Conversion operators.
Chris Lattner9e4c7612005-01-10 23:25:25 +00001819 case ISD::SIGN_EXTEND: return "sign_extend";
1820 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001821 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001822 case ISD::TRUNCATE: return "truncate";
1823 case ISD::FP_ROUND: return "fp_round";
Chris Lattner1001c6e2005-01-15 06:17:04 +00001824 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001825 case ISD::FP_EXTEND: return "fp_extend";
1826
1827 case ISD::SINT_TO_FP: return "sint_to_fp";
1828 case ISD::UINT_TO_FP: return "uint_to_fp";
1829 case ISD::FP_TO_SINT: return "fp_to_sint";
1830 case ISD::FP_TO_UINT: return "fp_to_uint";
1831
1832 // Control flow instructions
1833 case ISD::BR: return "br";
1834 case ISD::BRCOND: return "brcond";
Chris Lattnerb0713c72005-04-09 03:27:28 +00001835 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001836 case ISD::RET: return "ret";
1837 case ISD::CALL: return "call";
Chris Lattnerd0feb642005-05-13 18:43:43 +00001838 case ISD::TAILCALL:return "tailcall";
Chris Lattnere3677d62005-05-12 23:51:40 +00001839 case ISD::CALLSEQ_START: return "callseq_start";
1840 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001841
1842 // Other operators
1843 case ISD::LOAD: return "load";
1844 case ISD::STORE: return "store";
Chris Lattner39c67442005-01-14 22:08:15 +00001845 case ISD::EXTLOAD: return "extload";
1846 case ISD::SEXTLOAD: return "sextload";
1847 case ISD::ZEXTLOAD: return "zextload";
1848 case ISD::TRUNCSTORE: return "truncstore";
1849
Chris Lattner9e4c7612005-01-10 23:25:25 +00001850 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1851 case ISD::EXTRACT_ELEMENT: return "extract_element";
1852 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner844277f2005-01-11 05:57:01 +00001853 case ISD::MEMSET: return "memset";
1854 case ISD::MEMCPY: return "memcpy";
1855 case ISD::MEMMOVE: return "memmove";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001856
Chris Lattner93f4f5f2005-05-11 04:50:30 +00001857 // Bit counting
1858 case ISD::CTPOP: return "ctpop";
1859 case ISD::CTTZ: return "cttz";
1860 case ISD::CTLZ: return "ctlz";
1861
1862 // IO Intrinsics
Chris Lattner67ab9452005-05-09 20:22:17 +00001863 case ISD::READPORT: return "readport";
1864 case ISD::WRITEPORT: return "writeport";
1865 case ISD::READIO: return "readio";
1866 case ISD::WRITEIO: return "writeio";
1867
Chris Lattnerd47675e2005-08-09 20:20:18 +00001868 case ISD::CONDCODE:
1869 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattner9e4c7612005-01-10 23:25:25 +00001870 default: assert(0 && "Unknown setcc condition!");
Chris Lattnerd47675e2005-08-09 20:20:18 +00001871 case ISD::SETOEQ: return "setoeq";
1872 case ISD::SETOGT: return "setogt";
1873 case ISD::SETOGE: return "setoge";
1874 case ISD::SETOLT: return "setolt";
1875 case ISD::SETOLE: return "setole";
1876 case ISD::SETONE: return "setone";
Misha Brukman835702a2005-04-21 22:36:52 +00001877
Chris Lattnerd47675e2005-08-09 20:20:18 +00001878 case ISD::SETO: return "seto";
1879 case ISD::SETUO: return "setuo";
1880 case ISD::SETUEQ: return "setue";
1881 case ISD::SETUGT: return "setugt";
1882 case ISD::SETUGE: return "setuge";
1883 case ISD::SETULT: return "setult";
1884 case ISD::SETULE: return "setule";
1885 case ISD::SETUNE: return "setune";
Misha Brukman835702a2005-04-21 22:36:52 +00001886
Chris Lattnerd47675e2005-08-09 20:20:18 +00001887 case ISD::SETEQ: return "seteq";
1888 case ISD::SETGT: return "setgt";
1889 case ISD::SETGE: return "setge";
1890 case ISD::SETLT: return "setlt";
1891 case ISD::SETLE: return "setle";
1892 case ISD::SETNE: return "setne";
Chris Lattner9e4c7612005-01-10 23:25:25 +00001893 }
1894 }
1895}
Chris Lattner061a1ea2005-01-07 07:46:32 +00001896
Chris Lattnerbc892262005-08-16 18:33:07 +00001897void SDNode::dump() const { dump(0); }
1898void SDNode::dump(const SelectionDAG *G) const {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001899 std::cerr << (void*)this << ": ";
1900
1901 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1902 if (i) std::cerr << ",";
Chris Lattner09d1b3d2005-01-15 07:14:32 +00001903 if (getValueType(i) == MVT::Other)
1904 std::cerr << "ch";
1905 else
1906 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattner061a1ea2005-01-07 07:46:32 +00001907 }
Chris Lattnerbc892262005-08-16 18:33:07 +00001908 std::cerr << " = " << getOperationName(G);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001909
1910 std::cerr << " ";
1911 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1912 if (i) std::cerr << ", ";
1913 std::cerr << (void*)getOperand(i).Val;
1914 if (unsigned RN = getOperand(i).ResNo)
1915 std::cerr << ":" << RN;
1916 }
1917
1918 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1919 std::cerr << "<" << CSDN->getValue() << ">";
1920 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1921 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukman835702a2005-04-21 22:36:52 +00001922 } else if (const GlobalAddressSDNode *GADN =
Chris Lattner061a1ea2005-01-07 07:46:32 +00001923 dyn_cast<GlobalAddressSDNode>(this)) {
1924 std::cerr << "<";
1925 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukman77451162005-04-22 04:01:18 +00001926 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001927 std::cerr << "<" << FIDN->getIndex() << ">";
1928 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1929 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukman77451162005-04-22 04:01:18 +00001930 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001931 std::cerr << "<";
1932 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1933 if (LBB)
1934 std::cerr << LBB->getName() << " ";
1935 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnere727af02005-01-13 20:50:02 +00001936 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattner061a1ea2005-01-07 07:46:32 +00001937 std::cerr << "<reg #" << C2V->getReg() << ">";
1938 } else if (const ExternalSymbolSDNode *ES =
1939 dyn_cast<ExternalSymbolSDNode>(this)) {
1940 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner9440d6e2005-05-09 04:08:27 +00001941 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1942 if (M->getValue())
1943 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1944 else
1945 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattner061a1ea2005-01-07 07:46:32 +00001946 }
Chris Lattner061a1ea2005-01-07 07:46:32 +00001947}
1948
Chris Lattnerbc892262005-08-16 18:33:07 +00001949static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001950 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1951 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerbc892262005-08-16 18:33:07 +00001952 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnere6f78822005-01-09 20:38:33 +00001953 else
1954 std::cerr << "\n" << std::string(indent+2, ' ')
1955 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukman835702a2005-04-21 22:36:52 +00001956
Chris Lattnere6f78822005-01-09 20:38:33 +00001957
1958 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerbc892262005-08-16 18:33:07 +00001959 N->dump(G);
Chris Lattnere6f78822005-01-09 20:38:33 +00001960}
1961
Chris Lattner061a1ea2005-01-07 07:46:32 +00001962void SelectionDAG::dump() const {
1963 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner1270acc2005-01-09 20:26:36 +00001964 std::vector<SDNode*> Nodes(AllNodes);
1965 std::sort(Nodes.begin(), Nodes.end());
1966
1967 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnere6f78822005-01-09 20:38:33 +00001968 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerbc892262005-08-16 18:33:07 +00001969 DumpNodes(Nodes[i], 2, this);
Chris Lattner061a1ea2005-01-07 07:46:32 +00001970 }
Chris Lattnere6f78822005-01-09 20:38:33 +00001971
Chris Lattnerbc892262005-08-16 18:33:07 +00001972 DumpNodes(getRoot().Val, 2, this);
Chris Lattnere6f78822005-01-09 20:38:33 +00001973
Chris Lattner061a1ea2005-01-07 07:46:32 +00001974 std::cerr << "\n\n";
1975}
1976