blob: 6a018a546c1e0f017e43f96cb14b072df96dcdf2 [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"
Reid Spencer954da372004-07-04 12:19:56 +000021#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000022#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000023#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000024#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner5cdcc582005-01-09 20:52:51 +000027static bool isCommutativeBinOp(unsigned Opcode) {
28 switch (Opcode) {
29 case ISD::ADD:
30 case ISD::MUL:
31 case ISD::AND:
32 case ISD::OR:
33 case ISD::XOR: return true;
34 default: return false; // FIXME: Need commutative info for user ops!
35 }
36}
37
38static bool isAssociativeBinOp(unsigned Opcode) {
39 switch (Opcode) {
40 case ISD::ADD:
41 case ISD::MUL:
42 case ISD::AND:
43 case ISD::OR:
44 case ISD::XOR: return true;
45 default: return false; // FIXME: Need associative info for user ops!
46 }
47}
48
Chris Lattner5cdcc582005-01-09 20:52:51 +000049// isInvertibleForFree - Return true if there is no cost to emitting the logical
50// inverse of this node.
51static bool isInvertibleForFree(SDOperand N) {
52 if (isa<ConstantSDNode>(N.Val)) return true;
53 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
54 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000055 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000056}
57
58
Chris Lattnerc3aae252005-01-07 07:46:32 +000059/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
60/// when given the operation for (X op Y).
61ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
62 // To perform this operation, we just need to swap the L and G bits of the
63 // operation.
64 unsigned OldL = (Operation >> 2) & 1;
65 unsigned OldG = (Operation >> 1) & 1;
66 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
67 (OldL << 1) | // New G bit
68 (OldG << 2)); // New L bit.
69}
70
71/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
72/// 'op' is a valid SetCC operation.
73ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
74 unsigned Operation = Op;
75 if (isInteger)
76 Operation ^= 7; // Flip L, G, E bits, but not U.
77 else
78 Operation ^= 15; // Flip all of the condition bits.
79 if (Operation > ISD::SETTRUE2)
80 Operation &= ~8; // Don't let N and U bits get set.
81 return ISD::CondCode(Operation);
82}
83
84
85/// isSignedOp - For an integer comparison, return 1 if the comparison is a
86/// signed operation and 2 if the result is an unsigned comparison. Return zero
87/// if the operation does not depend on the sign of the input (setne and seteq).
88static int isSignedOp(ISD::CondCode Opcode) {
89 switch (Opcode) {
90 default: assert(0 && "Illegal integer setcc operation!");
91 case ISD::SETEQ:
92 case ISD::SETNE: return 0;
93 case ISD::SETLT:
94 case ISD::SETLE:
95 case ISD::SETGT:
96 case ISD::SETGE: return 1;
97 case ISD::SETULT:
98 case ISD::SETULE:
99 case ISD::SETUGT:
100 case ISD::SETUGE: return 2;
101 }
102}
103
104/// getSetCCOrOperation - Return the result of a logical OR between different
105/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
106/// returns SETCC_INVALID if it is not possible to represent the resultant
107/// comparison.
108ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
109 bool isInteger) {
110 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
111 // Cannot fold a signed integer setcc with an unsigned integer setcc.
112 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000113
Chris Lattnerc3aae252005-01-07 07:46:32 +0000114 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000115
Chris Lattnerc3aae252005-01-07 07:46:32 +0000116 // If the N and U bits get set then the resultant comparison DOES suddenly
117 // care about orderedness, and is true when ordered.
118 if (Op > ISD::SETTRUE2)
119 Op &= ~16; // Clear the N bit.
120 return ISD::CondCode(Op);
121}
122
123/// getSetCCAndOperation - Return the result of a logical AND between different
124/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
125/// function returns zero if it is not possible to represent the resultant
126/// comparison.
127ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
128 bool isInteger) {
129 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
130 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000131 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000132
133 // Combine all of the condition bits.
134 return ISD::CondCode(Op1 & Op2);
135}
136
Chris Lattnerb48da392005-01-23 04:39:44 +0000137const TargetMachine &SelectionDAG::getTarget() const {
138 return TLI.getTargetMachine();
139}
140
141
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000142/// RemoveDeadNodes - This method deletes all unreachable nodes in the
143/// SelectionDAG, including nodes (like loads) that have uses of their token
144/// chain but no other uses and no side effect. If a node is passed in as an
145/// argument, it is used as the seed for node deletion.
146void SelectionDAG::RemoveDeadNodes(SDNode *N) {
147 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
148
149 // Create a dummy node (which is not added to allnodes), that adds a reference
150 // to the root node, preventing it from being deleted.
151 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
152
153 DeleteNodeIfDead(N, &AllNodeSet);
154
155 Restart:
156 unsigned NumNodes = AllNodeSet.size();
157 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
158 I != E; ++I) {
159 // Try to delete this node.
160 DeleteNodeIfDead(*I, &AllNodeSet);
161
162 // If we actually deleted any nodes, do not use invalid iterators in
163 // AllNodeSet.
164 if (AllNodeSet.size() != NumNodes)
165 goto Restart;
166 }
167
168 // Restore AllNodes.
169 if (AllNodes.size() != NumNodes)
170 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
171
172 // If the root changed (e.g. it was a dead load, update the root).
173 setRoot(DummyNode->getOperand(0));
174
175 // Now that we are done with the dummy node, delete it.
176 DummyNode->getOperand(0).Val->removeUser(DummyNode);
177 delete DummyNode;
178}
179
180void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
181 if (!N->use_empty())
182 return;
183
184 // Okay, we really are going to delete this node. First take this out of the
185 // appropriate CSE map.
186 switch (N->getOpcode()) {
187 case ISD::Constant:
188 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
189 N->getValueType(0)));
190 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000191 case ISD::ConstantFP: {
192 union {
193 double DV;
194 uint64_t IV;
195 };
196 DV = cast<ConstantFPSDNode>(N)->getValue();
197 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000198 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000199 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000200 case ISD::GlobalAddress:
201 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
202 break;
203 case ISD::FrameIndex:
204 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
205 break;
206 case ISD::ConstantPool:
207 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
208 break;
209 case ISD::BasicBlock:
210 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
211 break;
212 case ISD::ExternalSymbol:
213 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
214 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000215 case ISD::VALUETYPE:
216 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
217 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000218 case ISD::SRCVALUE: {
219 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
220 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
221 break;
222 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000223 case ISD::LOAD:
224 Loads.erase(std::make_pair(N->getOperand(1),
225 std::make_pair(N->getOperand(0),
226 N->getValueType(0))));
227 break;
228 case ISD::SETCC:
229 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
230 N->getOperand(1)),
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000231 std::make_pair(
232 cast<SetCCSDNode>(N)->getCondition(),
233 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000234 break;
235 default:
236 if (N->getNumOperands() == 1)
237 UnaryOps.erase(std::make_pair(N->getOpcode(),
238 std::make_pair(N->getOperand(0),
239 N->getValueType(0))));
240 else if (N->getNumOperands() == 2)
241 BinaryOps.erase(std::make_pair(N->getOpcode(),
242 std::make_pair(N->getOperand(0),
243 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000244 else if (N->getNumValues() == 1) {
245 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
246 OneResultNodes.erase(std::make_pair(N->getOpcode(),
247 std::make_pair(N->getValueType(0),
248 Ops)));
249 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000250 // Remove the node from the ArbitraryNodes map.
251 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
252 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
253 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
254 std::make_pair(RV, Ops)));
255 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000256 break;
257 }
258
259 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000260 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000261 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000262 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000263 O->removeUser(N);
264
265 // Now that we removed this operand, see if there are no uses of it left.
266 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000267 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000269 // Remove the node from the nodes set and delete it.
270 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
271 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000272
273 // Now that the node is gone, check to see if any of the operands of this node
274 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000275 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000276}
277
278
Chris Lattner78ec3112003-08-11 14:57:33 +0000279SelectionDAG::~SelectionDAG() {
280 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
281 delete AllNodes[i];
282}
283
Chris Lattner0f2287b2005-04-13 02:38:18 +0000284SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000285 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000286 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000287 return getNode(ISD::AND, Op.getValueType(), Op,
288 getConstant(Imm, Op.getValueType()));
289}
290
Chris Lattnerc3aae252005-01-07 07:46:32 +0000291SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
292 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
293 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000294 if (VT != MVT::i64)
295 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000296
Chris Lattnerc3aae252005-01-07 07:46:32 +0000297 SDNode *&N = Constants[std::make_pair(Val, VT)];
298 if (N) return SDOperand(N, 0);
299 N = new ConstantSDNode(Val, VT);
300 AllNodes.push_back(N);
301 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000302}
303
Chris Lattnerc3aae252005-01-07 07:46:32 +0000304SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
305 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
306 if (VT == MVT::f32)
307 Val = (float)Val; // Mask out extra precision.
308
Chris Lattnerd8658612005-02-17 20:17:32 +0000309 // Do the map lookup using the actual bit pattern for the floating point
310 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
311 // we don't have issues with SNANs.
312 union {
313 double DV;
314 uint64_t IV;
315 };
Misha Brukmanedf128a2005-04-21 22:36:52 +0000316
Chris Lattnerd8658612005-02-17 20:17:32 +0000317 DV = Val;
318
319 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000320 if (N) return SDOperand(N, 0);
321 N = new ConstantFPSDNode(Val, VT);
322 AllNodes.push_back(N);
323 return SDOperand(N, 0);
324}
325
326
327
328SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
329 MVT::ValueType VT) {
330 SDNode *&N = GlobalValues[GV];
331 if (N) return SDOperand(N, 0);
332 N = new GlobalAddressSDNode(GV,VT);
333 AllNodes.push_back(N);
334 return SDOperand(N, 0);
335}
336
337SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
338 SDNode *&N = FrameIndices[FI];
339 if (N) return SDOperand(N, 0);
340 N = new FrameIndexSDNode(FI, VT);
341 AllNodes.push_back(N);
342 return SDOperand(N, 0);
343}
344
345SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
346 SDNode *N = ConstantPoolIndices[CPIdx];
347 if (N) return SDOperand(N, 0);
348 N = new ConstantPoolSDNode(CPIdx, VT);
349 AllNodes.push_back(N);
350 return SDOperand(N, 0);
351}
352
353SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
354 SDNode *&N = BBNodes[MBB];
355 if (N) return SDOperand(N, 0);
356 N = new BasicBlockSDNode(MBB);
357 AllNodes.push_back(N);
358 return SDOperand(N, 0);
359}
360
Chris Lattner15e4b012005-07-10 00:07:11 +0000361SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
362 if ((unsigned)VT >= ValueTypeNodes.size())
363 ValueTypeNodes.resize(VT+1);
364 if (ValueTypeNodes[VT] == 0) {
365 ValueTypeNodes[VT] = new VTSDNode(VT);
366 AllNodes.push_back(ValueTypeNodes[VT]);
367 }
368
369 return SDOperand(ValueTypeNodes[VT], 0);
370}
371
Chris Lattnerc3aae252005-01-07 07:46:32 +0000372SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
373 SDNode *&N = ExternalSymbols[Sym];
374 if (N) return SDOperand(N, 0);
375 N = new ExternalSymbolSDNode(Sym, VT);
376 AllNodes.push_back(N);
377 return SDOperand(N, 0);
378}
379
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000380SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
381 SDOperand N1, SDOperand N2) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000382 // These setcc operations always fold.
383 switch (Cond) {
384 default: break;
385 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000386 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000387 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000388 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000389 }
390
Chris Lattner67255a12005-04-07 18:14:58 +0000391 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
392 uint64_t C2 = N2C->getValue();
393 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
394 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000395
Chris Lattnerc3aae252005-01-07 07:46:32 +0000396 // Sign extend the operands if required
397 if (ISD::isSignedIntSetCC(Cond)) {
398 C1 = N1C->getSignExtended();
399 C2 = N2C->getSignExtended();
400 }
401
402 switch (Cond) {
403 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000404 case ISD::SETEQ: return getConstant(C1 == C2, VT);
405 case ISD::SETNE: return getConstant(C1 != C2, VT);
406 case ISD::SETULT: return getConstant(C1 < C2, VT);
407 case ISD::SETUGT: return getConstant(C1 > C2, VT);
408 case ISD::SETULE: return getConstant(C1 <= C2, VT);
409 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
410 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
411 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
412 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
413 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000414 }
Chris Lattner24673922005-04-07 18:58:54 +0000415 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000416 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
417 // the comparison on the input.
418 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
419 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
420
421 // If the comparison constant has bits in the upper part, the
422 // zero-extended value could never match.
423 if (C2 & (~0ULL << InSize)) {
424 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
425 switch (Cond) {
426 case ISD::SETUGT:
427 case ISD::SETUGE:
428 case ISD::SETEQ: return getConstant(0, VT);
429 case ISD::SETULT:
430 case ISD::SETULE:
431 case ISD::SETNE: return getConstant(1, VT);
432 case ISD::SETGT:
433 case ISD::SETGE:
434 // True if the sign bit of C2 is set.
435 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
436 case ISD::SETLT:
437 case ISD::SETLE:
438 // True if the sign bit of C2 isn't set.
439 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
440 default:
441 break;
442 }
443 }
444
445 // Otherwise, we can perform the comparison with the low bits.
446 switch (Cond) {
447 case ISD::SETEQ:
448 case ISD::SETNE:
449 case ISD::SETUGT:
450 case ISD::SETUGE:
451 case ISD::SETULT:
452 case ISD::SETULE:
453 return getSetCC(Cond, VT, N1.getOperand(0),
454 getConstant(C2, N1.getOperand(0).getValueType()));
455 default:
456 break; // todo, be more careful with signed comparisons
457 }
458 }
459
460
Chris Lattner67255a12005-04-07 18:14:58 +0000461 uint64_t MinVal, MaxVal;
462 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
463 if (ISD::isSignedIntSetCC(Cond)) {
464 MinVal = 1ULL << (OperandBitSize-1);
465 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
466 MaxVal = ~0ULL >> (65-OperandBitSize);
467 else
468 MaxVal = 0;
469 } else {
470 MinVal = 0;
471 MaxVal = ~0ULL >> (64-OperandBitSize);
472 }
473
474 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
475 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
476 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
477 --C2; // X >= C1 --> X > (C1-1)
478 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
479 N2 = getConstant(C2, N2.getValueType());
480 N2C = cast<ConstantSDNode>(N2.Val);
481 }
482
483 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
484 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
485 ++C2; // X <= C1 --> X < (C1+1)
486 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
487 N2 = getConstant(C2, N2.getValueType());
488 N2C = cast<ConstantSDNode>(N2.Val);
489 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000490
Nate Begeman72ea2812005-04-14 08:56:52 +0000491 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
492 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000493
Nate Begeman72ea2812005-04-14 08:56:52 +0000494 // Canonicalize setgt X, Min --> setne X, Min
495 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
496 return getSetCC(ISD::SETNE, VT, N1, N2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000497
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000498 // If we have setult X, 1, turn it into seteq X, 0
499 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
500 return getSetCC(ISD::SETEQ, VT, N1,
501 getConstant(MinVal, N1.getValueType()));
Nate Begeman72ea2812005-04-14 08:56:52 +0000502 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000503 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
504 return getSetCC(ISD::SETEQ, VT, N1,
505 getConstant(MaxVal, N1.getValueType()));
Chris Lattner67255a12005-04-07 18:14:58 +0000506
507 // If we have "setcc X, C1", check to see if we can shrink the immediate
508 // by changing cc.
509
510 // SETUGT X, SINTMAX -> SETLT X, 0
511 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
512 C2 == (~0ULL >> (65-OperandBitSize)))
513 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
514
515 // FIXME: Implement the rest of these.
516
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000517
518 // Fold bit comparisons when we can.
519 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
520 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
521 if (ConstantSDNode *AndRHS =
522 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
523 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
524 // Perform the xform if the AND RHS is a single bit.
525 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
526 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000527 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000528 TLI.getShiftAmountTy()));
529 }
530 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
531 // (X & 8) == 8 --> (X & 8) >> 3
532 // Perform the xform if C2 is a single bit.
533 if ((C2 & (C2-1)) == 0) {
534 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000535 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000536 }
537 }
538 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000539 }
Chris Lattner67255a12005-04-07 18:14:58 +0000540 } else if (isa<ConstantSDNode>(N1.Val)) {
541 // Ensure that the constant occurs on the RHS.
542 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
543 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000544
545 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
546 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
547 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000548
Chris Lattnerc3aae252005-01-07 07:46:32 +0000549 switch (Cond) {
550 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000551 case ISD::SETEQ: return getConstant(C1 == C2, VT);
552 case ISD::SETNE: return getConstant(C1 != C2, VT);
553 case ISD::SETLT: return getConstant(C1 < C2, VT);
554 case ISD::SETGT: return getConstant(C1 > C2, VT);
555 case ISD::SETLE: return getConstant(C1 <= C2, VT);
556 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000557 }
558 } else {
559 // Ensure that the constant occurs on the RHS.
560 Cond = ISD::getSetCCSwappedOperands(Cond);
561 std::swap(N1, N2);
562 }
563
564 if (N1 == N2) {
565 // We can always fold X == Y for integer setcc's.
566 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000567 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000568 unsigned UOF = ISD::getUnorderedFlavor(Cond);
569 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000570 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000571 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000572 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000573 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
574 // if it is not already.
575 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
576 }
577
Chris Lattner5cdcc582005-01-09 20:52:51 +0000578 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000579 MVT::isInteger(N1.getValueType())) {
580 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
581 N1.getOpcode() == ISD::XOR) {
582 // Simplify (X+Y) == (X+Z) --> Y == Z
583 if (N1.getOpcode() == N2.getOpcode()) {
584 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000585 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000586 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000587 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000588 if (isCommutativeBinOp(N1.getOpcode())) {
589 // If X op Y == Y op X, try other combinations.
590 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000591 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000592 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000593 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000594 }
595 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000596
597 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000598
Chris Lattner68dc3102005-01-10 02:03:02 +0000599 // Simplify (X+Z) == X --> Z == 0
600 if (N1.getOperand(0) == N2)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000601 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000602 getConstant(0, N1.getValueType()));
603 if (N1.getOperand(1) == N2) {
604 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000605 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000606 getConstant(0, N1.getValueType()));
607 else {
608 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
609 // (Z-X) == X --> Z == X<<1
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000610 return getSetCC(Cond, VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000611 getNode(ISD::SHL, N2.getValueType(),
Chris Lattnerb48da392005-01-23 04:39:44 +0000612 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner68dc3102005-01-10 02:03:02 +0000613 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000614 }
615 }
616
Chris Lattner68dc3102005-01-10 02:03:02 +0000617 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
618 N2.getOpcode() == ISD::XOR) {
619 // Simplify X == (X+Z) --> Z == 0
620 if (N2.getOperand(0) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000621 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000622 getConstant(0, N2.getValueType()));
623 else if (N2.getOperand(1) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000624 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000625 getConstant(0, N2.getValueType()));
626 }
627 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628
Chris Lattnerfda2b552005-04-18 04:48:12 +0000629 // Fold away ALL boolean setcc's.
630 if (N1.getValueType() == MVT::i1) {
631 switch (Cond) {
632 default: assert(0 && "Unknown integer setcc!");
633 case ISD::SETEQ: // X == Y -> (X^Y)^1
634 N1 = getNode(ISD::XOR, MVT::i1,
635 getNode(ISD::XOR, MVT::i1, N1, N2),
636 getConstant(1, MVT::i1));
637 break;
638 case ISD::SETNE: // X != Y --> (X^Y)
639 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
640 break;
641 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
642 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
643 N1 = getNode(ISD::AND, MVT::i1, N2,
644 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
645 break;
646 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
647 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
648 N1 = getNode(ISD::AND, MVT::i1, N1,
649 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
650 break;
651 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
652 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
653 N1 = getNode(ISD::OR, MVT::i1, N2,
654 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
655 break;
656 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
657 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
658 N1 = getNode(ISD::OR, MVT::i1, N1,
659 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
660 break;
661 }
662 if (VT != MVT::i1)
663 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
664 return N1;
665 }
666
667
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000668 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
669 std::make_pair(Cond, VT))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000670 if (N) return SDOperand(N, 0);
671 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000672 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000673 AllNodes.push_back(N);
674 return SDOperand(N, 0);
675}
676
677
678
679/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000680///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000681SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
682 SDNode *N = new SDNode(Opcode, VT);
683 AllNodes.push_back(N);
684 return SDOperand(N, 0);
685}
686
Chris Lattnerc3aae252005-01-07 07:46:32 +0000687SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
688 SDOperand Operand) {
689 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
690 uint64_t Val = C->getValue();
691 switch (Opcode) {
692 default: break;
693 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
694 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
695 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000696 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
697 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 }
699 }
700
701 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
702 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000703 case ISD::FNEG:
704 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000705 case ISD::FP_ROUND:
706 case ISD::FP_EXTEND:
707 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000708 case ISD::FP_TO_SINT:
709 return getConstant((int64_t)C->getValue(), VT);
710 case ISD::FP_TO_UINT:
711 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000712 }
713
714 unsigned OpOpcode = Operand.Val->getOpcode();
715 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000716 case ISD::TokenFactor:
717 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000718 case ISD::SIGN_EXTEND:
719 if (Operand.getValueType() == VT) return Operand; // noop extension
720 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
721 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
722 break;
723 case ISD::ZERO_EXTEND:
724 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000725 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000726 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000727 break;
728 case ISD::TRUNCATE:
729 if (Operand.getValueType() == VT) return Operand; // noop truncate
730 if (OpOpcode == ISD::TRUNCATE)
731 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000732 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
733 // If the source is smaller than the dest, we still need an extend.
734 if (Operand.Val->getOperand(0).getValueType() < VT)
735 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
736 else if (Operand.Val->getOperand(0).getValueType() > VT)
737 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
738 else
739 return Operand.Val->getOperand(0);
740 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000741 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000742 case ISD::FNEG:
743 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
744 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
745 Operand.Val->getOperand(0));
746 if (OpOpcode == ISD::FNEG) // --X -> X
747 return Operand.Val->getOperand(0);
748 break;
749 case ISD::FABS:
750 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
751 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
752 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000753 }
754
755 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
756 if (N) return SDOperand(N, 0);
757 N = new SDNode(Opcode, Operand);
758 N->setValueTypes(VT);
759 AllNodes.push_back(N);
760 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000761}
762
Chris Lattner36019aa2005-04-18 03:48:41 +0000763/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
764/// this predicate to simplify operations downstream. V and Mask are known to
765/// be the same type.
766static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
767 const TargetLowering &TLI) {
768 unsigned SrcBits;
769 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000770
Chris Lattner36019aa2005-04-18 03:48:41 +0000771 // If we know the result of a setcc has the top bits zero, use this info.
772 switch (Op.getOpcode()) {
773 case ISD::UNDEF:
774 return true;
775 case ISD::Constant:
776 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
777
778 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000779 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +0000780 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
781
782 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +0000783 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +0000784 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
785 case ISD::ZERO_EXTEND:
786 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
787 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
788
789 case ISD::AND:
790 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +0000791 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +0000792 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
793
794 // FALL THROUGH
795 case ISD::OR:
796 case ISD::XOR:
797 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
798 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
799 case ISD::SELECT:
800 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
801 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +0000802
803 case ISD::SRL:
804 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
805 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
806 uint64_t NewVal = Mask << ShAmt->getValue();
807 SrcBits = MVT::getSizeInBits(Op.getValueType());
808 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
809 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
810 }
811 return false;
812 case ISD::SHL:
813 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
814 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
815 uint64_t NewVal = Mask >> ShAmt->getValue();
816 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
817 }
818 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +0000819 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +0000820 default: break;
821 }
822
823 return false;
824}
825
826
827
Chris Lattnerc3aae252005-01-07 07:46:32 +0000828SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
829 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000830#ifndef NDEBUG
831 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000832 case ISD::TokenFactor:
833 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
834 N2.getValueType() == MVT::Other && "Invalid token factor!");
835 break;
Chris Lattner76365122005-01-16 02:23:22 +0000836 case ISD::AND:
837 case ISD::OR:
838 case ISD::XOR:
839 case ISD::UDIV:
840 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000841 case ISD::MULHU:
842 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000843 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
844 // fall through
845 case ISD::ADD:
846 case ISD::SUB:
847 case ISD::MUL:
848 case ISD::SDIV:
849 case ISD::SREM:
850 assert(N1.getValueType() == N2.getValueType() &&
851 N1.getValueType() == VT && "Binary operator types must match!");
852 break;
853
854 case ISD::SHL:
855 case ISD::SRA:
856 case ISD::SRL:
857 assert(VT == N1.getValueType() &&
858 "Shift operators return type must be the same as their first arg");
859 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000860 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000861 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000862 case ISD::FP_ROUND_INREG: {
863 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
864 assert(VT == N1.getValueType() && "Not an inreg round!");
865 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
866 "Cannot FP_ROUND_INREG integer types");
867 assert(EVT <= VT && "Not rounding down!");
868 break;
869 }
870 case ISD::SIGN_EXTEND_INREG: {
871 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
872 assert(VT == N1.getValueType() && "Not an inreg extend!");
873 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
874 "Cannot *_EXTEND_INREG FP types");
875 assert(EVT <= VT && "Not extending!");
876 }
877
Chris Lattner76365122005-01-16 02:23:22 +0000878 default: break;
879 }
880#endif
881
Chris Lattnerc3aae252005-01-07 07:46:32 +0000882 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
883 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
884 if (N1C) {
885 if (N2C) {
886 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
887 switch (Opcode) {
888 case ISD::ADD: return getConstant(C1 + C2, VT);
889 case ISD::SUB: return getConstant(C1 - C2, VT);
890 case ISD::MUL: return getConstant(C1 * C2, VT);
891 case ISD::UDIV:
892 if (C2) return getConstant(C1 / C2, VT);
893 break;
894 case ISD::UREM :
895 if (C2) return getConstant(C1 % C2, VT);
896 break;
897 case ISD::SDIV :
898 if (C2) return getConstant(N1C->getSignExtended() /
899 N2C->getSignExtended(), VT);
900 break;
901 case ISD::SREM :
902 if (C2) return getConstant(N1C->getSignExtended() %
903 N2C->getSignExtended(), VT);
904 break;
905 case ISD::AND : return getConstant(C1 & C2, VT);
906 case ISD::OR : return getConstant(C1 | C2, VT);
907 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000908 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
909 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
910 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000911 default: break;
912 }
913
914 } else { // Cannonicalize constant to RHS if commutative
915 if (isCommutativeBinOp(Opcode)) {
916 std::swap(N1C, N2C);
917 std::swap(N1, N2);
918 }
919 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000920
921 switch (Opcode) {
922 default: break;
923 case ISD::SHL: // shl 0, X -> 0
924 if (N1C->isNullValue()) return N1;
925 break;
926 case ISD::SRL: // srl 0, X -> 0
927 if (N1C->isNullValue()) return N1;
928 break;
929 case ISD::SRA: // sra -1, X -> -1
930 if (N1C->isAllOnesValue()) return N1;
931 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000932 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
933 // Extending a constant? Just return the extended constant.
934 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
935 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +0000936 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000937 }
938
939 if (N2C) {
940 uint64_t C2 = N2C->getValue();
941
942 switch (Opcode) {
943 case ISD::ADD:
944 if (!C2) return N1; // add X, 0 -> X
945 break;
946 case ISD::SUB:
947 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +0000948 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000949 case ISD::MUL:
950 if (!C2) return N2; // mul X, 0 -> 0
951 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
952 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
953
Chris Lattnerb48da392005-01-23 04:39:44 +0000954 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000955 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +0000956 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +0000957 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000958 }
959 break;
960
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000961 case ISD::MULHU:
962 case ISD::MULHS:
963 if (!C2) return N2; // mul X, 0 -> 0
964
965 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
966 return getConstant(0, VT);
967
968 // Many others could be handled here, including -1, powers of 2, etc.
969 break;
970
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +0000972 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000973 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +0000974 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +0000975 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000976 }
977 break;
978
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000979 case ISD::SHL:
980 case ISD::SRL:
981 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +0000982 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +0000983 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +0000984 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
985 return getNode(ISD::UNDEF, N1.getValueType());
986 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000987 if (C2 == 0) return N1;
Chris Lattner57aa5962005-05-09 17:06:45 +0000988
989 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
990 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
991 unsigned OpSAC = OpSA->getValue();
992 if (N1.getOpcode() == ISD::SHL) {
993 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
994 return getConstant(0, N1.getValueType());
995 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
996 getConstant(C2+OpSAC, N2.getValueType()));
997 } else if (N1.getOpcode() == ISD::SRL) {
998 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
999 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1000 getConstant(~0ULL << OpSAC, VT));
1001 if (C2 > OpSAC) {
1002 return getNode(ISD::SHL, VT, Mask,
1003 getConstant(C2-OpSAC, N2.getValueType()));
1004 } else {
1005 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1006 return getNode(ISD::SRL, VT, Mask,
1007 getConstant(OpSAC-C2, N2.getValueType()));
1008 }
1009 } else if (N1.getOpcode() == ISD::SRA) {
1010 // if C1 == C2, just mask out low bits.
1011 if (C2 == OpSAC)
1012 return getNode(ISD::AND, VT, N1.getOperand(0),
1013 getConstant(~0ULL << C2, VT));
1014 }
1015 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001016 break;
1017
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 case ISD::AND:
1019 if (!C2) return N2; // X and 0 -> 0
1020 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001021 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001022
Chris Lattner36019aa2005-04-18 03:48:41 +00001023 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1024 return getConstant(0, VT);
1025
Chris Lattner588bbbf2005-04-21 06:28:15 +00001026 {
1027 uint64_t NotC2 = ~C2;
1028 if (VT != MVT::i64)
1029 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1030
1031 if (MaskedValueIsZero(N1, NotC2, TLI))
1032 return N1; // if (X & ~C2) -> 0, the and is redundant
1033 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001034
Chris Lattnerdea29e22005-04-10 01:13:15 +00001035 // FIXME: Should add a corresponding version of this for
1036 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1037 // we don't have yet.
1038
Chris Lattner0f2287b2005-04-13 02:38:18 +00001039 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1040 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001041 // If we are masking out the part of our input that was extended, just
1042 // mask the input to the extension directly.
1043 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001044 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001045 if ((C2 & (~0ULL << ExtendBits)) == 0)
1046 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
1047 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048 break;
1049 case ISD::OR:
1050 if (!C2)return N1; // X or 0 -> X
1051 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001052 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001053 break;
1054 case ISD::XOR:
1055 if (!C2) return N1; // X xor 0 -> X
1056 if (N2C->isAllOnesValue()) {
1057 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
1058 // !(X op Y) -> (X !op Y)
1059 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1060 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001061 SetCC->getValueType(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001062 SetCC->getOperand(0), SetCC->getOperand(1));
1063 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1064 SDNode *Op = N1.Val;
1065 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1066 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1067 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1068 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1069 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1070 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1071 if (Op->getOpcode() == ISD::AND)
1072 return getNode(ISD::OR, VT, LHS, RHS);
1073 return getNode(ISD::AND, VT, LHS, RHS);
1074 }
1075 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001076 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077 }
1078 break;
1079 }
1080
1081 // Reassociate ((X op C1) op C2) if possible.
1082 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1083 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001084 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001085 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1086 }
1087
1088 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1089 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001090 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001091 if (N2CFP) {
1092 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1093 switch (Opcode) {
1094 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1095 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1096 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1097 case ISD::SDIV:
1098 if (C2) return getConstantFP(C1 / C2, VT);
1099 break;
1100 case ISD::SREM :
1101 if (C2) return getConstantFP(fmod(C1, C2), VT);
1102 break;
1103 default: break;
1104 }
1105
1106 } else { // Cannonicalize constant to RHS if commutative
1107 if (isCommutativeBinOp(Opcode)) {
1108 std::swap(N1CFP, N2CFP);
1109 std::swap(N1, N2);
1110 }
1111 }
1112
Chris Lattner15e4b012005-07-10 00:07:11 +00001113 if (Opcode == ISD::FP_ROUND_INREG)
1114 return getNode(ISD::FP_EXTEND, VT,
1115 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1116 }
1117
Chris Lattnerc3aae252005-01-07 07:46:32 +00001118 // Finally, fold operations that do not require constants.
1119 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001120 case ISD::TokenFactor:
1121 if (N1.getOpcode() == ISD::EntryToken)
1122 return N2;
1123 if (N2.getOpcode() == ISD::EntryToken)
1124 return N1;
1125 break;
1126
Chris Lattnerc3aae252005-01-07 07:46:32 +00001127 case ISD::AND:
1128 case ISD::OR:
1129 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
1130 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
1131 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1132 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1133 ISD::CondCode Op2 = RHS->getCondition();
1134
Chris Lattner706aa962005-04-18 03:59:53 +00001135 if (LR == RR && isa<ConstantSDNode>(LR) &&
Chris Lattner706aa962005-04-18 03:59:53 +00001136 Op2 == LHS->getCondition() && MVT::isInteger(LL.getValueType())) {
Chris Lattner229ab2e2005-04-25 21:20:28 +00001137 // (X != 0) | (Y != 0) -> (X|Y != 0)
1138 // (X == 0) & (Y == 0) -> (X|Y == 0)
1139 // (X < 0) | (Y < 0) -> (X|Y < 0)
1140 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1141 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1142 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1143 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001144 return getSetCC(Op2, VT,
Chris Lattner706aa962005-04-18 03:59:53 +00001145 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001146
1147 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1148 // (X == -1) & (Y == -1) -> (X&Y == -1)
1149 // (X != -1) | (Y != -1) -> (X&Y != -1)
Chris Lattnerd36f9792005-04-26 01:18:33 +00001150 // (X > -1) | (Y > -1) -> (X&Y > -1)
Chris Lattner229ab2e2005-04-25 21:20:28 +00001151 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
Chris Lattnerd36f9792005-04-26 01:18:33 +00001152 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1153 (Opcode == ISD::OR && Op2 == ISD::SETGT))
Chris Lattner229ab2e2005-04-25 21:20:28 +00001154 return getSetCC(Op2, VT,
1155 getNode(ISD::AND, LR.getValueType(), LL, RL), LR);
1156 // (X > -1) & (Y > -1) -> (X|Y > -1)
1157 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1158 return getSetCC(Op2, VT,
1159 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
1160 }
Chris Lattner706aa962005-04-18 03:59:53 +00001161 }
1162
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1164 if (LL == RR && LR == RL) {
1165 Op2 = ISD::getSetCCSwappedOperands(Op2);
1166 goto MatchedBackwards;
1167 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001168
Chris Lattnerc3aae252005-01-07 07:46:32 +00001169 if (LL == RL && LR == RR) {
1170 MatchedBackwards:
1171 ISD::CondCode Result;
1172 bool isInteger = MVT::isInteger(LL.getValueType());
1173 if (Opcode == ISD::OR)
1174 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
1175 isInteger);
1176 else
1177 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
1178 isInteger);
1179 if (Result != ISD::SETCC_INVALID)
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001180 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001181 }
1182 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001183
1184 // and/or zext(a), zext(b) -> zext(and/or a, b)
1185 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1186 N2.getOpcode() == ISD::ZERO_EXTEND &&
1187 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1188 return getNode(ISD::ZERO_EXTEND, VT,
1189 getNode(Opcode, N1.getOperand(0).getValueType(),
1190 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001191 break;
1192 case ISD::XOR:
1193 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1194 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001195 case ISD::ADD:
1196 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1197 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1198 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1199 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001200 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1201 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1202 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1203 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1204 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1205 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001206 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1207 !MVT::isFloatingPoint(N2.getValueType()))
1208 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001209 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001210 case ISD::SUB:
1211 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001212 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001213 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001214 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001215 if (N1.Val->getOperand(1) == N2 &&
1216 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001217 return N1.Val->getOperand(0); // (A+B)-B == A
1218 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001219 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1220 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001221 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001222 case ISD::FP_ROUND_INREG:
1223 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1224 break;
1225 case ISD::SIGN_EXTEND_INREG: {
1226 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1227 if (EVT == VT) return N1; // Not actually extending
1228
1229 // If we are sign extending an extension, use the original source.
1230 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1231 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1232 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001233
Chris Lattner15e4b012005-07-10 00:07:11 +00001234 // If we are sign extending a sextload, return just the load.
1235 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001236 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001237 return N1;
1238
1239 // If we are extending the result of a setcc, and we already know the
1240 // contents of the top bits, eliminate the extension.
1241 if (N1.getOpcode() == ISD::SETCC &&
1242 TLI.getSetCCResultContents() ==
1243 TargetLowering::ZeroOrNegativeOneSetCCResult)
1244 return N1;
1245
1246 // If we are sign extending the result of an (and X, C) operation, and we
1247 // know the extended bits are zeros already, don't do the extend.
1248 if (N1.getOpcode() == ISD::AND)
1249 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1250 uint64_t Mask = N1C->getValue();
1251 unsigned NumBits = MVT::getSizeInBits(EVT);
1252 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1253 return N1;
1254 }
1255 break;
1256 }
1257
Nate Begemaneea805e2005-04-13 21:23:31 +00001258 // FIXME: figure out how to safely handle things like
1259 // int foo(int x) { return 1 << (x & 255); }
1260 // int bar() { return foo(256); }
1261#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001262 case ISD::SHL:
1263 case ISD::SRL:
1264 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001265 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001266 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001267 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001268 else if (N2.getOpcode() == ISD::AND)
1269 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1270 // If the and is only masking out bits that cannot effect the shift,
1271 // eliminate the and.
1272 unsigned NumBits = MVT::getSizeInBits(VT);
1273 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1274 return getNode(Opcode, VT, N1, N2.getOperand(0));
1275 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001276 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001277#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001278 }
1279
Chris Lattner27e9b412005-05-11 18:57:39 +00001280 // Memoize this node if possible.
1281 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001282 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001283 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1284 if (BON) return SDOperand(BON, 0);
1285
1286 BON = N = new SDNode(Opcode, N1, N2);
1287 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001288 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001289 }
1290
Chris Lattner3e011362005-05-14 07:45:46 +00001291 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001292 AllNodes.push_back(N);
1293 return SDOperand(N, 0);
1294}
1295
Chris Lattner88de6e72005-05-12 00:17:04 +00001296// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001297// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001298void SDNode::setAdjCallChain(SDOperand N) {
1299 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001300 assert((getOpcode() == ISD::CALLSEQ_START ||
1301 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001302
1303 Operands[0].Val->removeUser(this);
1304 Operands[0] = N;
1305 N.Val->Uses.push_back(this);
1306}
1307
1308
1309
Chris Lattnerc3aae252005-01-07 07:46:32 +00001310SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001311 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001312 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001313 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1314 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001315 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001316
1317 // Loads have a token chain.
1318 N->setValueTypes(VT, MVT::Other);
1319 AllNodes.push_back(N);
1320 return SDOperand(N, 0);
1321}
1322
Chris Lattner5f056bf2005-07-10 01:55:33 +00001323
1324SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1325 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1326 MVT::ValueType EVT) {
1327 std::vector<SDOperand> Ops;
1328 Ops.reserve(4);
1329 Ops.push_back(Chain);
1330 Ops.push_back(Ptr);
1331 Ops.push_back(SV);
1332 Ops.push_back(getValueType(EVT));
1333 std::vector<MVT::ValueType> VTs;
1334 VTs.reserve(2);
1335 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1336 return getNode(Opcode, VTs, Ops);
1337}
1338
Chris Lattnerc3aae252005-01-07 07:46:32 +00001339SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1340 SDOperand N1, SDOperand N2, SDOperand N3) {
1341 // Perform various simplifications.
1342 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1343 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1344 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1345 switch (Opcode) {
1346 case ISD::SELECT:
1347 if (N1C)
1348 if (N1C->getValue())
1349 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001350 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001351 return N3; // select false, X, Y -> Y
1352
1353 if (N2 == N3) return N2; // select C, X, X -> X
1354
1355 if (VT == MVT::i1) { // Boolean SELECT
1356 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001357 if (N2C->getValue()) // select C, 1, X -> C | X
1358 return getNode(ISD::OR, VT, N1, N3);
1359 else // select C, 0, X -> ~C & X
1360 return getNode(ISD::AND, VT,
1361 getNode(ISD::XOR, N1.getValueType(), N1,
1362 getConstant(1, N1.getValueType())), N3);
1363 } else if (N3C) {
1364 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1365 return getNode(ISD::OR, VT,
1366 getNode(ISD::XOR, N1.getValueType(), N1,
1367 getConstant(1, N1.getValueType())), N2);
1368 else // select C, X, 0 -> C & X
1369 return getNode(ISD::AND, VT, N1, N2);
1370 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001371
1372 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1373 return getNode(ISD::OR, VT, N1, N3);
1374 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1375 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001376 }
1377
Chris Lattner59723e92005-04-09 05:15:53 +00001378 // If this is a selectcc, check to see if we can simplify the result.
1379 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1380 if (ConstantFPSDNode *CFP =
1381 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1382 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1383 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1384 if ((SetCC->getCondition() == ISD::SETGE ||
1385 SetCC->getCondition() == ISD::SETGT) &&
1386 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1387 N3.getOperand(0) == N2)
1388 return getNode(ISD::FABS, VT, N2);
1389
1390 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1391 if ((SetCC->getCondition() == ISD::SETLT ||
1392 SetCC->getCondition() == ISD::SETLE) &&
1393 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1394 N2.getOperand(0) == N3)
1395 return getNode(ISD::FABS, VT, N3);
1396 }
Chris Lattner6980d822005-05-12 06:27:02 +00001397 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1), A
Nate Begemaneea805e2005-04-13 21:23:31 +00001398 if (ConstantSDNode *CN =
1399 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1400 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
1401 if (SetCC->getCondition() == ISD::SETLT) {
1402 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1403 MVT::ValueType AType = N2.getValueType();
1404 if (XType >= AType) {
Chris Lattner6980d822005-05-12 06:27:02 +00001405 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1406 // single-bit constant. FIXME: remove once the dag combiner
1407 // exists.
1408 if (ConstantSDNode *AC = dyn_cast<ConstantSDNode>(N2))
1409 if ((AC->getValue() & (AC->getValue()-1)) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001410 unsigned ShCtV = Log2_64(AC->getValue());
Chris Lattner6980d822005-05-12 06:27:02 +00001411 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1412 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
1413 SDOperand Shift = getNode(ISD::SRL, XType,
1414 SetCC->getOperand(0), ShCt);
1415 if (XType > AType)
1416 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1417 return getNode(ISD::AND, AType, Shift, N2);
1418 }
1419
1420
Nate Begemaneea805e2005-04-13 21:23:31 +00001421 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1422 getConstant(MVT::getSizeInBits(XType)-1,
1423 TLI.getShiftAmountTy()));
1424 if (XType > AType)
1425 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1426 return getNode(ISD::AND, AType, Shift, N2);
1427 }
1428 }
Chris Lattner59723e92005-04-09 05:15:53 +00001429 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001430 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001431 case ISD::BRCOND:
1432 if (N2C)
1433 if (N2C->getValue()) // Unconditional branch
1434 return getNode(ISD::BR, MVT::Other, N1, N3);
1435 else
1436 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001437 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001438 }
1439
Chris Lattner385328c2005-05-14 07:42:29 +00001440 std::vector<SDOperand> Ops;
1441 Ops.reserve(3);
1442 Ops.push_back(N1);
1443 Ops.push_back(N2);
1444 Ops.push_back(N3);
1445
1446 // Memoize nodes.
1447 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1448 if (N) return SDOperand(N, 0);
1449
1450 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001451 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001452 AllNodes.push_back(N);
1453 return SDOperand(N, 0);
1454}
1455
1456SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001457 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001458 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001459 std::vector<SDOperand> Ops;
1460 Ops.reserve(4);
1461 Ops.push_back(N1);
1462 Ops.push_back(N2);
1463 Ops.push_back(N3);
1464 Ops.push_back(N4);
1465 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001466}
1467
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001468SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1469 SDOperand N1, SDOperand N2, SDOperand N3,
1470 SDOperand N4, SDOperand N5) {
1471 std::vector<SDOperand> Ops;
1472 Ops.reserve(5);
1473 Ops.push_back(N1);
1474 Ops.push_back(N2);
1475 Ops.push_back(N3);
1476 Ops.push_back(N4);
1477 Ops.push_back(N5);
1478 return getNode(Opcode, VT, Ops);
1479}
1480
1481
Chris Lattner0437cdd2005-05-09 04:14:13 +00001482SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001483 assert((!V || isa<PointerType>(V->getType())) &&
1484 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001485 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1486 if (N) return SDOperand(N, 0);
1487
1488 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001489 AllNodes.push_back(N);
1490 return SDOperand(N, 0);
1491}
1492
1493SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001494 std::vector<SDOperand> &Ops) {
1495 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001496 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001497 case 1: return getNode(Opcode, VT, Ops[0]);
1498 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1499 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001500 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001501 }
Chris Lattneref847df2005-04-09 03:27:28 +00001502
Chris Lattner89c34632005-05-14 06:20:26 +00001503 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001504 switch (Opcode) {
1505 default: break;
1506 case ISD::BRCONDTWOWAY:
1507 if (N1C)
1508 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001509 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001510 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001511 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001512 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001513
1514 case ISD::TRUNCSTORE: {
1515 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1516 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1517#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1518 // If this is a truncating store of a constant, convert to the desired type
1519 // and store it instead.
1520 if (isa<Constant>(Ops[0])) {
1521 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1522 if (isa<Constant>(Op))
1523 N1 = Op;
1524 }
1525 // Also for ConstantFP?
1526#endif
1527 if (Ops[0].getValueType() == EVT) // Normal store?
1528 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1529 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1530 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1531 "Can't do FP-INT conversion!");
1532 break;
1533 }
Chris Lattneref847df2005-04-09 03:27:28 +00001534 }
1535
Chris Lattner385328c2005-05-14 07:42:29 +00001536 // Memoize nodes.
1537 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1538 if (N) return SDOperand(N, 0);
1539 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001540 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001541 AllNodes.push_back(N);
1542 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001543}
1544
Chris Lattner89c34632005-05-14 06:20:26 +00001545SDOperand SelectionDAG::getNode(unsigned Opcode,
1546 std::vector<MVT::ValueType> &ResultTys,
1547 std::vector<SDOperand> &Ops) {
1548 if (ResultTys.size() == 1)
1549 return getNode(Opcode, ResultTys[0], Ops);
1550
Chris Lattner5f056bf2005-07-10 01:55:33 +00001551 switch (Opcode) {
1552 case ISD::EXTLOAD:
1553 case ISD::SEXTLOAD:
1554 case ISD::ZEXTLOAD: {
1555 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1556 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1557 // If they are asking for an extending load from/to the same thing, return a
1558 // normal load.
1559 if (ResultTys[0] == EVT)
1560 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1561 assert(EVT < ResultTys[0] &&
1562 "Should only be an extending load, not truncating!");
1563 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1564 "Cannot sign/zero extend a FP load!");
1565 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1566 "Cannot convert from FP to Int or Int -> FP!");
1567 break;
1568 }
1569
Chris Lattnere89083a2005-05-14 07:25:05 +00001570 // FIXME: figure out how to safely handle things like
1571 // int foo(int x) { return 1 << (x & 255); }
1572 // int bar() { return foo(256); }
1573#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001574 case ISD::SRA_PARTS:
1575 case ISD::SRL_PARTS:
1576 case ISD::SHL_PARTS:
1577 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001578 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001579 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1580 else if (N3.getOpcode() == ISD::AND)
1581 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1582 // If the and is only masking out bits that cannot effect the shift,
1583 // eliminate the and.
1584 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1585 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1586 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1587 }
1588 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001589#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001590 }
Chris Lattner89c34632005-05-14 06:20:26 +00001591
1592 // Memoize the node.
1593 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1594 Ops))];
1595 if (N) return SDOperand(N, 0);
1596 N = new SDNode(Opcode, Ops);
1597 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001598 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001599 return SDOperand(N, 0);
1600}
1601
Chris Lattner5c884562005-01-12 18:37:47 +00001602/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1603/// indicated value. This method ignores uses of other values defined by this
1604/// operation.
1605bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1606 assert(Value < getNumValues() && "Bad value!");
1607
1608 // If there is only one value, this is easy.
1609 if (getNumValues() == 1)
1610 return use_size() == NUses;
1611 if (Uses.size() < NUses) return false;
1612
1613 SDOperand TheValue(this, Value);
1614
1615 std::set<SDNode*> UsersHandled;
1616
1617 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1618 UI != E; ++UI) {
1619 SDNode *User = *UI;
1620 if (User->getNumOperands() == 1 ||
1621 UsersHandled.insert(User).second) // First time we've seen this?
1622 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1623 if (User->getOperand(i) == TheValue) {
1624 if (NUses == 0)
1625 return false; // too many uses
1626 --NUses;
1627 }
1628 }
1629
1630 // Found exactly the right number of uses?
1631 return NUses == 0;
1632}
1633
1634
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001635const char *SDNode::getOperationName() const {
1636 switch (getOpcode()) {
1637 default: return "<<Unknown>>";
Andrew Lenharth95762122005-03-31 21:24:06 +00001638 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001639 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001640 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001641 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001642 case ISD::Constant: return "Constant";
1643 case ISD::ConstantFP: return "ConstantFP";
1644 case ISD::GlobalAddress: return "GlobalAddress";
1645 case ISD::FrameIndex: return "FrameIndex";
1646 case ISD::BasicBlock: return "BasicBlock";
1647 case ISD::ExternalSymbol: return "ExternalSymbol";
1648 case ISD::ConstantPool: return "ConstantPoolIndex";
1649 case ISD::CopyToReg: return "CopyToReg";
1650 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001651 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001652 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001653
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001654 // Unary operators
1655 case ISD::FABS: return "fabs";
1656 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001657 case ISD::FSQRT: return "fsqrt";
1658 case ISD::FSIN: return "fsin";
1659 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001660
1661 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001662 case ISD::ADD: return "add";
1663 case ISD::SUB: return "sub";
1664 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001665 case ISD::MULHU: return "mulhu";
1666 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001667 case ISD::SDIV: return "sdiv";
1668 case ISD::UDIV: return "udiv";
1669 case ISD::SREM: return "srem";
1670 case ISD::UREM: return "urem";
1671 case ISD::AND: return "and";
1672 case ISD::OR: return "or";
1673 case ISD::XOR: return "xor";
1674 case ISD::SHL: return "shl";
1675 case ISD::SRA: return "sra";
1676 case ISD::SRL: return "srl";
1677
1678 case ISD::SELECT: return "select";
Chris Lattner17eee182005-01-20 18:50:55 +00001679 case ISD::ADD_PARTS: return "add_parts";
1680 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001681 case ISD::SHL_PARTS: return "shl_parts";
1682 case ISD::SRA_PARTS: return "sra_parts";
1683 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001684
Chris Lattner7f644642005-04-28 21:44:03 +00001685 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001686 case ISD::SIGN_EXTEND: return "sign_extend";
1687 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001688 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001689 case ISD::TRUNCATE: return "truncate";
1690 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001691 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001692 case ISD::FP_EXTEND: return "fp_extend";
1693
1694 case ISD::SINT_TO_FP: return "sint_to_fp";
1695 case ISD::UINT_TO_FP: return "uint_to_fp";
1696 case ISD::FP_TO_SINT: return "fp_to_sint";
1697 case ISD::FP_TO_UINT: return "fp_to_uint";
1698
1699 // Control flow instructions
1700 case ISD::BR: return "br";
1701 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001702 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001703 case ISD::RET: return "ret";
1704 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001705 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001706 case ISD::CALLSEQ_START: return "callseq_start";
1707 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001708
1709 // Other operators
1710 case ISD::LOAD: return "load";
1711 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001712 case ISD::EXTLOAD: return "extload";
1713 case ISD::SEXTLOAD: return "sextload";
1714 case ISD::ZEXTLOAD: return "zextload";
1715 case ISD::TRUNCSTORE: return "truncstore";
1716
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001717 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1718 case ISD::EXTRACT_ELEMENT: return "extract_element";
1719 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001720 case ISD::MEMSET: return "memset";
1721 case ISD::MEMCPY: return "memcpy";
1722 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001723
Chris Lattner276260b2005-05-11 04:50:30 +00001724 // Bit counting
1725 case ISD::CTPOP: return "ctpop";
1726 case ISD::CTTZ: return "cttz";
1727 case ISD::CTLZ: return "ctlz";
1728
1729 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001730 case ISD::READPORT: return "readport";
1731 case ISD::WRITEPORT: return "writeport";
1732 case ISD::READIO: return "readio";
1733 case ISD::WRITEIO: return "writeio";
1734
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001735 case ISD::SETCC:
1736 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1737 switch (SetCC->getCondition()) {
1738 default: assert(0 && "Unknown setcc condition!");
1739 case ISD::SETOEQ: return "setcc:setoeq";
1740 case ISD::SETOGT: return "setcc:setogt";
1741 case ISD::SETOGE: return "setcc:setoge";
1742 case ISD::SETOLT: return "setcc:setolt";
1743 case ISD::SETOLE: return "setcc:setole";
1744 case ISD::SETONE: return "setcc:setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001745
1746 case ISD::SETO: return "setcc:seto";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001747 case ISD::SETUO: return "setcc:setuo";
1748 case ISD::SETUEQ: return "setcc:setue";
1749 case ISD::SETUGT: return "setcc:setugt";
1750 case ISD::SETUGE: return "setcc:setuge";
1751 case ISD::SETULT: return "setcc:setult";
1752 case ISD::SETULE: return "setcc:setule";
1753 case ISD::SETUNE: return "setcc:setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001754
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001755 case ISD::SETEQ: return "setcc:seteq";
1756 case ISD::SETGT: return "setcc:setgt";
1757 case ISD::SETGE: return "setcc:setge";
1758 case ISD::SETLT: return "setcc:setlt";
1759 case ISD::SETLE: return "setcc:setle";
1760 case ISD::SETNE: return "setcc:setne";
1761 }
1762 }
1763}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001764
1765void SDNode::dump() const {
1766 std::cerr << (void*)this << ": ";
1767
1768 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1769 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001770 if (getValueType(i) == MVT::Other)
1771 std::cerr << "ch";
1772 else
1773 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001774 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001775 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001776
1777 std::cerr << " ";
1778 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1779 if (i) std::cerr << ", ";
1780 std::cerr << (void*)getOperand(i).Val;
1781 if (unsigned RN = getOperand(i).ResNo)
1782 std::cerr << ":" << RN;
1783 }
1784
1785 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1786 std::cerr << "<" << CSDN->getValue() << ">";
1787 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1788 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001789 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001790 dyn_cast<GlobalAddressSDNode>(this)) {
1791 std::cerr << "<";
1792 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001793 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001794 std::cerr << "<" << FIDN->getIndex() << ">";
1795 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1796 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001797 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001798 std::cerr << "<";
1799 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1800 if (LBB)
1801 std::cerr << LBB->getName() << " ";
1802 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001803 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001804 std::cerr << "<reg #" << C2V->getReg() << ">";
1805 } else if (const ExternalSymbolSDNode *ES =
1806 dyn_cast<ExternalSymbolSDNode>(this)) {
1807 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001808 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1809 if (M->getValue())
1810 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1811 else
1812 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001813 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001814}
1815
Chris Lattnerea946cd2005-01-09 20:38:33 +00001816static void DumpNodes(SDNode *N, unsigned indent) {
1817 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1818 if (N->getOperand(i).Val->hasOneUse())
1819 DumpNodes(N->getOperand(i).Val, indent+2);
1820 else
1821 std::cerr << "\n" << std::string(indent+2, ' ')
1822 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001823
Chris Lattnerea946cd2005-01-09 20:38:33 +00001824
1825 std::cerr << "\n" << std::string(indent, ' ');
1826 N->dump();
1827}
1828
Chris Lattnerc3aae252005-01-07 07:46:32 +00001829void SelectionDAG::dump() const {
1830 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001831 std::vector<SDNode*> Nodes(AllNodes);
1832 std::sort(Nodes.begin(), Nodes.end());
1833
1834 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001835 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1836 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001837 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001838
1839 DumpNodes(getRoot().Val, 2);
1840
Chris Lattnerc3aae252005-01-07 07:46:32 +00001841 std::cerr << "\n\n";
1842}
1843