blob: 549b48042f9fa50bdd07346a1cc53458cf2e2312 [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;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000053 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000054 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 Lattner7cf7e3f2005-08-09 20:20:18 +0000200 case ISD::CONDCODE:
201 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
202 "Cond code doesn't exist!");
203 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
204 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000205 case ISD::GlobalAddress:
206 GlobalValues.erase(cast<GlobalAddressSDNode>(N)->getGlobal());
207 break;
208 case ISD::FrameIndex:
209 FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
210 break;
211 case ISD::ConstantPool:
212 ConstantPoolIndices.erase(cast<ConstantPoolSDNode>(N)->getIndex());
213 break;
214 case ISD::BasicBlock:
215 BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
216 break;
217 case ISD::ExternalSymbol:
218 ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
219 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000220 case ISD::VALUETYPE:
221 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
222 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000223 case ISD::SRCVALUE: {
224 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
225 ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
226 break;
227 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000228 case ISD::LOAD:
229 Loads.erase(std::make_pair(N->getOperand(1),
230 std::make_pair(N->getOperand(0),
231 N->getValueType(0))));
232 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000233 default:
234 if (N->getNumOperands() == 1)
235 UnaryOps.erase(std::make_pair(N->getOpcode(),
236 std::make_pair(N->getOperand(0),
237 N->getValueType(0))));
238 else if (N->getNumOperands() == 2)
239 BinaryOps.erase(std::make_pair(N->getOpcode(),
240 std::make_pair(N->getOperand(0),
241 N->getOperand(1))));
Chris Lattner385328c2005-05-14 07:42:29 +0000242 else if (N->getNumValues() == 1) {
243 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
244 OneResultNodes.erase(std::make_pair(N->getOpcode(),
245 std::make_pair(N->getValueType(0),
246 Ops)));
247 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000248 // Remove the node from the ArbitraryNodes map.
249 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
250 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
251 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
252 std::make_pair(RV, Ops)));
253 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254 break;
255 }
256
257 // Next, brutally remove the operand list.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000258 while (!N->Operands.empty()) {
Chris Lattner7c68ec62005-01-07 23:32:00 +0000259 SDNode *O = N->Operands.back().Val;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000260 N->Operands.pop_back();
Chris Lattner7c68ec62005-01-07 23:32:00 +0000261 O->removeUser(N);
262
263 // Now that we removed this operand, see if there are no uses of it left.
264 DeleteNodeIfDead(O, NodeSet);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000265 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000267 // Remove the node from the nodes set and delete it.
268 std::set<SDNode*> &AllNodeSet = *(std::set<SDNode*>*)NodeSet;
269 AllNodeSet.erase(N);
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000270
271 // Now that the node is gone, check to see if any of the operands of this node
272 // are dead now.
Chris Lattner7c68ec62005-01-07 23:32:00 +0000273 delete N;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000274}
275
276
Chris Lattner78ec3112003-08-11 14:57:33 +0000277SelectionDAG::~SelectionDAG() {
278 for (unsigned i = 0, e = AllNodes.size(); i != e; ++i)
279 delete AllNodes[i];
280}
281
Chris Lattner0f2287b2005-04-13 02:38:18 +0000282SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000283 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000284 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000285 return getNode(ISD::AND, Op.getValueType(), Op,
286 getConstant(Imm, Op.getValueType()));
287}
288
Chris Lattnerc3aae252005-01-07 07:46:32 +0000289SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
290 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
291 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000292 if (VT != MVT::i64)
293 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000294
Chris Lattnerc3aae252005-01-07 07:46:32 +0000295 SDNode *&N = Constants[std::make_pair(Val, VT)];
296 if (N) return SDOperand(N, 0);
297 N = new ConstantSDNode(Val, VT);
298 AllNodes.push_back(N);
299 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000300}
301
Chris Lattnerc3aae252005-01-07 07:46:32 +0000302SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
303 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
304 if (VT == MVT::f32)
305 Val = (float)Val; // Mask out extra precision.
306
Chris Lattnerd8658612005-02-17 20:17:32 +0000307 // Do the map lookup using the actual bit pattern for the floating point
308 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
309 // we don't have issues with SNANs.
310 union {
311 double DV;
312 uint64_t IV;
313 };
Misha Brukmanedf128a2005-04-21 22:36:52 +0000314
Chris Lattnerd8658612005-02-17 20:17:32 +0000315 DV = Val;
316
317 SDNode *&N = ConstantFPs[std::make_pair(IV, VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000318 if (N) return SDOperand(N, 0);
319 N = new ConstantFPSDNode(Val, VT);
320 AllNodes.push_back(N);
321 return SDOperand(N, 0);
322}
323
324
325
326SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
327 MVT::ValueType VT) {
328 SDNode *&N = GlobalValues[GV];
329 if (N) return SDOperand(N, 0);
330 N = new GlobalAddressSDNode(GV,VT);
331 AllNodes.push_back(N);
332 return SDOperand(N, 0);
333}
334
335SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
336 SDNode *&N = FrameIndices[FI];
337 if (N) return SDOperand(N, 0);
338 N = new FrameIndexSDNode(FI, VT);
339 AllNodes.push_back(N);
340 return SDOperand(N, 0);
341}
342
343SDOperand SelectionDAG::getConstantPool(unsigned CPIdx, MVT::ValueType VT) {
344 SDNode *N = ConstantPoolIndices[CPIdx];
345 if (N) return SDOperand(N, 0);
346 N = new ConstantPoolSDNode(CPIdx, VT);
347 AllNodes.push_back(N);
348 return SDOperand(N, 0);
349}
350
351SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
352 SDNode *&N = BBNodes[MBB];
353 if (N) return SDOperand(N, 0);
354 N = new BasicBlockSDNode(MBB);
355 AllNodes.push_back(N);
356 return SDOperand(N, 0);
357}
358
Chris Lattner15e4b012005-07-10 00:07:11 +0000359SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
360 if ((unsigned)VT >= ValueTypeNodes.size())
361 ValueTypeNodes.resize(VT+1);
362 if (ValueTypeNodes[VT] == 0) {
363 ValueTypeNodes[VT] = new VTSDNode(VT);
364 AllNodes.push_back(ValueTypeNodes[VT]);
365 }
366
367 return SDOperand(ValueTypeNodes[VT], 0);
368}
369
Chris Lattnerc3aae252005-01-07 07:46:32 +0000370SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
371 SDNode *&N = ExternalSymbols[Sym];
372 if (N) return SDOperand(N, 0);
373 N = new ExternalSymbolSDNode(Sym, VT);
374 AllNodes.push_back(N);
375 return SDOperand(N, 0);
376}
377
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000378SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
379 if ((unsigned)Cond >= CondCodeNodes.size())
380 CondCodeNodes.resize(Cond+1);
381
Chris Lattner079a27a2005-08-09 20:40:02 +0000382 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000383 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000384 AllNodes.push_back(CondCodeNodes[Cond]);
385 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000386 return SDOperand(CondCodeNodes[Cond], 0);
387}
388
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000389SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
390 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000391 // These setcc operations always fold.
392 switch (Cond) {
393 default: break;
394 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000395 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000396 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000397 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000398 }
399
Chris Lattner67255a12005-04-07 18:14:58 +0000400 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
401 uint64_t C2 = N2C->getValue();
402 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
403 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000404
Chris Lattnerc3aae252005-01-07 07:46:32 +0000405 // Sign extend the operands if required
406 if (ISD::isSignedIntSetCC(Cond)) {
407 C1 = N1C->getSignExtended();
408 C2 = N2C->getSignExtended();
409 }
410
411 switch (Cond) {
412 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000413 case ISD::SETEQ: return getConstant(C1 == C2, VT);
414 case ISD::SETNE: return getConstant(C1 != C2, VT);
415 case ISD::SETULT: return getConstant(C1 < C2, VT);
416 case ISD::SETUGT: return getConstant(C1 > C2, VT);
417 case ISD::SETULE: return getConstant(C1 <= C2, VT);
418 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
419 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
420 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
421 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
422 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000423 }
Chris Lattner24673922005-04-07 18:58:54 +0000424 } else {
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000425 // If the LHS is a ZERO_EXTEND and if this is an ==/!= comparison, perform
426 // the comparison on the input.
427 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
428 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
429
430 // If the comparison constant has bits in the upper part, the
431 // zero-extended value could never match.
432 if (C2 & (~0ULL << InSize)) {
433 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
434 switch (Cond) {
435 case ISD::SETUGT:
436 case ISD::SETUGE:
437 case ISD::SETEQ: return getConstant(0, VT);
438 case ISD::SETULT:
439 case ISD::SETULE:
440 case ISD::SETNE: return getConstant(1, VT);
441 case ISD::SETGT:
442 case ISD::SETGE:
443 // True if the sign bit of C2 is set.
444 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
445 case ISD::SETLT:
446 case ISD::SETLE:
447 // True if the sign bit of C2 isn't set.
448 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
449 default:
450 break;
451 }
452 }
453
454 // Otherwise, we can perform the comparison with the low bits.
455 switch (Cond) {
456 case ISD::SETEQ:
457 case ISD::SETNE:
458 case ISD::SETUGT:
459 case ISD::SETUGE:
460 case ISD::SETULT:
461 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000462 return getSetCC(VT, N1.getOperand(0),
463 getConstant(C2, N1.getOperand(0).getValueType()),
464 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000465 default:
466 break; // todo, be more careful with signed comparisons
467 }
468 }
469
Chris Lattner67255a12005-04-07 18:14:58 +0000470 uint64_t MinVal, MaxVal;
471 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
472 if (ISD::isSignedIntSetCC(Cond)) {
473 MinVal = 1ULL << (OperandBitSize-1);
474 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
475 MaxVal = ~0ULL >> (65-OperandBitSize);
476 else
477 MaxVal = 0;
478 } else {
479 MinVal = 0;
480 MaxVal = ~0ULL >> (64-OperandBitSize);
481 }
482
483 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
484 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
485 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
486 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000487 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
488 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000489 }
490
491 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
492 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
493 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000494 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
495 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000496 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000497
Nate Begeman72ea2812005-04-14 08:56:52 +0000498 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
499 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000500
Nate Begeman72ea2812005-04-14 08:56:52 +0000501 // Canonicalize setgt X, Min --> setne X, Min
502 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000503 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000504
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000505 // If we have setult X, 1, turn it into seteq X, 0
506 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000507 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
508 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000509 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000510 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000511 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
512 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000513
514 // If we have "setcc X, C1", check to see if we can shrink the immediate
515 // by changing cc.
516
517 // SETUGT X, SINTMAX -> SETLT X, 0
518 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
519 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000520 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000521
522 // FIXME: Implement the rest of these.
523
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000524
525 // Fold bit comparisons when we can.
526 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
527 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
528 if (ConstantSDNode *AndRHS =
529 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
530 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
531 // Perform the xform if the AND RHS is a single bit.
532 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
533 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000534 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000535 TLI.getShiftAmountTy()));
536 }
537 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
538 // (X & 8) == 8 --> (X & 8) >> 3
539 // Perform the xform if C2 is a single bit.
540 if ((C2 & (C2-1)) == 0) {
541 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000542 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000543 }
544 }
545 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000546 }
Chris Lattner67255a12005-04-07 18:14:58 +0000547 } else if (isa<ConstantSDNode>(N1.Val)) {
548 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000549 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000550 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000551
552 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
553 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
554 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000555
Chris Lattnerc3aae252005-01-07 07:46:32 +0000556 switch (Cond) {
557 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000558 case ISD::SETEQ: return getConstant(C1 == C2, VT);
559 case ISD::SETNE: return getConstant(C1 != C2, VT);
560 case ISD::SETLT: return getConstant(C1 < C2, VT);
561 case ISD::SETGT: return getConstant(C1 > C2, VT);
562 case ISD::SETLE: return getConstant(C1 <= C2, VT);
563 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000564 }
565 } else {
566 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000567 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000568 }
569
570 if (N1 == N2) {
571 // We can always fold X == Y for integer setcc's.
572 if (MVT::isInteger(N1.getValueType()))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000573 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000574 unsigned UOF = ISD::getUnorderedFlavor(Cond);
575 if (UOF == 2) // FP operators that are undefined on NaNs.
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000576 return getConstant(ISD::isTrueWhenEqual(Cond), VT);
Jeff Cohen19bb2282005-05-10 02:22:38 +0000577 if (UOF == unsigned(ISD::isTrueWhenEqual(Cond)))
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000578 return getConstant(UOF, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000579 // Otherwise, we can't fold it. However, we can simplify it to SETUO/SETO
580 // if it is not already.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000581 ISD::CondCode NewCond = UOF == 0 ? ISD::SETUO : ISD::SETO;
582 if (NewCond != Cond)
583 return getSetCC(VT, N1, N2, NewCond);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000584 }
585
Chris Lattner5cdcc582005-01-09 20:52:51 +0000586 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
Chris Lattner68dc3102005-01-10 02:03:02 +0000587 MVT::isInteger(N1.getValueType())) {
588 if (N1.getOpcode() == ISD::ADD || N1.getOpcode() == ISD::SUB ||
589 N1.getOpcode() == ISD::XOR) {
590 // Simplify (X+Y) == (X+Z) --> Y == Z
591 if (N1.getOpcode() == N2.getOpcode()) {
592 if (N1.getOperand(0) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000593 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000594 if (N1.getOperand(1) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000595 return getSetCC(VT, N1.getOperand(0), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000596 if (isCommutativeBinOp(N1.getOpcode())) {
597 // If X op Y == Y op X, try other combinations.
598 if (N1.getOperand(0) == N2.getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000599 return getSetCC(VT, N1.getOperand(1), N2.getOperand(0), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000600 if (N1.getOperand(1) == N2.getOperand(0))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000601 return getSetCC(VT, N1.getOperand(1), N2.getOperand(1), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000602 }
603 }
Chris Lattnerb48da392005-01-23 04:39:44 +0000604
605 // FIXME: move this stuff to the DAG Combiner when it exists!
Misha Brukmanedf128a2005-04-21 22:36:52 +0000606
Chris Lattner68dc3102005-01-10 02:03:02 +0000607 // Simplify (X+Z) == X --> Z == 0
608 if (N1.getOperand(0) == N2)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000609 return getSetCC(VT, N1.getOperand(1),
610 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000611 if (N1.getOperand(1) == N2) {
612 if (isCommutativeBinOp(N1.getOpcode()))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000613 return getSetCC(VT, N1.getOperand(0),
614 getConstant(0, N1.getValueType()), Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000615 else {
616 assert(N1.getOpcode() == ISD::SUB && "Unexpected operation!");
617 // (Z-X) == X --> Z == X<<1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000618 return getSetCC(VT, N1.getOperand(0),
Misha Brukmanedf128a2005-04-21 22:36:52 +0000619 getNode(ISD::SHL, N2.getValueType(),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000620 N2, getConstant(1, TLI.getShiftAmountTy())),
621 Cond);
Chris Lattner68dc3102005-01-10 02:03:02 +0000622 }
Chris Lattner5cdcc582005-01-09 20:52:51 +0000623 }
624 }
625
Chris Lattner68dc3102005-01-10 02:03:02 +0000626 if (N2.getOpcode() == ISD::ADD || N2.getOpcode() == ISD::SUB ||
627 N2.getOpcode() == ISD::XOR) {
628 // Simplify X == (X+Z) --> Z == 0
Chris Lattner7c6e4522005-08-10 17:37:53 +0000629 if (N2.getOperand(0) == N1) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000630 return getSetCC(VT, N2.getOperand(1),
631 getConstant(0, N2.getValueType()), Cond);
Chris Lattner7c6e4522005-08-10 17:37:53 +0000632 } else if (N2.getOperand(1) == N1) {
633 if (isCommutativeBinOp(N2.getOpcode())) {
634 return getSetCC(VT, N2.getOperand(0),
635 getConstant(0, N2.getValueType()), Cond);
636 } else {
637 assert(N2.getOpcode() == ISD::SUB && "Unexpected operation!");
638 // X == (Z-X) --> X<<1 == Z
639 return getSetCC(VT, getNode(ISD::SHL, N2.getValueType(), N1,
640 getConstant(1, TLI.getShiftAmountTy())),
641 N2.getOperand(0), Cond);
642 }
643 }
Chris Lattner68dc3102005-01-10 02:03:02 +0000644 }
645 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000646
Chris Lattnerfda2b552005-04-18 04:48:12 +0000647 // Fold away ALL boolean setcc's.
648 if (N1.getValueType() == MVT::i1) {
649 switch (Cond) {
650 default: assert(0 && "Unknown integer setcc!");
651 case ISD::SETEQ: // X == Y -> (X^Y)^1
652 N1 = getNode(ISD::XOR, MVT::i1,
653 getNode(ISD::XOR, MVT::i1, N1, N2),
654 getConstant(1, MVT::i1));
655 break;
656 case ISD::SETNE: // X != Y --> (X^Y)
657 N1 = getNode(ISD::XOR, MVT::i1, N1, N2);
658 break;
659 case ISD::SETGT: // X >s Y --> X == 0 & Y == 1 --> X^1 & Y
660 case ISD::SETULT: // X <u Y --> X == 0 & Y == 1 --> X^1 & Y
661 N1 = getNode(ISD::AND, MVT::i1, N2,
662 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
663 break;
664 case ISD::SETLT: // X <s Y --> X == 1 & Y == 0 --> Y^1 & X
665 case ISD::SETUGT: // X >u Y --> X == 1 & Y == 0 --> Y^1 & X
666 N1 = getNode(ISD::AND, MVT::i1, N1,
667 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
668 break;
669 case ISD::SETULE: // X <=u Y --> X == 0 | Y == 1 --> X^1 | Y
670 case ISD::SETGE: // X >=s Y --> X == 0 | Y == 1 --> X^1 | Y
671 N1 = getNode(ISD::OR, MVT::i1, N2,
672 getNode(ISD::XOR, MVT::i1, N1, getConstant(1, MVT::i1)));
673 break;
674 case ISD::SETUGE: // X >=u Y --> X == 1 | Y == 0 --> Y^1 | X
675 case ISD::SETLE: // X <=s Y --> X == 1 | Y == 0 --> Y^1 | X
676 N1 = getNode(ISD::OR, MVT::i1, N1,
677 getNode(ISD::XOR, MVT::i1, N2, getConstant(1, MVT::i1)));
678 break;
679 }
680 if (VT != MVT::i1)
681 N1 = getNode(ISD::ZERO_EXTEND, VT, N1);
682 return N1;
683 }
684
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000685 // Could not fold it.
686 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +0000687}
688
689
690
691/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +0000692///
Chris Lattnerc3aae252005-01-07 07:46:32 +0000693SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
694 SDNode *N = new SDNode(Opcode, VT);
695 AllNodes.push_back(N);
696 return SDOperand(N, 0);
697}
698
Chris Lattnerc3aae252005-01-07 07:46:32 +0000699SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
700 SDOperand Operand) {
701 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
702 uint64_t Val = C->getValue();
703 switch (Opcode) {
704 default: break;
705 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
706 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
707 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000708 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
709 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000710 }
711 }
712
713 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
714 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +0000715 case ISD::FNEG:
716 return getConstantFP(-C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000717 case ISD::FP_ROUND:
718 case ISD::FP_EXTEND:
719 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000720 case ISD::FP_TO_SINT:
721 return getConstant((int64_t)C->getValue(), VT);
722 case ISD::FP_TO_UINT:
723 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000724 }
725
726 unsigned OpOpcode = Operand.Val->getOpcode();
727 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +0000728 case ISD::TokenFactor:
729 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000730 case ISD::SIGN_EXTEND:
731 if (Operand.getValueType() == VT) return Operand; // noop extension
732 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
733 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
734 break;
735 case ISD::ZERO_EXTEND:
736 if (Operand.getValueType() == VT) return Operand; // noop extension
Chris Lattner5a6bace2005-04-07 19:43:53 +0000737 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +0000738 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000739 break;
740 case ISD::TRUNCATE:
741 if (Operand.getValueType() == VT) return Operand; // noop truncate
742 if (OpOpcode == ISD::TRUNCATE)
743 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattnerfd8c39b2005-01-07 21:56:24 +0000744 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND) {
745 // If the source is smaller than the dest, we still need an extend.
746 if (Operand.Val->getOperand(0).getValueType() < VT)
747 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
748 else if (Operand.Val->getOperand(0).getValueType() > VT)
749 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
750 else
751 return Operand.Val->getOperand(0);
752 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000753 break;
Chris Lattner485df9b2005-04-09 03:02:46 +0000754 case ISD::FNEG:
755 if (OpOpcode == ISD::SUB) // -(X-Y) -> (Y-X)
756 return getNode(ISD::SUB, VT, Operand.Val->getOperand(1),
757 Operand.Val->getOperand(0));
758 if (OpOpcode == ISD::FNEG) // --X -> X
759 return Operand.Val->getOperand(0);
760 break;
761 case ISD::FABS:
762 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
763 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
764 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000765 }
766
767 SDNode *&N = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
768 if (N) return SDOperand(N, 0);
769 N = new SDNode(Opcode, Operand);
770 N->setValueTypes(VT);
771 AllNodes.push_back(N);
772 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000773}
774
Chris Lattner36019aa2005-04-18 03:48:41 +0000775/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
776/// this predicate to simplify operations downstream. V and Mask are known to
777/// be the same type.
778static bool MaskedValueIsZero(const SDOperand &Op, uint64_t Mask,
779 const TargetLowering &TLI) {
780 unsigned SrcBits;
781 if (Mask == 0) return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000782
Chris Lattner36019aa2005-04-18 03:48:41 +0000783 // If we know the result of a setcc has the top bits zero, use this info.
784 switch (Op.getOpcode()) {
Chris Lattner36019aa2005-04-18 03:48:41 +0000785 case ISD::Constant:
786 return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
787
788 case ISD::SETCC:
Misha Brukmanedf128a2005-04-21 22:36:52 +0000789 return ((Mask & 1) == 0) &&
Chris Lattner36019aa2005-04-18 03:48:41 +0000790 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
791
792 case ISD::ZEXTLOAD:
Chris Lattner5f056bf2005-07-10 01:55:33 +0000793 SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
Chris Lattner36019aa2005-04-18 03:48:41 +0000794 return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
795 case ISD::ZERO_EXTEND:
796 SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
797 return MaskedValueIsZero(Op.getOperand(0),Mask & ((1ULL << SrcBits)-1),TLI);
798
799 case ISD::AND:
800 // (X & C1) & C2 == 0 iff C1 & C2 == 0.
Chris Lattner588bbbf2005-04-21 06:28:15 +0000801 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
Chris Lattner36019aa2005-04-18 03:48:41 +0000802 return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask, TLI);
803
804 // FALL THROUGH
805 case ISD::OR:
806 case ISD::XOR:
807 return MaskedValueIsZero(Op.getOperand(0), Mask, TLI) &&
808 MaskedValueIsZero(Op.getOperand(1), Mask, TLI);
809 case ISD::SELECT:
810 return MaskedValueIsZero(Op.getOperand(1), Mask, TLI) &&
811 MaskedValueIsZero(Op.getOperand(2), Mask, TLI);
Chris Lattner588bbbf2005-04-21 06:28:15 +0000812
813 case ISD::SRL:
814 // (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
815 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
816 uint64_t NewVal = Mask << ShAmt->getValue();
817 SrcBits = MVT::getSizeInBits(Op.getValueType());
818 if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
819 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
820 }
821 return false;
822 case ISD::SHL:
823 // (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
824 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
825 uint64_t NewVal = Mask >> ShAmt->getValue();
826 return MaskedValueIsZero(Op.getOperand(0), NewVal, TLI);
827 }
828 return false;
Chris Lattner6ea92792005-04-25 21:03:25 +0000829 // TODO we could handle some SRA cases here.
Chris Lattner36019aa2005-04-18 03:48:41 +0000830 default: break;
831 }
832
833 return false;
834}
835
836
837
Chris Lattnerc3aae252005-01-07 07:46:32 +0000838SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
839 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +0000840#ifndef NDEBUG
841 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +0000842 case ISD::TokenFactor:
843 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
844 N2.getValueType() == MVT::Other && "Invalid token factor!");
845 break;
Chris Lattner76365122005-01-16 02:23:22 +0000846 case ISD::AND:
847 case ISD::OR:
848 case ISD::XOR:
849 case ISD::UDIV:
850 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000851 case ISD::MULHU:
852 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +0000853 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
854 // fall through
855 case ISD::ADD:
856 case ISD::SUB:
857 case ISD::MUL:
858 case ISD::SDIV:
859 case ISD::SREM:
860 assert(N1.getValueType() == N2.getValueType() &&
861 N1.getValueType() == VT && "Binary operator types must match!");
862 break;
863
864 case ISD::SHL:
865 case ISD::SRA:
866 case ISD::SRL:
867 assert(VT == N1.getValueType() &&
868 "Shift operators return type must be the same as their first arg");
869 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +0000870 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +0000871 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000872 case ISD::FP_ROUND_INREG: {
873 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
874 assert(VT == N1.getValueType() && "Not an inreg round!");
875 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
876 "Cannot FP_ROUND_INREG integer types");
877 assert(EVT <= VT && "Not rounding down!");
878 break;
879 }
880 case ISD::SIGN_EXTEND_INREG: {
881 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
882 assert(VT == N1.getValueType() && "Not an inreg extend!");
883 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
884 "Cannot *_EXTEND_INREG FP types");
885 assert(EVT <= VT && "Not extending!");
886 }
887
Chris Lattner76365122005-01-16 02:23:22 +0000888 default: break;
889 }
890#endif
891
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
893 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
894 if (N1C) {
895 if (N2C) {
896 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
897 switch (Opcode) {
898 case ISD::ADD: return getConstant(C1 + C2, VT);
899 case ISD::SUB: return getConstant(C1 - C2, VT);
900 case ISD::MUL: return getConstant(C1 * C2, VT);
901 case ISD::UDIV:
902 if (C2) return getConstant(C1 / C2, VT);
903 break;
904 case ISD::UREM :
905 if (C2) return getConstant(C1 % C2, VT);
906 break;
907 case ISD::SDIV :
908 if (C2) return getConstant(N1C->getSignExtended() /
909 N2C->getSignExtended(), VT);
910 break;
911 case ISD::SREM :
912 if (C2) return getConstant(N1C->getSignExtended() %
913 N2C->getSignExtended(), VT);
914 break;
915 case ISD::AND : return getConstant(C1 & C2, VT);
916 case ISD::OR : return getConstant(C1 | C2, VT);
917 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +0000918 case ISD::SHL : return getConstant(C1 << (int)C2, VT);
919 case ISD::SRL : return getConstant(C1 >> (unsigned)C2, VT);
920 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000921 default: break;
922 }
923
924 } else { // Cannonicalize constant to RHS if commutative
925 if (isCommutativeBinOp(Opcode)) {
926 std::swap(N1C, N2C);
927 std::swap(N1, N2);
928 }
929 }
Chris Lattner88218ef2005-01-19 17:29:49 +0000930
931 switch (Opcode) {
932 default: break;
933 case ISD::SHL: // shl 0, X -> 0
934 if (N1C->isNullValue()) return N1;
935 break;
936 case ISD::SRL: // srl 0, X -> 0
937 if (N1C->isNullValue()) return N1;
938 break;
939 case ISD::SRA: // sra -1, X -> -1
940 if (N1C->isAllOnesValue()) return N1;
941 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000942 case ISD::SIGN_EXTEND_INREG: // SIGN_EXTEND_INREG N1C, EVT
943 // Extending a constant? Just return the extended constant.
944 SDOperand Tmp = getNode(ISD::TRUNCATE, cast<VTSDNode>(N2)->getVT(), N1);
945 return getNode(ISD::SIGN_EXTEND, VT, Tmp);
Chris Lattner88218ef2005-01-19 17:29:49 +0000946 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000947 }
948
949 if (N2C) {
950 uint64_t C2 = N2C->getValue();
951
952 switch (Opcode) {
953 case ISD::ADD:
954 if (!C2) return N1; // add X, 0 -> X
955 break;
956 case ISD::SUB:
957 if (!C2) return N1; // sub X, 0 -> X
Chris Lattner88de6e72005-05-12 00:17:04 +0000958 return getNode(ISD::ADD, VT, N1, getConstant(-C2, VT));
Chris Lattnerc3aae252005-01-07 07:46:32 +0000959 case ISD::MUL:
960 if (!C2) return N2; // mul X, 0 -> 0
961 if (N2C->isAllOnesValue()) // mul X, -1 -> 0-X
962 return getNode(ISD::SUB, VT, getConstant(0, VT), N1);
963
Chris Lattnerb48da392005-01-23 04:39:44 +0000964 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000965 if ((C2 & C2-1) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +0000966 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +0000967 return getNode(ISD::SHL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000968 }
969 break;
970
Chris Lattnere5eb6f82005-05-15 05:39:08 +0000971 case ISD::MULHU:
972 case ISD::MULHS:
973 if (!C2) return N2; // mul X, 0 -> 0
974
975 if (C2 == 1) // 0X*01 -> 0X hi(0X) == 0
976 return getConstant(0, VT);
977
978 // Many others could be handled here, including -1, powers of 2, etc.
979 break;
980
Chris Lattnerc3aae252005-01-07 07:46:32 +0000981 case ISD::UDIV:
Chris Lattnerb48da392005-01-23 04:39:44 +0000982 // FIXME: Move this to the DAG combiner when it exists.
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983 if ((C2 & C2-1) == 0 && C2) {
Chris Lattner0561b3f2005-08-02 19:26:06 +0000984 SDOperand ShAmt = getConstant(Log2_64(C2), TLI.getShiftAmountTy());
Chris Lattnerb48da392005-01-23 04:39:44 +0000985 return getNode(ISD::SRL, VT, N1, ShAmt);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000986 }
987 break;
988
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000989 case ISD::SHL:
990 case ISD::SRL:
991 case ISD::SRA:
Nate Begemanb8827522005-04-12 23:12:17 +0000992 // If the shift amount is bigger than the size of the data, then all the
Chris Lattner36019aa2005-04-18 03:48:41 +0000993 // bits are shifted out. Simplify to undef.
Nate Begemanb8827522005-04-12 23:12:17 +0000994 if (C2 >= MVT::getSizeInBits(N1.getValueType())) {
995 return getNode(ISD::UNDEF, N1.getValueType());
996 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +0000997 if (C2 == 0) return N1;
Chris Lattner57aa5962005-05-09 17:06:45 +0000998
999 if (Opcode == ISD::SHL && N1.getNumOperands() == 2)
1000 if (ConstantSDNode *OpSA = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1001 unsigned OpSAC = OpSA->getValue();
1002 if (N1.getOpcode() == ISD::SHL) {
1003 if (C2+OpSAC >= MVT::getSizeInBits(N1.getValueType()))
1004 return getConstant(0, N1.getValueType());
1005 return getNode(ISD::SHL, N1.getValueType(), N1.getOperand(0),
1006 getConstant(C2+OpSAC, N2.getValueType()));
1007 } else if (N1.getOpcode() == ISD::SRL) {
1008 // (X >> C1) << C2: if C2 > C1, ((X & ~0<<C1) << C2-C1)
1009 SDOperand Mask = getNode(ISD::AND, VT, N1.getOperand(0),
1010 getConstant(~0ULL << OpSAC, VT));
1011 if (C2 > OpSAC) {
1012 return getNode(ISD::SHL, VT, Mask,
1013 getConstant(C2-OpSAC, N2.getValueType()));
1014 } else {
1015 // (X >> C1) << C2: if C2 <= C1, ((X & ~0<<C1) >> C1-C2)
1016 return getNode(ISD::SRL, VT, Mask,
1017 getConstant(OpSAC-C2, N2.getValueType()));
1018 }
1019 } else if (N1.getOpcode() == ISD::SRA) {
1020 // if C1 == C2, just mask out low bits.
1021 if (C2 == OpSAC)
1022 return getNode(ISD::AND, VT, N1.getOperand(0),
1023 getConstant(~0ULL << C2, VT));
1024 }
1025 }
Chris Lattnera8d9cc82005-01-11 04:25:13 +00001026 break;
1027
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028 case ISD::AND:
1029 if (!C2) return N2; // X and 0 -> 0
1030 if (N2C->isAllOnesValue())
Nate Begemaneea805e2005-04-13 21:23:31 +00001031 return N1; // X and -1 -> X
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001032
Chris Lattner36019aa2005-04-18 03:48:41 +00001033 if (MaskedValueIsZero(N1, C2, TLI)) // X and 0 -> 0
1034 return getConstant(0, VT);
1035
Chris Lattner588bbbf2005-04-21 06:28:15 +00001036 {
1037 uint64_t NotC2 = ~C2;
1038 if (VT != MVT::i64)
1039 NotC2 &= (1ULL << MVT::getSizeInBits(VT))-1;
1040
1041 if (MaskedValueIsZero(N1, NotC2, TLI))
1042 return N1; // if (X & ~C2) -> 0, the and is redundant
1043 }
Chris Lattner36019aa2005-04-18 03:48:41 +00001044
Chris Lattnerdea29e22005-04-10 01:13:15 +00001045 // FIXME: Should add a corresponding version of this for
1046 // ZERO_EXTEND/SIGN_EXTEND by converting them to an ANY_EXTEND node which
1047 // we don't have yet.
1048
Chris Lattner0f2287b2005-04-13 02:38:18 +00001049 // and (sign_extend_inreg x:16:32), 1 -> and x, 1
1050 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001051 // If we are masking out the part of our input that was extended, just
1052 // mask the input to the extension directly.
1053 unsigned ExtendBits =
Chris Lattner15e4b012005-07-10 00:07:11 +00001054 MVT::getSizeInBits(cast<VTSDNode>(N1.getOperand(1))->getVT());
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001055 if ((C2 & (~0ULL << ExtendBits)) == 0)
1056 return getNode(ISD::AND, VT, N1.getOperand(0), N2);
Chris Lattnerbf3fa972005-08-07 05:00:44 +00001057 } else if (N1.getOpcode() == ISD::OR) {
1058 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
1059 if ((ORI->getValue() & C2) == C2) {
1060 // If the 'or' is setting all of the bits that we are masking for,
1061 // we know the result of the AND will be the AND mask itself.
1062 return N2;
1063 }
Chris Lattnera2daa8c2005-04-09 21:43:54 +00001064 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001065 break;
1066 case ISD::OR:
1067 if (!C2)return N1; // X or 0 -> X
1068 if (N2C->isAllOnesValue())
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001069 return N2; // X or -1 -> -1
Chris Lattnerc3aae252005-01-07 07:46:32 +00001070 break;
1071 case ISD::XOR:
1072 if (!C2) return N1; // X xor 0 -> X
1073 if (N2C->isAllOnesValue()) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001074 if (N1.Val->getOpcode() == ISD::SETCC){
1075 SDNode *SetCC = N1.Val;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001076 // !(X op Y) -> (X !op Y)
1077 bool isInteger = MVT::isInteger(SetCC->getOperand(0).getValueType());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001078 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
1079 return getSetCC(SetCC->getValueType(0),
1080 SetCC->getOperand(0), SetCC->getOperand(1),
1081 ISD::getSetCCInverse(CC, isInteger));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001082 } else if (N1.getOpcode() == ISD::AND || N1.getOpcode() == ISD::OR) {
1083 SDNode *Op = N1.Val;
1084 // !(X or Y) -> (!X and !Y) iff X or Y are freely invertible
1085 // !(X and Y) -> (!X or !Y) iff X or Y are freely invertible
1086 SDOperand LHS = Op->getOperand(0), RHS = Op->getOperand(1);
1087 if (isInvertibleForFree(RHS) || isInvertibleForFree(LHS)) {
1088 LHS = getNode(ISD::XOR, VT, LHS, N2); // RHS = ~LHS
1089 RHS = getNode(ISD::XOR, VT, RHS, N2); // RHS = ~RHS
1090 if (Op->getOpcode() == ISD::AND)
1091 return getNode(ISD::OR, VT, LHS, RHS);
1092 return getNode(ISD::AND, VT, LHS, RHS);
1093 }
1094 }
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001095 // X xor -1 -> not(x) ?
Chris Lattnerc3aae252005-01-07 07:46:32 +00001096 }
1097 break;
1098 }
1099
1100 // Reassociate ((X op C1) op C2) if possible.
1101 if (N1.getOpcode() == Opcode && isAssociativeBinOp(Opcode))
1102 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N1.Val->getOperand(1)))
Chris Lattner4287d5e2005-01-07 22:44:09 +00001103 return getNode(Opcode, VT, N1.Val->getOperand(0),
Chris Lattnerc3aae252005-01-07 07:46:32 +00001104 getNode(Opcode, VT, N2, N1.Val->getOperand(1)));
1105 }
1106
1107 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1108 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001109 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001110 if (N2CFP) {
1111 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1112 switch (Opcode) {
1113 case ISD::ADD: return getConstantFP(C1 + C2, VT);
1114 case ISD::SUB: return getConstantFP(C1 - C2, VT);
1115 case ISD::MUL: return getConstantFP(C1 * C2, VT);
1116 case ISD::SDIV:
1117 if (C2) return getConstantFP(C1 / C2, VT);
1118 break;
1119 case ISD::SREM :
1120 if (C2) return getConstantFP(fmod(C1, C2), VT);
1121 break;
1122 default: break;
1123 }
1124
1125 } else { // Cannonicalize constant to RHS if commutative
1126 if (isCommutativeBinOp(Opcode)) {
1127 std::swap(N1CFP, N2CFP);
1128 std::swap(N1, N2);
1129 }
1130 }
1131
Chris Lattner15e4b012005-07-10 00:07:11 +00001132 if (Opcode == ISD::FP_ROUND_INREG)
1133 return getNode(ISD::FP_EXTEND, VT,
1134 getNode(ISD::FP_ROUND, cast<VTSDNode>(N2)->getVT(), N1));
1135 }
1136
Chris Lattnerc3aae252005-01-07 07:46:32 +00001137 // Finally, fold operations that do not require constants.
1138 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001139 case ISD::TokenFactor:
1140 if (N1.getOpcode() == ISD::EntryToken)
1141 return N2;
1142 if (N2.getOpcode() == ISD::EntryToken)
1143 return N1;
1144 break;
1145
Chris Lattnerc3aae252005-01-07 07:46:32 +00001146 case ISD::AND:
1147 case ISD::OR:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001148 if (N1.Val->getOpcode() == ISD::SETCC && N2.Val->getOpcode() == ISD::SETCC){
1149 SDNode *LHS = N1.Val, *RHS = N2.Val;
1150 SDOperand LL = LHS->getOperand(0), RL = RHS->getOperand(0);
1151 SDOperand LR = LHS->getOperand(1), RR = RHS->getOperand(1);
1152 ISD::CondCode Op1 = cast<CondCodeSDNode>(LHS->getOperand(2))->get();
1153 ISD::CondCode Op2 = cast<CondCodeSDNode>(RHS->getOperand(2))->get();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001154
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001155 if (LR == RR && isa<ConstantSDNode>(LR) &&
1156 Op2 == Op1 && MVT::isInteger(LL.getValueType())) {
1157 // (X != 0) | (Y != 0) -> (X|Y != 0)
1158 // (X == 0) & (Y == 0) -> (X|Y == 0)
1159 // (X < 0) | (Y < 0) -> (X|Y < 0)
1160 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1161 ((Op2 == ISD::SETEQ && Opcode == ISD::AND) ||
1162 (Op2 == ISD::SETNE && Opcode == ISD::OR) ||
1163 (Op2 == ISD::SETLT && Opcode == ISD::OR)))
1164 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL), LR,
1165 Op2);
Chris Lattner229ab2e2005-04-25 21:20:28 +00001166
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001167 if (cast<ConstantSDNode>(LR)->isAllOnesValue()) {
1168 // (X == -1) & (Y == -1) -> (X&Y == -1)
1169 // (X != -1) | (Y != -1) -> (X&Y != -1)
1170 // (X > -1) | (Y > -1) -> (X&Y > -1)
1171 if ((Opcode == ISD::AND && Op2 == ISD::SETEQ) ||
1172 (Opcode == ISD::OR && Op2 == ISD::SETNE) ||
1173 (Opcode == ISD::OR && Op2 == ISD::SETGT))
1174 return getSetCC(VT, getNode(ISD::AND, LR.getValueType(), LL, RL),
1175 LR, Op2);
1176 // (X > -1) & (Y > -1) -> (X|Y > -1)
1177 if (Opcode == ISD::AND && Op2 == ISD::SETGT)
1178 return getSetCC(VT, getNode(ISD::OR, LR.getValueType(), LL, RL),
1179 LR, Op2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001180 }
1181 }
Chris Lattner7467c9b2005-04-18 04:11:19 +00001182
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001183 // (X op1 Y) | (Y op2 X) -> (X op1 Y) | (X swapop2 Y)
1184 if (LL == RR && LR == RL) {
1185 Op2 = ISD::getSetCCSwappedOperands(Op2);
1186 goto MatchedBackwards;
1187 }
1188
1189 if (LL == RL && LR == RR) {
1190 MatchedBackwards:
1191 ISD::CondCode Result;
1192 bool isInteger = MVT::isInteger(LL.getValueType());
1193 if (Opcode == ISD::OR)
1194 Result = ISD::getSetCCOrOperation(Op1, Op2, isInteger);
1195 else
1196 Result = ISD::getSetCCAndOperation(Op1, Op2, isInteger);
1197
1198 if (Result != ISD::SETCC_INVALID)
1199 return getSetCC(LHS->getValueType(0), LL, LR, Result);
1200 }
1201 }
1202
Chris Lattner7467c9b2005-04-18 04:11:19 +00001203 // and/or zext(a), zext(b) -> zext(and/or a, b)
1204 if (N1.getOpcode() == ISD::ZERO_EXTEND &&
1205 N2.getOpcode() == ISD::ZERO_EXTEND &&
1206 N1.getOperand(0).getValueType() == N2.getOperand(0).getValueType())
1207 return getNode(ISD::ZERO_EXTEND, VT,
1208 getNode(Opcode, N1.getOperand(0).getValueType(),
1209 N1.getOperand(0), N2.getOperand(0)));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001210 break;
1211 case ISD::XOR:
1212 if (N1 == N2) return getConstant(0, VT); // xor X, Y -> 0
1213 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001214 case ISD::ADD:
1215 if (N2.getOpcode() == ISD::FNEG) // (A+ (-B) -> A-B
1216 return getNode(ISD::SUB, VT, N1, N2.getOperand(0));
1217 if (N1.getOpcode() == ISD::FNEG) // ((-A)+B) -> B-A
1218 return getNode(ISD::SUB, VT, N2, N1.getOperand(0));
Chris Lattneredeecfc2005-04-10 04:04:49 +00001219 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1220 cast<ConstantSDNode>(N1.getOperand(0))->getValue() == 0)
1221 return getNode(ISD::SUB, VT, N2, N1.getOperand(1)); // (0-A)+B -> B-A
1222 if (N2.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N2.getOperand(0)) &&
1223 cast<ConstantSDNode>(N2.getOperand(0))->getValue() == 0)
1224 return getNode(ISD::SUB, VT, N1, N2.getOperand(1)); // A+(0-B) -> A-B
Nate Begeman41aaf702005-06-16 07:06:03 +00001225 if (N2.getOpcode() == ISD::SUB && N1 == N2.Val->getOperand(1) &&
1226 !MVT::isFloatingPoint(N2.getValueType()))
1227 return N2.Val->getOperand(0); // A+(B-A) -> B
Chris Lattner485df9b2005-04-09 03:02:46 +00001228 break;
Chris Lattnerabd21822005-01-09 20:09:57 +00001229 case ISD::SUB:
1230 if (N1.getOpcode() == ISD::ADD) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001231 if (N1.Val->getOperand(0) == N2 &&
Nate Begeman41aaf702005-06-16 07:06:03 +00001232 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001233 return N1.Val->getOperand(1); // (A+B)-A == B
Nate Begeman41aaf702005-06-16 07:06:03 +00001234 if (N1.Val->getOperand(1) == N2 &&
1235 !MVT::isFloatingPoint(N2.getValueType()))
Chris Lattnerabd21822005-01-09 20:09:57 +00001236 return N1.Val->getOperand(0); // (A+B)-B == A
1237 }
Chris Lattner485df9b2005-04-09 03:02:46 +00001238 if (N2.getOpcode() == ISD::FNEG) // (A- (-B) -> A+B
1239 return getNode(ISD::ADD, VT, N1, N2.getOperand(0));
Chris Lattnerabd21822005-01-09 20:09:57 +00001240 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001241 case ISD::FP_ROUND_INREG:
1242 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1243 break;
1244 case ISD::SIGN_EXTEND_INREG: {
1245 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1246 if (EVT == VT) return N1; // Not actually extending
1247
1248 // If we are sign extending an extension, use the original source.
1249 if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG)
1250 if (cast<VTSDNode>(N1.getOperand(1))->getVT() <= EVT)
1251 return N1;
Jeff Cohen00b168892005-07-27 06:12:32 +00001252
Chris Lattner15e4b012005-07-10 00:07:11 +00001253 // If we are sign extending a sextload, return just the load.
1254 if (N1.getOpcode() == ISD::SEXTLOAD)
Chris Lattner5f056bf2005-07-10 01:55:33 +00001255 if (cast<VTSDNode>(N1.getOperand(3))->getVT() <= EVT)
Chris Lattner15e4b012005-07-10 00:07:11 +00001256 return N1;
1257
1258 // If we are extending the result of a setcc, and we already know the
1259 // contents of the top bits, eliminate the extension.
1260 if (N1.getOpcode() == ISD::SETCC &&
1261 TLI.getSetCCResultContents() ==
1262 TargetLowering::ZeroOrNegativeOneSetCCResult)
1263 return N1;
1264
1265 // If we are sign extending the result of an (and X, C) operation, and we
1266 // know the extended bits are zeros already, don't do the extend.
1267 if (N1.getOpcode() == ISD::AND)
1268 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1269 uint64_t Mask = N1C->getValue();
1270 unsigned NumBits = MVT::getSizeInBits(EVT);
1271 if ((Mask & (~0ULL << (NumBits-1))) == 0)
1272 return N1;
1273 }
1274 break;
1275 }
1276
Nate Begemaneea805e2005-04-13 21:23:31 +00001277 // FIXME: figure out how to safely handle things like
1278 // int foo(int x) { return 1 << (x & 255); }
1279 // int bar() { return foo(256); }
1280#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001281 case ISD::SHL:
1282 case ISD::SRL:
1283 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001284 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001285 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001286 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001287 else if (N2.getOpcode() == ISD::AND)
1288 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1289 // If the and is only masking out bits that cannot effect the shift,
1290 // eliminate the and.
1291 unsigned NumBits = MVT::getSizeInBits(VT);
1292 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1293 return getNode(Opcode, VT, N1, N2.getOperand(0));
1294 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001295 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001296#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001297 }
1298
Chris Lattner27e9b412005-05-11 18:57:39 +00001299 // Memoize this node if possible.
1300 SDNode *N;
Chris Lattner16cd04d2005-05-12 23:24:06 +00001301 if (Opcode != ISD::CALLSEQ_START && Opcode != ISD::CALLSEQ_END) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001302 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1303 if (BON) return SDOperand(BON, 0);
1304
1305 BON = N = new SDNode(Opcode, N1, N2);
1306 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001307 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001308 }
1309
Chris Lattner3e011362005-05-14 07:45:46 +00001310 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001311 AllNodes.push_back(N);
1312 return SDOperand(N, 0);
1313}
1314
Chris Lattner88de6e72005-05-12 00:17:04 +00001315// setAdjCallChain - This method changes the token chain of an
Chris Lattner16cd04d2005-05-12 23:24:06 +00001316// CALLSEQ_START/END node to be the specified operand.
Chris Lattner27e9b412005-05-11 18:57:39 +00001317void SDNode::setAdjCallChain(SDOperand N) {
1318 assert(N.getValueType() == MVT::Other);
Chris Lattner16cd04d2005-05-12 23:24:06 +00001319 assert((getOpcode() == ISD::CALLSEQ_START ||
1320 getOpcode() == ISD::CALLSEQ_END) && "Cannot adjust this node!");
Chris Lattner27e9b412005-05-11 18:57:39 +00001321
1322 Operands[0].Val->removeUser(this);
1323 Operands[0] = N;
1324 N.Val->Uses.push_back(this);
1325}
1326
1327
1328
Chris Lattnerc3aae252005-01-07 07:46:32 +00001329SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001330 SDOperand Chain, SDOperand Ptr,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001331 SDOperand SV) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001332 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1333 if (N) return SDOperand(N, 0);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001334 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001335
1336 // Loads have a token chain.
1337 N->setValueTypes(VT, MVT::Other);
1338 AllNodes.push_back(N);
1339 return SDOperand(N, 0);
1340}
1341
Chris Lattner5f056bf2005-07-10 01:55:33 +00001342
1343SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1344 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1345 MVT::ValueType EVT) {
1346 std::vector<SDOperand> Ops;
1347 Ops.reserve(4);
1348 Ops.push_back(Chain);
1349 Ops.push_back(Ptr);
1350 Ops.push_back(SV);
1351 Ops.push_back(getValueType(EVT));
1352 std::vector<MVT::ValueType> VTs;
1353 VTs.reserve(2);
1354 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1355 return getNode(Opcode, VTs, Ops);
1356}
1357
Chris Lattnerc3aae252005-01-07 07:46:32 +00001358SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1359 SDOperand N1, SDOperand N2, SDOperand N3) {
1360 // Perform various simplifications.
1361 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1362 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1363 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1364 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001365 case ISD::SETCC: {
1366 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001367 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001368 if (Simp.Val) return Simp;
1369 break;
1370 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001371 case ISD::SELECT:
1372 if (N1C)
1373 if (N1C->getValue())
1374 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001375 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001376 return N3; // select false, X, Y -> Y
1377
1378 if (N2 == N3) return N2; // select C, X, X -> X
1379
1380 if (VT == MVT::i1) { // Boolean SELECT
1381 if (N2C) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001382 if (N2C->getValue()) // select C, 1, X -> C | X
1383 return getNode(ISD::OR, VT, N1, N3);
1384 else // select C, 0, X -> ~C & X
1385 return getNode(ISD::AND, VT,
1386 getNode(ISD::XOR, N1.getValueType(), N1,
1387 getConstant(1, N1.getValueType())), N3);
1388 } else if (N3C) {
1389 if (N3C->getValue()) // select C, X, 1 -> ~C | X
1390 return getNode(ISD::OR, VT,
1391 getNode(ISD::XOR, N1.getValueType(), N1,
1392 getConstant(1, N1.getValueType())), N2);
1393 else // select C, X, 0 -> C & X
1394 return getNode(ISD::AND, VT, N1, N2);
1395 }
Chris Lattnerfd1f1ee2005-04-12 02:54:39 +00001396
1397 if (N1 == N2) // X ? X : Y --> X ? 1 : Y --> X | Y
1398 return getNode(ISD::OR, VT, N1, N3);
1399 if (N1 == N3) // X ? Y : X --> X ? Y : 0 --> X & Y
1400 return getNode(ISD::AND, VT, N1, N2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001401 }
1402
Chris Lattner59723e92005-04-09 05:15:53 +00001403 // If this is a selectcc, check to see if we can simplify the result.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001404 if (N1.Val->getOpcode() == ISD::SETCC) {
1405 SDNode *SetCC = N1.Val;
1406 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Chris Lattner59723e92005-04-09 05:15:53 +00001407 if (ConstantFPSDNode *CFP =
1408 dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1409 if (CFP->getValue() == 0.0) { // Allow either -0.0 or 0.0
1410 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001411 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
Chris Lattner59723e92005-04-09 05:15:53 +00001412 N2 == SetCC->getOperand(0) && N3.getOpcode() == ISD::FNEG &&
1413 N3.getOperand(0) == N2)
1414 return getNode(ISD::FABS, VT, N2);
1415
1416 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001417 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner59723e92005-04-09 05:15:53 +00001418 N3 == SetCC->getOperand(0) && N2.getOpcode() == ISD::FNEG &&
1419 N2.getOperand(0) == N3)
1420 return getNode(ISD::FABS, VT, N3);
1421 }
Chris Lattner6980d822005-05-12 06:27:02 +00001422 // select (setlt X, 0), A, 0 -> and (sra X, size(X)-1), A
Nate Begemaneea805e2005-04-13 21:23:31 +00001423 if (ConstantSDNode *CN =
1424 dyn_cast<ConstantSDNode>(SetCC->getOperand(1)))
1425 if (CN->getValue() == 0 && N3C && N3C->getValue() == 0)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001426 if (CC == ISD::SETLT) {
Nate Begemaneea805e2005-04-13 21:23:31 +00001427 MVT::ValueType XType = SetCC->getOperand(0).getValueType();
1428 MVT::ValueType AType = N2.getValueType();
1429 if (XType >= AType) {
Chris Lattner6980d822005-05-12 06:27:02 +00001430 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
1431 // single-bit constant. FIXME: remove once the dag combiner
1432 // exists.
1433 if (ConstantSDNode *AC = dyn_cast<ConstantSDNode>(N2))
1434 if ((AC->getValue() & (AC->getValue()-1)) == 0) {
Chris Lattner0561b3f2005-08-02 19:26:06 +00001435 unsigned ShCtV = Log2_64(AC->getValue());
Chris Lattner6980d822005-05-12 06:27:02 +00001436 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
1437 SDOperand ShCt = getConstant(ShCtV, TLI.getShiftAmountTy());
1438 SDOperand Shift = getNode(ISD::SRL, XType,
1439 SetCC->getOperand(0), ShCt);
1440 if (XType > AType)
1441 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1442 return getNode(ISD::AND, AType, Shift, N2);
1443 }
1444
1445
Nate Begemaneea805e2005-04-13 21:23:31 +00001446 SDOperand Shift = getNode(ISD::SRA, XType, SetCC->getOperand(0),
1447 getConstant(MVT::getSizeInBits(XType)-1,
1448 TLI.getShiftAmountTy()));
1449 if (XType > AType)
1450 Shift = getNode(ISD::TRUNCATE, AType, Shift);
1451 return getNode(ISD::AND, AType, Shift, N2);
1452 }
1453 }
Chris Lattner59723e92005-04-09 05:15:53 +00001454 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001455 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001456 case ISD::BRCOND:
1457 if (N2C)
1458 if (N2C->getValue()) // Unconditional branch
1459 return getNode(ISD::BR, MVT::Other, N1, N3);
1460 else
1461 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001462 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001463 }
1464
Chris Lattner385328c2005-05-14 07:42:29 +00001465 std::vector<SDOperand> Ops;
1466 Ops.reserve(3);
1467 Ops.push_back(N1);
1468 Ops.push_back(N2);
1469 Ops.push_back(N3);
1470
1471 // Memoize nodes.
1472 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1473 if (N) return SDOperand(N, 0);
1474
1475 N = new SDNode(Opcode, N1, N2, N3);
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001476 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001477 AllNodes.push_back(N);
1478 return SDOperand(N, 0);
1479}
1480
1481SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001482 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001483 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001484 std::vector<SDOperand> Ops;
1485 Ops.reserve(4);
1486 Ops.push_back(N1);
1487 Ops.push_back(N2);
1488 Ops.push_back(N3);
1489 Ops.push_back(N4);
1490 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001491}
1492
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001493SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1494 SDOperand N1, SDOperand N2, SDOperand N3,
1495 SDOperand N4, SDOperand N5) {
1496 std::vector<SDOperand> Ops;
1497 Ops.reserve(5);
1498 Ops.push_back(N1);
1499 Ops.push_back(N2);
1500 Ops.push_back(N3);
1501 Ops.push_back(N4);
1502 Ops.push_back(N5);
1503 return getNode(Opcode, VT, Ops);
1504}
1505
1506
Chris Lattner0437cdd2005-05-09 04:14:13 +00001507SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001508 assert((!V || isa<PointerType>(V->getType())) &&
1509 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001510 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1511 if (N) return SDOperand(N, 0);
1512
1513 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001514 AllNodes.push_back(N);
1515 return SDOperand(N, 0);
1516}
1517
1518SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001519 std::vector<SDOperand> &Ops) {
1520 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001521 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001522 case 1: return getNode(Opcode, VT, Ops[0]);
1523 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1524 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001525 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001526 }
Chris Lattneref847df2005-04-09 03:27:28 +00001527
Chris Lattner89c34632005-05-14 06:20:26 +00001528 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001529 switch (Opcode) {
1530 default: break;
1531 case ISD::BRCONDTWOWAY:
1532 if (N1C)
1533 if (N1C->getValue()) // Unconditional branch to true dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001534 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001535 else // Unconditional branch to false dest.
Chris Lattner89c34632005-05-14 06:20:26 +00001536 return getNode(ISD::BR, MVT::Other, Ops[0], Ops[3]);
Chris Lattneref847df2005-04-09 03:27:28 +00001537 break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001538
1539 case ISD::TRUNCSTORE: {
1540 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1541 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1542#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1543 // If this is a truncating store of a constant, convert to the desired type
1544 // and store it instead.
1545 if (isa<Constant>(Ops[0])) {
1546 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1547 if (isa<Constant>(Op))
1548 N1 = Op;
1549 }
1550 // Also for ConstantFP?
1551#endif
1552 if (Ops[0].getValueType() == EVT) // Normal store?
1553 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1554 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1555 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1556 "Can't do FP-INT conversion!");
1557 break;
1558 }
Chris Lattneref847df2005-04-09 03:27:28 +00001559 }
1560
Chris Lattner385328c2005-05-14 07:42:29 +00001561 // Memoize nodes.
1562 SDNode *&N = OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1563 if (N) return SDOperand(N, 0);
1564 N = new SDNode(Opcode, Ops);
Chris Lattnere89083a2005-05-14 07:25:05 +00001565 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001566 AllNodes.push_back(N);
1567 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001568}
1569
Chris Lattner89c34632005-05-14 06:20:26 +00001570SDOperand SelectionDAG::getNode(unsigned Opcode,
1571 std::vector<MVT::ValueType> &ResultTys,
1572 std::vector<SDOperand> &Ops) {
1573 if (ResultTys.size() == 1)
1574 return getNode(Opcode, ResultTys[0], Ops);
1575
Chris Lattner5f056bf2005-07-10 01:55:33 +00001576 switch (Opcode) {
1577 case ISD::EXTLOAD:
1578 case ISD::SEXTLOAD:
1579 case ISD::ZEXTLOAD: {
1580 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1581 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1582 // If they are asking for an extending load from/to the same thing, return a
1583 // normal load.
1584 if (ResultTys[0] == EVT)
1585 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
1586 assert(EVT < ResultTys[0] &&
1587 "Should only be an extending load, not truncating!");
1588 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
1589 "Cannot sign/zero extend a FP load!");
1590 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1591 "Cannot convert from FP to Int or Int -> FP!");
1592 break;
1593 }
1594
Chris Lattnere89083a2005-05-14 07:25:05 +00001595 // FIXME: figure out how to safely handle things like
1596 // int foo(int x) { return 1 << (x & 255); }
1597 // int bar() { return foo(256); }
1598#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001599 case ISD::SRA_PARTS:
1600 case ISD::SRL_PARTS:
1601 case ISD::SHL_PARTS:
1602 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001603 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001604 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1605 else if (N3.getOpcode() == ISD::AND)
1606 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1607 // If the and is only masking out bits that cannot effect the shift,
1608 // eliminate the and.
1609 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1610 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1611 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1612 }
1613 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001614#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001615 }
Chris Lattner89c34632005-05-14 06:20:26 +00001616
1617 // Memoize the node.
1618 SDNode *&N = ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys,
1619 Ops))];
1620 if (N) return SDOperand(N, 0);
1621 N = new SDNode(Opcode, Ops);
1622 N->setValueTypes(ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001623 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001624 return SDOperand(N, 0);
1625}
1626
Chris Lattner5c884562005-01-12 18:37:47 +00001627/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
1628/// indicated value. This method ignores uses of other values defined by this
1629/// operation.
1630bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) {
1631 assert(Value < getNumValues() && "Bad value!");
1632
1633 // If there is only one value, this is easy.
1634 if (getNumValues() == 1)
1635 return use_size() == NUses;
1636 if (Uses.size() < NUses) return false;
1637
1638 SDOperand TheValue(this, Value);
1639
1640 std::set<SDNode*> UsersHandled;
1641
1642 for (std::vector<SDNode*>::iterator UI = Uses.begin(), E = Uses.end();
1643 UI != E; ++UI) {
1644 SDNode *User = *UI;
1645 if (User->getNumOperands() == 1 ||
1646 UsersHandled.insert(User).second) // First time we've seen this?
1647 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1648 if (User->getOperand(i) == TheValue) {
1649 if (NUses == 0)
1650 return false; // too many uses
1651 --NUses;
1652 }
1653 }
1654
1655 // Found exactly the right number of uses?
1656 return NUses == 0;
1657}
1658
1659
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001660const char *SDNode::getOperationName() const {
1661 switch (getOpcode()) {
1662 default: return "<<Unknown>>";
Andrew Lenharth95762122005-03-31 21:24:06 +00001663 case ISD::PCMARKER: return "PCMarker";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001664 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001665 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00001666 case ISD::TokenFactor: return "TokenFactor";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001667 case ISD::Constant: return "Constant";
1668 case ISD::ConstantFP: return "ConstantFP";
1669 case ISD::GlobalAddress: return "GlobalAddress";
1670 case ISD::FrameIndex: return "FrameIndex";
1671 case ISD::BasicBlock: return "BasicBlock";
1672 case ISD::ExternalSymbol: return "ExternalSymbol";
1673 case ISD::ConstantPool: return "ConstantPoolIndex";
1674 case ISD::CopyToReg: return "CopyToReg";
1675 case ISD::CopyFromReg: return "CopyFromReg";
Chris Lattner18c2f132005-01-13 20:50:02 +00001676 case ISD::ImplicitDef: return "ImplicitDef";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001677 case ISD::UNDEF: return "undef";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001678
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001679 // Unary operators
1680 case ISD::FABS: return "fabs";
1681 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00001682 case ISD::FSQRT: return "fsqrt";
1683 case ISD::FSIN: return "fsin";
1684 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00001685
1686 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001687 case ISD::ADD: return "add";
1688 case ISD::SUB: return "sub";
1689 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00001690 case ISD::MULHU: return "mulhu";
1691 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001692 case ISD::SDIV: return "sdiv";
1693 case ISD::UDIV: return "udiv";
1694 case ISD::SREM: return "srem";
1695 case ISD::UREM: return "urem";
1696 case ISD::AND: return "and";
1697 case ISD::OR: return "or";
1698 case ISD::XOR: return "xor";
1699 case ISD::SHL: return "shl";
1700 case ISD::SRA: return "sra";
1701 case ISD::SRL: return "srl";
1702
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001703 case ISD::SETCC: return "setcc";
1704 case ISD::SELECT: return "select";
Chris Lattner17eee182005-01-20 18:50:55 +00001705 case ISD::ADD_PARTS: return "add_parts";
1706 case ISD::SUB_PARTS: return "sub_parts";
Chris Lattner41be9512005-04-02 03:30:42 +00001707 case ISD::SHL_PARTS: return "shl_parts";
1708 case ISD::SRA_PARTS: return "sra_parts";
1709 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001710
Chris Lattner7f644642005-04-28 21:44:03 +00001711 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001712 case ISD::SIGN_EXTEND: return "sign_extend";
1713 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00001714 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001715 case ISD::TRUNCATE: return "truncate";
1716 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00001717 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001718 case ISD::FP_EXTEND: return "fp_extend";
1719
1720 case ISD::SINT_TO_FP: return "sint_to_fp";
1721 case ISD::UINT_TO_FP: return "uint_to_fp";
1722 case ISD::FP_TO_SINT: return "fp_to_sint";
1723 case ISD::FP_TO_UINT: return "fp_to_uint";
1724
1725 // Control flow instructions
1726 case ISD::BR: return "br";
1727 case ISD::BRCOND: return "brcond";
Chris Lattneref847df2005-04-09 03:27:28 +00001728 case ISD::BRCONDTWOWAY: return "brcondtwoway";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001729 case ISD::RET: return "ret";
1730 case ISD::CALL: return "call";
Chris Lattnerd71c0412005-05-13 18:43:43 +00001731 case ISD::TAILCALL:return "tailcall";
Chris Lattnera364fa12005-05-12 23:51:40 +00001732 case ISD::CALLSEQ_START: return "callseq_start";
1733 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001734
1735 // Other operators
1736 case ISD::LOAD: return "load";
1737 case ISD::STORE: return "store";
Chris Lattner2ee743f2005-01-14 22:08:15 +00001738 case ISD::EXTLOAD: return "extload";
1739 case ISD::SEXTLOAD: return "sextload";
1740 case ISD::ZEXTLOAD: return "zextload";
1741 case ISD::TRUNCSTORE: return "truncstore";
1742
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001743 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
1744 case ISD::EXTRACT_ELEMENT: return "extract_element";
1745 case ISD::BUILD_PAIR: return "build_pair";
Chris Lattner4c633e82005-01-11 05:57:01 +00001746 case ISD::MEMSET: return "memset";
1747 case ISD::MEMCPY: return "memcpy";
1748 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001749
Chris Lattner276260b2005-05-11 04:50:30 +00001750 // Bit counting
1751 case ISD::CTPOP: return "ctpop";
1752 case ISD::CTTZ: return "cttz";
1753 case ISD::CTLZ: return "ctlz";
1754
1755 // IO Intrinsics
Chris Lattner3c691012005-05-09 20:22:17 +00001756 case ISD::READPORT: return "readport";
1757 case ISD::WRITEPORT: return "writeport";
1758 case ISD::READIO: return "readio";
1759 case ISD::WRITEIO: return "writeio";
1760
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001761 case ISD::CONDCODE:
1762 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001763 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001764 case ISD::SETOEQ: return "setoeq";
1765 case ISD::SETOGT: return "setogt";
1766 case ISD::SETOGE: return "setoge";
1767 case ISD::SETOLT: return "setolt";
1768 case ISD::SETOLE: return "setole";
1769 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001770
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001771 case ISD::SETO: return "seto";
1772 case ISD::SETUO: return "setuo";
1773 case ISD::SETUEQ: return "setue";
1774 case ISD::SETUGT: return "setugt";
1775 case ISD::SETUGE: return "setuge";
1776 case ISD::SETULT: return "setult";
1777 case ISD::SETULE: return "setule";
1778 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001779
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001780 case ISD::SETEQ: return "seteq";
1781 case ISD::SETGT: return "setgt";
1782 case ISD::SETGE: return "setge";
1783 case ISD::SETLT: return "setlt";
1784 case ISD::SETLE: return "setle";
1785 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001786 }
1787 }
1788}
Chris Lattnerc3aae252005-01-07 07:46:32 +00001789
1790void SDNode::dump() const {
1791 std::cerr << (void*)this << ": ";
1792
1793 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
1794 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00001795 if (getValueType(i) == MVT::Other)
1796 std::cerr << "ch";
1797 else
1798 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001799 }
Chris Lattnerd75f19f2005-01-10 23:25:25 +00001800 std::cerr << " = " << getOperationName();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001801
1802 std::cerr << " ";
1803 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
1804 if (i) std::cerr << ", ";
1805 std::cerr << (void*)getOperand(i).Val;
1806 if (unsigned RN = getOperand(i).ResNo)
1807 std::cerr << ":" << RN;
1808 }
1809
1810 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
1811 std::cerr << "<" << CSDN->getValue() << ">";
1812 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
1813 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001814 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00001815 dyn_cast<GlobalAddressSDNode>(this)) {
1816 std::cerr << "<";
1817 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001818 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001819 std::cerr << "<" << FIDN->getIndex() << ">";
1820 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
1821 std::cerr << "<" << CP->getIndex() << ">";
Misha Brukmandedf2bd2005-04-22 04:01:18 +00001822 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001823 std::cerr << "<";
1824 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
1825 if (LBB)
1826 std::cerr << LBB->getName() << " ";
1827 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattner18c2f132005-01-13 20:50:02 +00001828 } else if (const RegSDNode *C2V = dyn_cast<RegSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001829 std::cerr << "<reg #" << C2V->getReg() << ">";
1830 } else if (const ExternalSymbolSDNode *ES =
1831 dyn_cast<ExternalSymbolSDNode>(this)) {
1832 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00001833 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
1834 if (M->getValue())
1835 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
1836 else
1837 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnerc3aae252005-01-07 07:46:32 +00001838 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001839}
1840
Chris Lattnerea946cd2005-01-09 20:38:33 +00001841static void DumpNodes(SDNode *N, unsigned indent) {
1842 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1843 if (N->getOperand(i).Val->hasOneUse())
1844 DumpNodes(N->getOperand(i).Val, indent+2);
1845 else
1846 std::cerr << "\n" << std::string(indent+2, ' ')
1847 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00001848
Chris Lattnerea946cd2005-01-09 20:38:33 +00001849
1850 std::cerr << "\n" << std::string(indent, ' ');
1851 N->dump();
1852}
1853
Chris Lattnerc3aae252005-01-07 07:46:32 +00001854void SelectionDAG::dump() const {
1855 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattner49d24712005-01-09 20:26:36 +00001856 std::vector<SDNode*> Nodes(AllNodes);
1857 std::sort(Nodes.begin(), Nodes.end());
1858
1859 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00001860 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
1861 DumpNodes(Nodes[i], 2);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001862 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00001863
1864 DumpNodes(getRoot().Val, 2);
1865
Chris Lattnerc3aae252005-01-07 07:46:32 +00001866 std::cerr << "\n\n";
1867}
1868