blob: 7e870e6c2f99ecff147bbbf86f9d596fa62e97f8 [file] [log] [blame]
Chris Lattnerc3aae252005-01-07 07:46:32 +00001//===-- SelectionDAG.cpp - Implement the SelectionDAG data structures -----===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattnerb48da392005-01-23 04:39:44 +000019#include "llvm/Target/TargetLowering.h"
Reid Spencer954da372004-07-04 12:19:56 +000020#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000021#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000022#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000023#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000024using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner5cdcc582005-01-09 20:52:51 +000026static bool isCommutativeBinOp(unsigned Opcode) {
27 switch (Opcode) {
28 case ISD::ADD:
29 case ISD::MUL:
30 case ISD::AND:
31 case ISD::OR:
32 case ISD::XOR: return true;
33 default: return false; // FIXME: Need commutative info for user ops!
34 }
35}
36
37static bool isAssociativeBinOp(unsigned Opcode) {
38 switch (Opcode) {
39 case ISD::ADD:
40 case ISD::MUL:
41 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need associative info for user ops!
45 }
46}
47
48static unsigned ExactLog2(uint64_t Val) {
49 unsigned Count = 0;
50 while (Val != 1) {
51 Val >>= 1;
52 ++Count;
53 }
54 return Count;
55}
56
57// isInvertibleForFree - Return true if there is no cost to emitting the logical
58// inverse of this node.
59static bool isInvertibleForFree(SDOperand N) {
60 if (isa<ConstantSDNode>(N.Val)) return true;
61 if (isa<SetCCSDNode>(N.Val) && N.Val->hasOneUse())
62 return true;
63 return false;
64}
65
66
Chris Lattnerc3aae252005-01-07 07:46:32 +000067/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
68/// when given the operation for (X op Y).
69ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
70 // To perform this operation, we just need to swap the L and G bits of the
71 // operation.
72 unsigned OldL = (Operation >> 2) & 1;
73 unsigned OldG = (Operation >> 1) & 1;
74 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
75 (OldL << 1) | // New G bit
76 (OldG << 2)); // New L bit.
77}
78
79/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
80/// 'op' is a valid SetCC operation.
81ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
82 unsigned Operation = Op;
83 if (isInteger)
84 Operation ^= 7; // Flip L, G, E bits, but not U.
85 else
86 Operation ^= 15; // Flip all of the condition bits.
87 if (Operation > ISD::SETTRUE2)
88 Operation &= ~8; // Don't let N and U bits get set.
89 return ISD::CondCode(Operation);
90}
91
92
93/// isSignedOp - For an integer comparison, return 1 if the comparison is a
94/// signed operation and 2 if the result is an unsigned comparison. Return zero
95/// if the operation does not depend on the sign of the input (setne and seteq).
96static int isSignedOp(ISD::CondCode Opcode) {
97 switch (Opcode) {
98 default: assert(0 && "Illegal integer setcc operation!");
99 case ISD::SETEQ:
100 case ISD::SETNE: return 0;
101 case ISD::SETLT:
102 case ISD::SETLE:
103 case ISD::SETGT:
104 case ISD::SETGE: return 1;
105 case ISD::SETULT:
106 case ISD::SETULE:
107 case ISD::SETUGT:
108 case ISD::SETUGE: return 2;
109 }
110}
111
112/// getSetCCOrOperation - Return the result of a logical OR between different
113/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
114/// returns SETCC_INVALID if it is not possible to represent the resultant
115/// comparison.
116ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
117 bool isInteger) {
118 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
119 // Cannot fold a signed integer setcc with an unsigned integer setcc.
120 return ISD::SETCC_INVALID;
121
122 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
123
124 // If the N and U bits get set then the resultant comparison DOES suddenly
125 // care about orderedness, and is true when ordered.
126 if (Op > ISD::SETTRUE2)
127 Op &= ~16; // Clear the N bit.
128 return ISD::CondCode(Op);
129}
130
131/// getSetCCAndOperation - Return the result of a logical AND between different
132/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
133/// function returns zero if it is not possible to represent the resultant
134/// comparison.
135ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
136 bool isInteger) {
137 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
138 // Cannot fold a signed setcc with an unsigned setcc.
139 return ISD::SETCC_INVALID;
140
141 // Combine all of the condition bits.
142 return ISD::CondCode(Op1 & Op2);
143}
144
Chris Lattnerb48da392005-01-23 04:39:44 +0000145const TargetMachine &SelectionDAG::getTarget() const {
146 return TLI.getTargetMachine();
147}
148
149
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000150/// RemoveDeadNodes - This method deletes all unreachable nodes in the
151/// SelectionDAG, including nodes (like loads) that have uses of their token
152/// chain but no other uses and no side effect. If a node is passed in as an
153/// argument, it is used as the seed for node deletion.
154void SelectionDAG::RemoveDeadNodes(SDNode *N) {
155 std::set<SDNode*> AllNodeSet(AllNodes.begin(), AllNodes.end());
156
157 // Create a dummy node (which is not added to allnodes), that adds a reference
158 // to the root node, preventing it from being deleted.
159 SDNode *DummyNode = new SDNode(ISD::EntryToken, getRoot());
160
161 DeleteNodeIfDead(N, &AllNodeSet);
162
163 Restart:
164 unsigned NumNodes = AllNodeSet.size();
165 for (std::set<SDNode*>::iterator I = AllNodeSet.begin(), E = AllNodeSet.end();
166 I != E; ++I) {
167 // Try to delete this node.
168 DeleteNodeIfDead(*I, &AllNodeSet);
169
170 // If we actually deleted any nodes, do not use invalid iterators in
171 // AllNodeSet.
172 if (AllNodeSet.size() != NumNodes)
173 goto Restart;
174 }
175
176 // Restore AllNodes.
177 if (AllNodes.size() != NumNodes)
178 AllNodes.assign(AllNodeSet.begin(), AllNodeSet.end());
179
180 // If the root changed (e.g. it was a dead load, update the root).
181 setRoot(DummyNode->getOperand(0));
182
183 // Now that we are done with the dummy node, delete it.
184 DummyNode->getOperand(0).Val->removeUser(DummyNode);
185 delete DummyNode;
186}
187
188void SelectionDAG::DeleteNodeIfDead(SDNode *N, void *NodeSet) {
189 if (!N->use_empty())
190 return;
191
192 // Okay, we really are going to delete this node. First take this out of the
193 // appropriate CSE map.
194 switch (N->getOpcode()) {
195 case ISD::Constant:
196 Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
197 N->getValueType(0)));
198 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000199 case ISD::ConstantFP: {
200 union {
201 double DV;
202 uint64_t IV;
203 };
204 DV = cast<ConstantFPSDNode>(N)->getValue();
205 ConstantFPs.erase(std::make_pair(IV, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000206 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000207 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000208 case ISD::GlobalAddress:
209 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
210 break;
211 case ISD::FrameIndex:
212 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
213 break;
214 case ISD::ConstantPool:
215 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
216 break;
217 case ISD::BasicBlock:
218 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
219 break;
220 case ISD::ExternalSymbol:
221 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
222 break;
223
224 case ISD::LOAD:
225 Loads.erase(std::make_pair(N->getOperand(1),
226 std::make_pair(N->getOperand(0),
227 N->getValueType(0))));
228 break;
229 case ISD::SETCC:
230 SetCCs.erase(std::make_pair(std::make_pair(N->getOperand(0),
231 N->getOperand(1)),
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000232 std::make_pair(
233 cast<SetCCSDNode>(N)->getCondition(),
234 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000235 break;
Chris Lattner859157d2005-01-15 06:17:04 +0000236 case ISD::TRUNCSTORE:
237 case ISD::SIGN_EXTEND_INREG:
Chris Lattner859157d2005-01-15 06:17:04 +0000238 case ISD::FP_ROUND_INREG:
Chris Lattner69a52152005-01-14 22:38:01 +0000239 case ISD::EXTLOAD:
240 case ISD::SEXTLOAD:
241 case ISD::ZEXTLOAD: {
242 EVTStruct NN;
Chris Lattner8e8bd652005-04-07 00:30:13 +0000243 NN.Opcode = N->getOpcode();
Chris Lattner69a52152005-01-14 22:38:01 +0000244 NN.VT = N->getValueType(0);
245 NN.EVT = cast<MVTSDNode>(N)->getExtraValueType();
Chris Lattner859157d2005-01-15 06:17:04 +0000246 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
247 NN.Ops.push_back(N->getOperand(i));
Chris Lattner69a52152005-01-14 22:38:01 +0000248 MVTSDNodes.erase(NN);
249 break;
250 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000251 default:
252 if (N->getNumOperands() == 1)
253 UnaryOps.erase(std::make_pair(N->getOpcode(),
254 std::make_pair(N->getOperand(0),
255 N->getValueType(0))));
256 else if (N->getNumOperands() == 2)
257 BinaryOps.erase(std::make_pair(N->getOpcode(),
258 std::make_pair(N->getOperand(0),
259 N->getOperand(1))));
260 break;
261 }
262
263 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000265 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000267 O->removeUser(N);
268
269 // Now that we removed this operand, see if there are no uses of it left.
270 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271 }
272
273 // Remove the node from the nodes set and delete it.
274 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
275 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000276
277 // Now that the node is gone, check to see if any of the operands of this node
278 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000279 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000280}
281
282
Chris Lattner78ec3112003-08-11 14:57:33 +0000283SelectionDAG::~SelectionDAG() {
284 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
285 delete AllNodes[i];
286}
287
Chris Lattner0f2287b2005-04-13 02:38:18 +0000288SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000289 if (Op.getValueType() == VT) return Op;
Chris Lattner0f2287b2005-04-13 02:38:18 +0000290 int64_t Imm = ~0ULL >> 64-MVT::getSizeInBits(VT);
291 return getNode(ISD::AND, Op.getValueType(), Op,
292 getConstant(Imm, Op.getValueType()));
293}
294
Chris Lattnerc3aae252005-01-07 07:46:32 +0000295SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
296 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
297 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000298 if (VT != MVT::i64)
299 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
300
Chris Lattnerc3aae252005-01-07 07:46:32 +0000301 SDNode *&N = Constants[std::make_pair(Val, VT)];
302 if (N) return SDOperand(N, 0);
303 N = new ConstantSDNode(Val, VT);
304 AllNodes.push_back(N);
305 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000306}
307
Chris Lattnerc3aae252005-01-07 07:46:32 +0000308SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
309 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
310 if (VT == MVT::f32)
311 Val = (float)Val; // Mask out extra precision.
312
Chris Lattnerd8658612005-02-17 20:17:32 +0000313 // Do the map lookup using the actual bit pattern for the floating point
314 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
315 // we don't have issues with SNANs.
316 union {
317 double DV;
318 uint64_t IV;
319 };
320
321 DV = Val;
322
323 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000324 if (N) return SDOperand(N, 0);
325 N = new ConstantFPSDNode(Val, VT);
326 AllNodes.push_back(N);
327 return SDOperand(N, 0);
328}
329
330
331
332SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
333 MVT::ValueType VT) {
334 SDNode *&N = GlobalValues[GV];
335 if (N) return SDOperand(N, 0);
336 N = new GlobalAddressSDNode(GV,VT);
337 AllNodes.push_back(N);
338 return SDOperand(N, 0);
339}
340
341SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
342 SDNode *&N = FrameIndices[FI];
343 if (N) return SDOperand(N, 0);
344 N = new FrameIndexSDNode(FI, VT);
345 AllNodes.push_back(N);
346 return SDOperand(N, 0);
347}
348
349SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
350 SDNode *N = ConstantPoolIndices[CPIdx];
351 if (N) return SDOperand(N, 0);
352 N = new ConstantPoolSDNode(CPIdx, VT);
353 AllNodes.push_back(N);
354 return SDOperand(N, 0);
355}
356
357SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
358 SDNode *&N = BBNodes[MBB];
359 if (N) return SDOperand(N, 0);
360 N = new BasicBlockSDNode(MBB);
361 AllNodes.push_back(N);
362 return SDOperand(N, 0);
363}
364
365SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
366 SDNode *&N = ExternalSymbols[Sym];
367 if (N) return SDOperand(N, 0);
368 N = new ExternalSymbolSDNode(Sym, VT);
369 AllNodes.push_back(N);
370 return SDOperand(N, 0);
371}
372
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000373SDOperand SelectionDAG::getSetCC(ISD::CondCode Cond, MVT::ValueType VT,
374 SDOperand N1, SDOperand N2) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000375 // These setcc operations always fold.
376 switch (Cond) {
377 default: break;
378 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000379 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000380 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000381 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000382 }
383
Chris Lattner67255a12005-04-07 18:14:58 +0000384 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
385 uint64_t C2 = N2C->getValue();
386 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
387 uint64_t C1 = N1C->getValue();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000388
389 // Sign extend the operands if required
390 if (ISD::isSignedIntSetCC(Cond)) {
391 C1 = N1C->getSignExtended();
392 C2 = N2C->getSignExtended();
393 }
394
395 switch (Cond) {
396 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000397 case ISD::SETEQ: return getConstant(C1 == C2, VT);
398 case ISD::SETNE: return getConstant(C1 != C2, VT);
399 case ISD::SETULT: return getConstant(C1 < C2, VT);
400 case ISD::SETUGT: return getConstant(C1 > C2, VT);
401 case ISD::SETULE: return getConstant(C1 <= C2, VT);
402 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
403 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
404 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
405 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
406 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000407 }
Chris Lattner24673922005-04-07 18:58:54 +0000408 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000409 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
410 // the comparison on the input.
411 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
412 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
413
414 // If the comparison constant has bits in the upper part, the
415 // zero-extended value could never match.
416 if (C2 & (~0ULL << InSize)) {
417 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
418 switch (Cond) {
419 case ISD::SETUGT:
420 case ISD::SETUGE:
421 case ISD::SETEQ: return getConstant(0, VT);
422 case ISD::SETULT:
423 case ISD::SETULE:
424 case ISD::SETNE: return getConstant(1, VT);
425 case ISD::SETGT:
426 case ISD::SETGE:
427 // True if the sign bit of C2 is set.
428 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
429 case ISD::SETLT:
430 case ISD::SETLE:
431 // True if the sign bit of C2 isn't set.
432 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
433 default:
434 break;
435 }
436 }
437
438 // Otherwise, we can perform the comparison with the low bits.
439 switch (Cond) {
440 case ISD::SETEQ:
441 case ISD::SETNE:
442 case ISD::SETUGT:
443 case ISD::SETUGE:
444 case ISD::SETULT:
445 case ISD::SETULE:
446 return getSetCC(Cond, VT, N1.getOperand(0),
447 getConstant(C2, N1.getOperand(0).getValueType()));
448 default:
449 break; // todo, be more careful with signed comparisons
450 }
451 }
452
453
Chris Lattner67255a12005-04-07 18:14:58 +0000454 uint64_t MinVal, MaxVal;
455 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
456 if (ISD::isSignedIntSetCC(Cond)) {
457 MinVal = 1ULL << (OperandBitSize-1);
458 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
459 MaxVal = ~0ULL >> (65-OperandBitSize);
460 else
461 MaxVal = 0;
462 } else {
463 MinVal = 0;
464 MaxVal = ~0ULL >> (64-OperandBitSize);
465 }
466
467 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
468 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
469 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
470 --C2; // X >= C1 --> X > (C1-1)
471 Cond = (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT;
472 N2 = getConstant(C2, N2.getValueType());
473 N2C = cast<ConstantSDNode>(N2.Val);
474 }
475
476 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
477 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
478 ++C2; // X <= C1 --> X < (C1+1)
479 Cond = (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT;
480 N2 = getConstant(C2, N2.getValueType());
481 N2C = cast<ConstantSDNode>(N2.Val);
482 }
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000483
Nate Begeman72ea2812005-04-14 08:56:52 +0000484 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
485 return getConstant(0, VT); // X < MIN --> false
486
487 // Canonicalize setgt X, Min --> setne X, Min
488 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
489 return getSetCC(ISD::SETNE, VT, N1, N2);
490
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000491 // If we have setult X, 1, turn it into seteq X, 0
492 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
493 return getSetCC(ISD::SETEQ, VT, N1,
494 getConstant(MinVal, N1.getValueType()));
Nate Begeman72ea2812005-04-14 08:56:52 +0000495 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000496 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
497 return getSetCC(ISD::SETEQ, VT, N1,
498 getConstant(MaxVal, N1.getValueType()));
Chris Lattner67255a12005-04-07 18:14:58 +0000499
500 // If we have "setcc X, C1", check to see if we can shrink the immediate
501 // by changing cc.
502
503 // SETUGT X, SINTMAX -> SETLT X, 0
504 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
505 C2 == (~0ULL >> (65-OperandBitSize)))
506 return getSetCC(ISD::SETLT, VT, N1, getConstant(0, N2.getValueType()));
507
508 // FIXME: Implement the rest of these.
509
Chris Lattnerc3aae252005-01-07 07:46:32 +0000510 }
Chris Lattner67255a12005-04-07 18:14:58 +0000511 } else if (isa<ConstantSDNode>(N1.Val)) {
512 // Ensure that the constant occurs on the RHS.
513 return getSetCC(ISD::getSetCCSwappedOperands(Cond), VT, N2, N1);
514 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000515
516 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
517 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
518 double C1 = N1C->getValue(), C2 = N2C->getValue();
519
520 switch (Cond) {
521 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000522 case ISD::SETEQ: return getConstant(C1 == C2, VT);
523 case ISD::SETNE: return getConstant(C1 != C2, VT);
524 case ISD::SETLT: return getConstant(C1 < C2, VT);
525 case ISD::SETGT: return getConstant(C1 > C2, VT);
526 case ISD::SETLE: return getConstant(C1 <= C2, VT);
527 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000528 }
529 } else {
530 // Ensure that the constant occurs on the RHS.
531 Cond = ISD::getSetCCSwappedOperands(Cond);
532 std::swap(N1, N2);
533 }
534
535 if (N1 == N2) {
536 // We can always fold X == Y for integer setcc's.
537 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000538 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000539 unsigned UOF = ISD::getUnorderedFlavor(Cond);
540 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000541 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000542 if (UOF == ISD::isTrueWhenEqual(Cond))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000543 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000544 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
545 // if it is not already.
546 Cond = UOF == 0 ? ISD::SETUO : ISD::SETO;
547 }
548
Chris Lattner5cdcc582005-01-09 20:52:51 +0000549 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000550 MVT::isInteger(N1.getValueType())) {
551 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
552 N1.getOpcode() == ISD::XOR) {
553 // Simplify (X+Y) == (X+Z) --> Y == Z
554 if (N1.getOpcode() == N2.getOpcode()) {
555 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000556 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000557 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000558 return getSetCC(Cond, VT, N1.getOperand(0), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000559 if (isCommutativeBinOp(N1.getOpcode())) {
560 // If X op Y == Y op X, try other combinations.
561 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000562 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(0));
Chris Lattner68dc3102005-01-10 02:03:02 +0000563 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000564 return getSetCC(Cond, VT, N1.getOperand(1), N2.getOperand(1));
Chris Lattner68dc3102005-01-10 02:03:02 +0000565 }
566 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000567
568 // FIXME: move this stuff to the DAG Combiner when it exists!
Chris Lattner68dc3102005-01-10 02:03:02 +0000569
570 // Simplify (X+Z) == X --> Z == 0
571 if (N1.getOperand(0) == N2)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000572 return getSetCC(Cond, VT, N1.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000573 getConstant(0, N1.getValueType()));
574 if (N1.getOperand(1) == N2) {
575 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000576 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000577 getConstant(0, N1.getValueType()));
578 else {
579 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
580 // (Z-X) == X --> Z == X<<1
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000581 return getSetCC(Cond, VT, N1.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000582 getNode(ISD::SHL, N2.getValueType(),
Chris Lattnerb48da392005-01-23 04:39:44 +0000583 N2, getConstant(1, TLI.getShiftAmountTy())));
Chris Lattner68dc3102005-01-10 02:03:02 +0000584 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000585 }
586 }
587
Chris Lattner68dc3102005-01-10 02:03:02 +0000588 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
589 N2.getOpcode() == ISD::XOR) {
590 // Simplify X == (X+Z) --> Z == 0
591 if (N2.getOperand(0) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000592 return getSetCC(Cond, VT, N2.getOperand(1),
Chris Lattner68dc3102005-01-10 02:03:02 +0000593 getConstant(0, N2.getValueType()));
594 else if (N2.getOperand(1) == N1)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000595 return getSetCC(Cond, VT, N2.getOperand(0),
Chris Lattner68dc3102005-01-10 02:03:02 +0000596 getConstant(0, N2.getValueType()));
597 }
598 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000599
Chris Lattnerfda2b552005-04-18 04:48:12 +0000600 // Fold away ALL boolean setcc's.
601 if (N1.getValueType() == MVT::i1) {
602 switch (Cond) {
603 default: assert(0 && "Unknown integer setcc!");
604 case ISD::SETEQ: // X == Y -> (X^Y)^1
605 N1 = getNode(ISD::XOR, MVT::i1,
606 getNode(ISD::XOR, MVT::i1, N1, N2),
607 getConstant(1, MVT::i1));
608 break;
609 case ISD::SETNE: // X != Y --> (X^Y)
610 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
611 break;
612 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
613 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
614 N1 = getNode(ISD::AND, MVT::i1, N2,
615 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
616 break;
617 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
618 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
619 N1 = getNode(ISD::AND, MVT::i1, N1,
620 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
621 break;
622 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
623 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
624 N1 = getNode(ISD::OR, MVT::i1, N2,
625 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
626 break;
627 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
628 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
629 N1 = getNode(ISD::OR, MVT::i1, N1,
630 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
631 break;
632 }
633 if (VT != MVT::i1)
634 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
635 return N1;
636 }
637
638
Chris Lattner4a9b4f12005-01-18 19:26:36 +0000639 SetCCSDNode *&N = SetCCs[std::make_pair(std::make_pair(N1, N2),
640 std::make_pair(Cond, VT))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000641 if (N) return SDOperand(N, 0);
642 N = new SetCCSDNode(Cond, N1, N2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000643 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000644 AllNodes.push_back(N);
645 return SDOperand(N, 0);
646}
647
648
649
650/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000651///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000652SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
653 SDNode *N = new SDNode(Opcode, VT);
654 AllNodes.push_back(N);
655 return SDOperand(N, 0);
656}
657
Chris Lattnerc3aae252005-01-07 07:46:32 +0000658SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
659 SDOperand Operand) {
660 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
661 uint64_t Val = C->getValue();
662 switch (Opcode) {
663 default: break;
664 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
665 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
666 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000667 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
668 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000669 }
670 }
671
672 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
673 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000674 case ISD::FNEG:
675 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000676 case ISD::FP_ROUND:
677 case ISD::FP_EXTEND:
678 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000679 case ISD::FP_TO_SINT:
680 return getConstant((int64_t)C->getValue(), VT);
681 case ISD::FP_TO_UINT:
682 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000683 }
684
685 unsigned OpOpcode = Operand.Val->getOpcode();
686 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000687 case ISD::TokenFactor:
688 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689 case ISD::SIGN_EXTEND:
690 if (Operand.getValueType() == VT) return Operand; // noop extension
691 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
692 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
693 break;
694 case ISD::ZERO_EXTEND:
695 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000696 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000697 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000698 break;
699 case ISD::TRUNCATE:
700 if (Operand.getValueType() == VT) return Operand; // noop truncate
701 if (OpOpcode == ISD::TRUNCATE)
702 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000703 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
704 // If the source is smaller than the dest, we still need an extend.
705 if (Operand.Val->getOperand(0).getValueType() < VT)
706 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
707 else if (Operand.Val->getOperand(0).getValueType() > VT)
708 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
709 else
710 return Operand.Val->getOperand(0);
711 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000712 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000713 case ISD::FNEG:
714 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
715 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
716 Operand.Val->getOperand(0));
717 if (OpOpcode == ISD::FNEG) // --X -> X
718 return Operand.Val->getOperand(0);
719 break;
720 case ISD::FABS:
721 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
722 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
723 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000724 }
725
726 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
727 if (N) return SDOperand(N, 0);
728 N = new SDNode(Opcode, Operand);
729 N->setValueTypes(VT);
730 AllNodes.push_back(N);
731 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000732}
733
Chris Lattner36019aa2005-04-18 03:48:41 +0000734/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
735/// this predicate to simplify operations downstream. V and Mask are known to
736/// be the same type.
737static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
738 const TargetLowering &TLI) {
739 unsigned SrcBits;
740 if (Mask == 0) return true;
741
742 // If we know the result of a setcc has the top bits zero, use this info.
743 switch (Op.getOpcode()) {
744 case ISD::UNDEF:
745 return true;
746 case ISD::Constant:
747 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
748
749 case ISD::SETCC:
750 return ((Mask & 1) == 0) &&
751 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
752
753 case ISD::ZEXTLOAD:
754 SrcBits = MVT::getSizeInBits(cast<MVTSDNode>(Op)->getExtraValueType());
755 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
756 case ISD::ZERO_EXTEND:
757 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
758 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
759
760 case ISD::AND:
761 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
762 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(0)))
763 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
764
765 // FALL THROUGH
766 case ISD::OR:
767 case ISD::XOR:
768 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
769 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
770 case ISD::SELECT:
771 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
772 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
773
774 // TODO: (shl X, C1) & C2 == 0 iff (-1 << C1) & C2 == 0
775 // TODO: (ushr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
776 default: break;
777 }
778
779 return false;
780}
781
782
783
Chris Lattnerc3aae252005-01-07 07:46:32 +0000784SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
785 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000786#ifndef NDEBUG
787 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000788 case ISD::TokenFactor:
789 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
790 N2.getValueType() == MVT::Other && "Invalid token factor!");
791 break;
Chris Lattner76365122005-01-16 02:23:22 +0000792 case ISD::AND:
793 case ISD::OR:
794 case ISD::XOR:
795 case ISD::UDIV:
796 case ISD::UREM:
797 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
798 // fall through
799 case ISD::ADD:
800 case ISD::SUB:
801 case ISD::MUL:
802 case ISD::SDIV:
803 case ISD::SREM:
804 assert(N1.getValueType() == N2.getValueType() &&
805 N1.getValueType() == VT && "Binary operator types must match!");
806 break;
807
808 case ISD::SHL:
809 case ISD::SRA:
810 case ISD::SRL:
811 assert(VT == N1.getValueType() &&
812 "Shift operators return type must be the same as their first arg");
813 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000814 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000815 break;
816 default: break;
817 }
818#endif
819
Chris Lattnerc3aae252005-01-07 07:46:32 +0000820 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
821 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
822 if (N1C) {
823 if (N2C) {
824 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
825 switch (Opcode) {
826 case ISD::ADD: return getConstant(C1 + C2, VT);
827 case ISD::SUB: return getConstant(C1 - C2, VT);
828 case ISD::MUL: return getConstant(C1 * C2, VT);
829 case ISD::UDIV:
830 if (C2) return getConstant(C1 / C2, VT);
831 break;
832 case ISD::UREM :
833 if (C2) return getConstant(C1 % C2, VT);
834 break;
835 case ISD::SDIV :
836 if (C2) return getConstant(N1C->getSignExtended() /
837 N2C->getSignExtended(), VT);
838 break;
839 case ISD::SREM :
840 if (C2) return getConstant(N1C->getSignExtended() %
841 N2C->getSignExtended(), VT);
842 break;
843 case ISD::AND : return getConstant(C1 & C2, VT);
844 case ISD::OR : return getConstant(C1 | C2, VT);
845 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000846 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
847 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
848 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000849 default: break;
850 }
851
852 } else { // Cannonicalize constant to RHS if commutative
853 if (isCommutativeBinOp(Opcode)) {
854 std::swap(N1C, N2C);
855 std::swap(N1, N2);
856 }
857 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000858
859 switch (Opcode) {
860 default: break;
861 case ISD::SHL: // shl 0, X -> 0
862 if (N1C->isNullValue()) return N1;
863 break;
864 case ISD::SRL: // srl 0, X -> 0
865 if (N1C->isNullValue()) return N1;
866 break;
867 case ISD::SRA: // sra -1, X -> -1
868 if (N1C->isAllOnesValue()) return N1;
869 break;
870 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000871 }
872
873 if (N2C) {
874 uint64_t C2 = N2C->getValue();
875
876 switch (Opcode) {
877 case ISD::ADD:
878 if (!C2) return N1; // add X, 0 -> X
879 break;
880 case ISD::SUB:
881 if (!C2) return N1; // sub X, 0 -> X
882 break;
883 case ISD::MUL:
884 if (!C2) return N2; // mul X, 0 -> 0
885 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
886 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
887
Chris Lattnerb48da392005-01-23 04:39:44 +0000888 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000889 if ((C2 & C2-1) == 0) {
Chris Lattnerb48da392005-01-23 04:39:44 +0000890 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
891 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892 }
893 break;
894
895 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +0000896 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000897 if ((C2 & C2-1) == 0 && C2) {
Chris Lattnerb48da392005-01-23 04:39:44 +0000898 SDOperand ShAmt = getConstant(ExactLog2(C2), TLI.getShiftAmountTy());
899 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000900 }
901 break;
902
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000903 case ISD::SHL:
904 case ISD::SRL:
905 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +0000906 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +0000907 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +0000908 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
909 return getNode(ISD::UNDEF, N1.getValueType());
910 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000911 if (C2 == 0) return N1;
912 break;
913
Chris Lattnerc3aae252005-01-07 07:46:32 +0000914 case ISD::AND:
915 if (!C2) return N2; // X and 0 -> 0
916 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +0000917 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +0000918
Chris Lattner36019aa2005-04-18 03:48:41 +0000919 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
920 return getConstant(0, VT);
921
922 if (MaskedValueIsZero(N1, ~C2, TLI))
923 return N1; // if (X & ~C2) -> 0, the and is redundant
924
Chris Lattnerdea29e22005-04-10 01:13:15 +0000925 // FIXME: Should add a corresponding version of this for
926 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
927 // we don't have yet.
928
Chris Lattner0f2287b2005-04-13 02:38:18 +0000929 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
930 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +0000931 // If we are masking out the part of our input that was extended, just
932 // mask the input to the extension directly.
933 unsigned ExtendBits =
934 MVT::getSizeInBits(cast<MVTSDNode>(N1)->getExtraValueType());
935 if ((C2 & (~0ULL << ExtendBits)) == 0)
936 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
937 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000938 break;
939 case ISD::OR:
940 if (!C2)return N1; // X or 0 -> X
941 if (N2C->isAllOnesValue())
942 return N2; // X or -1 -> -1
943 break;
944 case ISD::XOR:
945 if (!C2) return N1; // X xor 0 -> X
946 if (N2C->isAllOnesValue()) {
947 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1.Val)){
948 // !(X op Y) -> (X !op Y)
949 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
950 return getSetCC(ISD::getSetCCInverse(SetCC->getCondition(),isInteger),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000951 SetCC->getValueType(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000952 SetCC->getOperand(0), SetCC->getOperand(1));
953 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
954 SDNode *Op = N1.Val;
955 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
956 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
957 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
958 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
959 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
960 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
961 if (Op->getOpcode() == ISD::AND)
962 return getNode(ISD::OR, VT, LHS, RHS);
963 return getNode(ISD::AND, VT, LHS, RHS);
964 }
965 }
966 // X xor -1 -> not(x) ?
967 }
968 break;
969 }
970
971 // Reassociate ((X op C1) op C2) if possible.
972 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
973 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +0000974 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +0000975 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
976 }
977
978 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
979 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
980 if (N1CFP)
981 if (N2CFP) {
982 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
983 switch (Opcode) {
984 case ISD::ADD: return getConstantFP(C1 + C2, VT);
985 case ISD::SUB: return getConstantFP(C1 - C2, VT);
986 case ISD::MUL: return getConstantFP(C1 * C2, VT);
987 case ISD::SDIV:
988 if (C2) return getConstantFP(C1 / C2, VT);
989 break;
990 case ISD::SREM :
991 if (C2) return getConstantFP(fmod(C1, C2), VT);
992 break;
993 default: break;
994 }
995
996 } else { // Cannonicalize constant to RHS if commutative
997 if (isCommutativeBinOp(Opcode)) {
998 std::swap(N1CFP, N2CFP);
999 std::swap(N1, N2);
1000 }
1001 }
1002
1003 // Finally, fold operations that do not require constants.
1004 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001005 case ISD::TokenFactor:
1006 if (N1.getOpcode() == ISD::EntryToken)
1007 return N2;
1008 if (N2.getOpcode() == ISD::EntryToken)
1009 return N1;
1010 break;
1011
Chris Lattnerc3aae252005-01-07 07:46:32 +00001012 case ISD::AND:
1013 case ISD::OR:
1014 if (SetCCSDNode *LHS = dyn_cast<SetCCSDNode>(N1.Val))
1015 if (SetCCSDNode *RHS = dyn_cast<SetCCSDNode>(N2.Val)) {
1016 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1017 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1018 ISD::CondCode Op2 = RHS->getCondition();
1019
Chris Lattner706aa962005-04-18 03:59:53 +00001020 // (X != 0) | (Y != 0) -> (X|Y != 0)
1021 // (X == 0) & (Y == 0) -> (X|Y == 0)
1022 if (LR == RR && isa<ConstantSDNode>(LR) &&
1023 cast<ConstantSDNode>(LR)->getValue() == 0 &&
1024 Op2 == LHS->getCondition() && MVT::isInteger(LL.getValueType())) {
1025 if ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1026 (Op2 == ISD::SETNE && Opcode == ISD::OR))
1027 return getSetCC(Op2, VT,
1028 getNode(ISD::OR, LR.getValueType(), LL, RL), LR);
1029 }
1030
Chris Lattnerc3aae252005-01-07 07:46:32 +00001031 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1032 if (LL == RR && LR == RL) {
1033 Op2 = ISD::getSetCCSwappedOperands(Op2);
1034 goto MatchedBackwards;
1035 }
1036
1037 if (LL == RL && LR == RR) {
1038 MatchedBackwards:
1039 ISD::CondCode Result;
1040 bool isInteger = MVT::isInteger(LL.getValueType());
1041 if (Opcode == ISD::OR)
1042 Result = ISD::getSetCCOrOperation(LHS->getCondition(), Op2,
1043 isInteger);
1044 else
1045 Result = ISD::getSetCCAndOperation(LHS->getCondition(), Op2,
1046 isInteger);
1047 if (Result != ISD::SETCC_INVALID)
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001048 return getSetCC(Result, LHS->getValueType(0), LL, LR);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001049 }
1050 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001051
1052 // and/or zext(a), zext(b) -> zext(and/or a, b)
1053 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1054 N2.getOpcode() == ISD::ZERO_EXTEND &&
1055 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1056 return getNode(ISD::ZERO_EXTEND, VT,
1057 getNode(Opcode, N1.getOperand(0).getValueType(),
1058 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001059 break;
1060 case ISD::XOR:
1061 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1062 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001063 case ISD::ADD:
1064 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1065 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1066 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1067 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001068 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1069 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1070 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1071 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1072 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1073 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Chris Lattner485df9b2005-04-09 03:02:46 +00001074 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001075 case ISD::SUB:
1076 if (N1.getOpcode() == ISD::ADD) {
1077 if (N1.Val->getOperand(0) == N2)
1078 return N1.Val->getOperand(1); // (A+B)-A == B
1079 if (N1.Val->getOperand(1) == N2)
1080 return N1.Val->getOperand(0); // (A+B)-B == A
1081 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001082 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1083 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001084 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001085 // FIXME: figure out how to safely handle things like
1086 // int foo(int x) { return 1 << (x & 255); }
1087 // int bar() { return foo(256); }
1088#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001089 case ISD::SHL:
1090 case ISD::SRL:
1091 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001092 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1093 cast<MVTSDNode>(N2)->getExtraValueType() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001094 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001095 else if (N2.getOpcode() == ISD::AND)
1096 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1097 // If the and is only masking out bits that cannot effect the shift,
1098 // eliminate the and.
1099 unsigned NumBits = MVT::getSizeInBits(VT);
1100 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1101 return getNode(Opcode, VT, N1, N2.getOperand(0));
1102 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001103 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001104#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001105 }
1106
1107 SDNode *&N = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1108 if (N) return SDOperand(N, 0);
1109 N = new SDNode(Opcode, N1, N2);
1110 N->setValueTypes(VT);
1111
1112 AllNodes.push_back(N);
1113 return SDOperand(N, 0);
1114}
1115
1116SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1117 SDOperand Chain, SDOperand Ptr) {
1118 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1119 if (N) return SDOperand(N, 0);
1120 N = new SDNode(ISD::LOAD, Chain, Ptr);
1121
1122 // Loads have a token chain.
1123 N->setValueTypes(VT, MVT::Other);
1124 AllNodes.push_back(N);
1125 return SDOperand(N, 0);
1126}
1127
1128
1129SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1130 SDOperand N1, SDOperand N2, SDOperand N3) {
1131 // Perform various simplifications.
1132 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1133 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1134 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1135 switch (Opcode) {
1136 case ISD::SELECT:
1137 if (N1C)
1138 if (N1C->getValue())
1139 return N2; // select true, X, Y -> X
1140 else
1141 return N3; // select false, X, Y -> Y
1142
1143 if (N2 == N3) return N2; // select C, X, X -> X
1144
1145 if (VT == MVT::i1) { // Boolean SELECT
1146 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001147 if (N2C->getValue()) // select C, 1, X -> C | X
1148 return getNode(ISD::OR, VT, N1, N3);
1149 else // select C, 0, X -> ~C & X
1150 return getNode(ISD::AND, VT,
1151 getNode(ISD::XOR, N1.getValueType(), N1,
1152 getConstant(1, N1.getValueType())), N3);
1153 } else if (N3C) {
1154 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1155 return getNode(ISD::OR, VT,
1156 getNode(ISD::XOR, N1.getValueType(), N1,
1157 getConstant(1, N1.getValueType())), N2);
1158 else // select C, X, 0 -> C & X
1159 return getNode(ISD::AND, VT, N1, N2);
1160 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001161
1162 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1163 return getNode(ISD::OR, VT, N1, N3);
1164 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1165 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001166 }
1167
Chris Lattner59723e92005-04-09 05:15:53 +00001168 // If this is a selectcc, check to see if we can simplify the result.
1169 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(N1)) {
1170 if (ConstantFPSDNode *CFP =
1171 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1172 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1173 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
1174 if ((SetCC->getCondition() == ISD::SETGE ||
1175 SetCC->getCondition() == ISD::SETGT) &&
1176 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1177 N3.getOperand(0) == N2)
1178 return getNode(ISD::FABS, VT, N2);
1179
1180 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
1181 if ((SetCC->getCondition() == ISD::SETLT ||
1182 SetCC->getCondition() == ISD::SETLE) &&
1183 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1184 N2.getOperand(0) == N3)
1185 return getNode(ISD::FABS, VT, N3);
1186 }
Nate Begemaneea805e2005-04-13 21:23:31 +00001187 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1, A)
1188 if (ConstantSDNode *CN =
1189 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1190 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
1191 if (SetCC->getCondition() == ISD::SETLT) {
1192 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1193 MVT::ValueType AType = N2.getValueType();
1194 if (XType >= AType) {
1195 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1196 getConstant(MVT::getSizeInBits(XType)-1,
1197 TLI.getShiftAmountTy()));
1198 if (XType > AType)
1199 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1200 return getNode(ISD::AND, AType, Shift, N2);
1201 }
1202 }
Chris Lattner59723e92005-04-09 05:15:53 +00001203 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001204 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001205 case ISD::BRCOND:
1206 if (N2C)
1207 if (N2C->getValue()) // Unconditional branch
1208 return getNode(ISD::BR, MVT::Other, N1, N3);
1209 else
1210 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001211 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001212 // FIXME: figure out how to safely handle things like
1213 // int foo(int x) { return 1 << (x & 255); }
1214 // int bar() { return foo(256); }
1215#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001216 case ISD::SRA_PARTS:
1217 case ISD::SRL_PARTS:
1218 case ISD::SHL_PARTS:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001219 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1220 cast<MVTSDNode>(N3)->getExtraValueType() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001221 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001222 else if (N3.getOpcode() == ISD::AND)
1223 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1224 // If the and is only masking out bits that cannot effect the shift,
1225 // eliminate the and.
1226 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1227 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1228 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1229 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001230 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001231#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001232 }
1233
1234 SDNode *N = new SDNode(Opcode, N1, N2, N3);
1235 switch (Opcode) {
1236 default:
1237 N->setValueTypes(VT);
1238 break;
1239 case ISD::DYNAMIC_STACKALLOC: // DYNAMIC_STACKALLOC produces pointer and chain
1240 N->setValueTypes(VT, MVT::Other);
1241 break;
Chris Lattner5b359c62005-04-02 04:00:59 +00001242
1243 case ISD::SRA_PARTS:
1244 case ISD::SRL_PARTS:
1245 case ISD::SHL_PARTS: {
1246 std::vector<MVT::ValueType> V(N->getNumOperands()-1, VT);
1247 N->setValueTypes(V);
1248 break;
1249 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001250 }
1251
1252 // FIXME: memoize NODES
1253 AllNodes.push_back(N);
1254 return SDOperand(N, 0);
1255}
1256
1257SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1258 std::vector<SDOperand> &Children) {
1259 switch (Children.size()) {
1260 case 0: return getNode(Opcode, VT);
1261 case 1: return getNode(Opcode, VT, Children[0]);
1262 case 2: return getNode(Opcode, VT, Children[0], Children[1]);
1263 case 3: return getNode(Opcode, VT, Children[0], Children[1], Children[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001264 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001265 }
Chris Lattneref847df2005-04-09 03:27:28 +00001266
1267 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Children[1].Val);
1268 switch (Opcode) {
1269 default: break;
1270 case ISD::BRCONDTWOWAY:
1271 if (N1C)
1272 if (N1C->getValue()) // Unconditional branch to true dest.
1273 return getNode(ISD::BR, MVT::Other, Children[0], Children[2]);
1274 else // Unconditional branch to false dest.
1275 return getNode(ISD::BR, MVT::Other, Children[0], Children[3]);
1276 break;
1277 }
1278
1279 // FIXME: MEMOIZE!!
1280 SDNode *N = new SDNode(Opcode, Children);
1281 if (Opcode != ISD::ADD_PARTS && Opcode != ISD::SUB_PARTS) {
1282 N->setValueTypes(VT);
1283 } else {
1284 std::vector<MVT::ValueType> V(N->getNumOperands()/2, VT);
1285 N->setValueTypes(V);
1286 }
1287 AllNodes.push_back(N);
1288 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001289}
1290
Chris Lattner2ee743f2005-01-14 22:08:15 +00001291SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner859157d2005-01-15 06:17:04 +00001292 MVT::ValueType EVT) {
1293
1294 switch (Opcode) {
1295 default: assert(0 && "Bad opcode for this accessor!");
1296 case ISD::FP_ROUND_INREG:
1297 assert(VT == N1.getValueType() && "Not an inreg round!");
1298 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1299 "Cannot FP_ROUND_INREG integer types");
1300 if (EVT == VT) return N1; // Not actually rounding
1301 assert(EVT < VT && "Not rounding down!");
Chris Lattner14723c22005-03-09 18:37:12 +00001302
1303 if (isa<ConstantFPSDNode>(N1))
1304 return getNode(ISD::FP_EXTEND, VT, getNode(ISD::FP_ROUND, EVT, N1));
Chris Lattner859157d2005-01-15 06:17:04 +00001305 break;
Chris Lattner859157d2005-01-15 06:17:04 +00001306 case ISD::SIGN_EXTEND_INREG:
1307 assert(VT == N1.getValueType() && "Not an inreg extend!");
1308 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1309 "Cannot *_EXTEND_INREG FP types");
1310 if (EVT == VT) return N1; // Not actually extending
1311 assert(EVT < VT && "Not extending!");
Chris Lattner950aa3c2005-01-16 00:17:20 +00001312
Chris Lattner0f2287b2005-04-13 02:38:18 +00001313 // Extending a constant? Just return the extended constant.
Chris Lattner14723c22005-03-09 18:37:12 +00001314 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
1315 SDOperand Tmp = getNode(ISD::TRUNCATE, EVT, N1);
Chris Lattner0f2287b2005-04-13 02:38:18 +00001316 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner14723c22005-03-09 18:37:12 +00001317 }
1318
Chris Lattner950aa3c2005-01-16 00:17:20 +00001319 // If we are sign extending an extension, use the original source.
Chris Lattner0f2287b2005-04-13 02:38:18 +00001320 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1321 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
Chris Lattner950aa3c2005-01-16 00:17:20 +00001322 return N1;
Chris Lattner950aa3c2005-01-16 00:17:20 +00001323
Chris Lattner0f2287b2005-04-13 02:38:18 +00001324 // If we are sign extending a sextload, return just the load.
1325 if (N1.getOpcode() == ISD::SEXTLOAD && Opcode == ISD::SIGN_EXTEND_INREG)
Chris Lattner45278e32005-04-10 04:33:08 +00001326 if (cast<MVTSDNode>(N1)->getExtraValueType() <= EVT)
1327 return N1;
1328
Chris Lattner5a6bace2005-04-07 19:43:53 +00001329 // If we are extending the result of a setcc, and we already know the
1330 // contents of the top bits, eliminate the extension.
Chris Lattner0f2287b2005-04-13 02:38:18 +00001331 if (N1.getOpcode() == ISD::SETCC &&
1332 TLI.getSetCCResultContents() ==
1333 TargetLowering::ZeroOrNegativeOneSetCCResult)
1334 return N1;
Chris Lattner2bb6f412005-04-10 23:37:16 +00001335
1336 // If we are sign extending the result of an (and X, C) operation, and we
1337 // know the extended bits are zeros already, don't do the extend.
1338 if (N1.getOpcode() == ISD::AND)
1339 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1340 uint64_t Mask = N1C->getValue();
1341 unsigned NumBits = MVT::getSizeInBits(EVT);
Chris Lattner0f2287b2005-04-13 02:38:18 +00001342 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1343 return N1;
Chris Lattner2bb6f412005-04-10 23:37:16 +00001344 }
Chris Lattner859157d2005-01-15 06:17:04 +00001345 break;
1346 }
1347
1348 EVTStruct NN;
1349 NN.Opcode = Opcode;
1350 NN.VT = VT;
1351 NN.EVT = EVT;
1352 NN.Ops.push_back(N1);
1353
1354 SDNode *&N = MVTSDNodes[NN];
1355 if (N) return SDOperand(N, 0);
1356 N = new MVTSDNode(Opcode, VT, N1, EVT);
1357 AllNodes.push_back(N);
1358 return SDOperand(N, 0);
1359}
1360
1361SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
Chris Lattner2ee743f2005-01-14 22:08:15 +00001362 SDOperand N2, MVT::ValueType EVT) {
1363 switch (Opcode) {
1364 default: assert(0 && "Bad opcode for this accessor!");
1365 case ISD::EXTLOAD:
1366 case ISD::SEXTLOAD:
1367 case ISD::ZEXTLOAD:
Chris Lattner0f2287b2005-04-13 02:38:18 +00001368 // If they are asking for an extending load from/to the same thing, return a
Chris Lattner2ee743f2005-01-14 22:08:15 +00001369 // normal load.
1370 if (VT == EVT)
1371 return getNode(ISD::LOAD, VT, N1, N2);
1372 assert(EVT < VT && "Should only be an extending load, not truncating!");
1373 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(VT)) &&
1374 "Cannot sign/zero extend a FP load!");
1375 assert(MVT::isInteger(VT) == MVT::isInteger(EVT) &&
1376 "Cannot convert from FP to Int or Int -> FP!");
1377 break;
1378 }
1379
1380 EVTStruct NN;
1381 NN.Opcode = Opcode;
1382 NN.VT = VT;
1383 NN.EVT = EVT;
1384 NN.Ops.push_back(N1);
1385 NN.Ops.push_back(N2);
1386
1387 SDNode *&N = MVTSDNodes[NN];
1388 if (N) return SDOperand(N, 0);
Chris Lattner69a52152005-01-14 22:38:01 +00001389 N = new MVTSDNode(Opcode, VT, MVT::Other, N1, N2, EVT);
Chris Lattner2ee743f2005-01-14 22:08:15 +00001390 AllNodes.push_back(N);
1391 return SDOperand(N, 0);
1392}
1393
1394SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,SDOperand N1,
1395 SDOperand N2, SDOperand N3, MVT::ValueType EVT) {
1396 switch (Opcode) {
1397 default: assert(0 && "Bad opcode for this accessor!");
1398 case ISD::TRUNCSTORE:
Chris Lattner859157d2005-01-15 06:17:04 +00001399#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1400 // If this is a truncating store of a constant, convert to the desired type
1401 // and store it instead.
1402 if (isa<Constant>(N1)) {
1403 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1404 if (isa<Constant>(Op))
1405 N1 = Op;
1406 }
1407 // Also for ConstantFP?
1408#endif
Chris Lattner2ee743f2005-01-14 22:08:15 +00001409 if (N1.getValueType() == EVT) // Normal store?
1410 return getNode(ISD::STORE, VT, N1, N2, N3);
Chris Lattner859157d2005-01-15 06:17:04 +00001411 assert(N2.getValueType() > EVT && "Not a truncation?");
1412 assert(MVT::isInteger(N2.getValueType()) == MVT::isInteger(EVT) &&
Chris Lattner2ee743f2005-01-14 22:08:15 +00001413 "Can't do FP-INT conversion!");
1414 break;
1415 }
1416
1417 EVTStruct NN;
1418 NN.Opcode = Opcode;
1419 NN.VT = VT;
1420 NN.EVT = EVT;
1421 NN.Ops.push_back(N1);
1422 NN.Ops.push_back(N2);
1423 NN.Ops.push_back(N3);
1424
1425 SDNode *&N = MVTSDNodes[NN];
1426 if (N) return SDOperand(N, 0);
1427 N = new MVTSDNode(Opcode, VT, N1, N2, N3, EVT);
1428 AllNodes.push_back(N);
1429 return SDOperand(N, 0);
1430}
1431
1432
Chris Lattner5c884562005-01-12 18:37:47 +00001433/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1434/// indicated value. This method ignores uses of other values defined by this
1435/// operation.
1436bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1437 assert(Value < getNumValues() && "Bad value!");
1438
1439 // If there is only one value, this is easy.
1440 if (getNumValues() == 1)
1441 return use_size() == NUses;
1442 if (Uses.size() < NUses) return false;
1443
1444 SDOperand TheValue(this, Value);
1445
1446 std::set<SDNode*> UsersHandled;
1447
1448 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1449 UI != E; ++UI) {
1450 SDNode *User = *UI;
1451 if (User->getNumOperands() == 1 ||
1452 UsersHandled.insert(User).second) // First time we've seen this?
1453 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1454 if (User->getOperand(i) == TheValue) {
1455 if (NUses == 0)
1456 return false; // too many uses
1457 --NUses;
1458 }
1459 }
1460
1461 // Found exactly the right number of uses?
1462 return NUses == 0;
1463}
1464
1465
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001466const char *SDNode::getOperationName() const {
1467 switch (getOpcode()) {
1468 default: return "<<Unknown>>";
Andrew Lenharth95762122005-03-31 21:24:06 +00001469 case ISD::PCMARKER: return "PCMarker";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001470 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001471 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001472 case ISD::Constant: return "Constant";
1473 case ISD::ConstantFP: return "ConstantFP";
1474 case ISD::GlobalAddress: return "GlobalAddress";
1475 case ISD::FrameIndex: return "FrameIndex";
1476 case ISD::BasicBlock: return "BasicBlock";
1477 case ISD::ExternalSymbol: return "ExternalSymbol";
1478 case ISD::ConstantPool: return "ConstantPoolIndex";
1479 case ISD::CopyToReg: return "CopyToReg";
1480 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001481 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001482 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001483
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001484 // Unary operators
1485 case ISD::FABS: return "fabs";
1486 case ISD::FNEG: return "fneg";
1487
1488 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001489 case ISD::ADD: return "add";
1490 case ISD::SUB: return "sub";
1491 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001492 case ISD::MULHU: return "mulhu";
1493 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001494 case ISD::SDIV: return "sdiv";
1495 case ISD::UDIV: return "udiv";
1496 case ISD::SREM: return "srem";
1497 case ISD::UREM: return "urem";
1498 case ISD::AND: return "and";
1499 case ISD::OR: return "or";
1500 case ISD::XOR: return "xor";
1501 case ISD::SHL: return "shl";
1502 case ISD::SRA: return "sra";
1503 case ISD::SRL: return "srl";
1504
1505 case ISD::SELECT: return "select";
Chris Lattner17eee182005-01-20 18:50:55 +00001506 case ISD::ADD_PARTS: return "add_parts";
1507 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001508 case ISD::SHL_PARTS: return "shl_parts";
1509 case ISD::SRA_PARTS: return "sra_parts";
1510 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001511
1512 // Conversion operators.
1513 case ISD::SIGN_EXTEND: return "sign_extend";
1514 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001515 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001516 case ISD::TRUNCATE: return "truncate";
1517 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001518 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001519 case ISD::FP_EXTEND: return "fp_extend";
1520
1521 case ISD::SINT_TO_FP: return "sint_to_fp";
1522 case ISD::UINT_TO_FP: return "uint_to_fp";
1523 case ISD::FP_TO_SINT: return "fp_to_sint";
1524 case ISD::FP_TO_UINT: return "fp_to_uint";
1525
1526 // Control flow instructions
1527 case ISD::BR: return "br";
1528 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001529 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001530 case ISD::RET: return "ret";
1531 case ISD::CALL: return "call";
1532 case ISD::ADJCALLSTACKDOWN: return "adjcallstackdown";
1533 case ISD::ADJCALLSTACKUP: return "adjcallstackup";
1534
1535 // Other operators
1536 case ISD::LOAD: return "load";
1537 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001538 case ISD::EXTLOAD: return "extload";
1539 case ISD::SEXTLOAD: return "sextload";
1540 case ISD::ZEXTLOAD: return "zextload";
1541 case ISD::TRUNCSTORE: return "truncstore";
1542
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001543 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1544 case ISD::EXTRACT_ELEMENT: return "extract_element";
1545 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001546 case ISD::MEMSET: return "memset";
1547 case ISD::MEMCPY: return "memcpy";
1548 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001549
1550 case ISD::SETCC:
1551 const SetCCSDNode *SetCC = cast<SetCCSDNode>(this);
1552 switch (SetCC->getCondition()) {
1553 default: assert(0 && "Unknown setcc condition!");
1554 case ISD::SETOEQ: return "setcc:setoeq";
1555 case ISD::SETOGT: return "setcc:setogt";
1556 case ISD::SETOGE: return "setcc:setoge";
1557 case ISD::SETOLT: return "setcc:setolt";
1558 case ISD::SETOLE: return "setcc:setole";
1559 case ISD::SETONE: return "setcc:setone";
1560
1561 case ISD::SETO: return "setcc:seto";
1562 case ISD::SETUO: return "setcc:setuo";
1563 case ISD::SETUEQ: return "setcc:setue";
1564 case ISD::SETUGT: return "setcc:setugt";
1565 case ISD::SETUGE: return "setcc:setuge";
1566 case ISD::SETULT: return "setcc:setult";
1567 case ISD::SETULE: return "setcc:setule";
1568 case ISD::SETUNE: return "setcc:setune";
1569
1570 case ISD::SETEQ: return "setcc:seteq";
1571 case ISD::SETGT: return "setcc:setgt";
1572 case ISD::SETGE: return "setcc:setge";
1573 case ISD::SETLT: return "setcc:setlt";
1574 case ISD::SETLE: return "setcc:setle";
1575 case ISD::SETNE: return "setcc:setne";
1576 }
1577 }
1578}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001579
1580void SDNode::dump() const {
1581 std::cerr << (void*)this << ": ";
1582
1583 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1584 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001585 if (getValueType(i) == MVT::Other)
1586 std::cerr << "ch";
1587 else
1588 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001589 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001590 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001591
1592 std::cerr << " ";
1593 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1594 if (i) std::cerr << ", ";
1595 std::cerr << (void*)getOperand(i).Val;
1596 if (unsigned RN = getOperand(i).ResNo)
1597 std::cerr << ":" << RN;
1598 }
1599
1600 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1601 std::cerr << "<" << CSDN->getValue() << ">";
1602 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1603 std::cerr << "<" << CSDN->getValue() << ">";
1604 } else if (const GlobalAddressSDNode *GADN =
1605 dyn_cast<GlobalAddressSDNode>(this)) {
1606 std::cerr << "<";
1607 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
1608 } else if (const FrameIndexSDNode *FIDN =
1609 dyn_cast<FrameIndexSDNode>(this)) {
1610 std::cerr << "<" << FIDN->getIndex() << ">";
1611 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1612 std::cerr << "<" << CP->getIndex() << ">";
1613 } else if (const BasicBlockSDNode *BBDN =
1614 dyn_cast<BasicBlockSDNode>(this)) {
1615 std::cerr << "<";
1616 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1617 if (LBB)
1618 std::cerr << LBB->getName() << " ";
1619 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001620 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001621 std::cerr << "<reg #" << C2V->getReg() << ">";
1622 } else if (const ExternalSymbolSDNode *ES =
1623 dyn_cast<ExternalSymbolSDNode>(this)) {
1624 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner8a389bb2005-01-15 21:11:37 +00001625 } else if (const MVTSDNode *M = dyn_cast<MVTSDNode>(this)) {
1626 std::cerr << " - Ty = " << MVT::getValueTypeString(M->getExtraValueType());
Chris Lattnerc3aae252005-01-07 07:46:32 +00001627 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001628}
1629
Chris Lattnerea946cd2005-01-09 20:38:33 +00001630static void DumpNodes(SDNode *N, unsigned indent) {
1631 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1632 if (N->getOperand(i).Val->hasOneUse())
1633 DumpNodes(N->getOperand(i).Val, indent+2);
1634 else
1635 std::cerr << "\n" << std::string(indent+2, ' ')
1636 << (void*)N->getOperand(i).Val << ": <multiple use>";
1637
1638
1639 std::cerr << "\n" << std::string(indent, ' ');
1640 N->dump();
1641}
1642
Chris Lattnerc3aae252005-01-07 07:46:32 +00001643void SelectionDAG::dump() const {
1644 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001645 std::vector<SDNode*> Nodes(AllNodes);
1646 std::sort(Nodes.begin(), Nodes.end());
1647
1648 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001649 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1650 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001651 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001652
1653 DumpNodes(getRoot().Val, 2);
1654
Chris Lattnerc3aae252005-01-07 07:46:32 +00001655 std::cerr << "\n\n";
1656}
1657