blob: 8fec8a268be8cbd3a1988bc21e5a36785530f2b5 [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) {
76 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Chris Lattner61d43992006-03-25 22:57:01 +000077
78 unsigned i = 0, e = N->getNumOperands();
79
80 // Skip over all of the undef values.
81 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
82 ++i;
83
84 // Do not accept an all-undef vector.
85 if (i == e) return false;
86
87 // Do not accept build_vectors that aren't all constants or which have non-~0
88 // elements.
Chris Lattner452e8352006-03-25 22:59:28 +000089 SDOperand NotZero = N->getOperand(i);
Evan Chenga8df1662006-03-27 06:58:47 +000090 if (isa<ConstantSDNode>(NotZero)) {
91 if (!cast<ConstantSDNode>(NotZero)->isAllOnesValue())
92 return false;
93 } else if (isa<ConstantFPSDNode>(NotZero)) {
Evan Chengf48b50a2006-03-27 07:26:17 +000094 if (DoubleToBits(cast<ConstantFPSDNode>(NotZero)->getValue()) ==
95 (0ULL - 1))
Evan Chenga8df1662006-03-27 06:58:47 +000096 return false;
97 } else
Chris Lattner61d43992006-03-25 22:57:01 +000098 return false;
99
100 // Okay, we have at least one ~0 value, check to see if the rest match or are
101 // undefs.
Chris Lattner61d43992006-03-25 22:57:01 +0000102 for (++i; i != e; ++i)
103 if (N->getOperand(i) != NotZero &&
104 N->getOperand(i).getOpcode() != ISD::UNDEF)
105 return false;
106 return true;
107}
108
109
Evan Cheng4a147842006-03-26 09:50:58 +0000110/// isBuildVectorAllZeros - Return true if the specified node is a
111/// BUILD_VECTOR where all of the elements are 0 or undef.
112bool ISD::isBuildVectorAllZeros(const SDNode *N) {
113 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
Evan Chenga8df1662006-03-27 06:58:47 +0000114
115 unsigned i = 0, e = N->getNumOperands();
116
117 // Skip over all of the undef values.
118 while (i != e && N->getOperand(i).getOpcode() == ISD::UNDEF)
119 ++i;
120
121 // Do not accept an all-undef vector.
122 if (i == e) return false;
123
124 // Do not accept build_vectors that aren't all constants or which have non-~0
125 // elements.
126 SDOperand Zero = N->getOperand(i);
127 if (isa<ConstantSDNode>(Zero)) {
128 if (!cast<ConstantSDNode>(Zero)->isNullValue())
129 return false;
130 } else if (isa<ConstantFPSDNode>(Zero)) {
131 if (!cast<ConstantFPSDNode>(Zero)->isExactlyValue(0.0))
132 return false;
133 } else
134 return false;
135
136 // Okay, we have at least one ~0 value, check to see if the rest match or are
137 // undefs.
138 for (++i; i != e; ++i)
139 if (N->getOperand(i) != Zero &&
140 N->getOperand(i).getOpcode() != ISD::UNDEF)
141 return false;
142 return true;
Evan Cheng4a147842006-03-26 09:50:58 +0000143}
144
Chris Lattnerc3aae252005-01-07 07:46:32 +0000145/// getSetCCSwappedOperands - Return the operation corresponding to (Y op X)
146/// when given the operation for (X op Y).
147ISD::CondCode ISD::getSetCCSwappedOperands(ISD::CondCode Operation) {
148 // To perform this operation, we just need to swap the L and G bits of the
149 // operation.
150 unsigned OldL = (Operation >> 2) & 1;
151 unsigned OldG = (Operation >> 1) & 1;
152 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
153 (OldL << 1) | // New G bit
154 (OldG << 2)); // New L bit.
155}
156
157/// getSetCCInverse - Return the operation corresponding to !(X op Y), where
158/// 'op' is a valid SetCC operation.
159ISD::CondCode ISD::getSetCCInverse(ISD::CondCode Op, bool isInteger) {
160 unsigned Operation = Op;
161 if (isInteger)
162 Operation ^= 7; // Flip L, G, E bits, but not U.
163 else
164 Operation ^= 15; // Flip all of the condition bits.
165 if (Operation > ISD::SETTRUE2)
166 Operation &= ~8; // Don't let N and U bits get set.
167 return ISD::CondCode(Operation);
168}
169
170
171/// isSignedOp - For an integer comparison, return 1 if the comparison is a
172/// signed operation and 2 if the result is an unsigned comparison. Return zero
173/// if the operation does not depend on the sign of the input (setne and seteq).
174static int isSignedOp(ISD::CondCode Opcode) {
175 switch (Opcode) {
176 default: assert(0 && "Illegal integer setcc operation!");
177 case ISD::SETEQ:
178 case ISD::SETNE: return 0;
179 case ISD::SETLT:
180 case ISD::SETLE:
181 case ISD::SETGT:
182 case ISD::SETGE: return 1;
183 case ISD::SETULT:
184 case ISD::SETULE:
185 case ISD::SETUGT:
186 case ISD::SETUGE: return 2;
187 }
188}
189
190/// getSetCCOrOperation - Return the result of a logical OR between different
191/// comparisons of identical values: ((X op1 Y) | (X op2 Y)). This function
192/// returns SETCC_INVALID if it is not possible to represent the resultant
193/// comparison.
194ISD::CondCode ISD::getSetCCOrOperation(ISD::CondCode Op1, ISD::CondCode Op2,
195 bool isInteger) {
196 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
197 // Cannot fold a signed integer setcc with an unsigned integer setcc.
198 return ISD::SETCC_INVALID;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000199
Chris Lattnerc3aae252005-01-07 07:46:32 +0000200 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000201
Chris Lattnerc3aae252005-01-07 07:46:32 +0000202 // If the N and U bits get set then the resultant comparison DOES suddenly
203 // care about orderedness, and is true when ordered.
204 if (Op > ISD::SETTRUE2)
205 Op &= ~16; // Clear the N bit.
206 return ISD::CondCode(Op);
207}
208
209/// getSetCCAndOperation - Return the result of a logical AND between different
210/// comparisons of identical values: ((X op1 Y) & (X op2 Y)). This
211/// function returns zero if it is not possible to represent the resultant
212/// comparison.
213ISD::CondCode ISD::getSetCCAndOperation(ISD::CondCode Op1, ISD::CondCode Op2,
214 bool isInteger) {
215 if (isInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
216 // Cannot fold a signed setcc with an unsigned setcc.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000217 return ISD::SETCC_INVALID;
Chris Lattnerc3aae252005-01-07 07:46:32 +0000218
219 // Combine all of the condition bits.
220 return ISD::CondCode(Op1 & Op2);
221}
222
Chris Lattnerb48da392005-01-23 04:39:44 +0000223const TargetMachine &SelectionDAG::getTarget() const {
224 return TLI.getTargetMachine();
225}
226
Jim Laskey58b968b2005-08-17 20:08:02 +0000227//===----------------------------------------------------------------------===//
228// SelectionDAG Class
229//===----------------------------------------------------------------------===//
Chris Lattnerb48da392005-01-23 04:39:44 +0000230
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000231/// RemoveDeadNodes - This method deletes all unreachable nodes in the
232/// SelectionDAG, including nodes (like loads) that have uses of their token
233/// chain but no other uses and no side effect. If a node is passed in as an
234/// argument, it is used as the seed for node deletion.
235void SelectionDAG::RemoveDeadNodes(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000236 // Create a dummy node (which is not added to allnodes), that adds a reference
237 // to the root node, preventing it from being deleted.
Chris Lattner95038592005-10-05 06:35:28 +0000238 HandleSDNode Dummy(getRoot());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000239
Chris Lattnerf469cb62005-11-08 18:52:27 +0000240 bool MadeChange = false;
241
Chris Lattner8b8749f2005-08-17 19:00:20 +0000242 // If we have a hint to start from, use it.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000243 if (N && N->use_empty()) {
244 DestroyDeadNode(N);
245 MadeChange = true;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000246 }
247
Chris Lattnerde202b32005-11-09 23:47:37 +0000248 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ++I)
249 if (I->use_empty() && I->getOpcode() != 65535) {
250 // Node is dead, recursively delete newly dead uses.
251 DestroyDeadNode(I);
Chris Lattnerf469cb62005-11-08 18:52:27 +0000252 MadeChange = true;
253 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000254
255 // Walk the nodes list, removing the nodes we've marked as dead.
256 if (MadeChange) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000257 for (allnodes_iterator I = allnodes_begin(), E = allnodes_end(); I != E; ) {
258 SDNode *N = I++;
259 if (N->use_empty())
260 AllNodes.erase(N);
261 }
Chris Lattnerf469cb62005-11-08 18:52:27 +0000262 }
263
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000264 // If the root changed (e.g. it was a dead load, update the root).
Chris Lattner95038592005-10-05 06:35:28 +0000265 setRoot(Dummy.getValue());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000266}
267
Chris Lattnerf469cb62005-11-08 18:52:27 +0000268/// DestroyDeadNode - We know that N is dead. Nuke it from the CSE maps for the
269/// graph. If it is the last user of any of its operands, recursively process
270/// them the same way.
271///
272void SelectionDAG::DestroyDeadNode(SDNode *N) {
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000273 // Okay, we really are going to delete this node. First take this out of the
274 // appropriate CSE map.
Chris Lattner149c58c2005-08-16 18:17:10 +0000275 RemoveNodeFromCSEMaps(N);
276
277 // Next, brutally remove the operand list. This is safe to do, as there are
278 // no cycles in the graph.
Chris Lattner65113b22005-11-08 22:07:03 +0000279 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I) {
280 SDNode *O = I->Val;
Chris Lattner149c58c2005-08-16 18:17:10 +0000281 O->removeUser(N);
282
283 // Now that we removed this operand, see if there are no uses of it left.
Chris Lattnerf469cb62005-11-08 18:52:27 +0000284 if (O->use_empty())
285 DestroyDeadNode(O);
Chris Lattner149c58c2005-08-16 18:17:10 +0000286 }
Chris Lattner65113b22005-11-08 22:07:03 +0000287 delete[] N->OperandList;
288 N->OperandList = 0;
289 N->NumOperands = 0;
Chris Lattnerf469cb62005-11-08 18:52:27 +0000290
291 // Mark the node as dead.
292 N->MorphNodeTo(65535);
Chris Lattner149c58c2005-08-16 18:17:10 +0000293}
294
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000295void SelectionDAG::DeleteNode(SDNode *N) {
296 assert(N->use_empty() && "Cannot delete a node that is not dead!");
297
298 // First take this out of the appropriate CSE map.
299 RemoveNodeFromCSEMaps(N);
300
Chris Lattner1e111c72005-09-07 05:37:01 +0000301 // Finally, remove uses due to operands of this node, remove from the
302 // AllNodes list, and delete the node.
303 DeleteNodeNotInCSEMaps(N);
304}
305
306void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
307
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000308 // Remove it from the AllNodes list.
Chris Lattnerde202b32005-11-09 23:47:37 +0000309 AllNodes.remove(N);
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000310
311 // Drop all of the operands and decrement used nodes use counts.
Chris Lattner65113b22005-11-08 22:07:03 +0000312 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ++I)
313 I->Val->removeUser(N);
314 delete[] N->OperandList;
315 N->OperandList = 0;
316 N->NumOperands = 0;
Chris Lattnerc26aefa2005-08-29 21:59:31 +0000317
318 delete N;
319}
320
Chris Lattner149c58c2005-08-16 18:17:10 +0000321/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
322/// correspond to it. This is useful when we're about to delete or repurpose
323/// the node. We don't want future request for structurally identical nodes
324/// to return N anymore.
325void SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000326 bool Erased = false;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000327 switch (N->getOpcode()) {
Chris Lattner95038592005-10-05 06:35:28 +0000328 case ISD::HANDLENODE: return; // noop.
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000329 case ISD::Constant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000330 Erased = Constants.erase(std::make_pair(cast<ConstantSDNode>(N)->getValue(),
331 N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000332 break;
Chris Lattner37bfbb42005-08-17 00:34:06 +0000333 case ISD::TargetConstant:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000334 Erased = TargetConstants.erase(std::make_pair(
335 cast<ConstantSDNode>(N)->getValue(),
336 N->getValueType(0)));
Chris Lattner37bfbb42005-08-17 00:34:06 +0000337 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000338 case ISD::ConstantFP: {
Jim Laskeycb6682f2005-08-17 19:34:49 +0000339 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000340 Erased = ConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000341 break;
Chris Lattnerd8658612005-02-17 20:17:32 +0000342 }
Chris Lattner3181a772006-01-29 06:26:56 +0000343 case ISD::TargetConstantFP: {
344 uint64_t V = DoubleToBits(cast<ConstantFPSDNode>(N)->getValue());
345 Erased = TargetConstantFPs.erase(std::make_pair(V, N->getValueType(0)));
346 break;
347 }
Chris Lattner36ce6912005-11-29 06:21:05 +0000348 case ISD::STRING:
349 Erased = StringNodes.erase(cast<StringSDNode>(N)->getValue());
350 break;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000351 case ISD::CONDCODE:
352 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
353 "Cond code doesn't exist!");
Chris Lattner6621e3b2005-09-02 19:15:44 +0000354 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != 0;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000355 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = 0;
356 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000357 case ISD::GlobalAddress: {
358 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
359 Erased = GlobalValues.erase(std::make_pair(GN->getGlobal(),
360 GN->getOffset()));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000361 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000362 }
363 case ISD::TargetGlobalAddress: {
364 GlobalAddressSDNode *GN = cast<GlobalAddressSDNode>(N);
365 Erased =TargetGlobalValues.erase(std::make_pair(GN->getGlobal(),
366 GN->getOffset()));
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000367 break;
Evan Cheng14229bb2005-11-30 02:49:21 +0000368 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000369 case ISD::FrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000370 Erased = FrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000371 break;
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000372 case ISD::TargetFrameIndex:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000373 Erased = TargetFrameIndices.erase(cast<FrameIndexSDNode>(N)->getIndex());
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000374 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000375 case ISD::ConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000376 Erased = ConstantPoolIndices.
377 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000378 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
379 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000380 break;
Chris Lattner4025a9c2005-08-25 05:03:06 +0000381 case ISD::TargetConstantPool:
Evan Chengb8973bd2006-01-31 22:23:14 +0000382 Erased = TargetConstantPoolIndices.
383 erase(std::make_pair(cast<ConstantPoolSDNode>(N)->get(),
Evan Cheng404cb4f2006-02-25 09:54:52 +0000384 std::make_pair(cast<ConstantPoolSDNode>(N)->getOffset(),
385 cast<ConstantPoolSDNode>(N)->getAlignment())));
Chris Lattner4025a9c2005-08-25 05:03:06 +0000386 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000387 case ISD::BasicBlock:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000388 Erased = BBNodes.erase(cast<BasicBlockSDNode>(N)->getBasicBlock());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000389 break;
390 case ISD::ExternalSymbol:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000391 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000392 break;
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000393 case ISD::TargetExternalSymbol:
Chris Lattner809ec112006-01-28 10:09:25 +0000394 Erased =
395 TargetExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000396 break;
Chris Lattner15e4b012005-07-10 00:07:11 +0000397 case ISD::VALUETYPE:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000398 Erased = ValueTypeNodes[cast<VTSDNode>(N)->getVT()] != 0;
Chris Lattner15e4b012005-07-10 00:07:11 +0000399 ValueTypeNodes[cast<VTSDNode>(N)->getVT()] = 0;
400 break;
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000401 case ISD::Register:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000402 Erased = RegNodes.erase(std::make_pair(cast<RegisterSDNode>(N)->getReg(),
403 N->getValueType(0)));
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000404 break;
Chris Lattnerc5343952005-08-05 16:55:31 +0000405 case ISD::SRCVALUE: {
406 SrcValueSDNode *SVN = cast<SrcValueSDNode>(N);
Chris Lattner6621e3b2005-09-02 19:15:44 +0000407 Erased =ValueNodes.erase(std::make_pair(SVN->getValue(), SVN->getOffset()));
Chris Lattnerc5343952005-08-05 16:55:31 +0000408 break;
409 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000410 case ISD::LOAD:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000411 Erased = Loads.erase(std::make_pair(N->getOperand(1),
412 std::make_pair(N->getOperand(0),
413 N->getValueType(0))));
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000414 break;
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000415 default:
Chris Lattner6621e3b2005-09-02 19:15:44 +0000416 if (N->getNumValues() == 1) {
Chris Lattner70b9b102005-09-02 19:36:17 +0000417 if (N->getNumOperands() == 0) {
418 Erased = NullaryOps.erase(std::make_pair(N->getOpcode(),
419 N->getValueType(0)));
420 } else if (N->getNumOperands() == 1) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000421 Erased =
422 UnaryOps.erase(std::make_pair(N->getOpcode(),
423 std::make_pair(N->getOperand(0),
424 N->getValueType(0))));
425 } else if (N->getNumOperands() == 2) {
426 Erased =
427 BinaryOps.erase(std::make_pair(N->getOpcode(),
428 std::make_pair(N->getOperand(0),
429 N->getOperand(1))));
430 } else {
431 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
432 Erased =
433 OneResultNodes.erase(std::make_pair(N->getOpcode(),
434 std::make_pair(N->getValueType(0),
435 Ops)));
436 }
Chris Lattner385328c2005-05-14 07:42:29 +0000437 } else {
Chris Lattner89c34632005-05-14 06:20:26 +0000438 // Remove the node from the ArbitraryNodes map.
439 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
440 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Chris Lattner6621e3b2005-09-02 19:15:44 +0000441 Erased =
442 ArbitraryNodes.erase(std::make_pair(N->getOpcode(),
443 std::make_pair(RV, Ops)));
Chris Lattner89c34632005-05-14 06:20:26 +0000444 }
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000445 break;
446 }
Chris Lattner6621e3b2005-09-02 19:15:44 +0000447#ifndef NDEBUG
448 // Verify that the node was actually in one of the CSE maps, unless it has a
449 // flag result (which cannot be CSE'd) or is one of the special cases that are
450 // not subject to CSE.
451 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Flag &&
Chris Lattner70814bc2006-01-29 07:58:15 +0000452 !N->isTargetOpcode()) {
Chris Lattner6621e3b2005-09-02 19:15:44 +0000453 N->dump();
454 assert(0 && "Node is not in map!");
455 }
456#endif
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000457}
458
Chris Lattner8b8749f2005-08-17 19:00:20 +0000459/// AddNonLeafNodeToCSEMaps - Add the specified node back to the CSE maps. It
460/// has been taken out and modified in some way. If the specified node already
461/// exists in the CSE maps, do not modify the maps, but return the existing node
462/// instead. If it doesn't exist, add it and return null.
463///
464SDNode *SelectionDAG::AddNonLeafNodeToCSEMaps(SDNode *N) {
465 assert(N->getNumOperands() && "This is a leaf node!");
Chris Lattner70814bc2006-01-29 07:58:15 +0000466 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerfe14b342005-12-01 23:14:50 +0000467 return 0; // Never add these nodes.
Chris Lattnerc85a9f32005-11-30 18:20:52 +0000468
Chris Lattner9f8cc692005-12-19 22:21:21 +0000469 // Check that remaining values produced are not flags.
470 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
471 if (N->getValueType(i) == MVT::Flag)
472 return 0; // Never CSE anything that produces a flag.
473
Chris Lattnerfe14b342005-12-01 23:14:50 +0000474 if (N->getNumValues() == 1) {
475 if (N->getNumOperands() == 1) {
476 SDNode *&U = UnaryOps[std::make_pair(N->getOpcode(),
477 std::make_pair(N->getOperand(0),
478 N->getValueType(0)))];
479 if (U) return U;
480 U = N;
481 } else if (N->getNumOperands() == 2) {
482 SDNode *&B = BinaryOps[std::make_pair(N->getOpcode(),
483 std::make_pair(N->getOperand(0),
484 N->getOperand(1)))];
485 if (B) return B;
486 B = N;
487 } else {
488 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
489 SDNode *&ORN = OneResultNodes[std::make_pair(N->getOpcode(),
Chris Lattner809ec112006-01-28 10:09:25 +0000490 std::make_pair(N->getValueType(0), Ops))];
Chris Lattnerfe14b342005-12-01 23:14:50 +0000491 if (ORN) return ORN;
492 ORN = N;
493 }
494 } else {
495 if (N->getOpcode() == ISD::LOAD) {
496 SDNode *&L = Loads[std::make_pair(N->getOperand(1),
497 std::make_pair(N->getOperand(0),
498 N->getValueType(0)))];
499 if (L) return L;
500 L = N;
501 } else {
502 // Remove the node from the ArbitraryNodes map.
503 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
504 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
505 SDNode *&AN = ArbitraryNodes[std::make_pair(N->getOpcode(),
506 std::make_pair(RV, Ops))];
507 if (AN) return AN;
508 AN = N;
509 }
Chris Lattner8b8749f2005-08-17 19:00:20 +0000510 }
511 return 0;
Chris Lattner8b8749f2005-08-17 19:00:20 +0000512}
513
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000514/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
515/// were replaced with those specified. If this node is never memoized,
516/// return null, otherwise return a pointer to the slot it would take. If a
517/// node already exists with these operands, the slot will be non-null.
518SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDOperand Op) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000519 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000520 return 0; // Never add these nodes.
521
522 // Check that remaining values produced are not flags.
523 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
524 if (N->getValueType(i) == MVT::Flag)
525 return 0; // Never CSE anything that produces a flag.
526
527 if (N->getNumValues() == 1) {
528 return &UnaryOps[std::make_pair(N->getOpcode(),
529 std::make_pair(Op, N->getValueType(0)))];
530 } else {
531 // Remove the node from the ArbitraryNodes map.
532 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
533 std::vector<SDOperand> Ops;
534 Ops.push_back(Op);
535 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
536 std::make_pair(RV, Ops))];
537 }
538 return 0;
539}
540
541/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
542/// were replaced with those specified. If this node is never memoized,
543/// return null, otherwise return a pointer to the slot it would take. If a
544/// node already exists with these operands, the slot will be non-null.
545SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
546 SDOperand Op1, SDOperand Op2) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000547 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000548 return 0; // Never add these nodes.
549
550 // Check that remaining values produced are not flags.
551 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
552 if (N->getValueType(i) == MVT::Flag)
553 return 0; // Never CSE anything that produces a flag.
554
555 if (N->getNumValues() == 1) {
556 return &BinaryOps[std::make_pair(N->getOpcode(),
557 std::make_pair(Op1, Op2))];
558 } else {
559 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
560 std::vector<SDOperand> Ops;
561 Ops.push_back(Op1);
562 Ops.push_back(Op2);
563 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
564 std::make_pair(RV, Ops))];
565 }
566 return 0;
567}
568
569
570/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
571/// were replaced with those specified. If this node is never memoized,
572/// return null, otherwise return a pointer to the slot it would take. If a
573/// node already exists with these operands, the slot will be non-null.
574SDNode **SelectionDAG::FindModifiedNodeSlot(SDNode *N,
575 const std::vector<SDOperand> &Ops) {
Chris Lattner70814bc2006-01-29 07:58:15 +0000576 if (N->getOpcode() == ISD::HANDLENODE || N->getValueType(0) == MVT::Flag)
Chris Lattnerdf6eb302006-01-28 09:32:45 +0000577 return 0; // Never add these nodes.
578
579 // Check that remaining values produced are not flags.
580 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
581 if (N->getValueType(i) == MVT::Flag)
582 return 0; // Never CSE anything that produces a flag.
583
584 if (N->getNumValues() == 1) {
585 if (N->getNumOperands() == 1) {
586 return &UnaryOps[std::make_pair(N->getOpcode(),
587 std::make_pair(Ops[0],
588 N->getValueType(0)))];
589 } else if (N->getNumOperands() == 2) {
590 return &BinaryOps[std::make_pair(N->getOpcode(),
591 std::make_pair(Ops[0], Ops[1]))];
592 } else {
593 return &OneResultNodes[std::make_pair(N->getOpcode(),
594 std::make_pair(N->getValueType(0),
595 Ops))];
596 }
597 } else {
598 if (N->getOpcode() == ISD::LOAD) {
599 return &Loads[std::make_pair(Ops[1],
600 std::make_pair(Ops[0], N->getValueType(0)))];
601 } else {
602 std::vector<MVT::ValueType> RV(N->value_begin(), N->value_end());
603 return &ArbitraryNodes[std::make_pair(N->getOpcode(),
604 std::make_pair(RV, Ops))];
605 }
606 }
607 return 0;
608}
Chris Lattner8b8749f2005-08-17 19:00:20 +0000609
Chris Lattner0e12e6e2005-01-07 21:09:16 +0000610
Chris Lattner78ec3112003-08-11 14:57:33 +0000611SelectionDAG::~SelectionDAG() {
Chris Lattnerde202b32005-11-09 23:47:37 +0000612 while (!AllNodes.empty()) {
613 SDNode *N = AllNodes.begin();
Chris Lattner65113b22005-11-08 22:07:03 +0000614 delete [] N->OperandList;
615 N->OperandList = 0;
616 N->NumOperands = 0;
Chris Lattnerde202b32005-11-09 23:47:37 +0000617 AllNodes.pop_front();
Chris Lattner65113b22005-11-08 22:07:03 +0000618 }
Chris Lattner78ec3112003-08-11 14:57:33 +0000619}
620
Chris Lattner0f2287b2005-04-13 02:38:18 +0000621SDOperand SelectionDAG::getZeroExtendInReg(SDOperand Op, MVT::ValueType VT) {
Chris Lattner51679c42005-04-13 19:41:05 +0000622 if (Op.getValueType() == VT) return Op;
Jeff Cohen19bb2282005-05-10 02:22:38 +0000623 int64_t Imm = ~0ULL >> (64-MVT::getSizeInBits(VT));
Chris Lattner0f2287b2005-04-13 02:38:18 +0000624 return getNode(ISD::AND, Op.getValueType(), Op,
625 getConstant(Imm, Op.getValueType()));
626}
627
Chris Lattnerc3aae252005-01-07 07:46:32 +0000628SDOperand SelectionDAG::getConstant(uint64_t Val, MVT::ValueType VT) {
629 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
630 // Mask out any bits that are not valid for this constant.
Chris Lattner623f70d2005-01-08 06:24:30 +0000631 if (VT != MVT::i64)
632 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000633
Chris Lattnerc3aae252005-01-07 07:46:32 +0000634 SDNode *&N = Constants[std::make_pair(Val, VT)];
635 if (N) return SDOperand(N, 0);
Chris Lattner37bfbb42005-08-17 00:34:06 +0000636 N = new ConstantSDNode(false, Val, VT);
637 AllNodes.push_back(N);
638 return SDOperand(N, 0);
639}
640
Chris Lattner36ce6912005-11-29 06:21:05 +0000641SDOperand SelectionDAG::getString(const std::string &Val) {
642 StringSDNode *&N = StringNodes[Val];
643 if (!N) {
644 N = new StringSDNode(Val);
645 AllNodes.push_back(N);
646 }
647 return SDOperand(N, 0);
648}
649
Chris Lattner37bfbb42005-08-17 00:34:06 +0000650SDOperand SelectionDAG::getTargetConstant(uint64_t Val, MVT::ValueType VT) {
651 assert(MVT::isInteger(VT) && "Cannot create FP integer constant!");
652 // Mask out any bits that are not valid for this constant.
653 if (VT != MVT::i64)
654 Val &= ((uint64_t)1 << MVT::getSizeInBits(VT)) - 1;
655
656 SDNode *&N = TargetConstants[std::make_pair(Val, VT)];
657 if (N) return SDOperand(N, 0);
658 N = new ConstantSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000659 AllNodes.push_back(N);
660 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +0000661}
662
Chris Lattnerc3aae252005-01-07 07:46:32 +0000663SDOperand SelectionDAG::getConstantFP(double Val, MVT::ValueType VT) {
664 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
665 if (VT == MVT::f32)
666 Val = (float)Val; // Mask out extra precision.
667
Chris Lattnerd8658612005-02-17 20:17:32 +0000668 // Do the map lookup using the actual bit pattern for the floating point
669 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
670 // we don't have issues with SNANs.
Jim Laskeycb6682f2005-08-17 19:34:49 +0000671 SDNode *&N = ConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000672 if (N) return SDOperand(N, 0);
Chris Lattner3181a772006-01-29 06:26:56 +0000673 N = new ConstantFPSDNode(false, Val, VT);
674 AllNodes.push_back(N);
675 return SDOperand(N, 0);
676}
677
678SDOperand SelectionDAG::getTargetConstantFP(double Val, MVT::ValueType VT) {
679 assert(MVT::isFloatingPoint(VT) && "Cannot create integer FP constant!");
680 if (VT == MVT::f32)
681 Val = (float)Val; // Mask out extra precision.
682
683 // Do the map lookup using the actual bit pattern for the floating point
684 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
685 // we don't have issues with SNANs.
686 SDNode *&N = TargetConstantFPs[std::make_pair(DoubleToBits(Val), VT)];
687 if (N) return SDOperand(N, 0);
688 N = new ConstantFPSDNode(true, Val, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000689 AllNodes.push_back(N);
690 return SDOperand(N, 0);
691}
692
Chris Lattnerc3aae252005-01-07 07:46:32 +0000693SDOperand SelectionDAG::getGlobalAddress(const GlobalValue *GV,
Evan Cheng14229bb2005-11-30 02:49:21 +0000694 MVT::ValueType VT, int offset) {
695 SDNode *&N = GlobalValues[std::make_pair(GV, offset)];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000696 if (N) return SDOperand(N, 0);
Nate Begeman512beb92005-12-30 00:10:38 +0000697 N = new GlobalAddressSDNode(false, GV, VT, offset);
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000698 AllNodes.push_back(N);
699 return SDOperand(N, 0);
700}
701
702SDOperand SelectionDAG::getTargetGlobalAddress(const GlobalValue *GV,
Evan Cheng61ca74b2005-11-30 02:04:11 +0000703 MVT::ValueType VT, int offset) {
Evan Cheng14229bb2005-11-30 02:49:21 +0000704 SDNode *&N = TargetGlobalValues[std::make_pair(GV, offset)];
Chris Lattneraaaa0b62005-08-19 22:31:04 +0000705 if (N) return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +0000706 N = new GlobalAddressSDNode(true, GV, VT, offset);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000707 AllNodes.push_back(N);
708 return SDOperand(N, 0);
709}
710
711SDOperand SelectionDAG::getFrameIndex(int FI, MVT::ValueType VT) {
712 SDNode *&N = FrameIndices[FI];
713 if (N) return SDOperand(N, 0);
Chris Lattnerafb2dd42005-08-25 00:43:01 +0000714 N = new FrameIndexSDNode(FI, VT, false);
715 AllNodes.push_back(N);
716 return SDOperand(N, 0);
717}
718
719SDOperand SelectionDAG::getTargetFrameIndex(int FI, MVT::ValueType VT) {
720 SDNode *&N = TargetFrameIndices[FI];
721 if (N) return SDOperand(N, 0);
722 N = new FrameIndexSDNode(FI, VT, true);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000723 AllNodes.push_back(N);
724 return SDOperand(N, 0);
725}
726
Evan Chengb8973bd2006-01-31 22:23:14 +0000727SDOperand SelectionDAG::getConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000728 unsigned Alignment, int Offset) {
729 SDNode *&N = ConstantPoolIndices[std::make_pair(C,
730 std::make_pair(Offset, Alignment))];
Chris Lattnerc3aae252005-01-07 07:46:32 +0000731 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000732 N = new ConstantPoolSDNode(false, C, VT, Offset, Alignment);
Chris Lattner4025a9c2005-08-25 05:03:06 +0000733 AllNodes.push_back(N);
734 return SDOperand(N, 0);
735}
736
Evan Chengb8973bd2006-01-31 22:23:14 +0000737SDOperand SelectionDAG::getTargetConstantPool(Constant *C, MVT::ValueType VT,
Evan Cheng404cb4f2006-02-25 09:54:52 +0000738 unsigned Alignment, int Offset) {
739 SDNode *&N = TargetConstantPoolIndices[std::make_pair(C,
740 std::make_pair(Offset, Alignment))];
Chris Lattner4025a9c2005-08-25 05:03:06 +0000741 if (N) return SDOperand(N, 0);
Evan Cheng404cb4f2006-02-25 09:54:52 +0000742 N = new ConstantPoolSDNode(true, C, VT, Offset, Alignment);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000743 AllNodes.push_back(N);
744 return SDOperand(N, 0);
745}
746
747SDOperand SelectionDAG::getBasicBlock(MachineBasicBlock *MBB) {
748 SDNode *&N = BBNodes[MBB];
749 if (N) return SDOperand(N, 0);
750 N = new BasicBlockSDNode(MBB);
751 AllNodes.push_back(N);
752 return SDOperand(N, 0);
753}
754
Chris Lattner15e4b012005-07-10 00:07:11 +0000755SDOperand SelectionDAG::getValueType(MVT::ValueType VT) {
756 if ((unsigned)VT >= ValueTypeNodes.size())
757 ValueTypeNodes.resize(VT+1);
758 if (ValueTypeNodes[VT] == 0) {
759 ValueTypeNodes[VT] = new VTSDNode(VT);
760 AllNodes.push_back(ValueTypeNodes[VT]);
761 }
762
763 return SDOperand(ValueTypeNodes[VT], 0);
764}
765
Chris Lattnerc3aae252005-01-07 07:46:32 +0000766SDOperand SelectionDAG::getExternalSymbol(const char *Sym, MVT::ValueType VT) {
767 SDNode *&N = ExternalSymbols[Sym];
768 if (N) return SDOperand(N, 0);
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000769 N = new ExternalSymbolSDNode(false, Sym, VT);
770 AllNodes.push_back(N);
771 return SDOperand(N, 0);
772}
773
Chris Lattner809ec112006-01-28 10:09:25 +0000774SDOperand SelectionDAG::getTargetExternalSymbol(const char *Sym,
775 MVT::ValueType VT) {
Andrew Lenharth2a2de662005-10-23 03:40:17 +0000776 SDNode *&N = TargetExternalSymbols[Sym];
777 if (N) return SDOperand(N, 0);
778 N = new ExternalSymbolSDNode(true, Sym, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000779 AllNodes.push_back(N);
780 return SDOperand(N, 0);
781}
782
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000783SDOperand SelectionDAG::getCondCode(ISD::CondCode Cond) {
784 if ((unsigned)Cond >= CondCodeNodes.size())
785 CondCodeNodes.resize(Cond+1);
786
Chris Lattner079a27a2005-08-09 20:40:02 +0000787 if (CondCodeNodes[Cond] == 0) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000788 CondCodeNodes[Cond] = new CondCodeSDNode(Cond);
Chris Lattner079a27a2005-08-09 20:40:02 +0000789 AllNodes.push_back(CondCodeNodes[Cond]);
790 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000791 return SDOperand(CondCodeNodes[Cond], 0);
792}
793
Chris Lattner0fdd7682005-08-30 22:38:38 +0000794SDOperand SelectionDAG::getRegister(unsigned RegNo, MVT::ValueType VT) {
795 RegisterSDNode *&Reg = RegNodes[std::make_pair(RegNo, VT)];
796 if (!Reg) {
797 Reg = new RegisterSDNode(RegNo, VT);
798 AllNodes.push_back(Reg);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000799 }
Chris Lattner0fdd7682005-08-30 22:38:38 +0000800 return SDOperand(Reg, 0);
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000801}
802
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000803SDOperand SelectionDAG::SimplifySetCC(MVT::ValueType VT, SDOperand N1,
804 SDOperand N2, ISD::CondCode Cond) {
Chris Lattnerc3aae252005-01-07 07:46:32 +0000805 // These setcc operations always fold.
806 switch (Cond) {
807 default: break;
808 case ISD::SETFALSE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000809 case ISD::SETFALSE2: return getConstant(0, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000810 case ISD::SETTRUE:
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000811 case ISD::SETTRUE2: return getConstant(1, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000812 }
813
Chris Lattner67255a12005-04-07 18:14:58 +0000814 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val)) {
815 uint64_t C2 = N2C->getValue();
816 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val)) {
817 uint64_t C1 = N1C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000818
Chris Lattnerc3aae252005-01-07 07:46:32 +0000819 // Sign extend the operands if required
820 if (ISD::isSignedIntSetCC(Cond)) {
821 C1 = N1C->getSignExtended();
822 C2 = N2C->getSignExtended();
823 }
824
825 switch (Cond) {
826 default: assert(0 && "Unknown integer setcc!");
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000827 case ISD::SETEQ: return getConstant(C1 == C2, VT);
828 case ISD::SETNE: return getConstant(C1 != C2, VT);
829 case ISD::SETULT: return getConstant(C1 < C2, VT);
830 case ISD::SETUGT: return getConstant(C1 > C2, VT);
831 case ISD::SETULE: return getConstant(C1 <= C2, VT);
832 case ISD::SETUGE: return getConstant(C1 >= C2, VT);
833 case ISD::SETLT: return getConstant((int64_t)C1 < (int64_t)C2, VT);
834 case ISD::SETGT: return getConstant((int64_t)C1 > (int64_t)C2, VT);
835 case ISD::SETLE: return getConstant((int64_t)C1 <= (int64_t)C2, VT);
836 case ISD::SETGE: return getConstant((int64_t)C1 >= (int64_t)C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000837 }
Chris Lattner24673922005-04-07 18:58:54 +0000838 } else {
Chris Lattner7b2880c2005-08-24 22:44:39 +0000839 // If the LHS is a ZERO_EXTEND, perform the comparison on the input.
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000840 if (N1.getOpcode() == ISD::ZERO_EXTEND) {
841 unsigned InSize = MVT::getSizeInBits(N1.getOperand(0).getValueType());
842
843 // If the comparison constant has bits in the upper part, the
844 // zero-extended value could never match.
845 if (C2 & (~0ULL << InSize)) {
846 unsigned VSize = MVT::getSizeInBits(N1.getValueType());
847 switch (Cond) {
848 case ISD::SETUGT:
849 case ISD::SETUGE:
850 case ISD::SETEQ: return getConstant(0, VT);
851 case ISD::SETULT:
852 case ISD::SETULE:
853 case ISD::SETNE: return getConstant(1, VT);
854 case ISD::SETGT:
855 case ISD::SETGE:
856 // True if the sign bit of C2 is set.
857 return getConstant((C2 & (1ULL << VSize)) != 0, VT);
858 case ISD::SETLT:
859 case ISD::SETLE:
860 // True if the sign bit of C2 isn't set.
861 return getConstant((C2 & (1ULL << VSize)) == 0, VT);
862 default:
863 break;
864 }
865 }
866
867 // Otherwise, we can perform the comparison with the low bits.
868 switch (Cond) {
869 case ISD::SETEQ:
870 case ISD::SETNE:
871 case ISD::SETUGT:
872 case ISD::SETUGE:
873 case ISD::SETULT:
874 case ISD::SETULE:
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000875 return getSetCC(VT, N1.getOperand(0),
876 getConstant(C2, N1.getOperand(0).getValueType()),
877 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000878 default:
879 break; // todo, be more careful with signed comparisons
880 }
Chris Lattner7b2880c2005-08-24 22:44:39 +0000881 } else if (N1.getOpcode() == ISD::SIGN_EXTEND_INREG &&
882 (Cond == ISD::SETEQ || Cond == ISD::SETNE)) {
883 MVT::ValueType ExtSrcTy = cast<VTSDNode>(N1.getOperand(1))->getVT();
884 unsigned ExtSrcTyBits = MVT::getSizeInBits(ExtSrcTy);
885 MVT::ValueType ExtDstTy = N1.getValueType();
886 unsigned ExtDstTyBits = MVT::getSizeInBits(ExtDstTy);
Nate Begeman56eb8682005-08-30 02:44:00 +0000887
Chris Lattner7b2880c2005-08-24 22:44:39 +0000888 // If the extended part has any inconsistent bits, it cannot ever
889 // compare equal. In other words, they have to be all ones or all
890 // zeros.
891 uint64_t ExtBits =
Jeff Cohen7383ce42005-08-31 02:47:06 +0000892 (~0ULL >> (64-ExtSrcTyBits)) & (~0ULL << (ExtDstTyBits-1));
Chris Lattner7b2880c2005-08-24 22:44:39 +0000893 if ((C2 & ExtBits) != 0 && (C2 & ExtBits) != ExtBits)
894 return getConstant(Cond == ISD::SETNE, VT);
895
896 // Otherwise, make this a use of a zext.
897 return getSetCC(VT, getZeroExtendInReg(N1.getOperand(0), ExtSrcTy),
Jeff Cohen7383ce42005-08-31 02:47:06 +0000898 getConstant(C2 & (~0ULL>>(64-ExtSrcTyBits)), ExtDstTy),
Chris Lattner7b2880c2005-08-24 22:44:39 +0000899 Cond);
Chris Lattner4a44c8d2005-04-18 04:30:45 +0000900 }
901
Chris Lattner67255a12005-04-07 18:14:58 +0000902 uint64_t MinVal, MaxVal;
903 unsigned OperandBitSize = MVT::getSizeInBits(N2C->getValueType(0));
904 if (ISD::isSignedIntSetCC(Cond)) {
905 MinVal = 1ULL << (OperandBitSize-1);
906 if (OperandBitSize != 1) // Avoid X >> 64, which is undefined.
907 MaxVal = ~0ULL >> (65-OperandBitSize);
908 else
909 MaxVal = 0;
910 } else {
911 MinVal = 0;
912 MaxVal = ~0ULL >> (64-OperandBitSize);
913 }
914
915 // Canonicalize GE/LE comparisons to use GT/LT comparisons.
916 if (Cond == ISD::SETGE || Cond == ISD::SETUGE) {
917 if (C2 == MinVal) return getConstant(1, VT); // X >= MIN --> true
918 --C2; // X >= C1 --> X > (C1-1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000919 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
920 (Cond == ISD::SETGE) ? ISD::SETGT : ISD::SETUGT);
Chris Lattner67255a12005-04-07 18:14:58 +0000921 }
922
923 if (Cond == ISD::SETLE || Cond == ISD::SETULE) {
924 if (C2 == MaxVal) return getConstant(1, VT); // X <= MAX --> true
925 ++C2; // X <= C1 --> X < (C1+1)
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000926 return getSetCC(VT, N1, getConstant(C2, N2.getValueType()),
927 (Cond == ISD::SETLE) ? ISD::SETLT : ISD::SETULT);
Chris Lattner67255a12005-04-07 18:14:58 +0000928 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000929
Nate Begeman72ea2812005-04-14 08:56:52 +0000930 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal)
931 return getConstant(0, VT); // X < MIN --> false
Misha Brukmanedf128a2005-04-21 22:36:52 +0000932
Nate Begeman72ea2812005-04-14 08:56:52 +0000933 // Canonicalize setgt X, Min --> setne X, Min
934 if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MinVal)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000935 return getSetCC(VT, N1, N2, ISD::SETNE);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000936
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000937 // If we have setult X, 1, turn it into seteq X, 0
938 if ((Cond == ISD::SETLT || Cond == ISD::SETULT) && C2 == MinVal+1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000939 return getSetCC(VT, N1, getConstant(MinVal, N1.getValueType()),
940 ISD::SETEQ);
Nate Begeman72ea2812005-04-14 08:56:52 +0000941 // If we have setugt X, Max-1, turn it into seteq X, Max
Chris Lattner3b2c1d92005-04-12 00:28:49 +0000942 else if ((Cond == ISD::SETGT || Cond == ISD::SETUGT) && C2 == MaxVal-1)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000943 return getSetCC(VT, N1, getConstant(MaxVal, N1.getValueType()),
944 ISD::SETEQ);
Chris Lattner67255a12005-04-07 18:14:58 +0000945
946 // If we have "setcc X, C1", check to see if we can shrink the immediate
947 // by changing cc.
948
949 // SETUGT X, SINTMAX -> SETLT X, 0
950 if (Cond == ISD::SETUGT && OperandBitSize != 1 &&
951 C2 == (~0ULL >> (65-OperandBitSize)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000952 return getSetCC(VT, N1, getConstant(0, N2.getValueType()), ISD::SETLT);
Chris Lattner67255a12005-04-07 18:14:58 +0000953
954 // FIXME: Implement the rest of these.
955
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000956
957 // Fold bit comparisons when we can.
958 if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
959 VT == N1.getValueType() && N1.getOpcode() == ISD::AND)
960 if (ConstantSDNode *AndRHS =
961 dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
962 if (Cond == ISD::SETNE && C2 == 0) {// (X & 8) != 0 --> (X & 8) >> 3
963 // Perform the xform if the AND RHS is a single bit.
964 if ((AndRHS->getValue() & (AndRHS->getValue()-1)) == 0) {
965 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000966 getConstant(Log2_64(AndRHS->getValue()),
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000967 TLI.getShiftAmountTy()));
968 }
969 } else if (Cond == ISD::SETEQ && C2 == AndRHS->getValue()) {
970 // (X & 8) == 8 --> (X & 8) >> 3
971 // Perform the xform if C2 is a single bit.
972 if ((C2 & (C2-1)) == 0) {
973 return getNode(ISD::SRL, VT, N1,
Chris Lattner0561b3f2005-08-02 19:26:06 +0000974 getConstant(Log2_64(C2),TLI.getShiftAmountTy()));
Chris Lattner1c2a9b92005-04-21 06:12:41 +0000975 }
976 }
977 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000978 }
Chris Lattner67255a12005-04-07 18:14:58 +0000979 } else if (isa<ConstantSDNode>(N1.Val)) {
980 // Ensure that the constant occurs on the RHS.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000981 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattner67255a12005-04-07 18:14:58 +0000982 }
Chris Lattnerc3aae252005-01-07 07:46:32 +0000983
984 if (ConstantFPSDNode *N1C = dyn_cast<ConstantFPSDNode>(N1.Val))
985 if (ConstantFPSDNode *N2C = dyn_cast<ConstantFPSDNode>(N2.Val)) {
986 double C1 = N1C->getValue(), C2 = N2C->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000987
Chris Lattnerc3aae252005-01-07 07:46:32 +0000988 switch (Cond) {
989 default: break; // FIXME: Implement the rest of these!
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000990 case ISD::SETEQ: return getConstant(C1 == C2, VT);
991 case ISD::SETNE: return getConstant(C1 != C2, VT);
992 case ISD::SETLT: return getConstant(C1 < C2, VT);
993 case ISD::SETGT: return getConstant(C1 > C2, VT);
994 case ISD::SETLE: return getConstant(C1 <= C2, VT);
995 case ISD::SETGE: return getConstant(C1 >= C2, VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +0000996 }
997 } else {
998 // Ensure that the constant occurs on the RHS.
Chris Lattnerbd8625b2005-08-09 23:09:05 +0000999 return getSetCC(VT, N2, N1, ISD::getSetCCSwappedOperands(Cond));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001000 }
1001
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001002 // Could not fold it.
1003 return SDOperand();
Chris Lattnerc3aae252005-01-07 07:46:32 +00001004}
1005
Chris Lattnerc3aae252005-01-07 07:46:32 +00001006/// getNode - Gets or creates the specified node.
Chris Lattner78ec3112003-08-11 14:57:33 +00001007///
Chris Lattnerc3aae252005-01-07 07:46:32 +00001008SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT) {
Chris Lattner70b9b102005-09-02 19:36:17 +00001009 SDNode *&N = NullaryOps[std::make_pair(Opcode, VT)];
1010 if (!N) {
1011 N = new SDNode(Opcode, VT);
1012 AllNodes.push_back(N);
1013 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001014 return SDOperand(N, 0);
1015}
1016
Chris Lattnerc3aae252005-01-07 07:46:32 +00001017SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1018 SDOperand Operand) {
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001019 unsigned Tmp1;
Chris Lattner94683772005-12-23 05:30:37 +00001020 // Constant fold unary operations with an integer constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001021 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Operand.Val)) {
1022 uint64_t Val = C->getValue();
1023 switch (Opcode) {
1024 default: break;
1025 case ISD::SIGN_EXTEND: return getConstant(C->getSignExtended(), VT);
Chris Lattner4ed11b42005-09-02 00:17:32 +00001026 case ISD::ANY_EXTEND:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001027 case ISD::ZERO_EXTEND: return getConstant(Val, VT);
1028 case ISD::TRUNCATE: return getConstant(Val, VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001029 case ISD::SINT_TO_FP: return getConstantFP(C->getSignExtended(), VT);
1030 case ISD::UINT_TO_FP: return getConstantFP(C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001031 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001032 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
Chris Lattner94683772005-12-23 05:30:37 +00001033 return getConstantFP(BitsToFloat(Val), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001034 else if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
Chris Lattner94683772005-12-23 05:30:37 +00001035 return getConstantFP(BitsToDouble(Val), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001036 break;
Nate Begeman1b5db7a2006-01-16 08:07:10 +00001037 case ISD::BSWAP:
1038 switch(VT) {
1039 default: assert(0 && "Invalid bswap!"); break;
1040 case MVT::i16: return getConstant(ByteSwap_16((unsigned short)Val), VT);
1041 case MVT::i32: return getConstant(ByteSwap_32((unsigned)Val), VT);
1042 case MVT::i64: return getConstant(ByteSwap_64(Val), VT);
1043 }
1044 break;
1045 case ISD::CTPOP:
1046 switch(VT) {
1047 default: assert(0 && "Invalid ctpop!"); break;
1048 case MVT::i1: return getConstant(Val != 0, VT);
1049 case MVT::i8:
1050 Tmp1 = (unsigned)Val & 0xFF;
1051 return getConstant(CountPopulation_32(Tmp1), VT);
1052 case MVT::i16:
1053 Tmp1 = (unsigned)Val & 0xFFFF;
1054 return getConstant(CountPopulation_32(Tmp1), VT);
1055 case MVT::i32:
1056 return getConstant(CountPopulation_32((unsigned)Val), VT);
1057 case MVT::i64:
1058 return getConstant(CountPopulation_64(Val), VT);
1059 }
1060 case ISD::CTLZ:
1061 switch(VT) {
1062 default: assert(0 && "Invalid ctlz!"); break;
1063 case MVT::i1: return getConstant(Val == 0, VT);
1064 case MVT::i8:
1065 Tmp1 = (unsigned)Val & 0xFF;
1066 return getConstant(CountLeadingZeros_32(Tmp1)-24, VT);
1067 case MVT::i16:
1068 Tmp1 = (unsigned)Val & 0xFFFF;
1069 return getConstant(CountLeadingZeros_32(Tmp1)-16, VT);
1070 case MVT::i32:
1071 return getConstant(CountLeadingZeros_32((unsigned)Val), VT);
1072 case MVT::i64:
1073 return getConstant(CountLeadingZeros_64(Val), VT);
1074 }
1075 case ISD::CTTZ:
1076 switch(VT) {
1077 default: assert(0 && "Invalid cttz!"); break;
1078 case MVT::i1: return getConstant(Val == 0, VT);
1079 case MVT::i8:
1080 Tmp1 = (unsigned)Val | 0x100;
1081 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1082 case MVT::i16:
1083 Tmp1 = (unsigned)Val | 0x10000;
1084 return getConstant(CountTrailingZeros_32(Tmp1), VT);
1085 case MVT::i32:
1086 return getConstant(CountTrailingZeros_32((unsigned)Val), VT);
1087 case MVT::i64:
1088 return getConstant(CountTrailingZeros_64(Val), VT);
1089 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001090 }
1091 }
1092
Chris Lattner94683772005-12-23 05:30:37 +00001093 // Constant fold unary operations with an floating point constant operand.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001094 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Operand.Val))
1095 switch (Opcode) {
Chris Lattner485df9b2005-04-09 03:02:46 +00001096 case ISD::FNEG:
1097 return getConstantFP(-C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001098 case ISD::FABS:
1099 return getConstantFP(fabs(C->getValue()), VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001100 case ISD::FP_ROUND:
1101 case ISD::FP_EXTEND:
1102 return getConstantFP(C->getValue(), VT);
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001103 case ISD::FP_TO_SINT:
1104 return getConstant((int64_t)C->getValue(), VT);
1105 case ISD::FP_TO_UINT:
1106 return getConstant((uint64_t)C->getValue(), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001107 case ISD::BIT_CONVERT:
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001108 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
Chris Lattner94683772005-12-23 05:30:37 +00001109 return getConstant(FloatToBits(C->getValue()), VT);
Chris Lattnere8a30fd2006-03-24 02:20:47 +00001110 else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
Chris Lattner94683772005-12-23 05:30:37 +00001111 return getConstant(DoubleToBits(C->getValue()), VT);
Chris Lattner94683772005-12-23 05:30:37 +00001112 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001113 }
1114
1115 unsigned OpOpcode = Operand.Val->getOpcode();
1116 switch (Opcode) {
Chris Lattnera93ec3e2005-01-21 18:01:22 +00001117 case ISD::TokenFactor:
1118 return Operand; // Factor of one node? No factor.
Chris Lattnerc3aae252005-01-07 07:46:32 +00001119 case ISD::SIGN_EXTEND:
1120 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001121 assert(Operand.getValueType() < VT && "Invalid sext node, dst < src!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001122 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND)
1123 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1124 break;
1125 case ISD::ZERO_EXTEND:
1126 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001127 assert(Operand.getValueType() < VT && "Invalid zext node, dst < src!");
Chris Lattner5a6bace2005-04-07 19:43:53 +00001128 if (OpOpcode == ISD::ZERO_EXTEND) // (zext (zext x)) -> (zext x)
Chris Lattner2f0ca792005-01-12 18:51:15 +00001129 return getNode(ISD::ZERO_EXTEND, VT, Operand.Val->getOperand(0));
Chris Lattnerc3aae252005-01-07 07:46:32 +00001130 break;
Chris Lattner4ed11b42005-09-02 00:17:32 +00001131 case ISD::ANY_EXTEND:
1132 if (Operand.getValueType() == VT) return Operand; // noop extension
Nate Begemanb0d04a72006-02-18 02:40:58 +00001133 assert(Operand.getValueType() < VT && "Invalid anyext node, dst < src!");
Chris Lattner4ed11b42005-09-02 00:17:32 +00001134 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND)
1135 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
1136 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1137 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001138 case ISD::TRUNCATE:
1139 if (Operand.getValueType() == VT) return Operand; // noop truncate
Nate Begemanb0d04a72006-02-18 02:40:58 +00001140 assert(Operand.getValueType() > VT && "Invalid truncate node, src < dst!");
Chris Lattnerc3aae252005-01-07 07:46:32 +00001141 if (OpOpcode == ISD::TRUNCATE)
1142 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
Chris Lattner4ed11b42005-09-02 00:17:32 +00001143 else if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
1144 OpOpcode == ISD::ANY_EXTEND) {
Chris Lattnerfd8c39b2005-01-07 21:56:24 +00001145 // If the source is smaller than the dest, we still need an extend.
1146 if (Operand.Val->getOperand(0).getValueType() < VT)
1147 return getNode(OpOpcode, VT, Operand.Val->getOperand(0));
1148 else if (Operand.Val->getOperand(0).getValueType() > VT)
1149 return getNode(ISD::TRUNCATE, VT, Operand.Val->getOperand(0));
1150 else
1151 return Operand.Val->getOperand(0);
1152 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001153 break;
Chris Lattner35481892005-12-23 00:16:34 +00001154 case ISD::BIT_CONVERT:
1155 // Basic sanity checking.
Chris Lattner1c6191f2006-03-21 19:20:37 +00001156 assert((Operand.getValueType() == MVT::Vector || // FIXME: This is a hack.
1157 MVT::getSizeInBits(VT) == MVT::getSizeInBits(Operand.getValueType()))
Chris Lattner28b5b1c2006-03-15 22:19:46 +00001158 && "Cannot BIT_CONVERT between two different types!");
Chris Lattner35481892005-12-23 00:16:34 +00001159 if (VT == Operand.getValueType()) return Operand; // noop conversion.
Chris Lattnerc8547d82005-12-23 05:37:50 +00001160 if (OpOpcode == ISD::BIT_CONVERT) // bitconv(bitconv(x)) -> bitconv(x)
1161 return getNode(ISD::BIT_CONVERT, VT, Operand.getOperand(0));
Chris Lattner35481892005-12-23 00:16:34 +00001162 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001163 case ISD::SCALAR_TO_VECTOR:
1164 assert(MVT::isVector(VT) && !MVT::isVector(Operand.getValueType()) &&
1165 MVT::getVectorBaseType(VT) == Operand.getValueType() &&
1166 "Illegal SCALAR_TO_VECTOR node!");
1167 break;
Chris Lattner485df9b2005-04-09 03:02:46 +00001168 case ISD::FNEG:
Chris Lattner01b3d732005-09-28 22:28:18 +00001169 if (OpOpcode == ISD::FSUB) // -(X-Y) -> (Y-X)
1170 return getNode(ISD::FSUB, VT, Operand.Val->getOperand(1),
Chris Lattner485df9b2005-04-09 03:02:46 +00001171 Operand.Val->getOperand(0));
1172 if (OpOpcode == ISD::FNEG) // --X -> X
1173 return Operand.Val->getOperand(0);
1174 break;
1175 case ISD::FABS:
1176 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
1177 return getNode(ISD::FABS, VT, Operand.Val->getOperand(0));
1178 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001179 }
1180
Chris Lattner43247a12005-08-25 19:12:10 +00001181 SDNode *N;
1182 if (VT != MVT::Flag) { // Don't CSE flag producing nodes
1183 SDNode *&E = UnaryOps[std::make_pair(Opcode, std::make_pair(Operand, VT))];
Chris Lattnerf07d0232005-08-26 00:13:12 +00001184 if (E) return SDOperand(E, 0);
Chris Lattner43247a12005-08-25 19:12:10 +00001185 E = N = new SDNode(Opcode, Operand);
1186 } else {
1187 N = new SDNode(Opcode, Operand);
1188 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001189 N->setValueTypes(VT);
1190 AllNodes.push_back(N);
1191 return SDOperand(N, 0);
Chris Lattner78ec3112003-08-11 14:57:33 +00001192}
1193
Chris Lattner36019aa2005-04-18 03:48:41 +00001194
1195
Chris Lattnerc3aae252005-01-07 07:46:32 +00001196SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1197 SDOperand N1, SDOperand N2) {
Chris Lattner76365122005-01-16 02:23:22 +00001198#ifndef NDEBUG
1199 switch (Opcode) {
Chris Lattner39908e02005-01-19 18:01:40 +00001200 case ISD::TokenFactor:
1201 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
1202 N2.getValueType() == MVT::Other && "Invalid token factor!");
1203 break;
Chris Lattner76365122005-01-16 02:23:22 +00001204 case ISD::AND:
1205 case ISD::OR:
1206 case ISD::XOR:
1207 case ISD::UDIV:
1208 case ISD::UREM:
Chris Lattnere5eb6f82005-05-15 05:39:08 +00001209 case ISD::MULHU:
1210 case ISD::MULHS:
Chris Lattner76365122005-01-16 02:23:22 +00001211 assert(MVT::isInteger(VT) && "This operator does not apply to FP types!");
1212 // fall through
1213 case ISD::ADD:
1214 case ISD::SUB:
1215 case ISD::MUL:
1216 case ISD::SDIV:
1217 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001218 assert(MVT::isInteger(N1.getValueType()) && "Should use F* for FP ops");
1219 // fall through.
1220 case ISD::FADD:
1221 case ISD::FSUB:
1222 case ISD::FMUL:
1223 case ISD::FDIV:
1224 case ISD::FREM:
Chris Lattner76365122005-01-16 02:23:22 +00001225 assert(N1.getValueType() == N2.getValueType() &&
1226 N1.getValueType() == VT && "Binary operator types must match!");
1227 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00001228 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
1229 assert(N1.getValueType() == VT &&
1230 MVT::isFloatingPoint(N1.getValueType()) &&
1231 MVT::isFloatingPoint(N2.getValueType()) &&
1232 "Invalid FCOPYSIGN!");
1233 break;
Chris Lattner76365122005-01-16 02:23:22 +00001234 case ISD::SHL:
1235 case ISD::SRA:
1236 case ISD::SRL:
Nate Begeman35ef9132006-01-11 21:21:00 +00001237 case ISD::ROTL:
1238 case ISD::ROTR:
Chris Lattner76365122005-01-16 02:23:22 +00001239 assert(VT == N1.getValueType() &&
1240 "Shift operators return type must be the same as their first arg");
1241 assert(MVT::isInteger(VT) && MVT::isInteger(N2.getValueType()) &&
Chris Lattner068a81e2005-01-17 17:15:02 +00001242 VT != MVT::i1 && "Shifts only work on integers");
Chris Lattner76365122005-01-16 02:23:22 +00001243 break;
Chris Lattner15e4b012005-07-10 00:07:11 +00001244 case ISD::FP_ROUND_INREG: {
1245 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1246 assert(VT == N1.getValueType() && "Not an inreg round!");
1247 assert(MVT::isFloatingPoint(VT) && MVT::isFloatingPoint(EVT) &&
1248 "Cannot FP_ROUND_INREG integer types");
1249 assert(EVT <= VT && "Not rounding down!");
1250 break;
1251 }
Nate Begeman56eb8682005-08-30 02:44:00 +00001252 case ISD::AssertSext:
1253 case ISD::AssertZext:
Chris Lattner15e4b012005-07-10 00:07:11 +00001254 case ISD::SIGN_EXTEND_INREG: {
1255 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1256 assert(VT == N1.getValueType() && "Not an inreg extend!");
1257 assert(MVT::isInteger(VT) && MVT::isInteger(EVT) &&
1258 "Cannot *_EXTEND_INREG FP types");
1259 assert(EVT <= VT && "Not extending!");
1260 }
1261
Chris Lattner76365122005-01-16 02:23:22 +00001262 default: break;
1263 }
1264#endif
1265
Chris Lattnerc3aae252005-01-07 07:46:32 +00001266 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1267 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1268 if (N1C) {
1269 if (N2C) {
1270 uint64_t C1 = N1C->getValue(), C2 = N2C->getValue();
1271 switch (Opcode) {
1272 case ISD::ADD: return getConstant(C1 + C2, VT);
1273 case ISD::SUB: return getConstant(C1 - C2, VT);
1274 case ISD::MUL: return getConstant(C1 * C2, VT);
1275 case ISD::UDIV:
1276 if (C2) return getConstant(C1 / C2, VT);
1277 break;
1278 case ISD::UREM :
1279 if (C2) return getConstant(C1 % C2, VT);
1280 break;
1281 case ISD::SDIV :
1282 if (C2) return getConstant(N1C->getSignExtended() /
1283 N2C->getSignExtended(), VT);
1284 break;
1285 case ISD::SREM :
1286 if (C2) return getConstant(N1C->getSignExtended() %
1287 N2C->getSignExtended(), VT);
1288 break;
1289 case ISD::AND : return getConstant(C1 & C2, VT);
1290 case ISD::OR : return getConstant(C1 | C2, VT);
1291 case ISD::XOR : return getConstant(C1 ^ C2, VT);
Nate Begemanb85dfab2005-08-31 00:27:53 +00001292 case ISD::SHL : return getConstant(C1 << C2, VT);
1293 case ISD::SRL : return getConstant(C1 >> C2, VT);
Chris Lattner8136d1f2005-01-10 00:07:15 +00001294 case ISD::SRA : return getConstant(N1C->getSignExtended() >>(int)C2, VT);
Nate Begeman35ef9132006-01-11 21:21:00 +00001295 case ISD::ROTL :
1296 return getConstant((C1 << C2) | (C1 >> (MVT::getSizeInBits(VT) - C2)),
1297 VT);
1298 case ISD::ROTR :
1299 return getConstant((C1 >> C2) | (C1 << (MVT::getSizeInBits(VT) - C2)),
1300 VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001301 default: break;
1302 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001303 } else { // Cannonicalize constant to RHS if commutative
1304 if (isCommutativeBinOp(Opcode)) {
1305 std::swap(N1C, N2C);
1306 std::swap(N1, N2);
1307 }
1308 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001309 }
1310
1311 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1.Val);
1312 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2.Val);
Chris Lattner15e4b012005-07-10 00:07:11 +00001313 if (N1CFP) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001314 if (N2CFP) {
1315 double C1 = N1CFP->getValue(), C2 = N2CFP->getValue();
1316 switch (Opcode) {
Chris Lattner01b3d732005-09-28 22:28:18 +00001317 case ISD::FADD: return getConstantFP(C1 + C2, VT);
1318 case ISD::FSUB: return getConstantFP(C1 - C2, VT);
1319 case ISD::FMUL: return getConstantFP(C1 * C2, VT);
1320 case ISD::FDIV:
Chris Lattnerc3aae252005-01-07 07:46:32 +00001321 if (C2) return getConstantFP(C1 / C2, VT);
1322 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00001323 case ISD::FREM :
Chris Lattnerc3aae252005-01-07 07:46:32 +00001324 if (C2) return getConstantFP(fmod(C1, C2), VT);
1325 break;
Chris Lattner3c232c82006-03-05 23:57:58 +00001326 case ISD::FCOPYSIGN: {
1327 union {
1328 double F;
1329 uint64_t I;
1330 } u1;
1331 union {
1332 double F;
1333 int64_t I;
1334 } u2;
1335 u1.F = C1;
1336 u2.F = C2;
1337 if (u2.I < 0) // Sign bit of RHS set?
1338 u1.I |= 1ULL << 63; // Set the sign bit of the LHS.
1339 else
1340 u1.I &= (1ULL << 63)-1; // Clear the sign bit of the LHS.
1341 return getConstantFP(u1.F, VT);
1342 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001343 default: break;
1344 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001345 } else { // Cannonicalize constant to RHS if commutative
1346 if (isCommutativeBinOp(Opcode)) {
1347 std::swap(N1CFP, N2CFP);
1348 std::swap(N1, N2);
1349 }
1350 }
Chris Lattner15e4b012005-07-10 00:07:11 +00001351 }
1352
Chris Lattnerc3aae252005-01-07 07:46:32 +00001353 // Finally, fold operations that do not require constants.
1354 switch (Opcode) {
Chris Lattner15e4b012005-07-10 00:07:11 +00001355 case ISD::FP_ROUND_INREG:
1356 if (cast<VTSDNode>(N2)->getVT() == VT) return N1; // Not actually rounding.
1357 break;
1358 case ISD::SIGN_EXTEND_INREG: {
1359 MVT::ValueType EVT = cast<VTSDNode>(N2)->getVT();
1360 if (EVT == VT) return N1; // Not actually extending
Chris Lattner15e4b012005-07-10 00:07:11 +00001361 break;
1362 }
1363
Nate Begemaneea805e2005-04-13 21:23:31 +00001364 // FIXME: figure out how to safely handle things like
1365 // int foo(int x) { return 1 << (x & 255); }
1366 // int bar() { return foo(256); }
1367#if 0
Nate Begemandb81eba2005-04-12 23:32:28 +00001368 case ISD::SHL:
1369 case ISD::SRL:
1370 case ISD::SRA:
Chris Lattnere666fcf2005-04-13 02:58:13 +00001371 if (N2.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001372 cast<VTSDNode>(N2.getOperand(1))->getVT() != MVT::i1)
Nate Begemandb81eba2005-04-12 23:32:28 +00001373 return getNode(Opcode, VT, N1, N2.getOperand(0));
Chris Lattnere666fcf2005-04-13 02:58:13 +00001374 else if (N2.getOpcode() == ISD::AND)
1375 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N2.getOperand(1))) {
1376 // If the and is only masking out bits that cannot effect the shift,
1377 // eliminate the and.
1378 unsigned NumBits = MVT::getSizeInBits(VT);
1379 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1380 return getNode(Opcode, VT, N1, N2.getOperand(0));
1381 }
Nate Begemandb81eba2005-04-12 23:32:28 +00001382 break;
Nate Begemaneea805e2005-04-13 21:23:31 +00001383#endif
Chris Lattnerc3aae252005-01-07 07:46:32 +00001384 }
1385
Chris Lattner27e9b412005-05-11 18:57:39 +00001386 // Memoize this node if possible.
1387 SDNode *N;
Chris Lattner70814bc2006-01-29 07:58:15 +00001388 if (VT != MVT::Flag) {
Chris Lattner27e9b412005-05-11 18:57:39 +00001389 SDNode *&BON = BinaryOps[std::make_pair(Opcode, std::make_pair(N1, N2))];
1390 if (BON) return SDOperand(BON, 0);
1391
1392 BON = N = new SDNode(Opcode, N1, N2);
1393 } else {
Chris Lattner88de6e72005-05-12 00:17:04 +00001394 N = new SDNode(Opcode, N1, N2);
Chris Lattner27e9b412005-05-11 18:57:39 +00001395 }
1396
Chris Lattner3e011362005-05-14 07:45:46 +00001397 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001398 AllNodes.push_back(N);
1399 return SDOperand(N, 0);
1400}
1401
Chris Lattnerc3aae252005-01-07 07:46:32 +00001402SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1403 SDOperand N1, SDOperand N2, SDOperand N3) {
1404 // Perform various simplifications.
1405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
1406 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
1407 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
1408 switch (Opcode) {
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001409 case ISD::SETCC: {
1410 // Use SimplifySetCC to simplify SETCC's.
Chris Lattnerbd8625b2005-08-09 23:09:05 +00001411 SDOperand Simp = SimplifySetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get());
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001412 if (Simp.Val) return Simp;
1413 break;
1414 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00001415 case ISD::SELECT:
1416 if (N1C)
1417 if (N1C->getValue())
1418 return N2; // select true, X, Y -> X
Misha Brukmanedf128a2005-04-21 22:36:52 +00001419 else
Chris Lattnerc3aae252005-01-07 07:46:32 +00001420 return N3; // select false, X, Y -> Y
1421
1422 if (N2 == N3) return N2; // select C, X, X -> X
Chris Lattnerc3aae252005-01-07 07:46:32 +00001423 break;
Chris Lattner5351e9b2005-01-07 22:49:57 +00001424 case ISD::BRCOND:
1425 if (N2C)
1426 if (N2C->getValue()) // Unconditional branch
1427 return getNode(ISD::BR, MVT::Other, N1, N3);
1428 else
1429 return N1; // Never-taken branch
Chris Lattner7c68ec62005-01-07 23:32:00 +00001430 break;
Chris Lattnerfb194b92006-03-19 23:56:04 +00001431 case ISD::VECTOR_SHUFFLE:
1432 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
1433 MVT::isVector(VT) && MVT::isVector(N3.getValueType()) &&
1434 N3.getOpcode() == ISD::BUILD_VECTOR &&
1435 MVT::getVectorNumElements(VT) == N3.getNumOperands() &&
1436 "Illegal VECTOR_SHUFFLE node!");
1437 break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001438 }
1439
Chris Lattner385328c2005-05-14 07:42:29 +00001440 std::vector<SDOperand> Ops;
1441 Ops.reserve(3);
1442 Ops.push_back(N1);
1443 Ops.push_back(N2);
1444 Ops.push_back(N3);
1445
Chris Lattner43247a12005-08-25 19:12:10 +00001446 // Memoize node if it doesn't produce a flag.
1447 SDNode *N;
1448 if (VT != MVT::Flag) {
1449 SDNode *&E = OneResultNodes[std::make_pair(Opcode,std::make_pair(VT, Ops))];
1450 if (E) return SDOperand(E, 0);
1451 E = N = new SDNode(Opcode, N1, N2, N3);
1452 } else {
1453 N = new SDNode(Opcode, N1, N2, N3);
1454 }
Chris Lattneradf6c2a2005-05-14 07:29:57 +00001455 N->setValueTypes(VT);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001456 AllNodes.push_back(N);
1457 return SDOperand(N, 0);
1458}
1459
1460SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Jeff Cohen00b168892005-07-27 06:12:32 +00001461 SDOperand N1, SDOperand N2, SDOperand N3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001462 SDOperand N4) {
Chris Lattnerb7f7d512005-05-14 07:32:14 +00001463 std::vector<SDOperand> Ops;
1464 Ops.reserve(4);
1465 Ops.push_back(N1);
1466 Ops.push_back(N2);
1467 Ops.push_back(N3);
1468 Ops.push_back(N4);
1469 return getNode(Opcode, VT, Ops);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001470}
1471
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001472SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
1473 SDOperand N1, SDOperand N2, SDOperand N3,
1474 SDOperand N4, SDOperand N5) {
1475 std::vector<SDOperand> Ops;
1476 Ops.reserve(5);
1477 Ops.push_back(N1);
1478 Ops.push_back(N2);
1479 Ops.push_back(N3);
1480 Ops.push_back(N4);
1481 Ops.push_back(N5);
1482 return getNode(Opcode, VT, Ops);
1483}
1484
Evan Cheng7038daf2005-12-10 00:37:58 +00001485SDOperand SelectionDAG::getLoad(MVT::ValueType VT,
1486 SDOperand Chain, SDOperand Ptr,
1487 SDOperand SV) {
1488 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, VT))];
1489 if (N) return SDOperand(N, 0);
1490 N = new SDNode(ISD::LOAD, Chain, Ptr, SV);
1491
1492 // Loads have a token chain.
1493 setNodeValueTypes(N, VT, MVT::Other);
1494 AllNodes.push_back(N);
1495 return SDOperand(N, 0);
1496}
1497
1498SDOperand SelectionDAG::getVecLoad(unsigned Count, MVT::ValueType EVT,
1499 SDOperand Chain, SDOperand Ptr,
1500 SDOperand SV) {
1501 SDNode *&N = Loads[std::make_pair(Ptr, std::make_pair(Chain, EVT))];
1502 if (N) return SDOperand(N, 0);
1503 std::vector<SDOperand> Ops;
1504 Ops.reserve(5);
Evan Cheng1ab7d852006-03-01 00:51:13 +00001505 Ops.push_back(Chain);
1506 Ops.push_back(Ptr);
Evan Cheng7038daf2005-12-10 00:37:58 +00001507 Ops.push_back(SV);
Chris Lattnerc7029802006-03-18 01:44:44 +00001508 Ops.push_back(getConstant(Count, MVT::i32));
1509 Ops.push_back(getValueType(EVT));
Evan Cheng7038daf2005-12-10 00:37:58 +00001510 std::vector<MVT::ValueType> VTs;
1511 VTs.reserve(2);
1512 VTs.push_back(MVT::Vector); VTs.push_back(MVT::Other); // Add token chain.
1513 return getNode(ISD::VLOAD, VTs, Ops);
1514}
1515
1516SDOperand SelectionDAG::getExtLoad(unsigned Opcode, MVT::ValueType VT,
1517 SDOperand Chain, SDOperand Ptr, SDOperand SV,
1518 MVT::ValueType EVT) {
1519 std::vector<SDOperand> Ops;
1520 Ops.reserve(4);
1521 Ops.push_back(Chain);
1522 Ops.push_back(Ptr);
1523 Ops.push_back(SV);
1524 Ops.push_back(getValueType(EVT));
1525 std::vector<MVT::ValueType> VTs;
1526 VTs.reserve(2);
1527 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1528 return getNode(Opcode, VTs, Ops);
1529}
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001530
Chris Lattner0437cdd2005-05-09 04:14:13 +00001531SDOperand SelectionDAG::getSrcValue(const Value *V, int Offset) {
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001532 assert((!V || isa<PointerType>(V->getType())) &&
1533 "SrcValue is not a pointer?");
Chris Lattner0437cdd2005-05-09 04:14:13 +00001534 SDNode *&N = ValueNodes[std::make_pair(V, Offset)];
1535 if (N) return SDOperand(N, 0);
1536
1537 N = new SrcValueSDNode(V, Offset);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001538 AllNodes.push_back(N);
1539 return SDOperand(N, 0);
1540}
1541
Nate Begemanacc398c2006-01-25 18:21:52 +00001542SDOperand SelectionDAG::getVAArg(MVT::ValueType VT,
1543 SDOperand Chain, SDOperand Ptr,
1544 SDOperand SV) {
1545 std::vector<SDOperand> Ops;
1546 Ops.reserve(3);
1547 Ops.push_back(Chain);
1548 Ops.push_back(Ptr);
1549 Ops.push_back(SV);
1550 std::vector<MVT::ValueType> VTs;
1551 VTs.reserve(2);
1552 VTs.push_back(VT); VTs.push_back(MVT::Other); // Add token chain.
1553 return getNode(ISD::VAARG, VTs, Ops);
1554}
1555
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001556SDOperand SelectionDAG::getNode(unsigned Opcode, MVT::ValueType VT,
Chris Lattner89c34632005-05-14 06:20:26 +00001557 std::vector<SDOperand> &Ops) {
1558 switch (Ops.size()) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00001559 case 0: return getNode(Opcode, VT);
Chris Lattner89c34632005-05-14 06:20:26 +00001560 case 1: return getNode(Opcode, VT, Ops[0]);
1561 case 2: return getNode(Opcode, VT, Ops[0], Ops[1]);
1562 case 3: return getNode(Opcode, VT, Ops[0], Ops[1], Ops[2]);
Chris Lattneref847df2005-04-09 03:27:28 +00001563 default: break;
Chris Lattnerc3aae252005-01-07 07:46:32 +00001564 }
Chris Lattnerde202b32005-11-09 23:47:37 +00001565
Chris Lattner89c34632005-05-14 06:20:26 +00001566 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Ops[1].Val);
Chris Lattneref847df2005-04-09 03:27:28 +00001567 switch (Opcode) {
1568 default: break;
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001569 case ISD::TRUNCSTORE: {
1570 assert(Ops.size() == 5 && "TRUNCSTORE takes 5 operands!");
1571 MVT::ValueType EVT = cast<VTSDNode>(Ops[4])->getVT();
1572#if 0 // FIXME: If the target supports EVT natively, convert to a truncate/store
1573 // If this is a truncating store of a constant, convert to the desired type
1574 // and store it instead.
1575 if (isa<Constant>(Ops[0])) {
1576 SDOperand Op = getNode(ISD::TRUNCATE, EVT, N1);
1577 if (isa<Constant>(Op))
1578 N1 = Op;
1579 }
1580 // Also for ConstantFP?
1581#endif
1582 if (Ops[0].getValueType() == EVT) // Normal store?
1583 return getNode(ISD::STORE, VT, Ops[0], Ops[1], Ops[2], Ops[3]);
1584 assert(Ops[1].getValueType() > EVT && "Not a truncation?");
1585 assert(MVT::isInteger(Ops[1].getValueType()) == MVT::isInteger(EVT) &&
1586 "Can't do FP-INT conversion!");
1587 break;
1588 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00001589 case ISD::SELECT_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001590 assert(Ops.size() == 5 && "SELECT_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001591 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
1592 "LHS and RHS of condition must have same type!");
1593 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1594 "True and False arms of SelectCC must have same type!");
1595 assert(Ops[2].getValueType() == VT &&
1596 "select_cc node must be of same type as true and false value!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001597 break;
1598 }
1599 case ISD::BR_CC: {
Chris Lattnerad137152005-10-05 06:37:22 +00001600 assert(Ops.size() == 5 && "BR_CC takes 5 operands!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001601 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
1602 "LHS/RHS of comparison should match types!");
Chris Lattner7b2880c2005-08-24 22:44:39 +00001603 break;
1604 }
Chris Lattneref847df2005-04-09 03:27:28 +00001605 }
1606
Chris Lattner385328c2005-05-14 07:42:29 +00001607 // Memoize nodes.
Chris Lattner43247a12005-08-25 19:12:10 +00001608 SDNode *N;
1609 if (VT != MVT::Flag) {
1610 SDNode *&E =
1611 OneResultNodes[std::make_pair(Opcode, std::make_pair(VT, Ops))];
1612 if (E) return SDOperand(E, 0);
1613 E = N = new SDNode(Opcode, Ops);
1614 } else {
1615 N = new SDNode(Opcode, Ops);
1616 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001617 N->setValueTypes(VT);
Chris Lattneref847df2005-04-09 03:27:28 +00001618 AllNodes.push_back(N);
1619 return SDOperand(N, 0);
Chris Lattnerc3aae252005-01-07 07:46:32 +00001620}
1621
Chris Lattner89c34632005-05-14 06:20:26 +00001622SDOperand SelectionDAG::getNode(unsigned Opcode,
1623 std::vector<MVT::ValueType> &ResultTys,
1624 std::vector<SDOperand> &Ops) {
1625 if (ResultTys.size() == 1)
1626 return getNode(Opcode, ResultTys[0], Ops);
1627
Chris Lattner5f056bf2005-07-10 01:55:33 +00001628 switch (Opcode) {
1629 case ISD::EXTLOAD:
1630 case ISD::SEXTLOAD:
1631 case ISD::ZEXTLOAD: {
1632 MVT::ValueType EVT = cast<VTSDNode>(Ops[3])->getVT();
1633 assert(Ops.size() == 4 && ResultTys.size() == 2 && "Bad *EXTLOAD!");
1634 // If they are asking for an extending load from/to the same thing, return a
1635 // normal load.
1636 if (ResultTys[0] == EVT)
1637 return getLoad(ResultTys[0], Ops[0], Ops[1], Ops[2]);
Chris Lattnerce872152006-03-19 06:31:19 +00001638 if (MVT::isVector(ResultTys[0])) {
1639 assert(EVT == MVT::getVectorBaseType(ResultTys[0]) &&
1640 "Invalid vector extload!");
1641 } else {
1642 assert(EVT < ResultTys[0] &&
1643 "Should only be an extending load, not truncating!");
1644 }
Chris Lattner5f056bf2005-07-10 01:55:33 +00001645 assert((Opcode == ISD::EXTLOAD || MVT::isInteger(ResultTys[0])) &&
Chris Lattnerce872152006-03-19 06:31:19 +00001646 "Cannot sign/zero extend a FP/Vector load!");
Chris Lattner5f056bf2005-07-10 01:55:33 +00001647 assert(MVT::isInteger(ResultTys[0]) == MVT::isInteger(EVT) &&
1648 "Cannot convert from FP to Int or Int -> FP!");
1649 break;
1650 }
1651
Chris Lattnere89083a2005-05-14 07:25:05 +00001652 // FIXME: figure out how to safely handle things like
1653 // int foo(int x) { return 1 << (x & 255); }
1654 // int bar() { return foo(256); }
1655#if 0
Chris Lattnere89083a2005-05-14 07:25:05 +00001656 case ISD::SRA_PARTS:
1657 case ISD::SRL_PARTS:
1658 case ISD::SHL_PARTS:
1659 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Chris Lattner15e4b012005-07-10 00:07:11 +00001660 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
Chris Lattnere89083a2005-05-14 07:25:05 +00001661 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1662 else if (N3.getOpcode() == ISD::AND)
1663 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
1664 // If the and is only masking out bits that cannot effect the shift,
1665 // eliminate the and.
1666 unsigned NumBits = MVT::getSizeInBits(VT)*2;
1667 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
1668 return getNode(Opcode, VT, N1, N2, N3.getOperand(0));
1669 }
1670 break;
Chris Lattnere89083a2005-05-14 07:25:05 +00001671#endif
Chris Lattner5f056bf2005-07-10 01:55:33 +00001672 }
Chris Lattner89c34632005-05-14 06:20:26 +00001673
Chris Lattner43247a12005-08-25 19:12:10 +00001674 // Memoize the node unless it returns a flag.
1675 SDNode *N;
1676 if (ResultTys.back() != MVT::Flag) {
1677 SDNode *&E =
1678 ArbitraryNodes[std::make_pair(Opcode, std::make_pair(ResultTys, Ops))];
1679 if (E) return SDOperand(E, 0);
1680 E = N = new SDNode(Opcode, Ops);
1681 } else {
1682 N = new SDNode(Opcode, Ops);
1683 }
Chris Lattnera3255112005-11-08 23:30:28 +00001684 setNodeValueTypes(N, ResultTys);
Chris Lattner5fa4fa42005-05-14 06:42:57 +00001685 AllNodes.push_back(N);
Chris Lattner89c34632005-05-14 06:20:26 +00001686 return SDOperand(N, 0);
1687}
1688
Chris Lattnera3255112005-11-08 23:30:28 +00001689void SelectionDAG::setNodeValueTypes(SDNode *N,
1690 std::vector<MVT::ValueType> &RetVals) {
1691 switch (RetVals.size()) {
1692 case 0: return;
1693 case 1: N->setValueTypes(RetVals[0]); return;
1694 case 2: setNodeValueTypes(N, RetVals[0], RetVals[1]); return;
1695 default: break;
1696 }
1697
1698 std::list<std::vector<MVT::ValueType> >::iterator I =
1699 std::find(VTList.begin(), VTList.end(), RetVals);
1700 if (I == VTList.end()) {
1701 VTList.push_front(RetVals);
1702 I = VTList.begin();
1703 }
1704
1705 N->setValueTypes(&(*I)[0], I->size());
1706}
1707
1708void SelectionDAG::setNodeValueTypes(SDNode *N, MVT::ValueType VT1,
1709 MVT::ValueType VT2) {
1710 for (std::list<std::vector<MVT::ValueType> >::iterator I = VTList.begin(),
1711 E = VTList.end(); I != E; ++I) {
1712 if (I->size() == 2 && (*I)[0] == VT1 && (*I)[1] == VT2) {
1713 N->setValueTypes(&(*I)[0], 2);
1714 return;
1715 }
1716 }
1717 std::vector<MVT::ValueType> V;
1718 V.push_back(VT1);
1719 V.push_back(VT2);
1720 VTList.push_front(V);
1721 N->setValueTypes(&(*VTList.begin())[0], 2);
1722}
1723
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001724/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
1725/// specified operands. If the resultant node already exists in the DAG,
1726/// this does not modify the specified node, instead it returns the node that
1727/// already exists. If the resultant node does not exist in the DAG, the
1728/// input node is returned. As a degenerate case, if you specify the same
1729/// input operands as the node already has, the input node is returned.
1730SDOperand SelectionDAG::
1731UpdateNodeOperands(SDOperand InN, SDOperand Op) {
1732 SDNode *N = InN.Val;
1733 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
1734
1735 // Check to see if there is no change.
1736 if (Op == N->getOperand(0)) return InN;
1737
1738 // See if the modified node already exists.
1739 SDNode **NewSlot = FindModifiedNodeSlot(N, Op);
1740 if (NewSlot && *NewSlot)
1741 return SDOperand(*NewSlot, InN.ResNo);
1742
1743 // Nope it doesn't. Remove the node from it's current place in the maps.
1744 if (NewSlot)
1745 RemoveNodeFromCSEMaps(N);
1746
1747 // Now we update the operands.
1748 N->OperandList[0].Val->removeUser(N);
1749 Op.Val->addUser(N);
1750 N->OperandList[0] = Op;
1751
1752 // If this gets put into a CSE map, add it.
1753 if (NewSlot) *NewSlot = N;
1754 return InN;
1755}
1756
1757SDOperand SelectionDAG::
1758UpdateNodeOperands(SDOperand InN, SDOperand Op1, SDOperand Op2) {
1759 SDNode *N = InN.Val;
1760 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
1761
1762 // Check to see if there is no change.
1763 bool AnyChange = false;
1764 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
1765 return InN; // No operands changed, just return the input node.
1766
1767 // See if the modified node already exists.
1768 SDNode **NewSlot = FindModifiedNodeSlot(N, Op1, Op2);
1769 if (NewSlot && *NewSlot)
1770 return SDOperand(*NewSlot, InN.ResNo);
1771
1772 // Nope it doesn't. Remove the node from it's current place in the maps.
1773 if (NewSlot)
1774 RemoveNodeFromCSEMaps(N);
1775
1776 // Now we update the operands.
1777 if (N->OperandList[0] != Op1) {
1778 N->OperandList[0].Val->removeUser(N);
1779 Op1.Val->addUser(N);
1780 N->OperandList[0] = Op1;
1781 }
1782 if (N->OperandList[1] != Op2) {
1783 N->OperandList[1].Val->removeUser(N);
1784 Op2.Val->addUser(N);
1785 N->OperandList[1] = Op2;
1786 }
1787
1788 // If this gets put into a CSE map, add it.
1789 if (NewSlot) *NewSlot = N;
1790 return InN;
1791}
1792
1793SDOperand SelectionDAG::
1794UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2, SDOperand Op3) {
1795 std::vector<SDOperand> Ops;
1796 Ops.push_back(Op1);
1797 Ops.push_back(Op2);
1798 Ops.push_back(Op3);
1799 return UpdateNodeOperands(N, Ops);
1800}
1801
1802SDOperand SelectionDAG::
1803UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1804 SDOperand Op3, SDOperand Op4) {
1805 std::vector<SDOperand> Ops;
1806 Ops.push_back(Op1);
1807 Ops.push_back(Op2);
1808 Ops.push_back(Op3);
1809 Ops.push_back(Op4);
1810 return UpdateNodeOperands(N, Ops);
1811}
1812
1813SDOperand SelectionDAG::
Chris Lattner809ec112006-01-28 10:09:25 +00001814UpdateNodeOperands(SDOperand N, SDOperand Op1, SDOperand Op2,
1815 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
1816 std::vector<SDOperand> Ops;
1817 Ops.push_back(Op1);
1818 Ops.push_back(Op2);
1819 Ops.push_back(Op3);
1820 Ops.push_back(Op4);
1821 Ops.push_back(Op5);
1822 return UpdateNodeOperands(N, Ops);
1823}
1824
1825
1826SDOperand SelectionDAG::
Chris Lattnerdf6eb302006-01-28 09:32:45 +00001827UpdateNodeOperands(SDOperand InN, const std::vector<SDOperand> &Ops) {
1828 SDNode *N = InN.Val;
1829 assert(N->getNumOperands() == Ops.size() &&
1830 "Update with wrong number of operands");
1831
1832 // Check to see if there is no change.
1833 unsigned NumOps = Ops.size();
1834 bool AnyChange = false;
1835 for (unsigned i = 0; i != NumOps; ++i) {
1836 if (Ops[i] != N->getOperand(i)) {
1837 AnyChange = true;
1838 break;
1839 }
1840 }
1841
1842 // No operands changed, just return the input node.
1843 if (!AnyChange) return InN;
1844
1845 // See if the modified node already exists.
1846 SDNode **NewSlot = FindModifiedNodeSlot(N, Ops);
1847 if (NewSlot && *NewSlot)
1848 return SDOperand(*NewSlot, InN.ResNo);
1849
1850 // Nope it doesn't. Remove the node from it's current place in the maps.
1851 if (NewSlot)
1852 RemoveNodeFromCSEMaps(N);
1853
1854 // Now we update the operands.
1855 for (unsigned i = 0; i != NumOps; ++i) {
1856 if (N->OperandList[i] != Ops[i]) {
1857 N->OperandList[i].Val->removeUser(N);
1858 Ops[i].Val->addUser(N);
1859 N->OperandList[i] = Ops[i];
1860 }
1861 }
1862
1863 // If this gets put into a CSE map, add it.
1864 if (NewSlot) *NewSlot = N;
1865 return InN;
1866}
1867
1868
1869
Chris Lattner149c58c2005-08-16 18:17:10 +00001870
1871/// SelectNodeTo - These are used for target selectors to *mutate* the
1872/// specified node to have the specified return type, Target opcode, and
1873/// operands. Note that target opcodes are stored as
1874/// ISD::BUILTIN_OP_END+TargetOpcode in the node opcode field.
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001875///
1876/// Note that SelectNodeTo returns the resultant node. If there is already a
1877/// node of the specified opcode and operands, it returns that node instead of
1878/// the current one.
Chris Lattnereb19e402005-11-30 22:45:14 +00001879SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1880 MVT::ValueType VT) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001881 // If an identical node already exists, use it.
1882 SDNode *&ON = NullaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc, VT)];
1883 if (ON) return SDOperand(ON, 0);
1884
Chris Lattner7651fa42005-08-24 23:00:29 +00001885 RemoveNodeFromCSEMaps(N);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001886
Chris Lattner7651fa42005-08-24 23:00:29 +00001887 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1888 N->setValueTypes(VT);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001889
1890 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001891 return SDOperand(N, 0);
Chris Lattner7651fa42005-08-24 23:00:29 +00001892}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001893
Chris Lattnereb19e402005-11-30 22:45:14 +00001894SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1895 MVT::ValueType VT, SDOperand Op1) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001896 // If an identical node already exists, use it.
1897 SDNode *&ON = UnaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1898 std::make_pair(Op1, VT))];
1899 if (ON) return SDOperand(ON, 0);
1900
Chris Lattner149c58c2005-08-16 18:17:10 +00001901 RemoveNodeFromCSEMaps(N);
1902 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1903 N->setValueTypes(VT);
1904 N->setOperands(Op1);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001905
1906 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001907 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001908}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001909
Chris Lattnereb19e402005-11-30 22:45:14 +00001910SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1911 MVT::ValueType VT, SDOperand Op1,
1912 SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001913 // If an identical node already exists, use it.
1914 SDNode *&ON = BinaryOps[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1915 std::make_pair(Op1, Op2))];
1916 if (ON) return SDOperand(ON, 0);
1917
Chris Lattner149c58c2005-08-16 18:17:10 +00001918 RemoveNodeFromCSEMaps(N);
1919 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1920 N->setValueTypes(VT);
1921 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001922
1923 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001924 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001925}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001926
Chris Lattnereb19e402005-11-30 22:45:14 +00001927SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1928 MVT::ValueType VT, SDOperand Op1,
1929 SDOperand Op2, SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001930 // If an identical node already exists, use it.
1931 std::vector<SDOperand> OpList;
1932 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1933 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1934 std::make_pair(VT, OpList))];
1935 if (ON) return SDOperand(ON, 0);
1936
Chris Lattner149c58c2005-08-16 18:17:10 +00001937 RemoveNodeFromCSEMaps(N);
1938 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1939 N->setValueTypes(VT);
1940 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001941
1942 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001943 return SDOperand(N, 0);
Chris Lattner149c58c2005-08-16 18:17:10 +00001944}
Chris Lattnerc975e1d2005-08-21 22:30:30 +00001945
Chris Lattnereb19e402005-11-30 22:45:14 +00001946SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1947 MVT::ValueType VT, SDOperand Op1,
1948 SDOperand Op2, SDOperand Op3,
1949 SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001950 // If an identical node already exists, use it.
1951 std::vector<SDOperand> OpList;
1952 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1953 OpList.push_back(Op4);
1954 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1955 std::make_pair(VT, OpList))];
1956 if (ON) return SDOperand(ON, 0);
1957
Nate Begeman294a0a12005-08-18 07:30:15 +00001958 RemoveNodeFromCSEMaps(N);
1959 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1960 N->setValueTypes(VT);
1961 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001962
1963 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001964 return SDOperand(N, 0);
Nate Begeman294a0a12005-08-18 07:30:15 +00001965}
Chris Lattner0fb094f2005-11-19 01:44:53 +00001966
Chris Lattnereb19e402005-11-30 22:45:14 +00001967SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1968 MVT::ValueType VT, SDOperand Op1,
1969 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1970 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001971 // If an identical node already exists, use it.
1972 std::vector<SDOperand> OpList;
1973 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1974 OpList.push_back(Op4); OpList.push_back(Op5);
1975 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1976 std::make_pair(VT, OpList))];
1977 if (ON) return SDOperand(ON, 0);
1978
Chris Lattner6b09a292005-08-21 18:49:33 +00001979 RemoveNodeFromCSEMaps(N);
1980 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
1981 N->setValueTypes(VT);
1982 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001983
1984 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00001985 return SDOperand(N, 0);
Chris Lattner6b09a292005-08-21 18:49:33 +00001986}
Chris Lattner149c58c2005-08-16 18:17:10 +00001987
Chris Lattnereb19e402005-11-30 22:45:14 +00001988SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
1989 MVT::ValueType VT, SDOperand Op1,
1990 SDOperand Op2, SDOperand Op3,SDOperand Op4,
1991 SDOperand Op5, SDOperand Op6) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00001992 // If an identical node already exists, use it.
1993 std::vector<SDOperand> OpList;
1994 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
1995 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
1996 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
1997 std::make_pair(VT, OpList))];
1998 if (ON) return SDOperand(ON, 0);
1999
Evan Cheng61ca74b2005-11-30 02:04:11 +00002000 RemoveNodeFromCSEMaps(N);
2001 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2002 N->setValueTypes(VT);
2003 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002004
2005 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002006 return SDOperand(N, 0);
Evan Cheng61ca74b2005-11-30 02:04:11 +00002007}
2008
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002009SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2010 MVT::ValueType VT, SDOperand Op1,
2011 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2012 SDOperand Op5, SDOperand Op6,
2013 SDOperand Op7) {
2014 // If an identical node already exists, use it.
2015 std::vector<SDOperand> OpList;
2016 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2017 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2018 OpList.push_back(Op7);
2019 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2020 std::make_pair(VT, OpList))];
2021 if (ON) return SDOperand(ON, 0);
2022
2023 RemoveNodeFromCSEMaps(N);
2024 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2025 N->setValueTypes(VT);
2026 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7);
2027
2028 ON = N; // Memoize the new node.
2029 return SDOperand(N, 0);
2030}
Andrew Lenharth7cf11b42006-01-23 21:51:14 +00002031SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2032 MVT::ValueType VT, SDOperand Op1,
2033 SDOperand Op2, SDOperand Op3,SDOperand Op4,
2034 SDOperand Op5, SDOperand Op6,
2035 SDOperand Op7, SDOperand Op8) {
2036 // If an identical node already exists, use it.
2037 std::vector<SDOperand> OpList;
2038 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2039 OpList.push_back(Op4); OpList.push_back(Op5); OpList.push_back(Op6);
2040 OpList.push_back(Op7); OpList.push_back(Op8);
2041 SDNode *&ON = OneResultNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2042 std::make_pair(VT, OpList))];
2043 if (ON) return SDOperand(ON, 0);
2044
2045 RemoveNodeFromCSEMaps(N);
2046 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2047 N->setValueTypes(VT);
2048 N->setOperands(Op1, Op2, Op3, Op4, Op5, Op6, Op7, Op8);
2049
2050 ON = N; // Memoize the new node.
2051 return SDOperand(N, 0);
2052}
Andrew Lenharth8c6f1ee2006-01-23 20:59:12 +00002053
Chris Lattnereb19e402005-11-30 22:45:14 +00002054SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2055 MVT::ValueType VT1, MVT::ValueType VT2,
2056 SDOperand Op1, SDOperand Op2) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002057 // If an identical node already exists, use it.
2058 std::vector<SDOperand> OpList;
2059 OpList.push_back(Op1); OpList.push_back(Op2);
2060 std::vector<MVT::ValueType> VTList;
2061 VTList.push_back(VT1); VTList.push_back(VT2);
2062 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2063 std::make_pair(VTList, OpList))];
2064 if (ON) return SDOperand(ON, 0);
2065
Chris Lattner0fb094f2005-11-19 01:44:53 +00002066 RemoveNodeFromCSEMaps(N);
2067 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2068 setNodeValueTypes(N, VT1, VT2);
2069 N->setOperands(Op1, Op2);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002070
2071 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002072 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002073}
2074
Chris Lattnereb19e402005-11-30 22:45:14 +00002075SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2076 MVT::ValueType VT1, MVT::ValueType VT2,
2077 SDOperand Op1, SDOperand Op2,
2078 SDOperand Op3) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002079 // If an identical node already exists, use it.
2080 std::vector<SDOperand> OpList;
2081 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2082 std::vector<MVT::ValueType> VTList;
2083 VTList.push_back(VT1); VTList.push_back(VT2);
2084 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2085 std::make_pair(VTList, OpList))];
2086 if (ON) return SDOperand(ON, 0);
2087
Chris Lattner0fb094f2005-11-19 01:44:53 +00002088 RemoveNodeFromCSEMaps(N);
2089 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2090 setNodeValueTypes(N, VT1, VT2);
2091 N->setOperands(Op1, Op2, Op3);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002092
2093 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002094 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002095}
2096
Chris Lattnereb19e402005-11-30 22:45:14 +00002097SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2098 MVT::ValueType VT1, MVT::ValueType VT2,
2099 SDOperand Op1, SDOperand Op2,
2100 SDOperand Op3, SDOperand Op4) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002101 // If an identical node already exists, use it.
2102 std::vector<SDOperand> OpList;
2103 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2104 OpList.push_back(Op4);
2105 std::vector<MVT::ValueType> VTList;
2106 VTList.push_back(VT1); VTList.push_back(VT2);
2107 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2108 std::make_pair(VTList, OpList))];
2109 if (ON) return SDOperand(ON, 0);
2110
Chris Lattner0fb094f2005-11-19 01:44:53 +00002111 RemoveNodeFromCSEMaps(N);
2112 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2113 setNodeValueTypes(N, VT1, VT2);
2114 N->setOperands(Op1, Op2, Op3, Op4);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002115
2116 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002117 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002118}
2119
Chris Lattnereb19e402005-11-30 22:45:14 +00002120SDOperand SelectionDAG::SelectNodeTo(SDNode *N, unsigned TargetOpc,
2121 MVT::ValueType VT1, MVT::ValueType VT2,
2122 SDOperand Op1, SDOperand Op2,
2123 SDOperand Op3, SDOperand Op4,
2124 SDOperand Op5) {
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002125 // If an identical node already exists, use it.
2126 std::vector<SDOperand> OpList;
2127 OpList.push_back(Op1); OpList.push_back(Op2); OpList.push_back(Op3);
2128 OpList.push_back(Op4); OpList.push_back(Op5);
2129 std::vector<MVT::ValueType> VTList;
2130 VTList.push_back(VT1); VTList.push_back(VT2);
2131 SDNode *&ON = ArbitraryNodes[std::make_pair(ISD::BUILTIN_OP_END+TargetOpc,
2132 std::make_pair(VTList, OpList))];
2133 if (ON) return SDOperand(ON, 0);
2134
Chris Lattner0fb094f2005-11-19 01:44:53 +00002135 RemoveNodeFromCSEMaps(N);
2136 N->MorphNodeTo(ISD::BUILTIN_OP_END+TargetOpc);
2137 setNodeValueTypes(N, VT1, VT2);
2138 N->setOperands(Op1, Op2, Op3, Op4, Op5);
Chris Lattnerc5e6c642005-12-01 18:00:57 +00002139
2140 ON = N; // Memoize the new node.
Chris Lattnereb19e402005-11-30 22:45:14 +00002141 return SDOperand(N, 0);
Chris Lattner0fb094f2005-11-19 01:44:53 +00002142}
2143
Evan Cheng6ae46c42006-02-09 07:15:23 +00002144/// getTargetNode - These are used for target selectors to create a new node
2145/// with specified return type(s), target opcode, and operands.
2146///
2147/// Note that getTargetNode returns the resultant node. If there is already a
2148/// node of the specified opcode and operands, it returns that node instead of
2149/// the current one.
2150SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT) {
2151 return getNode(ISD::BUILTIN_OP_END+Opcode, VT).Val;
2152}
2153SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2154 SDOperand Op1) {
2155 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1).Val;
2156}
2157SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2158 SDOperand Op1, SDOperand Op2) {
2159 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2).Val;
2160}
2161SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2162 SDOperand Op1, SDOperand Op2, SDOperand Op3) {
2163 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3).Val;
2164}
2165SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2166 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2167 SDOperand Op4) {
2168 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4).Val;
2169}
2170SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2171 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2172 SDOperand Op4, SDOperand Op5) {
2173 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Op1, Op2, Op3, Op4, Op5).Val;
2174}
2175SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2176 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2177 SDOperand Op4, SDOperand Op5, SDOperand Op6) {
2178 std::vector<SDOperand> Ops;
2179 Ops.reserve(6);
2180 Ops.push_back(Op1);
2181 Ops.push_back(Op2);
2182 Ops.push_back(Op3);
2183 Ops.push_back(Op4);
2184 Ops.push_back(Op5);
2185 Ops.push_back(Op6);
2186 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2187}
2188SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2189 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2190 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2191 SDOperand Op7) {
2192 std::vector<SDOperand> Ops;
2193 Ops.reserve(7);
2194 Ops.push_back(Op1);
2195 Ops.push_back(Op2);
2196 Ops.push_back(Op3);
2197 Ops.push_back(Op4);
2198 Ops.push_back(Op5);
2199 Ops.push_back(Op6);
2200 Ops.push_back(Op7);
2201 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2202}
2203SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2204 SDOperand Op1, SDOperand Op2, SDOperand Op3,
2205 SDOperand Op4, SDOperand Op5, SDOperand Op6,
2206 SDOperand Op7, SDOperand Op8) {
2207 std::vector<SDOperand> Ops;
2208 Ops.reserve(8);
2209 Ops.push_back(Op1);
2210 Ops.push_back(Op2);
2211 Ops.push_back(Op3);
2212 Ops.push_back(Op4);
2213 Ops.push_back(Op5);
2214 Ops.push_back(Op6);
2215 Ops.push_back(Op7);
2216 Ops.push_back(Op8);
2217 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2218}
2219SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT,
2220 std::vector<SDOperand> &Ops) {
2221 return getNode(ISD::BUILTIN_OP_END+Opcode, VT, Ops).Val;
2222}
2223SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2224 MVT::ValueType VT2, SDOperand Op1) {
2225 std::vector<MVT::ValueType> ResultTys;
2226 ResultTys.push_back(VT1);
2227 ResultTys.push_back(VT2);
2228 std::vector<SDOperand> Ops;
2229 Ops.push_back(Op1);
2230 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2231}
2232SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2233 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2) {
2234 std::vector<MVT::ValueType> ResultTys;
2235 ResultTys.push_back(VT1);
2236 ResultTys.push_back(VT2);
2237 std::vector<SDOperand> Ops;
2238 Ops.push_back(Op1);
2239 Ops.push_back(Op2);
2240 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2241}
2242SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2243 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2244 SDOperand Op3) {
2245 std::vector<MVT::ValueType> ResultTys;
2246 ResultTys.push_back(VT1);
2247 ResultTys.push_back(VT2);
2248 std::vector<SDOperand> Ops;
2249 Ops.push_back(Op1);
2250 Ops.push_back(Op2);
2251 Ops.push_back(Op3);
2252 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2253}
2254SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2255 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2256 SDOperand Op3, SDOperand Op4) {
2257 std::vector<MVT::ValueType> ResultTys;
2258 ResultTys.push_back(VT1);
2259 ResultTys.push_back(VT2);
2260 std::vector<SDOperand> Ops;
2261 Ops.push_back(Op1);
2262 Ops.push_back(Op2);
2263 Ops.push_back(Op3);
2264 Ops.push_back(Op4);
2265 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2266}
2267SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2268 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2269 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2270 std::vector<MVT::ValueType> ResultTys;
2271 ResultTys.push_back(VT1);
2272 ResultTys.push_back(VT2);
2273 std::vector<SDOperand> Ops;
2274 Ops.push_back(Op1);
2275 Ops.push_back(Op2);
2276 Ops.push_back(Op3);
2277 Ops.push_back(Op4);
2278 Ops.push_back(Op5);
2279 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2280}
2281SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2282 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2283 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2284 SDOperand Op6) {
2285 std::vector<MVT::ValueType> ResultTys;
2286 ResultTys.push_back(VT1);
2287 ResultTys.push_back(VT2);
2288 std::vector<SDOperand> Ops;
2289 Ops.push_back(Op1);
2290 Ops.push_back(Op2);
2291 Ops.push_back(Op3);
2292 Ops.push_back(Op4);
2293 Ops.push_back(Op5);
2294 Ops.push_back(Op6);
2295 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2296}
2297SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2298 MVT::ValueType VT2, SDOperand Op1, SDOperand Op2,
2299 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2300 SDOperand Op6, SDOperand Op7) {
2301 std::vector<MVT::ValueType> ResultTys;
2302 ResultTys.push_back(VT1);
2303 ResultTys.push_back(VT2);
2304 std::vector<SDOperand> Ops;
2305 Ops.push_back(Op1);
2306 Ops.push_back(Op2);
2307 Ops.push_back(Op3);
2308 Ops.push_back(Op4);
2309 Ops.push_back(Op5);
2310 Ops.push_back(Op6);
2311 Ops.push_back(Op7);
2312 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2313}
2314SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2315 MVT::ValueType VT2, MVT::ValueType VT3,
2316 SDOperand Op1, SDOperand Op2) {
2317 std::vector<MVT::ValueType> ResultTys;
2318 ResultTys.push_back(VT1);
2319 ResultTys.push_back(VT2);
2320 ResultTys.push_back(VT3);
2321 std::vector<SDOperand> Ops;
2322 Ops.push_back(Op1);
2323 Ops.push_back(Op2);
2324 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2325}
2326SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2327 MVT::ValueType VT2, MVT::ValueType VT3,
2328 SDOperand Op1, SDOperand Op2,
2329 SDOperand Op3, SDOperand Op4, SDOperand Op5) {
2330 std::vector<MVT::ValueType> ResultTys;
2331 ResultTys.push_back(VT1);
2332 ResultTys.push_back(VT2);
2333 ResultTys.push_back(VT3);
2334 std::vector<SDOperand> Ops;
2335 Ops.push_back(Op1);
2336 Ops.push_back(Op2);
2337 Ops.push_back(Op3);
2338 Ops.push_back(Op4);
2339 Ops.push_back(Op5);
2340 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2341}
2342SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2343 MVT::ValueType VT2, MVT::ValueType VT3,
2344 SDOperand Op1, SDOperand Op2,
2345 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2346 SDOperand Op6) {
2347 std::vector<MVT::ValueType> ResultTys;
2348 ResultTys.push_back(VT1);
2349 ResultTys.push_back(VT2);
2350 ResultTys.push_back(VT3);
2351 std::vector<SDOperand> Ops;
2352 Ops.push_back(Op1);
2353 Ops.push_back(Op2);
2354 Ops.push_back(Op3);
2355 Ops.push_back(Op4);
2356 Ops.push_back(Op5);
2357 Ops.push_back(Op6);
2358 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2359}
2360SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2361 MVT::ValueType VT2, MVT::ValueType VT3,
2362 SDOperand Op1, SDOperand Op2,
2363 SDOperand Op3, SDOperand Op4, SDOperand Op5,
2364 SDOperand Op6, SDOperand Op7) {
2365 std::vector<MVT::ValueType> ResultTys;
2366 ResultTys.push_back(VT1);
2367 ResultTys.push_back(VT2);
2368 ResultTys.push_back(VT3);
2369 std::vector<SDOperand> Ops;
2370 Ops.push_back(Op1);
2371 Ops.push_back(Op2);
2372 Ops.push_back(Op3);
2373 Ops.push_back(Op4);
2374 Ops.push_back(Op5);
2375 Ops.push_back(Op6);
2376 Ops.push_back(Op7);
2377 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2378}
2379SDNode *SelectionDAG::getTargetNode(unsigned Opcode, MVT::ValueType VT1,
2380 MVT::ValueType VT2, std::vector<SDOperand> &Ops) {
2381 std::vector<MVT::ValueType> ResultTys;
2382 ResultTys.push_back(VT1);
2383 ResultTys.push_back(VT2);
2384 return getNode(ISD::BUILTIN_OP_END+Opcode, ResultTys, Ops).Val;
2385}
2386
Chris Lattner0fb094f2005-11-19 01:44:53 +00002387// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
Chris Lattner8b8749f2005-08-17 19:00:20 +00002388/// This can cause recursive merging of nodes in the DAG.
2389///
Chris Lattner8b52f212005-08-26 18:36:28 +00002390/// This version assumes From/To have a single result value.
2391///
Chris Lattner1e111c72005-09-07 05:37:01 +00002392void SelectionDAG::ReplaceAllUsesWith(SDOperand FromN, SDOperand ToN,
2393 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002394 SDNode *From = FromN.Val, *To = ToN.Val;
2395 assert(From->getNumValues() == 1 && To->getNumValues() == 1 &&
2396 "Cannot replace with this method!");
Chris Lattner8b8749f2005-08-17 19:00:20 +00002397 assert(From != To && "Cannot replace uses of with self");
Chris Lattner8b52f212005-08-26 18:36:28 +00002398
Chris Lattner8b8749f2005-08-17 19:00:20 +00002399 while (!From->use_empty()) {
2400 // Process users until they are all gone.
2401 SDNode *U = *From->use_begin();
2402
2403 // This node is about to morph, remove its old self from the CSE maps.
2404 RemoveNodeFromCSEMaps(U);
2405
Chris Lattner65113b22005-11-08 22:07:03 +00002406 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2407 I != E; ++I)
2408 if (I->Val == From) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002409 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002410 I->Val = To;
Chris Lattner8b52f212005-08-26 18:36:28 +00002411 To->addUser(U);
2412 }
2413
2414 // Now that we have modified U, add it back to the CSE maps. If it already
2415 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002416 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2417 ReplaceAllUsesWith(U, Existing, Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002418 // U is now dead.
Chris Lattner1e111c72005-09-07 05:37:01 +00002419 if (Deleted) Deleted->push_back(U);
2420 DeleteNodeNotInCSEMaps(U);
2421 }
Chris Lattner8b52f212005-08-26 18:36:28 +00002422 }
2423}
2424
2425/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2426/// This can cause recursive merging of nodes in the DAG.
2427///
2428/// This version assumes From/To have matching types and numbers of result
2429/// values.
2430///
Chris Lattner1e111c72005-09-07 05:37:01 +00002431void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
2432 std::vector<SDNode*> *Deleted) {
Chris Lattner8b52f212005-08-26 18:36:28 +00002433 assert(From != To && "Cannot replace uses of with self");
2434 assert(From->getNumValues() == To->getNumValues() &&
2435 "Cannot use this version of ReplaceAllUsesWith!");
2436 if (From->getNumValues() == 1) { // If possible, use the faster version.
Chris Lattner1e111c72005-09-07 05:37:01 +00002437 ReplaceAllUsesWith(SDOperand(From, 0), SDOperand(To, 0), Deleted);
Chris Lattner8b52f212005-08-26 18:36:28 +00002438 return;
2439 }
2440
2441 while (!From->use_empty()) {
2442 // Process users until they are all gone.
2443 SDNode *U = *From->use_begin();
2444
2445 // This node is about to morph, remove its old self from the CSE maps.
2446 RemoveNodeFromCSEMaps(U);
2447
Chris Lattner65113b22005-11-08 22:07:03 +00002448 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2449 I != E; ++I)
2450 if (I->Val == From) {
Chris Lattner8b8749f2005-08-17 19:00:20 +00002451 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002452 I->Val = To;
Chris Lattner8b8749f2005-08-17 19:00:20 +00002453 To->addUser(U);
2454 }
2455
2456 // Now that we have modified U, add it back to the CSE maps. If it already
2457 // exists there, recursively merge the results together.
Chris Lattner1e111c72005-09-07 05:37:01 +00002458 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(U)) {
2459 ReplaceAllUsesWith(U, Existing, Deleted);
2460 // U is now dead.
2461 if (Deleted) Deleted->push_back(U);
2462 DeleteNodeNotInCSEMaps(U);
2463 }
Chris Lattner8b8749f2005-08-17 19:00:20 +00002464 }
2465}
2466
Chris Lattner8b52f212005-08-26 18:36:28 +00002467/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
2468/// This can cause recursive merging of nodes in the DAG.
2469///
2470/// This version can replace From with any result values. To must match the
2471/// number and types of values returned by From.
Chris Lattner7b2880c2005-08-24 22:44:39 +00002472void SelectionDAG::ReplaceAllUsesWith(SDNode *From,
Chris Lattner1e111c72005-09-07 05:37:01 +00002473 const std::vector<SDOperand> &To,
2474 std::vector<SDNode*> *Deleted) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002475 assert(From->getNumValues() == To.size() &&
2476 "Incorrect number of values to replace with!");
Chris Lattnerff016982005-08-28 23:59:36 +00002477 if (To.size() == 1 && To[0].Val->getNumValues() == 1) {
Chris Lattner7b2880c2005-08-24 22:44:39 +00002478 // Degenerate case handled above.
Chris Lattner1e111c72005-09-07 05:37:01 +00002479 ReplaceAllUsesWith(SDOperand(From, 0), To[0], Deleted);
Chris Lattner7b2880c2005-08-24 22:44:39 +00002480 return;
2481 }
2482
2483 while (!From->use_empty()) {
2484 // Process users until they are all gone.
2485 SDNode *U = *From->use_begin();
2486
2487 // This node is about to morph, remove its old self from the CSE maps.
2488 RemoveNodeFromCSEMaps(U);
2489
Chris Lattner65113b22005-11-08 22:07:03 +00002490 for (SDOperand *I = U->OperandList, *E = U->OperandList+U->NumOperands;
2491 I != E; ++I)
2492 if (I->Val == From) {
2493 const SDOperand &ToOp = To[I->ResNo];
Chris Lattner7b2880c2005-08-24 22:44:39 +00002494 From->removeUser(U);
Chris Lattner65113b22005-11-08 22:07:03 +00002495 *I = ToOp;
Chris Lattner7b2880c2005-08-24 22:44:39 +00002496 ToOp.Val->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);
2503 // U is now dead.
2504 if (Deleted) Deleted->push_back(U);
2505 DeleteNodeNotInCSEMaps(U);
2506 }
Chris Lattner7b2880c2005-08-24 22:44:39 +00002507 }
2508}
2509
Chris Lattner012f2412006-02-17 21:58:01 +00002510/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
2511/// uses of other values produced by From.Val alone. The Deleted vector is
2512/// handled the same was as for ReplaceAllUsesWith.
2513void SelectionDAG::ReplaceAllUsesOfValueWith(SDOperand From, SDOperand To,
2514 std::vector<SDNode*> &Deleted) {
2515 assert(From != To && "Cannot replace a value with itself");
2516 // Handle the simple, trivial, case efficiently.
2517 if (From.Val->getNumValues() == 1 && To.Val->getNumValues() == 1) {
2518 ReplaceAllUsesWith(From, To, &Deleted);
2519 return;
2520 }
2521
2522 // Get all of the users in a nice, deterministically ordered, uniqued set.
2523 SetVector<SDNode*> Users(From.Val->use_begin(), From.Val->use_end());
2524
2525 while (!Users.empty()) {
2526 // We know that this user uses some value of From. If it is the right
2527 // value, update it.
2528 SDNode *User = Users.back();
2529 Users.pop_back();
2530
2531 for (SDOperand *Op = User->OperandList,
2532 *E = User->OperandList+User->NumOperands; Op != E; ++Op) {
2533 if (*Op == From) {
2534 // Okay, we know this user needs to be updated. Remove its old self
2535 // from the CSE maps.
2536 RemoveNodeFromCSEMaps(User);
2537
2538 // Update all operands that match "From".
2539 for (; Op != E; ++Op) {
2540 if (*Op == From) {
2541 From.Val->removeUser(User);
2542 *Op = To;
2543 To.Val->addUser(User);
2544 }
2545 }
2546
2547 // Now that we have modified User, add it back to the CSE maps. If it
2548 // already exists there, recursively merge the results together.
2549 if (SDNode *Existing = AddNonLeafNodeToCSEMaps(User)) {
2550 unsigned NumDeleted = Deleted.size();
2551 ReplaceAllUsesWith(User, Existing, &Deleted);
2552
2553 // User is now dead.
2554 Deleted.push_back(User);
2555 DeleteNodeNotInCSEMaps(User);
2556
2557 // We have to be careful here, because ReplaceAllUsesWith could have
2558 // deleted a user of From, which means there may be dangling pointers
2559 // in the "Users" setvector. Scan over the deleted node pointers and
2560 // remove them from the setvector.
2561 for (unsigned i = NumDeleted, e = Deleted.size(); i != e; ++i)
2562 Users.remove(Deleted[i]);
2563 }
2564 break; // Exit the operand scanning loop.
2565 }
2566 }
2567 }
2568}
2569
Chris Lattner7b2880c2005-08-24 22:44:39 +00002570
Jim Laskey58b968b2005-08-17 20:08:02 +00002571//===----------------------------------------------------------------------===//
2572// SDNode Class
2573//===----------------------------------------------------------------------===//
Chris Lattner149c58c2005-08-16 18:17:10 +00002574
Chris Lattnera3255112005-11-08 23:30:28 +00002575
2576/// getValueTypeList - Return a pointer to the specified value type.
2577///
2578MVT::ValueType *SDNode::getValueTypeList(MVT::ValueType VT) {
2579 static MVT::ValueType VTs[MVT::LAST_VALUETYPE];
2580 VTs[VT] = VT;
2581 return &VTs[VT];
2582}
2583
Chris Lattner5c884562005-01-12 18:37:47 +00002584/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
2585/// indicated value. This method ignores uses of other values defined by this
2586/// operation.
Evan Cheng4ee62112006-02-05 06:29:23 +00002587bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
Chris Lattner5c884562005-01-12 18:37:47 +00002588 assert(Value < getNumValues() && "Bad value!");
2589
2590 // If there is only one value, this is easy.
2591 if (getNumValues() == 1)
2592 return use_size() == NUses;
2593 if (Uses.size() < NUses) return false;
2594
Evan Cheng4ee62112006-02-05 06:29:23 +00002595 SDOperand TheValue(const_cast<SDNode *>(this), Value);
Chris Lattner5c884562005-01-12 18:37:47 +00002596
2597 std::set<SDNode*> UsersHandled;
2598
Evan Cheng4ee62112006-02-05 06:29:23 +00002599 for (std::vector<SDNode*>::const_iterator UI = Uses.begin(), E = Uses.end();
Chris Lattner5c884562005-01-12 18:37:47 +00002600 UI != E; ++UI) {
2601 SDNode *User = *UI;
2602 if (User->getNumOperands() == 1 ||
2603 UsersHandled.insert(User).second) // First time we've seen this?
2604 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2605 if (User->getOperand(i) == TheValue) {
2606 if (NUses == 0)
2607 return false; // too many uses
2608 --NUses;
2609 }
2610 }
2611
2612 // Found exactly the right number of uses?
2613 return NUses == 0;
2614}
2615
2616
Evan Cheng4ee62112006-02-05 06:29:23 +00002617// isOnlyUse - Return true if this node is the only use of N.
2618bool SDNode::isOnlyUse(SDNode *N) const {
2619 bool Seen = false;
2620 for (SDNode::use_iterator I = N->use_begin(), E = N->use_end(); I != E; ++I) {
2621 SDNode *User = *I;
2622 if (User == this)
2623 Seen = true;
2624 else
2625 return false;
2626 }
2627
2628 return Seen;
2629}
2630
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002631// isOperand - Return true if this node is an operand of N.
Evan Chengbfa284f2006-03-03 06:42:32 +00002632bool SDOperand::isOperand(SDNode *N) const {
2633 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2634 if (*this == N->getOperand(i))
2635 return true;
2636 return false;
2637}
2638
Evan Cheng80d8eaa2006-03-03 06:24:54 +00002639bool SDNode::isOperand(SDNode *N) const {
2640 for (unsigned i = 0, e = N->NumOperands; i != e; ++i)
2641 if (this == N->OperandList[i].Val)
2642 return true;
2643 return false;
2644}
Evan Cheng4ee62112006-02-05 06:29:23 +00002645
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002646const char *SDNode::getOperationName(const SelectionDAG *G) const {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002647 switch (getOpcode()) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002648 default:
2649 if (getOpcode() < ISD::BUILTIN_OP_END)
2650 return "<<Unknown DAG Node>>";
2651 else {
Evan Cheng72261582005-12-20 06:22:03 +00002652 if (G) {
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002653 if (const TargetInstrInfo *TII = G->getTarget().getInstrInfo())
Chris Lattner08addbd2005-09-09 22:35:03 +00002654 if (getOpcode()-ISD::BUILTIN_OP_END < TII->getNumOpcodes())
2655 return TII->getName(getOpcode()-ISD::BUILTIN_OP_END);
Evan Cheng115c0362005-12-19 23:11:49 +00002656
Evan Cheng72261582005-12-20 06:22:03 +00002657 TargetLowering &TLI = G->getTargetLoweringInfo();
2658 const char *Name =
2659 TLI.getTargetNodeName(getOpcode());
2660 if (Name) return Name;
2661 }
2662
2663 return "<<Unknown Target Node>>";
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002664 }
2665
Andrew Lenharth95762122005-03-31 21:24:06 +00002666 case ISD::PCMARKER: return "PCMarker";
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002667 case ISD::READCYCLECOUNTER: return "ReadCycleCounter";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002668 case ISD::SRCVALUE: return "SrcValue";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002669 case ISD::EntryToken: return "EntryToken";
Chris Lattner282c5ca2005-01-13 17:59:10 +00002670 case ISD::TokenFactor: return "TokenFactor";
Nate Begeman56eb8682005-08-30 02:44:00 +00002671 case ISD::AssertSext: return "AssertSext";
2672 case ISD::AssertZext: return "AssertZext";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002673
2674 case ISD::STRING: return "String";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002675 case ISD::BasicBlock: return "BasicBlock";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002676 case ISD::VALUETYPE: return "ValueType";
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002677 case ISD::Register: return "Register";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002678
2679 case ISD::Constant: return "Constant";
2680 case ISD::ConstantFP: return "ConstantFP";
2681 case ISD::GlobalAddress: return "GlobalAddress";
2682 case ISD::FrameIndex: return "FrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002683 case ISD::ConstantPool: return "ConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002684 case ISD::ExternalSymbol: return "ExternalSymbol";
Chris Lattner70a248d2006-03-27 06:45:25 +00002685 case ISD::INTRINSIC:
2686 bool hasChain = getOperand(0).getValueType() == MVT::Other;
2687 unsigned IID = cast<ConstantSDNode>(getOperand(hasChain))->getValue();
2688 return Intrinsic::getName((Intrinsic::ID)IID);
Evan Cheng1ab7d852006-03-01 00:51:13 +00002689
Chris Lattnerb2827b02006-03-19 00:52:58 +00002690 case ISD::BUILD_VECTOR: return "BUILD_VECTOR";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002691 case ISD::TargetConstant: return "TargetConstant";
2692 case ISD::TargetConstantFP:return "TargetConstantFP";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002693 case ISD::TargetGlobalAddress: return "TargetGlobalAddress";
2694 case ISD::TargetFrameIndex: return "TargetFrameIndex";
Chris Lattner5839bf22005-08-26 17:15:30 +00002695 case ISD::TargetConstantPool: return "TargetConstantPool";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002696 case ISD::TargetExternalSymbol: return "TargetExternalSymbol";
Evan Cheng1ab7d852006-03-01 00:51:13 +00002697
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002698 case ISD::CopyToReg: return "CopyToReg";
2699 case ISD::CopyFromReg: return "CopyFromReg";
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002700 case ISD::UNDEF: return "undef";
Chris Lattnercc0aad22006-01-15 08:39:35 +00002701 case ISD::MERGE_VALUES: return "mergevalues";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002702 case ISD::INLINEASM: return "inlineasm";
Evan Cheng9fda2f92006-02-03 01:33:01 +00002703 case ISD::HANDLENODE: return "handlenode";
Chris Lattnerce7518c2006-01-26 22:24:51 +00002704
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002705 // Unary operators
2706 case ISD::FABS: return "fabs";
2707 case ISD::FNEG: return "fneg";
Chris Lattner7f644642005-04-28 21:44:03 +00002708 case ISD::FSQRT: return "fsqrt";
2709 case ISD::FSIN: return "fsin";
2710 case ISD::FCOS: return "fcos";
Chris Lattnerff9fd0a2005-04-02 04:58:41 +00002711
2712 // Binary operators
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002713 case ISD::ADD: return "add";
2714 case ISD::SUB: return "sub";
2715 case ISD::MUL: return "mul";
Nate Begeman18670542005-04-05 22:36:56 +00002716 case ISD::MULHU: return "mulhu";
2717 case ISD::MULHS: return "mulhs";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002718 case ISD::SDIV: return "sdiv";
2719 case ISD::UDIV: return "udiv";
2720 case ISD::SREM: return "srem";
2721 case ISD::UREM: return "urem";
2722 case ISD::AND: return "and";
2723 case ISD::OR: return "or";
2724 case ISD::XOR: return "xor";
2725 case ISD::SHL: return "shl";
2726 case ISD::SRA: return "sra";
2727 case ISD::SRL: return "srl";
Nate Begeman35ef9132006-01-11 21:21:00 +00002728 case ISD::ROTL: return "rotl";
2729 case ISD::ROTR: return "rotr";
Chris Lattner01b3d732005-09-28 22:28:18 +00002730 case ISD::FADD: return "fadd";
2731 case ISD::FSUB: return "fsub";
2732 case ISD::FMUL: return "fmul";
2733 case ISD::FDIV: return "fdiv";
2734 case ISD::FREM: return "frem";
Chris Lattnera09f8482006-03-05 05:09:38 +00002735 case ISD::FCOPYSIGN: return "fcopysign";
Nate Begeman5fbb5d22005-11-19 00:36:38 +00002736 case ISD::VADD: return "vadd";
2737 case ISD::VSUB: return "vsub";
2738 case ISD::VMUL: return "vmul";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002739
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002740 case ISD::SETCC: return "setcc";
2741 case ISD::SELECT: return "select";
Nate Begeman9373a812005-08-10 20:51:12 +00002742 case ISD::SELECT_CC: return "select_cc";
Chris Lattner0c486bd2006-03-17 19:53:59 +00002743 case ISD::INSERT_VECTOR_ELT: return "insert_vector_elt";
2744 case ISD::VINSERT_VECTOR_ELT: return "vinsert_vector_elt";
Chris Lattner384504c2006-03-21 20:44:12 +00002745 case ISD::EXTRACT_VECTOR_ELT: return "extract_vector_elt";
2746 case ISD::VEXTRACT_VECTOR_ELT: return "vextract_vector_elt";
Chris Lattnerce872152006-03-19 06:31:19 +00002747 case ISD::SCALAR_TO_VECTOR: return "scalar_to_vector";
Chris Lattnerfb194b92006-03-19 23:56:04 +00002748 case ISD::VBUILD_VECTOR: return "vbuild_vector";
2749 case ISD::VECTOR_SHUFFLE: return "vector_shuffle";
Chris Lattnere25ca692006-03-22 20:09:35 +00002750 case ISD::VBIT_CONVERT: return "vbit_convert";
Nate Begeman551bf3f2006-02-17 05:43:56 +00002751 case ISD::ADDC: return "addc";
2752 case ISD::ADDE: return "adde";
2753 case ISD::SUBC: return "subc";
2754 case ISD::SUBE: return "sube";
Chris Lattner41be9512005-04-02 03:30:42 +00002755 case ISD::SHL_PARTS: return "shl_parts";
2756 case ISD::SRA_PARTS: return "sra_parts";
2757 case ISD::SRL_PARTS: return "srl_parts";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002758
Chris Lattner7f644642005-04-28 21:44:03 +00002759 // Conversion operators.
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002760 case ISD::SIGN_EXTEND: return "sign_extend";
2761 case ISD::ZERO_EXTEND: return "zero_extend";
Chris Lattner4ed11b42005-09-02 00:17:32 +00002762 case ISD::ANY_EXTEND: return "any_extend";
Chris Lattner859157d2005-01-15 06:17:04 +00002763 case ISD::SIGN_EXTEND_INREG: return "sign_extend_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002764 case ISD::TRUNCATE: return "truncate";
2765 case ISD::FP_ROUND: return "fp_round";
Chris Lattner859157d2005-01-15 06:17:04 +00002766 case ISD::FP_ROUND_INREG: return "fp_round_inreg";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002767 case ISD::FP_EXTEND: return "fp_extend";
2768
2769 case ISD::SINT_TO_FP: return "sint_to_fp";
2770 case ISD::UINT_TO_FP: return "uint_to_fp";
2771 case ISD::FP_TO_SINT: return "fp_to_sint";
2772 case ISD::FP_TO_UINT: return "fp_to_uint";
Chris Lattner35481892005-12-23 00:16:34 +00002773 case ISD::BIT_CONVERT: return "bit_convert";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002774
2775 // Control flow instructions
2776 case ISD::BR: return "br";
2777 case ISD::BRCOND: return "brcond";
Nate Begeman81e80972006-03-17 01:40:33 +00002778 case ISD::BR_CC: return "br_cc";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002779 case ISD::RET: return "ret";
Chris Lattnera364fa12005-05-12 23:51:40 +00002780 case ISD::CALLSEQ_START: return "callseq_start";
2781 case ISD::CALLSEQ_END: return "callseq_end";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002782
2783 // Other operators
Nate Begemanacc398c2006-01-25 18:21:52 +00002784 case ISD::LOAD: return "load";
2785 case ISD::STORE: return "store";
2786 case ISD::VLOAD: return "vload";
2787 case ISD::EXTLOAD: return "extload";
2788 case ISD::SEXTLOAD: return "sextload";
2789 case ISD::ZEXTLOAD: return "zextload";
2790 case ISD::TRUNCSTORE: return "truncstore";
2791 case ISD::VAARG: return "vaarg";
2792 case ISD::VACOPY: return "vacopy";
2793 case ISD::VAEND: return "vaend";
2794 case ISD::VASTART: return "vastart";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002795 case ISD::DYNAMIC_STACKALLOC: return "dynamic_stackalloc";
Chris Lattner5a67afc2006-01-13 02:39:42 +00002796 case ISD::EXTRACT_ELEMENT: return "extract_element";
2797 case ISD::BUILD_PAIR: return "build_pair";
2798 case ISD::STACKSAVE: return "stacksave";
2799 case ISD::STACKRESTORE: return "stackrestore";
2800
2801 // Block memory operations.
Chris Lattner4c633e82005-01-11 05:57:01 +00002802 case ISD::MEMSET: return "memset";
2803 case ISD::MEMCPY: return "memcpy";
2804 case ISD::MEMMOVE: return "memmove";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002805
Nate Begeman1b5db7a2006-01-16 08:07:10 +00002806 // Bit manipulation
2807 case ISD::BSWAP: return "bswap";
Chris Lattner276260b2005-05-11 04:50:30 +00002808 case ISD::CTPOP: return "ctpop";
2809 case ISD::CTTZ: return "cttz";
2810 case ISD::CTLZ: return "ctlz";
2811
Chris Lattner36ce6912005-11-29 06:21:05 +00002812 // Debug info
2813 case ISD::LOCATION: return "location";
Jim Laskeyf5395ce2005-12-16 22:45:29 +00002814 case ISD::DEBUG_LOC: return "debug_loc";
Jim Laskeyabf6d172006-01-05 01:25:28 +00002815 case ISD::DEBUG_LABEL: return "debug_label";
Chris Lattner36ce6912005-11-29 06:21:05 +00002816
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002817 case ISD::CONDCODE:
2818 switch (cast<CondCodeSDNode>(this)->get()) {
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002819 default: assert(0 && "Unknown setcc condition!");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002820 case ISD::SETOEQ: return "setoeq";
2821 case ISD::SETOGT: return "setogt";
2822 case ISD::SETOGE: return "setoge";
2823 case ISD::SETOLT: return "setolt";
2824 case ISD::SETOLE: return "setole";
2825 case ISD::SETONE: return "setone";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002826
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002827 case ISD::SETO: return "seto";
2828 case ISD::SETUO: return "setuo";
2829 case ISD::SETUEQ: return "setue";
2830 case ISD::SETUGT: return "setugt";
2831 case ISD::SETUGE: return "setuge";
2832 case ISD::SETULT: return "setult";
2833 case ISD::SETULE: return "setule";
2834 case ISD::SETUNE: return "setune";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002835
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002836 case ISD::SETEQ: return "seteq";
2837 case ISD::SETGT: return "setgt";
2838 case ISD::SETGE: return "setge";
2839 case ISD::SETLT: return "setlt";
2840 case ISD::SETLE: return "setle";
2841 case ISD::SETNE: return "setne";
Chris Lattnerd75f19f2005-01-10 23:25:25 +00002842 }
2843 }
2844}
Chris Lattnerc3aae252005-01-07 07:46:32 +00002845
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002846void SDNode::dump() const { dump(0); }
2847void SDNode::dump(const SelectionDAG *G) const {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002848 std::cerr << (void*)this << ": ";
2849
2850 for (unsigned i = 0, e = getNumValues(); i != e; ++i) {
2851 if (i) std::cerr << ",";
Chris Lattner4ea69242005-01-15 07:14:32 +00002852 if (getValueType(i) == MVT::Other)
2853 std::cerr << "ch";
2854 else
2855 std::cerr << MVT::getValueTypeString(getValueType(i));
Chris Lattnerc3aae252005-01-07 07:46:32 +00002856 }
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002857 std::cerr << " = " << getOperationName(G);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002858
2859 std::cerr << " ";
2860 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2861 if (i) std::cerr << ", ";
2862 std::cerr << (void*)getOperand(i).Val;
2863 if (unsigned RN = getOperand(i).ResNo)
2864 std::cerr << ":" << RN;
2865 }
2866
2867 if (const ConstantSDNode *CSDN = dyn_cast<ConstantSDNode>(this)) {
2868 std::cerr << "<" << CSDN->getValue() << ">";
2869 } else if (const ConstantFPSDNode *CSDN = dyn_cast<ConstantFPSDNode>(this)) {
2870 std::cerr << "<" << CSDN->getValue() << ">";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002871 } else if (const GlobalAddressSDNode *GADN =
Chris Lattnerc3aae252005-01-07 07:46:32 +00002872 dyn_cast<GlobalAddressSDNode>(this)) {
Evan Cheng61ca74b2005-11-30 02:04:11 +00002873 int offset = GADN->getOffset();
Chris Lattnerc3aae252005-01-07 07:46:32 +00002874 std::cerr << "<";
2875 WriteAsOperand(std::cerr, GADN->getGlobal()) << ">";
Evan Cheng61ca74b2005-11-30 02:04:11 +00002876 if (offset > 0)
2877 std::cerr << " + " << offset;
2878 else
2879 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002880 } else if (const FrameIndexSDNode *FIDN = dyn_cast<FrameIndexSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002881 std::cerr << "<" << FIDN->getIndex() << ">";
2882 } else if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(this)){
Evan Cheng38b73272006-02-26 08:36:57 +00002883 int offset = CP->getOffset();
Chris Lattner5839bf22005-08-26 17:15:30 +00002884 std::cerr << "<" << *CP->get() << ">";
Evan Cheng38b73272006-02-26 08:36:57 +00002885 if (offset > 0)
2886 std::cerr << " + " << offset;
2887 else
2888 std::cerr << " " << offset;
Misha Brukmandedf2bd2005-04-22 04:01:18 +00002889 } else if (const BasicBlockSDNode *BBDN = dyn_cast<BasicBlockSDNode>(this)) {
Chris Lattnerc3aae252005-01-07 07:46:32 +00002890 std::cerr << "<";
2891 const Value *LBB = (const Value*)BBDN->getBasicBlock()->getBasicBlock();
2892 if (LBB)
2893 std::cerr << LBB->getName() << " ";
2894 std::cerr << (const void*)BBDN->getBasicBlock() << ">";
Chris Lattnerfa164b62005-08-19 21:34:13 +00002895 } else if (const RegisterSDNode *R = dyn_cast<RegisterSDNode>(this)) {
Evan Cheng140e99b2006-01-11 22:13:48 +00002896 if (G && R->getReg() && MRegisterInfo::isPhysicalRegister(R->getReg())) {
Chris Lattner7228aa72005-08-19 21:21:16 +00002897 std::cerr << " " <<G->getTarget().getRegisterInfo()->getName(R->getReg());
2898 } else {
2899 std::cerr << " #" << R->getReg();
2900 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002901 } else if (const ExternalSymbolSDNode *ES =
2902 dyn_cast<ExternalSymbolSDNode>(this)) {
2903 std::cerr << "'" << ES->getSymbol() << "'";
Chris Lattner2bf3c262005-05-09 04:08:27 +00002904 } else if (const SrcValueSDNode *M = dyn_cast<SrcValueSDNode>(this)) {
2905 if (M->getValue())
2906 std::cerr << "<" << M->getValue() << ":" << M->getOffset() << ">";
2907 else
2908 std::cerr << "<null:" << M->getOffset() << ">";
Chris Lattnera23e8152005-08-18 03:31:02 +00002909 } else if (const VTSDNode *N = dyn_cast<VTSDNode>(this)) {
2910 std::cerr << ":" << getValueTypeString(N->getVT());
Chris Lattnerc3aae252005-01-07 07:46:32 +00002911 }
Chris Lattnerc3aae252005-01-07 07:46:32 +00002912}
2913
Chris Lattnerde202b32005-11-09 23:47:37 +00002914static void DumpNodes(const SDNode *N, unsigned indent, const SelectionDAG *G) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002915 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
2916 if (N->getOperand(i).Val->hasOneUse())
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002917 DumpNodes(N->getOperand(i).Val, indent+2, G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002918 else
2919 std::cerr << "\n" << std::string(indent+2, ' ')
2920 << (void*)N->getOperand(i).Val << ": <multiple use>";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002921
Chris Lattnerea946cd2005-01-09 20:38:33 +00002922
2923 std::cerr << "\n" << std::string(indent, ' ');
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002924 N->dump(G);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002925}
2926
Chris Lattnerc3aae252005-01-07 07:46:32 +00002927void SelectionDAG::dump() const {
2928 std::cerr << "SelectionDAG has " << AllNodes.size() << " nodes:";
Chris Lattnerde202b32005-11-09 23:47:37 +00002929 std::vector<const SDNode*> Nodes;
2930 for (allnodes_const_iterator I = allnodes_begin(), E = allnodes_end();
2931 I != E; ++I)
2932 Nodes.push_back(I);
2933
Chris Lattner49d24712005-01-09 20:26:36 +00002934 std::sort(Nodes.begin(), Nodes.end());
2935
2936 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
Chris Lattnerea946cd2005-01-09 20:38:33 +00002937 if (!Nodes[i]->hasOneUse() && Nodes[i] != getRoot().Val)
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002938 DumpNodes(Nodes[i], 2, this);
Chris Lattnerc3aae252005-01-07 07:46:32 +00002939 }
Chris Lattnerea946cd2005-01-09 20:38:33 +00002940
Chris Lattnerf3e133a2005-08-16 18:33:07 +00002941 DumpNodes(getRoot().Val, 2, this);
Chris Lattnerea946cd2005-01-09 20:38:33 +00002942
Chris Lattnerc3aae252005-01-07 07:46:32 +00002943 std::cerr << "\n\n";
2944}
2945
Evan Chengfae9f1c2006-02-09 22:11:03 +00002946/// InsertISelMapEntry - A helper function to insert a key / element pair
2947/// into a SDOperand to SDOperand map. This is added to avoid the map
2948/// insertion operator from being inlined.
2949void SelectionDAG::InsertISelMapEntry(std::map<SDOperand, SDOperand> &Map,
2950 SDNode *Key, unsigned KeyResNo,
2951 SDNode *Element, unsigned ElementResNo) {
2952 Map.insert(std::make_pair(SDOperand(Key, KeyResNo),
2953 SDOperand(Element, ElementResNo)));
2954}