blob: ef7ffb246016e2427c293585a319a76e4b46021e [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner78ec3112003-08-11 14:57:33 +00009//
Chris Lattnerc3aae252005-01-07 07:46:32 +000010// This implements the SelectionDAG class.
Chris Lattner78ec3112003-08-11 14:57:33 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000015#include "llvm/Constants.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/Assembly/Writer.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000019#include "llvm/Support/MathExtras.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000020#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000021#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetMachine.h"
Reid Spencer954da372004-07-04 12:19:56 +000023#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000024#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000025#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000026#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner5cdcc582005-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 Lattner5cdcc582005-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 Lattner7cf7e3f2005-08-09 20:20:18 +000055 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000056 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000057 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000058}
59
60
Chris Lattnerc3aae252005-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 Brukmanedf128a2005-04-21 22:36:52 +0000115
Chris Lattnerc3aae252005-01-07 07:46:32 +0000116 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000117
Chris Lattnerc3aae252005-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 Brukmanedf128a2005-04-21 22:36:52 +0000133 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000134
135 // Combine all of the condition bits.
136 return ISD::CondCode(Op1 & Op2);
137}
138
Chris Lattnerb48da392005-01-23 04:39:44 +0000139const TargetMachine &SelectionDAG::getTarget() const {
140 return TLI.getTargetMachine();
141}
142
143
Chris Lattner0e12e6e2005-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 Lattner149c58c2005-08-16 18:17:10 +0000182
Chris Lattner0e12e6e2005-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 Lattner149c58c2005-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 Lattner0e12e6e2005-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 Lattner37bfbb42005-08-17 00:34:06 +0000221 case ISD::TargetConstant:
222 TargetConstants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
223 N->getValueType(0)));
224 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000225 case ISD::ConstantFP: {
226 union {
227 double DV;
228 uint64_t IV;
229 };
230 DV = cast<ConstantFPSDNode>(N)->getValue();
231 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000232 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000233 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000234 case ISD::CONDCODE:
235 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
236 "Cond code doesn't exist!");
237 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
238 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000239 case ISD::GlobalAddress:
240 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
241 break;
242 case ISD::FrameIndex:
243 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
244 break;
245 case ISD::ConstantPool:
246 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
247 break;
248 case ISD::BasicBlock:
249 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
250 break;
251 case ISD::ExternalSymbol:
252 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
253 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000254 case ISD::VALUETYPE:
255 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
256 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000257 case ISD::Register:
258 RegNodes[cast<RegisterSDNode>(N)->getReg()] = 0;
259 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000260 case ISD::SRCVALUE: {
261 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
262 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
263 break;
264 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265 case ISD::LOAD:
266 Loads.erase(std::make_pair(N->getOperand(1),
267 std::make_pair(N->getOperand(0),
268 N->getValueType(0))));
269 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000270 default:
271 if (N->getNumOperands() == 1)
272 UnaryOps.erase(std::make_pair(N->getOpcode(),
273 std::make_pair(N->getOperand(0),
274 N->getValueType(0))));
275 else if (N->getNumOperands() == 2)
276 BinaryOps.erase(std::make_pair(N->getOpcode(),
277 std::make_pair(N->getOperand(0),
278 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000279 else if (N->getNumValues() == 1) {
280 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
281 OneResultNodes.erase(std::make_pair(N->getOpcode(),
282 std::make_pair(N->getValueType(0),
283 Ops)));
284 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000285 // Remove the node from the ArbitraryNodes map.
286 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
287 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
288 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
289 std::make_pair(RV, Ops)));
290 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000291 break;
292 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000293}
294
295
Chris Lattner78ec3112003-08-11 14:57:33 +0000296SelectionDAG::~SelectionDAG() {
297 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
298 delete AllNodes[i];
299}
300
Chris Lattner0f2287b2005-04-13 02:38:18 +0000301SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000302 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000303 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000304 return getNode(ISD::AND, Op.getValueType(), Op,
305 getConstant(Imm, Op.getValueType()));
306}
307
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
309 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
310 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000311 if (VT != MVT::i64)
312 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000313
Chris Lattnerc3aae252005-01-07 07:46:32 +0000314 SDNode *&N = Constants[std::make_pair(Val, VT)];
315 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000316 N = new ConstantSDNode(false, Val, VT);
317 AllNodes.push_back(N);
318 return SDOperand(N, 0);
319}
320
321SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
322 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
323 // Mask out any bits that are not valid for this constant.
324 if (VT != MVT::i64)
325 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
326
327 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
328 if (N) return SDOperand(N, 0);
329 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000330 AllNodes.push_back(N);
331 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000332}
333
Chris Lattnerc3aae252005-01-07 07:46:32 +0000334SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
335 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
336 if (VT == MVT::f32)
337 Val = (float)Val; // Mask out extra precision.
338
Chris Lattnerd8658612005-02-17 20:17:32 +0000339 // Do the map lookup using the actual bit pattern for the floating point
340 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
341 // we don't have issues with SNANs.
342 union {
343 double DV;
344 uint64_t IV;
345 };
Misha Brukmanedf128a2005-04-21 22:36:52 +0000346
Chris Lattnerd8658612005-02-17 20:17:32 +0000347 DV = Val;
348
349 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000350 if (N) return SDOperand(N, 0);
351 N = new ConstantFPSDNode(Val, VT);
352 AllNodes.push_back(N);
353 return SDOperand(N, 0);
354}
355
356
357
358SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
359 MVT::ValueType VT) {
360 SDNode *&N = GlobalValues[GV];
361 if (N) return SDOperand(N, 0);
362 N = new GlobalAddressSDNode(GV,VT);
363 AllNodes.push_back(N);
364 return SDOperand(N, 0);
365}
366
367SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
368 SDNode *&N = FrameIndices[FI];
369 if (N) return SDOperand(N, 0);
370 N = new FrameIndexSDNode(FI, VT);
371 AllNodes.push_back(N);
372 return SDOperand(N, 0);
373}
374
375SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
376 SDNode *N = ConstantPoolIndices[CPIdx];
377 if (N) return SDOperand(N, 0);
378 N = new ConstantPoolSDNode(CPIdx, VT);
379 AllNodes.push_back(N);
380 return SDOperand(N, 0);
381}
382
383SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
384 SDNode *&N = BBNodes[MBB];
385 if (N) return SDOperand(N, 0);
386 N = new BasicBlockSDNode(MBB);
387 AllNodes.push_back(N);
388 return SDOperand(N, 0);
389}
390
Chris Lattner15e4b012005-07-10 00:07:11 +0000391SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
392 if ((unsigned)VT >= ValueTypeNodes.size())
393 ValueTypeNodes.resize(VT+1);
394 if (ValueTypeNodes[VT] == 0) {
395 ValueTypeNodes[VT] = new VTSDNode(VT);
396 AllNodes.push_back(ValueTypeNodes[VT]);
397 }
398
399 return SDOperand(ValueTypeNodes[VT], 0);
400}
401
Chris Lattnerc3aae252005-01-07 07:46:32 +0000402SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
403 SDNode *&N = ExternalSymbols[Sym];
404 if (N) return SDOperand(N, 0);
405 N = new ExternalSymbolSDNode(Sym, VT);
406 AllNodes.push_back(N);
407 return SDOperand(N, 0);
408}
409
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000410SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
411 if ((unsigned)Cond >= CondCodeNodes.size())
412 CondCodeNodes.resize(Cond+1);
413
Chris Lattner079a27a2005-08-09 20:40:02 +0000414 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000415 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000416 AllNodes.push_back(CondCodeNodes[Cond]);
417 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000418 return SDOperand(CondCodeNodes[Cond], 0);
419}
420
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000421SDOperand SelectionDAG::getRegister(unsigned Reg, MVT::ValueType VT) {
422 if (Reg >= RegNodes.size())
423 RegNodes.resize(Reg+1);
424 RegisterSDNode *&Result = RegNodes[Reg];
425 if (Result) {
426 assert(Result->getValueType(0) == VT &&
427 "Inconsistent value types for machine registers");
428 } else {
429 Result = new RegisterSDNode(Reg, VT);
430 AllNodes.push_back(Result);
431 }
432 return SDOperand(Result, 0);
433}
434
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000435SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
436 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000437 // These setcc operations always fold.
438 switch (Cond) {
439 default: break;
440 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000441 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000442 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000443 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000444 }
445
Chris Lattner67255a12005-04-07 18:14:58 +0000446 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
447 uint64_t C2 = N2C->getValue();
448 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
449 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000450
Chris Lattnerc3aae252005-01-07 07:46:32 +0000451 // Sign extend the operands if required
452 if (ISD::isSignedIntSetCC(Cond)) {
453 C1 = N1C->getSignExtended();
454 C2 = N2C->getSignExtended();
455 }
456
457 switch (Cond) {
458 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000459 case ISD::SETEQ: return getConstant(C1 == C2, VT);
460 case ISD::SETNE: return getConstant(C1 != C2, VT);
461 case ISD::SETULT: return getConstant(C1 < C2, VT);
462 case ISD::SETUGT: return getConstant(C1 > C2, VT);
463 case ISD::SETULE: return getConstant(C1 <= C2, VT);
464 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
465 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
466 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
467 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
468 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000469 }
Chris Lattner24673922005-04-07 18:58:54 +0000470 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000471 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
472 // the comparison on the input.
473 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
474 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
475
476 // If the comparison constant has bits in the upper part, the
477 // zero-extended value could never match.
478 if (C2 & (~0ULL << InSize)) {
479 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
480 switch (Cond) {
481 case ISD::SETUGT:
482 case ISD::SETUGE:
483 case ISD::SETEQ: return getConstant(0, VT);
484 case ISD::SETULT:
485 case ISD::SETULE:
486 case ISD::SETNE: return getConstant(1, VT);
487 case ISD::SETGT:
488 case ISD::SETGE:
489 // True if the sign bit of C2 is set.
490 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
491 case ISD::SETLT:
492 case ISD::SETLE:
493 // True if the sign bit of C2 isn't set.
494 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
495 default:
496 break;
497 }
498 }
499
500 // Otherwise, we can perform the comparison with the low bits.
501 switch (Cond) {
502 case ISD::SETEQ:
503 case ISD::SETNE:
504 case ISD::SETUGT:
505 case ISD::SETUGE:
506 case ISD::SETULT:
507 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000508 return getSetCC(VT, N1.getOperand(0),
509 getConstant(C2, N1.getOperand(0).getValueType()),
510 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000511 default:
512 break; // todo, be more careful with signed comparisons
513 }
514 }
515
Chris Lattner67255a12005-04-07 18:14:58 +0000516 uint64_t MinVal, MaxVal;
517 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
518 if (ISD::isSignedIntSetCC(Cond)) {
519 MinVal = 1ULL << (OperandBitSize-1);
520 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
521 MaxVal = ~0ULL >> (65-OperandBitSize);
522 else
523 MaxVal = 0;
524 } else {
525 MinVal = 0;
526 MaxVal = ~0ULL >> (64-OperandBitSize);
527 }
528
529 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
530 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
531 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
532 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000533 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
534 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000535 }
536
537 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
538 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
539 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000540 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
541 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000542 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000543
Nate Begeman72ea2812005-04-14 08:56:52 +0000544 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
545 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000546
Nate Begeman72ea2812005-04-14 08:56:52 +0000547 // Canonicalize setgt X, Min --> setne X, Min
548 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000549 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000550
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000551 // If we have setult X, 1, turn it into seteq X, 0
552 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000553 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
554 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000555 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000556 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000557 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
558 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000559
560 // If we have "setcc X, C1", check to see if we can shrink the immediate
561 // by changing cc.
562
563 // SETUGT X, SINTMAX -> SETLT X, 0
564 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
565 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000566 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000567
568 // FIXME: Implement the rest of these.
569
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000570
571 // Fold bit comparisons when we can.
572 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
573 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
574 if (ConstantSDNode *AndRHS =
575 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
576 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
577 // Perform the xform if the AND RHS is a single bit.
578 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
579 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000580 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000581 TLI.getShiftAmountTy()));
582 }
583 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
584 // (X & 8) == 8 --> (X & 8) >> 3
585 // Perform the xform if C2 is a single bit.
586 if ((C2 & (C2-1)) == 0) {
587 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000588 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000589 }
590 }
591 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000592 }
Chris Lattner67255a12005-04-07 18:14:58 +0000593 } else if (isa<ConstantSDNode>(N1.Val)) {
594 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000595 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000596 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000597
598 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
599 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
600 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000601
Chris Lattnerc3aae252005-01-07 07:46:32 +0000602 switch (Cond) {
603 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000604 case ISD::SETEQ: return getConstant(C1 == C2, VT);
605 case ISD::SETNE: return getConstant(C1 != C2, VT);
606 case ISD::SETLT: return getConstant(C1 < C2, VT);
607 case ISD::SETGT: return getConstant(C1 > C2, VT);
608 case ISD::SETLE: return getConstant(C1 <= C2, VT);
609 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000610 }
611 } else {
612 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000613 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000614 }
615
616 if (N1 == N2) {
617 // We can always fold X == Y for integer setcc's.
618 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000619 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000620 unsigned UOF = ISD::getUnorderedFlavor(Cond);
621 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000622 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000623 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000624 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000625 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
626 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000627 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
628 if (NewCond != Cond)
629 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000630 }
631
Chris Lattner5cdcc582005-01-09 20:52:51 +0000632 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000633 MVT::isInteger(N1.getValueType())) {
634 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
635 N1.getOpcode() == ISD::XOR) {
636 // Simplify (X+Y) == (X+Z) --> Y == Z
637 if (N1.getOpcode() == N2.getOpcode()) {
638 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000639 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000640 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000641 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000642 if (isCommutativeBinOp(N1.getOpcode())) {
643 // If X op Y == Y op X, try other combinations.
644 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000645 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000646 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000647 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000648 }
649 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000650
651 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000652
Chris Lattner68dc3102005-01-10 02:03:02 +0000653 // Simplify (X+Z) == X --> Z == 0
654 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000655 return getSetCC(VT, N1.getOperand(1),
656 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000657 if (N1.getOperand(1) == N2) {
658 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000659 return getSetCC(VT, N1.getOperand(0),
660 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000661 else {
662 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
663 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000664 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000665 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000666 N2, getConstant(1, TLI.getShiftAmountTy())),
667 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000668 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000669 }
670 }
671
Chris Lattner68dc3102005-01-10 02:03:02 +0000672 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
673 N2.getOpcode() == ISD::XOR) {
674 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000675 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000676 return getSetCC(VT, N2.getOperand(1),
677 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000678 } else if (N2.getOperand(1) == N1) {
679 if (isCommutativeBinOp(N2.getOpcode())) {
680 return getSetCC(VT, N2.getOperand(0),
681 getConstant(0, N2.getValueType()), Cond);
682 } else {
683 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
684 // X == (Z-X) --> X<<1 == Z
685 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
686 getConstant(1, TLI.getShiftAmountTy())),
687 N2.getOperand(0), Cond);
688 }
689 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000690 }
691 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000692
Chris Lattnerfda2b552005-04-18 04:48:12 +0000693 // Fold away ALL boolean setcc's.
694 if (N1.getValueType() == MVT::i1) {
695 switch (Cond) {
696 default: assert(0 && "Unknown integer setcc!");
697 case ISD::SETEQ: // X == Y -> (X^Y)^1
698 N1 = getNode(ISD::XOR, MVT::i1,
699 getNode(ISD::XOR, MVT::i1, N1, N2),
700 getConstant(1, MVT::i1));
701 break;
702 case ISD::SETNE: // X != Y --> (X^Y)
703 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
704 break;
705 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
706 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
707 N1 = getNode(ISD::AND, MVT::i1, N2,
708 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
709 break;
710 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
711 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
712 N1 = getNode(ISD::AND, MVT::i1, N1,
713 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
714 break;
715 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
716 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
717 N1 = getNode(ISD::OR, MVT::i1, N2,
718 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
719 break;
720 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
721 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
722 N1 = getNode(ISD::OR, MVT::i1, N1,
723 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
724 break;
725 }
726 if (VT != MVT::i1)
727 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
728 return N1;
729 }
730
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000731 // Could not fold it.
732 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733}
734
Nate Begemanff663682005-08-13 06:14:17 +0000735SDOperand SelectionDAG::SimplifySelectCC(SDOperand N1, SDOperand N2,
736 SDOperand N3, SDOperand N4,
737 ISD::CondCode CC) {
738 MVT::ValueType VT = N3.getValueType();
Nate Begeman32c392a2005-08-13 06:00:21 +0000739 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
740 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
741 ConstantSDNode *N4C = dyn_cast<ConstantSDNode>(N4.Val);
742
743 // Check to see if we can simplify the select into an fabs node
744 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N2)) {
745 // Allow either -0.0 or 0.0
746 if (CFP->getValue() == 0.0) {
747 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
748 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
749 N1 == N3 && N4.getOpcode() == ISD::FNEG &&
750 N1 == N4.getOperand(0))
751 return getNode(ISD::FABS, VT, N1);
752
753 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
754 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
755 N1 == N4 && N3.getOpcode() == ISD::FNEG &&
756 N3.getOperand(0) == N4)
757 return getNode(ISD::FABS, VT, N4);
758 }
759 }
760
761 // Check to see if we can perform the "gzip trick", transforming
762 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
763 if (N2C && N2C->isNullValue() && N4C && N4C->isNullValue() &&
764 MVT::isInteger(N1.getValueType()) &&
765 MVT::isInteger(N3.getValueType()) && CC == ISD::SETLT) {
766 MVT::ValueType XType = N1.getValueType();
767 MVT::ValueType AType = N3.getValueType();
768 if (XType >= AType) {
769 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
770 // single-bit constant. FIXME: remove once the dag combiner
771 // exists.
772 if (N3C && ((N3C->getValue() & (N3C->getValue()-1)) == 0)) {
773 unsigned ShCtV = Log2_64(N3C->getValue());
774 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
775 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
776 SDOperand Shift = getNode(ISD::SRL, XType, N1, ShCt);
777 if (XType > AType)
778 Shift = getNode(ISD::TRUNCATE, AType, Shift);
779 return getNode(ISD::AND, AType, Shift, N3);
780 }
781 SDOperand Shift = getNode(ISD::SRA, XType, N1,
782 getConstant(MVT::getSizeInBits(XType)-1,
783 TLI.getShiftAmountTy()));
784 if (XType > AType)
785 Shift = getNode(ISD::TRUNCATE, AType, Shift);
786 return getNode(ISD::AND, AType, Shift, N3);
787 }
788 }
789
790 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
791 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
792 if (N2C && N2C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
793 N1 == N4 && N3.getOpcode() == ISD::SUB && N1 == N3.getOperand(1)) {
794 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
795 MVT::ValueType XType = N1.getValueType();
796 if (SubC->isNullValue() && MVT::isInteger(XType)) {
797 SDOperand Shift = getNode(ISD::SRA, XType, N1,
798 getConstant(MVT::getSizeInBits(XType)-1,
799 TLI.getShiftAmountTy()));
800 return getNode(ISD::XOR, XType, getNode(ISD::ADD, XType, N1, Shift),
801 Shift);
802 }
803 }
804 }
805
806 // Could not fold it.
807 return SDOperand();
808}
809
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000811///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
813 SDNode *N = new SDNode(Opcode, VT);
814 AllNodes.push_back(N);
815 return SDOperand(N, 0);
816}
817
Chris Lattnerc3aae252005-01-07 07:46:32 +0000818SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
819 SDOperand Operand) {
820 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
821 uint64_t Val = C->getValue();
822 switch (Opcode) {
823 default: break;
824 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
825 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
826 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000827 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
828 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000829 }
830 }
831
832 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
833 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000834 case ISD::FNEG:
835 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000836 case ISD::FP_ROUND:
837 case ISD::FP_EXTEND:
838 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000839 case ISD::FP_TO_SINT:
840 return getConstant((int64_t)C->getValue(), VT);
841 case ISD::FP_TO_UINT:
842 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000843 }
844
845 unsigned OpOpcode = Operand.Val->getOpcode();
846 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000847 case ISD::TokenFactor:
848 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849 case ISD::SIGN_EXTEND:
850 if (Operand.getValueType() == VT) return Operand; // noop extension
851 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
852 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
853 break;
854 case ISD::ZERO_EXTEND:
855 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000856 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000857 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000858 break;
859 case ISD::TRUNCATE:
860 if (Operand.getValueType() == VT) return Operand; // noop truncate
861 if (OpOpcode == ISD::TRUNCATE)
862 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000863 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
864 // If the source is smaller than the dest, we still need an extend.
865 if (Operand.Val->getOperand(0).getValueType() < VT)
866 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
867 else if (Operand.Val->getOperand(0).getValueType() > VT)
868 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
869 else
870 return Operand.Val->getOperand(0);
871 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000872 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000873 case ISD::FNEG:
874 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
875 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
876 Operand.Val->getOperand(0));
877 if (OpOpcode == ISD::FNEG) // --X -> X
878 return Operand.Val->getOperand(0);
879 break;
880 case ISD::FABS:
881 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
882 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
883 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000884 }
885
886 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
887 if (N) return SDOperand(N, 0);
888 N = new SDNode(Opcode, Operand);
889 N->setValueTypes(VT);
890 AllNodes.push_back(N);
891 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000892}
893
Chris Lattner36019aa2005-04-18 03:48:41 +0000894/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
895/// this predicate to simplify operations downstream. V and Mask are known to
896/// be the same type.
897static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
898 const TargetLowering &TLI) {
899 unsigned SrcBits;
900 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000901
Chris Lattner36019aa2005-04-18 03:48:41 +0000902 // If we know the result of a setcc has the top bits zero, use this info.
903 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +0000904 case ISD::Constant:
905 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
906
907 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000908 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +0000909 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
910
911 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +0000912 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +0000913 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
914 case ISD::ZERO_EXTEND:
915 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
916 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
917
918 case ISD::AND:
919 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +0000920 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +0000921 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
922
923 // FALL THROUGH
924 case ISD::OR:
925 case ISD::XOR:
926 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
927 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
928 case ISD::SELECT:
929 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
930 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +0000931
932 case ISD::SRL:
933 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
934 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
935 uint64_t NewVal = Mask << ShAmt->getValue();
936 SrcBits = MVT::getSizeInBits(Op.getValueType());
937 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
938 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
939 }
940 return false;
941 case ISD::SHL:
942 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
943 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
944 uint64_t NewVal = Mask >> ShAmt->getValue();
945 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
946 }
947 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +0000948 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +0000949 default: break;
950 }
951
952 return false;
953}
954
955
956
Chris Lattnerc3aae252005-01-07 07:46:32 +0000957SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
958 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000959#ifndef NDEBUG
960 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000961 case ISD::TokenFactor:
962 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
963 N2.getValueType() == MVT::Other && "Invalid token factor!");
964 break;
Chris Lattner76365122005-01-16 02:23:22 +0000965 case ISD::AND:
966 case ISD::OR:
967 case ISD::XOR:
968 case ISD::UDIV:
969 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000970 case ISD::MULHU:
971 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000972 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
973 // fall through
974 case ISD::ADD:
975 case ISD::SUB:
976 case ISD::MUL:
977 case ISD::SDIV:
978 case ISD::SREM:
979 assert(N1.getValueType() == N2.getValueType() &&
980 N1.getValueType() == VT && "Binary operator types must match!");
981 break;
982
983 case ISD::SHL:
984 case ISD::SRA:
985 case ISD::SRL:
986 assert(VT == N1.getValueType() &&
987 "Shift operators return type must be the same as their first arg");
988 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000989 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000990 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000991 case ISD::FP_ROUND_INREG: {
992 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
993 assert(VT == N1.getValueType() && "Not an inreg round!");
994 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
995 "Cannot FP_ROUND_INREG integer types");
996 assert(EVT <= VT && "Not rounding down!");
997 break;
998 }
999 case ISD::SIGN_EXTEND_INREG: {
1000 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1001 assert(VT == N1.getValueType() && "Not an inreg extend!");
1002 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1003 "Cannot *_EXTEND_INREG FP types");
1004 assert(EVT <= VT && "Not extending!");
1005 }
1006
Chris Lattner76365122005-01-16 02:23:22 +00001007 default: break;
1008 }
1009#endif
1010
Chris Lattnerc3aae252005-01-07 07:46:32 +00001011 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1012 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1013 if (N1C) {
1014 if (N2C) {
1015 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1016 switch (Opcode) {
1017 case ISD::ADD: return getConstant(C1 + C2, VT);
1018 case ISD::SUB: return getConstant(C1 - C2, VT);
1019 case ISD::MUL: return getConstant(C1 * C2, VT);
1020 case ISD::UDIV:
1021 if (C2) return getConstant(C1 / C2, VT);
1022 break;
1023 case ISD::UREM :
1024 if (C2) return getConstant(C1 % C2, VT);
1025 break;
1026 case ISD::SDIV :
1027 if (C2) return getConstant(N1C->getSignExtended() /
1028 N2C->getSignExtended(), VT);
1029 break;
1030 case ISD::SREM :
1031 if (C2) return getConstant(N1C->getSignExtended() %
1032 N2C->getSignExtended(), VT);
1033 break;
1034 case ISD::AND : return getConstant(C1 & C2, VT);
1035 case ISD::OR : return getConstant(C1 | C2, VT);
1036 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001037 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
1038 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
1039 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 default: break;
1041 }
1042
1043 } else { // Cannonicalize constant to RHS if commutative
1044 if (isCommutativeBinOp(Opcode)) {
1045 std::swap(N1C, N2C);
1046 std::swap(N1, N2);
1047 }
1048 }
Chris Lattner88218ef2005-01-19 17:29:49 +00001049
1050 switch (Opcode) {
1051 default: break;
1052 case ISD::SHL: // shl 0, X -> 0
1053 if (N1C->isNullValue()) return N1;
1054 break;
1055 case ISD::SRL: // srl 0, X -> 0
1056 if (N1C->isNullValue()) return N1;
1057 break;
1058 case ISD::SRA: // sra -1, X -> -1
1059 if (N1C->isAllOnesValue()) return N1;
1060 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001061 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
1062 // Extending a constant? Just return the extended constant.
1063 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
1064 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +00001065 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001066 }
1067
1068 if (N2C) {
1069 uint64_t C2 = N2C->getValue();
1070
1071 switch (Opcode) {
1072 case ISD::ADD:
1073 if (!C2) return N1; // add X, 0 -> X
1074 break;
1075 case ISD::SUB:
1076 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +00001077 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001078 case ISD::MUL:
1079 if (!C2) return N2; // mul X, 0 -> 0
1080 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
1081 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
1082
Chris Lattnerb48da392005-01-23 04:39:44 +00001083 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001084 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001085 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001086 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001087 }
1088 break;
1089
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001090 case ISD::MULHU:
1091 case ISD::MULHS:
1092 if (!C2) return N2; // mul X, 0 -> 0
1093
1094 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
1095 return getConstant(0, VT);
1096
1097 // Many others could be handled here, including -1, powers of 2, etc.
1098 break;
1099
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +00001101 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001102 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001103 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +00001104 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 }
1106 break;
1107
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001108 case ISD::SHL:
1109 case ISD::SRL:
1110 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +00001111 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +00001112 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +00001113 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
1114 return getNode(ISD::UNDEF, N1.getValueType());
1115 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001116 if (C2 == 0) return N1;
Chris Lattner3e27b1f2005-08-12 23:54:58 +00001117
1118 if (Opcode == ISD::SRA) {
1119 // If the sign bit is known to be zero, switch this to a SRL.
1120 if (MaskedValueIsZero(N1,
1121 1ULL << MVT::getSizeInBits(N1.getValueType())-1,
1122 TLI))
1123 return getNode(ISD::SRL, N1.getValueType(), N1, N2);
1124 } else {
1125 // If the part left over is known to be zero, the whole thing is zero.
1126 uint64_t TypeMask = ~0ULL >> (64-MVT::getSizeInBits(N1.getValueType()));
1127 if (Opcode == ISD::SRL) {
1128 if (MaskedValueIsZero(N1, TypeMask << C2, TLI))
1129 return getConstant(0, N1.getValueType());
1130 } else if (Opcode == ISD::SHL) {
1131 if (MaskedValueIsZero(N1, TypeMask >> C2, TLI))
1132 return getConstant(0, N1.getValueType());
1133 }
1134 }
Chris Lattner57aa5962005-05-09 17:06:45 +00001135
1136 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1137 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1138 unsigned OpSAC = OpSA->getValue();
1139 if (N1.getOpcode() == ISD::SHL) {
1140 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1141 return getConstant(0, N1.getValueType());
1142 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1143 getConstant(C2+OpSAC, N2.getValueType()));
1144 } else if (N1.getOpcode() == ISD::SRL) {
1145 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1146 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1147 getConstant(~0ULL << OpSAC, VT));
1148 if (C2 > OpSAC) {
1149 return getNode(ISD::SHL, VT, Mask,
1150 getConstant(C2-OpSAC, N2.getValueType()));
1151 } else {
1152 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1153 return getNode(ISD::SRL, VT, Mask,
1154 getConstant(OpSAC-C2, N2.getValueType()));
1155 }
1156 } else if (N1.getOpcode() == ISD::SRA) {
1157 // if C1 == C2, just mask out low bits.
1158 if (C2 == OpSAC)
1159 return getNode(ISD::AND, VT, N1.getOperand(0),
1160 getConstant(~0ULL << C2, VT));
1161 }
1162 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001163 break;
1164
Chris Lattnerc3aae252005-01-07 07:46:32 +00001165 case ISD::AND:
1166 if (!C2) return N2; // X and 0 -> 0
1167 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001168 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001169
Chris Lattner36019aa2005-04-18 03:48:41 +00001170 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1171 return getConstant(0, VT);
1172
Chris Lattner588bbbf2005-04-21 06:28:15 +00001173 {
1174 uint64_t NotC2 = ~C2;
1175 if (VT != MVT::i64)
1176 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1177
1178 if (MaskedValueIsZero(N1, NotC2, TLI))
1179 return N1; // if (X & ~C2) -> 0, the and is redundant
1180 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001181
Chris Lattnerdea29e22005-04-10 01:13:15 +00001182 // FIXME: Should add a corresponding version of this for
1183 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1184 // we don't have yet.
1185
Chris Lattner0f2287b2005-04-13 02:38:18 +00001186 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1187 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001188 // If we are masking out the part of our input that was extended, just
1189 // mask the input to the extension directly.
1190 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001191 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001192 if ((C2 & (~0ULL << ExtendBits)) == 0)
1193 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001194 } else if (N1.getOpcode() == ISD::OR) {
1195 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1196 if ((ORI->getValue() & C2) == C2) {
1197 // If the 'or' is setting all of the bits that we are masking for,
1198 // we know the result of the AND will be the AND mask itself.
1199 return N2;
1200 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001201 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001202 break;
1203 case ISD::OR:
1204 if (!C2)return N1; // X or 0 -> X
1205 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001206 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001207 break;
1208 case ISD::XOR:
1209 if (!C2) return N1; // X xor 0 -> X
1210 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001211 if (N1.Val->getOpcode() == ISD::SETCC){
1212 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001213 // !(X op Y) -> (X !op Y)
1214 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001215 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1216 return getSetCC(SetCC->getValueType(0),
1217 SetCC->getOperand(0), SetCC->getOperand(1),
1218 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001219 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1220 SDNode *Op = N1.Val;
1221 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1222 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1223 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1224 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1225 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1226 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1227 if (Op->getOpcode() == ISD::AND)
1228 return getNode(ISD::OR, VT, LHS, RHS);
1229 return getNode(ISD::AND, VT, LHS, RHS);
1230 }
1231 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001232 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001233 }
1234 break;
1235 }
1236
1237 // Reassociate ((X op C1) op C2) if possible.
1238 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1239 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001240 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001241 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1242 }
1243
1244 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1245 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001246 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001247 if (N2CFP) {
1248 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1249 switch (Opcode) {
1250 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1251 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1252 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1253 case ISD::SDIV:
1254 if (C2) return getConstantFP(C1 / C2, VT);
1255 break;
1256 case ISD::SREM :
1257 if (C2) return getConstantFP(fmod(C1, C2), VT);
1258 break;
1259 default: break;
1260 }
1261
1262 } else { // Cannonicalize constant to RHS if commutative
1263 if (isCommutativeBinOp(Opcode)) {
1264 std::swap(N1CFP, N2CFP);
1265 std::swap(N1, N2);
1266 }
1267 }
1268
Chris Lattner15e4b012005-07-10 00:07:11 +00001269 if (Opcode == ISD::FP_ROUND_INREG)
1270 return getNode(ISD::FP_EXTEND, VT,
1271 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1272 }
1273
Chris Lattnerc3aae252005-01-07 07:46:32 +00001274 // Finally, fold operations that do not require constants.
1275 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001276 case ISD::TokenFactor:
1277 if (N1.getOpcode() == ISD::EntryToken)
1278 return N2;
1279 if (N2.getOpcode() == ISD::EntryToken)
1280 return N1;
1281 break;
1282
Chris Lattnerc3aae252005-01-07 07:46:32 +00001283 case ISD::AND:
1284 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001285 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1286 SDNode *LHS = N1.Val, *RHS = N2.Val;
1287 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1288 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1289 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1290 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001291
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001292 if (LR == RR && isa<ConstantSDNode>(LR) &&
1293 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1294 // (X != 0) | (Y != 0) -> (X|Y != 0)
1295 // (X == 0) & (Y == 0) -> (X|Y == 0)
1296 // (X < 0) | (Y < 0) -> (X|Y < 0)
1297 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1298 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1299 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1300 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1301 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1302 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001303
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001304 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1305 // (X == -1) & (Y == -1) -> (X&Y == -1)
1306 // (X != -1) | (Y != -1) -> (X&Y != -1)
1307 // (X > -1) | (Y > -1) -> (X&Y > -1)
1308 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1309 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1310 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1311 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1312 LR, Op2);
1313 // (X > -1) & (Y > -1) -> (X|Y > -1)
1314 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1315 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1316 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001317 }
1318 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001319
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001320 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1321 if (LL == RR && LR == RL) {
1322 Op2 = ISD::getSetCCSwappedOperands(Op2);
1323 goto MatchedBackwards;
1324 }
1325
1326 if (LL == RL && LR == RR) {
1327 MatchedBackwards:
1328 ISD::CondCode Result;
1329 bool isInteger = MVT::isInteger(LL.getValueType());
1330 if (Opcode == ISD::OR)
1331 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1332 else
1333 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1334
1335 if (Result != ISD::SETCC_INVALID)
1336 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1337 }
1338 }
1339
Chris Lattner7467c9b2005-04-18 04:11:19 +00001340 // and/or zext(a), zext(b) -> zext(and/or a, b)
1341 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1342 N2.getOpcode() == ISD::ZERO_EXTEND &&
1343 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1344 return getNode(ISD::ZERO_EXTEND, VT,
1345 getNode(Opcode, N1.getOperand(0).getValueType(),
1346 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001347 break;
1348 case ISD::XOR:
1349 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1350 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001351 case ISD::ADD:
1352 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1353 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1354 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1355 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001356 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1357 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1358 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1359 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1360 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1361 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001362 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1363 !MVT::isFloatingPoint(N2.getValueType()))
1364 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001365 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001366 case ISD::SUB:
1367 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001368 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001369 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001370 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001371 if (N1.Val->getOperand(1) == N2 &&
1372 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001373 return N1.Val->getOperand(0); // (A+B)-B == A
1374 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001375 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1376 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001377 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001378 case ISD::FP_ROUND_INREG:
1379 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1380 break;
1381 case ISD::SIGN_EXTEND_INREG: {
1382 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1383 if (EVT == VT) return N1; // Not actually extending
1384
1385 // If we are sign extending an extension, use the original source.
1386 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1387 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1388 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001389
Chris Lattner15e4b012005-07-10 00:07:11 +00001390 // If we are sign extending a sextload, return just the load.
1391 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001392 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001393 return N1;
1394
1395 // If we are extending the result of a setcc, and we already know the
1396 // contents of the top bits, eliminate the extension.
1397 if (N1.getOpcode() == ISD::SETCC &&
1398 TLI.getSetCCResultContents() ==
1399 TargetLowering::ZeroOrNegativeOneSetCCResult)
1400 return N1;
1401
1402 // If we are sign extending the result of an (and X, C) operation, and we
1403 // know the extended bits are zeros already, don't do the extend.
1404 if (N1.getOpcode() == ISD::AND)
1405 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1406 uint64_t Mask = N1C->getValue();
1407 unsigned NumBits = MVT::getSizeInBits(EVT);
1408 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1409 return N1;
1410 }
1411 break;
1412 }
1413
Nate Begemaneea805e2005-04-13 21:23:31 +00001414 // FIXME: figure out how to safely handle things like
1415 // int foo(int x) { return 1 << (x & 255); }
1416 // int bar() { return foo(256); }
1417#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001418 case ISD::SHL:
1419 case ISD::SRL:
1420 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001421 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001422 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001423 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001424 else if (N2.getOpcode() == ISD::AND)
1425 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1426 // If the and is only masking out bits that cannot effect the shift,
1427 // eliminate the and.
1428 unsigned NumBits = MVT::getSizeInBits(VT);
1429 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1430 return getNode(Opcode, VT, N1, N2.getOperand(0));
1431 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001432 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001433#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001434 }
1435
Chris Lattner27e9b412005-05-11 18:57:39 +00001436 // Memoize this node if possible.
1437 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001438 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001439 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1440 if (BON) return SDOperand(BON, 0);
1441
1442 BON = N = new SDNode(Opcode, N1, N2);
1443 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001444 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001445 }
1446
Chris Lattner3e011362005-05-14 07:45:46 +00001447 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001448 AllNodes.push_back(N);
1449 return SDOperand(N, 0);
1450}
1451
Chris Lattner88de6e72005-05-12 00:17:04 +00001452// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001453// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001454void SDNode::setAdjCallChain(SDOperand N) {
1455 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001456 assert((getOpcode() == ISD::CALLSEQ_START ||
1457 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001458
1459 Operands[0].Val->removeUser(this);
1460 Operands[0] = N;
1461 N.Val->Uses.push_back(this);
1462}
1463
1464
1465
Chris Lattnerc3aae252005-01-07 07:46:32 +00001466SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001467 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001468 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001469 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1470 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001471 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001472
1473 // Loads have a token chain.
1474 N->setValueTypes(VT, MVT::Other);
1475 AllNodes.push_back(N);
1476 return SDOperand(N, 0);
1477}
1478
Chris Lattner5f056bf2005-07-10 01:55:33 +00001479
1480SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1481 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1482 MVT::ValueType EVT) {
1483 std::vector<SDOperand> Ops;
1484 Ops.reserve(4);
1485 Ops.push_back(Chain);
1486 Ops.push_back(Ptr);
1487 Ops.push_back(SV);
1488 Ops.push_back(getValueType(EVT));
1489 std::vector<MVT::ValueType> VTs;
1490 VTs.reserve(2);
1491 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1492 return getNode(Opcode, VTs, Ops);
1493}
1494
Chris Lattnerc3aae252005-01-07 07:46:32 +00001495SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1496 SDOperand N1, SDOperand N2, SDOperand N3) {
1497 // Perform various simplifications.
1498 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1499 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1500 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1501 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001502 case ISD::SETCC: {
1503 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001504 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001505 if (Simp.Val) return Simp;
1506 break;
1507 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001508 case ISD::SELECT:
1509 if (N1C)
1510 if (N1C->getValue())
1511 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001512 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001513 return N3; // select false, X, Y -> Y
1514
1515 if (N2 == N3) return N2; // select C, X, X -> X
1516
1517 if (VT == MVT::i1) { // Boolean SELECT
1518 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001519 if (N2C->getValue()) // select C, 1, X -> C | X
1520 return getNode(ISD::OR, VT, N1, N3);
1521 else // select C, 0, X -> ~C & X
1522 return getNode(ISD::AND, VT,
1523 getNode(ISD::XOR, N1.getValueType(), N1,
1524 getConstant(1, N1.getValueType())), N3);
1525 } else if (N3C) {
1526 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1527 return getNode(ISD::OR, VT,
1528 getNode(ISD::XOR, N1.getValueType(), N1,
1529 getConstant(1, N1.getValueType())), N2);
1530 else // select C, X, 0 -> C & X
1531 return getNode(ISD::AND, VT, N1, N2);
1532 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001533
1534 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1535 return getNode(ISD::OR, VT, N1, N3);
1536 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1537 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001538 }
Nate Begeman32c392a2005-08-13 06:00:21 +00001539 if (N1.getOpcode() == ISD::SETCC) {
Nate Begemanff663682005-08-13 06:14:17 +00001540 SDOperand Simp = SimplifySelectCC(N1.getOperand(0), N1.getOperand(1), N2,
1541 N3, cast<CondCodeSDNode>(N1.getOperand(2))->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001542 if (Simp.Val) return Simp;
1543 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001544 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001545 case ISD::BRCOND:
1546 if (N2C)
1547 if (N2C->getValue()) // Unconditional branch
1548 return getNode(ISD::BR, MVT::Other, N1, N3);
1549 else
1550 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001551 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001552 }
1553
Chris Lattner385328c2005-05-14 07:42:29 +00001554 std::vector<SDOperand> Ops;
1555 Ops.reserve(3);
1556 Ops.push_back(N1);
1557 Ops.push_back(N2);
1558 Ops.push_back(N3);
1559
1560 // Memoize nodes.
1561 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1562 if (N) return SDOperand(N, 0);
1563
1564 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001565 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001566 AllNodes.push_back(N);
1567 return SDOperand(N, 0);
1568}
1569
1570SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001571 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001572 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001573 std::vector<SDOperand> Ops;
1574 Ops.reserve(4);
1575 Ops.push_back(N1);
1576 Ops.push_back(N2);
1577 Ops.push_back(N3);
1578 Ops.push_back(N4);
1579 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001580}
1581
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001582SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1583 SDOperand N1, SDOperand N2, SDOperand N3,
1584 SDOperand N4, SDOperand N5) {
Nate Begemane5d63822005-08-11 01:12:20 +00001585 if (ISD::SELECT_CC == Opcode) {
1586 assert(N1.getValueType() == N2.getValueType() &&
1587 "LHS and RHS of condition must have same type!");
1588 assert(N3.getValueType() == N4.getValueType() &&
1589 "True and False arms of SelectCC must have same type!");
Nate Begemanff663682005-08-13 06:14:17 +00001590 assert(N3.getValueType() == VT &&
1591 "select_cc node must be of same type as true and false value!");
1592 SDOperand Simp = SimplifySelectCC(N1, N2, N3, N4,
1593 cast<CondCodeSDNode>(N5)->get());
Nate Begeman32c392a2005-08-13 06:00:21 +00001594 if (Simp.Val) return Simp;
Nate Begemane5d63822005-08-11 01:12:20 +00001595 }
1596
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001597 std::vector<SDOperand> Ops;
1598 Ops.reserve(5);
1599 Ops.push_back(N1);
1600 Ops.push_back(N2);
1601 Ops.push_back(N3);
1602 Ops.push_back(N4);
1603 Ops.push_back(N5);
1604 return getNode(Opcode, VT, Ops);
1605}
1606
1607
Chris Lattner0437cdd2005-05-09 04:14:13 +00001608SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001609 assert((!V || isa<PointerType>(V->getType())) &&
1610 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001611 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1612 if (N) return SDOperand(N, 0);
1613
1614 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001615 AllNodes.push_back(N);
1616 return SDOperand(N, 0);
1617}
1618
1619SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001620 std::vector<SDOperand> &Ops) {
1621 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001622 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001623 case 1: return getNode(Opcode, VT, Ops[0]);
1624 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1625 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001626 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001627 }
Chris Lattneref847df2005-04-09 03:27:28 +00001628
Chris Lattner89c34632005-05-14 06:20:26 +00001629 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001630 switch (Opcode) {
1631 default: break;
1632 case ISD::BRCONDTWOWAY:
1633 if (N1C)
1634 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001635 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001636 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001637 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001638 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001639 case ISD::BRTWOWAY_CC:
1640 assert(Ops.size() == 6 && "BRTWOWAY_CC takes 6 operands!");
1641 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1642 "LHS and RHS of comparison must have same type!");
1643 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001644 case ISD::TRUNCSTORE: {
1645 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1646 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1647#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1648 // If this is a truncating store of a constant, convert to the desired type
1649 // and store it instead.
1650 if (isa<Constant>(Ops[0])) {
1651 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1652 if (isa<Constant>(Op))
1653 N1 = Op;
1654 }
1655 // Also for ConstantFP?
1656#endif
1657 if (Ops[0].getValueType() == EVT) // Normal store?
1658 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1659 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1660 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1661 "Can't do FP-INT conversion!");
1662 break;
1663 }
Chris Lattneref847df2005-04-09 03:27:28 +00001664 }
1665
Chris Lattner385328c2005-05-14 07:42:29 +00001666 // Memoize nodes.
1667 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1668 if (N) return SDOperand(N, 0);
1669 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001670 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001671 AllNodes.push_back(N);
1672 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001673}
1674
Chris Lattner89c34632005-05-14 06:20:26 +00001675SDOperand SelectionDAG::getNode(unsigned Opcode,
1676 std::vector<MVT::ValueType> &ResultTys,
1677 std::vector<SDOperand> &Ops) {
1678 if (ResultTys.size() == 1)
1679 return getNode(Opcode, ResultTys[0], Ops);
1680
Chris Lattner5f056bf2005-07-10 01:55:33 +00001681 switch (Opcode) {
1682 case ISD::EXTLOAD:
1683 case ISD::SEXTLOAD:
1684 case ISD::ZEXTLOAD: {
1685 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1686 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1687 // If they are asking for an extending load from/to the same thing, return a
1688 // normal load.
1689 if (ResultTys[0] == EVT)
1690 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1691 assert(EVT < ResultTys[0] &&
1692 "Should only be an extending load, not truncating!");
1693 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1694 "Cannot sign/zero extend a FP load!");
1695 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1696 "Cannot convert from FP to Int or Int -> FP!");
1697 break;
1698 }
1699
Chris Lattnere89083a2005-05-14 07:25:05 +00001700 // FIXME: figure out how to safely handle things like
1701 // int foo(int x) { return 1 << (x & 255); }
1702 // int bar() { return foo(256); }
1703#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001704 case ISD::SRA_PARTS:
1705 case ISD::SRL_PARTS:
1706 case ISD::SHL_PARTS:
1707 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001708 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001709 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1710 else if (N3.getOpcode() == ISD::AND)
1711 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1712 // If the and is only masking out bits that cannot effect the shift,
1713 // eliminate the and.
1714 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1715 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1716 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1717 }
1718 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001719#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001720 }
Chris Lattner89c34632005-05-14 06:20:26 +00001721
1722 // Memoize the node.
1723 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1724 Ops))];
1725 if (N) return SDOperand(N, 0);
1726 N = new SDNode(Opcode, Ops);
1727 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001728 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001729 return SDOperand(N, 0);
1730}
1731
Chris Lattner149c58c2005-08-16 18:17:10 +00001732
1733/// SelectNodeTo - These are used for target selectors to *mutate* the
1734/// specified node to have the specified return type, Target opcode, and
1735/// operands. Note that target opcodes are stored as
1736/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
1737void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1738 unsigned TargetOpc, SDOperand Op1) {
1739 RemoveNodeFromCSEMaps(N);
1740 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1741 N->setValueTypes(VT);
1742 N->setOperands(Op1);
1743}
1744void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1745 unsigned TargetOpc, SDOperand Op1,
1746 SDOperand Op2) {
1747 RemoveNodeFromCSEMaps(N);
1748 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1749 N->setValueTypes(VT);
1750 N->setOperands(Op1, Op2);
1751}
1752void SelectionDAG::SelectNodeTo(SDNode *N, MVT::ValueType VT,
1753 unsigned TargetOpc, SDOperand Op1,
1754 SDOperand Op2, SDOperand Op3) {
1755 RemoveNodeFromCSEMaps(N);
1756 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1757 N->setValueTypes(VT);
1758 N->setOperands(Op1, Op2, Op3);
1759}
1760
1761
Chris Lattner5c884562005-01-12 18:37:47 +00001762/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1763/// indicated value. This method ignores uses of other values defined by this
1764/// operation.
1765bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1766 assert(Value < getNumValues() && "Bad value!");
1767
1768 // If there is only one value, this is easy.
1769 if (getNumValues() == 1)
1770 return use_size() == NUses;
1771 if (Uses.size() < NUses) return false;
1772
1773 SDOperand TheValue(this, Value);
1774
1775 std::set<SDNode*> UsersHandled;
1776
1777 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1778 UI != E; ++UI) {
1779 SDNode *User = *UI;
1780 if (User->getNumOperands() == 1 ||
1781 UsersHandled.insert(User).second) // First time we've seen this?
1782 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1783 if (User->getOperand(i) == TheValue) {
1784 if (NUses == 0)
1785 return false; // too many uses
1786 --NUses;
1787 }
1788 }
1789
1790 // Found exactly the right number of uses?
1791 return NUses == 0;
1792}
1793
1794
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001795const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001796 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001797 default:
1798 if (getOpcode() < ISD::BUILTIN_OP_END)
1799 return "<<Unknown DAG Node>>";
1800 else {
1801 if (G)
1802 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
1803 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
1804 return "<<Unknown Target Node>>";
1805 }
1806
Andrew Lenharth95762122005-03-31 21:24:06 +00001807 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001808 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001809 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001810 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001811 case ISD::Constant: return "Constant";
Chris Lattner37bfbb42005-08-17 00:34:06 +00001812 case ISD::TargetConstant: return "TargetConstant";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001813 case ISD::ConstantFP: return "ConstantFP";
1814 case ISD::GlobalAddress: return "GlobalAddress";
1815 case ISD::FrameIndex: return "FrameIndex";
1816 case ISD::BasicBlock: return "BasicBlock";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001817 case ISD::Register: return "Register";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001818 case ISD::ExternalSymbol: return "ExternalSymbol";
1819 case ISD::ConstantPool: return "ConstantPoolIndex";
1820 case ISD::CopyToReg: return "CopyToReg";
1821 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001822 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001823 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001824
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001825 // Unary operators
1826 case ISD::FABS: return "fabs";
1827 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001828 case ISD::FSQRT: return "fsqrt";
1829 case ISD::FSIN: return "fsin";
1830 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001831
1832 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001833 case ISD::ADD: return "add";
1834 case ISD::SUB: return "sub";
1835 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001836 case ISD::MULHU: return "mulhu";
1837 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001838 case ISD::SDIV: return "sdiv";
1839 case ISD::UDIV: return "udiv";
1840 case ISD::SREM: return "srem";
1841 case ISD::UREM: return "urem";
1842 case ISD::AND: return "and";
1843 case ISD::OR: return "or";
1844 case ISD::XOR: return "xor";
1845 case ISD::SHL: return "shl";
1846 case ISD::SRA: return "sra";
1847 case ISD::SRL: return "srl";
1848
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001849 case ISD::SETCC: return "setcc";
1850 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00001851 case ISD::SELECT_CC: return "select_cc";
Chris Lattner17eee182005-01-20 18:50:55 +00001852 case ISD::ADD_PARTS: return "add_parts";
1853 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001854 case ISD::SHL_PARTS: return "shl_parts";
1855 case ISD::SRA_PARTS: return "sra_parts";
1856 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001857
Chris Lattner7f644642005-04-28 21:44:03 +00001858 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001859 case ISD::SIGN_EXTEND: return "sign_extend";
1860 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001861 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001862 case ISD::TRUNCATE: return "truncate";
1863 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001864 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001865 case ISD::FP_EXTEND: return "fp_extend";
1866
1867 case ISD::SINT_TO_FP: return "sint_to_fp";
1868 case ISD::UINT_TO_FP: return "uint_to_fp";
1869 case ISD::FP_TO_SINT: return "fp_to_sint";
1870 case ISD::FP_TO_UINT: return "fp_to_uint";
1871
1872 // Control flow instructions
1873 case ISD::BR: return "br";
1874 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001875 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Nate Begeman7cbd5252005-08-16 19:49:35 +00001876 case ISD::BR_CC: return "br_cc";
1877 case ISD::BRTWOWAY_CC: return "brtwoway_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001878 case ISD::RET: return "ret";
1879 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001880 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001881 case ISD::CALLSEQ_START: return "callseq_start";
1882 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001883
1884 // Other operators
1885 case ISD::LOAD: return "load";
1886 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001887 case ISD::EXTLOAD: return "extload";
1888 case ISD::SEXTLOAD: return "sextload";
1889 case ISD::ZEXTLOAD: return "zextload";
1890 case ISD::TRUNCSTORE: return "truncstore";
1891
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001892 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1893 case ISD::EXTRACT_ELEMENT: return "extract_element";
1894 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001895 case ISD::MEMSET: return "memset";
1896 case ISD::MEMCPY: return "memcpy";
1897 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001898
Chris Lattner276260b2005-05-11 04:50:30 +00001899 // Bit counting
1900 case ISD::CTPOP: return "ctpop";
1901 case ISD::CTTZ: return "cttz";
1902 case ISD::CTLZ: return "ctlz";
1903
1904 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001905 case ISD::READPORT: return "readport";
1906 case ISD::WRITEPORT: return "writeport";
1907 case ISD::READIO: return "readio";
1908 case ISD::WRITEIO: return "writeio";
1909
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001910 case ISD::CONDCODE:
1911 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001912 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001913 case ISD::SETOEQ: return "setoeq";
1914 case ISD::SETOGT: return "setogt";
1915 case ISD::SETOGE: return "setoge";
1916 case ISD::SETOLT: return "setolt";
1917 case ISD::SETOLE: return "setole";
1918 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001919
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001920 case ISD::SETO: return "seto";
1921 case ISD::SETUO: return "setuo";
1922 case ISD::SETUEQ: return "setue";
1923 case ISD::SETUGT: return "setugt";
1924 case ISD::SETUGE: return "setuge";
1925 case ISD::SETULT: return "setult";
1926 case ISD::SETULE: return "setule";
1927 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001928
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001929 case ISD::SETEQ: return "seteq";
1930 case ISD::SETGT: return "setgt";
1931 case ISD::SETGE: return "setge";
1932 case ISD::SETLT: return "setlt";
1933 case ISD::SETLE: return "setle";
1934 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001935 }
1936 }
1937}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001938
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001939void SDNode::dump() const { dump(0); }
1940void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001941 std::cerr << (void*)this << ": ";
1942
1943 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1944 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001945 if (getValueType(i) == MVT::Other)
1946 std::cerr << "ch";
1947 else
1948 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001949 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001950 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001951
1952 std::cerr << " ";
1953 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1954 if (i) std::cerr << ", ";
1955 std::cerr << (void*)getOperand(i).Val;
1956 if (unsigned RN = getOperand(i).ResNo)
1957 std::cerr << ":" << RN;
1958 }
1959
1960 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1961 std::cerr << "<" << CSDN->getValue() << ">";
1962 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1963 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001964 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001965 dyn_cast<GlobalAddressSDNode>(this)) {
1966 std::cerr << "<";
1967 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001968 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001969 std::cerr << "<" << FIDN->getIndex() << ">";
1970 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1971 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001972 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001973 std::cerr << "<";
1974 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1975 if (LBB)
1976 std::cerr << LBB->getName() << " ";
1977 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001978 } else if (const RegisterSDNode *C2V = dyn_cast<RegisterSDNode>(this)) {
1979 std::cerr << " #" << C2V->getReg();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001980 } else if (const ExternalSymbolSDNode *ES =
1981 dyn_cast<ExternalSymbolSDNode>(this)) {
1982 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001983 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1984 if (M->getValue())
1985 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1986 else
1987 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001988 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001989}
1990
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001991static void DumpNodes(SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001992 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1993 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00001994 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00001995 else
1996 std::cerr << "\n" << std::string(indent+2, ' ')
1997 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001998
Chris Lattnerea946cd2005-01-09 20:38:33 +00001999
2000 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002001 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002002}
2003
Chris Lattnerc3aae252005-01-07 07:46:32 +00002004void SelectionDAG::dump() const {
2005 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00002006 std::vector<SDNode*> Nodes(AllNodes);
2007 std::sort(Nodes.begin(), Nodes.end());
2008
2009 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002010 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002011 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002012 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002013
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002014 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002015
Chris Lattnerc3aae252005-01-07 07:46:32 +00002016 std::cerr << "\n\n";
2017}
2018