blob: c26632a41e882bf7cfc412594de99dd9639b536c [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)
220 Op &= ~16; // Clear the N bit.
221 return ISD::CondCode(Op);
222}
223
224/// getSetCCAndOperation - Return the result of a logical AND between different
225/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
226/// function returns zero if it is not possible to represent the resultant
227/// comparison.
228ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
229 bool isInteger) {
230 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
231 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000232 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000233
234 // Combine all of the condition bits.
235 return ISD::CondCode(Op1 & Op2);
236}
237
Chris Lattnerb48da392005-01-23 04:39:44 +0000238const TargetMachine &SelectionDAG::getTarget() const {
239 return TLI.getTargetMachine();
240}
241
Jim Laskey58b968b2005-08-17 20:08:02 +0000242//===----------------------------------------------------------------------===//
243// SelectionDAG Class
244//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000245
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000246/// RemoveDeadNodes - This method deletes all unreachable nodes in the
247/// SelectionDAG, including nodes (like loads) that have uses of their token
248/// chain but no other uses and no side effect. If a node is passed in as an
249/// argument, it is used as the seed for node deletion.
250void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000251 // Create a dummy node (which is not added to allnodes), that adds a reference
252 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000253 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000254
Chris Lattnerf469cb62005-11-08 18:52:27 +0000255 bool MadeChange = false;
256
Chris Lattner8b8749f2005-08-17 19:00:20 +0000257 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000258 if (N && N->use_empty()) {
259 DestroyDeadNode(N);
260 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000261 }
262
Chris Lattnerde202b32005-11-09 23:47:37 +0000263 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
264 if (I->use_empty() && I->getOpcode() != 65535) {
265 // Node is dead, recursively delete newly dead uses.
266 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000267 MadeChange = true;
268 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000269
270 // Walk the nodes list, removing the nodes we've marked as dead.
271 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000272 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
273 SDNode *N = I++;
274 if (N->use_empty())
275 AllNodes.erase(N);
276 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000277 }
278
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000279 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000280 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000281}
282
Chris Lattnerf469cb62005-11-08 18:52:27 +0000283/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
284/// graph. If it is the last user of any of its operands, recursively process
285/// them the same way.
286///
287void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000288 // Okay, we really are going to delete this node. First take this out of the
289 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000290 RemoveNodeFromCSEMaps(N);
291
292 // Next, brutally remove the operand list. This is safe to do, as there are
293 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000294 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
295 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000296 O->removeUser(N);
297
298 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000299 if (O->use_empty())
300 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000301 }
Chris Lattner65113b22005-11-08 22:07:03 +0000302 delete[] N->OperandList;
303 N->OperandList = 0;
304 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000305
306 // Mark the node as dead.
307 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000308}
309
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000310void SelectionDAG::DeleteNode(SDNode *N) {
311 assert(N->use_empty() && "Cannot delete a node that is not dead!");
312
313 // First take this out of the appropriate CSE map.
314 RemoveNodeFromCSEMaps(N);
315
Chris Lattner1e111c72005-09-07 05:37:01 +0000316 // Finally, remove uses due to operands of this node, remove from the
317 // AllNodes list, and delete the node.
318 DeleteNodeNotInCSEMaps(N);
319}
320
321void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
322
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000323 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000324 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000325
326 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000327 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
328 I->Val->removeUser(N);
329 delete[] N->OperandList;
330 N->OperandList = 0;
331 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000332
333 delete N;
334}
335
Chris Lattner149c58c2005-08-16 18:17:10 +0000336/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
337/// correspond to it. This is useful when we're about to delete or repurpose
338/// the node. We don't want future request for structurally identical nodes
339/// to return N anymore.
340void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000341 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000342 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000343 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000344 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000345 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
346 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000347 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000348 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000349 Erased = TargetConstants.erase(std::make_pair(
350 cast<ConstantSDNode>(N)->getValue(),
351 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000352 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000353 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000354 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000355 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000356 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000357 }
Chris Lattner3181a772006-01-29 06:26:56 +0000358 case ISD::TargetConstantFP: {
359 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
360 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
361 break;
362 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000363 case ISD::STRING:
364 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
365 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000366 case ISD::CONDCODE:
367 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
368 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000369 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000370 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
371 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000372 case ISD::GlobalAddress: {
373 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
374 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
375 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000376 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000377 }
378 case ISD::TargetGlobalAddress: {
379 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
380 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
381 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000382 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000383 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000384 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000385 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000386 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000387 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000388 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000389 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000390 case ISD::JumpTable:
391 Erased = JumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
392 break;
393 case ISD::TargetJumpTable:
394 Erased =
395 TargetJumpTableIndices.erase(cast<JumpTableSDNode>(N)->getIndex());
396 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000397 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000398 Erased = ConstantPoolIndices.
399 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000400 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
401 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000402 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000403 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000404 Erased = TargetConstantPoolIndices.
405 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000406 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
407 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000408 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000409 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000410 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000411 break;
412 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000413 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000414 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000415 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000416 Erased =
417 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000418 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000419 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000420 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000421 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
422 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000423 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000424 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
425 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000426 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000427 case ISD::SRCVALUE: {
428 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000429 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000430 break;
431 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000432 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000433 Erased = Loads.erase(std::make_pair(N->getOperand(1),
434 std::make_pair(N->getOperand(0),
435 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000436 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000437 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000438 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000439 if (N->getNumOperands() == 0) {
440 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
441 N->getValueType(0)));
442 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000443 Erased =
444 UnaryOps.erase(std::make_pair(N->getOpcode(),
445 std::make_pair(N->getOperand(0),
446 N->getValueType(0))));
447 } else if (N->getNumOperands() == 2) {
448 Erased =
449 BinaryOps.erase(std::make_pair(N->getOpcode(),
450 std::make_pair(N->getOperand(0),
451 N->getOperand(1))));
452 } else {
453 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
454 Erased =
455 OneResultNodes.erase(std::make_pair(N->getOpcode(),
456 std::make_pair(N->getValueType(0),
457 Ops)));
458 }
Chris Lattner385328c2005-05-14 07:42:29 +0000459 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000460 // Remove the node from the ArbitraryNodes map.
461 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
462 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000463 Erased =
464 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
465 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000466 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000467 break;
468 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000469#ifndef NDEBUG
470 // Verify that the node was actually in one of the CSE maps, unless it has a
471 // flag result (which cannot be CSE'd) or is one of the special cases that are
472 // not subject to CSE.
473 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000474 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000475 N->dump();
476 assert(0 && "Node is not in map!");
477 }
478#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000479}
480
Chris Lattner8b8749f2005-08-17 19:00:20 +0000481/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
482/// has been taken out and modified in some way. If the specified node already
483/// exists in the CSE maps, do not modify the maps, but return the existing node
484/// instead. If it doesn't exist, add it and return null.
485///
486SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
487 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000488 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000489 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000490
Chris Lattner9f8cc692005-12-19 22:21:21 +0000491 // Check that remaining values produced are not flags.
492 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
493 if (N->getValueType(i) == MVT::Flag)
494 return 0; // Never CSE anything that produces a flag.
495
Chris Lattnerfe14b342005-12-01 23:14:50 +0000496 if (N->getNumValues() == 1) {
497 if (N->getNumOperands() == 1) {
498 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
499 std::make_pair(N->getOperand(0),
500 N->getValueType(0)))];
501 if (U) return U;
502 U = N;
503 } else if (N->getNumOperands() == 2) {
504 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
505 std::make_pair(N->getOperand(0),
506 N->getOperand(1)))];
507 if (B) return B;
508 B = N;
509 } else {
510 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
511 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000512 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000513 if (ORN) return ORN;
514 ORN = N;
515 }
516 } else {
517 if (N->getOpcode() == ISD::LOAD) {
518 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
519 std::make_pair(N->getOperand(0),
520 N->getValueType(0)))];
521 if (L) return L;
522 L = N;
523 } else {
524 // Remove the node from the ArbitraryNodes map.
525 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
526 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
527 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
528 std::make_pair(RV, Ops))];
529 if (AN) return AN;
530 AN = N;
531 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000532 }
533 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000534}
535
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000536/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
537/// were replaced with those specified. If this node is never memoized,
538/// return null, otherwise return a pointer to the slot it would take. If a
539/// node already exists with these operands, the slot will be non-null.
540SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000541 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000542 return 0; // Never add these nodes.
543
544 // Check that remaining values produced are not flags.
545 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
546 if (N->getValueType(i) == MVT::Flag)
547 return 0; // Never CSE anything that produces a flag.
548
549 if (N->getNumValues() == 1) {
550 return &UnaryOps[std::make_pair(N->getOpcode(),
551 std::make_pair(Op, N->getValueType(0)))];
552 } else {
553 // Remove the node from the ArbitraryNodes map.
554 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
555 std::vector<SDOperand> Ops;
556 Ops.push_back(Op);
557 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
558 std::make_pair(RV, Ops))];
559 }
560 return 0;
561}
562
563/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
564/// were replaced with those specified. If this node is never memoized,
565/// return null, otherwise return a pointer to the slot it would take. If a
566/// node already exists with these operands, the slot will be non-null.
567SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
568 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000569 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000570 return 0; // Never add these nodes.
571
572 // Check that remaining values produced are not flags.
573 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
574 if (N->getValueType(i) == MVT::Flag)
575 return 0; // Never CSE anything that produces a flag.
576
577 if (N->getNumValues() == 1) {
578 return &BinaryOps[std::make_pair(N->getOpcode(),
579 std::make_pair(Op1, Op2))];
580 } else {
581 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
582 std::vector<SDOperand> Ops;
583 Ops.push_back(Op1);
584 Ops.push_back(Op2);
585 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
586 std::make_pair(RV, Ops))];
587 }
588 return 0;
589}
590
591
592/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
593/// were replaced with those specified. If this node is never memoized,
594/// return null, otherwise return a pointer to the slot it would take. If a
595/// node already exists with these operands, the slot will be non-null.
596SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
597 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000598 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000599 return 0; // Never add these nodes.
600
601 // Check that remaining values produced are not flags.
602 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
603 if (N->getValueType(i) == MVT::Flag)
604 return 0; // Never CSE anything that produces a flag.
605
606 if (N->getNumValues() == 1) {
607 if (N->getNumOperands() == 1) {
608 return &UnaryOps[std::make_pair(N->getOpcode(),
609 std::make_pair(Ops[0],
610 N->getValueType(0)))];
611 } else if (N->getNumOperands() == 2) {
612 return &BinaryOps[std::make_pair(N->getOpcode(),
613 std::make_pair(Ops[0], Ops[1]))];
614 } else {
615 return &OneResultNodes[std::make_pair(N->getOpcode(),
616 std::make_pair(N->getValueType(0),
617 Ops))];
618 }
619 } else {
620 if (N->getOpcode() == ISD::LOAD) {
621 return &Loads[std::make_pair(Ops[1],
622 std::make_pair(Ops[0], N->getValueType(0)))];
623 } else {
624 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
625 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
626 std::make_pair(RV, Ops))];
627 }
628 }
629 return 0;
630}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000631
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000632
Chris Lattner78ec3112003-08-11 14:57:33 +0000633SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000634 while (!AllNodes.empty()) {
635 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000636 delete [] N->OperandList;
637 N->OperandList = 0;
638 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000639 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000640 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000641}
642
Chris Lattner0f2287b2005-04-13 02:38:18 +0000643SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000644 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000645 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000646 return getNode(ISD::AND, Op.getValueType(), Op,
647 getConstant(Imm, Op.getValueType()));
648}
649
Chris Lattnerc3aae252005-01-07 07:46:32 +0000650SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
651 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
Chris Lattnerf35b2972006-03-28 19:04:49 +0000652 assert(!MVT::isVector(VT) && "Cannot create Vector ConstantSDNodes!");
653
Chris Lattnerc3aae252005-01-07 07:46:32 +0000654 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000655 if (VT != MVT::i64)
656 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000657
Chris Lattnerc3aae252005-01-07 07:46:32 +0000658 SDNode *&N = Constants[std::make_pair(Val, VT)];
659 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000660 N = new ConstantSDNode(false, Val, VT);
661 AllNodes.push_back(N);
662 return SDOperand(N, 0);
663}
664
Chris Lattner36ce6912005-11-29 06:21:05 +0000665SDOperand SelectionDAG::getString(const std::string &Val) {
666 StringSDNode *&N = StringNodes[Val];
667 if (!N) {
668 N = new StringSDNode(Val);
669 AllNodes.push_back(N);
670 }
671 return SDOperand(N, 0);
672}
673
Chris Lattner37bfbb42005-08-17 00:34:06 +0000674SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
675 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
676 // Mask out any bits that are not valid for this constant.
677 if (VT != MVT::i64)
678 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
679
680 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
681 if (N) return SDOperand(N, 0);
682 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000683 AllNodes.push_back(N);
684 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000685}
686
Chris Lattnerc3aae252005-01-07 07:46:32 +0000687SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
688 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
689 if (VT == MVT::f32)
690 Val = (float)Val; // Mask out extra precision.
691
Chris Lattnerd8658612005-02-17 20:17:32 +0000692 // Do the map lookup using the actual bit pattern for the floating point
693 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
694 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000695 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000696 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000697 N = new ConstantFPSDNode(false, Val, VT);
698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
700}
701
702SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
703 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
704 if (VT == MVT::f32)
705 Val = (float)Val; // Mask out extra precision.
706
707 // Do the map lookup using the actual bit pattern for the floating point
708 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
709 // we don't have issues with SNANs.
710 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
711 if (N) return SDOperand(N, 0);
712 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000713 AllNodes.push_back(N);
714 return SDOperand(N, 0);
715}
716
Chris Lattnerc3aae252005-01-07 07:46:32 +0000717SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000718 MVT::ValueType VT, int offset) {
719 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000720 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000721 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000722 AllNodes.push_back(N);
723 return SDOperand(N, 0);
724}
725
726SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000727 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000728 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000729 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000730 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000731 AllNodes.push_back(N);
732 return SDOperand(N, 0);
733}
734
735SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
736 SDNode *&N = FrameIndices[FI];
737 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000738 N = new FrameIndexSDNode(FI, VT, false);
739 AllNodes.push_back(N);
740 return SDOperand(N, 0);
741}
742
743SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
744 SDNode *&N = TargetFrameIndices[FI];
745 if (N) return SDOperand(N, 0);
746 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000747 AllNodes.push_back(N);
748 return SDOperand(N, 0);
749}
750
Nate Begeman37efe672006-04-22 18:53:45 +0000751SDOperand SelectionDAG::getJumpTable(int JTI, MVT::ValueType VT) {
752 SDNode *&N = JumpTableIndices[JTI];
753 if (N) return SDOperand(N, 0);
754 N = new JumpTableSDNode(JTI, VT, false);
755 AllNodes.push_back(N);
756 return SDOperand(N, 0);
757}
758
759SDOperand SelectionDAG::getTargetJumpTable(int JTI, MVT::ValueType VT) {
760 SDNode *&N = TargetJumpTableIndices[JTI];
761 if (N) return SDOperand(N, 0);
762 N = new JumpTableSDNode(JTI, VT, true);
763 AllNodes.push_back(N);
764 return SDOperand(N, 0);
765}
766
Evan Chengb8973bd2006-01-31 22:23:14 +0000767SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000768 unsigned Alignment, int Offset) {
769 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
770 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000771 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000772 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000773 AllNodes.push_back(N);
774 return SDOperand(N, 0);
775}
776
Evan Chengb8973bd2006-01-31 22:23:14 +0000777SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000778 unsigned Alignment, int Offset) {
779 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
780 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000781 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000782 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000783 AllNodes.push_back(N);
784 return SDOperand(N, 0);
785}
786
787SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
788 SDNode *&N = BBNodes[MBB];
789 if (N) return SDOperand(N, 0);
790 N = new BasicBlockSDNode(MBB);
791 AllNodes.push_back(N);
792 return SDOperand(N, 0);
793}
794
Chris Lattner15e4b012005-07-10 00:07:11 +0000795SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
796 if ((unsigned)VT >= ValueTypeNodes.size())
797 ValueTypeNodes.resize(VT+1);
798 if (ValueTypeNodes[VT] == 0) {
799 ValueTypeNodes[VT] = new VTSDNode(VT);
800 AllNodes.push_back(ValueTypeNodes[VT]);
801 }
802
803 return SDOperand(ValueTypeNodes[VT], 0);
804}
805
Chris Lattnerc3aae252005-01-07 07:46:32 +0000806SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
807 SDNode *&N = ExternalSymbols[Sym];
808 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000809 N = new ExternalSymbolSDNode(false, Sym, VT);
810 AllNodes.push_back(N);
811 return SDOperand(N, 0);
812}
813
Chris Lattner809ec112006-01-28 10:09:25 +0000814SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
815 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000816 SDNode *&N = TargetExternalSymbols[Sym];
817 if (N) return SDOperand(N, 0);
818 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819 AllNodes.push_back(N);
820 return SDOperand(N, 0);
821}
822
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000823SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
824 if ((unsigned)Cond >= CondCodeNodes.size())
825 CondCodeNodes.resize(Cond+1);
826
Chris Lattner079a27a2005-08-09 20:40:02 +0000827 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000828 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000829 AllNodes.push_back(CondCodeNodes[Cond]);
830 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000831 return SDOperand(CondCodeNodes[Cond], 0);
832}
833
Chris Lattner0fdd7682005-08-30 22:38:38 +0000834SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
835 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
836 if (!Reg) {
837 Reg = new RegisterSDNode(RegNo, VT);
838 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000839 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000840 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000841}
842
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000843SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
844 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000845 // These setcc operations always fold.
846 switch (Cond) {
847 default: break;
848 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000849 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000850 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000851 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000852 }
853
Chris Lattner67255a12005-04-07 18:14:58 +0000854 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
855 uint64_t C2 = N2C->getValue();
856 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
857 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000858
Chris Lattnerc3aae252005-01-07 07:46:32 +0000859 // Sign extend the operands if required
860 if (ISD::isSignedIntSetCC(Cond)) {
861 C1 = N1C->getSignExtended();
862 C2 = N2C->getSignExtended();
863 }
864
865 switch (Cond) {
866 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000867 case ISD::SETEQ: return getConstant(C1 == C2, VT);
868 case ISD::SETNE: return getConstant(C1 != C2, VT);
869 case ISD::SETULT: return getConstant(C1 < C2, VT);
870 case ISD::SETUGT: return getConstant(C1 > C2, VT);
871 case ISD::SETULE: return getConstant(C1 <= C2, VT);
872 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
873 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
874 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
875 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
876 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000877 }
Chris Lattner24673922005-04-07 18:58:54 +0000878 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000879 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000880 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
881 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
882
883 // If the comparison constant has bits in the upper part, the
884 // zero-extended value could never match.
885 if (C2 & (~0ULL << InSize)) {
886 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
887 switch (Cond) {
888 case ISD::SETUGT:
889 case ISD::SETUGE:
890 case ISD::SETEQ: return getConstant(0, VT);
891 case ISD::SETULT:
892 case ISD::SETULE:
893 case ISD::SETNE: return getConstant(1, VT);
894 case ISD::SETGT:
895 case ISD::SETGE:
896 // True if the sign bit of C2 is set.
897 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
898 case ISD::SETLT:
899 case ISD::SETLE:
900 // True if the sign bit of C2 isn't set.
901 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
902 default:
903 break;
904 }
905 }
906
907 // Otherwise, we can perform the comparison with the low bits.
908 switch (Cond) {
909 case ISD::SETEQ:
910 case ISD::SETNE:
911 case ISD::SETUGT:
912 case ISD::SETUGE:
913 case ISD::SETULT:
914 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000915 return getSetCC(VT, N1.getOperand(0),
916 getConstant(C2, N1.getOperand(0).getValueType()),
917 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000918 default:
919 break; // todo, be more careful with signed comparisons
920 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000921 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
922 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
923 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
924 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
925 MVT::ValueType ExtDstTy = N1.getValueType();
926 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000927
Chris Lattner7b2880c2005-08-24 22:44:39 +0000928 // If the extended part has any inconsistent bits, it cannot ever
929 // compare equal. In other words, they have to be all ones or all
930 // zeros.
931 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000932 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000933 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
934 return getConstant(Cond == ISD::SETNE, VT);
935
936 // Otherwise, make this a use of a zext.
937 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000938 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000939 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000940 }
941
Chris Lattner67255a12005-04-07 18:14:58 +0000942 uint64_t MinVal, MaxVal;
943 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
944 if (ISD::isSignedIntSetCC(Cond)) {
945 MinVal = 1ULL << (OperandBitSize-1);
946 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
947 MaxVal = ~0ULL >> (65-OperandBitSize);
948 else
949 MaxVal = 0;
950 } else {
951 MinVal = 0;
952 MaxVal = ~0ULL >> (64-OperandBitSize);
953 }
954
955 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
956 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
957 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
958 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000959 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
960 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000961 }
962
963 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
964 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
965 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000966 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
967 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000968 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000969
Nate Begeman72ea2812005-04-14 08:56:52 +0000970 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
971 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000972
Nate Begeman72ea2812005-04-14 08:56:52 +0000973 // Canonicalize setgt X, Min --> setne X, Min
974 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000975 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000976
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000977 // If we have setult X, 1, turn it into seteq X, 0
978 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000979 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
980 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000981 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000982 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000983 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
984 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000985
986 // If we have "setcc X, C1", check to see if we can shrink the immediate
987 // by changing cc.
988
989 // SETUGT X, SINTMAX -> SETLT X, 0
990 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
991 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000992 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000993
994 // FIXME: Implement the rest of these.
995
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000996
997 // Fold bit comparisons when we can.
998 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
999 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
1000 if (ConstantSDNode *AndRHS =
1001 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
1002 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
1003 // Perform the xform if the AND RHS is a single bit.
1004 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
1005 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001006 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001007 TLI.getShiftAmountTy()));
1008 }
1009 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
1010 // (X & 8) == 8 --> (X & 8) >> 3
1011 // Perform the xform if C2 is a single bit.
1012 if ((C2 & (C2-1)) == 0) {
1013 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +00001014 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +00001015 }
1016 }
1017 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001018 }
Chris Lattner67255a12005-04-07 18:14:58 +00001019 } else if (isa<ConstantSDNode>(N1.Val)) {
1020 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001021 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +00001022 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001023
1024 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
1025 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
1026 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001027
Chris Lattnerc3aae252005-01-07 07:46:32 +00001028 switch (Cond) {
1029 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001030 case ISD::SETEQ: return getConstant(C1 == C2, VT);
1031 case ISD::SETNE: return getConstant(C1 != C2, VT);
1032 case ISD::SETLT: return getConstant(C1 < C2, VT);
1033 case ISD::SETGT: return getConstant(C1 > C2, VT);
1034 case ISD::SETLE: return getConstant(C1 <= C2, VT);
1035 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001036 }
1037 } else {
1038 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001039 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001040 }
1041
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001042 // Could not fold it.
1043 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001044}
1045
Chris Lattnerc3aae252005-01-07 07:46:32 +00001046/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001047///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001048SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001049 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1050 if (!N) {
1051 N = new SDNode(Opcode, VT);
1052 AllNodes.push_back(N);
1053 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001054 return SDOperand(N, 0);
1055}
1056
Chris Lattnerc3aae252005-01-07 07:46:32 +00001057SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1058 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001059 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001060 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1062 uint64_t Val = C->getValue();
1063 switch (Opcode) {
1064 default: break;
1065 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001066 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001067 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1068 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001069 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1070 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001071 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001072 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001073 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001074 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001075 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001076 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001077 case ISD::BSWAP:
1078 switch(VT) {
1079 default: assert(0 && "Invalid bswap!"); break;
1080 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1081 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1082 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1083 }
1084 break;
1085 case ISD::CTPOP:
1086 switch(VT) {
1087 default: assert(0 && "Invalid ctpop!"); break;
1088 case MVT::i1: return getConstant(Val != 0, VT);
1089 case MVT::i8:
1090 Tmp1 = (unsigned)Val & 0xFF;
1091 return getConstant(CountPopulation_32(Tmp1), VT);
1092 case MVT::i16:
1093 Tmp1 = (unsigned)Val & 0xFFFF;
1094 return getConstant(CountPopulation_32(Tmp1), VT);
1095 case MVT::i32:
1096 return getConstant(CountPopulation_32((unsigned)Val), VT);
1097 case MVT::i64:
1098 return getConstant(CountPopulation_64(Val), VT);
1099 }
1100 case ISD::CTLZ:
1101 switch(VT) {
1102 default: assert(0 && "Invalid ctlz!"); break;
1103 case MVT::i1: return getConstant(Val == 0, VT);
1104 case MVT::i8:
1105 Tmp1 = (unsigned)Val & 0xFF;
1106 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1107 case MVT::i16:
1108 Tmp1 = (unsigned)Val & 0xFFFF;
1109 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1110 case MVT::i32:
1111 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1112 case MVT::i64:
1113 return getConstant(CountLeadingZeros_64(Val), VT);
1114 }
1115 case ISD::CTTZ:
1116 switch(VT) {
1117 default: assert(0 && "Invalid cttz!"); break;
1118 case MVT::i1: return getConstant(Val == 0, VT);
1119 case MVT::i8:
1120 Tmp1 = (unsigned)Val | 0x100;
1121 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1122 case MVT::i16:
1123 Tmp1 = (unsigned)Val | 0x10000;
1124 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1125 case MVT::i32:
1126 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1127 case MVT::i64:
1128 return getConstant(CountTrailingZeros_64(Val), VT);
1129 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130 }
1131 }
1132
Chris Lattner94683772005-12-23 05:30:37 +00001133 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001134 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1135 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001136 case ISD::FNEG:
1137 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001138 case ISD::FABS:
1139 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001140 case ISD::FP_ROUND:
1141 case ISD::FP_EXTEND:
1142 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001143 case ISD::FP_TO_SINT:
1144 return getConstant((int64_t)C->getValue(), VT);
1145 case ISD::FP_TO_UINT:
1146 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001147 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001148 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001149 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001150 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001151 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001152 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 }
1154
1155 unsigned OpOpcode = Operand.Val->getOpcode();
1156 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001157 case ISD::TokenFactor:
1158 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001159 case ISD::SIGN_EXTEND:
1160 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001161 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001162 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1163 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1164 break;
1165 case ISD::ZERO_EXTEND:
1166 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001167 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001168 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001169 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001170 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001171 case ISD::ANY_EXTEND:
1172 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001173 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001174 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1175 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1176 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1177 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001178 case ISD::TRUNCATE:
1179 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001180 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001181 if (OpOpcode == ISD::TRUNCATE)
1182 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001183 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1184 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001185 // If the source is smaller than the dest, we still need an extend.
1186 if (Operand.Val->getOperand(0).getValueType() < VT)
1187 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1188 else if (Operand.Val->getOperand(0).getValueType() > VT)
1189 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1190 else
1191 return Operand.Val->getOperand(0);
1192 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001193 break;
Chris Lattner35481892005-12-23 00:16:34 +00001194 case ISD::BIT_CONVERT:
1195 // Basic sanity checking.
Chris Lattner70c2a612006-03-31 02:06:56 +00001196 assert(MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType())
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001197 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001198 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001199 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1200 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner08da55e2006-04-04 01:02:22 +00001201 if (OpOpcode == ISD::UNDEF)
1202 return getNode(ISD::UNDEF, VT);
Chris Lattner35481892005-12-23 00:16:34 +00001203 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001204 case ISD::SCALAR_TO_VECTOR:
1205 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1206 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1207 "Illegal SCALAR_TO_VECTOR node!");
1208 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001209 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001210 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1211 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001212 Operand.Val->getOperand(0));
1213 if (OpOpcode == ISD::FNEG) // --X -> X
1214 return Operand.Val->getOperand(0);
1215 break;
1216 case ISD::FABS:
1217 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1218 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1219 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001220 }
1221
Chris Lattner43247a12005-08-25 19:12:10 +00001222 SDNode *N;
1223 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1224 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001225 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001226 E = N = new SDNode(Opcode, Operand);
1227 } else {
1228 N = new SDNode(Opcode, Operand);
1229 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001230 N->setValueTypes(VT);
1231 AllNodes.push_back(N);
1232 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001233}
1234
Chris Lattner36019aa2005-04-18 03:48:41 +00001235
1236
Chris Lattnerc3aae252005-01-07 07:46:32 +00001237SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1238 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001239#ifndef NDEBUG
1240 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001241 case ISD::TokenFactor:
1242 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1243 N2.getValueType() == MVT::Other && "Invalid token factor!");
1244 break;
Chris Lattner76365122005-01-16 02:23:22 +00001245 case ISD::AND:
1246 case ISD::OR:
1247 case ISD::XOR:
1248 case ISD::UDIV:
1249 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001250 case ISD::MULHU:
1251 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001252 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1253 // fall through
1254 case ISD::ADD:
1255 case ISD::SUB:
1256 case ISD::MUL:
1257 case ISD::SDIV:
1258 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001259 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1260 // fall through.
1261 case ISD::FADD:
1262 case ISD::FSUB:
1263 case ISD::FMUL:
1264 case ISD::FDIV:
1265 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001266 assert(N1.getValueType() == N2.getValueType() &&
1267 N1.getValueType() == VT && "Binary operator types must match!");
1268 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001269 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1270 assert(N1.getValueType() == VT &&
1271 MVT::isFloatingPoint(N1.getValueType()) &&
1272 MVT::isFloatingPoint(N2.getValueType()) &&
1273 "Invalid FCOPYSIGN!");
1274 break;
Chris Lattner76365122005-01-16 02:23:22 +00001275 case ISD::SHL:
1276 case ISD::SRA:
1277 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001278 case ISD::ROTL:
1279 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001280 assert(VT == N1.getValueType() &&
1281 "Shift operators return type must be the same as their first arg");
1282 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001283 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001284 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001285 case ISD::FP_ROUND_INREG: {
1286 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1287 assert(VT == N1.getValueType() && "Not an inreg round!");
1288 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1289 "Cannot FP_ROUND_INREG integer types");
1290 assert(EVT <= VT && "Not rounding down!");
1291 break;
1292 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001293 case ISD::AssertSext:
1294 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001295 case ISD::SIGN_EXTEND_INREG: {
1296 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1297 assert(VT == N1.getValueType() && "Not an inreg extend!");
1298 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1299 "Cannot *_EXTEND_INREG FP types");
1300 assert(EVT <= VT && "Not extending!");
1301 }
1302
Chris Lattner76365122005-01-16 02:23:22 +00001303 default: break;
1304 }
1305#endif
1306
Chris Lattnerc3aae252005-01-07 07:46:32 +00001307 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1308 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1309 if (N1C) {
1310 if (N2C) {
1311 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1312 switch (Opcode) {
1313 case ISD::ADD: return getConstant(C1 + C2, VT);
1314 case ISD::SUB: return getConstant(C1 - C2, VT);
1315 case ISD::MUL: return getConstant(C1 * C2, VT);
1316 case ISD::UDIV:
1317 if (C2) return getConstant(C1 / C2, VT);
1318 break;
1319 case ISD::UREM :
1320 if (C2) return getConstant(C1 % C2, VT);
1321 break;
1322 case ISD::SDIV :
1323 if (C2) return getConstant(N1C->getSignExtended() /
1324 N2C->getSignExtended(), VT);
1325 break;
1326 case ISD::SREM :
1327 if (C2) return getConstant(N1C->getSignExtended() %
1328 N2C->getSignExtended(), VT);
1329 break;
1330 case ISD::AND : return getConstant(C1 & C2, VT);
1331 case ISD::OR : return getConstant(C1 | C2, VT);
1332 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001333 case ISD::SHL : return getConstant(C1 << C2, VT);
1334 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001335 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001336 case ISD::ROTL :
1337 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1338 VT);
1339 case ISD::ROTR :
1340 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1341 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001342 default: break;
1343 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001344 } else { // Cannonicalize constant to RHS if commutative
1345 if (isCommutativeBinOp(Opcode)) {
1346 std::swap(N1C, N2C);
1347 std::swap(N1, N2);
1348 }
1349 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001350 }
1351
1352 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1353 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001354 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001355 if (N2CFP) {
1356 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1357 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001358 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1359 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1360 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1361 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001362 if (C2) return getConstantFP(C1 / C2, VT);
1363 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001364 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001365 if (C2) return getConstantFP(fmod(C1, C2), VT);
1366 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001367 case ISD::FCOPYSIGN: {
1368 union {
1369 double F;
1370 uint64_t I;
1371 } u1;
1372 union {
1373 double F;
1374 int64_t I;
1375 } u2;
1376 u1.F = C1;
1377 u2.F = C2;
1378 if (u2.I < 0) // Sign bit of RHS set?
1379 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1380 else
1381 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1382 return getConstantFP(u1.F, VT);
1383 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001384 default: break;
1385 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001386 } else { // Cannonicalize constant to RHS if commutative
1387 if (isCommutativeBinOp(Opcode)) {
1388 std::swap(N1CFP, N2CFP);
1389 std::swap(N1, N2);
1390 }
1391 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001392 }
Chris Lattner62b57722006-04-20 05:39:12 +00001393
1394 // Canonicalize an UNDEF to the RHS, even over a constant.
1395 if (N1.getOpcode() == ISD::UNDEF) {
1396 if (isCommutativeBinOp(Opcode)) {
1397 std::swap(N1, N2);
1398 } else {
1399 switch (Opcode) {
1400 case ISD::FP_ROUND_INREG:
1401 case ISD::SIGN_EXTEND_INREG:
1402 case ISD::SUB:
1403 case ISD::FSUB:
1404 case ISD::FDIV:
1405 case ISD::FREM:
1406 return N1; // fold op(undef, arg2) -> undef
1407 case ISD::UDIV:
1408 case ISD::SDIV:
1409 case ISD::UREM:
1410 case ISD::SREM:
1411 return getConstant(0, VT); // fold op(undef, arg2) -> 0
1412 }
1413 }
1414 }
1415
1416 // Fold a bunch of operators that
1417 if (N2.getOpcode() == ISD::UNDEF) {
1418 switch (Opcode) {
1419 case ISD::ADD:
1420 case ISD::SUB:
1421 case ISD::FADD:
1422 case ISD::FSUB:
1423 case ISD::FMUL:
1424 case ISD::FDIV:
1425 case ISD::FREM:
1426 case ISD::UDIV:
1427 case ISD::SDIV:
1428 case ISD::UREM:
1429 case ISD::SREM:
1430 case ISD::XOR:
1431 return N2; // fold op(arg1, undef) -> undef
1432 case ISD::MUL:
1433 case ISD::AND:
1434 return getConstant(0, VT); // fold op(arg1, undef) -> 0
1435 case ISD::OR:
1436 return getConstant(MVT::getIntVTBitMask(VT), VT);
1437 }
1438 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001439
Chris Lattnerc3aae252005-01-07 07:46:32 +00001440 // Finally, fold operations that do not require constants.
1441 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001442 case ISD::FP_ROUND_INREG:
1443 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1444 break;
1445 case ISD::SIGN_EXTEND_INREG: {
1446 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1447 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001448 break;
1449 }
1450
Nate Begemaneea805e2005-04-13 21:23:31 +00001451 // FIXME: figure out how to safely handle things like
1452 // int foo(int x) { return 1 << (x & 255); }
1453 // int bar() { return foo(256); }
1454#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001455 case ISD::SHL:
1456 case ISD::SRL:
1457 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001458 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001459 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001460 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001461 else if (N2.getOpcode() == ISD::AND)
1462 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1463 // If the and is only masking out bits that cannot effect the shift,
1464 // eliminate the and.
1465 unsigned NumBits = MVT::getSizeInBits(VT);
1466 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1467 return getNode(Opcode, VT, N1, N2.getOperand(0));
1468 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001469 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001470#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001471 }
1472
Chris Lattner27e9b412005-05-11 18:57:39 +00001473 // Memoize this node if possible.
1474 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001475 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001476 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1477 if (BON) return SDOperand(BON, 0);
1478
1479 BON = N = new SDNode(Opcode, N1, N2);
1480 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001481 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001482 }
1483
Chris Lattner3e011362005-05-14 07:45:46 +00001484 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001485 AllNodes.push_back(N);
1486 return SDOperand(N, 0);
1487}
1488
Chris Lattnerc3aae252005-01-07 07:46:32 +00001489SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1490 SDOperand N1, SDOperand N2, SDOperand N3) {
1491 // Perform various simplifications.
1492 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1493 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1494 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1495 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001496 case ISD::SETCC: {
1497 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001498 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001499 if (Simp.Val) return Simp;
1500 break;
1501 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001502 case ISD::SELECT:
1503 if (N1C)
1504 if (N1C->getValue())
1505 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001506 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001507 return N3; // select false, X, Y -> Y
1508
1509 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001510 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001511 case ISD::BRCOND:
1512 if (N2C)
1513 if (N2C->getValue()) // Unconditional branch
1514 return getNode(ISD::BR, MVT::Other, N1, N3);
1515 else
1516 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001517 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001518 case ISD::VECTOR_SHUFFLE:
1519 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1520 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1521 N3.getOpcode() == ISD::BUILD_VECTOR &&
1522 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1523 "Illegal VECTOR_SHUFFLE node!");
1524 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001525 }
1526
Chris Lattner385328c2005-05-14 07:42:29 +00001527 std::vector<SDOperand> Ops;
1528 Ops.reserve(3);
1529 Ops.push_back(N1);
1530 Ops.push_back(N2);
1531 Ops.push_back(N3);
1532
Chris Lattner43247a12005-08-25 19:12:10 +00001533 // Memoize node if it doesn't produce a flag.
1534 SDNode *N;
1535 if (VT != MVT::Flag) {
1536 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1537 if (E) return SDOperand(E, 0);
1538 E = N = new SDNode(Opcode, N1, N2, N3);
1539 } else {
1540 N = new SDNode(Opcode, N1, N2, N3);
1541 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001542 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001543 AllNodes.push_back(N);
1544 return SDOperand(N, 0);
1545}
1546
1547SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001548 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001549 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001550 std::vector<SDOperand> Ops;
1551 Ops.reserve(4);
1552 Ops.push_back(N1);
1553 Ops.push_back(N2);
1554 Ops.push_back(N3);
1555 Ops.push_back(N4);
1556 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001557}
1558
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001559SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1560 SDOperand N1, SDOperand N2, SDOperand N3,
1561 SDOperand N4, SDOperand N5) {
1562 std::vector<SDOperand> Ops;
1563 Ops.reserve(5);
1564 Ops.push_back(N1);
1565 Ops.push_back(N2);
1566 Ops.push_back(N3);
1567 Ops.push_back(N4);
1568 Ops.push_back(N5);
1569 return getNode(Opcode, VT, Ops);
1570}
1571
Evan Cheng7038daf2005-12-10 00:37:58 +00001572SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1573 SDOperand Chain, SDOperand Ptr,
1574 SDOperand SV) {
1575 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1576 if (N) return SDOperand(N, 0);
1577 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1578
1579 // Loads have a token chain.
1580 setNodeValueTypes(N, VT, MVT::Other);
1581 AllNodes.push_back(N);
1582 return SDOperand(N, 0);
1583}
1584
1585SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1586 SDOperand Chain, SDOperand Ptr,
1587 SDOperand SV) {
Evan Cheng7038daf2005-12-10 00:37:58 +00001588 std::vector<SDOperand> Ops;
1589 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001590 Ops.push_back(Chain);
1591 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001592 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001593 Ops.push_back(getConstant(Count, MVT::i32));
1594 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001595 std::vector<MVT::ValueType> VTs;
1596 VTs.reserve(2);
1597 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1598 return getNode(ISD::VLOAD, VTs, Ops);
1599}
1600
1601SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1602 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1603 MVT::ValueType EVT) {
1604 std::vector<SDOperand> Ops;
1605 Ops.reserve(4);
1606 Ops.push_back(Chain);
1607 Ops.push_back(Ptr);
1608 Ops.push_back(SV);
1609 Ops.push_back(getValueType(EVT));
1610 std::vector<MVT::ValueType> VTs;
1611 VTs.reserve(2);
1612 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1613 return getNode(Opcode, VTs, Ops);
1614}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001615
Chris Lattner0437cdd2005-05-09 04:14:13 +00001616SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001617 assert((!V || isa<PointerType>(V->getType())) &&
1618 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001619 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1620 if (N) return SDOperand(N, 0);
1621
1622 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001623 AllNodes.push_back(N);
1624 return SDOperand(N, 0);
1625}
1626
Nate Begemanacc398c2006-01-25 18:21:52 +00001627SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1628 SDOperand Chain, SDOperand Ptr,
1629 SDOperand SV) {
1630 std::vector<SDOperand> Ops;
1631 Ops.reserve(3);
1632 Ops.push_back(Chain);
1633 Ops.push_back(Ptr);
1634 Ops.push_back(SV);
1635 std::vector<MVT::ValueType> VTs;
1636 VTs.reserve(2);
1637 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1638 return getNode(ISD::VAARG, VTs, Ops);
1639}
1640
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001641SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001642 std::vector<SDOperand> &Ops) {
1643 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001644 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001645 case 1: return getNode(Opcode, VT, Ops[0]);
1646 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1647 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001648 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001649 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001650
Chris Lattner89c34632005-05-14 06:20:26 +00001651 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001652 switch (Opcode) {
1653 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001654 case ISD::TRUNCSTORE: {
1655 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1656 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1657#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1658 // If this is a truncating store of a constant, convert to the desired type
1659 // and store it instead.
1660 if (isa<Constant>(Ops[0])) {
1661 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1662 if (isa<Constant>(Op))
1663 N1 = Op;
1664 }
1665 // Also for ConstantFP?
1666#endif
1667 if (Ops[0].getValueType() == EVT) // Normal store?
1668 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1669 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1670 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1671 "Can't do FP-INT conversion!");
1672 break;
1673 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001674 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001675 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001676 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1677 "LHS and RHS of condition must have same type!");
1678 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1679 "True and False arms of SelectCC must have same type!");
1680 assert(Ops[2].getValueType() == VT &&
1681 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001682 break;
1683 }
1684 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001685 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001686 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1687 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001688 break;
1689 }
Chris Lattneref847df2005-04-09 03:27:28 +00001690 }
1691
Chris Lattner385328c2005-05-14 07:42:29 +00001692 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001693 SDNode *N;
1694 if (VT != MVT::Flag) {
1695 SDNode *&E =
1696 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1697 if (E) return SDOperand(E, 0);
1698 E = N = new SDNode(Opcode, Ops);
1699 } else {
1700 N = new SDNode(Opcode, Ops);
1701 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001702 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001703 AllNodes.push_back(N);
1704 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001705}
1706
Chris Lattner89c34632005-05-14 06:20:26 +00001707SDOperand SelectionDAG::getNode(unsigned Opcode,
1708 std::vector<MVT::ValueType> &ResultTys,
1709 std::vector<SDOperand> &Ops) {
1710 if (ResultTys.size() == 1)
1711 return getNode(Opcode, ResultTys[0], Ops);
1712
Chris Lattner5f056bf2005-07-10 01:55:33 +00001713 switch (Opcode) {
1714 case ISD::EXTLOAD:
1715 case ISD::SEXTLOAD:
1716 case ISD::ZEXTLOAD: {
1717 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1718 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1719 // If they are asking for an extending load from/to the same thing, return a
1720 // normal load.
1721 if (ResultTys[0] == EVT)
1722 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001723 if (MVT::isVector(ResultTys[0])) {
1724 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1725 "Invalid vector extload!");
1726 } else {
1727 assert(EVT < ResultTys[0] &&
1728 "Should only be an extending load, not truncating!");
1729 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001730 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001731 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001732 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1733 "Cannot convert from FP to Int or Int -> FP!");
1734 break;
1735 }
1736
Chris Lattnere89083a2005-05-14 07:25:05 +00001737 // FIXME: figure out how to safely handle things like
1738 // int foo(int x) { return 1 << (x & 255); }
1739 // int bar() { return foo(256); }
1740#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001741 case ISD::SRA_PARTS:
1742 case ISD::SRL_PARTS:
1743 case ISD::SHL_PARTS:
1744 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001745 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001746 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1747 else if (N3.getOpcode() == ISD::AND)
1748 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1749 // If the and is only masking out bits that cannot effect the shift,
1750 // eliminate the and.
1751 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1752 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1753 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1754 }
1755 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001756#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001757 }
Chris Lattner89c34632005-05-14 06:20:26 +00001758
Chris Lattner43247a12005-08-25 19:12:10 +00001759 // Memoize the node unless it returns a flag.
1760 SDNode *N;
1761 if (ResultTys.back() != MVT::Flag) {
1762 SDNode *&E =
1763 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1764 if (E) return SDOperand(E, 0);
1765 E = N = new SDNode(Opcode, Ops);
1766 } else {
1767 N = new SDNode(Opcode, Ops);
1768 }
Chris Lattnera3255112005-11-08 23:30:28 +00001769 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001770 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001771 return SDOperand(N, 0);
1772}
1773
Chris Lattnera3255112005-11-08 23:30:28 +00001774void SelectionDAG::setNodeValueTypes(SDNode *N,
1775 std::vector<MVT::ValueType> &RetVals) {
1776 switch (RetVals.size()) {
1777 case 0: return;
1778 case 1: N->setValueTypes(RetVals[0]); return;
1779 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1780 default: break;
1781 }
1782
1783 std::list<std::vector<MVT::ValueType> >::iterator I =
1784 std::find(VTList.begin(), VTList.end(), RetVals);
1785 if (I == VTList.end()) {
1786 VTList.push_front(RetVals);
1787 I = VTList.begin();
1788 }
1789
1790 N->setValueTypes(&(*I)[0], I->size());
1791}
1792
1793void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1794 MVT::ValueType VT2) {
1795 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1796 E = VTList.end(); I != E; ++I) {
1797 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1798 N->setValueTypes(&(*I)[0], 2);
1799 return;
1800 }
1801 }
1802 std::vector<MVT::ValueType> V;
1803 V.push_back(VT1);
1804 V.push_back(VT2);
1805 VTList.push_front(V);
1806 N->setValueTypes(&(*VTList.begin())[0], 2);
1807}
1808
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001809/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1810/// specified operands. If the resultant node already exists in the DAG,
1811/// this does not modify the specified node, instead it returns the node that
1812/// already exists. If the resultant node does not exist in the DAG, the
1813/// input node is returned. As a degenerate case, if you specify the same
1814/// input operands as the node already has, the input node is returned.
1815SDOperand SelectionDAG::
1816UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1817 SDNode *N = InN.Val;
1818 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1819
1820 // Check to see if there is no change.
1821 if (Op == N->getOperand(0)) return InN;
1822
1823 // See if the modified node already exists.
1824 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1825 if (NewSlot && *NewSlot)
1826 return SDOperand(*NewSlot, InN.ResNo);
1827
1828 // Nope it doesn't. Remove the node from it's current place in the maps.
1829 if (NewSlot)
1830 RemoveNodeFromCSEMaps(N);
1831
1832 // Now we update the operands.
1833 N->OperandList[0].Val->removeUser(N);
1834 Op.Val->addUser(N);
1835 N->OperandList[0] = Op;
1836
1837 // If this gets put into a CSE map, add it.
1838 if (NewSlot) *NewSlot = N;
1839 return InN;
1840}
1841
1842SDOperand SelectionDAG::
1843UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1844 SDNode *N = InN.Val;
1845 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1846
1847 // Check to see if there is no change.
1848 bool AnyChange = false;
1849 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1850 return InN; // No operands changed, just return the input node.
1851
1852 // See if the modified node already exists.
1853 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1854 if (NewSlot && *NewSlot)
1855 return SDOperand(*NewSlot, InN.ResNo);
1856
1857 // Nope it doesn't. Remove the node from it's current place in the maps.
1858 if (NewSlot)
1859 RemoveNodeFromCSEMaps(N);
1860
1861 // Now we update the operands.
1862 if (N->OperandList[0] != Op1) {
1863 N->OperandList[0].Val->removeUser(N);
1864 Op1.Val->addUser(N);
1865 N->OperandList[0] = Op1;
1866 }
1867 if (N->OperandList[1] != Op2) {
1868 N->OperandList[1].Val->removeUser(N);
1869 Op2.Val->addUser(N);
1870 N->OperandList[1] = Op2;
1871 }
1872
1873 // If this gets put into a CSE map, add it.
1874 if (NewSlot) *NewSlot = N;
1875 return InN;
1876}
1877
1878SDOperand SelectionDAG::
1879UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1880 std::vector<SDOperand> Ops;
1881 Ops.push_back(Op1);
1882 Ops.push_back(Op2);
1883 Ops.push_back(Op3);
1884 return UpdateNodeOperands(N, Ops);
1885}
1886
1887SDOperand SelectionDAG::
1888UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1889 SDOperand Op3, SDOperand Op4) {
1890 std::vector<SDOperand> Ops;
1891 Ops.push_back(Op1);
1892 Ops.push_back(Op2);
1893 Ops.push_back(Op3);
1894 Ops.push_back(Op4);
1895 return UpdateNodeOperands(N, Ops);
1896}
1897
1898SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001899UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1900 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1901 std::vector<SDOperand> Ops;
1902 Ops.push_back(Op1);
1903 Ops.push_back(Op2);
1904 Ops.push_back(Op3);
1905 Ops.push_back(Op4);
1906 Ops.push_back(Op5);
1907 return UpdateNodeOperands(N, Ops);
1908}
1909
1910
1911SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001912UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1913 SDNode *N = InN.Val;
1914 assert(N->getNumOperands() == Ops.size() &&
1915 "Update with wrong number of operands");
1916
1917 // Check to see if there is no change.
1918 unsigned NumOps = Ops.size();
1919 bool AnyChange = false;
1920 for (unsigned i = 0; i != NumOps; ++i) {
1921 if (Ops[i] != N->getOperand(i)) {
1922 AnyChange = true;
1923 break;
1924 }
1925 }
1926
1927 // No operands changed, just return the input node.
1928 if (!AnyChange) return InN;
1929
1930 // See if the modified node already exists.
1931 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1932 if (NewSlot && *NewSlot)
1933 return SDOperand(*NewSlot, InN.ResNo);
1934
1935 // Nope it doesn't. Remove the node from it's current place in the maps.
1936 if (NewSlot)
1937 RemoveNodeFromCSEMaps(N);
1938
1939 // Now we update the operands.
1940 for (unsigned i = 0; i != NumOps; ++i) {
1941 if (N->OperandList[i] != Ops[i]) {
1942 N->OperandList[i].Val->removeUser(N);
1943 Ops[i].Val->addUser(N);
1944 N->OperandList[i] = Ops[i];
1945 }
1946 }
1947
1948 // If this gets put into a CSE map, add it.
1949 if (NewSlot) *NewSlot = N;
1950 return InN;
1951}
1952
1953
1954
Chris Lattner149c58c2005-08-16 18:17:10 +00001955
1956/// SelectNodeTo - These are used for target selectors to *mutate* the
1957/// specified node to have the specified return type, Target opcode, and
1958/// operands. Note that target opcodes are stored as
1959/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001960///
1961/// Note that SelectNodeTo returns the resultant node. If there is already a
1962/// node of the specified opcode and operands, it returns that node instead of
1963/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001964SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1965 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001966 // If an identical node already exists, use it.
1967 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1968 if (ON) return SDOperand(ON, 0);
1969
Chris Lattner7651fa42005-08-24 23:00:29 +00001970 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001971
Chris Lattner7651fa42005-08-24 23:00:29 +00001972 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1973 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001974
1975 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001976 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001977}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001978
Chris Lattnereb19e402005-11-30 22:45:14 +00001979SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1980 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001981 // If an identical node already exists, use it.
1982 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1983 std::make_pair(Op1, VT))];
1984 if (ON) return SDOperand(ON, 0);
1985
Chris Lattner149c58c2005-08-16 18:17:10 +00001986 RemoveNodeFromCSEMaps(N);
1987 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1988 N->setValueTypes(VT);
1989 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001990
1991 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001992 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001993}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001994
Chris Lattnereb19e402005-11-30 22:45:14 +00001995SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1996 MVT::ValueType VT, SDOperand Op1,
1997 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001998 // If an identical node already exists, use it.
1999 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2000 std::make_pair(Op1, Op2))];
2001 if (ON) return SDOperand(ON, 0);
2002
Chris Lattner149c58c2005-08-16 18:17:10 +00002003 RemoveNodeFromCSEMaps(N);
2004 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2005 N->setValueTypes(VT);
2006 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002007
2008 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002009 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002010}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002011
Chris Lattnereb19e402005-11-30 22:45:14 +00002012SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2013 MVT::ValueType VT, SDOperand Op1,
2014 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002015 // If an identical node already exists, use it.
2016 std::vector<SDOperand> OpList;
2017 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2018 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2019 std::make_pair(VT, OpList))];
2020 if (ON) return SDOperand(ON, 0);
2021
Chris Lattner149c58c2005-08-16 18:17:10 +00002022 RemoveNodeFromCSEMaps(N);
2023 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2024 N->setValueTypes(VT);
2025 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002026
2027 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002028 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00002029}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00002030
Chris Lattnereb19e402005-11-30 22:45:14 +00002031SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2032 MVT::ValueType VT, SDOperand Op1,
2033 SDOperand Op2, SDOperand Op3,
2034 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002035 // If an identical node already exists, use it.
2036 std::vector<SDOperand> OpList;
2037 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2038 OpList.push_back(Op4);
2039 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2040 std::make_pair(VT, OpList))];
2041 if (ON) return SDOperand(ON, 0);
2042
Nate Begeman294a0a12005-08-18 07:30:15 +00002043 RemoveNodeFromCSEMaps(N);
2044 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2045 N->setValueTypes(VT);
2046 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002047
2048 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002049 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00002050}
Chris Lattner0fb094f2005-11-19 01:44:53 +00002051
Chris Lattnereb19e402005-11-30 22:45:14 +00002052SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2053 MVT::ValueType VT, SDOperand Op1,
2054 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2055 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002056 // If an identical node already exists, use it.
2057 std::vector<SDOperand> OpList;
2058 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2059 OpList.push_back(Op4); OpList.push_back(Op5);
2060 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2061 std::make_pair(VT, OpList))];
2062 if (ON) return SDOperand(ON, 0);
2063
Chris Lattner6b09a292005-08-21 18:49:33 +00002064 RemoveNodeFromCSEMaps(N);
2065 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2066 N->setValueTypes(VT);
2067 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002068
2069 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002070 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00002071}
Chris Lattner149c58c2005-08-16 18:17:10 +00002072
Chris Lattnereb19e402005-11-30 22:45:14 +00002073SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2074 MVT::ValueType VT, SDOperand Op1,
2075 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2076 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002077 // If an identical node already exists, use it.
2078 std::vector<SDOperand> OpList;
2079 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2080 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2081 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2082 std::make_pair(VT, OpList))];
2083 if (ON) return SDOperand(ON, 0);
2084
Evan Cheng61ca74b2005-11-30 02:04:11 +00002085 RemoveNodeFromCSEMaps(N);
2086 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2087 N->setValueTypes(VT);
2088 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002089
2090 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002091 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002092}
2093
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002094SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2095 MVT::ValueType VT, SDOperand Op1,
2096 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2097 SDOperand Op5, SDOperand Op6,
2098 SDOperand Op7) {
2099 // If an identical node already exists, use it.
2100 std::vector<SDOperand> OpList;
2101 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2102 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2103 OpList.push_back(Op7);
2104 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2105 std::make_pair(VT, OpList))];
2106 if (ON) return SDOperand(ON, 0);
2107
2108 RemoveNodeFromCSEMaps(N);
2109 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2110 N->setValueTypes(VT);
2111 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2112
2113 ON = N; // Memoize the new node.
2114 return SDOperand(N, 0);
2115}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002116SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2117 MVT::ValueType VT, SDOperand Op1,
2118 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2119 SDOperand Op5, SDOperand Op6,
2120 SDOperand Op7, SDOperand Op8) {
2121 // If an identical node already exists, use it.
2122 std::vector<SDOperand> OpList;
2123 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2124 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2125 OpList.push_back(Op7); OpList.push_back(Op8);
2126 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2127 std::make_pair(VT, OpList))];
2128 if (ON) return SDOperand(ON, 0);
2129
2130 RemoveNodeFromCSEMaps(N);
2131 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2132 N->setValueTypes(VT);
2133 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2134
2135 ON = N; // Memoize the new node.
2136 return SDOperand(N, 0);
2137}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002138
Chris Lattnereb19e402005-11-30 22:45:14 +00002139SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2140 MVT::ValueType VT1, MVT::ValueType VT2,
2141 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002142 // If an identical node already exists, use it.
2143 std::vector<SDOperand> OpList;
2144 OpList.push_back(Op1); OpList.push_back(Op2);
2145 std::vector<MVT::ValueType> VTList;
2146 VTList.push_back(VT1); VTList.push_back(VT2);
2147 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2148 std::make_pair(VTList, OpList))];
2149 if (ON) return SDOperand(ON, 0);
2150
Chris Lattner0fb094f2005-11-19 01:44:53 +00002151 RemoveNodeFromCSEMaps(N);
2152 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2153 setNodeValueTypes(N, VT1, VT2);
2154 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002155
2156 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002157 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002158}
2159
Chris Lattnereb19e402005-11-30 22:45:14 +00002160SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2161 MVT::ValueType VT1, MVT::ValueType VT2,
2162 SDOperand Op1, SDOperand Op2,
2163 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002164 // If an identical node already exists, use it.
2165 std::vector<SDOperand> OpList;
2166 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2167 std::vector<MVT::ValueType> VTList;
2168 VTList.push_back(VT1); VTList.push_back(VT2);
2169 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2170 std::make_pair(VTList, OpList))];
2171 if (ON) return SDOperand(ON, 0);
2172
Chris Lattner0fb094f2005-11-19 01:44:53 +00002173 RemoveNodeFromCSEMaps(N);
2174 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2175 setNodeValueTypes(N, VT1, VT2);
2176 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002177
2178 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002179 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002180}
2181
Chris Lattnereb19e402005-11-30 22:45:14 +00002182SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2183 MVT::ValueType VT1, MVT::ValueType VT2,
2184 SDOperand Op1, SDOperand Op2,
2185 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002186 // If an identical node already exists, use it.
2187 std::vector<SDOperand> OpList;
2188 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2189 OpList.push_back(Op4);
2190 std::vector<MVT::ValueType> VTList;
2191 VTList.push_back(VT1); VTList.push_back(VT2);
2192 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2193 std::make_pair(VTList, OpList))];
2194 if (ON) return SDOperand(ON, 0);
2195
Chris Lattner0fb094f2005-11-19 01:44:53 +00002196 RemoveNodeFromCSEMaps(N);
2197 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2198 setNodeValueTypes(N, VT1, VT2);
2199 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002200
2201 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002202 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002203}
2204
Chris Lattnereb19e402005-11-30 22:45:14 +00002205SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2206 MVT::ValueType VT1, MVT::ValueType VT2,
2207 SDOperand Op1, SDOperand Op2,
2208 SDOperand Op3, SDOperand Op4,
2209 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002210 // If an identical node already exists, use it.
2211 std::vector<SDOperand> OpList;
2212 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2213 OpList.push_back(Op4); OpList.push_back(Op5);
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, Op4, Op5);
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
Evan Cheng6ae46c42006-02-09 07:15:23 +00002229/// getTargetNode - These are used for target selectors to create a new node
2230/// with specified return type(s), target opcode, and operands.
2231///
2232/// Note that getTargetNode returns the resultant node. If there is already a
2233/// node of the specified opcode and operands, it returns that node instead of
2234/// the current one.
2235SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2236 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2237}
2238SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2239 SDOperand Op1) {
2240 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2241}
2242SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2243 SDOperand Op1, SDOperand Op2) {
2244 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2245}
2246SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2247 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2248 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2249}
2250SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2251 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2252 SDOperand Op4) {
2253 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2254}
2255SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2256 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2257 SDOperand Op4, SDOperand Op5) {
2258 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2259}
2260SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2261 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2262 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2263 std::vector<SDOperand> Ops;
2264 Ops.reserve(6);
2265 Ops.push_back(Op1);
2266 Ops.push_back(Op2);
2267 Ops.push_back(Op3);
2268 Ops.push_back(Op4);
2269 Ops.push_back(Op5);
2270 Ops.push_back(Op6);
2271 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2272}
2273SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2274 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2275 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2276 SDOperand Op7) {
2277 std::vector<SDOperand> Ops;
2278 Ops.reserve(7);
2279 Ops.push_back(Op1);
2280 Ops.push_back(Op2);
2281 Ops.push_back(Op3);
2282 Ops.push_back(Op4);
2283 Ops.push_back(Op5);
2284 Ops.push_back(Op6);
2285 Ops.push_back(Op7);
2286 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2287}
2288SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2289 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2290 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2291 SDOperand Op7, SDOperand Op8) {
2292 std::vector<SDOperand> Ops;
2293 Ops.reserve(8);
2294 Ops.push_back(Op1);
2295 Ops.push_back(Op2);
2296 Ops.push_back(Op3);
2297 Ops.push_back(Op4);
2298 Ops.push_back(Op5);
2299 Ops.push_back(Op6);
2300 Ops.push_back(Op7);
2301 Ops.push_back(Op8);
2302 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2303}
2304SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2305 std::vector<SDOperand> &Ops) {
2306 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2307}
2308SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2309 MVT::ValueType VT2, SDOperand Op1) {
2310 std::vector<MVT::ValueType> ResultTys;
2311 ResultTys.push_back(VT1);
2312 ResultTys.push_back(VT2);
2313 std::vector<SDOperand> Ops;
2314 Ops.push_back(Op1);
2315 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2316}
2317SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2318 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2319 std::vector<MVT::ValueType> ResultTys;
2320 ResultTys.push_back(VT1);
2321 ResultTys.push_back(VT2);
2322 std::vector<SDOperand> Ops;
2323 Ops.push_back(Op1);
2324 Ops.push_back(Op2);
2325 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2326}
2327SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2328 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2329 SDOperand Op3) {
2330 std::vector<MVT::ValueType> ResultTys;
2331 ResultTys.push_back(VT1);
2332 ResultTys.push_back(VT2);
2333 std::vector<SDOperand> Ops;
2334 Ops.push_back(Op1);
2335 Ops.push_back(Op2);
2336 Ops.push_back(Op3);
2337 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2338}
2339SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2340 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2341 SDOperand Op3, SDOperand Op4) {
2342 std::vector<MVT::ValueType> ResultTys;
2343 ResultTys.push_back(VT1);
2344 ResultTys.push_back(VT2);
2345 std::vector<SDOperand> Ops;
2346 Ops.push_back(Op1);
2347 Ops.push_back(Op2);
2348 Ops.push_back(Op3);
2349 Ops.push_back(Op4);
2350 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2351}
2352SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2353 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2354 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2355 std::vector<MVT::ValueType> ResultTys;
2356 ResultTys.push_back(VT1);
2357 ResultTys.push_back(VT2);
2358 std::vector<SDOperand> Ops;
2359 Ops.push_back(Op1);
2360 Ops.push_back(Op2);
2361 Ops.push_back(Op3);
2362 Ops.push_back(Op4);
2363 Ops.push_back(Op5);
2364 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2365}
2366SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2367 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2368 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2369 SDOperand Op6) {
2370 std::vector<MVT::ValueType> ResultTys;
2371 ResultTys.push_back(VT1);
2372 ResultTys.push_back(VT2);
2373 std::vector<SDOperand> Ops;
2374 Ops.push_back(Op1);
2375 Ops.push_back(Op2);
2376 Ops.push_back(Op3);
2377 Ops.push_back(Op4);
2378 Ops.push_back(Op5);
2379 Ops.push_back(Op6);
2380 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2381}
2382SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2383 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2384 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2385 SDOperand Op6, SDOperand Op7) {
2386 std::vector<MVT::ValueType> ResultTys;
2387 ResultTys.push_back(VT1);
2388 ResultTys.push_back(VT2);
2389 std::vector<SDOperand> Ops;
2390 Ops.push_back(Op1);
2391 Ops.push_back(Op2);
2392 Ops.push_back(Op3);
2393 Ops.push_back(Op4);
2394 Ops.push_back(Op5);
2395 Ops.push_back(Op6);
2396 Ops.push_back(Op7);
2397 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2398}
2399SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2400 MVT::ValueType VT2, MVT::ValueType VT3,
2401 SDOperand Op1, SDOperand Op2) {
2402 std::vector<MVT::ValueType> ResultTys;
2403 ResultTys.push_back(VT1);
2404 ResultTys.push_back(VT2);
2405 ResultTys.push_back(VT3);
2406 std::vector<SDOperand> Ops;
2407 Ops.push_back(Op1);
2408 Ops.push_back(Op2);
2409 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2410}
2411SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2412 MVT::ValueType VT2, MVT::ValueType VT3,
2413 SDOperand Op1, SDOperand Op2,
2414 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2415 std::vector<MVT::ValueType> ResultTys;
2416 ResultTys.push_back(VT1);
2417 ResultTys.push_back(VT2);
2418 ResultTys.push_back(VT3);
2419 std::vector<SDOperand> Ops;
2420 Ops.push_back(Op1);
2421 Ops.push_back(Op2);
2422 Ops.push_back(Op3);
2423 Ops.push_back(Op4);
2424 Ops.push_back(Op5);
2425 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2426}
2427SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2428 MVT::ValueType VT2, MVT::ValueType VT3,
2429 SDOperand Op1, SDOperand Op2,
2430 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2431 SDOperand Op6) {
2432 std::vector<MVT::ValueType> ResultTys;
2433 ResultTys.push_back(VT1);
2434 ResultTys.push_back(VT2);
2435 ResultTys.push_back(VT3);
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 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2444}
2445SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2446 MVT::ValueType VT2, MVT::ValueType VT3,
2447 SDOperand Op1, SDOperand Op2,
2448 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2449 SDOperand Op6, SDOperand Op7) {
2450 std::vector<MVT::ValueType> ResultTys;
2451 ResultTys.push_back(VT1);
2452 ResultTys.push_back(VT2);
2453 ResultTys.push_back(VT3);
2454 std::vector<SDOperand> Ops;
2455 Ops.push_back(Op1);
2456 Ops.push_back(Op2);
2457 Ops.push_back(Op3);
2458 Ops.push_back(Op4);
2459 Ops.push_back(Op5);
2460 Ops.push_back(Op6);
2461 Ops.push_back(Op7);
2462 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2463}
2464SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2465 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2466 std::vector<MVT::ValueType> ResultTys;
2467 ResultTys.push_back(VT1);
2468 ResultTys.push_back(VT2);
2469 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2470}
2471
Chris Lattner0fb094f2005-11-19 01:44:53 +00002472// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002473/// This can cause recursive merging of nodes in the DAG.
2474///
Chris Lattner8b52f212005-08-26 18:36:28 +00002475/// This version assumes From/To have a single result value.
2476///
Chris Lattner1e111c72005-09-07 05:37:01 +00002477void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2478 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002479 SDNode *From = FromN.Val, *To = ToN.Val;
2480 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2481 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002482 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002483
Chris Lattner8b8749f2005-08-17 19:00:20 +00002484 while (!From->use_empty()) {
2485 // Process users until they are all gone.
2486 SDNode *U = *From->use_begin();
2487
2488 // This node is about to morph, remove its old self from the CSE maps.
2489 RemoveNodeFromCSEMaps(U);
2490
Chris Lattner65113b22005-11-08 22:07:03 +00002491 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2492 I != E; ++I)
2493 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002494 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002495 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002496 To->addUser(U);
2497 }
2498
2499 // Now that we have modified U, add it back to the CSE maps. If it already
2500 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002501 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2502 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002503 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002504 if (Deleted) Deleted->push_back(U);
2505 DeleteNodeNotInCSEMaps(U);
2506 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002507 }
2508}
2509
2510/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2511/// This can cause recursive merging of nodes in the DAG.
2512///
2513/// This version assumes From/To have matching types and numbers of result
2514/// values.
2515///
Chris Lattner1e111c72005-09-07 05:37:01 +00002516void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2517 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002518 assert(From != To && "Cannot replace uses of with self");
2519 assert(From->getNumValues() == To->getNumValues() &&
2520 "Cannot use this version of ReplaceAllUsesWith!");
2521 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002522 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002523 return;
2524 }
2525
2526 while (!From->use_empty()) {
2527 // Process users until they are all gone.
2528 SDNode *U = *From->use_begin();
2529
2530 // This node is about to morph, remove its old self from the CSE maps.
2531 RemoveNodeFromCSEMaps(U);
2532
Chris Lattner65113b22005-11-08 22:07:03 +00002533 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2534 I != E; ++I)
2535 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002536 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002537 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002538 To->addUser(U);
2539 }
2540
2541 // Now that we have modified U, add it back to the CSE maps. If it already
2542 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002543 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2544 ReplaceAllUsesWith(U, Existing, Deleted);
2545 // U is now dead.
2546 if (Deleted) Deleted->push_back(U);
2547 DeleteNodeNotInCSEMaps(U);
2548 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002549 }
2550}
2551
Chris Lattner8b52f212005-08-26 18:36:28 +00002552/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2553/// This can cause recursive merging of nodes in the DAG.
2554///
2555/// This version can replace From with any result values. To must match the
2556/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002557void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002558 const std::vector<SDOperand> &To,
2559 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002560 assert(From->getNumValues() == To.size() &&
2561 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002562 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002563 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002564 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002565 return;
2566 }
2567
2568 while (!From->use_empty()) {
2569 // Process users until they are all gone.
2570 SDNode *U = *From->use_begin();
2571
2572 // This node is about to morph, remove its old self from the CSE maps.
2573 RemoveNodeFromCSEMaps(U);
2574
Chris Lattner65113b22005-11-08 22:07:03 +00002575 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2576 I != E; ++I)
2577 if (I->Val == From) {
2578 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002579 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002580 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002581 ToOp.Val->addUser(U);
2582 }
2583
2584 // Now that we have modified U, add it back to the CSE maps. If it already
2585 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002586 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2587 ReplaceAllUsesWith(U, Existing, Deleted);
2588 // U is now dead.
2589 if (Deleted) Deleted->push_back(U);
2590 DeleteNodeNotInCSEMaps(U);
2591 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002592 }
2593}
2594
Chris Lattner012f2412006-02-17 21:58:01 +00002595/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2596/// uses of other values produced by From.Val alone. The Deleted vector is
2597/// handled the same was as for ReplaceAllUsesWith.
2598void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2599 std::vector<SDNode*> &Deleted) {
2600 assert(From != To && "Cannot replace a value with itself");
2601 // Handle the simple, trivial, case efficiently.
2602 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2603 ReplaceAllUsesWith(From, To, &Deleted);
2604 return;
2605 }
2606
2607 // Get all of the users in a nice, deterministically ordered, uniqued set.
2608 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2609
2610 while (!Users.empty()) {
2611 // We know that this user uses some value of From. If it is the right
2612 // value, update it.
2613 SDNode *User = Users.back();
2614 Users.pop_back();
2615
2616 for (SDOperand *Op = User->OperandList,
2617 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2618 if (*Op == From) {
2619 // Okay, we know this user needs to be updated. Remove its old self
2620 // from the CSE maps.
2621 RemoveNodeFromCSEMaps(User);
2622
2623 // Update all operands that match "From".
2624 for (; Op != E; ++Op) {
2625 if (*Op == From) {
2626 From.Val->removeUser(User);
2627 *Op = To;
2628 To.Val->addUser(User);
2629 }
2630 }
2631
2632 // Now that we have modified User, add it back to the CSE maps. If it
2633 // already exists there, recursively merge the results together.
2634 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2635 unsigned NumDeleted = Deleted.size();
2636 ReplaceAllUsesWith(User, Existing, &Deleted);
2637
2638 // User is now dead.
2639 Deleted.push_back(User);
2640 DeleteNodeNotInCSEMaps(User);
2641
2642 // We have to be careful here, because ReplaceAllUsesWith could have
2643 // deleted a user of From, which means there may be dangling pointers
2644 // in the "Users" setvector. Scan over the deleted node pointers and
2645 // remove them from the setvector.
2646 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2647 Users.remove(Deleted[i]);
2648 }
2649 break; // Exit the operand scanning loop.
2650 }
2651 }
2652 }
2653}
2654
Chris Lattner7b2880c2005-08-24 22:44:39 +00002655
Jim Laskey58b968b2005-08-17 20:08:02 +00002656//===----------------------------------------------------------------------===//
2657// SDNode Class
2658//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002659
Chris Lattnera3255112005-11-08 23:30:28 +00002660
2661/// getValueTypeList - Return a pointer to the specified value type.
2662///
2663MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2664 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2665 VTs[VT] = VT;
2666 return &VTs[VT];
2667}
2668
Chris Lattner5c884562005-01-12 18:37:47 +00002669/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2670/// indicated value. This method ignores uses of other values defined by this
2671/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002672bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002673 assert(Value < getNumValues() && "Bad value!");
2674
2675 // If there is only one value, this is easy.
2676 if (getNumValues() == 1)
2677 return use_size() == NUses;
2678 if (Uses.size() < NUses) return false;
2679
Evan Cheng4ee62112006-02-05 06:29:23 +00002680 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002681
2682 std::set<SDNode*> UsersHandled;
2683
Evan Cheng4ee62112006-02-05 06:29:23 +00002684 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002685 UI != E; ++UI) {
2686 SDNode *User = *UI;
2687 if (User->getNumOperands() == 1 ||
2688 UsersHandled.insert(User).second) // First time we've seen this?
2689 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2690 if (User->getOperand(i) == TheValue) {
2691 if (NUses == 0)
2692 return false; // too many uses
2693 --NUses;
2694 }
2695 }
2696
2697 // Found exactly the right number of uses?
2698 return NUses == 0;
2699}
2700
2701
Evan Cheng4ee62112006-02-05 06:29:23 +00002702// isOnlyUse - Return true if this node is the only use of N.
2703bool SDNode::isOnlyUse(SDNode *N) const {
2704 bool Seen = false;
2705 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2706 SDNode *User = *I;
2707 if (User == this)
2708 Seen = true;
2709 else
2710 return false;
2711 }
2712
2713 return Seen;
2714}
2715
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002716// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002717bool SDOperand::isOperand(SDNode *N) const {
2718 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2719 if (*this == N->getOperand(i))
2720 return true;
2721 return false;
2722}
2723
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002724bool SDNode::isOperand(SDNode *N) const {
2725 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2726 if (this == N->OperandList[i].Val)
2727 return true;
2728 return false;
2729}
Evan Cheng4ee62112006-02-05 06:29:23 +00002730
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002731const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002732 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002733 default:
2734 if (getOpcode() < ISD::BUILTIN_OP_END)
2735 return "<<Unknown DAG Node>>";
2736 else {
Evan Cheng72261582005-12-20 06:22:03 +00002737 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002738 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002739 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2740 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002741
Evan Cheng72261582005-12-20 06:22:03 +00002742 TargetLowering &TLI = G->getTargetLoweringInfo();
2743 const char *Name =
2744 TLI.getTargetNodeName(getOpcode());
2745 if (Name) return Name;
2746 }
2747
2748 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002749 }
2750
Andrew Lenharth95762122005-03-31 21:24:06 +00002751 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002752 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002753 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002754 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002755 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002756 case ISD::AssertSext: return "AssertSext";
2757 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002758
2759 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002760 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002761 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002762 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002763
2764 case ISD::Constant: return "Constant";
2765 case ISD::ConstantFP: return "ConstantFP";
2766 case ISD::GlobalAddress: return "GlobalAddress";
2767 case ISD::FrameIndex: return "FrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002768 case ISD::JumpTable: return "JumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002769 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002770 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner48b61a72006-03-28 00:40:33 +00002771 case ISD::INTRINSIC_WO_CHAIN: {
2772 unsigned IID = cast<ConstantSDNode>(getOperand(0))->getValue();
2773 return Intrinsic::getName((Intrinsic::ID)IID);
2774 }
2775 case ISD::INTRINSIC_VOID:
2776 case ISD::INTRINSIC_W_CHAIN: {
2777 unsigned IID = cast<ConstantSDNode>(getOperand(1))->getValue();
Chris Lattner70a248d2006-03-27 06:45:25 +00002778 return Intrinsic::getName((Intrinsic::ID)IID);
Chris Lattner401ec7f2006-03-27 16:10:59 +00002779 }
Evan Cheng1ab7d852006-03-01 00:51:13 +00002780
Chris Lattnerb2827b02006-03-19 00:52:58 +00002781 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002782 case ISD::TargetConstant: return "TargetConstant";
2783 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002784 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2785 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Nate Begeman37efe672006-04-22 18:53:45 +00002786 case ISD::TargetJumpTable: return "TargetJumpTable";
Chris Lattner5839bf22005-08-26 17:15:30 +00002787 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002788 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002789
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002790 case ISD::CopyToReg: return "CopyToReg";
2791 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002792 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002793 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002794 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002795 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerfdfded52006-04-12 16:20:43 +00002796 case ISD::FORMAL_ARGUMENTS: return "formal_arguments";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002797
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002798 // Unary operators
2799 case ISD::FABS: return "fabs";
2800 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002801 case ISD::FSQRT: return "fsqrt";
2802 case ISD::FSIN: return "fsin";
2803 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002804
2805 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002806 case ISD::ADD: return "add";
2807 case ISD::SUB: return "sub";
2808 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002809 case ISD::MULHU: return "mulhu";
2810 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002811 case ISD::SDIV: return "sdiv";
2812 case ISD::UDIV: return "udiv";
2813 case ISD::SREM: return "srem";
2814 case ISD::UREM: return "urem";
2815 case ISD::AND: return "and";
2816 case ISD::OR: return "or";
2817 case ISD::XOR: return "xor";
2818 case ISD::SHL: return "shl";
2819 case ISD::SRA: return "sra";
2820 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002821 case ISD::ROTL: return "rotl";
2822 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002823 case ISD::FADD: return "fadd";
2824 case ISD::FSUB: return "fsub";
2825 case ISD::FMUL: return "fmul";
2826 case ISD::FDIV: return "fdiv";
2827 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002828 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002829 case ISD::VADD: return "vadd";
2830 case ISD::VSUB: return "vsub";
2831 case ISD::VMUL: return "vmul";
Chris Lattner97d23332006-04-02 02:41:18 +00002832 case ISD::VSDIV: return "vsdiv";
2833 case ISD::VUDIV: return "vudiv";
2834 case ISD::VAND: return "vand";
2835 case ISD::VOR: return "vor";
2836 case ISD::VXOR: return "vxor";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002837
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002838 case ISD::SETCC: return "setcc";
2839 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002840 case ISD::SELECT_CC: return "select_cc";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002841 case ISD::VSELECT: return "vselect";
2842 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2843 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
2844 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002845 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerb22e35a2006-04-08 22:22:57 +00002846 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
2847 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2848 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
2849 case ISD::VVECTOR_SHUFFLE: return "vvector_shuffle";
2850 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002851 case ISD::ADDC: return "addc";
2852 case ISD::ADDE: return "adde";
2853 case ISD::SUBC: return "subc";
2854 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002855 case ISD::SHL_PARTS: return "shl_parts";
2856 case ISD::SRA_PARTS: return "sra_parts";
2857 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002858
Chris Lattner7f644642005-04-28 21:44:03 +00002859 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002860 case ISD::SIGN_EXTEND: return "sign_extend";
2861 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002862 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002863 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002864 case ISD::TRUNCATE: return "truncate";
2865 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002866 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002867 case ISD::FP_EXTEND: return "fp_extend";
2868
2869 case ISD::SINT_TO_FP: return "sint_to_fp";
2870 case ISD::UINT_TO_FP: return "uint_to_fp";
2871 case ISD::FP_TO_SINT: return "fp_to_sint";
2872 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002873 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002874
2875 // Control flow instructions
2876 case ISD::BR: return "br";
Nate Begeman37efe672006-04-22 18:53:45 +00002877 case ISD::BRIND: return "brind";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002878 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002879 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002880 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002881 case ISD::CALLSEQ_START: return "callseq_start";
2882 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002883
2884 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002885 case ISD::LOAD: return "load";
2886 case ISD::STORE: return "store";
2887 case ISD::VLOAD: return "vload";
2888 case ISD::EXTLOAD: return "extload";
2889 case ISD::SEXTLOAD: return "sextload";
2890 case ISD::ZEXTLOAD: return "zextload";
2891 case ISD::TRUNCSTORE: return "truncstore";
2892 case ISD::VAARG: return "vaarg";
2893 case ISD::VACOPY: return "vacopy";
2894 case ISD::VAEND: return "vaend";
2895 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002896 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002897 case ISD::EXTRACT_ELEMENT: return "extract_element";
2898 case ISD::BUILD_PAIR: return "build_pair";
2899 case ISD::STACKSAVE: return "stacksave";
2900 case ISD::STACKRESTORE: return "stackrestore";
2901
2902 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002903 case ISD::MEMSET: return "memset";
2904 case ISD::MEMCPY: return "memcpy";
2905 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002906
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002907 // Bit manipulation
2908 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002909 case ISD::CTPOP: return "ctpop";
2910 case ISD::CTTZ: return "cttz";
2911 case ISD::CTLZ: return "ctlz";
2912
Chris Lattner36ce6912005-11-29 06:21:05 +00002913 // Debug info
2914 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002915 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002916 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002917
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002918 case ISD::CONDCODE:
2919 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002920 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002921 case ISD::SETOEQ: return "setoeq";
2922 case ISD::SETOGT: return "setogt";
2923 case ISD::SETOGE: return "setoge";
2924 case ISD::SETOLT: return "setolt";
2925 case ISD::SETOLE: return "setole";
2926 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002927
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002928 case ISD::SETO: return "seto";
2929 case ISD::SETUO: return "setuo";
2930 case ISD::SETUEQ: return "setue";
2931 case ISD::SETUGT: return "setugt";
2932 case ISD::SETUGE: return "setuge";
2933 case ISD::SETULT: return "setult";
2934 case ISD::SETULE: return "setule";
2935 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002936
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002937 case ISD::SETEQ: return "seteq";
2938 case ISD::SETGT: return "setgt";
2939 case ISD::SETGE: return "setge";
2940 case ISD::SETLT: return "setlt";
2941 case ISD::SETLE: return "setle";
2942 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002943 }
2944 }
2945}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002946
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002947void SDNode::dump() const { dump(0); }
2948void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002949 std::cerr << (void*)this << ": ";
2950
2951 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2952 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002953 if (getValueType(i) == MVT::Other)
2954 std::cerr << "ch";
2955 else
2956 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002957 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002958 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002959
2960 std::cerr << " ";
2961 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2962 if (i) std::cerr << ", ";
2963 std::cerr << (void*)getOperand(i).Val;
2964 if (unsigned RN = getOperand(i).ResNo)
2965 std::cerr << ":" << RN;
2966 }
2967
2968 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2969 std::cerr << "<" << CSDN->getValue() << ">";
2970 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2971 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002972 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002973 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002974 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002975 std::cerr << "<";
2976 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002977 if (offset > 0)
2978 std::cerr << " + " << offset;
2979 else
2980 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002981 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002982 std::cerr << "<" << FIDN->getIndex() << ">";
2983 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002984 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002985 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002986 if (offset > 0)
2987 std::cerr << " + " << offset;
2988 else
2989 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002990 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002991 std::cerr << "<";
2992 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2993 if (LBB)
2994 std::cerr << LBB->getName() << " ";
2995 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002996 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002997 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002998 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2999 } else {
3000 std::cerr << " #" << R->getReg();
3001 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003002 } else if (const ExternalSymbolSDNode *ES =
3003 dyn_cast<ExternalSymbolSDNode>(this)) {
3004 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00003005 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
3006 if (M->getValue())
3007 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
3008 else
3009 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00003010 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
3011 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00003012 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00003013}
3014
Chris Lattnerde202b32005-11-09 23:47:37 +00003015static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003016 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
3017 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003018 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003019 else
3020 std::cerr << "\n" << std::string(indent+2, ' ')
3021 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00003022
Chris Lattnerea946cd2005-01-09 20:38:33 +00003023
3024 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003025 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003026}
3027
Chris Lattnerc3aae252005-01-07 07:46:32 +00003028void SelectionDAG::dump() const {
3029 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00003030 std::vector<const SDNode*> Nodes;
3031 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
3032 I != E; ++I)
3033 Nodes.push_back(I);
3034
Chris Lattner49d24712005-01-09 20:26:36 +00003035 std::sort(Nodes.begin(), Nodes.end());
3036
3037 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00003038 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003039 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00003040 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00003041
Chris Lattnerf3e133a2005-08-16 18:33:07 +00003042 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00003043
Chris Lattnerc3aae252005-01-07 07:46:32 +00003044 std::cerr << "\n\n";
3045}
3046
Evan Chengfae9f1c2006-02-09 22:11:03 +00003047/// InsertISelMapEntry - A helper function to insert a key / element pair
3048/// into a SDOperand to SDOperand map. This is added to avoid the map
3049/// insertion operator from being inlined.
3050void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
3051 SDNode *Key, unsigned KeyResNo,
3052 SDNode *Element, unsigned ElementResNo) {
3053 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
3054 SDOperand(Element, ElementResNo)));
3055}