blob: a9907fa55a929b956b9b1ae34e3081bfa505ffc8 [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()) {
Chris Lattner36019aa2005-04-18 03:48:41 +0000773 case ISD::Constant:
774 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
775
776 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000777 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +0000778 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
779
780 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +0000781 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +0000782 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
783 case ISD::ZERO_EXTEND:
784 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
785 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
786
787 case ISD::AND:
788 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +0000789 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +0000790 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
791
792 // FALL THROUGH
793 case ISD::OR:
794 case ISD::XOR:
795 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
796 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
797 case ISD::SELECT:
798 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
799 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +0000800
801 case ISD::SRL:
802 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
803 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
804 uint64_t NewVal = Mask << ShAmt->getValue();
805 SrcBits = MVT::getSizeInBits(Op.getValueType());
806 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
807 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
808 }
809 return false;
810 case ISD::SHL:
811 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
812 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
813 uint64_t NewVal = Mask >> ShAmt->getValue();
814 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
815 }
816 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +0000817 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +0000818 default: break;
819 }
820
821 return false;
822}
823
824
825
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
827 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000828#ifndef NDEBUG
829 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000830 case ISD::TokenFactor:
831 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
832 N2.getValueType() == MVT::Other && "Invalid token factor!");
833 break;
Chris Lattner76365122005-01-16 02:23:22 +0000834 case ISD::AND:
835 case ISD::OR:
836 case ISD::XOR:
837 case ISD::UDIV:
838 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000839 case ISD::MULHU:
840 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000841 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
842 // fall through
843 case ISD::ADD:
844 case ISD::SUB:
845 case ISD::MUL:
846 case ISD::SDIV:
847 case ISD::SREM:
848 assert(N1.getValueType() == N2.getValueType() &&
849 N1.getValueType() == VT && "Binary operator types must match!");
850 break;
851
852 case ISD::SHL:
853 case ISD::SRA:
854 case ISD::SRL:
855 assert(VT == N1.getValueType() &&
856 "Shift operators return type must be the same as their first arg");
857 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000858 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000859 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000860 case ISD::FP_ROUND_INREG: {
861 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
862 assert(VT == N1.getValueType() && "Not an inreg round!");
863 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
864 "Cannot FP_ROUND_INREG integer types");
865 assert(EVT <= VT && "Not rounding down!");
866 break;
867 }
868 case ISD::SIGN_EXTEND_INREG: {
869 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
870 assert(VT == N1.getValueType() && "Not an inreg extend!");
871 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
872 "Cannot *_EXTEND_INREG FP types");
873 assert(EVT <= VT && "Not extending!");
874 }
875
Chris Lattner76365122005-01-16 02:23:22 +0000876 default: break;
877 }
878#endif
879
Chris Lattnerc3aae252005-01-07 07:46:32 +0000880 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
881 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
882 if (N1C) {
883 if (N2C) {
884 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
885 switch (Opcode) {
886 case ISD::ADD: return getConstant(C1 + C2, VT);
887 case ISD::SUB: return getConstant(C1 - C2, VT);
888 case ISD::MUL: return getConstant(C1 * C2, VT);
889 case ISD::UDIV:
890 if (C2) return getConstant(C1 / C2, VT);
891 break;
892 case ISD::UREM :
893 if (C2) return getConstant(C1 % C2, VT);
894 break;
895 case ISD::SDIV :
896 if (C2) return getConstant(N1C->getSignExtended() /
897 N2C->getSignExtended(), VT);
898 break;
899 case ISD::SREM :
900 if (C2) return getConstant(N1C->getSignExtended() %
901 N2C->getSignExtended(), VT);
902 break;
903 case ISD::AND : return getConstant(C1 & C2, VT);
904 case ISD::OR : return getConstant(C1 | C2, VT);
905 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000906 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
907 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
908 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000909 default: break;
910 }
911
912 } else { // Cannonicalize constant to RHS if commutative
913 if (isCommutativeBinOp(Opcode)) {
914 std::swap(N1C, N2C);
915 std::swap(N1, N2);
916 }
917 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000918
919 switch (Opcode) {
920 default: break;
921 case ISD::SHL: // shl 0, X -> 0
922 if (N1C->isNullValue()) return N1;
923 break;
924 case ISD::SRL: // srl 0, X -> 0
925 if (N1C->isNullValue()) return N1;
926 break;
927 case ISD::SRA: // sra -1, X -> -1
928 if (N1C->isAllOnesValue()) return N1;
929 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000930 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
931 // Extending a constant? Just return the extended constant.
932 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
933 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +0000934 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000935 }
936
937 if (N2C) {
938 uint64_t C2 = N2C->getValue();
939
940 switch (Opcode) {
941 case ISD::ADD:
942 if (!C2) return N1; // add X, 0 -> X
943 break;
944 case ISD::SUB:
945 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +0000946 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947 case ISD::MUL:
948 if (!C2) return N2; // mul X, 0 -> 0
949 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
950 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
951
Chris Lattnerb48da392005-01-23 04:39:44 +0000952 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000953 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +0000954 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +0000955 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000956 }
957 break;
958
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000959 case ISD::MULHU:
960 case ISD::MULHS:
961 if (!C2) return N2; // mul X, 0 -> 0
962
963 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
964 return getConstant(0, VT);
965
966 // Many others could be handled here, including -1, powers of 2, etc.
967 break;
968
Chris Lattnerc3aae252005-01-07 07:46:32 +0000969 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +0000970 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000971 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +0000972 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +0000973 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000974 }
975 break;
976
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000977 case ISD::SHL:
978 case ISD::SRL:
979 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +0000980 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +0000981 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +0000982 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
983 return getNode(ISD::UNDEF, N1.getValueType());
984 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000985 if (C2 == 0) return N1;
Chris Lattner57aa5962005-05-09 17:06:45 +0000986
987 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
988 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
989 unsigned OpSAC = OpSA->getValue();
990 if (N1.getOpcode() == ISD::SHL) {
991 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
992 return getConstant(0, N1.getValueType());
993 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
994 getConstant(C2+OpSAC, N2.getValueType()));
995 } else if (N1.getOpcode() == ISD::SRL) {
996 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
997 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
998 getConstant(~0ULL << OpSAC, VT));
999 if (C2 > OpSAC) {
1000 return getNode(ISD::SHL, VT, Mask,
1001 getConstant(C2-OpSAC, N2.getValueType()));
1002 } else {
1003 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1004 return getNode(ISD::SRL, VT, Mask,
1005 getConstant(OpSAC-C2, N2.getValueType()));
1006 }
1007 } else if (N1.getOpcode() == ISD::SRA) {
1008 // if C1 == C2, just mask out low bits.
1009 if (C2 == OpSAC)
1010 return getNode(ISD::AND, VT, N1.getOperand(0),
1011 getConstant(~0ULL << C2, VT));
1012 }
1013 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001014 break;
1015
Chris Lattnerc3aae252005-01-07 07:46:32 +00001016 case ISD::AND:
1017 if (!C2) return N2; // X and 0 -> 0
1018 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001019 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001020
Chris Lattner36019aa2005-04-18 03:48:41 +00001021 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1022 return getConstant(0, VT);
1023
Chris Lattner588bbbf2005-04-21 06:28:15 +00001024 {
1025 uint64_t NotC2 = ~C2;
1026 if (VT != MVT::i64)
1027 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1028
1029 if (MaskedValueIsZero(N1, NotC2, TLI))
1030 return N1; // if (X & ~C2) -> 0, the and is redundant
1031 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001032
Chris Lattnerdea29e22005-04-10 01:13:15 +00001033 // FIXME: Should add a corresponding version of this for
1034 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1035 // we don't have yet.
1036
Chris Lattner0f2287b2005-04-13 02:38:18 +00001037 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1038 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001039 // If we are masking out the part of our input that was extended, just
1040 // mask the input to the extension directly.
1041 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001042 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001043 if ((C2 & (~0ULL << ExtendBits)) == 0)
1044 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001045 } else if (N1.getOpcode() == ISD::OR) {
1046 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1047 if ((ORI->getValue() & C2) == C2) {
1048 // If the 'or' is setting all of the bits that we are masking for,
1049 // we know the result of the AND will be the AND mask itself.
1050 return N2;
1051 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001052 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001053 break;
1054 case ISD::OR:
1055 if (!C2)return N1; // X or 0 -> X
1056 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001057 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001058 break;
1059 case ISD::XOR:
1060 if (!C2) return N1; // X xor 0 -> X
1061 if (N2C->isAllOnesValue()) {
1062 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
1063 // !(X op Y) -> (X !op Y)
1064 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
1065 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001066 SetCC->getValueType(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067 SetCC->getOperand(0), SetCC->getOperand(1));
1068 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1069 SDNode *Op = N1.Val;
1070 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1071 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1072 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1073 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1074 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1075 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1076 if (Op->getOpcode() == ISD::AND)
1077 return getNode(ISD::OR, VT, LHS, RHS);
1078 return getNode(ISD::AND, VT, LHS, RHS);
1079 }
1080 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001081 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 }
1083 break;
1084 }
1085
1086 // Reassociate ((X op C1) op C2) if possible.
1087 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1088 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001089 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001090 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1091 }
1092
1093 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1094 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001095 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 if (N2CFP) {
1097 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1098 switch (Opcode) {
1099 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1100 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1101 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1102 case ISD::SDIV:
1103 if (C2) return getConstantFP(C1 / C2, VT);
1104 break;
1105 case ISD::SREM :
1106 if (C2) return getConstantFP(fmod(C1, C2), VT);
1107 break;
1108 default: break;
1109 }
1110
1111 } else { // Cannonicalize constant to RHS if commutative
1112 if (isCommutativeBinOp(Opcode)) {
1113 std::swap(N1CFP, N2CFP);
1114 std::swap(N1, N2);
1115 }
1116 }
1117
Chris Lattner15e4b012005-07-10 00:07:11 +00001118 if (Opcode == ISD::FP_ROUND_INREG)
1119 return getNode(ISD::FP_EXTEND, VT,
1120 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1121 }
1122
Chris Lattnerc3aae252005-01-07 07:46:32 +00001123 // Finally, fold operations that do not require constants.
1124 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001125 case ISD::TokenFactor:
1126 if (N1.getOpcode() == ISD::EntryToken)
1127 return N2;
1128 if (N2.getOpcode() == ISD::EntryToken)
1129 return N1;
1130 break;
1131
Chris Lattnerc3aae252005-01-07 07:46:32 +00001132 case ISD::AND:
1133 case ISD::OR:
1134 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
1135 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
1136 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1137 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1138 ISD::CondCode Op2 = RHS->getCondition();
1139
Chris Lattner706aa962005-04-18 03:59:53 +00001140 if (LR == RR && isa<ConstantSDNode>(LR) &&
Chris Lattner706aa962005-04-18 03:59:53 +00001141 Op2 == LHS->getCondition() && MVT::isInteger(LL.getValueType())) {
Chris Lattner229ab2e2005-04-25 21:20:28 +00001142 // (X != 0) | (Y != 0) -> (X|Y != 0)
1143 // (X == 0) & (Y == 0) -> (X|Y == 0)
1144 // (X < 0) | (Y < 0) -> (X|Y < 0)
1145 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1146 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1147 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1148 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001149 return getSetCC(Op2, VT,
Chris Lattner706aa962005-04-18 03:59:53 +00001150 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001151
1152 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1153 // (X == -1) & (Y == -1) -> (X&Y == -1)
1154 // (X != -1) | (Y != -1) -> (X&Y != -1)
Chris Lattnerd36f9792005-04-26 01:18:33 +00001155 // (X > -1) | (Y > -1) -> (X&Y > -1)
Chris Lattner229ab2e2005-04-25 21:20:28 +00001156 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
Chris Lattnerd36f9792005-04-26 01:18:33 +00001157 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1158 (Opcode == ISD::OR && Op2 == ISD::SETGT))
Chris Lattner229ab2e2005-04-25 21:20:28 +00001159 return getSetCC(Op2, VT,
1160 getNode(ISD::AND, LR.getValueType(), LL, RL), LR);
1161 // (X > -1) & (Y > -1) -> (X|Y > -1)
1162 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1163 return getSetCC(Op2, VT,
1164 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
1165 }
Chris Lattner706aa962005-04-18 03:59:53 +00001166 }
1167
Chris Lattnerc3aae252005-01-07 07:46:32 +00001168 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1169 if (LL == RR && LR == RL) {
1170 Op2 = ISD::getSetCCSwappedOperands(Op2);
1171 goto MatchedBackwards;
1172 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001173
Chris Lattnerc3aae252005-01-07 07:46:32 +00001174 if (LL == RL && LR == RR) {
1175 MatchedBackwards:
1176 ISD::CondCode Result;
1177 bool isInteger = MVT::isInteger(LL.getValueType());
1178 if (Opcode == ISD::OR)
1179 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
1180 isInteger);
1181 else
1182 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
1183 isInteger);
1184 if (Result != ISD::SETCC_INVALID)
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001185 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001186 }
1187 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001188
1189 // and/or zext(a), zext(b) -> zext(and/or a, b)
1190 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1191 N2.getOpcode() == ISD::ZERO_EXTEND &&
1192 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1193 return getNode(ISD::ZERO_EXTEND, VT,
1194 getNode(Opcode, N1.getOperand(0).getValueType(),
1195 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001196 break;
1197 case ISD::XOR:
1198 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1199 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001200 case ISD::ADD:
1201 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1202 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1203 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1204 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001205 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1206 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1207 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1208 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1209 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1210 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001211 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1212 !MVT::isFloatingPoint(N2.getValueType()))
1213 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001214 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001215 case ISD::SUB:
1216 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001217 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001218 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001219 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001220 if (N1.Val->getOperand(1) == N2 &&
1221 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001222 return N1.Val->getOperand(0); // (A+B)-B == A
1223 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001224 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1225 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001226 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001227 case ISD::FP_ROUND_INREG:
1228 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1229 break;
1230 case ISD::SIGN_EXTEND_INREG: {
1231 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1232 if (EVT == VT) return N1; // Not actually extending
1233
1234 // If we are sign extending an extension, use the original source.
1235 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1236 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1237 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001238
Chris Lattner15e4b012005-07-10 00:07:11 +00001239 // If we are sign extending a sextload, return just the load.
1240 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001241 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001242 return N1;
1243
1244 // If we are extending the result of a setcc, and we already know the
1245 // contents of the top bits, eliminate the extension.
1246 if (N1.getOpcode() == ISD::SETCC &&
1247 TLI.getSetCCResultContents() ==
1248 TargetLowering::ZeroOrNegativeOneSetCCResult)
1249 return N1;
1250
1251 // If we are sign extending the result of an (and X, C) operation, and we
1252 // know the extended bits are zeros already, don't do the extend.
1253 if (N1.getOpcode() == ISD::AND)
1254 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1255 uint64_t Mask = N1C->getValue();
1256 unsigned NumBits = MVT::getSizeInBits(EVT);
1257 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1258 return N1;
1259 }
1260 break;
1261 }
1262
Nate Begemaneea805e2005-04-13 21:23:31 +00001263 // FIXME: figure out how to safely handle things like
1264 // int foo(int x) { return 1 << (x & 255); }
1265 // int bar() { return foo(256); }
1266#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001267 case ISD::SHL:
1268 case ISD::SRL:
1269 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001270 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001271 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001272 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001273 else if (N2.getOpcode() == ISD::AND)
1274 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1275 // If the and is only masking out bits that cannot effect the shift,
1276 // eliminate the and.
1277 unsigned NumBits = MVT::getSizeInBits(VT);
1278 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1279 return getNode(Opcode, VT, N1, N2.getOperand(0));
1280 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001281 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001282#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001283 }
1284
Chris Lattner27e9b412005-05-11 18:57:39 +00001285 // Memoize this node if possible.
1286 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001287 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001288 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1289 if (BON) return SDOperand(BON, 0);
1290
1291 BON = N = new SDNode(Opcode, N1, N2);
1292 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001293 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001294 }
1295
Chris Lattner3e011362005-05-14 07:45:46 +00001296 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001297 AllNodes.push_back(N);
1298 return SDOperand(N, 0);
1299}
1300
Chris Lattner88de6e72005-05-12 00:17:04 +00001301// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001302// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001303void SDNode::setAdjCallChain(SDOperand N) {
1304 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001305 assert((getOpcode() == ISD::CALLSEQ_START ||
1306 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001307
1308 Operands[0].Val->removeUser(this);
1309 Operands[0] = N;
1310 N.Val->Uses.push_back(this);
1311}
1312
1313
1314
Chris Lattnerc3aae252005-01-07 07:46:32 +00001315SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001316 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001317 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001318 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1319 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001320 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321
1322 // Loads have a token chain.
1323 N->setValueTypes(VT, MVT::Other);
1324 AllNodes.push_back(N);
1325 return SDOperand(N, 0);
1326}
1327
Chris Lattner5f056bf2005-07-10 01:55:33 +00001328
1329SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1330 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1331 MVT::ValueType EVT) {
1332 std::vector<SDOperand> Ops;
1333 Ops.reserve(4);
1334 Ops.push_back(Chain);
1335 Ops.push_back(Ptr);
1336 Ops.push_back(SV);
1337 Ops.push_back(getValueType(EVT));
1338 std::vector<MVT::ValueType> VTs;
1339 VTs.reserve(2);
1340 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1341 return getNode(Opcode, VTs, Ops);
1342}
1343
Chris Lattnerc3aae252005-01-07 07:46:32 +00001344SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1345 SDOperand N1, SDOperand N2, SDOperand N3) {
1346 // Perform various simplifications.
1347 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1348 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1349 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1350 switch (Opcode) {
1351 case ISD::SELECT:
1352 if (N1C)
1353 if (N1C->getValue())
1354 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001355 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001356 return N3; // select false, X, Y -> Y
1357
1358 if (N2 == N3) return N2; // select C, X, X -> X
1359
1360 if (VT == MVT::i1) { // Boolean SELECT
1361 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001362 if (N2C->getValue()) // select C, 1, X -> C | X
1363 return getNode(ISD::OR, VT, N1, N3);
1364 else // select C, 0, X -> ~C & X
1365 return getNode(ISD::AND, VT,
1366 getNode(ISD::XOR, N1.getValueType(), N1,
1367 getConstant(1, N1.getValueType())), N3);
1368 } else if (N3C) {
1369 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1370 return getNode(ISD::OR, VT,
1371 getNode(ISD::XOR, N1.getValueType(), N1,
1372 getConstant(1, N1.getValueType())), N2);
1373 else // select C, X, 0 -> C & X
1374 return getNode(ISD::AND, VT, N1, N2);
1375 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001376
1377 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1378 return getNode(ISD::OR, VT, N1, N3);
1379 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1380 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001381 }
1382
Chris Lattner59723e92005-04-09 05:15:53 +00001383 // If this is a selectcc, check to see if we can simplify the result.
1384 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1385 if (ConstantFPSDNode *CFP =
1386 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1387 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1388 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1389 if ((SetCC->getCondition() == ISD::SETGE ||
1390 SetCC->getCondition() == ISD::SETGT) &&
1391 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1392 N3.getOperand(0) == N2)
1393 return getNode(ISD::FABS, VT, N2);
1394
1395 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1396 if ((SetCC->getCondition() == ISD::SETLT ||
1397 SetCC->getCondition() == ISD::SETLE) &&
1398 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1399 N2.getOperand(0) == N3)
1400 return getNode(ISD::FABS, VT, N3);
1401 }
Chris Lattner6980d822005-05-12 06:27:02 +00001402 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1), A
Nate Begemaneea805e2005-04-13 21:23:31 +00001403 if (ConstantSDNode *CN =
1404 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1405 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
1406 if (SetCC->getCondition() == ISD::SETLT) {
1407 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1408 MVT::ValueType AType = N2.getValueType();
1409 if (XType >= AType) {
Chris Lattner6980d822005-05-12 06:27:02 +00001410 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1411 // single-bit constant. FIXME: remove once the dag combiner
1412 // exists.
1413 if (ConstantSDNode *AC = dyn_cast<ConstantSDNode>(N2))
1414 if ((AC->getValue() & (AC->getValue()-1)) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001415 unsigned ShCtV = Log2_64(AC->getValue());
Chris Lattner6980d822005-05-12 06:27:02 +00001416 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1417 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
1418 SDOperand Shift = getNode(ISD::SRL, XType,
1419 SetCC->getOperand(0), ShCt);
1420 if (XType > AType)
1421 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1422 return getNode(ISD::AND, AType, Shift, N2);
1423 }
1424
1425
Nate Begemaneea805e2005-04-13 21:23:31 +00001426 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1427 getConstant(MVT::getSizeInBits(XType)-1,
1428 TLI.getShiftAmountTy()));
1429 if (XType > AType)
1430 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1431 return getNode(ISD::AND, AType, Shift, N2);
1432 }
1433 }
Chris Lattner59723e92005-04-09 05:15:53 +00001434 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001435 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001436 case ISD::BRCOND:
1437 if (N2C)
1438 if (N2C->getValue()) // Unconditional branch
1439 return getNode(ISD::BR, MVT::Other, N1, N3);
1440 else
1441 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001442 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001443 }
1444
Chris Lattner385328c2005-05-14 07:42:29 +00001445 std::vector<SDOperand> Ops;
1446 Ops.reserve(3);
1447 Ops.push_back(N1);
1448 Ops.push_back(N2);
1449 Ops.push_back(N3);
1450
1451 // Memoize nodes.
1452 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1453 if (N) return SDOperand(N, 0);
1454
1455 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001456 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001457 AllNodes.push_back(N);
1458 return SDOperand(N, 0);
1459}
1460
1461SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001462 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001463 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001464 std::vector<SDOperand> Ops;
1465 Ops.reserve(4);
1466 Ops.push_back(N1);
1467 Ops.push_back(N2);
1468 Ops.push_back(N3);
1469 Ops.push_back(N4);
1470 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001471}
1472
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001473SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1474 SDOperand N1, SDOperand N2, SDOperand N3,
1475 SDOperand N4, SDOperand N5) {
1476 std::vector<SDOperand> Ops;
1477 Ops.reserve(5);
1478 Ops.push_back(N1);
1479 Ops.push_back(N2);
1480 Ops.push_back(N3);
1481 Ops.push_back(N4);
1482 Ops.push_back(N5);
1483 return getNode(Opcode, VT, Ops);
1484}
1485
1486
Chris Lattner0437cdd2005-05-09 04:14:13 +00001487SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001488 assert((!V || isa<PointerType>(V->getType())) &&
1489 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001490 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1491 if (N) return SDOperand(N, 0);
1492
1493 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001494 AllNodes.push_back(N);
1495 return SDOperand(N, 0);
1496}
1497
1498SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001499 std::vector<SDOperand> &Ops) {
1500 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001501 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001502 case 1: return getNode(Opcode, VT, Ops[0]);
1503 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1504 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001505 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001506 }
Chris Lattneref847df2005-04-09 03:27:28 +00001507
Chris Lattner89c34632005-05-14 06:20:26 +00001508 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001509 switch (Opcode) {
1510 default: break;
1511 case ISD::BRCONDTWOWAY:
1512 if (N1C)
1513 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001514 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001515 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001516 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001517 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001518
1519 case ISD::TRUNCSTORE: {
1520 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1521 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1522#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1523 // If this is a truncating store of a constant, convert to the desired type
1524 // and store it instead.
1525 if (isa<Constant>(Ops[0])) {
1526 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1527 if (isa<Constant>(Op))
1528 N1 = Op;
1529 }
1530 // Also for ConstantFP?
1531#endif
1532 if (Ops[0].getValueType() == EVT) // Normal store?
1533 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1534 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1535 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1536 "Can't do FP-INT conversion!");
1537 break;
1538 }
Chris Lattneref847df2005-04-09 03:27:28 +00001539 }
1540
Chris Lattner385328c2005-05-14 07:42:29 +00001541 // Memoize nodes.
1542 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1543 if (N) return SDOperand(N, 0);
1544 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001545 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001546 AllNodes.push_back(N);
1547 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001548}
1549
Chris Lattner89c34632005-05-14 06:20:26 +00001550SDOperand SelectionDAG::getNode(unsigned Opcode,
1551 std::vector<MVT::ValueType> &ResultTys,
1552 std::vector<SDOperand> &Ops) {
1553 if (ResultTys.size() == 1)
1554 return getNode(Opcode, ResultTys[0], Ops);
1555
Chris Lattner5f056bf2005-07-10 01:55:33 +00001556 switch (Opcode) {
1557 case ISD::EXTLOAD:
1558 case ISD::SEXTLOAD:
1559 case ISD::ZEXTLOAD: {
1560 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1561 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1562 // If they are asking for an extending load from/to the same thing, return a
1563 // normal load.
1564 if (ResultTys[0] == EVT)
1565 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1566 assert(EVT < ResultTys[0] &&
1567 "Should only be an extending load, not truncating!");
1568 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1569 "Cannot sign/zero extend a FP load!");
1570 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1571 "Cannot convert from FP to Int or Int -> FP!");
1572 break;
1573 }
1574
Chris Lattnere89083a2005-05-14 07:25:05 +00001575 // FIXME: figure out how to safely handle things like
1576 // int foo(int x) { return 1 << (x & 255); }
1577 // int bar() { return foo(256); }
1578#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001579 case ISD::SRA_PARTS:
1580 case ISD::SRL_PARTS:
1581 case ISD::SHL_PARTS:
1582 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001583 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001584 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1585 else if (N3.getOpcode() == ISD::AND)
1586 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1587 // If the and is only masking out bits that cannot effect the shift,
1588 // eliminate the and.
1589 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1590 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1591 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1592 }
1593 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001594#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001595 }
Chris Lattner89c34632005-05-14 06:20:26 +00001596
1597 // Memoize the node.
1598 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1599 Ops))];
1600 if (N) return SDOperand(N, 0);
1601 N = new SDNode(Opcode, Ops);
1602 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001603 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001604 return SDOperand(N, 0);
1605}
1606
Chris Lattner5c884562005-01-12 18:37:47 +00001607/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1608/// indicated value. This method ignores uses of other values defined by this
1609/// operation.
1610bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1611 assert(Value < getNumValues() && "Bad value!");
1612
1613 // If there is only one value, this is easy.
1614 if (getNumValues() == 1)
1615 return use_size() == NUses;
1616 if (Uses.size() < NUses) return false;
1617
1618 SDOperand TheValue(this, Value);
1619
1620 std::set<SDNode*> UsersHandled;
1621
1622 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1623 UI != E; ++UI) {
1624 SDNode *User = *UI;
1625 if (User->getNumOperands() == 1 ||
1626 UsersHandled.insert(User).second) // First time we've seen this?
1627 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1628 if (User->getOperand(i) == TheValue) {
1629 if (NUses == 0)
1630 return false; // too many uses
1631 --NUses;
1632 }
1633 }
1634
1635 // Found exactly the right number of uses?
1636 return NUses == 0;
1637}
1638
1639
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001640const char *SDNode::getOperationName() const {
1641 switch (getOpcode()) {
1642 default: return "<<Unknown>>";
Andrew Lenharth95762122005-03-31 21:24:06 +00001643 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001644 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001645 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001646 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001647 case ISD::Constant: return "Constant";
1648 case ISD::ConstantFP: return "ConstantFP";
1649 case ISD::GlobalAddress: return "GlobalAddress";
1650 case ISD::FrameIndex: return "FrameIndex";
1651 case ISD::BasicBlock: return "BasicBlock";
1652 case ISD::ExternalSymbol: return "ExternalSymbol";
1653 case ISD::ConstantPool: return "ConstantPoolIndex";
1654 case ISD::CopyToReg: return "CopyToReg";
1655 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001656 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001657 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001658
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001659 // Unary operators
1660 case ISD::FABS: return "fabs";
1661 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001662 case ISD::FSQRT: return "fsqrt";
1663 case ISD::FSIN: return "fsin";
1664 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001665
1666 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001667 case ISD::ADD: return "add";
1668 case ISD::SUB: return "sub";
1669 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001670 case ISD::MULHU: return "mulhu";
1671 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001672 case ISD::SDIV: return "sdiv";
1673 case ISD::UDIV: return "udiv";
1674 case ISD::SREM: return "srem";
1675 case ISD::UREM: return "urem";
1676 case ISD::AND: return "and";
1677 case ISD::OR: return "or";
1678 case ISD::XOR: return "xor";
1679 case ISD::SHL: return "shl";
1680 case ISD::SRA: return "sra";
1681 case ISD::SRL: return "srl";
1682
1683 case ISD::SELECT: return "select";
Chris Lattner17eee182005-01-20 18:50:55 +00001684 case ISD::ADD_PARTS: return "add_parts";
1685 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001686 case ISD::SHL_PARTS: return "shl_parts";
1687 case ISD::SRA_PARTS: return "sra_parts";
1688 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001689
Chris Lattner7f644642005-04-28 21:44:03 +00001690 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001691 case ISD::SIGN_EXTEND: return "sign_extend";
1692 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001693 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001694 case ISD::TRUNCATE: return "truncate";
1695 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001696 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001697 case ISD::FP_EXTEND: return "fp_extend";
1698
1699 case ISD::SINT_TO_FP: return "sint_to_fp";
1700 case ISD::UINT_TO_FP: return "uint_to_fp";
1701 case ISD::FP_TO_SINT: return "fp_to_sint";
1702 case ISD::FP_TO_UINT: return "fp_to_uint";
1703
1704 // Control flow instructions
1705 case ISD::BR: return "br";
1706 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001707 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001708 case ISD::RET: return "ret";
1709 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001710 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001711 case ISD::CALLSEQ_START: return "callseq_start";
1712 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001713
1714 // Other operators
1715 case ISD::LOAD: return "load";
1716 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001717 case ISD::EXTLOAD: return "extload";
1718 case ISD::SEXTLOAD: return "sextload";
1719 case ISD::ZEXTLOAD: return "zextload";
1720 case ISD::TRUNCSTORE: return "truncstore";
1721
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001722 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1723 case ISD::EXTRACT_ELEMENT: return "extract_element";
1724 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001725 case ISD::MEMSET: return "memset";
1726 case ISD::MEMCPY: return "memcpy";
1727 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001728
Chris Lattner276260b2005-05-11 04:50:30 +00001729 // Bit counting
1730 case ISD::CTPOP: return "ctpop";
1731 case ISD::CTTZ: return "cttz";
1732 case ISD::CTLZ: return "ctlz";
1733
1734 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001735 case ISD::READPORT: return "readport";
1736 case ISD::WRITEPORT: return "writeport";
1737 case ISD::READIO: return "readio";
1738 case ISD::WRITEIO: return "writeio";
1739
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001740 case ISD::SETCC:
1741 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1742 switch (SetCC->getCondition()) {
1743 default: assert(0 && "Unknown setcc condition!");
1744 case ISD::SETOEQ: return "setcc:setoeq";
1745 case ISD::SETOGT: return "setcc:setogt";
1746 case ISD::SETOGE: return "setcc:setoge";
1747 case ISD::SETOLT: return "setcc:setolt";
1748 case ISD::SETOLE: return "setcc:setole";
1749 case ISD::SETONE: return "setcc:setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001750
1751 case ISD::SETO: return "setcc:seto";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001752 case ISD::SETUO: return "setcc:setuo";
1753 case ISD::SETUEQ: return "setcc:setue";
1754 case ISD::SETUGT: return "setcc:setugt";
1755 case ISD::SETUGE: return "setcc:setuge";
1756 case ISD::SETULT: return "setcc:setult";
1757 case ISD::SETULE: return "setcc:setule";
1758 case ISD::SETUNE: return "setcc:setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001759
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001760 case ISD::SETEQ: return "setcc:seteq";
1761 case ISD::SETGT: return "setcc:setgt";
1762 case ISD::SETGE: return "setcc:setge";
1763 case ISD::SETLT: return "setcc:setlt";
1764 case ISD::SETLE: return "setcc:setle";
1765 case ISD::SETNE: return "setcc:setne";
1766 }
1767 }
1768}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001769
1770void SDNode::dump() const {
1771 std::cerr << (void*)this << ": ";
1772
1773 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1774 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001775 if (getValueType(i) == MVT::Other)
1776 std::cerr << "ch";
1777 else
1778 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001779 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001780 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001781
1782 std::cerr << " ";
1783 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1784 if (i) std::cerr << ", ";
1785 std::cerr << (void*)getOperand(i).Val;
1786 if (unsigned RN = getOperand(i).ResNo)
1787 std::cerr << ":" << RN;
1788 }
1789
1790 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1791 std::cerr << "<" << CSDN->getValue() << ">";
1792 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1793 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001794 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001795 dyn_cast<GlobalAddressSDNode>(this)) {
1796 std::cerr << "<";
1797 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001798 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001799 std::cerr << "<" << FIDN->getIndex() << ">";
1800 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1801 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001802 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001803 std::cerr << "<";
1804 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1805 if (LBB)
1806 std::cerr << LBB->getName() << " ";
1807 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001808 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001809 std::cerr << "<reg #" << C2V->getReg() << ">";
1810 } else if (const ExternalSymbolSDNode *ES =
1811 dyn_cast<ExternalSymbolSDNode>(this)) {
1812 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001813 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1814 if (M->getValue())
1815 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1816 else
1817 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001818 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001819}
1820
Chris Lattnerea946cd2005-01-09 20:38:33 +00001821static void DumpNodes(SDNode *N, unsigned indent) {
1822 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1823 if (N->getOperand(i).Val->hasOneUse())
1824 DumpNodes(N->getOperand(i).Val, indent+2);
1825 else
1826 std::cerr << "\n" << std::string(indent+2, ' ')
1827 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001828
Chris Lattnerea946cd2005-01-09 20:38:33 +00001829
1830 std::cerr << "\n" << std::string(indent, ' ');
1831 N->dump();
1832}
1833
Chris Lattnerc3aae252005-01-07 07:46:32 +00001834void SelectionDAG::dump() const {
1835 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001836 std::vector<SDNode*> Nodes(AllNodes);
1837 std::sort(Nodes.begin(), Nodes.end());
1838
1839 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001840 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1841 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001842 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001843
1844 DumpNodes(getRoot().Val, 2);
1845
Chris Lattnerc3aae252005-01-07 07:46:32 +00001846 std::cerr << "\n\n";
1847}
1848