blob: cf1d41f705fadbe5e6df2cad3b273e06f5e051f5 [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"
Chris Lattner70a248d2006-03-27 06:45:25 +000017#include "llvm/Intrinsics.h"
Chris Lattnerc3aae252005-01-07 07:46:32 +000018#include "llvm/Assembly/Writer.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner0561b3f2005-08-02 19:26:06 +000020#include "llvm/Support/MathExtras.h"
Chris Lattnerfa164b62005-08-19 21:34:13 +000021#include "llvm/Target/MRegisterInfo.h"
Chris Lattnerb48da392005-01-23 04:39:44 +000022#include "llvm/Target/TargetLowering.h"
Chris Lattnerf3e133a2005-08-16 18:33:07 +000023#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
Chris Lattner012f2412006-02-17 21:58:01 +000025#include "llvm/ADT/SetVector.h"
Evan Cheng115c0362005-12-19 23:11:49 +000026#include "llvm/ADT/StringExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000027#include <iostream>
Chris Lattner0e12e6e2005-01-07 21:09:16 +000028#include <set>
Chris Lattnerc3aae252005-01-07 07:46:32 +000029#include <cmath>
Jeff Cohenfd161e92005-01-09 20:41:56 +000030#include <algorithm>
Chris Lattnere25738c2004-06-02 04:28:06 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner5cdcc582005-01-09 20:52:51 +000033static bool isCommutativeBinOp(unsigned Opcode) {
34 switch (Opcode) {
35 case ISD::ADD:
36 case ISD::MUL:
Nate Begeman1b5db7a2006-01-16 08:07:10 +000037 case ISD::MULHU:
38 case ISD::MULHS:
Chris Lattner01b3d732005-09-28 22:28:18 +000039 case ISD::FADD:
40 case ISD::FMUL:
Chris Lattner5cdcc582005-01-09 20:52:51 +000041 case ISD::AND:
42 case ISD::OR:
43 case ISD::XOR: return true;
44 default: return false; // FIXME: Need commutative info for user ops!
45 }
46}
47
Chris Lattner5cdcc582005-01-09 20:52:51 +000048// isInvertibleForFree - Return true if there is no cost to emitting the logical
49// inverse of this node.
50static bool isInvertibleForFree(SDOperand N) {
51 if (isa<ConstantSDNode>(N.Val)) return true;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +000052 if (N.Val->getOpcode() == ISD::SETCC && N.Val->hasOneUse())
Chris Lattner5cdcc582005-01-09 20:52:51 +000053 return true;
Misha Brukmanedf128a2005-04-21 22:36:52 +000054 return false;
Chris Lattner5cdcc582005-01-09 20:52:51 +000055}
56
Jim Laskey58b968b2005-08-17 20:08:02 +000057//===----------------------------------------------------------------------===//
58// ConstantFPSDNode Class
59//===----------------------------------------------------------------------===//
60
61/// isExactlyValue - We don't rely on operator== working on double values, as
62/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
63/// As such, this method can be used to do an exact bit-for-bit comparison of
64/// two floating point values.
65bool ConstantFPSDNode::isExactlyValue(double V) const {
66 return DoubleToBits(V) == DoubleToBits(Value);
67}
68
69//===----------------------------------------------------------------------===//
Chris Lattner61d43992006-03-25 22:57:01 +000070// ISD Namespace
Jim Laskey58b968b2005-08-17 20:08:02 +000071//===----------------------------------------------------------------------===//
Chris Lattner5cdcc582005-01-09 20:52:51 +000072
Evan Chenga8df1662006-03-27 06:58:47 +000073/// isBuildVectorAllOnes - Return true if the specified node is a
Chris Lattner61d43992006-03-25 22:57:01 +000074/// BUILD_VECTOR where all of the elements are ~0 or undef.
Evan Chenga8df1662006-03-27 06:58:47 +000075bool ISD::isBuildVectorAllOnes(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +000076 // Look through a bit convert.
77 if (N->getOpcode() == ISD::BIT_CONVERT)
78 N = N->getOperand(0).Val;
79
Evan Chenga8df1662006-03-27 06:58:47 +000080 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000081
82 unsigned i = 0, e = N->getNumOperands();
83
84 // Skip over all of the undef values.
85 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
86 ++i;
87
88 // Do not accept an all-undef vector.
89 if (i == e) return false;
90
91 // Do not accept build_vectors that aren't all constants or which have non-~0
92 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000093 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000094 if (isa<ConstantSDNode>(NotZero)) {
95 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
96 return false;
97 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Cheng23cc8702006-03-27 08:10:26 +000098 MVT::ValueType VT = NotZero.getValueType();
99 if (VT== MVT::f64) {
100 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
101 (uint64_t)-1)
102 return false;
103 } else {
104 if (FloatToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) !=
105 (uint32_t)-1)
106 return false;
107 }
Evan Chenga8df1662006-03-27 06:58:47 +0000108 } else
Chris Lattner61d43992006-03-25 22:57:01 +0000109 return false;
110
111 // Okay, we have at least one ~0 value, check to see if the rest match or are
112 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000113 for (++i; i != e; ++i)
114 if (N->getOperand(i) != NotZero &&
115 N->getOperand(i).getOpcode() != ISD::UNDEF)
116 return false;
117 return true;
118}
119
120
Evan Cheng4a147842006-03-26 09:50:58 +0000121/// isBuildVectorAllZeros - Return true if the specified node is a
122/// BUILD_VECTOR where all of the elements are 0 or undef.
123bool ISD::isBuildVectorAllZeros(const SDNode *N) {
Chris Lattner547a16f2006-04-15 23:38:00 +0000124 // Look through a bit convert.
125 if (N->getOpcode() == ISD::BIT_CONVERT)
126 N = N->getOperand(0).Val;
127
Evan Cheng4a147842006-03-26 09:50:58 +0000128 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000129
130 unsigned i = 0, e = N->getNumOperands();
131
132 // Skip over all of the undef values.
133 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
134 ++i;
135
136 // Do not accept an all-undef vector.
137 if (i == e) return false;
138
139 // Do not accept build_vectors that aren't all constants or which have non-~0
140 // elements.
141 SDOperand Zero = N->getOperand(i);
142 if (isa<ConstantSDNode>(Zero)) {
143 if (!cast<ConstantSDNode>(Zero)->isNullValue())
144 return false;
145 } else if (isa<ConstantFPSDNode>(Zero)) {
146 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
147 return false;
148 } else
149 return false;
150
151 // Okay, we have at least one ~0 value, check to see if the rest match or are
152 // undefs.
153 for (++i; i != e; ++i)
154 if (N->getOperand(i) != Zero &&
155 N->getOperand(i).getOpcode() != ISD::UNDEF)
156 return false;
157 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000158}
159
Chris Lattnerc3aae252005-01-07 07:46:32 +0000160/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
161/// when given the operation for (X op Y).
162ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
163 // To perform this operation, we just need to swap the L and G bits of the
164 // operation.
165 unsigned OldL = (Operation >> 2) & 1;
166 unsigned OldG = (Operation >> 1) & 1;
167 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
168 (OldL << 1) | // New G bit
169 (OldG << 2)); // New L bit.
170}
171
172/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
173/// 'op' is a valid SetCC operation.
174ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
175 unsigned Operation = Op;
176 if (isInteger)
177 Operation ^= 7; // Flip L, G, E bits, but not U.
178 else
179 Operation ^= 15; // Flip all of the condition bits.
180 if (Operation > ISD::SETTRUE2)
181 Operation &= ~8; // Don't let N and U bits get set.
182 return ISD::CondCode(Operation);
183}
184
185
186/// isSignedOp - For an integer comparison, return 1 if the comparison is a
187/// signed operation and 2 if the result is an unsigned comparison. Return zero
188/// if the operation does not depend on the sign of the input (setne and seteq).
189static int isSignedOp(ISD::CondCode Opcode) {
190 switch (Opcode) {
191 default: assert(0 && "Illegal integer setcc operation!");
192 case ISD::SETEQ:
193 case ISD::SETNE: return 0;
194 case ISD::SETLT:
195 case ISD::SETLE:
196 case ISD::SETGT:
197 case ISD::SETGE: return 1;
198 case ISD::SETULT:
199 case ISD::SETULE:
200 case ISD::SETUGT:
201 case ISD::SETUGE: return 2;
202 }
203}
204
205/// getSetCCOrOperation - Return the result of a logical OR between different
206/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
207/// returns SETCC_INVALID if it is not possible to represent the resultant
208/// comparison.
209ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
210 bool isInteger) {
211 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
212 // Cannot fold a signed integer setcc with an unsigned integer setcc.
213 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000214
Chris Lattnerc3aae252005-01-07 07:46:32 +0000215 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000216
Chris Lattnerc3aae252005-01-07 07:46:32 +0000217 // If the N and U bits get set then the resultant comparison DOES suddenly
218 // care about orderedness, and is true when ordered.
219 if (Op > ISD::SETTRUE2)
Chris Lattnere41102b2006-05-12 17:03:46 +0000220 Op &= ~16; // Clear the U bit if the N bit is set.
221
222 // Canonicalize illegal integer setcc's.
223 if (isInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
224 Op = ISD::SETNE;
225
Chris Lattnerc3aae252005-01-07 07:46:32 +0000226 return ISD::CondCode(Op);
227}
228
229/// getSetCCAndOperation - Return the result of a logical AND between different
230/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
231/// function returns zero if it is not possible to represent the resultant
232/// comparison.
233ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
234 bool isInteger) {
235 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
236 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000237 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000238
239 // Combine all of the condition bits.
Chris Lattnera83385f2006-04-27 05:01:07 +0000240 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
241
242 // Canonicalize illegal integer setcc's.
243 if (isInteger) {
244 switch (Result) {
245 default: break;
246 case ISD::SETUO: // e.g. SETUGT & SETULT
247 Result = ISD::SETFALSE;
248 break;
249 case ISD::SETUEQ: // e.g. SETUGE & SETULE
250 Result = ISD::SETEQ;
251 break;
252 }
253 }
254
255 return Result;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000256}
257
Chris Lattnerb48da392005-01-23 04:39:44 +0000258const TargetMachine &SelectionDAG::getTarget() const {
259 return TLI.getTargetMachine();
260}
261
Jim Laskey58b968b2005-08-17 20:08:02 +0000262//===----------------------------------------------------------------------===//
263// SelectionDAG Class
264//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000265
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266/// RemoveDeadNodes - This method deletes all unreachable nodes in the
267/// SelectionDAG, including nodes (like loads) that have uses of their token
268/// chain but no other uses and no side effect. If a node is passed in as an
269/// argument, it is used as the seed for node deletion.
270void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000271 // Create a dummy node (which is not added to allnodes), that adds a reference
272 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000273 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000274
Chris Lattnerf469cb62005-11-08 18:52:27 +0000275 bool MadeChange = false;
276
Chris Lattner8b8749f2005-08-17 19:00:20 +0000277 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000278 if (N && N->use_empty()) {
279 DestroyDeadNode(N);
280 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000281 }
282
Chris Lattnerde202b32005-11-09 23:47:37 +0000283 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
284 if (I->use_empty() && I->getOpcode() != 65535) {
285 // Node is dead, recursively delete newly dead uses.
286 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000287 MadeChange = true;
288 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000289
290 // Walk the nodes list, removing the nodes we've marked as dead.
291 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000292 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
293 SDNode *N = I++;
294 if (N->use_empty())
295 AllNodes.erase(N);
296 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000297 }
298
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000299 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000300 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000301}
302
Chris Lattnerf469cb62005-11-08 18:52:27 +0000303/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
304/// graph. If it is the last user of any of its operands, recursively process
305/// them the same way.
306///
307void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000308 // Okay, we really are going to delete this node. First take this out of the
309 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000310 RemoveNodeFromCSEMaps(N);
311
312 // Next, brutally remove the operand list. This is safe to do, as there are
313 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000314 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
315 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000316 O->removeUser(N);
317
318 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000319 if (O->use_empty())
320 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000321 }
Chris Lattner65113b22005-11-08 22:07:03 +0000322 delete[] N->OperandList;
323 N->OperandList = 0;
324 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000325
326 // Mark the node as dead.
327 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000328}
329
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000330void SelectionDAG::DeleteNode(SDNode *N) {
331 assert(N->use_empty() && "Cannot delete a node that is not dead!");
332
333 // First take this out of the appropriate CSE map.
334 RemoveNodeFromCSEMaps(N);
335
Chris Lattner1e111c72005-09-07 05:37:01 +0000336 // Finally, remove uses due to operands of this node, remove from the
337 // AllNodes list, and delete the node.
338 DeleteNodeNotInCSEMaps(N);
339}
340
341void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
342
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000343 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000344 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000345
346 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000347 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
348 I->Val->removeUser(N);
349 delete[] N->OperandList;
350 N->OperandList = 0;
351 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000352
353 delete N;
354}
355
Chris Lattner149c58c2005-08-16 18:17:10 +0000356/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
357/// correspond to it. This is useful when we're about to delete or repurpose
358/// the node. We don't want future request for structurally identical nodes
359/// to return N anymore.
360void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000361 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000362 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000363 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000364 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000365 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
366 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000367 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000368 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 Erased = TargetConstants.erase(std::make_pair(
370 cast<ConstantSDNode>(N)->getValue(),
371 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000372 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000373 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000374 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000375 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000377 }
Chris Lattner3181a772006-01-29 06:26:56 +0000378 case ISD::TargetConstantFP: {
379 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
380 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
381 break;
382 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000383 case ISD::STRING:
384 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
385 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000386 case ISD::CONDCODE:
387 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
388 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000389 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000390 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
391 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000392 case ISD::GlobalAddress: {
393 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
394 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
395 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000396 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000397 }
398 case ISD::TargetGlobalAddress: {
399 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
400 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
401 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000402 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000403 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000404 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000405 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000406 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000407 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000408 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000409 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000410 case ISD::JumpTable:
411 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
412 break;
413 case ISD::TargetJumpTable:
414 Erased =
415 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
416 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000417 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000418 Erased = ConstantPoolIndices.
419 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000420 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
421 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000422 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000423 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000424 Erased = TargetConstantPoolIndices.
425 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000426 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
427 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000428 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000429 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000430 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000431 break;
432 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000433 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000434 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000435 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000436 Erased =
437 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000438 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000439 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000440 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000441 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
442 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000443 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000444 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
445 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000446 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000447 case ISD::SRCVALUE: {
448 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000449 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000450 break;
451 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000452 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000453 Erased = Loads.erase(std::make_pair(N->getOperand(1),
454 std::make_pair(N->getOperand(0),
455 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000456 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000457 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000458 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000459 if (N->getNumOperands() == 0) {
460 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
461 N->getValueType(0)));
462 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000463 Erased =
464 UnaryOps.erase(std::make_pair(N->getOpcode(),
465 std::make_pair(N->getOperand(0),
466 N->getValueType(0))));
467 } else if (N->getNumOperands() == 2) {
468 Erased =
469 BinaryOps.erase(std::make_pair(N->getOpcode(),
470 std::make_pair(N->getOperand(0),
471 N->getOperand(1))));
472 } else {
473 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
474 Erased =
475 OneResultNodes.erase(std::make_pair(N->getOpcode(),
476 std::make_pair(N->getValueType(0),
477 Ops)));
478 }
Chris Lattner385328c2005-05-14 07:42:29 +0000479 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000480 // Remove the node from the ArbitraryNodes map.
481 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
482 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000483 Erased =
484 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
485 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000486 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000487 break;
488 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000489#ifndef NDEBUG
490 // Verify that the node was actually in one of the CSE maps, unless it has a
491 // flag result (which cannot be CSE'd) or is one of the special cases that are
492 // not subject to CSE.
493 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000494 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000495 N->dump();
496 assert(0 && "Node is not in map!");
497 }
498#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000499}
500
Chris Lattner8b8749f2005-08-17 19:00:20 +0000501/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
502/// has been taken out and modified in some way. If the specified node already
503/// exists in the CSE maps, do not modify the maps, but return the existing node
504/// instead. If it doesn't exist, add it and return null.
505///
506SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
507 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000508 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000509 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000510
Chris Lattner9f8cc692005-12-19 22:21:21 +0000511 // Check that remaining values produced are not flags.
512 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
513 if (N->getValueType(i) == MVT::Flag)
514 return 0; // Never CSE anything that produces a flag.
515
Chris Lattnerfe14b342005-12-01 23:14:50 +0000516 if (N->getNumValues() == 1) {
517 if (N->getNumOperands() == 1) {
518 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
519 std::make_pair(N->getOperand(0),
520 N->getValueType(0)))];
521 if (U) return U;
522 U = N;
523 } else if (N->getNumOperands() == 2) {
524 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
525 std::make_pair(N->getOperand(0),
526 N->getOperand(1)))];
527 if (B) return B;
528 B = N;
529 } else {
530 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
531 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000532 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000533 if (ORN) return ORN;
534 ORN = N;
535 }
536 } else {
537 if (N->getOpcode() == ISD::LOAD) {
538 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
539 std::make_pair(N->getOperand(0),
540 N->getValueType(0)))];
541 if (L) return L;
542 L = N;
543 } else {
544 // Remove the node from the ArbitraryNodes map.
545 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
546 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
547 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
548 std::make_pair(RV, Ops))];
549 if (AN) return AN;
550 AN = N;
551 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000552 }
553 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000554}
555
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000556/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
557/// were replaced with those specified. If this node is never memoized,
558/// return null, otherwise return a pointer to the slot it would take. If a
559/// node already exists with these operands, the slot will be non-null.
560SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000561 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000562 return 0; // Never add these nodes.
563
564 // Check that remaining values produced are not flags.
565 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
566 if (N->getValueType(i) == MVT::Flag)
567 return 0; // Never CSE anything that produces a flag.
568
569 if (N->getNumValues() == 1) {
570 return &UnaryOps[std::make_pair(N->getOpcode(),
571 std::make_pair(Op, N->getValueType(0)))];
572 } else {
573 // Remove the node from the ArbitraryNodes map.
574 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
575 std::vector<SDOperand> Ops;
576 Ops.push_back(Op);
577 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
578 std::make_pair(RV, Ops))];
579 }
580 return 0;
581}
582
583/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
584/// were replaced with those specified. If this node is never memoized,
585/// return null, otherwise return a pointer to the slot it would take. If a
586/// node already exists with these operands, the slot will be non-null.
587SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
588 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000589 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000590 return 0; // Never add these nodes.
591
592 // Check that remaining values produced are not flags.
593 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
594 if (N->getValueType(i) == MVT::Flag)
595 return 0; // Never CSE anything that produces a flag.
596
597 if (N->getNumValues() == 1) {
598 return &BinaryOps[std::make_pair(N->getOpcode(),
599 std::make_pair(Op1, Op2))];
600 } else {
601 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
602 std::vector<SDOperand> Ops;
603 Ops.push_back(Op1);
604 Ops.push_back(Op2);
605 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
606 std::make_pair(RV, Ops))];
607 }
608 return 0;
609}
610
611
612/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
613/// were replaced with those specified. If this node is never memoized,
614/// return null, otherwise return a pointer to the slot it would take. If a
615/// node already exists with these operands, the slot will be non-null.
616SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
617 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000618 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000619 return 0; // Never add these nodes.
620
621 // Check that remaining values produced are not flags.
622 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
623 if (N->getValueType(i) == MVT::Flag)
624 return 0; // Never CSE anything that produces a flag.
625
626 if (N->getNumValues() == 1) {
627 if (N->getNumOperands() == 1) {
628 return &UnaryOps[std::make_pair(N->getOpcode(),
629 std::make_pair(Ops[0],
630 N->getValueType(0)))];
631 } else if (N->getNumOperands() == 2) {
632 return &BinaryOps[std::make_pair(N->getOpcode(),
633 std::make_pair(Ops[0], Ops[1]))];
634 } else {
635 return &OneResultNodes[std::make_pair(N->getOpcode(),
636 std::make_pair(N->getValueType(0),
637 Ops))];
638 }
639 } else {
640 if (N->getOpcode() == ISD::LOAD) {
641 return &Loads[std::make_pair(Ops[1],
642 std::make_pair(Ops[0], N->getValueType(0)))];
643 } else {
644 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
645 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
646 std::make_pair(RV, Ops))];
647 }
648 }
649 return 0;
650}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000651
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000652
Chris Lattner78ec3112003-08-11 14:57:33 +0000653SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000654 while (!AllNodes.empty()) {
655 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000656 delete [] N->OperandList;
657 N->OperandList = 0;
658 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000659 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000660 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000661}
662
Chris Lattner0f2287b2005-04-13 02:38:18 +0000663SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000664 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000665 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000666 return getNode(ISD::AND, Op.getValueType(), Op,
667 getConstant(Imm, Op.getValueType()));
668}
669
Chris Lattnerc3aae252005-01-07 07:46:32 +0000670SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
671 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000672 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
673
Chris Lattnerc3aae252005-01-07 07:46:32 +0000674 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000675 if (VT != MVT::i64)
676 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000677
Chris Lattnerc3aae252005-01-07 07:46:32 +0000678 SDNode *&N = Constants[std::make_pair(Val, VT)];
679 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000680 N = new ConstantSDNode(false, Val, VT);
681 AllNodes.push_back(N);
682 return SDOperand(N, 0);
683}
684
Chris Lattner36ce6912005-11-29 06:21:05 +0000685SDOperand SelectionDAG::getString(const std::string &Val) {
686 StringSDNode *&N = StringNodes[Val];
687 if (!N) {
688 N = new StringSDNode(Val);
689 AllNodes.push_back(N);
690 }
691 return SDOperand(N, 0);
692}
693
Chris Lattner37bfbb42005-08-17 00:34:06 +0000694SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
695 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
696 // Mask out any bits that are not valid for this constant.
697 if (VT != MVT::i64)
698 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
699
700 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
701 if (N) return SDOperand(N, 0);
702 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000703 AllNodes.push_back(N);
704 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000705}
706
Chris Lattnerc3aae252005-01-07 07:46:32 +0000707SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
708 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
709 if (VT == MVT::f32)
710 Val = (float)Val; // Mask out extra precision.
711
Chris Lattnerd8658612005-02-17 20:17:32 +0000712 // Do the map lookup using the actual bit pattern for the floating point
713 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
714 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000715 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000716 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000717 N = new ConstantFPSDNode(false, Val, VT);
718 AllNodes.push_back(N);
719 return SDOperand(N, 0);
720}
721
722SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
723 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
724 if (VT == MVT::f32)
725 Val = (float)Val; // Mask out extra precision.
726
727 // Do the map lookup using the actual bit pattern for the floating point
728 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
729 // we don't have issues with SNANs.
730 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
731 if (N) return SDOperand(N, 0);
732 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
735}
736
Chris Lattnerc3aae252005-01-07 07:46:32 +0000737SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000738 MVT::ValueType VT, int offset) {
739 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000740 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000741 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000742 AllNodes.push_back(N);
743 return SDOperand(N, 0);
744}
745
746SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000747 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000748 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000749 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000750 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000751 AllNodes.push_back(N);
752 return SDOperand(N, 0);
753}
754
755SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
756 SDNode *&N = FrameIndices[FI];
757 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000758 N = new FrameIndexSDNode(FI, VT, false);
759 AllNodes.push_back(N);
760 return SDOperand(N, 0);
761}
762
763SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
764 SDNode *&N = TargetFrameIndices[FI];
765 if (N) return SDOperand(N, 0);
766 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000767 AllNodes.push_back(N);
768 return SDOperand(N, 0);
769}
770
Nate Begeman37efe672006-04-22 18:53:45 +0000771SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
772 SDNode *&N = JumpTableIndices[JTI];
773 if (N) return SDOperand(N, 0);
774 N = new JumpTableSDNode(JTI, VT, false);
775 AllNodes.push_back(N);
776 return SDOperand(N, 0);
777}
778
779SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
780 SDNode *&N = TargetJumpTableIndices[JTI];
781 if (N) return SDOperand(N, 0);
782 N = new JumpTableSDNode(JTI, VT, true);
783 AllNodes.push_back(N);
784 return SDOperand(N, 0);
785}
786
Evan Chengb8973bd2006-01-31 22:23:14 +0000787SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000788 unsigned Alignment, int Offset) {
789 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
790 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000791 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000792 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000793 AllNodes.push_back(N);
794 return SDOperand(N, 0);
795}
796
Evan Chengb8973bd2006-01-31 22:23:14 +0000797SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000798 unsigned Alignment, int Offset) {
799 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
800 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000801 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000802 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000803 AllNodes.push_back(N);
804 return SDOperand(N, 0);
805}
806
807SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
808 SDNode *&N = BBNodes[MBB];
809 if (N) return SDOperand(N, 0);
810 N = new BasicBlockSDNode(MBB);
811 AllNodes.push_back(N);
812 return SDOperand(N, 0);
813}
814
Chris Lattner15e4b012005-07-10 00:07:11 +0000815SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
816 if ((unsigned)VT >= ValueTypeNodes.size())
817 ValueTypeNodes.resize(VT+1);
818 if (ValueTypeNodes[VT] == 0) {
819 ValueTypeNodes[VT] = new VTSDNode(VT);
820 AllNodes.push_back(ValueTypeNodes[VT]);
821 }
822
823 return SDOperand(ValueTypeNodes[VT], 0);
824}
825
Chris Lattnerc3aae252005-01-07 07:46:32 +0000826SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
827 SDNode *&N = ExternalSymbols[Sym];
828 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000829 N = new ExternalSymbolSDNode(false, Sym, VT);
830 AllNodes.push_back(N);
831 return SDOperand(N, 0);
832}
833
Chris Lattner809ec112006-01-28 10:09:25 +0000834SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
835 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000836 SDNode *&N = TargetExternalSymbols[Sym];
837 if (N) return SDOperand(N, 0);
838 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000839 AllNodes.push_back(N);
840 return SDOperand(N, 0);
841}
842
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000843SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
844 if ((unsigned)Cond >= CondCodeNodes.size())
845 CondCodeNodes.resize(Cond+1);
846
Chris Lattner079a27a2005-08-09 20:40:02 +0000847 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000848 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000849 AllNodes.push_back(CondCodeNodes[Cond]);
850 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000851 return SDOperand(CondCodeNodes[Cond], 0);
852}
853
Chris Lattner0fdd7682005-08-30 22:38:38 +0000854SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
855 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
856 if (!Reg) {
857 Reg = new RegisterSDNode(RegNo, VT);
858 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000859 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000860 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000861}
862
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000863SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
864 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000865 // These setcc operations always fold.
866 switch (Cond) {
867 default: break;
868 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000869 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000870 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000871 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnera83385f2006-04-27 05:01:07 +0000872
873 case ISD::SETOEQ:
874 case ISD::SETOGT:
875 case ISD::SETOGE:
876 case ISD::SETOLT:
877 case ISD::SETOLE:
878 case ISD::SETONE:
879 case ISD::SETO:
880 case ISD::SETUO:
881 case ISD::SETUEQ:
882 case ISD::SETUNE:
883 assert(!MVT::isInteger(N1.getValueType()) && "Illegal setcc for integer!");
884 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000885 }
886
Chris Lattner67255a12005-04-07 18:14:58 +0000887 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
888 uint64_t C2 = N2C->getValue();
889 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
890 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000891
Chris Lattnerc3aae252005-01-07 07:46:32 +0000892 // Sign extend the operands if required
893 if (ISD::isSignedIntSetCC(Cond)) {
894 C1 = N1C->getSignExtended();
895 C2 = N2C->getSignExtended();
896 }
897
898 switch (Cond) {
899 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000900 case ISD::SETEQ: return getConstant(C1 == C2, VT);
901 case ISD::SETNE: return getConstant(C1 != C2, VT);
902 case ISD::SETULT: return getConstant(C1 < C2, VT);
903 case ISD::SETUGT: return getConstant(C1 > C2, VT);
904 case ISD::SETULE: return getConstant(C1 <= C2, VT);
905 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
906 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
907 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
908 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
909 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000910 }
Chris Lattner24673922005-04-07 18:58:54 +0000911 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000912 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000913 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
914 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
915
916 // If the comparison constant has bits in the upper part, the
917 // zero-extended value could never match.
918 if (C2 & (~0ULL << InSize)) {
919 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
920 switch (Cond) {
921 case ISD::SETUGT:
922 case ISD::SETUGE:
923 case ISD::SETEQ: return getConstant(0, VT);
924 case ISD::SETULT:
925 case ISD::SETULE:
926 case ISD::SETNE: return getConstant(1, VT);
927 case ISD::SETGT:
928 case ISD::SETGE:
929 // True if the sign bit of C2 is set.
930 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
931 case ISD::SETLT:
932 case ISD::SETLE:
933 // True if the sign bit of C2 isn't set.
934 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
935 default:
936 break;
937 }
938 }
939
940 // Otherwise, we can perform the comparison with the low bits.
941 switch (Cond) {
942 case ISD::SETEQ:
943 case ISD::SETNE:
944 case ISD::SETUGT:
945 case ISD::SETUGE:
946 case ISD::SETULT:
947 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000948 return getSetCC(VT, N1.getOperand(0),
949 getConstant(C2, N1.getOperand(0).getValueType()),
950 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000951 default:
952 break; // todo, be more careful with signed comparisons
953 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000954 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
955 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
956 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
957 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
958 MVT::ValueType ExtDstTy = N1.getValueType();
959 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000960
Chris Lattner7b2880c2005-08-24 22:44:39 +0000961 // If the extended part has any inconsistent bits, it cannot ever
962 // compare equal. In other words, they have to be all ones or all
963 // zeros.
964 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000965 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000966 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
967 return getConstant(Cond == ISD::SETNE, VT);
968
969 // Otherwise, make this a use of a zext.
970 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000971 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000972 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000973 }
974
Chris Lattner67255a12005-04-07 18:14:58 +0000975 uint64_t MinVal, MaxVal;
976 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
977 if (ISD::isSignedIntSetCC(Cond)) {
978 MinVal = 1ULL << (OperandBitSize-1);
979 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
980 MaxVal = ~0ULL >> (65-OperandBitSize);
981 else
982 MaxVal = 0;
983 } else {
984 MinVal = 0;
985 MaxVal = ~0ULL >> (64-OperandBitSize);
986 }
987
988 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
989 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
990 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
991 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000992 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
993 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000994 }
995
996 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
997 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
998 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000999 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
1000 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +00001001 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001002
Nate Begeman72ea2812005-04-14 08:56:52 +00001003 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
1004 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +00001005
Nate Begeman72ea2812005-04-14 08:56:52 +00001006 // Canonicalize setgt X, Min --> setne X, Min
1007 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001008 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001009
Chris Lattner3b2c1d92005-04-12 00:28:49 +00001010 // If we have setult X, 1, turn it into seteq X, 0
1011 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001012 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
1013 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +00001014 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +00001015 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001016 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
1017 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +00001018
1019 // If we have "setcc X, C1", check to see if we can shrink the immediate
1020 // by changing cc.
1021
1022 // SETUGT X, SINTMAX -> SETLT X, 0
1023 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
1024 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001025 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +00001026
1027 // FIXME: Implement the rest of these.
1028
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001029
1030 // Fold bit comparisons when we can.
1031 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
1032 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
1033 if (ConstantSDNode *AndRHS =
1034 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1035 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1036 // Perform the xform if the AND RHS is a single bit.
1037 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1038 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001039 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001040 TLI.getShiftAmountTy()));
1041 }
1042 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
1043 // (X & 8) == 8 --> (X & 8) >> 3
1044 // Perform the xform if C2 is a single bit.
1045 if ((C2 & (C2-1)) == 0) {
1046 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001047 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001048 }
1049 }
1050 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001051 }
Chris Lattner67255a12005-04-07 18:14:58 +00001052 } else if (isa<ConstantSDNode>(N1.Val)) {
1053 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001054 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +00001055 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001056
1057 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1058 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1059 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001060
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 switch (Cond) {
1062 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001063 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1064 case ISD::SETNE: return getConstant(C1 != C2, VT);
1065 case ISD::SETLT: return getConstant(C1 < C2, VT);
1066 case ISD::SETGT: return getConstant(C1 > C2, VT);
1067 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1068 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001069 }
1070 } else {
1071 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001072 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001073 }
1074
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001075 // Could not fold it.
1076 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001077}
1078
Chris Lattnerc3aae252005-01-07 07:46:32 +00001079/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001080///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001081SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001082 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1083 if (!N) {
1084 N = new SDNode(Opcode, VT);
1085 AllNodes.push_back(N);
1086 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001087 return SDOperand(N, 0);
1088}
1089
Chris Lattnerc3aae252005-01-07 07:46:32 +00001090SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1091 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001092 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001093 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1095 uint64_t Val = C->getValue();
1096 switch (Opcode) {
1097 default: break;
1098 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001099 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1101 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001102 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1103 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001104 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001105 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001106 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001107 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001108 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001109 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001110 case ISD::BSWAP:
1111 switch(VT) {
1112 default: assert(0 && "Invalid bswap!"); break;
1113 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1114 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1115 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1116 }
1117 break;
1118 case ISD::CTPOP:
1119 switch(VT) {
1120 default: assert(0 && "Invalid ctpop!"); break;
1121 case MVT::i1: return getConstant(Val != 0, VT);
1122 case MVT::i8:
1123 Tmp1 = (unsigned)Val & 0xFF;
1124 return getConstant(CountPopulation_32(Tmp1), VT);
1125 case MVT::i16:
1126 Tmp1 = (unsigned)Val & 0xFFFF;
1127 return getConstant(CountPopulation_32(Tmp1), VT);
1128 case MVT::i32:
1129 return getConstant(CountPopulation_32((unsigned)Val), VT);
1130 case MVT::i64:
1131 return getConstant(CountPopulation_64(Val), VT);
1132 }
1133 case ISD::CTLZ:
1134 switch(VT) {
1135 default: assert(0 && "Invalid ctlz!"); break;
1136 case MVT::i1: return getConstant(Val == 0, VT);
1137 case MVT::i8:
1138 Tmp1 = (unsigned)Val & 0xFF;
1139 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1140 case MVT::i16:
1141 Tmp1 = (unsigned)Val & 0xFFFF;
1142 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1143 case MVT::i32:
1144 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1145 case MVT::i64:
1146 return getConstant(CountLeadingZeros_64(Val), VT);
1147 }
1148 case ISD::CTTZ:
1149 switch(VT) {
1150 default: assert(0 && "Invalid cttz!"); break;
1151 case MVT::i1: return getConstant(Val == 0, VT);
1152 case MVT::i8:
1153 Tmp1 = (unsigned)Val | 0x100;
1154 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1155 case MVT::i16:
1156 Tmp1 = (unsigned)Val | 0x10000;
1157 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1158 case MVT::i32:
1159 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1160 case MVT::i64:
1161 return getConstant(CountTrailingZeros_64(Val), VT);
1162 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001163 }
1164 }
1165
Chris Lattner94683772005-12-23 05:30:37 +00001166 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001167 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1168 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001169 case ISD::FNEG:
1170 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001171 case ISD::FABS:
1172 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001173 case ISD::FP_ROUND:
1174 case ISD::FP_EXTEND:
1175 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001176 case ISD::FP_TO_SINT:
1177 return getConstant((int64_t)C->getValue(), VT);
1178 case ISD::FP_TO_UINT:
1179 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001180 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001181 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001182 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001183 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001184 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001185 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001186 }
1187
1188 unsigned OpOpcode = Operand.Val->getOpcode();
1189 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001190 case ISD::TokenFactor:
1191 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001192 case ISD::SIGN_EXTEND:
1193 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001194 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001195 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1196 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1197 break;
1198 case ISD::ZERO_EXTEND:
1199 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001200 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001201 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001202 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001203 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001204 case ISD::ANY_EXTEND:
1205 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001206 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001207 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1208 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1209 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1210 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001211 case ISD::TRUNCATE:
1212 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001213 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001214 if (OpOpcode == ISD::TRUNCATE)
1215 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001216 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1217 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001218 // If the source is smaller than the dest, we still need an extend.
1219 if (Operand.Val->getOperand(0).getValueType() < VT)
1220 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1221 else if (Operand.Val->getOperand(0).getValueType() > VT)
1222 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1223 else
1224 return Operand.Val->getOperand(0);
1225 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001226 break;
Chris Lattner35481892005-12-23 00:16:34 +00001227 case ISD::BIT_CONVERT:
1228 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001229 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001230 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001231 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001232 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1233 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001234 if (OpOpcode == ISD::UNDEF)
1235 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001236 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001237 case ISD::SCALAR_TO_VECTOR:
1238 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1239 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1240 "Illegal SCALAR_TO_VECTOR node!");
1241 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001242 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001243 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1244 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001245 Operand.Val->getOperand(0));
1246 if (OpOpcode == ISD::FNEG) // --X -> X
1247 return Operand.Val->getOperand(0);
1248 break;
1249 case ISD::FABS:
1250 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1251 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1252 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001253 }
1254
Chris Lattner43247a12005-08-25 19:12:10 +00001255 SDNode *N;
1256 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1257 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001258 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001259 E = N = new SDNode(Opcode, Operand);
1260 } else {
1261 N = new SDNode(Opcode, Operand);
1262 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001263 N->setValueTypes(VT);
1264 AllNodes.push_back(N);
1265 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001266}
1267
Chris Lattner36019aa2005-04-18 03:48:41 +00001268
1269
Chris Lattnerc3aae252005-01-07 07:46:32 +00001270SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1271 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001272#ifndef NDEBUG
1273 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001274 case ISD::TokenFactor:
1275 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1276 N2.getValueType() == MVT::Other && "Invalid token factor!");
1277 break;
Chris Lattner76365122005-01-16 02:23:22 +00001278 case ISD::AND:
1279 case ISD::OR:
1280 case ISD::XOR:
1281 case ISD::UDIV:
1282 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001283 case ISD::MULHU:
1284 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001285 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1286 // fall through
1287 case ISD::ADD:
1288 case ISD::SUB:
1289 case ISD::MUL:
1290 case ISD::SDIV:
1291 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001292 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1293 // fall through.
1294 case ISD::FADD:
1295 case ISD::FSUB:
1296 case ISD::FMUL:
1297 case ISD::FDIV:
1298 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001299 assert(N1.getValueType() == N2.getValueType() &&
1300 N1.getValueType() == VT && "Binary operator types must match!");
1301 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001302 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1303 assert(N1.getValueType() == VT &&
1304 MVT::isFloatingPoint(N1.getValueType()) &&
1305 MVT::isFloatingPoint(N2.getValueType()) &&
1306 "Invalid FCOPYSIGN!");
1307 break;
Chris Lattner76365122005-01-16 02:23:22 +00001308 case ISD::SHL:
1309 case ISD::SRA:
1310 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001311 case ISD::ROTL:
1312 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001313 assert(VT == N1.getValueType() &&
1314 "Shift operators return type must be the same as their first arg");
1315 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001316 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001317 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001318 case ISD::FP_ROUND_INREG: {
1319 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1320 assert(VT == N1.getValueType() && "Not an inreg round!");
1321 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1322 "Cannot FP_ROUND_INREG integer types");
1323 assert(EVT <= VT && "Not rounding down!");
1324 break;
1325 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001326 case ISD::AssertSext:
1327 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001328 case ISD::SIGN_EXTEND_INREG: {
1329 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1330 assert(VT == N1.getValueType() && "Not an inreg extend!");
1331 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1332 "Cannot *_EXTEND_INREG FP types");
1333 assert(EVT <= VT && "Not extending!");
1334 }
1335
Chris Lattner76365122005-01-16 02:23:22 +00001336 default: break;
1337 }
1338#endif
1339
Chris Lattnerc3aae252005-01-07 07:46:32 +00001340 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1341 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1342 if (N1C) {
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001343 if (Opcode == ISD::SIGN_EXTEND_INREG) {
1344 int64_t Val = N1C->getValue();
1345 unsigned FromBits = MVT::getSizeInBits(cast<VTSDNode>(N2)->getVT());
1346 Val <<= 64-FromBits;
1347 Val >>= 64-FromBits;
1348 return getConstant(Val, VT);
1349 }
1350
Chris Lattnerc3aae252005-01-07 07:46:32 +00001351 if (N2C) {
1352 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1353 switch (Opcode) {
1354 case ISD::ADD: return getConstant(C1 + C2, VT);
1355 case ISD::SUB: return getConstant(C1 - C2, VT);
1356 case ISD::MUL: return getConstant(C1 * C2, VT);
1357 case ISD::UDIV:
1358 if (C2) return getConstant(C1 / C2, VT);
1359 break;
1360 case ISD::UREM :
1361 if (C2) return getConstant(C1 % C2, VT);
1362 break;
1363 case ISD::SDIV :
1364 if (C2) return getConstant(N1C->getSignExtended() /
1365 N2C->getSignExtended(), VT);
1366 break;
1367 case ISD::SREM :
1368 if (C2) return getConstant(N1C->getSignExtended() %
1369 N2C->getSignExtended(), VT);
1370 break;
1371 case ISD::AND : return getConstant(C1 & C2, VT);
1372 case ISD::OR : return getConstant(C1 | C2, VT);
1373 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001374 case ISD::SHL : return getConstant(C1 << C2, VT);
1375 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001376 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001377 case ISD::ROTL :
1378 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1379 VT);
1380 case ISD::ROTR :
1381 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1382 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001383 default: break;
1384 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001385 } else { // Cannonicalize constant to RHS if commutative
1386 if (isCommutativeBinOp(Opcode)) {
1387 std::swap(N1C, N2C);
1388 std::swap(N1, N2);
1389 }
1390 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001391 }
1392
1393 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1394 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001395 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001396 if (N2CFP) {
1397 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1398 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001399 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1400 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1401 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1402 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001403 if (C2) return getConstantFP(C1 / C2, VT);
1404 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001405 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001406 if (C2) return getConstantFP(fmod(C1, C2), VT);
1407 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001408 case ISD::FCOPYSIGN: {
1409 union {
1410 double F;
1411 uint64_t I;
1412 } u1;
1413 union {
1414 double F;
1415 int64_t I;
1416 } u2;
1417 u1.F = C1;
1418 u2.F = C2;
1419 if (u2.I < 0) // Sign bit of RHS set?
1420 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1421 else
1422 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1423 return getConstantFP(u1.F, VT);
1424 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001425 default: break;
1426 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001427 } else { // Cannonicalize constant to RHS if commutative
1428 if (isCommutativeBinOp(Opcode)) {
1429 std::swap(N1CFP, N2CFP);
1430 std::swap(N1, N2);
1431 }
1432 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001433 }
Chris Lattner62b57722006-04-20 05:39:12 +00001434
1435 // Canonicalize an UNDEF to the RHS, even over a constant.
1436 if (N1.getOpcode() == ISD::UNDEF) {
1437 if (isCommutativeBinOp(Opcode)) {
1438 std::swap(N1, N2);
1439 } else {
1440 switch (Opcode) {
1441 case ISD::FP_ROUND_INREG:
1442 case ISD::SIGN_EXTEND_INREG:
1443 case ISD::SUB:
1444 case ISD::FSUB:
1445 case ISD::FDIV:
1446 case ISD::FREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001447 case ISD::SRA:
Chris Lattner62b57722006-04-20 05:39:12 +00001448 return N1; // fold op(undef, arg2) -> undef
1449 case ISD::UDIV:
1450 case ISD::SDIV:
1451 case ISD::UREM:
1452 case ISD::SREM:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001453 case ISD::SRL:
1454 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001455 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1456 }
1457 }
1458 }
1459
Chris Lattnerb9ebacd2006-05-06 23:05:41 +00001460 // Fold a bunch of operators when the RHS is undef.
Chris Lattner62b57722006-04-20 05:39:12 +00001461 if (N2.getOpcode() == ISD::UNDEF) {
1462 switch (Opcode) {
1463 case ISD::ADD:
1464 case ISD::SUB:
1465 case ISD::FADD:
1466 case ISD::FSUB:
1467 case ISD::FMUL:
1468 case ISD::FDIV:
1469 case ISD::FREM:
1470 case ISD::UDIV:
1471 case ISD::SDIV:
1472 case ISD::UREM:
1473 case ISD::SREM:
1474 case ISD::XOR:
1475 return N2; // fold op(arg1, undef) -> undef
1476 case ISD::MUL:
1477 case ISD::AND:
Chris Lattner2cfd6742006-05-08 17:29:49 +00001478 case ISD::SRL:
1479 case ISD::SHL:
Chris Lattner62b57722006-04-20 05:39:12 +00001480 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1481 case ISD::OR:
1482 return getConstant(MVT::getIntVTBitMask(VT), VT);
Chris Lattner2cfd6742006-05-08 17:29:49 +00001483 case ISD::SRA:
1484 return N1;
Chris Lattner62b57722006-04-20 05:39:12 +00001485 }
1486 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001487
Chris Lattnerc3aae252005-01-07 07:46:32 +00001488 // Finally, fold operations that do not require constants.
1489 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001490 case ISD::FP_ROUND_INREG:
1491 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1492 break;
1493 case ISD::SIGN_EXTEND_INREG: {
1494 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1495 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001496 break;
1497 }
1498
Nate Begemaneea805e2005-04-13 21:23:31 +00001499 // FIXME: figure out how to safely handle things like
1500 // int foo(int x) { return 1 << (x & 255); }
1501 // int bar() { return foo(256); }
1502#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001503 case ISD::SHL:
1504 case ISD::SRL:
1505 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001506 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001507 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001508 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001509 else if (N2.getOpcode() == ISD::AND)
1510 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1511 // If the and is only masking out bits that cannot effect the shift,
1512 // eliminate the and.
1513 unsigned NumBits = MVT::getSizeInBits(VT);
1514 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1515 return getNode(Opcode, VT, N1, N2.getOperand(0));
1516 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001517 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001518#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001519 }
1520
Chris Lattner27e9b412005-05-11 18:57:39 +00001521 // Memoize this node if possible.
1522 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001523 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001524 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1525 if (BON) return SDOperand(BON, 0);
1526
1527 BON = N = new SDNode(Opcode, N1, N2);
1528 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001529 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001530 }
1531
Chris Lattner3e011362005-05-14 07:45:46 +00001532 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001533 AllNodes.push_back(N);
1534 return SDOperand(N, 0);
1535}
1536
Chris Lattnerc3aae252005-01-07 07:46:32 +00001537SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1538 SDOperand N1, SDOperand N2, SDOperand N3) {
1539 // Perform various simplifications.
1540 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1541 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
Chris Lattnerf1343c12006-05-12 18:04:28 +00001542 //ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001543 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001544 case ISD::SETCC: {
1545 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001546 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001547 if (Simp.Val) return Simp;
1548 break;
1549 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001550 case ISD::SELECT:
1551 if (N1C)
1552 if (N1C->getValue())
1553 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001554 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001555 return N3; // select false, X, Y -> Y
1556
1557 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001558 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001559 case ISD::BRCOND:
1560 if (N2C)
1561 if (N2C->getValue()) // Unconditional branch
1562 return getNode(ISD::BR, MVT::Other, N1, N3);
1563 else
1564 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001565 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001566 case ISD::VECTOR_SHUFFLE:
1567 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1568 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1569 N3.getOpcode() == ISD::BUILD_VECTOR &&
1570 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1571 "Illegal VECTOR_SHUFFLE node!");
1572 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001573 }
1574
Chris Lattner385328c2005-05-14 07:42:29 +00001575 std::vector<SDOperand> Ops;
1576 Ops.reserve(3);
1577 Ops.push_back(N1);
1578 Ops.push_back(N2);
1579 Ops.push_back(N3);
1580
Chris Lattner43247a12005-08-25 19:12:10 +00001581 // Memoize node if it doesn't produce a flag.
1582 SDNode *N;
1583 if (VT != MVT::Flag) {
1584 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1585 if (E) return SDOperand(E, 0);
1586 E = N = new SDNode(Opcode, N1, N2, N3);
1587 } else {
1588 N = new SDNode(Opcode, N1, N2, N3);
1589 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001590 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001591 AllNodes.push_back(N);
1592 return SDOperand(N, 0);
1593}
1594
1595SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001596 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001597 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001598 std::vector<SDOperand> Ops;
1599 Ops.reserve(4);
1600 Ops.push_back(N1);
1601 Ops.push_back(N2);
1602 Ops.push_back(N3);
1603 Ops.push_back(N4);
1604 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001605}
1606
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001607SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1608 SDOperand N1, SDOperand N2, SDOperand N3,
1609 SDOperand N4, SDOperand N5) {
1610 std::vector<SDOperand> Ops;
1611 Ops.reserve(5);
1612 Ops.push_back(N1);
1613 Ops.push_back(N2);
1614 Ops.push_back(N3);
1615 Ops.push_back(N4);
1616 Ops.push_back(N5);
1617 return getNode(Opcode, VT, Ops);
1618}
1619
Evan Cheng7038daf2005-12-10 00:37:58 +00001620SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1621 SDOperand Chain, SDOperand Ptr,
1622 SDOperand SV) {
1623 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1624 if (N) return SDOperand(N, 0);
1625 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1626
1627 // Loads have a token chain.
1628 setNodeValueTypes(N, VT, MVT::Other);
1629 AllNodes.push_back(N);
1630 return SDOperand(N, 0);
1631}
1632
1633SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1634 SDOperand Chain, SDOperand Ptr,
1635 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001636 std::vector<SDOperand> Ops;
1637 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001638 Ops.push_back(Chain);
1639 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001640 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001641 Ops.push_back(getConstant(Count, MVT::i32));
1642 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001643 std::vector<MVT::ValueType> VTs;
1644 VTs.reserve(2);
1645 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1646 return getNode(ISD::VLOAD, VTs, Ops);
1647}
1648
1649SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1650 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1651 MVT::ValueType EVT) {
1652 std::vector<SDOperand> Ops;
1653 Ops.reserve(4);
1654 Ops.push_back(Chain);
1655 Ops.push_back(Ptr);
1656 Ops.push_back(SV);
1657 Ops.push_back(getValueType(EVT));
1658 std::vector<MVT::ValueType> VTs;
1659 VTs.reserve(2);
1660 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1661 return getNode(Opcode, VTs, Ops);
1662}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001663
Chris Lattner0437cdd2005-05-09 04:14:13 +00001664SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001665 assert((!V || isa<PointerType>(V->getType())) &&
1666 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001667 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1668 if (N) return SDOperand(N, 0);
1669
1670 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001671 AllNodes.push_back(N);
1672 return SDOperand(N, 0);
1673}
1674
Nate Begemanacc398c2006-01-25 18:21:52 +00001675SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1676 SDOperand Chain, SDOperand Ptr,
1677 SDOperand SV) {
1678 std::vector<SDOperand> Ops;
1679 Ops.reserve(3);
1680 Ops.push_back(Chain);
1681 Ops.push_back(Ptr);
1682 Ops.push_back(SV);
1683 std::vector<MVT::ValueType> VTs;
1684 VTs.reserve(2);
1685 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1686 return getNode(ISD::VAARG, VTs, Ops);
1687}
1688
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001689SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001690 std::vector<SDOperand> &Ops) {
1691 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001692 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001693 case 1: return getNode(Opcode, VT, Ops[0]);
1694 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1695 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001696 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001697 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001698
Chris Lattneref847df2005-04-09 03:27:28 +00001699 switch (Opcode) {
1700 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001701 case ISD::TRUNCSTORE: {
1702 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1703 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1704#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1705 // If this is a truncating store of a constant, convert to the desired type
1706 // and store it instead.
1707 if (isa<Constant>(Ops[0])) {
1708 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1709 if (isa<Constant>(Op))
1710 N1 = Op;
1711 }
1712 // Also for ConstantFP?
1713#endif
1714 if (Ops[0].getValueType() == EVT) // Normal store?
1715 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1716 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1717 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1718 "Can't do FP-INT conversion!");
1719 break;
1720 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001721 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001722 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001723 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1724 "LHS and RHS of condition must have same type!");
1725 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1726 "True and False arms of SelectCC must have same type!");
1727 assert(Ops[2].getValueType() == VT &&
1728 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001729 break;
1730 }
1731 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001732 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001733 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1734 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001735 break;
1736 }
Chris Lattneref847df2005-04-09 03:27:28 +00001737 }
1738
Chris Lattner385328c2005-05-14 07:42:29 +00001739 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001740 SDNode *N;
1741 if (VT != MVT::Flag) {
1742 SDNode *&E =
1743 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1744 if (E) return SDOperand(E, 0);
1745 E = N = new SDNode(Opcode, Ops);
1746 } else {
1747 N = new SDNode(Opcode, Ops);
1748 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001749 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001750 AllNodes.push_back(N);
1751 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001752}
1753
Chris Lattner89c34632005-05-14 06:20:26 +00001754SDOperand SelectionDAG::getNode(unsigned Opcode,
1755 std::vector<MVT::ValueType> &ResultTys,
1756 std::vector<SDOperand> &Ops) {
1757 if (ResultTys.size() == 1)
1758 return getNode(Opcode, ResultTys[0], Ops);
1759
Chris Lattner5f056bf2005-07-10 01:55:33 +00001760 switch (Opcode) {
1761 case ISD::EXTLOAD:
1762 case ISD::SEXTLOAD:
1763 case ISD::ZEXTLOAD: {
1764 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1765 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1766 // If they are asking for an extending load from/to the same thing, return a
1767 // normal load.
1768 if (ResultTys[0] == EVT)
1769 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001770 if (MVT::isVector(ResultTys[0])) {
1771 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1772 "Invalid vector extload!");
1773 } else {
1774 assert(EVT < ResultTys[0] &&
1775 "Should only be an extending load, not truncating!");
1776 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001777 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001778 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001779 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1780 "Cannot convert from FP to Int or Int -> FP!");
1781 break;
1782 }
1783
Chris Lattnere89083a2005-05-14 07:25:05 +00001784 // FIXME: figure out how to safely handle things like
1785 // int foo(int x) { return 1 << (x & 255); }
1786 // int bar() { return foo(256); }
1787#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001788 case ISD::SRA_PARTS:
1789 case ISD::SRL_PARTS:
1790 case ISD::SHL_PARTS:
1791 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001792 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001793 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1794 else if (N3.getOpcode() == ISD::AND)
1795 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1796 // If the and is only masking out bits that cannot effect the shift,
1797 // eliminate the and.
1798 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1799 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1800 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1801 }
1802 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001803#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001804 }
Chris Lattner89c34632005-05-14 06:20:26 +00001805
Chris Lattner43247a12005-08-25 19:12:10 +00001806 // Memoize the node unless it returns a flag.
1807 SDNode *N;
1808 if (ResultTys.back() != MVT::Flag) {
1809 SDNode *&E =
1810 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1811 if (E) return SDOperand(E, 0);
1812 E = N = new SDNode(Opcode, Ops);
1813 } else {
1814 N = new SDNode(Opcode, Ops);
1815 }
Chris Lattnera3255112005-11-08 23:30:28 +00001816 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001817 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001818 return SDOperand(N, 0);
1819}
1820
Chris Lattnera3255112005-11-08 23:30:28 +00001821void SelectionDAG::setNodeValueTypes(SDNode *N,
1822 std::vector<MVT::ValueType> &RetVals) {
1823 switch (RetVals.size()) {
1824 case 0: return;
1825 case 1: N->setValueTypes(RetVals[0]); return;
1826 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1827 default: break;
1828 }
1829
1830 std::list<std::vector<MVT::ValueType> >::iterator I =
1831 std::find(VTList.begin(), VTList.end(), RetVals);
1832 if (I == VTList.end()) {
1833 VTList.push_front(RetVals);
1834 I = VTList.begin();
1835 }
1836
1837 N->setValueTypes(&(*I)[0], I->size());
1838}
1839
1840void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1841 MVT::ValueType VT2) {
1842 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1843 E = VTList.end(); I != E; ++I) {
1844 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1845 N->setValueTypes(&(*I)[0], 2);
1846 return;
1847 }
1848 }
1849 std::vector<MVT::ValueType> V;
1850 V.push_back(VT1);
1851 V.push_back(VT2);
1852 VTList.push_front(V);
1853 N->setValueTypes(&(*VTList.begin())[0], 2);
1854}
1855
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001856/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1857/// specified operands. If the resultant node already exists in the DAG,
1858/// this does not modify the specified node, instead it returns the node that
1859/// already exists. If the resultant node does not exist in the DAG, the
1860/// input node is returned. As a degenerate case, if you specify the same
1861/// input operands as the node already has, the input node is returned.
1862SDOperand SelectionDAG::
1863UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1864 SDNode *N = InN.Val;
1865 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1866
1867 // Check to see if there is no change.
1868 if (Op == N->getOperand(0)) return InN;
1869
1870 // See if the modified node already exists.
1871 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1872 if (NewSlot && *NewSlot)
1873 return SDOperand(*NewSlot, InN.ResNo);
1874
1875 // Nope it doesn't. Remove the node from it's current place in the maps.
1876 if (NewSlot)
1877 RemoveNodeFromCSEMaps(N);
1878
1879 // Now we update the operands.
1880 N->OperandList[0].Val->removeUser(N);
1881 Op.Val->addUser(N);
1882 N->OperandList[0] = Op;
1883
1884 // If this gets put into a CSE map, add it.
1885 if (NewSlot) *NewSlot = N;
1886 return InN;
1887}
1888
1889SDOperand SelectionDAG::
1890UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1891 SDNode *N = InN.Val;
1892 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1893
1894 // Check to see if there is no change.
1895 bool AnyChange = false;
1896 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1897 return InN; // No operands changed, just return the input node.
1898
1899 // See if the modified node already exists.
1900 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1901 if (NewSlot && *NewSlot)
1902 return SDOperand(*NewSlot, InN.ResNo);
1903
1904 // Nope it doesn't. Remove the node from it's current place in the maps.
1905 if (NewSlot)
1906 RemoveNodeFromCSEMaps(N);
1907
1908 // Now we update the operands.
1909 if (N->OperandList[0] != Op1) {
1910 N->OperandList[0].Val->removeUser(N);
1911 Op1.Val->addUser(N);
1912 N->OperandList[0] = Op1;
1913 }
1914 if (N->OperandList[1] != Op2) {
1915 N->OperandList[1].Val->removeUser(N);
1916 Op2.Val->addUser(N);
1917 N->OperandList[1] = Op2;
1918 }
1919
1920 // If this gets put into a CSE map, add it.
1921 if (NewSlot) *NewSlot = N;
1922 return InN;
1923}
1924
1925SDOperand SelectionDAG::
1926UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1927 std::vector<SDOperand> Ops;
1928 Ops.push_back(Op1);
1929 Ops.push_back(Op2);
1930 Ops.push_back(Op3);
1931 return UpdateNodeOperands(N, Ops);
1932}
1933
1934SDOperand SelectionDAG::
1935UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1936 SDOperand Op3, SDOperand Op4) {
1937 std::vector<SDOperand> Ops;
1938 Ops.push_back(Op1);
1939 Ops.push_back(Op2);
1940 Ops.push_back(Op3);
1941 Ops.push_back(Op4);
1942 return UpdateNodeOperands(N, Ops);
1943}
1944
1945SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001946UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1947 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1948 std::vector<SDOperand> Ops;
1949 Ops.push_back(Op1);
1950 Ops.push_back(Op2);
1951 Ops.push_back(Op3);
1952 Ops.push_back(Op4);
1953 Ops.push_back(Op5);
1954 return UpdateNodeOperands(N, Ops);
1955}
1956
1957
1958SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001959UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1960 SDNode *N = InN.Val;
1961 assert(N->getNumOperands() == Ops.size() &&
1962 "Update with wrong number of operands");
1963
1964 // Check to see if there is no change.
1965 unsigned NumOps = Ops.size();
1966 bool AnyChange = false;
1967 for (unsigned i = 0; i != NumOps; ++i) {
1968 if (Ops[i] != N->getOperand(i)) {
1969 AnyChange = true;
1970 break;
1971 }
1972 }
1973
1974 // No operands changed, just return the input node.
1975 if (!AnyChange) return InN;
1976
1977 // See if the modified node already exists.
1978 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1979 if (NewSlot && *NewSlot)
1980 return SDOperand(*NewSlot, InN.ResNo);
1981
1982 // Nope it doesn't. Remove the node from it's current place in the maps.
1983 if (NewSlot)
1984 RemoveNodeFromCSEMaps(N);
1985
1986 // Now we update the operands.
1987 for (unsigned i = 0; i != NumOps; ++i) {
1988 if (N->OperandList[i] != Ops[i]) {
1989 N->OperandList[i].Val->removeUser(N);
1990 Ops[i].Val->addUser(N);
1991 N->OperandList[i] = Ops[i];
1992 }
1993 }
1994
1995 // If this gets put into a CSE map, add it.
1996 if (NewSlot) *NewSlot = N;
1997 return InN;
1998}
1999
2000
2001
Chris Lattner149c58c2005-08-16 18:17:10 +00002002
2003/// SelectNodeTo - These are used for target selectors to *mutate* the
2004/// specified node to have the specified return type, Target opcode, and
2005/// operands. Note that target opcodes are stored as
2006/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002007///
2008/// Note that SelectNodeTo returns the resultant node. If there is already a
2009/// node of the specified opcode and operands, it returns that node instead of
2010/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00002011SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2012 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002013 // If an identical node already exists, use it.
2014 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
2015 if (ON) return SDOperand(ON, 0);
2016
Chris Lattner7651fa42005-08-24 23:00:29 +00002017 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002018
Chris Lattner7651fa42005-08-24 23:00:29 +00002019 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2020 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002021
2022 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002023 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00002024}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002025
Chris Lattnereb19e402005-11-30 22:45:14 +00002026SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2027 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002028 // If an identical node already exists, use it.
2029 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2030 std::make_pair(Op1, VT))];
2031 if (ON) return SDOperand(ON, 0);
2032
Chris Lattner149c58c2005-08-16 18:17:10 +00002033 RemoveNodeFromCSEMaps(N);
2034 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2035 N->setValueTypes(VT);
2036 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002037
2038 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002039 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002040}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002041
Chris Lattnereb19e402005-11-30 22:45:14 +00002042SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2043 MVT::ValueType VT, SDOperand Op1,
2044 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002045 // If an identical node already exists, use it.
2046 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2047 std::make_pair(Op1, Op2))];
2048 if (ON) return SDOperand(ON, 0);
2049
Chris Lattner149c58c2005-08-16 18:17:10 +00002050 RemoveNodeFromCSEMaps(N);
2051 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2052 N->setValueTypes(VT);
2053 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002054
2055 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002056 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002057}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002058
Chris Lattnereb19e402005-11-30 22:45:14 +00002059SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2060 MVT::ValueType VT, SDOperand Op1,
2061 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002062 // If an identical node already exists, use it.
2063 std::vector<SDOperand> OpList;
2064 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2065 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2066 std::make_pair(VT, OpList))];
2067 if (ON) return SDOperand(ON, 0);
2068
Chris Lattner149c58c2005-08-16 18:17:10 +00002069 RemoveNodeFromCSEMaps(N);
2070 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2071 N->setValueTypes(VT);
2072 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002073
2074 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002075 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002076}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002077
Chris Lattnereb19e402005-11-30 22:45:14 +00002078SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2079 MVT::ValueType VT, SDOperand Op1,
2080 SDOperand Op2, SDOperand Op3,
2081 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002082 // If an identical node already exists, use it.
2083 std::vector<SDOperand> OpList;
2084 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2085 OpList.push_back(Op4);
2086 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2087 std::make_pair(VT, OpList))];
2088 if (ON) return SDOperand(ON, 0);
2089
Nate Begeman294a0a12005-08-18 07:30:15 +00002090 RemoveNodeFromCSEMaps(N);
2091 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2092 N->setValueTypes(VT);
2093 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002094
2095 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002096 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002097}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002098
Chris Lattnereb19e402005-11-30 22:45:14 +00002099SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2100 MVT::ValueType VT, SDOperand Op1,
2101 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2102 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002103 // If an identical node already exists, use it.
2104 std::vector<SDOperand> OpList;
2105 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2106 OpList.push_back(Op4); OpList.push_back(Op5);
2107 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2108 std::make_pair(VT, OpList))];
2109 if (ON) return SDOperand(ON, 0);
2110
Chris Lattner6b09a292005-08-21 18:49:33 +00002111 RemoveNodeFromCSEMaps(N);
2112 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2113 N->setValueTypes(VT);
2114 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002115
2116 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002117 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002118}
Chris Lattner149c58c2005-08-16 18:17:10 +00002119
Chris Lattnereb19e402005-11-30 22:45:14 +00002120SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2121 MVT::ValueType VT, SDOperand Op1,
2122 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2123 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002124 // If an identical node already exists, use it.
2125 std::vector<SDOperand> OpList;
2126 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2127 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2128 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2129 std::make_pair(VT, OpList))];
2130 if (ON) return SDOperand(ON, 0);
2131
Evan Cheng61ca74b2005-11-30 02:04:11 +00002132 RemoveNodeFromCSEMaps(N);
2133 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2134 N->setValueTypes(VT);
2135 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002136
2137 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002138 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002139}
2140
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002141SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2142 MVT::ValueType VT, SDOperand Op1,
2143 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2144 SDOperand Op5, SDOperand Op6,
2145 SDOperand Op7) {
2146 // If an identical node already exists, use it.
2147 std::vector<SDOperand> OpList;
2148 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2149 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2150 OpList.push_back(Op7);
2151 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2152 std::make_pair(VT, OpList))];
2153 if (ON) return SDOperand(ON, 0);
2154
2155 RemoveNodeFromCSEMaps(N);
2156 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2157 N->setValueTypes(VT);
2158 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2159
2160 ON = N; // Memoize the new node.
2161 return SDOperand(N, 0);
2162}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002163SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2164 MVT::ValueType VT, SDOperand Op1,
2165 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2166 SDOperand Op5, SDOperand Op6,
2167 SDOperand Op7, SDOperand Op8) {
2168 // If an identical node already exists, use it.
2169 std::vector<SDOperand> OpList;
2170 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2171 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2172 OpList.push_back(Op7); OpList.push_back(Op8);
2173 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2174 std::make_pair(VT, OpList))];
2175 if (ON) return SDOperand(ON, 0);
2176
2177 RemoveNodeFromCSEMaps(N);
2178 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2179 N->setValueTypes(VT);
2180 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2181
2182 ON = N; // Memoize the new node.
2183 return SDOperand(N, 0);
2184}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002185
Chris Lattnereb19e402005-11-30 22:45:14 +00002186SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2187 MVT::ValueType VT1, MVT::ValueType VT2,
2188 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002189 // If an identical node already exists, use it.
2190 std::vector<SDOperand> OpList;
2191 OpList.push_back(Op1); OpList.push_back(Op2);
2192 std::vector<MVT::ValueType> VTList;
2193 VTList.push_back(VT1); VTList.push_back(VT2);
2194 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2195 std::make_pair(VTList, OpList))];
2196 if (ON) return SDOperand(ON, 0);
2197
Chris Lattner0fb094f2005-11-19 01:44:53 +00002198 RemoveNodeFromCSEMaps(N);
2199 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2200 setNodeValueTypes(N, VT1, VT2);
2201 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002202
2203 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002204 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002205}
2206
Chris Lattnereb19e402005-11-30 22:45:14 +00002207SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2208 MVT::ValueType VT1, MVT::ValueType VT2,
2209 SDOperand Op1, SDOperand Op2,
2210 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002211 // If an identical node already exists, use it.
2212 std::vector<SDOperand> OpList;
2213 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2214 std::vector<MVT::ValueType> VTList;
2215 VTList.push_back(VT1); VTList.push_back(VT2);
2216 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2217 std::make_pair(VTList, OpList))];
2218 if (ON) return SDOperand(ON, 0);
2219
Chris Lattner0fb094f2005-11-19 01:44:53 +00002220 RemoveNodeFromCSEMaps(N);
2221 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2222 setNodeValueTypes(N, VT1, VT2);
2223 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002224
2225 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002226 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002227}
2228
Chris Lattnereb19e402005-11-30 22:45:14 +00002229SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2230 MVT::ValueType VT1, MVT::ValueType VT2,
2231 SDOperand Op1, SDOperand Op2,
2232 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002233 // If an identical node already exists, use it.
2234 std::vector<SDOperand> OpList;
2235 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2236 OpList.push_back(Op4);
2237 std::vector<MVT::ValueType> VTList;
2238 VTList.push_back(VT1); VTList.push_back(VT2);
2239 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2240 std::make_pair(VTList, OpList))];
2241 if (ON) return SDOperand(ON, 0);
2242
Chris Lattner0fb094f2005-11-19 01:44:53 +00002243 RemoveNodeFromCSEMaps(N);
2244 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2245 setNodeValueTypes(N, VT1, VT2);
2246 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002247
2248 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002249 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002250}
2251
Chris Lattnereb19e402005-11-30 22:45:14 +00002252SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2253 MVT::ValueType VT1, MVT::ValueType VT2,
2254 SDOperand Op1, SDOperand Op2,
2255 SDOperand Op3, SDOperand Op4,
2256 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002257 // If an identical node already exists, use it.
2258 std::vector<SDOperand> OpList;
2259 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2260 OpList.push_back(Op4); OpList.push_back(Op5);
2261 std::vector<MVT::ValueType> VTList;
2262 VTList.push_back(VT1); VTList.push_back(VT2);
2263 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2264 std::make_pair(VTList, OpList))];
2265 if (ON) return SDOperand(ON, 0);
2266
Chris Lattner0fb094f2005-11-19 01:44:53 +00002267 RemoveNodeFromCSEMaps(N);
2268 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2269 setNodeValueTypes(N, VT1, VT2);
2270 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002271
2272 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002273 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002274}
2275
Evan Cheng6ae46c42006-02-09 07:15:23 +00002276/// getTargetNode - These are used for target selectors to create a new node
2277/// with specified return type(s), target opcode, and operands.
2278///
2279/// Note that getTargetNode returns the resultant node. If there is already a
2280/// node of the specified opcode and operands, it returns that node instead of
2281/// the current one.
2282SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2283 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2284}
2285SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2286 SDOperand Op1) {
2287 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2288}
2289SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2290 SDOperand Op1, SDOperand Op2) {
2291 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2292}
2293SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2294 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2295 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2296}
2297SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2298 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2299 SDOperand Op4) {
2300 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2301}
2302SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2303 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2304 SDOperand Op4, SDOperand Op5) {
2305 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2306}
2307SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2308 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2309 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2310 std::vector<SDOperand> Ops;
2311 Ops.reserve(6);
2312 Ops.push_back(Op1);
2313 Ops.push_back(Op2);
2314 Ops.push_back(Op3);
2315 Ops.push_back(Op4);
2316 Ops.push_back(Op5);
2317 Ops.push_back(Op6);
2318 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2319}
2320SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2321 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2322 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2323 SDOperand Op7) {
2324 std::vector<SDOperand> Ops;
2325 Ops.reserve(7);
2326 Ops.push_back(Op1);
2327 Ops.push_back(Op2);
2328 Ops.push_back(Op3);
2329 Ops.push_back(Op4);
2330 Ops.push_back(Op5);
2331 Ops.push_back(Op6);
2332 Ops.push_back(Op7);
2333 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2334}
2335SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2336 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2337 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2338 SDOperand Op7, SDOperand Op8) {
2339 std::vector<SDOperand> Ops;
2340 Ops.reserve(8);
2341 Ops.push_back(Op1);
2342 Ops.push_back(Op2);
2343 Ops.push_back(Op3);
2344 Ops.push_back(Op4);
2345 Ops.push_back(Op5);
2346 Ops.push_back(Op6);
2347 Ops.push_back(Op7);
2348 Ops.push_back(Op8);
2349 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2350}
2351SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2352 std::vector<SDOperand> &Ops) {
2353 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2354}
2355SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2356 MVT::ValueType VT2, SDOperand Op1) {
2357 std::vector<MVT::ValueType> ResultTys;
2358 ResultTys.push_back(VT1);
2359 ResultTys.push_back(VT2);
2360 std::vector<SDOperand> Ops;
2361 Ops.push_back(Op1);
2362 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2363}
2364SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2365 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2366 std::vector<MVT::ValueType> ResultTys;
2367 ResultTys.push_back(VT1);
2368 ResultTys.push_back(VT2);
2369 std::vector<SDOperand> Ops;
2370 Ops.push_back(Op1);
2371 Ops.push_back(Op2);
2372 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2373}
2374SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2375 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2376 SDOperand Op3) {
2377 std::vector<MVT::ValueType> ResultTys;
2378 ResultTys.push_back(VT1);
2379 ResultTys.push_back(VT2);
2380 std::vector<SDOperand> Ops;
2381 Ops.push_back(Op1);
2382 Ops.push_back(Op2);
2383 Ops.push_back(Op3);
2384 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2385}
2386SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2387 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2388 SDOperand Op3, SDOperand Op4) {
2389 std::vector<MVT::ValueType> ResultTys;
2390 ResultTys.push_back(VT1);
2391 ResultTys.push_back(VT2);
2392 std::vector<SDOperand> Ops;
2393 Ops.push_back(Op1);
2394 Ops.push_back(Op2);
2395 Ops.push_back(Op3);
2396 Ops.push_back(Op4);
2397 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2398}
2399SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2400 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2401 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2402 std::vector<MVT::ValueType> ResultTys;
2403 ResultTys.push_back(VT1);
2404 ResultTys.push_back(VT2);
2405 std::vector<SDOperand> Ops;
2406 Ops.push_back(Op1);
2407 Ops.push_back(Op2);
2408 Ops.push_back(Op3);
2409 Ops.push_back(Op4);
2410 Ops.push_back(Op5);
2411 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2412}
2413SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2414 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2415 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2416 SDOperand Op6) {
2417 std::vector<MVT::ValueType> ResultTys;
2418 ResultTys.push_back(VT1);
2419 ResultTys.push_back(VT2);
2420 std::vector<SDOperand> Ops;
2421 Ops.push_back(Op1);
2422 Ops.push_back(Op2);
2423 Ops.push_back(Op3);
2424 Ops.push_back(Op4);
2425 Ops.push_back(Op5);
2426 Ops.push_back(Op6);
2427 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2428}
2429SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2430 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2431 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2432 SDOperand Op6, SDOperand Op7) {
2433 std::vector<MVT::ValueType> ResultTys;
2434 ResultTys.push_back(VT1);
2435 ResultTys.push_back(VT2);
2436 std::vector<SDOperand> Ops;
2437 Ops.push_back(Op1);
2438 Ops.push_back(Op2);
2439 Ops.push_back(Op3);
2440 Ops.push_back(Op4);
2441 Ops.push_back(Op5);
2442 Ops.push_back(Op6);
2443 Ops.push_back(Op7);
2444 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2445}
2446SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2447 MVT::ValueType VT2, MVT::ValueType VT3,
2448 SDOperand Op1, SDOperand Op2) {
2449 std::vector<MVT::ValueType> ResultTys;
2450 ResultTys.push_back(VT1);
2451 ResultTys.push_back(VT2);
2452 ResultTys.push_back(VT3);
2453 std::vector<SDOperand> Ops;
2454 Ops.push_back(Op1);
2455 Ops.push_back(Op2);
2456 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2457}
2458SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2459 MVT::ValueType VT2, MVT::ValueType VT3,
2460 SDOperand Op1, SDOperand Op2,
2461 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2462 std::vector<MVT::ValueType> ResultTys;
2463 ResultTys.push_back(VT1);
2464 ResultTys.push_back(VT2);
2465 ResultTys.push_back(VT3);
2466 std::vector<SDOperand> Ops;
2467 Ops.push_back(Op1);
2468 Ops.push_back(Op2);
2469 Ops.push_back(Op3);
2470 Ops.push_back(Op4);
2471 Ops.push_back(Op5);
2472 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2473}
2474SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2475 MVT::ValueType VT2, MVT::ValueType VT3,
2476 SDOperand Op1, SDOperand Op2,
2477 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2478 SDOperand Op6) {
2479 std::vector<MVT::ValueType> ResultTys;
2480 ResultTys.push_back(VT1);
2481 ResultTys.push_back(VT2);
2482 ResultTys.push_back(VT3);
2483 std::vector<SDOperand> Ops;
2484 Ops.push_back(Op1);
2485 Ops.push_back(Op2);
2486 Ops.push_back(Op3);
2487 Ops.push_back(Op4);
2488 Ops.push_back(Op5);
2489 Ops.push_back(Op6);
2490 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2491}
2492SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2493 MVT::ValueType VT2, MVT::ValueType VT3,
2494 SDOperand Op1, SDOperand Op2,
2495 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2496 SDOperand Op6, SDOperand Op7) {
2497 std::vector<MVT::ValueType> ResultTys;
2498 ResultTys.push_back(VT1);
2499 ResultTys.push_back(VT2);
2500 ResultTys.push_back(VT3);
2501 std::vector<SDOperand> Ops;
2502 Ops.push_back(Op1);
2503 Ops.push_back(Op2);
2504 Ops.push_back(Op3);
2505 Ops.push_back(Op4);
2506 Ops.push_back(Op5);
2507 Ops.push_back(Op6);
2508 Ops.push_back(Op7);
2509 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2510}
2511SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2512 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2513 std::vector<MVT::ValueType> ResultTys;
2514 ResultTys.push_back(VT1);
2515 ResultTys.push_back(VT2);
2516 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2517}
2518
Chris Lattner0fb094f2005-11-19 01:44:53 +00002519// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002520/// This can cause recursive merging of nodes in the DAG.
2521///
Chris Lattner8b52f212005-08-26 18:36:28 +00002522/// This version assumes From/To have a single result value.
2523///
Chris Lattner1e111c72005-09-07 05:37:01 +00002524void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2525 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002526 SDNode *From = FromN.Val, *To = ToN.Val;
2527 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2528 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002529 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002530
Chris Lattner8b8749f2005-08-17 19:00:20 +00002531 while (!From->use_empty()) {
2532 // Process users until they are all gone.
2533 SDNode *U = *From->use_begin();
2534
2535 // This node is about to morph, remove its old self from the CSE maps.
2536 RemoveNodeFromCSEMaps(U);
2537
Chris Lattner65113b22005-11-08 22:07:03 +00002538 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2539 I != E; ++I)
2540 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002541 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002542 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002543 To->addUser(U);
2544 }
2545
2546 // Now that we have modified U, add it back to the CSE maps. If it already
2547 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002548 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2549 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002550 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002551 if (Deleted) Deleted->push_back(U);
2552 DeleteNodeNotInCSEMaps(U);
2553 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002554 }
2555}
2556
2557/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2558/// This can cause recursive merging of nodes in the DAG.
2559///
2560/// This version assumes From/To have matching types and numbers of result
2561/// values.
2562///
Chris Lattner1e111c72005-09-07 05:37:01 +00002563void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2564 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002565 assert(From != To && "Cannot replace uses of with self");
2566 assert(From->getNumValues() == To->getNumValues() &&
2567 "Cannot use this version of ReplaceAllUsesWith!");
2568 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002569 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002570 return;
2571 }
2572
2573 while (!From->use_empty()) {
2574 // Process users until they are all gone.
2575 SDNode *U = *From->use_begin();
2576
2577 // This node is about to morph, remove its old self from the CSE maps.
2578 RemoveNodeFromCSEMaps(U);
2579
Chris Lattner65113b22005-11-08 22:07:03 +00002580 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2581 I != E; ++I)
2582 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002583 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002584 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002585 To->addUser(U);
2586 }
2587
2588 // Now that we have modified U, add it back to the CSE maps. If it already
2589 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002590 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2591 ReplaceAllUsesWith(U, Existing, Deleted);
2592 // U is now dead.
2593 if (Deleted) Deleted->push_back(U);
2594 DeleteNodeNotInCSEMaps(U);
2595 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002596 }
2597}
2598
Chris Lattner8b52f212005-08-26 18:36:28 +00002599/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2600/// This can cause recursive merging of nodes in the DAG.
2601///
2602/// This version can replace From with any result values. To must match the
2603/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002604void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002605 const std::vector<SDOperand> &To,
2606 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002607 assert(From->getNumValues() == To.size() &&
2608 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002609 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002610 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002611 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002612 return;
2613 }
2614
2615 while (!From->use_empty()) {
2616 // Process users until they are all gone.
2617 SDNode *U = *From->use_begin();
2618
2619 // This node is about to morph, remove its old self from the CSE maps.
2620 RemoveNodeFromCSEMaps(U);
2621
Chris Lattner65113b22005-11-08 22:07:03 +00002622 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2623 I != E; ++I)
2624 if (I->Val == From) {
2625 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002626 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002627 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002628 ToOp.Val->addUser(U);
2629 }
2630
2631 // Now that we have modified U, add it back to the CSE maps. If it already
2632 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002633 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2634 ReplaceAllUsesWith(U, Existing, Deleted);
2635 // U is now dead.
2636 if (Deleted) Deleted->push_back(U);
2637 DeleteNodeNotInCSEMaps(U);
2638 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002639 }
2640}
2641
Chris Lattner012f2412006-02-17 21:58:01 +00002642/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2643/// uses of other values produced by From.Val alone. The Deleted vector is
2644/// handled the same was as for ReplaceAllUsesWith.
2645void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2646 std::vector<SDNode*> &Deleted) {
2647 assert(From != To && "Cannot replace a value with itself");
2648 // Handle the simple, trivial, case efficiently.
2649 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2650 ReplaceAllUsesWith(From, To, &Deleted);
2651 return;
2652 }
2653
2654 // Get all of the users in a nice, deterministically ordered, uniqued set.
2655 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2656
2657 while (!Users.empty()) {
2658 // We know that this user uses some value of From. If it is the right
2659 // value, update it.
2660 SDNode *User = Users.back();
2661 Users.pop_back();
2662
2663 for (SDOperand *Op = User->OperandList,
2664 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2665 if (*Op == From) {
2666 // Okay, we know this user needs to be updated. Remove its old self
2667 // from the CSE maps.
2668 RemoveNodeFromCSEMaps(User);
2669
2670 // Update all operands that match "From".
2671 for (; Op != E; ++Op) {
2672 if (*Op == From) {
2673 From.Val->removeUser(User);
2674 *Op = To;
2675 To.Val->addUser(User);
2676 }
2677 }
2678
2679 // Now that we have modified User, add it back to the CSE maps. If it
2680 // already exists there, recursively merge the results together.
2681 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2682 unsigned NumDeleted = Deleted.size();
2683 ReplaceAllUsesWith(User, Existing, &Deleted);
2684
2685 // User is now dead.
2686 Deleted.push_back(User);
2687 DeleteNodeNotInCSEMaps(User);
2688
2689 // We have to be careful here, because ReplaceAllUsesWith could have
2690 // deleted a user of From, which means there may be dangling pointers
2691 // in the "Users" setvector. Scan over the deleted node pointers and
2692 // remove them from the setvector.
2693 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2694 Users.remove(Deleted[i]);
2695 }
2696 break; // Exit the operand scanning loop.
2697 }
2698 }
2699 }
2700}
2701
Chris Lattner7b2880c2005-08-24 22:44:39 +00002702
Jim Laskey58b968b2005-08-17 20:08:02 +00002703//===----------------------------------------------------------------------===//
2704// SDNode Class
2705//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002706
Chris Lattnera3255112005-11-08 23:30:28 +00002707
2708/// getValueTypeList - Return a pointer to the specified value type.
2709///
2710MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2711 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2712 VTs[VT] = VT;
2713 return &VTs[VT];
2714}
2715
Chris Lattner5c884562005-01-12 18:37:47 +00002716/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2717/// indicated value. This method ignores uses of other values defined by this
2718/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002719bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002720 assert(Value < getNumValues() && "Bad value!");
2721
2722 // If there is only one value, this is easy.
2723 if (getNumValues() == 1)
2724 return use_size() == NUses;
2725 if (Uses.size() < NUses) return false;
2726
Evan Cheng4ee62112006-02-05 06:29:23 +00002727 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002728
2729 std::set<SDNode*> UsersHandled;
2730
Evan Cheng4ee62112006-02-05 06:29:23 +00002731 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002732 UI != E; ++UI) {
2733 SDNode *User = *UI;
2734 if (User->getNumOperands() == 1 ||
2735 UsersHandled.insert(User).second) // First time we've seen this?
2736 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2737 if (User->getOperand(i) == TheValue) {
2738 if (NUses == 0)
2739 return false; // too many uses
2740 --NUses;
2741 }
2742 }
2743
2744 // Found exactly the right number of uses?
2745 return NUses == 0;
2746}
2747
2748
Evan Cheng4ee62112006-02-05 06:29:23 +00002749// isOnlyUse - Return true if this node is the only use of N.
2750bool SDNode::isOnlyUse(SDNode *N) const {
2751 bool Seen = false;
2752 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2753 SDNode *User = *I;
2754 if (User == this)
2755 Seen = true;
2756 else
2757 return false;
2758 }
2759
2760 return Seen;
2761}
2762
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002763// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002764bool SDOperand::isOperand(SDNode *N) const {
2765 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2766 if (*this == N->getOperand(i))
2767 return true;
2768 return false;
2769}
2770
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002771bool SDNode::isOperand(SDNode *N) const {
2772 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2773 if (this == N->OperandList[i].Val)
2774 return true;
2775 return false;
2776}
Evan Cheng4ee62112006-02-05 06:29:23 +00002777
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002778const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002779 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002780 default:
2781 if (getOpcode() < ISD::BUILTIN_OP_END)
2782 return "<<Unknown DAG Node>>";
2783 else {
Evan Cheng72261582005-12-20 06:22:03 +00002784 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002785 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002786 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2787 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002788
Evan Cheng72261582005-12-20 06:22:03 +00002789 TargetLowering &TLI = G->getTargetLoweringInfo();
2790 const char *Name =
2791 TLI.getTargetNodeName(getOpcode());
2792 if (Name) return Name;
2793 }
2794
2795 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002796 }
2797
Andrew Lenharth95762122005-03-31 21:24:06 +00002798 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002799 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002800 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002801 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002802 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002803 case ISD::AssertSext: return "AssertSext";
2804 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002805
2806 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002807 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002808 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002809 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002810
2811 case ISD::Constant: return "Constant";
2812 case ISD::ConstantFP: return "ConstantFP";
2813 case ISD::GlobalAddress: return "GlobalAddress";
2814 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002815 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002816 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002817 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002818 case ISD::INTRINSIC_WO_CHAIN: {
2819 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2820 return Intrinsic::getName((Intrinsic::ID)IID);
2821 }
2822 case ISD::INTRINSIC_VOID:
2823 case ISD::INTRINSIC_W_CHAIN: {
2824 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002825 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002826 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002827
Chris Lattnerb2827b02006-03-19 00:52:58 +00002828 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002829 case ISD::TargetConstant: return "TargetConstant";
2830 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002831 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2832 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002833 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002834 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002835 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002836
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002837 case ISD::CopyToReg: return "CopyToReg";
2838 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002839 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002840 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002841 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002842 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002843 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002844
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002845 // Unary operators
2846 case ISD::FABS: return "fabs";
2847 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002848 case ISD::FSQRT: return "fsqrt";
2849 case ISD::FSIN: return "fsin";
2850 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002851
2852 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002853 case ISD::ADD: return "add";
2854 case ISD::SUB: return "sub";
2855 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002856 case ISD::MULHU: return "mulhu";
2857 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002858 case ISD::SDIV: return "sdiv";
2859 case ISD::UDIV: return "udiv";
2860 case ISD::SREM: return "srem";
2861 case ISD::UREM: return "urem";
2862 case ISD::AND: return "and";
2863 case ISD::OR: return "or";
2864 case ISD::XOR: return "xor";
2865 case ISD::SHL: return "shl";
2866 case ISD::SRA: return "sra";
2867 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002868 case ISD::ROTL: return "rotl";
2869 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002870 case ISD::FADD: return "fadd";
2871 case ISD::FSUB: return "fsub";
2872 case ISD::FMUL: return "fmul";
2873 case ISD::FDIV: return "fdiv";
2874 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002875 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002876 case ISD::VADD: return "vadd";
2877 case ISD::VSUB: return "vsub";
2878 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002879 case ISD::VSDIV: return "vsdiv";
2880 case ISD::VUDIV: return "vudiv";
2881 case ISD::VAND: return "vand";
2882 case ISD::VOR: return "vor";
2883 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002884
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002885 case ISD::SETCC: return "setcc";
2886 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002887 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002888 case ISD::VSELECT: return "vselect";
2889 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2890 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2891 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002892 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002893 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2894 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2895 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2896 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2897 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002898 case ISD::ADDC: return "addc";
2899 case ISD::ADDE: return "adde";
2900 case ISD::SUBC: return "subc";
2901 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002902 case ISD::SHL_PARTS: return "shl_parts";
2903 case ISD::SRA_PARTS: return "sra_parts";
2904 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002905
Chris Lattner7f644642005-04-28 21:44:03 +00002906 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002907 case ISD::SIGN_EXTEND: return "sign_extend";
2908 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002909 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002910 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002911 case ISD::TRUNCATE: return "truncate";
2912 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002913 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002914 case ISD::FP_EXTEND: return "fp_extend";
2915
2916 case ISD::SINT_TO_FP: return "sint_to_fp";
2917 case ISD::UINT_TO_FP: return "uint_to_fp";
2918 case ISD::FP_TO_SINT: return "fp_to_sint";
2919 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002920 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002921
2922 // Control flow instructions
2923 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002924 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002925 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002926 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002927 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002928 case ISD::CALLSEQ_START: return "callseq_start";
2929 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002930
2931 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002932 case ISD::LOAD: return "load";
2933 case ISD::STORE: return "store";
2934 case ISD::VLOAD: return "vload";
2935 case ISD::EXTLOAD: return "extload";
2936 case ISD::SEXTLOAD: return "sextload";
2937 case ISD::ZEXTLOAD: return "zextload";
2938 case ISD::TRUNCSTORE: return "truncstore";
2939 case ISD::VAARG: return "vaarg";
2940 case ISD::VACOPY: return "vacopy";
2941 case ISD::VAEND: return "vaend";
2942 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002943 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002944 case ISD::EXTRACT_ELEMENT: return "extract_element";
2945 case ISD::BUILD_PAIR: return "build_pair";
2946 case ISD::STACKSAVE: return "stacksave";
2947 case ISD::STACKRESTORE: return "stackrestore";
2948
2949 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002950 case ISD::MEMSET: return "memset";
2951 case ISD::MEMCPY: return "memcpy";
2952 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002953
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002954 // Bit manipulation
2955 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002956 case ISD::CTPOP: return "ctpop";
2957 case ISD::CTTZ: return "cttz";
2958 case ISD::CTLZ: return "ctlz";
2959
Chris Lattner36ce6912005-11-29 06:21:05 +00002960 // Debug info
2961 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002962 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002963 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002964
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002965 case ISD::CONDCODE:
2966 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002967 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002968 case ISD::SETOEQ: return "setoeq";
2969 case ISD::SETOGT: return "setogt";
2970 case ISD::SETOGE: return "setoge";
2971 case ISD::SETOLT: return "setolt";
2972 case ISD::SETOLE: return "setole";
2973 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002974
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002975 case ISD::SETO: return "seto";
2976 case ISD::SETUO: return "setuo";
2977 case ISD::SETUEQ: return "setue";
2978 case ISD::SETUGT: return "setugt";
2979 case ISD::SETUGE: return "setuge";
2980 case ISD::SETULT: return "setult";
2981 case ISD::SETULE: return "setule";
2982 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002983
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002984 case ISD::SETEQ: return "seteq";
2985 case ISD::SETGT: return "setgt";
2986 case ISD::SETGE: return "setge";
2987 case ISD::SETLT: return "setlt";
2988 case ISD::SETLE: return "setle";
2989 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002990 }
2991 }
2992}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002993
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002994void SDNode::dump() const { dump(0); }
2995void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002996 std::cerr << (void*)this << ": ";
2997
2998 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2999 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00003000 if (getValueType(i) == MVT::Other)
3001 std::cerr << "ch";
3002 else
3003 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00003004 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003005 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003006
3007 std::cerr << " ";
3008 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
3009 if (i) std::cerr << ", ";
3010 std::cerr << (void*)getOperand(i).Val;
3011 if (unsigned RN = getOperand(i).ResNo)
3012 std::cerr << ":" << RN;
3013 }
3014
3015 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
3016 std::cerr << "<" << CSDN->getValue() << ">";
3017 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
3018 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003019 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00003020 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00003021 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00003022 std::cerr << "<";
3023 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00003024 if (offset > 0)
3025 std::cerr << " + " << offset;
3026 else
3027 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003028 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003029 std::cerr << "<" << FIDN->getIndex() << ">";
3030 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00003031 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00003032 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00003033 if (offset > 0)
3034 std::cerr << " + " << offset;
3035 else
3036 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00003037 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00003038 std::cerr << "<";
3039 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
3040 if (LBB)
3041 std::cerr << LBB->getName() << " ";
3042 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00003043 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00003044 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00003045 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
3046 } else {
3047 std::cerr << " #" << R->getReg();
3048 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003049 } else if (const ExternalSymbolSDNode *ES =
3050 dyn_cast<ExternalSymbolSDNode>(this)) {
3051 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003052 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3053 if (M->getValue())
3054 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3055 else
3056 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003057 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3058 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003059 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003060}
3061
Chris Lattnerde202b32005-11-09 23:47:37 +00003062static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003063 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3064 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003065 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003066 else
3067 std::cerr << "\n" << std::string(indent+2, ' ')
3068 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003069
Chris Lattnerea946cd2005-01-09 20:38:33 +00003070
3071 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003072 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003073}
3074
Chris Lattnerc3aae252005-01-07 07:46:32 +00003075void SelectionDAG::dump() const {
3076 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003077 std::vector<const SDNode*> Nodes;
3078 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3079 I != E; ++I)
3080 Nodes.push_back(I);
3081
Chris Lattner49d24712005-01-09 20:26:36 +00003082 std::sort(Nodes.begin(), Nodes.end());
3083
3084 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003085 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003086 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003087 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003088
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003089 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003090
Chris Lattnerc3aae252005-01-07 07:46:32 +00003091 std::cerr << "\n\n";
3092}
3093
Evan Chengfae9f1c2006-02-09 22:11:03 +00003094/// InsertISelMapEntry - A helper function to insert a key / element pair
3095/// into a SDOperand to SDOperand map. This is added to avoid the map
3096/// insertion operator from being inlined.
3097void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3098 SDNode *Key, unsigned KeyResNo,
3099 SDNode *Element, unsigned ElementResNo) {
3100 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3101 SDOperand(Element, ElementResNo)));
3102}